1 /* 2 * Copyright (C) 2020 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 android.platform.test.scenario.sample; 18 19 import android.util.Log; 20 import android.platform.test.option.BooleanOption; 21 import android.platform.test.rule.TestWatcher; 22 import android.platform.test.scenario.annotation.Scenario; 23 24 import org.junit.After; 25 import org.junit.AfterClass; 26 import org.junit.Before; 27 import org.junit.BeforeClass; 28 import org.junit.ClassRule; 29 import org.junit.Rule; 30 import org.junit.Test; 31 import org.junit.rules.RuleChain; 32 import org.junit.runner.Description; 33 import org.junit.runner.RunWith; 34 import org.junit.runners.JUnit4; 35 36 /** 37 * A test showcasing the order of execution for different components of standard JUnit 4 test. 38 * 39 * <p>Run this test with the listener alongside, {@link PrintListener}, to see how they interact. 40 */ 41 @Scenario 42 @RunWith(JUnit4.class) 43 public class SampleTest { 44 45 public static final String LOG_TAG = SampleTest.class.getSimpleName(); 46 47 // Class-level rules 48 @ClassRule 49 public static RuleChain atClassRules = 50 RuleChain.outerRule(new PrintRule("@ClassRule #1")) 51 .around(new PrintRule("@ClassRule #2")) 52 .around(new PrintRule("@ClassRule #3")); 53 54 // Method-level rules 55 @Rule 56 public RuleChain atRules = 57 RuleChain.outerRule(new PrintRule("@Rule #1")) 58 .around(new PrintRule("@Rule #2")) 59 .around(new PrintRule("@Rule #3")); 60 61 @ClassRule 62 public static BooleanOption failBeforeClass = 63 new BooleanOption("fail-before-class").setRequired(false).setDefault(false); 64 65 @ClassRule 66 public static BooleanOption failBeforeRule = 67 new BooleanOption("fail-before-rule").setRequired(false).setDefault(false); 68 69 @Rule 70 public BooleanOption failBefore = 71 new BooleanOption("fail-before").setRequired(false).setDefault(false); 72 73 @Rule 74 public BooleanOption failTest = 75 new BooleanOption("fail-test").setRequired(false).setDefault(false); 76 77 @Rule 78 public BooleanOption failAfter = 79 new BooleanOption("fail-after").setRequired(false).setDefault(false); 80 81 @ClassRule 82 public static BooleanOption failAfterRule = 83 new BooleanOption("fail-after-rule").setRequired(false).setDefault(false); 84 85 @ClassRule 86 public static BooleanOption failAfterClass = 87 new BooleanOption("fail-after-class").setRequired(false).setDefault(false); 88 89 @BeforeClass beforeClassMethod()90 public static void beforeClassMethod() { 91 failIfRequested(failBeforeClass, "@BeforeClass"); 92 Log.d(LOG_TAG, "@BeforeClass"); 93 } 94 95 @Before beforeMethod()96 public void beforeMethod() { 97 failIfRequested(failBefore, "@Before"); 98 Log.d(LOG_TAG, "@Before"); 99 } 100 101 @Test testMethod()102 public void testMethod() { 103 failIfRequested(failTest, "@Test"); 104 Log.d(LOG_TAG, "@Test"); 105 } 106 107 @After afterMethod()108 public void afterMethod() { 109 failIfRequested(failAfter, "@After"); 110 Log.d(LOG_TAG, "@After"); 111 } 112 113 @AfterClass afterClassMethod()114 public static void afterClassMethod() { 115 failIfRequested(failAfterClass, "@AfterClass"); 116 Log.d(LOG_TAG, "@AfterClass"); 117 } 118 119 /** Log and throw a failure if the provided {@code option} is set. */ failIfRequested(BooleanOption option, String location)120 public static void failIfRequested(BooleanOption option, String location) { 121 if (option.get()) { 122 String message = String.format("Failed %s", location); 123 Log.d(LOG_TAG, message); 124 throw new RuntimeException(message); 125 } 126 } 127 128 /** A {@link TestWatcher} that prints the methods it executes. */ 129 private static class PrintRule extends TestWatcher { 130 131 private String mTag; 132 PrintRule(String tag)133 public PrintRule(String tag) { 134 mTag = tag; 135 } 136 137 @Override starting(Description description)138 protected void starting(Description description) { 139 failIfRequested(failBeforeRule, String.format(mTag, "%s starting")); 140 Log.d(LOG_TAG, String.format("%s#starting", mTag)); 141 } 142 143 @Override finished(Description description)144 protected void finished(Description description) { 145 failIfRequested(failAfterRule, String.format(mTag, "%s finished")); 146 Log.d(LOG_TAG, String.format("%s#finished.", mTag)); 147 } 148 } 149 } 150