Migrate hello world example test.

Bug: b/167145686
Change-Id: I34b4d81661d996e902fbb27f7f376f6033e69733
diff --git a/src/test/examples/hello/keep-rules.txt b/src/test/examples/hello/keep-rules.txt
deleted file mode 100644
index 5a49e41..0000000
--- a/src/test/examples/hello/keep-rules.txt
+++ /dev/null
@@ -1 +0,0 @@
--keep class hello.Hello { public static void main(...);}
diff --git a/src/test/java/com/android/tools/r8/R8RunExamplesTest.java b/src/test/java/com/android/tools/r8/R8RunExamplesTest.java
index 2a5bb6c..d63d66c 100644
--- a/src/test/java/com/android/tools/r8/R8RunExamplesTest.java
+++ b/src/test/java/com/android/tools/r8/R8RunExamplesTest.java
@@ -26,7 +26,6 @@
   public static Collection<String[]> data() {
     String[] tests = {
         "arithmetic.Arithmetic",
-        "hello.Hello",
         "ifstatements.IfStatements",
         "inlining.Inlining",
         "instancevariable.InstanceVariable",
diff --git a/src/test/java/com/android/tools/r8/cf/bootstrap/BootstrapCurrentEqualityTest.java b/src/test/java/com/android/tools/r8/cf/bootstrap/BootstrapCurrentEqualityTest.java
index fe5441b..9227df7 100644
--- a/src/test/java/com/android/tools/r8/cf/bootstrap/BootstrapCurrentEqualityTest.java
+++ b/src/test/java/com/android/tools/r8/cf/bootstrap/BootstrapCurrentEqualityTest.java
@@ -4,7 +4,6 @@
 package com.android.tools.r8.cf.bootstrap;
 
 import static com.android.tools.r8.graph.GenericSignatureIdentityTest.testParseSignaturesInJar;
-import static com.android.tools.r8.utils.FileUtils.JAR_EXTENSION;
 import static org.hamcrest.CoreMatchers.containsString;
 import static org.hamcrest.CoreMatchers.not;
 import static org.hamcrest.MatcherAssert.assertThat;
@@ -22,6 +21,7 @@
 import com.android.tools.r8.TestRuntime.CfVm;
 import com.android.tools.r8.ToolHelper;
 import com.android.tools.r8.ToolHelper.ProcessResult;
+import com.android.tools.r8.examples.hello.HelloTestRunner;
 import com.android.tools.r8.retrace.ProguardMapProducer;
 import com.android.tools.r8.retrace.ProguardMappingSupplier;
 import com.android.tools.r8.retrace.Retrace;
@@ -51,14 +51,14 @@
 
   private static final Path MAIN_KEEP = Paths.get("src/main/keep.txt");
 
-  private static final String HELLO_NAME = "hello.Hello";
+  private static final String HELLO_NAME = HelloTestRunner.getHelloClass().getTypeName();
   private static final String[] KEEP_HELLO = {
     "-keep class " + HELLO_NAME + " {",
     "  public static void main(...);",
     "}",
     "-allowaccessmodification"
   };
-  private static String HELLO_EXPECTED = StringUtils.lines("Hello, world");
+  private static String HELLO_EXPECTED = HelloTestRunner.getExpectedOutput();
 
   private static Pair<Path, Path> r8R8Debug;
   private static Pair<Path, Path> r8R8Release;
@@ -199,7 +199,7 @@
 
   @Test
   public void test() throws Exception {
-    Path program = Paths.get(ToolHelper.EXAMPLES_BUILD_DIR, "hello" + JAR_EXTENSION);
+    Path program = HelloTestRunner.writeHelloProgramJar(temp);
     testForJvm(parameters)
         .addProgramFiles(program)
         .run(parameters.getRuntime(), HELLO_NAME)
diff --git a/src/test/java/com/android/tools/r8/cf/bootstrap/BootstrapTest.java b/src/test/java/com/android/tools/r8/cf/bootstrap/BootstrapTest.java
index 0210341..87bbfaf 100644
--- a/src/test/java/com/android/tools/r8/cf/bootstrap/BootstrapTest.java
+++ b/src/test/java/com/android/tools/r8/cf/bootstrap/BootstrapTest.java
@@ -12,8 +12,8 @@
 import com.android.tools.r8.TestParametersCollection;
 import com.android.tools.r8.ToolHelper;
 import com.android.tools.r8.ToolHelper.ProcessResult;
+import com.android.tools.r8.examples.hello.HelloTestRunner;
 import com.android.tools.r8.utils.FileUtils;
-import com.android.tools.r8.utils.StringUtils;
 import com.google.common.base.Charsets;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -30,7 +30,8 @@
   private static final Path R8_STABLE_JAR =
       Paths.get("third_party", "r8-releases", "3.2.54", "r8.jar");
 
-  private static final String HELLO_EXPECTED = StringUtils.lines("Hello World!");
+  private static final Class<?> HELLO_CLASS = HelloTestRunner.getHelloClass();
+  private static final String HELLO_EXPECTED = HelloTestRunner.getExpectedOutput();
 
   private static class R8Result {
 
@@ -59,18 +60,18 @@
   }
 
   private Path getHelloInputs() {
-    return ToolHelper.getClassFileForTestClass(Hello.class);
+    return ToolHelper.getClassFileForTestClass(HELLO_CLASS);
   }
 
   private String getHelloKeepRules() {
-    return TestBase.keepMainProguardConfiguration(Hello.class);
+    return TestBase.keepMainProguardConfiguration(HELLO_CLASS);
   }
 
   @Test
   public void reference() throws Exception {
     testForJvm(parameters)
         .addProgramFiles(getHelloInputs())
-        .run(parameters.getRuntime(), Hello.class)
+        .run(parameters.getRuntime(), HELLO_CLASS)
         .assertSuccessWithOutput(HELLO_EXPECTED);
   }
 
@@ -90,7 +91,7 @@
         runExternalR8(R8_STABLE_JAR, getHelloInputs(), getHelloKeepRules(), mode);
     testForJvm(parameters)
         .addProgramFiles(helloCompiledWithR8.outputJar)
-        .run(parameters.getRuntime(), Hello.class)
+        .run(parameters.getRuntime(), HELLO_CLASS)
         .assertSuccessWithOutput(HELLO_EXPECTED);
 
     compareR8(helloCompiledWithR8, mode);
@@ -151,11 +152,4 @@
     String pgMap = FileUtils.readTextFile(pgMapFile, Charsets.UTF_8);
     return new R8Result(processResult, outputJar, pgMap);
   }
-
-  public static class Hello {
-
-    public static void main(String[] args) {
-      System.out.println("Hello World!");
-    }
-  }
 }
diff --git a/src/test/java/com/android/tools/r8/debug/ExamplesDebugTest.java b/src/test/java/com/android/tools/r8/debug/ExamplesDebugTest.java
index 7696491..577eb22 100644
--- a/src/test/java/com/android/tools/r8/debug/ExamplesDebugTest.java
+++ b/src/test/java/com/android/tools/r8/debug/ExamplesDebugTest.java
@@ -60,11 +60,6 @@
   }
 
   @Test
