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 /**
54 * Log warnings into an array for later inspection.
55 */
56 public class RecordingLogger implements Logger {
57 private static List<Warning> warnings = new ArrayList<>();
58
59 /**
60 * Clear the warnings, automatically done by
61 * {@link org.eclipse.jgit.junit.http.AppServer#setUp()}
62 */
63 public static void clear() {
64 synchronized (warnings) {
65 warnings.clear();
66 }
67 }
68
69 /**
70 * Get the <code>warnings</code>.
71 *
72 * @return the warnings (if any) from the last execution
73 */
74 public static List<Warning> getWarnings() {
75 synchronized (warnings) {
76 ArrayList<Warning> copy = new ArrayList<>(warnings);
77 return Collections.unmodifiableList(copy);
78 }
79 }
80
81 @SuppressWarnings("serial")
82 public static class Warning extends Exception {
83 public Warning(String msg) {
84 super(msg);
85 }
86
87 public Warning(String msg, Throwable cause) {
88 super(msg, cause);
89 }
90
91 public Warning(Throwable thrown) {
92 super(thrown);
93 }
94 }
95
96 private final String name;
97
98 /**
99 * Constructor for <code>RecordingLogger</code>.
100 */
101 public RecordingLogger() {
102 this("");
103 }
104
105 /**
106 * Constructor for <code>RecordingLogger</code>.
107 *
108 * @param name
109 */
110 public RecordingLogger(String name) {
111 this.name = name;
112 }
113
114 /** {@inheritDoc} */
115 @Override
116 public Logger getLogger(@SuppressWarnings("hiding") String name) {
117 return new RecordingLogger(name);
118 }
119
120 /** {@inheritDoc} */
121 @Override
122 public String getName() {
123 return name;
124 }
125
126 /**
127 * Warning
128 *
129 * @param msg
130 * @param arg0
131 * @param arg1
132 */
133 public void warn(String msg, Object arg0, Object arg1) {
134 synchronized (warnings) {
135 warnings.add(new Warning(MessageFormat.format(msg, arg0, arg1)));
136 }
137 }
138
139 /** {@inheritDoc} */
140 @Override
141 public void warn(String msg, Throwable th) {
142 synchronized (warnings) {
143 warnings.add(new Warning(msg, th));
144 }
145 }
146
147 /**
148 * Warning
149 *
150 * @param msg
151 * warning message
152 */
153 public void warn(String msg) {
154 synchronized (warnings) {
155 warnings.add(new Warning(msg));
156 }
157 }
158
159 /**
160 * Debug log
161 *
162 * @param msg
163 * @param arg0
164 * @param arg1
165 */
166 public void debug(String msg, Object arg0, Object arg1) {
167 // Ignore (not relevant to test failures)
168 }
169
170 /** {@inheritDoc} */
171 @Override
172 public void debug(String msg, Throwable th) {
173 // Ignore (not relevant to test failures)
174 }
175
176 /**
177 * Debug log
178 *
179 * @param msg
180 * debug message
181 */
182 public void debug(String msg) {
183 // Ignore (not relevant to test failures)
184 }
185
186 /**
187 * Info
188 *
189 * @param msg
190 * @param arg0
191 * @param arg1
192 */
193 public void info(String msg, Object arg0, Object arg1) {
194 // Ignore (not relevant to test failures)
195 }
196
197 /**
198 * Info
199 *
200 * @param msg
201 */
202 public void info(String msg) {
203 // Ignore (not relevant to test failures)
204 }
205
206 /** {@inheritDoc} */
207 @Override
208 public boolean isDebugEnabled() {
209 return false;
210 }
211
212 /** {@inheritDoc} */
213 @Override
214 public void setDebugEnabled(boolean enabled) {
215 // Ignore (not relevant to test failures)
216 }
217
218 /** {@inheritDoc} */
219 @Override
220 public void warn(String msg, Object... args) {
221 synchronized (warnings) {
222 int i = 0;
223 int index = msg.indexOf("{}");
224 while (index >= 0) {
225 msg = msg.replaceFirst("\\{\\}", "{" + i++ + "}");
226 index = msg.indexOf("{}");
227 }
228 warnings.add(new Warning(MessageFormat.format(msg, args)));
229 }
230 }
231
232 /** {@inheritDoc} */
233 @Override
234 public void warn(Throwable thrown) {
235 synchronized (warnings) {
236 warnings.add(new Warning(thrown));
237 }
238 }
239
240 /** {@inheritDoc} */
241 @Override
242 public void info(String msg, Object... args) {
243 // Ignore (not relevant to test failures)
244 }
245
246 /** {@inheritDoc} */
247 @Override
248 public void info(Throwable thrown) {
249 // Ignore (not relevant to test failures)
250 }
251
252 /** {@inheritDoc} */
253 @Override
254 public void info(String msg, Throwable thrown) {
255 // Ignore (not relevant to test failures)
256 }
257
258 /** {@inheritDoc} */
259 @Override
260 public void debug(String msg, Object... args) {
261 // Ignore (not relevant to test failures)
262 }
263
264 /** {@inheritDoc} */
265 @Override
266 public void debug(Throwable thrown) {
267 // Ignore (not relevant to test failures)
268 }
269
270 /** {@inheritDoc} */
271 @Override
272 public void ignore(Throwable arg0) {
273 // Ignore (not relevant to test failures)
274 }
275
276 /** {@inheritDoc} */
277 @Override
278 public void debug(String msg, long value) {
279 // Ignore (not relevant to test failures)
280 }
281 }