Project Report: fawkez

Packagesummary org.jcoderz.commons.logging

org.jcoderz.commons.logging.LoggerUtil

LineHitsNoteSource
1  /*
2   * $Id: LoggerUtil.java 1302 2009-03-23 21:08:18Z 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  
36  import java.util.Iterator;
37  import java.util.List;
38  import java.util.Set;
39  import java.util.logging.LogRecord;
40  import junit.framework.Assert;
41  import org.jcoderz.commons.Loggable;
42  import org.jcoderz.commons.types.Date;
43  
44  /**
45   * This helper class provides utils for checking log items.
46   *
47   */
48 (1)public final class LoggerUtil
49  {
50     private LoggerUtil ()
510    {
52        // no instances allowed.
530    }
54  
55     /**
56      * This checks the structure of the LogItem against the LogRecord.
57      *
58      * @param item The log item to check.
59      * @param record THe LogRecord to check against.
60      */
61     public static void checkLogItem (
62           final LogItem item,
63           final LogRecord record)
64     {
65100       Throwable thrown = null;
66100       LogItem currentItem = item;
67  
68100       if ((record.getParameters() != null)
69              && record.getParameters()[0] instanceof Loggable)
70        {
71100          final Loggable loggable = (Loggable) record.getParameters()[0];
72100          checkLoggable(item, loggable);
73100          thrown = loggable.getCause();
74100       }
75        else
76        {
77100          thrown = record.getThrown();
78        }
79100       while (thrown != null)
80        {
81100          currentItem = (LogItem) currentItem.getNestedItem();
82100          if (currentItem == null)
83           {
840             Assert.fail("LogRecord has still nested throwables, which are "
85                    + "missing in LogElement");
86           }
87           else
88           {
89100             checkNestedItem(currentItem, thrown);
90           }
91100          thrown = thrown.getCause();
92        }
93100       if (currentItem.getNestedItem() != null)
94        {
950          Assert.fail("LogElement has too much nested elements");
96        }
97100    }
98  
99     private static void checkNestedItem (
100           final LogItem item,
101           final Throwable nested)
102     {
103100       if (!((nested instanceof Loggable) || item.isExceptionItem()))
104        {
1050          Assert.fail("Found exception, but LogElement is not exception item");
106        }
107100(2)      else if ((nested instanceof Loggable) && item.isExceptionItem())
108        {
1090          Assert.fail("Found Loggable, but LogElement is exception item");
110        }
111100       else if (nested instanceof Loggable)
112        {
113100          checkLoggable(item, (Loggable) nested);
114        }
115100    }
116  
117     private static void checkLoggable (
118           final LogItem item,
119           final Loggable loggable)
120     {
121100       Assert.assertEquals("Business Impact must match",
122              item.getBusinessImpact(),
123              loggable.getLogMessageInfo().getBusinessImpact());
124100       Assert.assertEquals("Thread Name must match",
125              item.getThreadName(),
126              loggable.getThreadName());
127100       Assert.assertEquals("Instance ID must match",
128              item.getInstanceId(), loggable.getInstanceId());
129100       Assert.assertEquals("Log level must match",
130              item.getLoggerLevel(),
131              loggable.getLogMessageInfo().getLogLevel());
132100       Assert.assertEquals("Message must match",
133              String.valueOf(item.getMessage()),
134              String.valueOf(loggable.getMessage()));
135100       Assert.assertEquals("Node Id must match",
136              item.getNodeId(), loggable.getNodeId());
137100       Assert.assertEquals("Solution must match",
138              item.getSolution(),
139              loggable.getLogMessageInfo().getSolution());
140100       Assert.assertEquals("Symbol must match",
141              item.getSymbol(), loggable.getLogMessageInfo().getSymbol());
142100       Assert.assertEquals("Symbol ID must match",
143              item.getSymbolId(),
144              Integer.toHexString(loggable.getLogMessageInfo().toInt()));
145100       Assert.assertEquals("Timestamp must match",
146              item.getTimestamp(), new Date(loggable.getEventTime()));
147100       Assert.assertEquals("Tracking number must match",
148              item.getTrackingNumber(), loggable.getTrackingNumber());
149100       Assert.assertEquals("Thread id must match",
150              new Long(item.getThreadId()),
151              new Long(loggable.getThreadId()));
152100       checkParameters(item, loggable);
153100    }
154  
155     private static void checkParameters (
156        final LogItem item,
157        final Loggable loggable)
158     {
159100       final Set epNames = item.getParameterNames();
160100       final Set lpNames = loggable.getParameterNames();
161  
16250       Assert.assertFalse("No parameters for loggable set, but log element "
163              + "has parameters: " + epNames,
164              (lpNames == null && epNames != null && ! epNames.isEmpty()));
165  
166100       if (lpNames != null)
167        {
168100          boolean epNamesIsEmpty = false;
169100          if (!((epNames == null) || epNames.isEmpty()))
170           {
171100             if (!lpNames.containsAll(epNames))
172              {
1730                epNames.removeAll(lpNames);
1740                Assert.fail(
175                       "Formatted element contains parameters not in loggable: "
176                       + epNames);
177              }
178           }
179           else
180           {
181100             epNamesIsEmpty = true;
182           }
183100          for (final Iterator iter = lpNames.iterator(); iter.hasNext(); )
184           {
185100             final String name = (String) iter.next();
186100             if (! name.startsWith(LogItem.INTERNAL_PARAMETER_PREFIX))
187              {
18875                Assert.assertTrue("Item has no parameters, but must have: "
189                       + name, ! epNamesIsEmpty);
190100                Assert.assertTrue("Item must contain parameter " + name,
191                       epNames.contains(name));
192100                final List eparams = item.getParameterValues(name);
193100                final List lparams = loggable.getParameter(name);
194100                Assert.assertEquals("The parameter values must match for "
195                       + name, eparams, lparams);
196              }
197100          }
198        }
199100    }
200  }

Findings in this File

c (1) 48 : 0 Type Javadoc comment is missing an @author tag.
i (2) 107 : 0 Method org.jcoderz.commons.logging.LoggerUtil.checkNestedItem(LogItem, Throwable) uses instanceof on multiple types to arbitrate logic (test code) Decreased severity from 'warning' for testcode.