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
17package android.test.suitebuilder;
18
19import com.android.internal.util.Predicate;
20import static android.test.suitebuilder.ListTestCaseNames.getTestCaseNames;
21import android.test.suitebuilder.examples.OuterTest;
22import android.test.suitebuilder.examples.suppress.SuppressedTest;
23import android.test.suitebuilder.examples.error.ErrorTest;
24import android.test.suitebuilder.examples.error.FailingTest;
25import android.test.suitebuilder.examples.nested.Level1Test;
26import android.test.suitebuilder.examples.nested.nested.Level2Test;
27import android.test.suitebuilder.examples.simple.SimpleTest;
28import android.test.suitebuilder.examples.subclass.SubclassTest;
29import junit.framework.AssertionFailedError;
30import junit.framework.Test;
31import junit.framework.TestCase;
32import junit.framework.TestListener;
33import junit.framework.TestResult;
34import junit.framework.TestSuite;
35
36import java.util.HashSet;
37import java.util.List;
38import java.util.Set;
39
40
41public class TestSuiteBuilderTest extends TestCase {
42
43    private TestSuiteBuilder testSuiteBuilder;
44
45    protected void setUp() throws Exception {
46        super.setUp();
47        testSuiteBuilder = new TestSuiteBuilder(getClass());
48    }
49
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
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
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
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
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
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
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
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     */
134    public void testIncludeAllPackagesUnderHere() throws Exception {
135
136        TestSuite testSuite = new OuterTest().buildTestsUnderHereRecursively();
137        assertContentsInOrder(getTestCaseNames(testSuite),
138                "testOuter", "testErrorOne", "testErrorTwo", "testFailOne", "testFailTwo",
139                "testInstrumentation", "testLevel1", "testLevel2", "testAnotherOne",
140                "testSimpleOne", "testSimpleTwo", "testNonSmoke", "testSmoke", "testSubclass",
141                "testSuperclass", "testUnSuppressedMethod");
142    }
143
144    private void assertContentsInOrder(List<String> actual, String... source) {
145        String[] clonedSource = source.clone();
146        assertEquals("Unexpected number of items.", clonedSource.length, actual.size());
147        for (int i = 0; i < actual.size(); i++) {
148            String actualItem = actual.get(i);
149            String sourceItem = clonedSource[i];
150            assertEquals("Unexpected item. Index: " + i, sourceItem, actualItem);
151        }
152    }
153
154    private static String packageFor(Class clazz) {
155        String className = clazz.getName();
156        return className.substring(0, className.lastIndexOf('.'));
157    }
158
159    private Predicate<TestMethod> testsWhoseNameContains(final String string) {
160        return new Predicate<TestMethod>() {
161            public boolean apply(TestMethod testMethod) {
162                return testMethod.getName().contains(string);
163            }
164        };
165    }
166
167    private SuiteExecutionRecorder runSuite(TestSuiteBuilder builder) {
168        TestSuite suite = builder.build();
169        SuiteExecutionRecorder recorder = new SuiteExecutionRecorder();
170        TestResult result = new TestResult();
171        result.addListener(recorder);
172        suite.run(result);
173        return recorder;
174    }
175
176    private class SuiteExecutionRecorder implements TestListener {
177
178        private Set<String> failures = new HashSet<String>();
179        private Set<String> errors = new HashSet<String>();
180        private Set<String> testsSeen = new HashSet<String>();
181
182        public void addError(Test test, Throwable t) {
183            errors.add(testName(test));
184        }
185
186        public void addFailure(Test test, AssertionFailedError t) {
187            failures.add(testName(test));
188        }
189
190        public void endTest(Test test) {
191        }
192
193        public void startTest(Test test) {
194            testsSeen.add(testName(test));
195        }
196
197        public boolean saw(String testName) {
198            return testsSeen.contains(testName);
199        }
200
201        public boolean failed(String testName) {
202            return failures.contains(testName);
203        }
204
205        public boolean errored(String testName) {
206            return errors.contains(testName);
207        }
208
209        public boolean passed(String testName) {
210            return saw(testName) && !failed(testName) && !errored(testName);
211        }
212
213        private String testName(Test test) {
214            TestCase testCase = (TestCase) test;
215            return testCase.getClass().getSimpleName() + "." + testCase.getName();
216        }
217    }
218}
219