Add JUnit 4 TestRule for running tests with a specific target SDK

Refactors code to replace lots of custom code for testing behavior for
specific target SDK versions, whether it is try...finally... blocks or
methods using lambdas with a JUnit 4 TestRule.

As the tests were JUnit 3 that also required switching them from
extending TestCase to extending TestCaseWithRules which adds support
for JUnit 4 TestRules to TestCase.

This is preparation work for some follow up work on ObjectStreamClass.

Test: atest \
    core-tests:org.apache.harmony.tests.java.io.ObjectStreamClassTest \
    core-tests:libcore.java.lang.OldRuntimeTest \
    core-tests:libcore.java.lang.PackageTest \
    core-tests:libcore.java.lang.reflect.annotations.AnnotationsTest \
    core-tests:libcore.java.util.CollectionsTest \
    core-tests:org.apache.harmony.regex.tests.java.util.regex.PatternTest \
    core-tests:libcore.junit.util.SwitchTargetSdkVersionRuleTest
Bug: 109930347

Change-Id: I4ea11288eb344eaeb2e6cc9f3225748e518e1827
9 files changed