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$
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:cs@schulte.it">Christian Schulte</a> 1.0
63   * @version 1.3
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.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" )
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.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" )
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().isConfigEnabled();
98              this.getLogger().isDebugEnabled();
99              this.getLogger().isErrorEnabled();
100             this.getLogger().isFatalEnabled();
101             this.getLogger().isInfoEnabled();
102             this.getLogger().isTraceEnabled();
103             this.getLogger().isWarnEnabled();
104         }
105         else
106         {
107             fail( this.getTestImplementationNotFoundMessage( this.getLocale() ) );
108         }
109     }
110 
111     /**
112      * Tests the various logger methods to not throw any exceptions.
113      *
114      * @throws Exception if testing fails.
115      */
116     @Test public void testLog() throws Exception
117     {
118         if ( this.getLogger() != null )
119         {
120             this.getLogger().config( "TEST" );
121             this.getLogger().config( new Exception() );
122             this.getLogger().config( "TEST", new Exception() );
123 
124             this.getLogger().debug( "TEST" );
125             this.getLogger().debug( new Exception() );
126             this.getLogger().debug( "TEST", new Exception() );
127 
128             this.getLogger().error( "TEST" );
129             this.getLogger().error( new Exception() );
130             this.getLogger().error( "TEST", new Exception() );
131 
132             this.getLogger().fatal( "TEST" );
133             this.getLogger().fatal( new Exception() );
134             this.getLogger().fatal( "TEST", new Exception() );
135 
136             this.getLogger().info( "TEST" );
137             this.getLogger().info( new Exception() );
138             this.getLogger().info( "TEST", new Exception() );
139 
140             this.getLogger().trace( "TEST" );
141             this.getLogger().trace( new Exception() );
142             this.getLogger().trace( "TEST", new Exception() );
143 
144             this.getLogger().warn( "TEST" );
145             this.getLogger().warn( new Exception() );
146             this.getLogger().warn( "TEST", new Exception() );
147         }
148         else
149         {
150             fail( this.getTestImplementationNotFoundMessage( this.getLocale() ) );
151         }
152     }
153 
154     /**
155      * Test runner entry point.
156      * <p>This method sets up the JDK's {@code LogManager} with properties found at class path location
157      * {@code "/logging.properties"} and executes {@link JUnitCore#main} passing the given arguments with this classes
158      * name prepended.</p>
159      *
160      * @param args Command line arguments.
161      */
162     public static void main( final String... args )
163     {
164         try
165         {
166             final URL loggingProperties = LoggerTest.class.getResource( "/logging.properties" );
167             if ( loggingProperties != null )
168             {
169                 final InputStream in = loggingProperties.openStream();
170                 LogManager.getLogManager().readConfiguration( in );
171                 in.close();
172             }
173 
174             final List<String> l = new ArrayList<String>( Arrays.asList( args ) );
175             l.add( 0, LoggerTest.class.getName() );
176             JUnitCore.main( l.toArray( new String[ l.size() ] ) );
177         }
178         catch ( final IOException e )
179         {
180             e.printStackTrace();
181             System.exit( 1 );
182         }
183     }
184 
185     // SECTION-END
186     // SECTION-START[Dependencies]
187     // <editor-fold defaultstate="collapsed" desc=" Generated Dependencies ">
188     /**
189      * Gets the {@code <Locale>} dependency.
190      * <p>
191      *   This method returns the {@code <default>} object of the {@code <java.util.Locale>} specification at specification level 1.1.
192      *   That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.
193      * </p>
194      * <dl>
195      *   <dt><b>Final:</b></dt><dd>No</dd>
196      * </dl>
197      * @return The {@code <Locale>} dependency.
198      * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
199      */
200     @SuppressWarnings("unused")
201     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" )
202     private java.util.Locale getLocale()
203     {
204         final java.util.Locale _d = (java.util.Locale) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "Locale" );
205         assert _d != null : "'Locale' dependency not found.";
206         return _d;
207     }
208     /**
209      * Gets the {@code <Logger>} dependency.
210      * <p>
211      *   This method returns any available object of the {@code <org.jomc.logging.Logger>} specification at specification level 1.1.
212      *   That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.
213      * </p>
214      * <p><strong>Properties:</strong>
215      *   <table border="1" width="100%" cellpadding="3" cellspacing="0">
216      *     <tr class="TableSubHeadingColor">
217      *       <th align="left" scope="col" nowrap><b>Name</b></th>
218      *       <th align="left" scope="col" nowrap><b>Type</b></th>
219      *       <th align="left" scope="col" nowrap><b>Documentation</b></th>
220      *     </tr>
221      *     <tr class="TableRow">
222      *       <td align="left" valign="top" nowrap>{@code <name>}</td>
223      *       <td align="left" valign="top" nowrap>{@code java.lang.String}</td>
224      *       <td align="left" valign="top"></td>
225      *     </tr>
226      *   </table>
227      * </p>
228      * <dl>
229      *   <dt><b>Final:</b></dt><dd>No</dd>
230      * </dl>
231      * @return Implementation tests are performed with.
232      * {@code null} if no object is available.
233      * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
234      */
235     @SuppressWarnings("unused")
236     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" )
237     private org.jomc.logging.Logger getLogger()
238     {
239         return (org.jomc.logging.Logger) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "Logger" );
240     }
241     // </editor-fold>
242     // SECTION-END
243     // SECTION-START[Properties]
244     // SECTION-END
245     // SECTION-START[Messages]
246     // <editor-fold defaultstate="collapsed" desc=" Generated Messages ">
247     /**
248      * Gets the text of the {@code <Test Implementation Not Found Message>} message.
249      * <p><dl>
250      *   <dt><b>Languages:</b></dt>
251      *     <dd>English (default)</dd>
252      *     <dd>Deutsch</dd>
253      *   <dt><b>Final:</b></dt><dd>Yes</dd>
254      * </dl></p>
255      * @param locale The locale of the message to return.
256      * @return The text of the {@code <Test Implementation Not Found Message>} message for {@code locale}.
257      * @throws org.jomc.ObjectManagementException if getting the message instance fails.
258      */
259     @SuppressWarnings("unused")
260     @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" )
261     private String getTestImplementationNotFoundMessage( final java.util.Locale locale )
262     {
263         final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Test Implementation Not Found Message", locale );
264         assert _m != null : "'Test Implementation Not Found Message' message not found.";
265         return _m;
266     }
267     // </editor-fold>
268     // SECTION-END
269 }