| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 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 | | | |
| 51 | | | |
| 52 | | | @author |
| 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 | | | |
| 63 | | | |
| 64 | | | private static final String BASEDIR = "basedir"; |
| 65 | | | |
| 66 | | | |
| 67 | | | |
| 68 | | | |
| 69 | | | |
| 70 | | | private static final String DEFAULT_BASEDIR = "."; |
| 71 | | | |
| 72 | | | |
| 73 | | | static |
| 74 | | | { |
| 75 | 100 | (1) | LoggingUtils.setGlobalHandlerLogLevel(Level.ALL); |
| 76 | 100 | | } |
| 77 | | | |
| 78 | | | |
| 79 | | | |
| 80 | | | |
| 81 | | | public TestCase () |
| 82 | | | { |
| 83 | 100 | | super(); |
| 84 | 100 | | } |
| 85 | | | |
| 86 | | | |
| 87 | | | <code></code> |
| 88 | | | @param |
| 89 | | | |
| 90 | | | public TestCase (String name) |
| 91 | | | { |
| 92 | 100 | | super(name); |
| 93 | 100 | | } |
| 94 | | | |
| 95 | | | |
| 96 | | | |
| 97 | | | @return |
| 98 | | | |
| 99 | | | public static File getBaseDir () |
| 100 | | | { |
| 101 | 100 | | return new File(System.getProperty(BASEDIR, DEFAULT_BASEDIR)); |
| 102 | | | } |
| 103 | | | |
| 104 | | | |
| 105 | | | |
| 106 | | | |
| 107 | | | @return |
| 108 | | | |
| 109 | | | public static String getHostName () |
| 110 | | | { |
| 111 | 0 | | String result = null; |
| 112 | | | try |
| 113 | | | { |
| 114 | 0 | | final InetAddress addr = InetAddress.getLocalHost(); |
| 115 | 0 | | result = addr.getCanonicalHostName(); |
| 116 | | | } |
| 117 | 0 | | catch (UnknownHostException e) |
| 118 | | | { |
| 119 | | | |
| 120 | 0 | | } |
| 121 | 0 | | return result; |
| 122 | | | } |
| 123 | | | |
| 124 | | | |
| 125 | | | |
| 126 | | | |
| 127 | | | @return |
| 128 | | | |
| 129 | | | public static boolean hasTestCases () |
| 130 | | | { |
| 131 | 60 | (2) | return |
| 132 | | | System.getProperty(TEST_METHODS) == null |
| 133 | | | || System.getProperty(TEST_METHODS).equals(ANT_PROPERTY) |
| 134 | | | ? false : true; |
| 135 | | | } |
| 136 | | | |
| 137 | | | |
| 138 | | | |
| 139 | | | |
| 140 | | | |
| 141 | | | @param |
| 142 | | | |
| 143 | | | |
| 144 | | | @return |
| 145 | | | |
| 146 | | | |
| 147 | | | @throws |
| 148 | | | |
| 149 | | | |
| 150 | | | @throws |
| 151 | | | |
| 152 | | | |
| 153 | | | public static TestSuite getSuite (Class testClass) |
| 154 | | | throws RuntimeException, IllegalArgumentException |
| 155 | | | { |
| 156 | 0 | | if (!TestCase.class.isAssignableFrom(testClass)) |
| 157 | | | { |
| 158 | 0 | (3) | throw new IllegalArgumentException |
| 159 | | | ("Must pass in a subclass of TestCase"); |
| 160 | | | } |
| 161 | 0 | | final TestSuite suite = new TestSuite(); |
| 162 | | | try |
| 163 | | | { |
| 164 | 0 | | final Constructor constructor |
| 165 | | | = testClass.getConstructor(new Class[] {}); |
| 166 | 0 | | final List testCaseNames = getTestCaseNames(); |
| 167 | 0 | | for (final Iterator testCases = testCaseNames.iterator(); |
| 168 | 0 | | testCases.hasNext();) |
| 169 | | | { |
| 170 | 0 | | final String testCaseName = (String) testCases.next(); |
| 171 | 0 | | final TestCase test = (TestCase) constructor.newInstance( |
| 172 | | | new Object[] {}); |
| 173 | 0 | | test.setName(testCaseName); |
| 174 | 0 | | suite.addTest(test); |
| 175 | 0 | | } |
| 176 | | | } |
| 177 | 0 | | catch (Exception e) |
| 178 | | | { |
| 179 | 0 | (4) | throw new RuntimeException |
| 180 | | | (testClass.getName() + " doesn't have the proper constructor"); |
| 181 | 0 | | } |
| 182 | 0 | | return suite; |
| 183 | | | } |
| 184 | | | |
| 185 | | | |
| 186 | | | |
| 187 | | | |
| 188 | | | |
| 189 | | | @return |
| 190 | | | |
| 191 | | | @throws |
| 192 | | | |
| 193 | | | |
| 194 | | | private static List getTestCaseNames () |
| 195 | | | throws NullPointerException |
| 196 | | | { |
| 197 | 0 | | if (System.getProperty(TEST_METHODS) == null) |
| 198 | | | { |
| 199 | 0 | | throw new NullPointerException( |
| 200 | | | "Property <" + TEST_METHODS + "> is not set"); |
| 201 | | | } |
| 202 | 0 | | final List testCaseNames = new ArrayList(); |
| 203 | 0 | | final String testCases = System.getProperty(TEST_METHODS); |
| 204 | 0 | | final StringTokenizer tokenizer |
| 205 | | | = new StringTokenizer(testCases, DELIMITER); |
| 206 | 0 | | while (tokenizer.hasMoreTokens()) |
| 207 | | | { |
| 208 | 0 | | testCaseNames.add(tokenizer.nextToken()); |
| 209 | | | } |
| 210 | 0 | | return testCaseNames; |
| 211 | | | } |
| 212 | | | } |