• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
18 
19 import java.util.List;
20 import org.junit.rules.TestRule;
21 import org.junit.runner.Description;
22 import org.junit.runner.notification.RunNotifier;
23 import org.junit.runners.ParentRunner;
24 import org.junit.runners.model.InitializationError;
25 import org.junit.runners.model.Statement;
26 
27 /**
28  * A {@link ParentRunner} whose children are {@link DescribableStatement} instances.
29  *
30  * <p>This is a generally useful form of {@link ParentRunner} that can easily be customized.
31  *
32  * <p>This is used for JUnit3 based classes which may have multiple constructors (a default and
33  * one that takes the test name) so it passes a {@link Dummy} class to the parent and overrides the
34  * {@link #getName()} method. That works because this does not use any information from the supplied
35  * class.
36  */
37 public class ParentStatementRunner extends ParentRunner<DescribableStatement> {
38 
39     private final String name;
40     private final List<DescribableStatement> statements;
41     private final TestRule testRule;
42 
ParentStatementRunner(Class<?> testClass, List<DescribableStatement> statements, RunnerParams runnerParams)43     public ParentStatementRunner(Class<?> testClass, List<DescribableStatement> statements,
44                                  RunnerParams runnerParams)
45             throws InitializationError {
46         super(Dummy.class);
47         name = testClass.getName();
48         this.statements = statements;
49         testRule = runnerParams.getTestRule();
50     }
51 
52     @Override
getName()53     protected String getName() {
54         return name;
55     }
56 
57     @Override
getChildren()58     protected List<DescribableStatement> getChildren() {
59         return statements;
60     }
61 
62     @Override
describeChild(DescribableStatement child)63     protected Description describeChild(DescribableStatement child) {
64         return child.getDescription();
65     }
66 
67     @Override
runChild(final DescribableStatement child, RunNotifier notifier)68     protected void runChild(final DescribableStatement child, RunNotifier notifier) {
69         Description description = describeChild(child);
70         Statement statement = child;
71         statement = testRule.apply(statement, description);
72         ParentRunnerHelper.abortingRunLeaf(statement, description, notifier);
73     }
74 
75     /**
76      * ParentRunner requires that the class be public.
77      */
78     public static class Dummy {
79     }
80 }
81