View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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     // this is just a debugging helper for separating unit test output...
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 }