Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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