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: LoggerTest.java 4723 2013-01-03 08:50:05Z schulte $
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.2
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.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
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.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
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().isDebugEnabled();
098            this.getLogger().isErrorEnabled();
099            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    @SuppressWarnings("unused")
196    @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
197    private java.util.Locale getLocale()
198    {
199        final java.util.Locale _d = (java.util.Locale) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "Locale" );
200        assert _d != null : "'Locale' dependency not found.";
201        return _d;
202    }
203    /**
204     * Gets the {@code <Logger>} dependency.
205     * <p>
206     *   This method returns any available object of the {@code <org.jomc.logging.Logger>} specification at specification level 1.0.
207     *   That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.
208     * </p>
209     * <p><strong>Properties:</strong>
210     *   <table border="1" width="100%" cellpadding="3" cellspacing="0">
211     *     <tr class="TableSubHeadingColor">
212     *       <th align="left" scope="col" nowrap><b>Name</b></th>
213     *       <th align="left" scope="col" nowrap><b>Type</b></th>
214     *       <th align="left" scope="col" nowrap><b>Documentation</b></th>
215     *     </tr>
216     *     <tr class="TableRow">
217     *       <td align="left" valign="top" nowrap>{@code <name>}</td>
218     *       <td align="left" valign="top" nowrap>{@code java.lang.String}</td>
219     *       <td align="left" valign="top"></td>
220     *     </tr>
221     *   </table>
222     * </p>
223     * <dl>
224     *   <dt><b>Final:</b></dt><dd>No</dd>
225     * </dl>
226     * @return Implementation tests are performed with.
227     * {@code null} if no object is available.
228     * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
229     */
230    @SuppressWarnings("unused")
231    @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
232    private org.jomc.logging.Logger getLogger()
233    {
234        return (org.jomc.logging.Logger) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "Logger" );
235    }
236    // </editor-fold>
237    // SECTION-END
238    // SECTION-START[Properties]
239    // SECTION-END
240    // SECTION-START[Messages]
241    // <editor-fold defaultstate="collapsed" desc=" Generated Messages ">
242    /**
243     * Gets the text of the {@code <Test Implementation Not Found Message>} message.
244     * <p><dl>
245     *   <dt><b>Languages:</b></dt>
246     *     <dd>English (default)</dd>
247     *     <dd>Deutsch</dd>
248     *   <dt><b>Final:</b></dt><dd>Yes</dd>
249     * </dl></p>
250     * @param locale The locale of the message to return.
251     * @return The text of the {@code <Test Implementation Not Found Message>} message for {@code locale}.
252     * @throws org.jomc.ObjectManagementException if getting the message instance fails.
253     */
254    @SuppressWarnings("unused")
255    @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.4", comments = "See http://www.jomc.org/jomc/1.4/jomc-tools-1.4" )
256    private String getTestImplementationNotFoundMessage( final java.util.Locale locale )
257    {
258        final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "Test Implementation Not Found Message", locale );
259        assert _m != null : "'Test Implementation Not Found Message' message not found.";
260        return _m;
261    }
262    // </editor-fold>
263    // SECTION-END
264}