1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package junit.framework; 18 19 // Note: this class was written without inspecting the junit.framework code 20 21 import java.util.ArrayList; 22 import java.util.Collections; 23 import java.util.Enumeration; 24 import java.util.List; 25 import java.util.ListIterator; 26 import vogar.target.junit.Junit3; 27 28 public class TestSuite implements Test { 29 /** A heterogeneous list containing of tests and test classes. */ 30 private final List<Object> testsAndSuites = new ArrayList<Object>(); 31 private final String name; 32 TestSuite()33 public TestSuite() { 34 this.name = null; 35 } 36 TestSuite(String name)37 public TestSuite(String name) { 38 this.name = name; 39 } 40 TestSuite(Class<?> suite)41 public TestSuite(Class<?> suite) { 42 this(suite, null); 43 } 44 TestSuite(Class<?> suite, String name)45 public TestSuite(Class<?> suite, String name) { 46 if (suite == null) { 47 throw new IllegalArgumentException("suite == null"); 48 } 49 testsAndSuites.add(suite); 50 this.name = name; 51 } 52 getName()53 public String getName() { 54 return name; 55 } 56 addTest(Test test)57 public void addTest(Test test) { 58 if (!(test instanceof TestCase) && !(test instanceof TestSuite)) { 59 throw new IllegalArgumentException("Unexpected test: " + test); 60 } 61 testsAndSuites.add(test); 62 } 63 addTestSuite(Class<?> suite)64 public void addTestSuite(Class<?> suite) { 65 testsAndSuites.add(suite); 66 } 67 countTestCases()68 public int countTestCases() { 69 return testsAndSuites.size(); 70 } 71 72 /** 73 * The official JUnit framework creates test instances eagerly and holds 74 * them for the duration of the test run. We prefer to create tests lazily, 75 * and release them after use. Unfortunately, calls to this method require 76 * us to fall back to JUnit-style eager creation. This method should only be 77 * used by framework code. 78 */ tests()79 public Enumeration<?> tests() { 80 for (ListIterator<Object> i = testsAndSuites.listIterator(); i.hasNext(); ) { 81 Object o = i.next(); 82 if (o instanceof Class) { 83 i.remove(); 84 for (Test test : Junit3.classToJunitTests((Class<?>) o)) { 85 i.add(test); 86 } 87 } 88 } 89 return Collections.enumeration(testsAndSuites); 90 } 91 getTestsAndSuites()92 public final List<Object> getTestsAndSuites() { 93 return testsAndSuites; 94 } 95 } 96