-  public void testHello() throws Exception {
-    testDebugging("hello", "Hello");
-  }
-
-  @Test
   public void testIfStatements() throws Exception {
     testDebugging("ifstatements", "IfStatements");
   }
diff --git a/src/test/java/com/android/tools/r8/desugar/r8bootstrap/Java17R8BootstrapTest.java b/src/test/java/com/android/tools/r8/desugar/r8bootstrap/Java17R8BootstrapTest.java
index 2ed69e1..8d2a21e 100644
--- a/src/test/java/com/android/tools/r8/desugar/r8bootstrap/Java17R8BootstrapTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/r8bootstrap/Java17R8BootstrapTest.java
@@ -5,7 +5,6 @@
 package com.android.tools.r8.desugar.r8bootstrap;
 
 import static com.android.tools.r8.desugar.r8bootstrap.JavaBootstrapUtils.MAIN_KEEP;
-import static com.android.tools.r8.utils.FileUtils.JAR_EXTENSION;
 import static junit.framework.TestCase.assertEquals;
 
 import com.android.tools.r8.Jdk11TestUtils;
@@ -15,9 +14,9 @@
 import com.android.tools.r8.TestRuntime.CfVm;
 import com.android.tools.r8.ToolHelper;
 import com.android.tools.r8.cf.bootstrap.BootstrapCurrentEqualityTest;
+import com.android.tools.r8.examples.hello.HelloTestRunner;
 import com.google.common.collect.ImmutableList;
 import java.nio.file.Path;
