1 /*
2  * Copyright (C) 2016 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 vogar.target.junit.junit3;
18 
19 import junit.framework.TestCase;
20 import org.junit.runner.Runner;
21 import org.junit.runners.model.RunnerBuilder;
22 import vogar.target.junit.DescribableComparator;
23 import vogar.target.junit.DescribableStatement;
24 import vogar.target.junit.RunnerParams;
25 
26 /**
27  * Creates a {@link Runner} for {@link TestCase} derived classes.
28  */
29 public class AlternateTestCaseBuilder extends RunnerBuilder {
30 
31     private final TestCaseRunnerFactory testCaseRunnerFactory;
32 
AlternateTestCaseBuilder(RunnerParams runnerParams)33     public AlternateTestCaseBuilder(RunnerParams runnerParams) {
34         this(new TestCaseRunnerFactory(runnerParams));
35     }
36 
AlternateTestCaseBuilder(TestCaseRunnerFactory testCaseRunnerFactory)37     public AlternateTestCaseBuilder(TestCaseRunnerFactory testCaseRunnerFactory) {
38         this.testCaseRunnerFactory = testCaseRunnerFactory;
39     }
40 
41 
42     @Override
runnerForClass(Class<?> testClass)43     public Runner runnerForClass(Class<?> testClass) throws Throwable {
44         if (junit.framework.TestCase.class.isAssignableFrom(testClass)) {
45             // Transform the test case into a Runner.
46             Class<? extends TestCase> testCaseClass = testClass.asSubclass(TestCase.class);
47             TestCaseTransformer<Runner, DescribableStatement> testCaseTransformer =
48                     new TestCaseTransformer<>(testCaseRunnerFactory,
49                             DescribableComparator.getInstance());
50             return testCaseTransformer.createSuite(testCaseClass);
51         }
52         return null;
53     }
54 }
55