View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.junit.http;
45  
46  import java.text.MessageFormat;
47  import java.util.ArrayList;
48  import java.util.Collections;
49  import java.util.List;
50  
51  import org.eclipse.jetty.util.log.Logger;
52  
53  /** Logs warnings into an array for later inspection. */
54  public class RecordingLogger implements Logger {
55  	private static List<Warning> warnings = new ArrayList<Warning>();
56  
57  	/** Clear the warnings, automatically done by {@link AppServer#setUp()} */
58  	public static void clear() {
59  		synchronized (warnings) {
60  			warnings.clear();
61  		}
62  	}
63  
64  	/** @return the warnings (if any) from the last execution */
65  	public static List<Warning> getWarnings() {
66  		synchronized (warnings) {
67  			ArrayList<Warning> copy = new ArrayList<Warning>(warnings);
68  			return Collections.unmodifiableList(copy);
69  		}
70  	}
71  
72  	@SuppressWarnings("serial")
73  	public static class Warning extends Exception {
74  		public Warning(String msg) {
75  			super(msg);
76  		}
77  
78  		public Warning(String msg, Throwable cause) {
79  			super(msg, cause);
80  		}
81  
82  		public Warning(Throwable thrown) {
83  			super(thrown);
84  		}
85  	}
86  
87  	private final String name;
88  
89  	public RecordingLogger() {
90  		this("");
91  	}
92  
93  	public RecordingLogger(final String name) {
94  		this.name = name;
95  	}
96  
97  	public Logger getLogger(@SuppressWarnings("hiding") String name) {
98  		return new RecordingLogger(name);
99  	}
100 
101 	public String getName() {
102 		return name;
103 	}
104 
105 	public void warn(String msg, Object arg0, Object arg1) {
106 		synchronized (warnings) {
107 			warnings.add(new Warning(MessageFormat.format(msg, arg0, arg1)));
108 		}
109 	}
110 
111 	public void warn(String msg, Throwable th) {
112 		synchronized (warnings) {
113 			warnings.add(new Warning(msg, th));
114 		}
115 	}
116 
117 	public void warn(String msg) {
118 		synchronized (warnings) {
119 			warnings.add(new Warning(msg));
120 		}
121 	}
122 
123 	public void debug(@SuppressWarnings("unused") String msg,
124 			          @SuppressWarnings("unused") Object arg0,
125 			          @SuppressWarnings("unused") Object arg1) {
126 		// Ignore (not relevant to test failures)
127 	}
128 
129 	public void debug(String msg, Throwable th) {
130 		// Ignore (not relevant to test failures)
131 	}
132 
133 	public void debug(@SuppressWarnings("unused") String msg) {
134 		// Ignore (not relevant to test failures)
135 	}
136 
137 	public void info(@SuppressWarnings("unused") String msg,
138 			         @SuppressWarnings("unused") Object arg0,
139 			         @SuppressWarnings("unused") Object arg1) {
140 		// Ignore (not relevant to test failures)
141 	}
142 
143 	public void info(@SuppressWarnings("unused") String msg) {
144 		// Ignore (not relevant to test failures)
145 	}
146 
147 	public boolean isDebugEnabled() {
148 		return false;
149 	}
150 
151 	public void setDebugEnabled(boolean enabled) {
152 		// Ignore (not relevant to test failures)
153 	}
154 
155 	public void warn(String msg, Object... args) {
156 		synchronized (warnings) {
157 			warnings.add(new Warning(MessageFormat.format(msg, args)));
158 		}
159 	}
160 
161 	public void warn(Throwable thrown) {
162 		synchronized (warnings) {
163 			warnings.add(new Warning(thrown));
164 		}
165 	}
166 
167 	public void info(String msg, Object... args) {
168 		// Ignore (not relevant to test failures)
169 	}
170 
171 	public void info(Throwable thrown) {
172 		// Ignore (not relevant to test failures)
173 	}
174 
175 	public void info(String msg, Throwable thrown) {
176 		// Ignore (not relevant to test failures)
177 	}
178 
179 	public void debug(String msg, Object... args) {
180 		// Ignore (not relevant to test failures)
181 	}
182 
183 	public void debug(Throwable thrown) {
184 		// Ignore (not relevant to test failures)
185 	}
186 
187 	public void ignore(Throwable arg0) {
188 		// Ignore (not relevant to test failures)
189 	}
190 
191 	@Override
192 	public void debug(String msg, long value) {
193 		// Ignore (not relevant to test failures)
194 	}
195 }