001// SECTION-START[License Header]
002// <editor-fold defaultstate="collapsed" desc=" Generated License ">
003/*
004 *   Java Object Management and Configuration
005 *   Copyright (C) Christian Schulte, 2005-206
006 *   All rights reserved.
007 *
008 *   Redistribution and use in source and binary forms, with or without
009 *   modification, are permitted provided that the following conditions
010 *   are met:
011 *
012 *     o Redistributions of source code must retain the above copyright
013 *       notice, this list of conditions and the following disclaimer.
014 *
015 *     o Redistributions in binary form must reproduce the above copyright
016 *       notice, this list of conditions and the following disclaimer in
017 *       the documentation and/or other materials provided with the
018 *       distribution.
019 *
020 *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
021 *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
022 *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
023 *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
024 *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
025 *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
026 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
027 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
028 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
029 *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030 *
031 *   $JOMC$
032 *
033 */
034// </editor-fold>
035// SECTION-END
036package org.jomc.logging.it;
037
038import java.io.IOException;
039import java.io.InputStream;
040import java.net.URL;
041import java.util.ArrayList;
042import java.util.Arrays;
043import java.util.List;
044import java.util.logging.LogManager;
045import org.junit.Test;
046import org.junit.runner.JUnitCore;
047import static org.junit.Assert.fail;
048
049// SECTION-START[Documentation]
050// <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
051/**
052 * Object management and configuration logging system test suite.
053 *
054 * <dl>
055 *   <dt><b>Identifier:</b></dt><dd>org.jomc.logging.it.LoggerTest</dd>
056 *   <dt><b>Name:</b></dt><dd>JOMC Logging ⁑ Test Suite</dd>
057 *   <dt><b>Abstract:</b></dt><dd>No</dd>
058 *   <dt><b>Final:</b></dt><dd>No</dd>
059 *   <dt><b>Stateless:</b></dt><dd>Yes</dd>
060 * </dl>
061 *
062 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 1.0
063 * @version 1.3
064 */
065// </editor-fold>
066// SECTION-END
067// SECTION-START[Annotations]
068// <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
069@javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" )
070// </editor-fold>
071// SECTION-END
072public class LoggerTest
073{
074    // SECTION-START[Constructors]
075    // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
076    /** Creates a new {@code LoggerTest} instance. */
077    @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.5", comments = "See http://www.jomc.org/jomc/1.5/jomc-tools-1.5" )
078    public LoggerTest()
079    {
080        // SECTION-START[Default Constructor]
081        super();
082        // SECTION-END
083    }
084    // </editor-fold>
085    // SECTION-END
086    // SECTION-START[LoggerTest]
087
088    /**
089     * Tests the {@link org.jomc.logging.Logger#isInfoEnabled() isXxxEnabled()} methods to not throw any exceptions.
090     *
091     * @throws Exception if testing fails.
092     */
093    @Test public void testIsEnabled() throws Exception
094    {
095        if ( this.getLogger() != null )
096        {
097            this.getLogger().isConfigEnabled();
098            this.getLogger().isDebugEnabled();
099            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}