Postpone compilation of jdk11 time files

to the tests to decrease build time.

Bug: 201247429
Change-Id: I0c68e4277ff435b92a76198fcc11ca4971329e33
diff --git a/build.gradle b/build.gradle
index 178d8e8..834dcd4 100644
--- a/build.gradle
+++ b/build.gradle
@@ -136,15 +136,6 @@
             srcDirs = ['src/test/examplesJava17']
         }
     }
-    jdk11TimeTests {
-        java {
-            srcDirs = [
-                    'third_party/openjdk/jdk-11-test/java/time/tck',
-                    'third_party/openjdk/jdk-11-test/java/time/test'
-            ]
-            exclude '**/TestZoneTextPrinterParser.java'
-        }
-    }
     examplesTestNGRunner {
         java {
             srcDirs = ['src/test/testngrunner']
@@ -249,7 +240,6 @@
     implementation group: 'org.ow2.asm', name: 'asm-analysis', version: asmVersion
     implementation group: 'org.ow2.asm', name: 'asm-util', version: asmVersion
     implementation files('third_party/android_jar/api-database/api-database.jar')
-    jdk11TimeTestsCompile group: 'org.testng', name: 'testng', version: testngVersion
     examplesTestNGRunnerCompile group: 'org.testng', name: 'testng', version: testngVersion
     testCompile sourceSets.examples.output
     testCompile "junit:junit:$junitVersion"
@@ -599,11 +589,6 @@
         JavaVersion.VERSION_1_9,
         false)
 setJdkCompilationWithCompatibility(
-        sourceSets.examplesJava11.compileJavaTaskName,
-        'jdk-11',
-        JavaVersion.VERSION_11,
-        false)
-setJdkCompilationWithCompatibility(
         sourceSets.examplesJava10.compileJavaTaskName,
         'jdk-11',
         JavaVersion.VERSION_1_10,
@@ -619,11 +604,6 @@
         JavaVersion.VERSION_11,
         false)
 setJdkCompilationWithCompatibility(
-        sourceSets.jdk11TimeTests.compileJavaTaskName,
-        'jdk-11',
-        JavaVersion.VERSION_11,
-        false)
-setJdkCompilationWithCompatibility(
         sourceSets.examplesJava17.compileJavaTaskName,
         'jdk-17',
         JavaVersion.VERSION_17,
@@ -1634,28 +1614,6 @@
     })
 }
 
