Project Report: fawkez

Packagesummary org.jcoderz.commons.logging

org.jcoderz.commons.logging.LogViewerTest

LineHitsNoteSource
1  /*
2   * $Id: LogViewerTest.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.text.ParseException;
36  import java.util.logging.Level;
37  import java.util.logging.Logger;
38  import org.jcoderz.commons.ArgumentMalformedException;
39  import org.jcoderz.commons.TestCase;
40  import org.jcoderz.commons.types.Date;
41  import org.jcoderz.commons.types.Period;
42  
43  
44  
45  /**
46   *
47   * Tests several static methods of the class
48   * {@linkplain org.jcoderz.commons.logging.LogViewer}.
49   *
50   */
51100(1)public class LogViewerTest
52        extends TestCase
53  {
54     /** The full qualified name of this class. */
5575    private static final String CLASSNAME = LogViewerTest.class.getName();
56     /** The logger to use. */
57100    private static final Logger logger = Logger.getLogger(CLASSNAME);
58  
59     /**
60      * Tests the method
61      * {@link LogViewer#getDateFromOptValue(String, boolean)}.
62      */
63     public void testGetDateFromOptValue ()
64     {
65100       checkGetDateFromOptValueBad("wrong string");
66100       checkGetDateFromOptValueBad("2005");
67100       checkGetDateFromOptValueBad("2005-11");
68100       checkGetDateFromOptValueBad("2005/11/01");
69100       checkGetDateFromOptValueBad("2005-11-30T14:33:24.123");
70100       checkGetDateFromOptValueBad("2005-11-30T14:33:24");
71  
72100       final String expected = "2005-11-30T00:00:00.000Z";
73100       checkGetDateFromOptValueGood("2005-11-30", expected);
74100       checkGetDateFromOptValueGood("2005-11-30Z", expected);
75100       String same = "2005-11-30T14:33:24Z";
76100       checkGetDateFromOptValueGood(same, same);
77100       same = "2005-11-30T14:33:24.123Z";
78100       checkGetDateFromOptValueGood(same, same);
79100    }
80  
81     /**
82      * Tests the method {@link LogViewer#getDateFrom(String)}.
83      */
84     public void testGetDateFrom ()
85     {
86        try
87        {
88100          assertDate(LogViewer.getDateFrom(null), Date.OLD_DATE);
89100          assertDate(LogViewer.getDateFrom(""), Date.OLD_DATE);
90        }
910       catch (ParseException e)
92        {
930          logErr("LogViewer.getDateFrom ", e);
94100       }
95100    }
96  
97     /**
98      * Tests the method {@link LogViewer#getDateTo(String)}.
99      */
100     public void testGetDateTo ()
101     {
102        try
103        {
104100          assertDate(LogViewer.getDateTo(null), Date.FUTURE_DATE);
105100          assertDate(LogViewer.getDateTo(""), Date.FUTURE_DATE);
106        }
1070       catch (ParseException e)
108        {
1090          logErr("LogViewer.getDateTo ", e);
110100       }
111100    }
112  
113     /**
114      * Tests the method
115      * {@linkplain LogViewer#getPeriodsFromOptionValues(String[])}.
116      */
117     public void testGetPeriodsFromOptionsValues ()
118     {
119100       final String start = "2005-11-30T00:00:00.000Z";
120100       final String end = "2005-11-30T00:00:00.000Z";
121  
122100       assertPeriod(start, end, start, end);
123100       assertPeriod(start, null, start, Date.FUTURE_DATE.toString());
124100       assertPeriod(start, "", start, Date.FUTURE_DATE.toString());
125100       assertPeriod(null, end, Date.OLD_DATE.toString(), end);
126100       assertPeriod("", end, Date.OLD_DATE.toString(), end);
127100    }
128  
129     private void assertPeriod (String start, String end, String expectedStart,
130           String expectedEndend)
131     {
132100       final StringBuffer sb = new StringBuffer();
133100       if (start != null)
134        {
135100          sb.append(start);
136        }
137100       sb.append(',');
138100       if (end != null)
139        {
140100          sb.append(end);
141        }
142100       final String [] values = {sb.toString()};
143  
144        try
145        {
146100          final Period [] expectedPeriod = new Period [] {
147                 Period.createPeriod(Date.fromString(expectedStart),
148                       Date.fromString(expectedEndend))};
149100          final Period [] p = LogViewer.getPeriodsFromOptionValues(values);
150100          assertNotNull("LogViewer.getPeriodsFromOptionValues returned null", p);
151100          assertEquals("Period array has an unexpected length", p.length,
152                 expectedPeriod.length);
153100          assertEquals("Got unexcpected period", p[0], expectedPeriod[0]);
154        }
1550       catch (ArgumentMalformedException e)
156        {
1570          logErr(errMsgGetPeriodsFromOptionsValues(values[0]), e);
158        }
1590       catch (ParseException e)
160        {
1610          logErr(errMsgGetPeriodsFromOptionsValues(values[0]), e);
16250       }
163100    }
164  
165     private void assertDate (Date result, Date expected)
166     {
167100       assertNotNull("Returned value must not be null", result);
168100       assertEquals("Unexpected date", result, expected);
169100    }
170  
171     private void checkGetDateFromOptValueBad (String str)
172     {
173        try
174        {
1750          LogViewer.getDateFromOptValue(str, true);
1760          fail(errMsgGetDateFromOptValue(str) + "must throw the ParseException");
177        }
178100       catch (ParseException e)
179        {
180           // expected
1810       }
182100    }
183  
184     private void checkGetDateFromOptValueGood (String str, String expected)
185     {
186100       Date result = null;
187100       Date expDate = null;
188        try
189        {
190100          expDate = Date.fromString(expected);
191100          result = LogViewer.getDateFromOptValue(str, true);
192        }
1930       catch (Exception e)
194        {
1950          logErr(errMsgGetDateFromOptValue(str), e);
196100       }
197100       assertNotNull("Got a null date", result);
198100       assertEquals(errMsgGetDateFromOptValue(str)
199              + "returned unexcpected date.", result, expDate);
200100    }
201  
202     private String errMsgGetDateFromOptValue (String str)
203     {
204100       return "LogViewer.getDateFromOptValue(" + str + ", true) ";
205     }
206  
207     private String errMsgGetPeriodsFromOptionsValues (String str)
208     {
2090       return "LogViewer.getPeriodsFromOptionsValues(" + str + ") ";
210     }
211  
212     private void logErr (String str, Exception e)
213     {
2140       final String msg = str + " failed due to unexpected Exception "
215              + e.getMessage();
2160       logger.log(Level.SEVERE, msg, e);
2170       fail(msg);
2180    }
219  }

Findings in this File

c (1) 51 : 0 Type Javadoc comment is missing an @author tag.