Project Report: fawkez

Packagesummary org.jcoderz.commons

org.jcoderz.commons.TestCase

LineHitsNoteSource
1  /*
2   * $Id: TestCase.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.File;
36  import java.lang.reflect.Constructor;
37  import java.net.InetAddress;
38  import java.net.UnknownHostException;
39  import java.util.ArrayList;
40  import java.util.Iterator;
41  import java.util.List;
42  import java.util.StringTokenizer;
43  import java.util.logging.Level;
44  
45  import junit.framework.TestSuite;
46  
47  import org.jcoderz.commons.util.LoggingUtils;
48  
49  /**
50   * Base class for a JUnit test that provides additional utility methods.
51   *
52   * @author Michael Griffel
53   */
54  public abstract class TestCase
55        extends junit.framework.TestCase
56  {
57     private static final String TEST_METHODS = "methods";
58     private static final String ANT_PROPERTY = "${methods}";
59     private static final String DELIMITER = ",";
60  
61     /**
62      * System property name for the projects base directory.
63      */
64     private static final String BASEDIR = "basedir";
65  
66     /**
67      * Default base directory if the system property for the base directory
68      * is not set.
69      */
70     private static final String DEFAULT_BASEDIR = ".";
71  
72     // sets all loggers to Level.ALL
73     static
74     {
75100(1)      LoggingUtils.setGlobalHandlerLogLevel(Level.ALL);
76100    }
77  
78     /**
79      * Default constructor.
80      */
81     public TestCase ()
82     {
83100       super();
84100    }
85  
86     /**
87      * Constructs a TestCase with the given <code>name</code>.
88      * @param name The test case name.
89      */
90     public TestCase (String name)
91     {
92100       super(name);
93100    }
94  
95     /**
96      * Returns the projects base directory.
97      * @return the projects base directory.
98      */
99     public static File getBaseDir ()
100     {
101100       return new File(System.getProperty(BASEDIR, DEFAULT_BASEDIR));
102     }
103  
104     /**
105      * Returns the hostname of localhost.
106      *
107      * @return String the hostname of localhost.
108      */
109     public static String getHostName ()
110     {
1110       String result = null;
112        try
113        {
1140          final InetAddress addr = InetAddress.getLocalHost();
1150          result = addr.getCanonicalHostName();
116        }
1170       catch (UnknownHostException e)
118        {
119           // ignore
1200       }
1210       return result;
122     }
123  
124     /**
125      * Check to see if the test cases property is set. Ignores Ant's
126      * default setting for the property (or null to be on the safe side).
127      * @return boolean true if the test case property is set, false else
128      **/
129     public static boolean hasTestCases ()
130     {
13160(2)       return
132             System.getProperty(TEST_METHODS) == null
133             || System.getProperty(TEST_METHODS).equals(ANT_PROPERTY)
134             ? false : true;
135     }
136  
137      /**
138       * Create a TestSuite using the TestCase subclass and the list
139       * of test cases to run specified using the TEST_CASES JVM property.
140       *
141       * @param testClass the TestCase subclass to instantiate as tests in
142       * the suite.
143       *
144       * @return a TestSuite with new instances of testClass for each
145       * test case specified in the JVM property.
146       *
147       * @throws IllegalArgumentException if testClass is not a subclass or
148       * implementation of junit.framework.TestCase.
149       *
150       * @throws RuntimeException if testClass is written incorrectly and does
151       * not have the approriate constructor.
152       **/
153     public static TestSuite getSuite (Class testClass)
154           throws RuntimeException, IllegalArgumentException
155     {
1560       if (!TestCase.class.isAssignableFrom(testClass))
157        {
1580(3)         throw new IllegalArgumentException
159              ("Must pass in a subclass of TestCase");
160        }
1610       final TestSuite suite = new TestSuite();
162        try
163        {
1640          final Constructor constructor
165                 = testClass.getConstructor(new Class[] {});
1660          final List testCaseNames = getTestCaseNames();
1670          for (final Iterator testCases = testCaseNames.iterator();
1680                   testCases.hasNext();)
169           {
1700             final String testCaseName = (String) testCases.next();
1710             final TestCase test = (TestCase) constructor.newInstance(
172                    new Object[] {});
1730             test.setName(testCaseName);
1740             suite.addTest(test);
1750          }
176        }
1770       catch (Exception e)
178        {
1790(4)         throw new RuntimeException
180                 (testClass.getName() + " doesn't have the proper constructor");
1810       }
1820       return suite;
183     }
184  
185     /**
186      * Create a List of String names of test cases specified in the
187      * JVM property in comma-separated format.
188      *
189      * @return a List of String test case names
190      *
191      * @throws NullPointerException if the TEST_CASES property
192      * isn't set
193      **/
194     private static List getTestCaseNames ()
195           throws NullPointerException
196     {
1970       if (System.getProperty(TEST_METHODS) == null)
198        {
1990          throw new NullPointerException(
200                 "Property <" + TEST_METHODS + "> is not set");
201        }
2020       final List testCaseNames = new ArrayList();
2030       final String testCases = System.getProperty(TEST_METHODS);
2040       final StringTokenizer tokenizer
205              = new StringTokenizer(testCases, DELIMITER);
2060       while (tokenizer.hasMoreTokens())
207        {
2080          testCaseNames.add(tokenizer.nextToken());
209        }
2100       return testCaseNames;
211     }
212  }

Findings in this File

w (1) 75 : 0 The class name org.jcoderz.commons.TestCase shadows the simple name of the superclass junit.framework.TestCase (test code) Decreased severity from 'error' for testcode.
i (2) 131 : 0 method org.jcoderz.commons.TestCase.hasTestCases() makes literal string comparisons passing the literal as an argument (test code)
i (3) 158 : 0 method org.jcoderz.commons.TestCase.getSuite(Class) throws exception with static message string (test code)
i (4) 179 : 0 Method org.jcoderz.commons.TestCase.getSuite(Class) throws alternative exception from catch block without history (test code) Decreased severity from 'warning' for testcode.