1/*
2 * Copyright (C) 2015 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 */
16package vogar.target;
17
18import java.util.ArrayList;
19import java.util.Arrays;
20import java.util.Properties;
21import org.junit.runners.model.FrameworkMethod;
22import org.junit.runners.model.Statement;
23import vogar.TestProperties;
24import vogar.testing.UndeprecatedMethodRule;
25
26/**
27 * Creates {@link TestRunner} for tests.
28 *
29 * <p>Obtains test specific arguments from the {@link TestRunnerProperties} annotation on the test
30 * and makes them available to the test itself.
31 *
32 * @see TestRunnerProperties
33 */
34public class TestRunnerRule implements UndeprecatedMethodRule {
35
36    private Properties properties;
37
38    @Override
39    public Statement apply(Statement base, FrameworkMethod method, Object target) {
40        TestRunnerProperties testRunnerProperties =
41                method.getAnnotation(TestRunnerProperties.class);
42        if (testRunnerProperties != null) {
43            properties = new Properties();
44            setProperty(TestProperties.MONITOR_PORT, testRunnerProperties.monitorPort());
45            setProperty(TestProperties.PROFILE, testRunnerProperties.profile());
46            setProperty(TestProperties.PROFILE_DEPTH, testRunnerProperties.profileDepth());
47            setProperty(TestProperties.PROFILE_FILE, testRunnerProperties.profileFile());
48            setProperty(TestProperties.PROFILE_INTERVAL, testRunnerProperties.profileInterval());
49            setProperty(TestProperties.PROFILE_THREAD_GROUP,
50                    testRunnerProperties.profileThreadGroup());
51            setProperty(TestProperties.QUALIFIED_NAME, testRunnerProperties.qualifiedName());
52            String testClassOrPackage = treatEmptyAsNull(testRunnerProperties.testClassOrPackage());
53            if (testClassOrPackage == null) {
54                Class<?> testClass = testRunnerProperties.testClass();
55                if (testClass != TestRunnerProperties.Default.class) {
56                    testClassOrPackage = testClass.getName();
57                }
58            }
59            setProperty(TestProperties.TEST_CLASS_OR_PACKAGE, testClassOrPackage);
60            setProperty(TestProperties.TEST_ONLY, testRunnerProperties.testOnly());
61            setProperty(TestProperties.TIMEOUT, testRunnerProperties.timeout());
62        }
63        return base;
64    }
65
66    private void setProperty(String key, String value) {
67        value = treatEmptyAsNull(value);
68        if (value != null) {
69            properties.setProperty(key, value);
70        }
71    }
72
73    private void setProperty(String key, boolean value) {
74        properties.setProperty(key, String.valueOf(value));
75    }
76
77    private void setProperty(String key, int value) {
78        properties.setProperty(key, String.valueOf(value));
79    }
80
81    private String treatEmptyAsNull(String s) {
82        return s.equals("") ? null : s;
83    }
84
85    /**
86     * Create the {@link TestRunner} using properties provided by {@link TestRunnerProperties} if
87     * available.
88     *
89     * @param args the command line arguments for the {@link Runner} instance.
90     */
91    public TestRunner createTestRunner(String... args) {
92        if (properties == null) {
93            throw new IllegalStateException("Cannot create TestRunner as test does not have an "
94                    + "associated @" + TestRunnerProperties.class.getName() + " annotation");
95        }
96
97        return new TestRunner(properties, new ArrayList<>(Arrays.asList(args)));
98    }
99}
100