Project Report: fawkez

Packagesummary org.jcoderz.commons

org.jcoderz.commons.LogMessageInfoTest

LineHitsNoteSource
1  /*
2   * $Id: LogMessageInfoTest.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;
34  
35  import java.io.ByteArrayInputStream;
36  import java.io.ByteArrayOutputStream;
37  import java.io.ObjectInputStream;
38  import java.io.ObjectOutputStream;
39  import java.util.Collections;
40  import java.util.Date;
41  import java.util.HashMap;
42  import java.util.Map;
43  import java.util.logging.Level;
44  import junit.framework.TestCase;
45  
46  /**
47   * Tests the LogMessageInfo class.
48   *
49   * @author Michael Griffel
50   */
51100 public class LogMessageInfoTest
52        extends TestCase
53  {
54100    private static final TstLogMessage CODE = TstLogMessage.TEST_MESSAGE;
55  
56     /**
57      * Tests that it is possible to use the int representation of the log
58      * message in a switch statement.
59      */
60     public void testMisc ()
61     {
62  
63100       switch (CODE.toInt())
64        {
65           case TstLogMessage.TestMessage.INT_VALUE:
66                 // expected
67100             break;
68           default:
690             fail("Unexpected int " + CODE.toInt() + ", expected "
70                    + TstLogMessage.TestMessage.INT_VALUE);
71        }
72100    }
73  
74     /**
75      * Tests the method {@link TstLogMessage#fromString(String)}.
76      */
77     public void testFromString ()
78     {
79100       assertSame("fromString method should return the same instance", CODE,
80              TstLogMessage.fromString(CODE.toString()));
81100    }
82  
83     /**
84      * Tests the method {@link TstLogMessage#fromInt(int)}.
85      */
86     public void testFromInt ()
87     {
88100       assertSame("fromInt method should return the same instance", CODE,
89              TstLogMessage.fromInt(CODE.toInt()));
90100    }
91  
92     /**
93      * Tests the method {@link LogMessageInfoImpl#toInt()}.
94      */
95     public void testToInt ()
96     {
97100       assertEquals("constant int representation",
98              TstLogMessage.TestMessage.INT_VALUE,
99              TstLogMessage.TEST_MESSAGE.toInt());
100100    }
101  
102     /**
103      * Tests the method {@link LogMessageInfoImpl#toString()}.
104      */
105     public void testToString ()
106     {
107100       assertEquals("string representation should be the message symbol",
108              TstLogMessage.TEST_MESSAGE.getSymbol(),
109              TstLogMessage.TEST_MESSAGE.toString());
110100    }
111  
112     /**
113      * Tests the method {@link LogMessageInfoImpl#getSymbol()}.
114      */
115     public void testGetSymbol ()
116     {
117100       assertEquals("message symbol",
118              "FWK_TST_TEST_MESSAGE", TstLogMessage.TEST_MESSAGE.getSymbol());
119100    }
120  
121     /**
122      * Tests the method {@link LogMessageInfoImpl#getLogLevel()}.
123      */
124     public void testGetLogLevel ()
125     {
126100       assertEquals("default log level should be OFF",
127              Level.INFO, TstLogMessage.TEST_MESSAGE.getLogLevel());
128100    }
129  
130     /**
131      * Tests the method {@link LogMessageInfoImpl#getMessagePattern()}.
132      */
133     public void testGetMessagePattern ()
134     {
135100       final String regex = ".*\\{0\\}.*\\{1,date\\}.*\\{1,time\\}.*";
136100       assertTrue("message pattern test",
137              TstLogMessage.TEST_MESSAGE.getMessagePattern().matches(regex));
138100    }
139  
140     /**
141      * Tests the method {@link LogMessageInfoImpl#formatMessage(Map, StringBuffer)}.
142      */
143     public void testFormatMessage ()
144     {
145100       final Map parameters = new HashMap();
146100       parameters.put(TstLogMessage.TestMessage.PARAM_FOO,
147              Collections.singletonList("param"));
148100       parameters.put(TstLogMessage.TestMessage.PARAM_NOW,
149              Collections.singletonList(new Date()));
150100(1)      System.out.println("message: "
151              + TstLogMessage.TEST_MESSAGE.formatMessage(parameters, null));
152100    }
153  
154     /**
155      * Tests the method {@link LogMessageInfoImpl#getSolution()}.
156      */
157     public void testGetSolution ()
158     {
159100       assertNotNull("test solution", TstLogMessage.TEST_MESSAGE.getSolution());
160100    }
161  
162     /**
163      * Tests the method {@link LogMessageInfoImpl#getBusinessImpact()}.
164      */
165     public void testGetBusinessImpact ()
166     {
167100       assertEquals("test business impact", BusinessImpact.UNDEFINED,
168              TstLogMessage.TEST_MESSAGE.getBusinessImpact());
169100    }
170  
171     /**
172      * Tests the method {@link Object#hashCode()}.
173      */
174     public void testHashCode ()
175     {
176100       assertEquals("fromString method should return the same hashCode",
177              CODE.hashCode(),
178              TstLogMessage.fromString(CODE.toString()).hashCode());
179100       assertEquals("fromInt method should return the same hashCode",
180              CODE.hashCode(),
181              TstLogMessage.fromInt(CODE.toInt()).hashCode());
182100    }
183  
184     /**
185      * Tests the method readResolve().
186      * @throws Exception in case of an unexpected error.
187      */
188     public void testSerialize ()
189           throws Exception
190     {
191100       final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
192100       final ObjectOutputStream objOut = new ObjectOutputStream(bOut);
193  
194100       objOut.writeObject(CODE);
195100       objOut.flush();
196  
197100       final ByteArrayInputStream bIn
198              = new ByteArrayInputStream(bOut.toByteArray());
199100       final ObjectInputStream objIn = new ObjectInputStream(bIn);
200  
201100       final TstLogMessage p = (TstLogMessage) objIn.readObject();
202  
203100       assertEquals("Object must be equal after serialization", p, CODE);
204100       assertSame("Object must be same(!) after serialization", p, CODE);
205100    }
206  
207     /**
208      * Test the usage of the test message w/ exception wrapper.
209      */
210     public void testSampleError ()
211     {
212100       final String parameter = "bar";
213100       final SampleError error = new SampleError(parameter);
214100       error.log();
215100       assertEquals("The parameter foo should not be modified",
216              parameter,
217              error.getParameter(TstLogMessage.TestMessage.PARAM_FOO).get(0));
218100    }
219  
220     /**
221      * Tests the serializable implementation.
222      * @throws Exception in case of an unexpected error.
223      */
224     public void testSerializable ()
225           throws Exception
226     {
227100       final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
228100       final ObjectOutputStream objOut = new ObjectOutputStream(bOut);
229        ByteArrayInputStream bIn;
230        ObjectInputStream objIn;
231  
232100       objOut.writeObject(TstLogMessage.TEST_MESSAGE);
233100       objOut.flush();
234100       bIn = new ByteArrayInputStream(bOut.toByteArray());
235100       objIn = new ObjectInputStream(bIn);
236100       final TstLogMessage messageRead = (TstLogMessage) objIn.readObject();
237  
238100       assertSame("Values or reference changed during serialization.",
239              TstLogMessage.TEST_MESSAGE, messageRead);
240100    }
241  
242     /**
243      * Tests the if the special audit log event is generated.
244      */
245     public void testAuditLogEvent ()
246     {
247100       final String dummyToString = "Dummy Audit Log Event";
248100       final AuditPrincipal principal = new AuditPrincipal()
249100(2)         {
250              private static final long serialVersionUID = 1L;
251  
252              public String toString ()
253              {
254100                return dummyToString;
255              }
256           };
257100       final AuditLogEvent event
258              = TstLogMessage.TestAuditMessage.create(principal);
259100       assertEquals("Expected dummy to string representation", dummyToString,
260              event.getAuditPrincipal().toString());
261100       event.log();
262100    }
263  
264     /**
265      * Sample exception using test log message.
266      *
267      * @author Michael Griffel
268      */
269100    public static final class SampleError
270           extends BaseException
271        {
272           static final long serialVersionUID = 1;
273  
274           /**
275            * Constructor.
276            * @param foo The parameter value for foo.
277            */
278           public SampleError (String foo)
279           {
280100             super(TstLogMessage.TEST_MESSAGE);
281100             addParameter(TstLogMessage.TestMessage.PARAM_FOO, foo);
282100             addParameter(TstLogMessage.TestMessage.PARAM_NOW, new Date());
283100          }
284        }
285  
286  }

Findings in this File

d (1) 150 : 7 System.out.print is used
i (2) 249 : 0 The class org.jcoderz.commons.LogMessageInfoTest$1 could be refactored into a named _static_ inner class (test code)