Project Report: fawkez

Packagesummary org.jcoderz.commons

org.jcoderz.commons.LogFormatterOutputTest

LineHitsNoteSource
1  /*
2   * $Id: LogFormatterOutputTest.java 1610 2010-03-11 08:19:11Z amandel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5   *
6   * Redistribution and use in source and binary forms, with or without
7   * modification, are permitted provided that the following conditions are
8   * met:
9   *
10   *    * Redistributions of source code must retain the above copyright
11   *      notice, this list of conditions and the following disclaimer.
12   *    * Redistributions in binary form must reproduce the above
13   *      copyright notice, this list of conditions and the following
14   *      disclaimer in the documentation and/or other materials
15   *      provided with the distribution.
16   *    * Neither the name of the jCoderZ.org Project nor the names of
17   *      its contributors may be used to endorse or promote products
18   *      derived from this software without specific prior written
19   *      permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   */
33  package org.jcoderz.commons;
34  
35  import java.util.logging.Level;
36  import java.util.logging.Logger;
37  import junit.framework.TestCase;
38  
39  import org.jcoderz.commons.test.TssLogMessage;
40  import org.jcoderz.commons.types.Date;
41  import org.xml.sax.SAXException;
42  
43  
44  
45  /**
46   * JUnit test to demonstrate the different outputs of
47   * the {@link org.jcoderz.commons.LogFormatter}.
48   *
49   * @author Michael Griffel
50   */
51100 public class LogFormatterOutputTest
52        extends TestCase
53  {
5475    private static final String CLASSNAME
55           = LogFormatterOutputTest.class.getName();
56100    private static final Logger logger = Logger.getLogger(CLASSNAME);
57  
58     /**
59      * Produces a log message from an exception w/ two parameters.
60      */
61     public void testLogEvent ()
62     {
63100       TstLogMessage.TestMessage.log("foo", Date.now().toUtilDate());
64100    }
65  
66     /**
67      * Produces a log message from an exception w/ two parameters.
68      */
69     public void testLogEventWithImpliedParams ()
70     {
71100       TssLogMessage.ImpliedParametersLog.log(new RuntimeException("Foo!"));
72100    }
73  
74     /**
75      * Produces a log message from an exception w/ two parameters and exception
76      * as cause.
77      */
78     public void testLogException ()
79     {
80100       TstLogMessage.TestMessage.log("foo", Date.now().toUtilDate(),
81              new Exception("This is the top level exception"));
82100    }
83  
84     /**
85      * Produces a log message from a standard log record with a Throwable.
86      *
87      */
88     public void testLogThrowable ()
89     {
90100       final Throwable th = new Exception (
91              "This is a top level test exception");
92100       logger.logp(Level.SEVERE, CLASSNAME, "logException", "Logging exception",
93              th);
94100    }
95  
96     /**
97      * Produces a log message from a standard log record with a nested Throwable.
98      */
99     public void testLogNestedThrowable ()
100     {
101100       final Throwable th = new Exception (
102              "This is a top level test exception");
103100       th.initCause(new Throwable("This is a nestedException"));
104100       logger.logp(Level.SEVERE, CLASSNAME, "logException", "Logging exception",
105              th);
106100    }
107  
108     /** Test singel nested exception. */
109     public void testServerExceptionLogWithNestedNullPointer ()
110     {
111100       new InternalErrorException(
112              "dump stack trace w/ nested NullPointerException",
113              new NullPointerException()).log();
114100    }
115     /** Test singel nested exception. */
116     public void testServerExceptionLogWithNestedInternalErrorAndNullPointer ()
117     {
118100       final NullPointerException e = new NullPointerException("root");
119100       final InternalErrorException x = new InternalErrorException("middle", e);
120100(1)      System.err.println("---------------------------------------------------");
121100       new InternalErrorException(
122              "dump stack trace w/ nested InternalErrorException and "
123                 + "NullPointerException",
124              x).log();
125100(2)      System.err.println("---------------------------------------------------");
126100    }
127  
128     /** Test deep nested exception. */
129     public void testDeepNesting ()
130     {
131100       final InternalErrorException inner
132              = new InternalErrorException("inner");
133100       inner.addParameter("INNER-PARAMETER", "value");
134100       final InternalErrorException middle
135              = new InternalErrorException("middle", inner);
136100       middle.addParameter("MIDDLE-PARAMETER", "this is just a value");
137100       final Exception middleDefault
138              = new NullPointerException("middle-nullpointer");
139100       middleDefault.initCause(middle);
140100       final Loggable outer
141              = new InternalErrorException("outer", middleDefault);
142100       outer.addParameter("OUTER-PARAMETER", "this is just an other value");
143100       outer.log();
144100    }
145  
146     /** Test nested exceptions. */
147     public void testNesting ()
148     {
149        try
150        {
1510          a();
152        }
153100       catch (HighLevelException e)
154        {
155100          new InternalErrorException("Test nesting.", e).log();
1560       }
157100    }
158  
159     /** Test deep nested with SAX exceptions exception. */
160     public void testNestingWithSax ()
161     {
162100       final InternalErrorException inner
163              = new InternalErrorException("inner");
164100       inner.addParameter("INNER-PARAMETER", "value");
165100       final InternalErrorException middle
166              = new InternalErrorException("middle", inner);
167100       middle.addParameter("MIDDLE-PARAMETER", "this is just a value");
168100       final Exception middleDefault
169              = new SAXException("SAX Exception", middle);
170100       final Loggable outer
171              = new InternalErrorException("outer", middleDefault);
172100       outer.addParameter("OUTER-PARAMETER", "this is just an other value");
173100       outer.log();
174100    }
175  
176     static void a ()
177           throws HighLevelException
178     {
179        try
180        {
1810          b();
182        }
183100       catch (MidLevelException e)
184        {
185100          throw new HighLevelException(e);
1860       }
1870    }
188  
189     static void b ()
190           throws MidLevelException
191     {
1920       c();
1930    }
194  
195  
196     static void c ()
197           throws MidLevelException
198     {
199        try
200        {
2010          d();
202        }
203100       catch (LowLevelException e)
204        {
205100          throw new MidLevelException(e);
2060       }
2070    }
208  
209     static void d ()
210           throws LowLevelException
211     {
2120       e();
2130    }
214  
215     static void e ()
216           throws LowLevelException
217     {
218100       throw new LowLevelException();
219     }
220  
221     static final class HighLevelException
222           extends Exception
223     {
224        private static final long serialVersionUID = 1L;
225  
226        HighLevelException (Throwable cause)
227        {
228100          super(cause);
229100       }
230     }
231  
232     static final class MidLevelException
233           extends Exception
234     {
235        private static final long serialVersionUID = 1L;
236  
237        MidLevelException (Throwable cause)
238        {
239100          super(cause);
240100       }
241     }
242  
243100    static final class LowLevelException
244           extends Exception
245     {
246        private static final long serialVersionUID = 1L;
247     }
248  
249  }

Findings in this File

d (1) 120 : 7 System.out.print is used
d (2) 125 : 7 System.out.print is used