View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.junit.http;
12  
13  import java.text.MessageFormat;
14  import java.util.ArrayList;
15  import java.util.Collections;
16  import java.util.List;
17  
18  import org.eclipse.jetty.util.log.Logger;
19  
20  /**
21   * Log warnings into an array for later inspection.
22   */
23  public class RecordingLogger implements Logger {
24  	private static List<Warning> warnings = new ArrayList<>();
25  
26  	/**
27  	 * Clear the warnings, automatically done by
28  	 * {@link org.eclipse.jgit.junit.http.AppServer#setUp()}
29  	 */
30  	public static void clear() {
31  		synchronized (warnings) {
32  			warnings.clear();
33  		}
34  	}
35  
36  	/**
37  	 * Get the <code>warnings</code>.
38  	 *
39  	 * @return the warnings (if any) from the last execution
40  	 */
41  	public static List<Warning> getWarnings() {
42  		synchronized (warnings) {
43  			ArrayList<Warning> copy = new ArrayList<>(warnings);
44  			return Collections.unmodifiableList(copy);
45  		}
46  	}
47  
48  	@SuppressWarnings("serial")
49  	public static class Warning extends Exception {
50  		public Warning(String msg) {
51  			super(msg);
52  		}
53  
54  		public Warning(String msg, Throwable cause) {
55  			super(msg, cause);
56  		}
57  
58  		public Warning(Throwable thrown) {
59  			super(thrown);
60  		}
61  	}
62  
63  	private final String name;
64  
65  	/**
66  	 * Constructor for <code>RecordingLogger</code>.
67  	 */
68  	public RecordingLogger() {
69  		this("");
70  	}
71  
72  	/**
73  	 * Constructor for <code>RecordingLogger</code>.
74  	 *
75  	 * @param name
76  	 */
77  	public RecordingLogger(String name) {
78  		this.name = name;
79  	}
80  
81  	/** {@inheritDoc} */
82  	@Override
83  	public Logger getLogger(@SuppressWarnings("hiding") String name) {
84  		return new RecordingLogger(name);
85  	}
86  
87  	/** {@inheritDoc} */
88  	@Override
89  	public String getName() {
90  		return name;
91  	}
92  
93  	/**
94  	 * Warning
95  	 *
96  	 * @param msg
97  	 * @param arg0
98  	 * @param arg1
99  	 */
100 	public void warn(String msg, Object arg0, Object arg1) {
101 		synchronized (warnings) {
102 			warnings.add(new Warning(MessageFormat.format(msg, arg0, arg1)));
103 		}
104 	}
105 
106 	/** {@inheritDoc} */
107 	@Override
108 	public void warn(String msg, Throwable th) {
109 		synchronized (warnings) {
110 			warnings.add(new Warning(msg, th));
111 		}
112 	}
113 
114 	/**
115 	 * Warning
116 	 *
117 	 * @param msg
118 	 *            warning message
119 	 */
120 	public void warn(String msg) {
121 		synchronized (warnings) {
122 			warnings.add(new Warning(msg));
123 		}
124 	}
125 
126 	/**
127 	 * Debug log
128 	 *
129 	 * @param msg
130 	 * @param arg0
131 	 * @param arg1
132 	 */
133 	public void debug(String msg, Object arg0, Object arg1) {
134 		// Ignore (not relevant to test failures)
135 	}
136 
137 	/** {@inheritDoc} */
138 	@Override
139 	public void debug(String msg, Throwable th) {
140 		// Ignore (not relevant to test failures)
141 	}
142 
143 	/**
144 	 * Debug log
145 	 *
146 	 * @param msg
147 	 *            debug message
148 	 */
149 	public void debug(String msg) {
150 		// Ignore (not relevant to test failures)
151 	}
152 
153 	/**
154 	 * Info
155 	 *
156 	 * @param msg
157 	 * @param arg0
158 	 * @param arg1
159 	 */
160 	public void info(String msg, Object arg0, Object arg1) {
161 		// Ignore (not relevant to test failures)
162 	}
163 
164 	/**
165 	 * Info
166 	 *
167 	 * @param msg
168 	 */
169 	public void info(String msg) {
170 		// Ignore (not relevant to test failures)
171 	}
172 
173 	/** {@inheritDoc} */
174 	@Override
175 	public boolean isDebugEnabled() {
176 		return false;
177 	}
178 
179 	/** {@inheritDoc} */
180 	@Override
181 	public void setDebugEnabled(boolean enabled) {
182 		// Ignore (not relevant to test failures)
183 	}
184 
185 	/** {@inheritDoc} */
186 	@Override
187 	public void warn(String msg, Object... args) {
188 		synchronized (warnings) {
189 			int i = 0;
190 			int index = msg.indexOf("{}");
191 			while (index >= 0) {
192 				msg = msg.replaceFirst("\\{\\}", "{" + i++ + "}");
193 				index = msg.indexOf("{}");
194 			}
195 			warnings.add(new Warning(MessageFormat.format(msg, args)));
196 		}
197 	}
198 
199 	/** {@inheritDoc} */
200 	@Override
201 	public void warn(Throwable thrown) {
202 		synchronized (warnings) {
203 			warnings.add(new Warning(thrown));
204 		}
205 	}
206 
207 	/** {@inheritDoc} */
208 	@Override
209 	public void info(String msg, Object... args) {
210 		// Ignore (not relevant to test failures)
211 	}
212 
213 	/** {@inheritDoc} */
214 	@Override
215 	public void info(Throwable thrown) {
216 		// Ignore (not relevant to test failures)
217 	}
218 
219 	/** {@inheritDoc} */
220 	@Override
221 	public void info(String msg, Throwable thrown) {
222 		// Ignore (not relevant to test failures)
223 	}
224 
225 	/** {@inheritDoc} */
226 	@Override
227 	public void debug(String msg, Object... args) {
228 		// Ignore (not relevant to test failures)
229 	}
230 
231 	/** {@inheritDoc} */
232 	@Override
233 	public void debug(Throwable thrown) {
234 		// Ignore (not relevant to test failures)
235 	}
236 
237 	/** {@inheritDoc} */
238 	@Override
239 	public void ignore(Throwable arg0) {
240 		// Ignore (not relevant to test failures)
241 	}
242 
243 	/** {@inheritDoc} */
244 	@Override
245 	public void debug(String msg, long value) {
246 		// Ignore (not relevant to test failures)
247 	}
248 }