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 4274 2012-02-17 04:07:29Z schulte2005 $
032     *
033     */
034    // </editor-fold>
035    // SECTION-END
036    package org.jomc.logging.it;
037    
038    import java.io.IOException;
039    import java.io.InputStream;
040    import java.net.URL;
041    import java.util.ArrayList;
042    import java.util.Arrays;
043    import java.util.List;
044    import java.util.logging.LogManager;
045    import org.junit.Test;
046    import org.junit.runner.JUnitCore;
047    import 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:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
063     * @version 1.1
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.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2" )
070    // </editor-fold>
071    // SECTION-END
072    public 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.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2" )
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        @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    }