git clone of logicmail with some fixes/features added
at master 249 lines 6.9 kB view raw
1//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2// This file is part of J2MEUnit, a Java 2 Micro Edition unit testing framework. 3// 4// J2MEUnit is free software distributed under the Common Public License (CPL). 5// It may be redistributed and/or modified under the terms of the CPL. You 6// should have received a copy of the license along with J2MEUnit. It is also 7// available from the website of the Open Source Initiative at 8// http://www.opensource.org. 9// 10// J2MEUnit is distributed in the hope that it will be useful, but WITHOUT ANY 11// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 12// FOR A PARTICULAR PURPOSE. 13//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 14package j2meunit.framework; 15 16import java.util.*; 17 18 19/******************************************************************** 20 * A <code>TestSuite</code> is a collection of Test instances which can be run 21 * together. It is created by adding single tests (normally by using a 22 * TestMethod) or other suites to it: 23 * <pre> 24 * TestSuite suite = new TestSuite(); 25 * suite.addTest(new MathTest("testAdd")); 26 * suite.addTest(new MathTest("testDivideByZero")); 27 * suite.addTest(new ValueTest().suite()); 28 * </pre> 29 * 30 * @author $author$ 31 * @version $Revision: 10 $ 32 */ 33public class TestSuite implements Test 34{ 35 //~ Instance fields -------------------------------------------------------- 36 37 private String fName; 38 private Vector fTests = new Vector(10); 39 40 //~ Constructors ----------------------------------------------------------- 41 42 /*************************************** 43 * Default constructor. 44 */ 45 public TestSuite() 46 { 47 } 48 49 /*************************************** 50 * To create a test suite with a particular name. 51 * 52 * @param sName The name of the test suite. 53 */ 54 public TestSuite(String sName) 55 { 56 fName = sName; 57 } 58 59 /*************************************** 60 * To create a test suite initialized with a single test. 61 * 62 * @param rTest The test to add to the suite 63 */ 64 public TestSuite(Test rTest) 65 { 66 addTest(rTest); 67 } 68 69 /*************************************** 70 * To create a test suite initialized with multiple tests. 71 * 72 * @param rTests The tests to add to the suite 73 */ 74 public TestSuite(Test[] rTests) 75 { 76 for (int i = 0; i < rTests.length; i++) 77 addTest(rTests[i]); 78 } 79 80 /*************************************** 81 * Creates a new test suite for certain methods of a particular test case 82 * class. The methods are defined as strings that should contain the method 83 * names. These names will be stored in the created TestCase instances as 84 * their names. Because J2ME doesn't provide reflection, the TestCase class 85 * must implement the runTest() method and invoke the corresponding methods 86 * by doing a string compare with the name attribute of the test case 87 * instance. 88 * <p> 89 * Since version 1.1 the recommended (and easier) method to create test 90 * instances and suites is to use the TestMethod interface to wrap the 91 * methods of a test case in an anonymous inner class, initialize a TestCase 92 * instance for each, and then hand the test(s) over to one of the 93 * constructors of TestSuite that accept Test instances. 94 * 95 * @param theClass The Class instance of a TestCase subclass 96 * @param testNames The names of the methods to run 97 */ 98 public TestSuite(Class theClass, String[] testNames) 99 { 100 this(theClass.getName()); 101 102 for (int i = 0; i < testNames.length; i++) 103 { 104 TestCase testCase = null; 105 106 try 107 { 108 testCase = (TestCase) theClass.newInstance(); 109 } 110 catch (Exception e) 111 { 112 String sMessage = "Need to have public default constructor in " + 113 theClass.getName(); 114 System.out.println(sMessage); 115 throw new RuntimeException(sMessage); 116 } 117 118 testCase.setName(testNames[i]); 119 addTest(testCase); 120 } 121 } 122 123 //~ Methods ---------------------------------------------------------------- 124 125 /*************************************** 126 * Adds a test to the suite. 127 * 128 * @param test The test to add 129 */ 130 public void addTest(Test test) 131 { 132 fTests.addElement(test); 133 } 134 135 /*************************************** 136 * Counts the number of test cases that will be run by this suite. 137 * 138 * @return The number of test cases to be run by the suite 139 */ 140 public int countTestCases() 141 { 142 int count = 0; 143 144 for (int i = 0, cnt = fTests.size(); i < cnt; i++) 145 count += ((Test) fTests.elementAt(i)).countTestCases(); 146 147 return count; 148 } 149 150 /*************************************** 151 * Counts the number of test steps that will be run by this suite. 152 * 153 * @return The number of test steps to be run by the suite 154 * 155 * @see Test#countTestSteps() 156 */ 157 public int countTestSteps() 158 { 159 int count = 0; 160 161 for (int i = 0, cnt = fTests.size(); i < cnt; i++) 162 count += ((Test) fTests.elementAt(i)).countTestSteps(); 163 164 return count; 165 } 166 167 /*************************************** 168 * Runs the tests and collects their result in a TestResult. 169 * 170 * @param result The TestResult to collect the results in 171 */ 172 public void run(TestResult result) 173 { 174 for (Enumeration e = tests(); e.hasMoreElements();) 175 { 176 if (result.shouldStop()) 177 break; 178 179 Test test = (Test) e.nextElement(); 180 test.run(result); 181 } 182 } 183 184 /*************************************** 185 * Returns the test at the given index. 186 * 187 * @param index The index position of the test 188 * 189 * @return The test at the index position 190 */ 191 public Test testAt(int index) 192 { 193 return (Test) fTests.elementAt(index); 194 } 195 196 /*************************************** 197 * Returns the number of tests in this suite. 198 * 199 * @return The test count of the suite 200 */ 201 public int testCount() 202 { 203 return fTests.size(); 204 } 205 206 /*************************************** 207 * Returns the tests as an enumeration. 208 * 209 * @return A java.util.Enumeration for all tests 210 */ 211 public Enumeration tests() 212 { 213 return fTests.elements(); 214 } 215 216 /*************************************** 217 * Create a string description of the suite. 218 * 219 * @return A description of the suite 220 */ 221 public String toString() 222 { 223 if (fName != null) 224 return fName; 225 else 226 227 return super.toString(); 228 } 229 230// Commented to avoid warnings from unused code 231// /*************************************** 232// * Internal method to returns a test which will fail and log a warning 233// * message. 234// * 235// * @param message The message to display 236// * 237// * @return A new TestCase instance 238// */ 239// private Test warning(final String message) 240// { 241// return new TestCase("warning") 242// { 243// protected void runTest() 244// { 245// fail(message); 246// } 247// }; 248// } 249}