Convert libcore tests to Android.bp

Bug: 71899243
Test: m checkbuild
Test: atest CtsLibcoreTestCases
Test: atest CtsLibcoreJsr166TestCases
Test: atest CtsFileSystemTestCases
Test: mmma libcore EXPERIMENTAL_USE_OPENJDK9=true
Change-Id: Ic41b671055f1c9c802e5b7fc9f70c17da7be4e7d
diff --git a/JavaLibrary.bp b/JavaLibrary.bp
index e548bf3..295b09f 100644
--- a/JavaLibrary.bp
+++ b/JavaLibrary.bp
@@ -51,7 +51,6 @@
         //"-Xlint:-serial,-deprecation,-unchecked",
     ],
     dxflags: ["--core-library"],
-    no_standard_libs: true,
     errorprone: {
         javacflags: [
             "-Xep:MissingOverride:OFF",  // Ignore missing @Override.
@@ -74,10 +73,14 @@
         ":android_icu4j_src_files",
         ":openjdk_lambda_stub_files",
     ],
+
+    no_standard_libs: true,
+    system_modules: "none",
     openjdk9: {
         srcs: ["luni/src/module/java/module-info.java"],
         javacflags: ["--patch-module=java.base=."],
     },
+
     java_resource_dirs: core_resource_dirs,
     java_resources: [":android_icu4j_resources"],
 
@@ -86,8 +89,6 @@
         "tzlookup.xml",
     ],
 
-    system_modules: "none",
-
     installable: false,
 }
 
@@ -104,11 +105,14 @@
 
     srcs: [":openjdk_java_files"],
     java_resource_dirs: core_resource_dirs,
+
+    no_standard_libs: true,
     libs: ["core-all"],
     system_modules: "core-all-system-modules",
     openjdk9: {
         javacflags: ["--patch-module=java.base=."],
     },
