1 /* 2 * Copyright (C) 2008 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 android.test.suitebuilder; 18 19 import com.android.internal.util.Predicate; 20 import static android.test.suitebuilder.ListTestCaseNames.getTestCaseNames; 21 import android.test.suitebuilder.examples.OuterTest; 22 import android.test.suitebuilder.examples.suppress.SuppressedTest; 23 import android.test.suitebuilder.examples.error.ErrorTest; 24 import android.test.suitebuilder.examples.error.FailingTest; 25 import android.test.suitebuilder.examples.nested.Level1Test; 26 import android.test.suitebuilder.examples.nested.nested.Level2Test; 27 import android.test.suitebuilder.examples.simple.SimpleTest; 28 import android.test.suitebuilder.examples.subclass.SubclassTest; 29 import junit.framework.AssertionFailedError; 30 import junit.framework.Test; 31 import junit.framework.TestCase; 32 import junit.framework.TestListener; 33 import junit.framework.TestResult; 34 import junit.framework.TestSuite; 35 36 import java.util.HashSet; 37 import java.util.List; 38 import java.util.Set; 39 40 41 public class TestSuiteBuilderTest extends TestCase { 42 43 private TestSuiteBuilder testSuiteBuilder; 44 setUp()45 protected void setUp() throws Exception { 46 super.setUp(); 47 testSuiteBuilder = new TestSuiteBuilder(getClass()); 48 } 49 testShouldRunSimpleTests()50 public void testShouldRunSimpleTests() throws Exception { 51 testSuiteBuilder.includePackages(packageFor(SimpleTest.class)); 52 53 SuiteExecutionRecorder recorder = runSuite(testSuiteBuilder); 54 55 assertTrue(recorder.passed("SimpleTest.testSimpleOne")); 56 assertTrue(recorder.passed("SimpleTest.testSimpleTwo")); 57 assertTrue(recorder.passed("AnotherSimpleTest.testAnotherOne")); 58 } 59 testShouldOnlyIncludeTestsThatSatisfyAllPredicates()60 public void testShouldOnlyIncludeTestsThatSatisfyAllPredicates() throws Exception { 61 testSuiteBuilder.includePackages(packageFor(SimpleTest.class)) 62 .addRequirements(testsWhoseNameContains("test")) 63 .addRequirements(testsWhoseNameContains("Simple")) 64 .addRequirements(testsWhoseNameContains("Two")); 65 66 SuiteExecutionRecorder recorder = runSuite(testSuiteBuilder); 67 68 assertTrue(recorder.passed("SimpleTest.testSimpleTwo")); 69 } 70 testShouldAddFailingTestsToSuite()71 public void testShouldAddFailingTestsToSuite() throws Exception { 72 testSuiteBuilder.includePackages(packageFor(FailingTest.class)); 73 74 SuiteExecutionRecorder recorder = runSuite(testSuiteBuilder); 75 76 assertTrue(recorder.failed("FailingTest.testFailOne")); 77 assertTrue(recorder.failed("FailingTest.testFailTwo")); 78 } 79 testShouldAddTestsWithErrorsToSuite()80 public void testShouldAddTestsWithErrorsToSuite() throws Exception { 81 testSuiteBuilder.includePackages(packageFor(ErrorTest.class)); 82 83 SuiteExecutionRecorder recorder = runSuite(testSuiteBuilder); 84 85 assertTrue(recorder.errored("ErrorTest.testErrorOne")); 86 assertTrue(recorder.errored("ErrorTest.testErrorTwo")); 87 } 88 testShouldRunTestsInheritedFromSuperclass()89 public void testShouldRunTestsInheritedFromSuperclass() throws Exception { 90 testSuiteBuilder.includePackages(packageFor(SubclassTest.class)); 91 92 SuiteExecutionRecorder recorder = runSuite(testSuiteBuilder); 93 94 assertEquals(2, getTestCaseNames(testSuiteBuilder.build()).size()); 95 96 assertTrue(recorder.passed("SubclassTest.testSubclass")); 97 assertTrue(recorder.passed("SubclassTest.testSuperclass")); 98 assertFalse(recorder.saw("SuperclassTest.testSuperclass")); 99 } 100 testShouldIncludeTestsInSubPackagesRecursively()101 public void testShouldIncludeTestsInSubPackagesRecursively() throws Exception { 102 testSuiteBuilder.includePackages(packageFor(Level1Test.class)); 103 104 SuiteExecutionRecorder recorder = runSuite(testSuiteBuilder); 105 106 assertTrue(recorder.passed("Level1Test.testLevel1")); 107 assertTrue(recorder.passed("Level2Test.testLevel2")); 108 } 109 testExcludePackage()110 public void testExcludePackage() throws Exception { 111 testSuiteBuilder.includePackages(packageFor(SimpleTest.class), 112 packageFor(Level1Test.class)).excludePackages(packageFor(Level2Test.class)); 113 114 TestSuite testSuite = testSuiteBuilder.build(); 115 assertContentsInOrder(getTestCaseNames(testSuite), 116 "testLevel1", "testAnotherOne", "testSimpleOne", "testSimpleTwo"); 117 } 118 testShouldExcludeSuppressedTests()119 public void testShouldExcludeSuppressedTests() throws Exception { 120 testSuiteBuilder.includePackages(packageFor(SuppressedTest.class)); 121 testSuiteBuilder.build(); 122 123 SuiteExecutionRecorder recorder = runSuite(testSuiteBuilder); 124 125 assertEquals(1, recorder.testsSeen.size()); 126 assertTrue(recorder.passed("PartiallySuppressedTest.testUnSuppressedMethod")); 127 } 128 129 /** 130 * This test calls {@link OuterTest#buildTestsUnderHereRecursively()} to control 131 * the packages under test. The call to {@link TestSuiteBuilder#includeAllPackagesUnderHere()} 132 * is made from there so that only return the example tests. 133 */ testIncludeAllPackagesUnderHere()134 public void testIncludeAllPackagesUnderHere() throws Exception { 135 136 TestSuite testSuite = new OuterTest().buildTestsUnderHereRecursively(); 137 assertContentsInOrder(getTestCaseNames(testSuite), 138 "testOuter", 139 "testPublicConstructor", 140 "testErrorOne", 141 "testErrorTwo", 142 "testFailOne", 143 "testFailTwo", 144 "testInstrumentation", 145 "testLevel1", 146 "testLevel2", 147 "testAnotherOne", 148 "testSimpleOne", 149 "testSimpleTwo", 150 "testNonSmoke", 151 "testSmoke", 152 "testSubclass", 153 "testSuperclass", 154 "testUnSuppressedMethod"); 155 } 156 assertContentsInOrder(List<String> actual, String... source)157 private void assertContentsInOrder(List<String> actual, String... source) { 158 String[] clonedSource = source.clone(); 159 assertEquals("Unexpected number of items.", clonedSource.length, actual.size()); 160 for (int i = 0; i < actual.size(); i++) { 161 String actualItem = actual.get(i); 162 String sourceItem = clonedSource[i]; 163 assertEquals("Unexpected item. Index: " + i, sourceItem, actualItem); 164 } 165 } 166 packageFor(Class clazz)167 private static String packageFor(Class clazz) { 168 String className = clazz.getName(); 169 return className.substring(0, className.lastIndexOf('.')); 170 } 171 testsWhoseNameContains(final String string)172 private Predicate<TestMethod> testsWhoseNameContains(final String string) { 173 return new Predicate<TestMethod>() { 174 public boolean apply(TestMethod testMethod) { 175 return testMethod.getName().contains(string); 176 } 177 }; 178 } 179 180 private SuiteExecutionRecorder runSuite(TestSuiteBuilder builder) { 181 TestSuite suite = builder.build(); 182 SuiteExecutionRecorder recorder = new SuiteExecutionRecorder(); 183 TestResult result = new TestResult(); 184 result.addListener(recorder); 185 suite.run(result); 186 return recorder; 187 } 188 189 private class SuiteExecutionRecorder implements TestListener { 190 191 private Set<String> failures = new HashSet<String>(); 192 private Set<String> errors = new HashSet<String>(); 193 private Set<String> testsSeen = new HashSet<String>(); 194 195 public void addError(Test test, Throwable t) { 196 errors.add(testName(test)); 197 } 198 199 public void addFailure(Test test, AssertionFailedError t) { 200 failures.add(testName(test)); 201 } 202 203 public void endTest(Test test) { 204 } 205 206 public void startTest(Test test) { 207 testsSeen.add(testName(test)); 208 } 209 210 public boolean saw(String testName) { 211 return testsSeen.contains(testName); 212 } 213 214 public boolean failed(String testName) { 215 return failures.contains(testName); 216 } 217 218 public boolean errored(String testName) { 219 return errors.contains(testName); 220 } 221 222 public boolean passed(String testName) { 223 return saw(testName) && !failed(testName) && !errored(testName); 224 } 225 226 private String testName(Test test) { 227 TestCase testCase = (TestCase) test; 228 return testCase.getClass().getSimpleName() + "." + testCase.getName(); 229 } 230 } 231 } 232