View Javadoc

1   // SECTION-START[License Header]
2   // <editor-fold defaultstate="collapsed" desc=" Generated License ">
3   /*
4    *   Java Object Management and Configuration
5    *   Copyright (C) Christian Schulte, 2005-206
6    *   All rights reserved.
7    *
8    *   Redistribution and use in source and binary forms, with or without
9    *   modification, are permitted provided that the following conditions
10   *   are met:
11   *
12   *     o Redistributions of source code must retain the above copyright
13   *       notice, this list of conditions and the following disclaimer.
14   *
15   *     o Redistributions in binary form must reproduce the above copyright
16   *       notice, this list of conditions and the following disclaimer in
17   *       the documentation and/or other materials provided with the
18   *       distribution.
19   *
20   *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
21   *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22   *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23   *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
24   *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25   *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26   *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27   *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28   *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29   *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   *   $JOMC: LoggerTest.java 4274 2012-02-17 04:07:29Z schulte2005 $
32   *
33   */
34  // </editor-fold>
35  // SECTION-END
36  package org.jomc.logging.it;
37  
38  import java.io.IOException;
39  import java.io.InputStream;
40  import java.net.URL;
41  import java.util.ArrayList;
42  import java.util.Arrays;
43  import java.util.List;
44  import java.util.logging.LogManager;
45  import org.junit.Test;
46  import org.junit.runner.JUnitCore;
47  import static org.junit.Assert.fail;
48  
49  // SECTION-START[Documentation]
50  // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
51  /**
52   * Object management and configuration logging system test suite.
53   *
54   * <dl>
55   *   <dt><b>Identifier:</b></dt><dd>org.jomc.logging.it.LoggerTest</dd>
56   *   <dt><b>Name:</b></dt><dd>JOMC Logging Test Suite</dd>
57   *   <dt><b>Abstract:</b></dt><dd>No</dd>
58   *   <dt><b>Final:</b></dt><dd>No</dd>
59   *   <dt><b>Stateless:</b></dt><dd>Yes</dd>
60   * </dl>
61   *
62   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
63   * @version 1.1
64   */
65  // </editor-fold>
66  // SECTION-END
67  // SECTION-START[Annotations]
68  // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
69  @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2" )
70  // </editor-fold>
71  // SECTION-END
72  public class LoggerTest
73  {
74      // SECTION-START[Constructors]
75      // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
76      /** Creates a new {@code LoggerTest} instance. */
77      @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2" )
78      public LoggerTest()
79      {
80          // SECTION-START[Default Constructor]
81          super();
82          // SECTION-END
83      }
84      // </editor-fold>
85      // SECTION-END
86      // SECTION-START[LoggerTest]
87  
88      /**
89       * Tests the {@link org.jomc.logging.Logger#isInfoEnabled() isXxxEnabled()} methods to not throw any exceptions.
90       *
91       * @throws Exception if testing fails.
92       */
93      @Test public void testIsEnabled() throws Exception
94      {
95          if ( this.getLogger() != null )
96          {
97              this.getLogger().isDebugEnabled();
98              this.getLogger().isErrorEnabled();
99              this.getLogger().isFatalEnabled();
100             this.getLogger().isInfoEnabled();
101             this.getLogger().isTraceEnabled();
102             this.getLogger().isWarnEnabled();
103         }
104         else
105         {
106             fail( this.getTestImplementationNotFoundMessage( this.getLocale() ) );
107         }
108     }
109 
110     /**
111      * Tests the various logger methods to not throw any exceptions.
112      *
113      * @throws Exception if testing fails.
114      */
115     @Test public void testLog() throws Exception
116     {
117         if ( this.getLogger() != null )
118         {
119             this.getLogger().debug( "TEST" );
120             this.getLogger().debug( new Exception() );
121             this.getLogger().debug( "TEST", new Exception() );
122 
123             this.getLogger().error( "TEST" );
124             this.getLogger().error( new Exception() );
125             this.getLogger().error( "TEST", new Exception() );
126 
127             this.getLogger().fatal( "TEST" );
128             this.getLogger().fatal( new Exception() );
129             this.getLogger().fatal( "TEST", new Exception() );
130 
131             this.getLogger().info( "TEST" );
132             this.getLogger().info( new Exception() );
133             this.getLogger().info( "TEST", new Exception() );
134 
135             this.getLogger().trace( "TEST" );
136             this.getLogger().trace( new Exception() );
137             this.getLogger().trace( "TEST", new Exception() );
138 
139             this.getLogger().warn( "TEST" );
140             this.getLogger().warn( new Exception() );
141             this.getLogger().warn( "TEST", new Exception() );
142         }
143         else
144         {
145             fail( this.getTestImplementationNotFoundMessage( this.getLocale() ) );
146         }
147     }
148 
149     /**
150      * Test runner entry point.
151      * <p>This method sets up the JDK's {@code LogManager} with properties found at class path location
152      * {@code "/logging.properties"} and executes {@link JUnitCore#main} passing the given arguments with this classes
153      * name prepended.</p>
154      *
155      * @param args Command line arguments.
156      */
157     public static void main( final String... args )
158     {
159         try
160         {
161             final URL loggingProperties = LoggerTest.class.getResource( "/logging.properties" );
162             if ( loggingProperties != null )
163             {
164                 final InputStream in = loggingProperties.openStream();
165                 LogManager.getLogManager().readConfiguration( in );
166                 in.close();
167             }
168 
169             final List<String> l = new ArrayList<String>( Arrays.asList( args ) );
170             l.add( 0, LoggerTest.class.getName() );
171             JUnitCore.main( l.toArray( new String[ l.size() ] ) );
172         }
173         catch ( final IOException e )
174         {
175             e.printStackTrace();
176             System.exit( 1 );
177         }
178     }
179 
180     // SECTION-END
181     // SECTION-START[Dependencies]
182     // <editor-fold defaultstate="collapsed" desc=" Generated Dependencies ">
183     /**
184      * Gets the {@code <Locale>} dependency.
185      * <p>
186      *   This method returns the {@code <default>} object of the {@code <java.util.Locale>} specification at specification level 1.1.
187      *   That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.
188      * </p>
189      * <dl>
190      *   <dt><b>Final:</b></dt><dd>No</dd>
191      * </dl>
192      * @return The {@code <Locale>} dependency.
193      * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
194      */
195     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2" )
196     private java.util.Locale getLocale()
197     {
198         final java.util.Locale _d = (java.util.Locale) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "Locale" );
199         assert _d != null : "'Locale' dependency not found.";
200         return _d;
201     }
202     /**
203      * Gets the {@code <Logger>} dependency.
204      * <p>
205      *   This method returns any available object of the {@code <org.jomc.logging.Logger>} specification at specification level 1.0.
206      *   That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.
207      * </p>
208      * <p><strong>Properties:</strong>
209      *   <table border="1" width="100%" cellpadding="3" cellspacing="0">
210      *     <tr class="TableSubHeadingColor">
211      *       <th align="left" scope="col" nowrap><b>Name</b></th>
212      *       <th align="left" scope="col" nowrap><b>Type</b></th>
213      *       <th align="left" scope="col" nowrap><b>Documentation</b></th>
214      *     </tr>
215      *     <tr class="TableRow">
216      *       <td align="left" valign="top" nowrap>{@code <name>}</td>
217      *       <td align="left" valign="top" nowrap>{@code java.lang.String}</td>
218      *       <td align="left" valign="top"></td>
219      *     </tr>
220      *   </table>
221      * </p>
222      * <dl>
223      *   <dt><b>Final:</b></dt><dd>No</dd>
224      * </dl>
225      * @return Implementation tests are performed with.
226      * {@code null} if no object is available.
227      * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
228      */
229     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2" )
230     private org.jomc.logging.Logger getLogger()
231     {
232         return (org.jomc.logging.Logger) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "Logger" );
233     }
234     // </editor-fold>
235     // SECTION-END
236     // SECTION-START[Properties]
237     // SECTION-END
238     // SECTION-START[Messages]
239     // <editor-fold defaultstate="collapsed" desc=" Generated Messages ">
240     /**
241      * Gets the text of the {@code <Test Implementation Not Found Message>} message.
242      * <p><dl>
243      *   <dt><b>Languages:</b></dt>
244      *     <dd>English (default)</dd>
245      *     <dd>Deutsch</dd>
246      *   <dt><b>Final:</b></dt><dd>Yes</dd>
247      * </dl></p>
248      * @param locale The locale of the message to return.
249      * @return The text of the {@code <Test Implementation Not Found Message>} message for {@code locale}.
250      * @throws org.jomc.ObjectManagementException if getting the message instance fails.
251      */
252     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2" )
253     private String getTestImplementationNotFoundMessage( final java.util.Locale locale )
254     {
255         final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Test Implementation Not Found Message", locale );
256         assert _m != null : "'Test Implementation Not Found Message' message not found.";
257         return _m;
258     }
259     // </editor-fold>
260     // SECTION-END
261 }