1 /* 2 * Copyright (C) 2024 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 package com.android.adservices.shared.meta_testing; 17 18 import static com.android.adservices.shared.testing.TestNamer.DEFAULT_TEST_NAME; 19 20 import static org.junit.Assert.assertThrows; 21 22 import com.android.adservices.shared.testing.TestNamer; 23 24 import com.google.common.truth.Expect; 25 26 import org.junit.rules.TestRule; 27 import org.junit.runner.Description; 28 import org.junit.runners.model.Statement; 29 30 import java.util.Objects; 31 import java.util.concurrent.atomic.AtomicReference; 32 33 /** 34 * Helper used to test {@link TestRule JUnit rules} that implements {@link TestNamer}. 35 * 36 * @param <R> rule being tested 37 */ 38 public final class TestNamerRuleTester<R extends TestRule & TestNamer> { 39 40 private final Expect mExpect; 41 private final R mRule; 42 private final Description mDescription = 43 Description.createTestDescription(TestNamerRuleTester.class, "aTestHasNoName"); 44 45 /** Default constructor. */ TestNamerRuleTester(Expect expect, R rule)46 public TestNamerRuleTester(Expect expect, R rule) { 47 mExpect = Objects.requireNonNull(expect); 48 mRule = Objects.requireNonNull(rule); 49 } 50 51 /** Tests the expected behavior when running or not running a test. */ justDoIt()52 public void justDoIt() throws Throwable { 53 mExpect.withMessage("getTestName() BEFORE running a test") 54 .that(mRule.getTestName()) 55 .isEqualTo(DEFAULT_TEST_NAME); 56 57 assertInsideTest(); 58 59 mExpect.withMessage("getTestName() AFTER running a test") 60 .that(mRule.getTestName()) 61 .isEqualTo(DEFAULT_TEST_NAME); 62 63 assertAfterTestThatFails(); 64 } 65 assertInsideTest()66 private void assertInsideTest() throws Throwable { 67 // Cannot use String because it must be final 68 AtomicReference<String> nameRef = new AtomicReference<>(); 69 Statement statement = 70 new Statement() { 71 72 @Override 73 public void evaluate() throws Throwable { 74 nameRef.set(mRule.getTestName()); 75 } 76 }; 77 78 mRule.apply(statement, mDescription).evaluate(); 79 80 String testName = nameRef.get(); 81 mExpect.withMessage("getTestName()").that(testName).contains("TestNamerRuleTester"); 82 mExpect.withMessage("getTestName()").that(testName).contains("aTestHasNoName()"); 83 } 84 assertAfterTestThatFails()85 private void assertAfterTestThatFails() throws Throwable { 86 Throwable expected = new Throwable("D'OH!"); 87 88 Statement statement = 89 new Statement() { 90 91 @Override 92 public void evaluate() throws Throwable { 93 throw expected; 94 } 95 }; 96 97 Throwable actual = 98 assertThrows( 99 Throwable.class, () -> mRule.apply(statement, mDescription).evaluate()); 100 mExpect.withMessage("exception thrown by test").that(actual).isSameInstanceAs(expected); 101 102 mExpect.withMessage("getTestName() AFTER running a test that failed") 103 .that(mRule.getTestName()) 104 .isEqualTo(DEFAULT_TEST_NAME); 105 } 106 } 107