View Javadoc
1   package org.apache.maven.shared.invoker;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.net.MalformedURLException;
27  
28  import org.junit.Test;
29  
30  public class SystemOutLoggerTest
31  {
32  
33      private static final Throwable EXCEPTION =
34          new MalformedURLException( "This is meant to happen. It's part of the test." );
35  
36      private static final String MESSAGE = "This is a test message.";
37  
38      @Test
39      public void testDebugWithMessageOnly()
40      {
41          logTestStart();
42          new SystemOutLogger().debug( MESSAGE );
43      }
44  
45      @Test
46      public void testDebugWithMessageAndError()
47      {
48          logTestStart();
49          new SystemOutLogger().debug( MESSAGE, EXCEPTION );
50      }
51  
52      @Test
53      public void testDebugWithNullMessageAndNoError()
54      {
55          logTestStart();
56          new SystemOutLogger().debug( null );
57      }
58  
59      @Test
60      public void testDebugWithNullMessageError()
61      {
62          logTestStart();
63          new SystemOutLogger().debug( null, EXCEPTION );
64      }
65  
66      @Test
67      public void testDebugWithMessageNullError()
68      {
69          logTestStart();
70          new SystemOutLogger().debug( MESSAGE, null );
71      }
72  
73      @Test
74      public void testInfoWithMessageOnly()
75      {
76          logTestStart();
77          new SystemOutLogger().info( MESSAGE );
78      }
79  
80      @Test
81      public void testInfoWithMessageAndError()
82      {
83          logTestStart();
84          new SystemOutLogger().info( MESSAGE, EXCEPTION );
85      }
86  
87      @Test
88      public void testInfoWithNullMessageAndNoError()
89      {
90          logTestStart();
91          new SystemOutLogger().info( null );
92      }
93  
94      @Test
95      public void testInfoWithNullMessageError()
96      {
97          logTestStart();
98          new SystemOutLogger().info( null, EXCEPTION );
99      }
100 
101     @Test
102     public void testInfoWithMessageNullError()
103     {
104         logTestStart();
105         new SystemOutLogger().info( MESSAGE, null );
106     }
107 
108     @Test
109     public void testWarnWithMessageOnly()
110     {
111         logTestStart();
112         new SystemOutLogger().warn( MESSAGE );
113     }
114 
115     @Test
116     public void testWarnWithMessageAndError()
117     {
118         logTestStart();
119         new SystemOutLogger().warn( MESSAGE, EXCEPTION );
120     }
121 
122     @Test
123     public void testWarnWithNullMessageAndNoError()
124     {
125         logTestStart();
126         new SystemOutLogger().warn( null );
127     }
128 
129     @Test
130     public void testWarnWithNullMessageError()
131     {
132         logTestStart();
133         new SystemOutLogger().warn( null, EXCEPTION );
134     }
135 
136     @Test
137     public void testWarnWithMessageNullError()
138     {
139         logTestStart();
140         new SystemOutLogger().warn( MESSAGE, null );
141     }
142 
143     @Test
144     public void testErrorWithMessageOnly()
145     {
146         logTestStart();
147         new SystemOutLogger().error( MESSAGE );
148     }
149 
150     @Test
151     public void testErrorWithMessageAndError()
152     {
153         logTestStart();
154         new SystemOutLogger().error( MESSAGE, EXCEPTION );
155     }
156 
157     @Test
158     public void testErrorWithNullMessageAndNoError()
159     {
160         logTestStart();
161         new SystemOutLogger().error( null );
162     }
163 
164     @Test
165     public void testErrorWithNullMessageError()
166     {
167         logTestStart();
168         new SystemOutLogger().error( null, EXCEPTION );
169     }
170 
171     @Test
172     public void testErrorWithMessageNullError()
173     {
174         logTestStart();
175         new SystemOutLogger().error( MESSAGE, null );
176     }
177 
178     @Test
179     public void testFatalErrorWithMessageOnly()
180     {
181         logTestStart();
182         new SystemOutLogger().fatalError( MESSAGE );
183     }
184 
185     @Test
186     public void testFatalErrorWithMessageAndError()
187     {
188         logTestStart();
189         new SystemOutLogger().fatalError( MESSAGE, EXCEPTION );
190     }
191 
192     @Test
193     public void testFatalErrorWithNullMessageAndNoError()
194     {
195         logTestStart();
196         new SystemOutLogger().fatalError( null );
197     }
198 
199     @Test
200     public void testFatalErrorWithNullMessageError()
201     {
202         logTestStart();
203         new SystemOutLogger().fatalError( null, EXCEPTION );
204     }
205 
206     @Test
207     public void testFatalErrorWithMessageNullError()
208     {
209         logTestStart();
210         new SystemOutLogger().fatalError( MESSAGE, null );
211     }
212 
213     @Test
214     public void testDefaultThresholdInfo()
215     {
216         assertEquals( InvokerLogger.INFO, new SystemOutLogger().getThreshold() );
217     }
218 
219     @Test
220     public void testThresholdDebug()
221     {
222         InvokerLogger logger = new SystemOutLogger();
223         logger.setThreshold( InvokerLogger.DEBUG );
224         assertTrue( logger.isDebugEnabled() );
225         assertTrue( logger.isInfoEnabled() );
226         assertTrue( logger.isWarnEnabled() );
227         assertTrue( logger.isErrorEnabled() );
228         assertTrue( logger.isFatalErrorEnabled() );
229     }
230 
231     @Test
232     public void testThresholdInfo()
233     {
234         InvokerLogger logger = new SystemOutLogger();
235         logger.setThreshold( InvokerLogger.INFO );
236         assertFalse( logger.isDebugEnabled() );
237         assertTrue( logger.isInfoEnabled() );
238         assertTrue( logger.isWarnEnabled() );
239         assertTrue( logger.isErrorEnabled() );
240         assertTrue( logger.isFatalErrorEnabled() );
241     }
242 
243     @Test
244     public void testThresholdWarn()
245     {
246         InvokerLogger logger = new SystemOutLogger();
247         logger.setThreshold( InvokerLogger.WARN );
248         assertFalse( logger.isDebugEnabled() );
249         assertFalse( logger.isInfoEnabled() );
250         assertTrue( logger.isWarnEnabled() );
251         assertTrue( logger.isErrorEnabled() );
252         assertTrue( logger.isFatalErrorEnabled() );
253     }
254 
255     @Test
256     public void testThresholdError()
257     {
258         InvokerLogger logger = new SystemOutLogger();
259         logger.setThreshold( InvokerLogger.ERROR );
260         assertFalse( logger.isDebugEnabled() );
261         assertFalse( logger.isInfoEnabled() );
262         assertFalse( logger.isWarnEnabled() );
263         assertTrue( logger.isErrorEnabled() );
264         assertTrue( logger.isFatalErrorEnabled() );
265     }
266 
267     @Test
268     public void testThresholdFatal()
269     {
270         InvokerLogger logger = new SystemOutLogger();
271         logger.setThreshold( InvokerLogger.FATAL );
272         assertFalse( logger.isDebugEnabled() );
273         assertFalse( logger.isInfoEnabled() );
274         assertFalse( logger.isWarnEnabled() );
275         assertFalse( logger.isErrorEnabled() );
276         assertTrue( logger.isFatalErrorEnabled() );
277     }
278 
279     // this is just a debugging helper for separating unit test output...
280     private void logTestStart()
281     {
282         NullPointerException npe = new NullPointerException();
283         StackTraceElement element = npe.getStackTrace()[1];
284 
285         System.out.println( "Starting: " + element.getMethodName() );
286     }
287 
288 }