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 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 String testClassOrPackage = treatEmptyAsNull(testRunnerProperties.testClassOrPackage()); 52 if (testClassOrPackage == null) { 53 Class<?> testClass = testRunnerProperties.testClass(); 54 if (testClass != TestRunnerProperties.Default.class) { 55 testClassOrPackage = testClass.getName(); 56 } 57 } 58 setProperty(TestProperties.TEST_CLASS_OR_PACKAGE, testClassOrPackage); 59 setProperty(TestProperties.RUNNER_TYPE, testRunnerProperties.runnerType().toString()); 60 setProperty(TestProperties.TIMEOUT, testRunnerProperties.timeout()); 61 } 62 return base; 63 } 64 65 public Class<?> testClass() { 66 return testRunnerProperties.testClass(); 67 } 68 69 private void setProperty(String key, String value) { 70 value = treatEmptyAsNull(value); 71 if (value != null) { 72 properties.setProperty(key, value); 73 } 74 } 75 76 private void setProperty(String key, boolean value) { 77 properties.setProperty(key, String.valueOf(value)); 78 } 79 80 private void setProperty(String key, int value) { 81 properties.setProperty(key, String.valueOf(value)); 82 } 83 84 private String treatEmptyAsNull(String s) { 85 return s.equals("") ? null : s; 86 } 87 88 /** 89 * Create the {@link TestRunner} using properties provided by {@link TestRunnerProperties} if 90 * available. 91 * 92 * @param args the command line arguments for the {@link TargetRunner} instance. 93 */ 94 public TestRunner createTestRunner(String... args) { 95 if (properties == null) { 96 throw new IllegalStateException("Cannot create TestRunner as test does not have an " 97 + "associated @" + TestRunnerProperties.class.getName() + " annotation"); 98 } 99 100 return new TestRunner(properties, new ArrayList<>(Arrays.asList(args))); 101 } 102} 103