| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 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 | { |
|---|
| 75 | LoggingUtils.setGlobalHandlerLogLevel(Level.ALL); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | /** |
|---|
| 79 | * Default constructor. |
|---|
| 80 | */ |
|---|
| 81 | public TestCase () |
|---|
| 82 | { |
|---|
| 83 | super(); |
|---|
| 84 | } |
|---|
| 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 | { |
|---|
| 92 | super(name); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | /** |
|---|
| 96 | * Returns the projects base directory. |
|---|
| 97 | * @return the projects base directory. |
|---|
| 98 | */ |
|---|
| 99 | public static File getBaseDir () |
|---|
| 100 | { |
|---|
| 101 | 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 | { |
|---|
| 111 | String result = null; |
|---|
| 112 | try |
|---|
| 113 | { |
|---|
| 114 | final InetAddress addr = InetAddress.getLocalHost(); |
|---|
| 115 | result = addr.getCanonicalHostName(); |
|---|
| 116 | } |
|---|
| 117 | catch (UnknownHostException e) |
|---|
| 118 | { |
|---|
| 119 | // ignore |
|---|
| 120 | } |
|---|
| 121 | 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 | { |
|---|
| 131 | 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 | { |
|---|
| 156 | if (!TestCase.class.isAssignableFrom(testClass)) |
|---|
| 157 | { |
|---|
| 158 | throw new IllegalArgumentException |
|---|
| 159 | ("Must pass in a subclass of TestCase"); |
|---|
| 160 | } |
|---|
| 161 | final TestSuite suite = new TestSuite(); |
|---|
| 162 | try |
|---|
| 163 | { |
|---|
| 164 | final Constructor constructor |
|---|
| 165 | = testClass.getConstructor(new Class[] {}); |
|---|
| 166 | final List testCaseNames = getTestCaseNames(); |
|---|
| 167 | for (final Iterator testCases = testCaseNames.iterator(); |
|---|
| 168 | testCases.hasNext();) |
|---|
| 169 | { |
|---|
| 170 | final String testCaseName = (String) testCases.next(); |
|---|
| 171 | final TestCase test = (TestCase) constructor.newInstance( |
|---|
| 172 | new Object[] {}); |
|---|
| 173 | test.setName(testCaseName); |
|---|
| 174 | suite.addTest(test); |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | catch (Exception e) |
|---|
| 178 | { |
|---|
| 179 | throw new RuntimeException |
|---|
| 180 | (testClass.getName() + " doesn't have the proper constructor"); |
|---|
| 181 | } |
|---|
| 182 | 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 | { |
|---|
| 197 | if (System.getProperty(TEST_METHODS) == null) |
|---|
| 198 | { |
|---|
| 199 | throw new NullPointerException( |
|---|
| 200 | "Property <" + TEST_METHODS + "> is not set"); |
|---|
| 201 | } |
|---|
| 202 | final List testCaseNames = new ArrayList(); |
|---|
| 203 | final String testCases = System.getProperty(TEST_METHODS); |
|---|
| 204 | final StringTokenizer tokenizer |
|---|
| 205 | = new StringTokenizer(testCases, DELIMITER); |
|---|
| 206 | while (tokenizer.hasMoreTokens()) |
|---|
| 207 | { |
|---|
| 208 | testCaseNames.add(tokenizer.nextToken()); |
|---|
| 209 | } |
|---|
| 210 | return testCaseNames; |
|---|
| 211 | } |
|---|
| 212 | } |
|---|