Re-enable and move TWR test sources to java9 source set.

Fixes: b/228587114
Change-Id: I6c61b07b74c23169b8332bddfe4af6b900aab2dd
diff --git a/src/test/examplesJava9/twraddsuppressed/TestClass.java b/src/test/examplesJava9/twraddsuppressed/TestClass.java
new file mode 100644
index 0000000..61285ce
--- /dev/null
+++ b/src/test/examplesJava9/twraddsuppressed/TestClass.java
@@ -0,0 +1,46 @@
+// Copyright (c) 2022, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package twraddsuppressed;
+
+import java.io.Closeable;
+
+public class TestClass {
+
+  public static class MyClosable implements Closeable {
+
+    @Override
+    public void close() {
+      throw new RuntimeException("CLOSE");
+    }
+  }
+
+  public static void foo() {
+    throw new RuntimeException("FOO");
+  }
+
+  public static void bar() {
+    // Use twr twice to have javac generate a shared $closeResource helper.
+    try (MyClosable closable = new MyClosable()) {
+      foo();
+    }
+    try (MyClosable closable = new MyClosable()) {
+      foo();
+    }
+  }
+
+  public static void main(String[] args) {
+    try {
+      bar();
+    } catch (Exception e) {
+      Throwable[] suppressed = e.getSuppressed();
+      if (suppressed.length == 0) {
+        System.out.println("NONE");
+      } else {
+        for (Throwable throwable : suppressed) {
+          System.out.println(throwable.getMessage());
+        }
+      }
+    }
+  }
+}
diff --git a/src/test/examplesJava9/twrcloseresourceduplication/TwrCloseResourceDuplication.java b/src/test/examplesJava9/twrcloseresourceduplication/TwrCloseResourceDuplication.java
new file mode 100644
index 0000000..fa58d18
--- /dev/null
+++ b/src/test/examplesJava9/twrcloseresourceduplication/TwrCloseResourceDuplication.java
@@ -0,0 +1,56 @@
+// Copyright (c) 2022, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package twrcloseresourceduplication;
+
+import java.util.jar.JarFile;
+
+public class TwrCloseResourceDuplication {
+
+  public static class Foo {
+
+    void foo(String name) {
+      try (JarFile f = new JarFile(name)) {
+        System.out.println("foo opened 1");
+      } catch (Exception e) {
+        System.out.println("foo caught from 1: " + e.getClass().getSimpleName());
+      } finally {
+        System.out.println("foo post close 1");
+      }
+      try (JarFile f = new JarFile(name)) {
+        System.out.println("foo opened 2");
+        throw new RuntimeException();
+      } catch (Exception e) {
+        System.out.println("foo caught from 2: " + e.getClass().getSimpleName());
+      } finally {
+        System.out.println("foo post close 2");
+      }
+    }
+  }
+
+  public static class Bar {
+
+    void bar(String name) {
+      try (JarFile f = new JarFile(name)) {
+        System.out.println("bar opened 1");
+      } catch (Exception e) {
+        System.out.println("bar caught from 1: " + e.getClass().getSimpleName());
+      } finally {
+        System.out.println("bar post close 1");
+      }
+      try (JarFile f = new JarFile(name)) {
+        System.out.println("bar opened 2");
+        throw new RuntimeException();
+      } catch (Exception e) {
+        System.out.println("bar caught from 2: " + e.getClass().getSimpleName());
+      } finally {
+        System.out.println("bar post close 2");
+      }
+    }
+  }
+
+  public static void main(String[] args) {
+    new Foo().foo(args[0]);
+    new Bar().bar(args[0]);
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/desugar/suppressedexceptions/TwrSuppressedExceptionsTest.java b/src/test/java/com/android/tools/r8/desugar/suppressedexceptions/TwrSuppressedExceptionsTest.java
index 443c7ed..62d5240 100644
--- a/src/test/java/com/android/tools/r8/desugar/suppressedexceptions/TwrSuppressedExceptionsTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/suppressedexceptions/TwrSuppressedExceptionsTest.java
@@ -10,17 +10,19 @@
 import com.android.tools.r8.TestBase;
 import com.android.tools.r8.TestParameters;
 import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.TestRuntime.CfVm;
 import com.android.tools.r8.ToolHelper.DexVm.Version;
+import com.android.tools.r8.examples.JavaExampleClassProxy;
 import com.android.tools.r8.utils.AndroidApiLevel;
 import com.android.tools.r8.utils.IntBox;
 import com.android.tools.r8.utils.StringUtils;
 import com.android.tools.r8.utils.codeinspector.ClassSubject;
 import com.android.tools.r8.utils.codeinspector.InstructionSubject;
 import com.android.tools.r8.utils.codeinspector.MethodSubject;
-import java.io.Closeable;
+import com.google.common.collect.ImmutableList;
+import java.nio.file.Path;
 import java.util.List;
 import java.util.stream.Collectors;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
@@ -28,11 +30,19 @@
 @RunWith(Parameterized.class)
 public class TwrSuppressedExceptionsTest extends TestBase {
 
+  private static final String PKG = "twraddsuppressed";
+  private static final String EXAMPLE = "examplesJava9/" + PKG;
+  private final JavaExampleClassProxy MAIN = new JavaExampleClassProxy(EXAMPLE, PKG + ".TestClass");
+
   private final TestParameters parameters;
 
   @Parameterized.Parameters(name = "{0}")
   public static TestParametersCollection data() {
-    return getTestParameters().withAllRuntimes().withAllApiLevelsAlsoForCf().build();
+    return getTestParameters()
+        .withCfRuntimesStartingFromIncluding(CfVm.JDK9)
+        .withDexRuntimes()
+        .withAllApiLevelsAlsoForCf()
+        .build();
   }
 
   public TwrSuppressedExceptionsTest(TestParameters parameters) {
@@ -58,18 +68,21 @@
     return parameters.getApiLevel().isGreaterThanOrEqualTo(apiLevelWithTwrCloseResourceSupport());
   }
 
+  public List<Path> getProgramInputs() {
+    return ImmutableList.of(JavaExampleClassProxy.examplesJar(EXAMPLE));
+  }
+
   @Test
-  @Ignore("b/228587114")
   public void testD8() throws Exception {
     testForDesugaring(parameters)
-        .addProgramClasses(TestClass.class, MyClosable.class)
-        .run(parameters.getRuntime(), TestClass.class)
+        .addProgramFiles(getProgramInputs())
+        .run(parameters.getRuntime(), MAIN.typeName())
         .assertSuccessWithOutput(
             runtimeHasSuppressedExceptionsSupport() ? StringUtils.lines("CLOSE") : "NONE")
         .inspectIf(
             DesugarTestConfiguration::isDesugared,
             inspector -> {
-              ClassSubject clazz = inspector.clazz(TestClass.class);
+              ClassSubject clazz = inspector.clazz(MAIN.typeName());
               hasInvokesTo(
                   clazz.uniqueMethodWithName("bar"),
                   "$closeResource",
@@ -89,22 +102,21 @@
         .inspectIf(
             DesugarTestConfiguration::isNotDesugared,
             inspector -> {
-              ClassSubject clazz = inspector.clazz(TestClass.class);
+              ClassSubject clazz = inspector.clazz(MAIN.typeName());
               hasInvokesTo(clazz.uniqueMethodWithName("bar"), "$closeResource", 4);
               hasInvokesTo(clazz.mainMethod(), "getSuppressed", 1);
             });
   }
 
   @Test
-  @Ignore("b/228587114")
   public void testR8() throws Exception {
     assumeTrue(
         "R8 does not desugar CF so only run the high API variant.",
         parameters.isDexRuntime() || parameters.getApiLevel().isGreaterThan(AndroidApiLevel.B));
     testForR8(parameters.getBackend())
-        .addInnerClasses(TwrSuppressedExceptionsTest.class)
+        .addProgramFiles(getProgramInputs())
         .setMinApi(parameters.getApiLevel())
-        .addKeepMainRule(TestClass.class)
+        .addKeepMainRule(MAIN.typeName())
         // TODO(b/214250388): Don't warn about AutoClosable in synthesized code.
         .apply(
             b -> {
@@ -112,7 +124,7 @@
                 b.addDontWarn(AutoCloseable.class);
               }
             })
-        .run(parameters.getRuntime(), TestClass.class)
+        .run(parameters.getRuntime(), MAIN.typeName())
         .assertSuccessWithOutput(
             runtimeHasSuppressedExceptionsSupport() ? StringUtils.lines("CLOSE") : "NONE")
         .inspect(
@@ -127,7 +139,7 @@
                             adds.increment(getInvokesTo(m, "addSuppressed").size());
                           }));
               if (apiLevelHasSuppressedExceptionsSupport()) {
-                hasInvokesTo(inspector.clazz(TestClass.class).mainMethod(), "getSuppressed", 1);
+                hasInvokesTo(inspector.clazz(MAIN.typeName()).mainMethod(), "getSuppressed", 1);
                 assertEquals(1, gets.get());
                 assertEquals(1, adds.get());
               } else {
@@ -148,44 +160,4 @@
         .filter(i -> i.isInvoke() && i.getMethod().getName().toString().equals(callee))
         .collect(Collectors.toList());
   }
-
-  static class MyClosable implements Closeable {
-
-    @Override
-    public void close() {
-      throw new RuntimeException("CLOSE");
-    }
-  }
-
-  static class TestClass {
-
-    public static void foo() {
-      throw new RuntimeException("FOO");
-    }
-
-    public static void bar() {
-      // Use twr twice to have javac generate a shared $closeResource helper.
-      try (MyClosable closable = new MyClosable()) {
-        foo();
-      }
-      try (MyClosable closable = new MyClosable()) {
-        foo();
-      }
-    }
-
-    public static void main(String[] args) {
-      try {
-        bar();
-      } catch (Exception e) {
-        Throwable[] suppressed = e.getSuppressed();
-        if (suppressed.length == 0) {
-          System.out.println("NONE");
-        } else {
-          for (Throwable throwable : suppressed) {
-            System.out.println(throwable.getMessage());
-          }
-        }
-      }
-    }
-  }
 }
diff --git a/src/test/java/com/android/tools/r8/desugar/twr/TwrCloseResourceDuplicationTest.java b/src/test/java/com/android/tools/r8/desugar/twr/TwrCloseResourceDuplicationTest.java
index 2bee502..5ca1974 100644
--- a/src/test/java/com/android/tools/r8/desugar/twr/TwrCloseResourceDuplicationTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/twr/TwrCloseResourceDuplicationTest.java
@@ -9,14 +9,16 @@
 import com.android.tools.r8.TestBase;
 import com.android.tools.r8.TestParameters;
 import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.TestRuntime.CfVm;
+import com.android.tools.r8.examples.JavaExampleClassProxy;
 import com.android.tools.r8.utils.BooleanUtils;
 import com.android.tools.r8.utils.StringUtils;
 import com.android.tools.r8.utils.ZipUtils;
 import com.android.tools.r8.utils.codeinspector.FoundClassSubject;
+import com.google.common.collect.ImmutableList;
 import java.io.IOException;
+import java.nio.file.Path;
 import java.util.List;
-import java.util.jar.JarFile;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
@@ -24,6 +26,15 @@
 @RunWith(Parameterized.class)
 public class TwrCloseResourceDuplicationTest extends TestBase {
 
+  private static final String PKG = "twrcloseresourceduplication";
+  private static final String EXAMPLE = "examplesJava9/" + PKG;
+  private final JavaExampleClassProxy MAIN =
+      new JavaExampleClassProxy(EXAMPLE, PKG + ".TwrCloseResourceDuplication");
+  private final JavaExampleClassProxy FOO =
+      new JavaExampleClassProxy(EXAMPLE, PKG + ".TwrCloseResourceDuplication$Foo");
+  private final JavaExampleClassProxy BAR =
+      new JavaExampleClassProxy(EXAMPLE, PKG + ".TwrCloseResourceDuplication$Bar");
+
   static final int INPUT_CLASSES = 3;
 
   static final String EXPECTED =
@@ -43,7 +54,11 @@
 
   @Parameterized.Parameters(name = "{0}")
   public static TestParametersCollection data() {
-    return getTestParameters().withAllRuntimes().withAllApiLevelsAlsoForCf().build();
+    return getTestParameters()
+        .withCfRuntimesStartingFromIncluding(CfVm.JDK9)
+        .withDexRuntimes()
+        .withAllApiLevelsAlsoForCf()
+        .build();
   }
 
   public TwrCloseResourceDuplicationTest(TestParameters parameters) {
@@ -58,22 +73,25 @@
         .toString();
   }
 
+  private List<Path> getProgramInputs() throws Exception {
+    return ImmutableList.of(JavaExampleClassProxy.examplesJar(EXAMPLE));
+  }
+
   @Test
   public void testJvm() throws Exception {
     assumeTrue(parameters.isCfRuntime());
     testForJvm()
-        .addInnerClasses(getClass())
-        .run(parameters.getRuntime(), TestClass.class, getZipFile())
+        .addProgramFiles(getProgramInputs())
+        .run(parameters.getRuntime(), MAIN.typeName(), getZipFile())
         .assertSuccessWithOutput(EXPECTED);
   }
 
   @Test
-  @Ignore("b/228587114")
   public void testD8() throws Exception {
     testForD8(parameters.getBackend())
-        .addInnerClasses(getClass())
+        .addProgramFiles(getProgramInputs())
         .setMinApi(parameters.getApiLevel())
-        .run(parameters.getRuntime(), TestClass.class, getZipFile())
+        .run(parameters.getRuntime(), MAIN.typeName(), getZipFile())
         .assertSuccessWithOutput(EXPECTED)
         .inspect(
             inspector -> {
@@ -91,20 +109,19 @@
   }
 
   @Test
-  @Ignore("b/228587114")
   public void testR8() throws Exception {
     assumeTrue(parameters.isDexRuntime());
     testForR8(parameters.getBackend())
-        .addInnerClasses(getClass())
-        .addKeepMainRule(TestClass.class)
-        .addKeepClassAndMembersRules(Foo.class, Bar.class)
+        .addProgramFiles(getProgramInputs())
+        .addKeepMainRule(MAIN.typeName())
+        .addKeepClassAndMembersRules(FOO.typeName(), BAR.typeName())
         // TODO(b/214250388): Don't warn about synthetic code.
         .applyIf(
             parameters.getApiLevel().isLessThan(apiLevelWithTwrCloseResourceSupport()),
             builder -> builder.addDontWarn("java.lang.AutoCloseable"))
         .setMinApi(parameters.getApiLevel())
         .noMinification()
-        .run(parameters.getRuntime(), TestClass.class, getZipFile())
+        .run(parameters.getRuntime(), MAIN.typeName(), getZipFile())
         .assertSuccessWithOutput(EXPECTED)
         .inspect(
             inspector -> {
@@ -118,51 +135,4 @@
             });
   }
 
-  static class Foo {
-    void foo(String name) {
-      try (JarFile f = new JarFile(name)) {
-        System.out.println("foo opened 1");
-      } catch (Exception e) {
-        System.out.println("foo caught from 1: " + e.getClass().getSimpleName());
-      } finally {
-        System.out.println("foo post close 1");
-      }
-      try (JarFile f = new JarFile(name)) {
-        System.out.println("foo opened 2");
-        throw new RuntimeException();
-      } catch (Exception e) {
-        System.out.println("foo caught from 2: " + e.getClass().getSimpleName());
-      } finally {
-        System.out.println("foo post close 2");
-      }
-    }
-  }
-
-  static class Bar {
-    void bar(String name) {
-      try (JarFile f = new JarFile(name)) {
-        System.out.println("bar opened 1");
-      } catch (Exception e) {
-        System.out.println("bar caught from 1: " + e.getClass().getSimpleName());
-      } finally {
-        System.out.println("bar post close 1");
-      }
-      try (JarFile f = new JarFile(name)) {
-        System.out.println("bar opened 2");
-        throw new RuntimeException();
-      } catch (Exception e) {
-        System.out.println("bar caught from 2: " + e.getClass().getSimpleName());
-      } finally {
-        System.out.println("bar post close 2");
-      }
-    }
-  }
-
-  static class TestClass {
-
-    public static void main(String[] args) {
-      new Foo().foo(args[0]);
-      new Bar().bar(args[0]);
-    }
-  }
 }