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