diff options
author | 2024-11-22 01:07:39 +0000 | |
---|---|---|
committer | 2024-11-22 02:00:40 +0000 | |
commit | a2379ea64e13a9aa4d1d9abe0a67de287db01473 (patch) | |
tree | 95849458937b6bc311b4ecd5c66260293765dd7c /ravenwood/junit-impl-src/android | |
parent | b870b936ca774ef624939cc06c4113421d065032 (diff) |
[Ravenwood] Fully remove RavenwoodConfig support
At the same time, relax some RavenwoodRule checks since RavenwoodRule is
now just a normal JUnit rule.
Flag: EXEMPT host test change only
Bug: 377765941
Test: $ANDROID_BUILD_TOP/frameworks/base/ravenwood/scripts/run-ravenwood-tests.sh
Change-Id: Ia3f8afd61f8636f458e0eafae4b75139f5bc14b9
Diffstat (limited to 'ravenwood/junit-impl-src/android')
-rw-r--r-- | ravenwood/junit-impl-src/android/platform/test/ravenwood/RavenwoodRunnerState.java | 162 |
1 files changed, 1 insertions, 161 deletions
diff --git a/ravenwood/junit-impl-src/android/platform/test/ravenwood/RavenwoodRunnerState.java b/ravenwood/junit-impl-src/android/platform/test/ravenwood/RavenwoodRunnerState.java index 4ab1fa1579e1..110de989b524 100644 --- a/ravenwood/junit-impl-src/android/platform/test/ravenwood/RavenwoodRunnerState.java +++ b/ravenwood/junit-impl-src/android/platform/test/ravenwood/RavenwoodRunnerState.java @@ -15,22 +15,10 @@ */ package android.platform.test.ravenwood; -import static com.android.ravenwood.common.RavenwoodCommonUtils.ensureIsPublicMember; - -import static org.junit.Assert.fail; - -import android.annotation.Nullable; import android.util.Log; -import com.android.ravenwood.common.RavenwoodRuntimeException; - -import org.junit.ClassRule; -import org.junit.Rule; -import org.junit.rules.TestRule; import org.junit.runner.Description; -import java.lang.reflect.Field; - /** * Used to store various states associated with the current test runner that's inly needed * in junit-impl. @@ -52,30 +40,10 @@ public final class RavenwoodRunnerState { mRunner = runner; } - /** - * The RavenwoodConfig declared in the test class - */ - private RavenwoodConfig mConfig; - /** - * The RavenwoodRule currently in effect, declared in the test class - */ - private RavenwoodRule mRule; - private boolean mHasRavenwoodRule; private Description mMethodDescription; public void enterTestRunner() { Log.i(TAG, "enterTestRunner: " + mRunner); - - mHasRavenwoodRule = hasRavenwoodRule(mRunner.mTestJavaClass); - mConfig = extractConfiguration(mRunner.mTestJavaClass); - - if (mConfig != null) { - if (mHasRavenwoodRule) { - fail("RavenwoodConfig and RavenwoodRule cannot be used in the same class." - + " Suggest migrating to RavenwoodConfig."); - } - } - RavenwoodRuntimeEnvironmentController.initForRunner(); } @@ -85,12 +53,7 @@ public final class RavenwoodRunnerState { public void exitTestClass() { Log.i(TAG, "exitTestClass: " + mRunner.mTestJavaClass.getName()); - try { - RavenwoodRuntimeEnvironmentController.exitTestClass(); - } finally { - mConfig = null; - mRule = null; - } + RavenwoodRuntimeEnvironmentController.exitTestClass(); } public void enterTestMethod(Description description) { @@ -103,132 +66,9 @@ public final class RavenwoodRunnerState { } public void enterRavenwoodRule(RavenwoodRule rule) { - if (!mHasRavenwoodRule) { - fail("If you have a RavenwoodRule in your test, make sure the field type is" - + " RavenwoodRule so Ravenwood can detect it."); - } - if (mRule != null) { - fail("Multiple nesting RavenwoodRule's are detected in the same class," - + " which is not supported."); - } - mRule = rule; RavenwoodRuntimeEnvironmentController.setSystemProperties(rule.mSystemProperties); } public void exitRavenwoodRule(RavenwoodRule rule) { - if (mRule != rule) { - fail("RavenwoodRule did not take effect."); - } - mRule = null; - } - - /** - * @return a configuration from a test class, if any. - */ - @Nullable - private static RavenwoodConfig extractConfiguration(Class<?> testClass) { - var field = findConfigurationField(testClass); - if (field == null) { - return null; - } - - try { - return (RavenwoodConfig) field.get(null); - } catch (IllegalAccessException e) { - throw new RavenwoodRuntimeException("Failed to fetch from the configuration field", e); - } - } - - /** - * @return true if the current target class (or its super classes) has any @Rule / @ClassRule - * fields of type RavenwoodRule. - * - * Note, this check won't detect cases where a Rule is of type - * {@link TestRule} and still be a {@link RavenwoodRule}. But that'll be detected at runtime - * as a failure, in {@link #enterRavenwoodRule}. - */ - private static boolean hasRavenwoodRule(Class<?> testClass) { - for (var field : testClass.getDeclaredFields()) { - if (!field.isAnnotationPresent(Rule.class) - && !field.isAnnotationPresent(ClassRule.class)) { - continue; - } - if (field.getType().equals(RavenwoodRule.class)) { - return true; - } - } - // JUnit supports rules as methods, so we need to check them too. - for (var method : testClass.getDeclaredMethods()) { - if (!method.isAnnotationPresent(Rule.class) - && !method.isAnnotationPresent(ClassRule.class)) { - continue; - } - if (method.getReturnType().equals(RavenwoodRule.class)) { - return true; - } - } - // Look into the super class. - if (!testClass.getSuperclass().equals(Object.class)) { - return hasRavenwoodRule(testClass.getSuperclass()); - } - return false; - } - - /** - * Find and return a field with @RavenwoodConfig.Config, which must be of type - * RavenwoodConfig. - */ - @Nullable - private static Field findConfigurationField(Class<?> testClass) { - Field foundField = null; - - for (var field : testClass.getDeclaredFields()) { - final var hasAnot = field.isAnnotationPresent(RavenwoodConfig.Config.class); - final var isType = field.getType().equals(RavenwoodConfig.class); - - if (hasAnot) { - if (isType) { - // Good, use this field. - if (foundField != null) { - fail(String.format( - "Class %s has multiple fields with %s", - testClass.getCanonicalName(), - "@RavenwoodConfig.Config")); - } - // Make sure it's static public - ensureIsPublicMember(field, true); - - foundField = field; - } else { - fail(String.format( - "Field %s.%s has %s but type is not %s", - testClass.getCanonicalName(), - field.getName(), - "@RavenwoodConfig.Config", - "RavenwoodConfig")); - return null; // unreachable - } - } else { - if (isType) { - fail(String.format( - "Field %s.%s does not have %s but type is %s", - testClass.getCanonicalName(), - field.getName(), - "@RavenwoodConfig.Config", - "RavenwoodConfig")); - return null; // unreachable - } else { - // Unrelated field, ignore. - continue; - } - } - } - if (foundField != null) { - return foundField; - } - if (!testClass.getSuperclass().equals(Object.class)) { - return findConfigurationField(testClass.getSuperclass()); - } - return null; } } |