Project Report: fawkez

Packagesummary org.jcoderz.commons.logging

org.jcoderz.commons.logging.XmlPrinterTest

LineHitsNoteSource
1  /*
2   * $Id: XmlPrinterTest.java 1011 2008-06-16 17:57:36Z 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.logging;
34  
35  import java.io.CharArrayWriter;
36  import java.io.PrintWriter;
37  import java.util.logging.Formatter;
38  import java.util.logging.Handler;
39  import java.util.logging.LogRecord;
40  import java.util.logging.Logger;
41  import org.jcoderz.commons.LogFormatterOutputTest;
42  
43  
44  /**
45   * This class is used for testing the XmlPrinter. It uses the handler being
46   * installed by the LogElementTest and sets a formatter using th XmlPrinter.
47   * The log messages are generated by the LogFormatterOutputTest.
48   *
49   */
50 (1)(2)public class XmlPrinterTest
51        extends LogFormatterOutputTest
52  {
5375    private static final String CLASSNAME = FormatTest.class.getName();
54100    private static final Logger logger = Logger.getLogger(CLASSNAME);
55  
56100    private static final Logger ROOT_LOGGER = Logger.getLogger("");
57  
58100    private Handler mHandler = null;
59  
60 (3)   private static class LogElementHandler
61           extends Handler
62     {
63        /**
64         * Creates a new instance of this and sets teh xml formatter as formatter.
65         */
66        private LogElementHandler ()
67100       {
68100          setFormatter(new XmlFormatter());
69100       }
70  
71        /**
72         * Closes this Handler, this is a NOP.
73         *
74         * @see java.util.logging.Handler#close()
75         */
76        public void close ()
77              throws SecurityException
78        {
79           // nop
800       }
81  
82        /**
83         * Flushes this Handler, this is a NOP.
84         *
85         * @see java.util.logging.Handler#flush()
86         */
87        public void flush ()
88        {
89           // nop
900       }
91  
92        /**
93         * Just formats the LogRecord with the xml formatter.
94         *
95         * @see java.util.logging.Handler#publish(java.util.logging.LogRecord)
96         */
97        public void publish (LogRecord record)
98        {
99100          getFormatter().format(record);
100100       }
101     }
102  
103 (4)   private static class XmlFormatter
104           extends Formatter
105     {
106 (5)      final PrintWriter mPrinter;
107 (6)      final CharArrayWriter mCharWriter;
108100(7)      XmlPrinter mXmlPrinter = null;
109  
110        private XmlFormatter ()
111100       {
112100          mCharWriter = new CharArrayWriter();
113100          mPrinter = new PrintWriter(mCharWriter);
114           try
115           {
116100             mXmlPrinter = new XmlPrinter();
117           }
1180          catch (InstantiationException e)
119           {
1200             fail("Could not instantiate an Xml Printer");
121100          }
122100       }
123  
124        /** {@inheritDoc} */
125        public String format (LogRecord record)
126        {
127100          mCharWriter.reset();
128100          final LogItem element = new LogElement(record);
129100          mXmlPrinter.print(mPrinter, element);
130100          return mCharWriter.toString();
131        }
132     }
133  
134     /**
135      * Creates a new test case instance.
136      */
137 (8)   public XmlPrinterTest ()
138     {
139100       super();
140100    }
141  
142 (9)   public void testSimpleLogRecord ()
143     {
144100       logger.info("testSimpleLogRecord: " + getClass().getName());
145100    }
146  
147  
148     /**
149      * Installs an additional Handler, which publishes RocRecords using
150      * LogElements.
151      *
152      * @see junit.framework.TestCase#setUp()
153      */
154     protected void setUp ()
155     {
156100       mHandler = new LogElementHandler();
157100       ROOT_LOGGER.addHandler(mHandler);
158100    }
159  
160     /**
161      * Deinstalls the additional Handler.
162      *
163      * @see junit.framework.TestCase#setUp()
164      */
165     protected void tearDown ()
166     {
167100       if (mHandler != null)
168        {
169100          ROOT_LOGGER.removeHandler(mHandler);
170100          mHandler = null;
171        }
172100    }
173  }

Findings in this File

c (1) 50 : 0 Type Javadoc comment is missing an @author tag.
d (2) 50 : 8 Class contains more than one logger.
c (3) 60 : 0 Class LogElementHandler should be declared as final.
c (4) 103 : 0 Class XmlFormatter should be declared as final.
c (5) 106 : 25 Variable 'mPrinter' must be private and have accessor methods.
c (6) 107 : 29 Variable 'mCharWriter' must be private and have accessor methods.
c (7) 108 : 18 Variable 'mXmlPrinter' must be private and have accessor methods.
c (8) 137 : 11 Avoid unnecessary constructors - the compiler will generate these for you
c (9) 142 : 4 Missing a Javadoc comment.