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.rules.TestRule; 22import org.junit.runner.Description; 23import org.junit.runners.model.Statement; 24import vogar.TestProperties; 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 TestRule { 35 36 private Properties properties; 37 private TestRunnerProperties testRunnerProperties; 38 39 @Override 40 public Statement apply(Statement base, Description description) { 41 testRunnerProperties = description.getAnnotation(TestRunnerProperties.class); 42 if (testRunnerProperties != null) { 43 properties = new Properties(); 44 setProperty(TestProperties.MONITOR_PORT, testRunnerProperties.monitorPort()); 45 String testClassOrPackage = treatEmptyAsNull(testRunnerProperties.testClassOrPackage()); 46 if (testClassOrPackage == null) { 47 Class<?> testClass = testRunnerProperties.testClass(); 48 if (testClass != TestRunnerProperties.Default.class) { 49 testClassOrPackage = testClass.getName(); 50 } 51 } 52 setProperty(TestProperties.TEST_CLASS_OR_PACKAGE, testClassOrPackage); 53 setProperty(TestProperties.RUNNER_TYPE, testRunnerProperties.runnerType().toString()); 54 setProperty(TestProperties.TIMEOUT, testRunnerProperties.timeout()); 55 } 56 return base; 57 } 58 59 public Class<?> testClass() { 60 return testRunnerProperties.testClass(); 61 } 62 63 private void setProperty(String key, String value) { 64 value = treatEmptyAsNull(value); 65 if (value != null) { 66 properties.setProperty(key, value); 67 } 68 } 69 70 private void setProperty(String key, boolean value) { 71 properties.setProperty(key, String.valueOf(value)); 72 } 73 74 private void setProperty(String key, int value) { 75 properties.setProperty(key, String.valueOf(value)); 76 } 77 78 private String treatEmptyAsNull(String s) { 79 return s.equals("") ? null : s; 80 } 81 82 /** 83 * Create the {@link TestRunner} using properties provided by {@link TestRunnerProperties} if 84 * available. 85 * 86 * @param args the command line arguments for the {@link TargetRunner} instance. 87 */ 88 public TestRunner createTestRunner(String... args) { 89 if (properties == null) { 90 throw new IllegalStateException("Cannot create TestRunner as test does not have an " 91 + "associated @" + TestRunnerProperties.class.getName() + " annotation"); 92 } 93 94 return new TestRunner(properties, new ArrayList<>(Arrays.asList(args))); 95 } 96} 97