1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.shared.invoker;
20
21 import java.net.MalformedURLException;
22
23 import org.junit.jupiter.api.Test;
24
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28
29 class SystemOutLoggerTest {
30
31 private static final Throwable EXCEPTION =
32 new MalformedURLException("This is meant to happen. It's part of the test.");
33
34 private static final String MESSAGE = "This is a test message.";
35
36 @Test
37 void testDebugWithMessageOnly() {
38 logTestStart();
39 new SystemOutLogger().debug(MESSAGE);
40 }
41
42 @Test
43 void testDebugWithMessageAndError() {
44 logTestStart();
45 new SystemOutLogger().debug(MESSAGE, EXCEPTION);
46 }
47
48 @Test
49 void testDebugWithNullMessageAndNoError() {
50 logTestStart();
51 new SystemOutLogger().debug(null);
52 }
53
54 @Test
55 void testDebugWithNullMessageError() {
56 logTestStart();
57 new SystemOutLogger().debug(null, EXCEPTION);
58 }
59
60 @Test
61 void testDebugWithMessageNullError() {
62 logTestStart();
63 new SystemOutLogger().debug(MESSAGE, null);
64 }
65
66 @Test
67 void testInfoWithMessageOnly() {
68 logTestStart();
69 new SystemOutLogger().info(MESSAGE);
70 }
71
72 @Test
73 void testInfoWithMessageAndError() {
74 logTestStart();
75 new SystemOutLogger().info(MESSAGE, EXCEPTION);
76 }
77
78 @Test
79 void testInfoWithNullMessageAndNoError() {
80 logTestStart();
81 new SystemOutLogger().info(null);
82 }
83
84 @Test
85 void testInfoWithNullMessageError() {
86 logTestStart();
87 new SystemOutLogger().info(null, EXCEPTION);
88 }
89
90 @Test
91 void testInfoWithMessageNullError() {
92 logTestStart();
93 new SystemOutLogger().info(MESSAGE, null);
94 }
95
96 @Test
97 void testWarnWithMessageOnly() {
98 logTestStart();
99 new SystemOutLogger().warn(MESSAGE);
100 }
101
102 @Test
103 void testWarnWithMessageAndError() {
104 logTestStart();
105 new SystemOutLogger().warn(MESSAGE, EXCEPTION);
106 }
107
108 @Test
109 void testWarnWithNullMessageAndNoError() {
110 logTestStart();
111 new SystemOutLogger().warn(null);
112 }
113
114 @Test
115 void testWarnWithNullMessageError() {
116 logTestStart();
117 new SystemOutLogger().warn(null, EXCEPTION);
118 }
119
120 @Test
121 void testWarnWithMessageNullError() {
122 logTestStart();
123 new SystemOutLogger().warn(MESSAGE, null);
124 }
125
126 @Test
127 void testErrorWithMessageOnly() {
128 logTestStart();
129 new SystemOutLogger().error(MESSAGE);
130 }
131
132 @Test
133 void testErrorWithMessageAndError() {
134 logTestStart();
135 new SystemOutLogger().error(MESSAGE, EXCEPTION);
136 }
137
138 @Test
139 void testErrorWithNullMessageAndNoError() {
140 logTestStart();
141 new SystemOutLogger().error(null);
142 }
143
144 @Test
145 void testErrorWithNullMessageError() {
146 logTestStart();
147 new SystemOutLogger().error(null, EXCEPTION);
148 }
149
150 @Test
151 void testErrorWithMessageNullError() {
152 logTestStart();
153 new SystemOutLogger().error(MESSAGE, null);
154 }
155
156 @Test
157 void testFatalErrorWithMessageOnly() {
158 logTestStart();
159 new SystemOutLogger().fatalError(MESSAGE);
160 }
161
162 @Test
163 void testFatalErrorWithMessageAndError() {
164 logTestStart();
165 new SystemOutLogger().fatalError(MESSAGE, EXCEPTION);
166 }
167
168 @Test
169 void testFatalErrorWithNullMessageAndNoError() {
170 logTestStart();
171 new SystemOutLogger().fatalError(null);
172 }
173
174 @Test
175 void testFatalErrorWithNullMessageError() {
176 logTestStart();
177 new SystemOutLogger().fatalError(null, EXCEPTION);
178 }
179
180 @Test
181 void testFatalErrorWithMessageNullError() {
182 logTestStart();
183 new SystemOutLogger().fatalError(MESSAGE, null);
184 }
185
186 @Test
187 void testDefaultThresholdInfo() {
188 assertEquals(InvokerLogger.INFO, new SystemOutLogger().getThreshold());
189 }
190
191 @Test
192 void testThresholdDebug() {
193 InvokerLogger logger = new SystemOutLogger();
194 logger.setThreshold(InvokerLogger.DEBUG);
195 assertTrue(logger.isDebugEnabled());
196 assertTrue(logger.isInfoEnabled());
197 assertTrue(logger.isWarnEnabled());
198 assertTrue(logger.isErrorEnabled());
199 assertTrue(logger.isFatalErrorEnabled());
200 }
201
202 @Test
203 void testThresholdInfo() {
204 InvokerLogger logger = new SystemOutLogger();
205 logger.setThreshold(InvokerLogger.INFO);
206 assertFalse(logger.isDebugEnabled());
207 assertTrue(logger.isInfoEnabled());
208 assertTrue(logger.isWarnEnabled());
209 assertTrue(logger.isErrorEnabled());
210 assertTrue(logger.isFatalErrorEnabled());
211 }
212
213 @Test
214 void testThresholdWarn() {
215 InvokerLogger logger = new SystemOutLogger();
216 logger.setThreshold(InvokerLogger.WARN);
217 assertFalse(logger.isDebugEnabled());
218 assertFalse(logger.isInfoEnabled());
219 assertTrue(logger.isWarnEnabled());
220 assertTrue(logger.isErrorEnabled());
221 assertTrue(logger.isFatalErrorEnabled());
222 }
223
224 @Test
225 void testThresholdError() {
226 InvokerLogger logger = new SystemOutLogger();
227 logger.setThreshold(InvokerLogger.ERROR);
228 assertFalse(logger.isDebugEnabled());
229 assertFalse(logger.isInfoEnabled());
230 assertFalse(logger.isWarnEnabled());
231 assertTrue(logger.isErrorEnabled());
232 assertTrue(logger.isFatalErrorEnabled());
233 }
234
235 @Test
236 void testThresholdFatal() {
237 InvokerLogger logger = new SystemOutLogger();
238 logger.setThreshold(InvokerLogger.FATAL);
239 assertFalse(logger.isDebugEnabled());
240 assertFalse(logger.isInfoEnabled());
241 assertFalse(logger.isWarnEnabled());
242 assertFalse(logger.isErrorEnabled());
243 assertTrue(logger.isFatalErrorEnabled());
244 }
245
246
247 private void logTestStart() {
248 NullPointerException npe = new NullPointerException();
249 StackTraceElement element = npe.getStackTrace()[1];
250
251 System.out.println("Starting: " + element.getMethodName());
252 }
253 }