-import java.nio.file.Paths;
 import org.junit.Assume;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -35,8 +34,9 @@
 @RunWith(Parameterized.class)
 public class Java17R8BootstrapTest extends TestBase {
 
+  private static final Class<?> HELLO_CLASS = HelloTestRunner.getHelloClass();
   private static final String[] HELLO_KEEP = {
-    "-keep class hello.Hello {  public static void main(...);}"
+    "-keep class " + HELLO_CLASS.getTypeName() + " {  public static void main(...);}"
   };
 
   private static Path r8Lib17NoDesugar;
@@ -85,17 +85,20 @@
     Assume.assumeTrue(supportsSealedClassesWhenGeneratingCf());
     Path prevGeneratedJar = null;
     String prevRunResult = null;
+    Path helloJar = HelloTestRunner.writeHelloProgramJar(temp);
     for (Path jar : jarsToCompare()) {
       Path generatedJar =
           testForExternalR8(Backend.CF, parameters.getRuntime())
               .useProvidedR8(jar)
-              .addProgramFiles(Paths.get(ToolHelper.EXAMPLES_BUILD_DIR, "hello" + JAR_EXTENSION))
+              .addProgramFiles(helloJar)
               .addKeepRules(HELLO_KEEP)
               .compile()
               .outputJar();
       String runResult =
           ToolHelper.runJava(
-                  parameters.getRuntime().asCf(), ImmutableList.of(generatedJar), "hello.Hello")
+                  parameters.getRuntime().asCf(),
+                  ImmutableList.of(generatedJar),
+                  HELLO_CLASS.getTypeName())
               .toString();
       if (prevRunResult != null) {
         assertEquals(prevRunResult, runResult);
@@ -117,11 +120,12 @@
     Assume.assumeTrue(JavaBootstrapUtils.exists(ToolHelper.R8_WITH_RELOCATED_DEPS_17_JAR));
     Assume.assumeTrue(supportsSealedClassesWhenGeneratingCf());
     Path prevGeneratedJar = null;
+    Path helloJar = HelloTestRunner.writeHelloProgramJar(temp);
     for (Path jar : jarsToCompare()) {
       Path generatedJar =
           testForExternalR8(Backend.CF, parameters.getRuntime())
               .useProvidedR8(jar)
-              .addProgramFiles(Paths.get(ToolHelper.EXAMPLES_BUILD_DIR, "hello" + JAR_EXTENSION))
+              .addProgramFiles(helloJar)
               .addKeepRuleFiles(MAIN_KEEP)
               .compile()
               .outputJar();
diff --git a/src/test/examples/hello/Hello.java b/src/test/java/com/android/tools/r8/examples/hello/Hello.java
similarity index 90%
rename from src/test/examples/hello/Hello.java
rename to src/test/java/com/android/tools/r8/examples/hello/Hello.java
index c58f329..0be083c 100644
--- a/src/test/examples/hello/Hello.java
+++ b/src/test/java/com/android/tools/r8/examples/hello/Hello.java
@@ -5,7 +5,7 @@
 // This code is not run directly. It needs to be compiled to dex code.
 // 'hello.dex' is what is run.
 
-package hello;
+package com.android.tools.r8.examples.hello;
 
 class Hello {
   public static void main(String[] args) {
diff --git a/src/test/java/com/android/tools/r8/examples/hello/HelloTestRunner.java b/src/test/java/com/android/tools/r8/examples/hello/HelloTestRunner.java
new file mode 100644
index 0000000..cb305d2
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/examples/hello/HelloTestRunner.java
@@ -0,0 +1,69 @@
+// Copyright (c) 2023, 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 com.android.tools.r8.examples.hello;
+
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.examples.ExamplesTestBase;
+import com.android.tools.r8.utils.StringUtils;
+import java.io.IOException;
+import java.nio.file.Path;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class HelloTestRunner extends ExamplesTestBase {
+
+  @Parameterized.Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withAllRuntimesAndApiLevels().enableApiLevelsForCf().build();
+  }
+
+  // The "hello" program is reused in various tests via these static methods.
+
+  public static Class<?> getHelloClass() {
+    return Hello.class;
+  }
+
+  public static String getExpectedOutput() {
+    return StringUtils.lines("Hello, world");
+  }
+
+  public static Path writeHelloProgramJar(TemporaryFolder temp) throws IOException {
+    Path jar = temp.newFolder().toPath().resolve("hello.jar");
+    writeClassesToJar(jar, Hello.class);
+    return jar;
+  }
+
+  public HelloTestRunner(TestParameters parameters) {
+    super(parameters);
+  }
+
+  @Override
+  public Class<?> getMainClass() {
+    return getHelloClass();
+  }
+
+  @Override
+  public String getExpected() {
+    return getExpectedOutput();
+  }
+
+  @Test
+  public void testDesugaring() throws Exception {
+    runTestDesugaring();
+  }
+
+  @Test
+  public void testR8() throws Exception {
+    runTestR8();
+  }
+
+  @Test
+  public void testDebug() throws Exception {
+    runTestDebugComparator();
+  }
+}