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 android.test.suitebuilder.examples.instrumentation.InstrumentationTest;
20
21import junit.framework.Assert;
22import junit.framework.AssertionFailedError;
23import junit.framework.Test;
24import junit.framework.TestCase;
25import junit.framework.TestListener;
26import junit.framework.TestResult;
27import junit.framework.TestSuite;
28
29import java.util.HashSet;
30import java.util.Set;
31
32public class UnitTestSuiteBuilderTest extends TestCase {
33
34    private UnitTestSuiteBuilder unitTestSuiteBuilder;
35
36    protected void setUp() throws Exception {
37        super.setUp();
38        unitTestSuiteBuilder = new UnitTestSuiteBuilder(getClass());
39    }
40
41    public void testShouldExcludeIntrumentationTests() throws Exception {
42        unitTestSuiteBuilder.includePackages(packageFor(InstrumentationTest.class));
43
44        TestSuite testSuite = unitTestSuiteBuilder.build();
45        Assert.assertEquals(0, ListTestCaseNames.getTestCaseNames(testSuite).size());
46
47        SuiteExecutionRecorder recorder = runSuite(unitTestSuiteBuilder);
48
49        assertFalse(recorder.saw("InstrumentationTest.testInstrumentation"));
50        assertTrue(recorder.testsSeen.isEmpty());
51    }
52
53    private static String packageFor(Class clazz) {
54        String className = clazz.getName();
55        return className.substring(0, className.lastIndexOf('.'));
56    }
57
58    private SuiteExecutionRecorder runSuite(TestSuiteBuilder builder) {
59        TestSuite suite = builder.build();
60        SuiteExecutionRecorder recorder = new SuiteExecutionRecorder();
61        TestResult result = new TestResult();
62        result.addListener(recorder);
63        suite.run(result);
64        return recorder;
65    }
66
67    private class SuiteExecutionRecorder implements TestListener {
68
69        private Set<String> failures = new HashSet<String>();
70        private Set<String> errors = new HashSet<String>();
71        private Set<String> testsSeen = new HashSet<String>();
72
73        public void addError(Test test, Throwable t) {
74            errors.add(testName(test));
75        }
76
77        public void addFailure(Test test, AssertionFailedError t) {
78            failures.add(testName(test));
79        }
80
81        public void endTest(Test test) {
82        }
83
84        public void startTest(Test test) {
85            testsSeen.add(testName(test));
86        }
87
88        public boolean saw(String testName) {
89            return testsSeen.contains(testName);
90        }
91
92        public boolean failed(String testName) {
93            return failures.contains(testName);
94        }
95
96        public boolean errored(String testName) {
97            return errors.contains(testName);
98        }
99
100        public boolean passed(String testName) {
101            return saw(testName) && !failed(testName) && !errored(testName);
102        }
103
104        private String testName(Test test) {
105            TestCase testCase = (TestCase) test;
106            return testCase.getClass().getSimpleName() + "." + testCase.getName();
107        }
108    }
109}
110