-task provideJdk11TestsDependencies(type: org.gradle.api.tasks.Copy) {
-    from sourceSets.jdk11TimeTests.compileClasspath
-    include "**/**.jar"
-    into file("build/test/jdk11Tests")
-}
-
-task buildJdk11TimeTestsJar {
-    def exampleOutputDir = file("build/test/jdk11Tests");
-    def jarName = "jdk11TimeTests.jar"
-    dependsOn "jar_jdk11TimeTests"
-    dependsOn provideJdk11TestsDependencies
-    task "jar_jdk11TimeTests"(type: Jar) {
-        archiveName = jarName
-        destinationDir = exampleOutputDir
-        from sourceSets.examplesTestNGRunner.output
-        include "**.class"
-        from sourceSets.jdk11TimeTests.output
-        include "**.class"
-        include "**/**.class"
-    }
-}
-
 task buildKotlinR8TestResources {
     def examplesDir = file("src/test/kotlinR8TestResources")
     examplesDir.eachDir { dir ->
@@ -2407,7 +2365,6 @@
         dependsOn buildDebugInfoExamplesDex
         dependsOn buildPreNJdwpTestsJar
         dependsOn buildPreNJdwpTestsDex
-        dependsOn buildJdk11TimeTestsJar
         dependsOn provideArtFrameworksDependencies
     } else {
         logger.lifecycle("WARNING: Testing in not supported on your platform. Testing is only fully supported on " +
diff --git a/src/test/java/com/android/tools/r8/ToolHelper.java b/src/test/java/com/android/tools/r8/ToolHelper.java
index 14db34c..be6c8f0 100644
--- a/src/test/java/com/android/tools/r8/ToolHelper.java
+++ b/src/test/java/com/android/tools/r8/ToolHelper.java
@@ -144,6 +144,7 @@
   private static final AndroidApiLevel DEFAULT_MIN_SDK = AndroidApiLevel.I;
 
   public static final String JDK_11_TESTS_DIR = "third_party/openjdk/jdk-11-test/";
+  public static final String JDK_11_TIME_TESTS_DIR = JDK_11_TESTS_DIR + "java/time/";
 
   private static final String PROGUARD5_2_1 = "third_party/proguard/proguard5.2.1/bin/proguard";
   private static final String PROGUARD6_0_1 = "third_party/proguard/proguard6.0.1/bin/proguard";
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeTests.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeTests.java
index 2d6b833..a0a51c0 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeTests.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdktests/Jdk11TimeTests.java
@@ -5,19 +5,29 @@
 package com.android.tools.r8.desugar.desugaredlibrary.jdktests;
 
 import static com.android.tools.r8.ToolHelper.JDK_TESTS_BUILD_DIR;
+import static com.android.tools.r8.utils.FileUtils.CLASS_EXTENSION;
+import static com.android.tools.r8.utils.FileUtils.JAVA_EXTENSION;
 import static org.junit.Assert.assertTrue;
 
 import com.android.tools.r8.D8TestCompileResult;
 import com.android.tools.r8.D8TestRunResult;
 import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestRuntime;
 import com.android.tools.r8.ToolHelper;
 import com.android.tools.r8.ToolHelper.DexVm;
 import com.android.tools.r8.ToolHelper.DexVm.Version;
 import com.android.tools.r8.utils.AndroidApiLevel;
 import com.android.tools.r8.utils.BooleanUtils;
 import com.android.tools.r8.utils.StringUtils;
+import com.google.common.collect.ImmutableList;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
+import java.util.stream.Collectors;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
@@ -47,6 +57,54 @@
     this.parameters = parameters;
   }
 
+  private static final Path JDK_11_TCK_TEST_FILES_DIR =
+      Paths.get(ToolHelper.JDK_11_TIME_TESTS_DIR).resolve("tck");
+  private static final Path JDK_11_TIME_TEST_FILES_DIR =
+      Paths.get(ToolHelper.JDK_11_TIME_TESTS_DIR).resolve("test");
+  private static final String JDK_11_TIME_TEST_EXCLUDE = "TestZoneTextPrinterParser.java";
+  private static Path[] JDK_11_TIME_TEST_COMPILED_FILES;
+
+  private static List<Path> getJdk11TimeTestFiles() throws Exception {
+    List<Path> tckFiles =
+        Files.walk(JDK_11_TCK_TEST_FILES_DIR)
+            .filter(path -> path.toString().endsWith(JAVA_EXTENSION))
+            .filter(path -> !path.toString().endsWith(JDK_11_TIME_TEST_EXCLUDE))
+            .collect(Collectors.toList());
+    List<Path> timeFiles =
+        Files.walk(JDK_11_TIME_TEST_FILES_DIR)
+            .filter(path -> path.toString().endsWith(JAVA_EXTENSION))
+            .filter(path -> !path.toString().endsWith(JDK_11_TIME_TEST_EXCLUDE))
+            .collect(Collectors.toList());
+    ArrayList<Path> files = new ArrayList<>();
+    files.addAll(timeFiles);
+    files.addAll(tckFiles);
+    assert files.size() > 0;
+    return files;
+  }
+
+  @BeforeClass
+  public static void compileJdk11StreamTests() throws Exception {
+    Path tmpDirectory = getStaticTemp().newFolder("time").toPath();
+    List<String> options =
+        Arrays.asList(
+            "--add-reads",
+            "java.base=ALL-UNNAMED",
+            "--patch-module",
+            "java.base=" + JDK_11_JAVA_BASE_EXTENSION_CLASSES_DIR);
+    javac(TestRuntime.getCheckedInJdk11(), getStaticTemp())
+        .addOptions(options)
+        .addClasspathFiles(
+            ImmutableList.of(
+                Paths.get(JDK_TESTS_BUILD_DIR + "testng-6.10.jar"),
+                Paths.get(JDK_TESTS_BUILD_DIR + "jcommander-1.48.jar")))
+        .addSourceFiles(getJdk11TimeTestFiles())
+        .setOutputPath(tmpDirectory)
+        .compile();
+    JDK_11_TIME_TEST_COMPILED_FILES =
+        getAllFilesWithSuffixInDirectory(tmpDirectory, CLASS_EXTENSION);
+    assert JDK_11_TIME_TEST_COMPILED_FILES.length > 0;
+  }
+
   // Following tests are also failing on the Bazel build, they cannot be run easily on
   // Android (difference in time precision, iAndroid printing, etc.).
   private static String[] wontFixFailures =
@@ -145,9 +203,12 @@
         testForD8()
             .addLibraryFiles(ToolHelper.getAndroidJar(AndroidApiLevel.P))
             .addProgramFiles(getPathsFiles())
-            .addProgramFiles(Paths.get(JDK_TESTS_BUILD_DIR + "jdk11TimeTests.jar"))
+            .addProgramFiles(JDK_11_TIME_TEST_COMPILED_FILES)
             .addProgramFiles(Paths.get(JDK_TESTS_BUILD_DIR + "testng-6.10.jar"))
             .addProgramFiles(Paths.get(JDK_TESTS_BUILD_DIR + "jcommander-1.48.jar"))
+            .addProgramFiles(
+                Paths.get(
+                    ToolHelper.JAVA_CLASSES_DIR + "examplesTestNGRunner/TestNGMainRunner.class"))
             .setMinApi(parameters.getApiLevel())
             .enableCoreLibraryDesugaring(parameters.getApiLevel(), keepRuleConsumer)
             .compile()