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