+
     jacoco: {
         exclude_filter: [
             "java.lang.Class",
@@ -148,11 +152,13 @@
     ],
     java_resources: [":android_icu4j_resources"],
 
+    no_standard_libs: true,
     libs: ["core-all"],
     system_modules: "core-all-system-modules",
     openjdk9: {
         javacflags: ["--patch-module=java.base=."],
     },
+
     jacoco: {
         exclude_filter: [
             "java.lang.DexCache",
@@ -216,6 +222,7 @@
         ":openjdk_lambda_duplicate_stub_files",
     ],
 
+    no_standard_libs: true,
     libs: ["core-all"],
     system_modules: "core-all-system-modules",
     openjdk9: {
@@ -266,7 +273,7 @@
 }
 
 // Make the jsr166-tests library.
-java_library_static {
+java_test {
     name: "jsr166-tests",
     srcs: ["jsr166-tests/src/test/java/**/*.java"],
     no_framework_libs: true,
@@ -275,6 +282,155 @@
     ],
 }
 
+// Build a library just containing files from luni/src/test/filesystems for use in tests.
+java_library {
+    name: "filesystemstest",
+    compile_dex: true,
+    srcs: ["luni/src/test/filesystems/src/**/*.java"],
+    java_resource_dirs: ["luni/src/test/filesystems/resources"],
+    no_framework_libs: true,
+    errorprone: {
+        javacflags: ["-Xep:MissingOverride:OFF"],
+    },
+}
+
+// Build a library just containing files from luni/src/test/parameter_metadata for use in tests.
+java_library {
+    name: "parameter-metadata-test",
+    compile_dex: true,
+    srcs: ["luni/src/test/parameter_metadata/src/**/*.java"],
+    no_framework_libs: true,
+    javacflags: ["-parameters"],
+    errorprone: {
+        javacflags: ["-Xep:MissingOverride:OFF"],
+    },
+}
+
+// Make the core-tests library.
+java_test {
+    name: "core-tests",
+    defaults: ["libcore_java_defaults"],
+    hostdex: true,
+    srcs: [
+        "dalvik/src/test/java/**/*.java",
+        "dalvik/test-rules/src/test/java/**/*.java",
+        "dom/src/test/java/**/*.java",
+        "harmony-tests/src/test/java/**/*.java",
+        "json/src/test/java/**/*.java",
+        "luni/src/test/java/**/*.java",
+        "xml/src/test/java/**/*.java",
+    ],
+    exclude_srcs: [
+        "luni/src/test/java/libcore/java/util/zip/Zip64Test.java",
+        "luni/src/test/java/libcore/java/util/zip/Zip64FileTest.java",
+    ],
+
+    java_resource_dirs: [
+        "*/src/test/java",
+        "*/src/test/resources",
+    ],
+    exclude_java_resource_dirs: [
+        "ojluni/src/test/java",
+        "ojluni/src/test/resources",
+    ],
+
+    java_resources: [
+        ":filesystemstest",
+        ":parameter-metadata-test",
+    ],
+
+    libs: [
+        "okhttp",
+        "bouncycastle",
+    ],
+    static_libs: [
+        "archive-patcher",
+        "core-test-rules",
+        "core-tests-support",
+        "junit-params",
+        "mockftpserver",
+        "mockito-target",
+        "mockwebserver",
+        "nist-pkix-tests",
+        "slf4j-jdk14",
+        "sqlite-jdbc",
+        "tzdata-testing",
+    ],
+
+    errorprone: {
+        javacflags: [
+            "-Xep:TryFailThrowable:ERROR",
+            "-Xep:ComparisonOutOfRange:ERROR",
+        ],
+    },
+}
+
+// Make the core-ojtests library.
+java_test {
+    name: "core-ojtests",
+    defaults: ["libcore_java_defaults"],
+    hostdex: true,
+
+    srcs: [
+        "ojluni/src/test/java/**/*.java",
+    ],
+    java_resource_dirs: [
+        "ojluni/src/test/java",
+        "ojluni/src/test/resources",
+    ],
+    libs: [
+        "okhttp",
+        "bouncycastle",
+    ],
+    static_libs: ["testng"],
+
+    // ojluni/src/test/java/util/stream/{bootlib,boottest}
+    // contains tests that are in packages from java.base;
+    // By default, OpenJDK 9's javac will only compile such
+    // code if it's declared to also be in java.base at
+    // compile time.
+    //
+    // For now, we use --patch-module to put all sources
+    // and dependencies from this make target into java.base;
+    // other source directories in this make target are in
+    // packages not from java.base; if this becomes a problem
+    // in future, this could be addressed eg. by splitting
+    // boot{lib,test} out into a separate make target,
+    // deleting those tests or moving them to a different
+    // package.
+    patch_module: "java.base",
+}
+
+// Make the core-ojtests-public library. Excludes any private API tests.
+java_test {
+    name: "core-ojtests-public",
+    defaults: ["libcore_java_defaults"],
+    srcs: [
+        "ojluni/src/test/java/**/*.java",
+    ],
+    // Filter out the following:
+    // 1.) DeserializeMethodTest and SerializedLambdaTest, because they depends on stub classes
+    //     and won't actually run, and
+    // 2.) util/stream/boot*. Those directories contain classes in the package java.util.stream;
+    //     excluding them means we don't need patch_module: "java.base"
+    exclude_srcs: [
+        "**/DeserializeMethodTest.java",
+        "**/SerializedLambdaTest.java",
+        "ojluni/src/test/java/util/stream/boot*/**/*",
+    ],
+    java_resource_dirs: [
+        "ojluni/src/test/java",
+        "ojluni/src/test/resources",
+        // Include source code as part of JAR
+        "ojluni/src/test/dist",
+    ],
+    libs: [
+        "bouncycastle",
+        "okhttp",
+        "testng",
+    ],
+}
+
 genrule {
     name: "gen-ojluni-jaif-annotated-srcs",
     tools: [
diff --git a/JavaLibrary.mk b/JavaLibrary.mk
index ad17c58..9848331 100644
--- a/JavaLibrary.mk
+++ b/JavaLibrary.mk
@@ -14,80 +14,6 @@
 # limitations under the License.
 
 #
-# Definitions for building the Java library and associated tests.
-#
-
-#
-# Common definitions for host and target.
-#
-
-# libcore is divided into modules.
-#
-# The structure of each module is:
-#
-#   src/
-#       main/               # To be shipped on every device.
-#            java/          # Java source for library code.
-#            native/        # C++ source for library code.
-#            resources/     # Support files.
-#       test/               # Built only on demand, for testing.
-#            java/          # Java source for tests.
-#            native/        # C++ source for tests (rare).
-#            resources/     # Support files.
-#
-# All subdirectories are optional (hence the "2> /dev/null"s below).
-
-define all-test-java-files-under
-$(foreach dir,$(1),$(patsubst ./%,%,$(shell cd $(LOCAL_PATH) && (find $(dir)/src/test/java -name "*.java" 2> /dev/null) | grep -v -f java_tests_blacklist)))
-endef
-
-define all-core-resource-dirs
-$(shell cd $(LOCAL_PATH) && ls -d */src/$(1)/{java,resources} 2> /dev/null)
-endef
-
-# The Java files and their associated resources.
-core_resource_dirs := \
-  luni/src/main/java \
-  ojluni/src/main/resources/
-test_resource_dirs := $(filter-out ojluni/%,$(call all-core-resource-dirs,test))
-test_src_files := $(call all-test-java-files-under,dalvik dalvik/test-rules dom harmony-tests json luni xml)
-ojtest_src_files := $(call all-test-java-files-under,ojluni)
-ojtest_resource_dirs := $(filter ojluni/%,$(call all-core-resource-dirs,test))
-
-ifeq ($(EMMA_INSTRUMENT),true)
-ifneq ($(EMMA_INSTRUMENT_STATIC),true)
-    nojcore_src_files += $(call all-java-files-under, ../external/emma/core ../external/emma/pregenerated)
-    core_resource_dirs += ../external/emma/core/res ../external/emma/pregenerated/res
-endif
-endif
-
-local_javac_flags=-encoding UTF-8
-#local_javac_flags+=-Xlint:all -Xlint:-serial,-deprecation,-unchecked
-local_javac_flags+=-Xmaxwarns 9999999
-
-# For user / userdebug builds, strip the local variable table and the local variable
-# type table. This has no bearing on stack traces, but will leave less information
-# available via JDWP.
-#
-# TODO: Should this be conditioned on a PRODUCT_ flag or should we just turn this
-# on for all builds. Also, name of the flag TBD.
-ifneq (,$(PRODUCT_MINIMIZE_JAVA_DEBUG_INFO))
-ifneq (,$(filter userdebug user,$(TARGET_BUILD_VARIANT)))
-local_javac_flags+= -g:source,lines
-local_jack_flags+= -D jack.dex.debug.vars=false -D jack.dex.debug.vars.synthetic=false
-endif
-endif
-
-#
-# ICU4J related rules.
-#
-# We compile android_icu4j along with core-libart because we're implementing parts of core-libart
-# in terms of android_icu4j.
-android_icu4j_root := ../external/icu/android_icu4j/
-android_icu4j_src_files := $(call all-java-files-under,$(android_icu4j_root)/src/main/java)
-android_icu4j_resource_dirs := $(android_icu4j_root)/resources
-
-#
 # Build jaif-annotated source files for ojluni target .
 #
 ojluni_annotate_dir := $(call intermediates-dir-for,JAVA_LIBRARIES,core-oj,,COMMON)/annotated
@@ -125,191 +51,6 @@
 ojluni_annotate_input:=
 ojluni_annotate_output:=
 
-#
-# Build for the target (device).
-#
-ifeq ($(LIBCORE_SKIP_TESTS),)
-# Build a library just containing files from luni/src/test/filesystems for use in tests.
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := $(call all-java-files-under, luni/src/test/filesystems/src)
-LOCAL_JAVA_RESOURCE_DIRS := luni/src/test/filesystems/resources
-LOCAL_NO_STANDARD_LIBRARIES := true
-LOCAL_MODULE := filesystemstest
-LOCAL_JAVA_LIBRARIES := core-oj core-libart
-LOCAL_DEX_PREOPT := false
-LOCAL_ERROR_PRONE_FLAGS := -Xep:MissingOverride:OFF
-include $(BUILD_JAVA_LIBRARY)
-
-filesystemstest_jar := $(intermediates)/$(LOCAL_MODULE).jar
-$(filesystemstest_jar): $(LOCAL_BUILT_MODULE)
-	$(call copy-file-to-target)
-
-# Build a library just containing files from luni/src/test/parameter_metadata for use in tests.
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := $(call all-java-files-under, luni/src/test/parameter_metadata/src)
-LOCAL_NO_STANDARD_LIBRARIES := true
-LOCAL_MODULE := parameter-metadata-test
-LOCAL_JAVA_LIBRARIES := core-oj core-libart
-LOCAL_DEX_PREOPT := false
-LOCAL_JAVACFLAGS := -parameters
-LOCAL_ERROR_PRONE_FLAGS := -Xep:MissingOverride:OFF
-include $(BUILD_JAVA_LIBRARY)
-
-parameter_metadata_test_jar := $(intermediates)/$(LOCAL_MODULE).jar
-$(parameter_metadata_test_jar): $(LOCAL_BUILT_MODULE)
-	$(call copy-file-to-target)
-
-endif
-
-ifeq ($(LIBCORE_SKIP_TESTS),)
-# Make the core-tests library.
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := $(test_src_files)
-LOCAL_JAVA_RESOURCE_DIRS := $(test_resource_dirs)
-# Include individual dex.jar files (jars containing resources and a classes.dex) so that they
-# be loaded by tests using ClassLoaders but are not in the main classes.dex.
-LOCAL_JAVA_RESOURCE_FILES := $(filesystemstest_jar) $(parameter_metadata_test_jar)
-LOCAL_NO_STANDARD_LIBRARIES := true
-LOCAL_JAVA_LIBRARIES := core-oj core-libart okhttp bouncycastle
-LOCAL_STATIC_JAVA_LIBRARIES := \
-	archive-patcher \
-	core-test-rules \
-	core-tests-support \
-	junit-params \
-	mockftpserver \
-	mockito-target \
-	mockwebserver \
-	nist-pkix-tests \
-	slf4j-jdk14 \
-	sqlite-jdbc \
-	tzdata-testing
-LOCAL_JAVACFLAGS := $(local_javac_flags)
-LOCAL_JACK_FLAGS := $(local_jack_flags)
-LOCAL_ERROR_PRONE_FLAGS := \
-        -Xep:TryFailThrowable:ERROR \
-        -Xep:ComparisonOutOfRange:ERROR \
-        -Xep:MissingOverride:OFF
-LOCAL_MODULE := core-tests
-include $(BUILD_STATIC_JAVA_LIBRARY)
-endif
-
-# Make the core-ojtests library.
-ifeq ($(LIBCORE_SKIP_TESTS),)
-    include $(CLEAR_VARS)
-    LOCAL_JAVA_RESOURCE_DIRS := $(ojtest_resource_dirs)
-    LOCAL_NO_STANDARD_LIBRARIES := true
-    LOCAL_JAVA_LIBRARIES := core-oj core-libart okhttp bouncycastle
-    LOCAL_STATIC_JAVA_LIBRARIES := testng
-    LOCAL_JAVACFLAGS := $(local_javac_flags)
-    LOCAL_JACK_FLAGS := $(local_jack_flags)
-    LOCAL_DX_FLAGS := --core-library
-    LOCAL_MODULE_TAGS := optional
-    LOCAL_MODULE := core-ojtests
-    LOCAL_ERROR_PRONE_FLAGS := -Xep:MissingOverride:OFF
-    include $(BUILD_JAVA_LIBRARY)
-endif
-
-# Make the core-ojtests-public library. Excludes any private API tests.
-ifeq ($(LIBCORE_SKIP_TESTS),)
-    include $(CLEAR_VARS)
-    # Filter out the following:
-    # 1.) DeserializeMethodTest and SerializedLambdaTest, because they depends on stub classes
-    #     and won't actually run, and
-    # 2.) util/stream/boot*. Those directories contain classes in the package java.util.stream;
-    #     excluding them means we don't need LOCAL_PATCH_MODULE := java.base
-    LOCAL_SRC_FILES := $(filter-out %/DeserializeMethodTest.java %/SerializedLambdaTest.java ojluni/src/test/java/util/stream/boot%,$(ojtest_src_files))
-    # Include source code as part of JAR
-    LOCAL_JAVA_RESOURCE_DIRS := ojluni/src/test/dist $(ojtest_resource_dirs)
-    LOCAL_NO_STANDARD_LIBRARIES := true
-    LOCAL_JAVA_LIBRARIES := \
-        bouncycastle \
-        core-libart \
-        core-oj \
-        okhttp \
-        testng
-    LOCAL_JAVACFLAGS := $(local_javac_flags)
-    LOCAL_JACK_FLAGS := $(local_jack_flags)
-    LOCAL_DX_FLAGS := --core-library
-    LOCAL_MODULE_TAGS := optional
-    LOCAL_MODULE := core-ojtests-public
-    LOCAL_ERROR_PRONE_FLAGS := -Xep:MissingOverride:OFF
-    # Some tests have known overflow. To not have to add annotations, turn off the overflow check.
-    LOCAL_ERROR_PRONE_FLAGS += -Xep:ConstantOverflow:OFF
-    include $(BUILD_JAVA_LIBRARY)
-endif
-
-#
-# Build for the host.
-#
-
-ifeq ($(HOST_OS),linux)
-
-# Make the core-tests-hostdex library.
-ifeq ($(LIBCORE_SKIP_TESTS),)
-    include $(CLEAR_VARS)
-    LOCAL_SRC_FILES := $(test_src_files)
-    LOCAL_JAVA_RESOURCE_DIRS := $(test_resource_dirs)
-    LOCAL_NO_STANDARD_LIBRARIES := true
-    LOCAL_JAVA_LIBRARIES := \
-        bouncycastle-hostdex \
-        core-libart-hostdex \
-        core-oj-hostdex \
-        core-tests-support-hostdex \
-        junit-hostdex \
-        mockito-api-hostdex \
-        okhttp-hostdex
-    LOCAL_STATIC_JAVA_LIBRARIES := \
-        archive-patcher-hostdex \
-        core-test-rules-hostdex \
-        junit-params-hostdex \
-        mockftpserver-hostdex \
-        mockwebserver-hostdex \
-        nist-pkix-tests-host \
-        slf4j-jdk14-hostdex \
-        sqlite-jdbc-host \
-        tzdata-testing-hostdex
-    LOCAL_JAVACFLAGS := $(local_javac_flags)
-    LOCAL_MODULE_TAGS := optional
-    LOCAL_MODULE := core-tests-hostdex
-    LOCAL_ERROR_PRONE_FLAGS := -Xep:MissingOverride:OFF
-    include $(BUILD_HOST_DALVIK_JAVA_LIBRARY)
-endif
-
-# Make the core-ojtests-hostdex library.
-ifeq ($(LIBCORE_SKIP_TESTS),)
-    include $(CLEAR_VARS)
-    LOCAL_SRC_FILES := $(ojtest_src_files)
-    LOCAL_NO_STANDARD_LIBRARIES := true
-    LOCAL_JAVA_LIBRARIES := \
-        bouncycastle-hostdex \
-        core-libart-hostdex \
-        core-oj-hostdex \
-        okhttp-hostdex
-    LOCAL_STATIC_JAVA_LIBRARIES := testng-hostdex
-    LOCAL_JAVACFLAGS := $(local_javac_flags)
-    LOCAL_DX_FLAGS := --core-library
-    LOCAL_MODULE_TAGS := optional
-    LOCAL_MODULE := core-ojtests-hostdex
-    # ojluni/src/test/java/util/stream/{bootlib,boottest}
-    # contains tests that are in packages from java.base;
-    # By default, OpenJDK 9's javac will only compile such
-    # code if it's declared to also be in java.base at
-    # compile time.
-    #
-    # For now, we use --patch-module to put all sources
-    # and dependencies from this make target into java.base;
-    # other source directories in this make target are in
-    # packages not from java.base; if this becomes a proble
-    # in future, this could be addressed eg. by splitting
-    # boot{lib,test} out into a separate make target,
-    # deleting those tests or moving them to a different
-    # package.
-    LOCAL_PATCH_MODULE := java.base
-    LOCAL_ERROR_PRONE_FLAGS := -Xep:MissingOverride:OFF
-    include $(BUILD_HOST_DALVIK_JAVA_LIBRARY)
-endif
-
-endif # HOST_OS == linux
 
 # Archive a copy of the classes.jar in SDK build.
 full_classes_jar := $(call intermediates-dir-for,JAVA_LIBRARIES,core.current.stubs,,COMMON)/classes.jar
diff --git a/java_tests_blacklist b/java_tests_blacklist
deleted file mode 100644
index fe3ddc2..0000000
--- a/java_tests_blacklist
+++ /dev/null
@@ -1,2 +0,0 @@
-luni/src/test/java/libcore/java/util/zip/Zip64Test.java
-luni/src/test/java/libcore/java/util/zip/Zip64FileTest.java