Add `java_library` tests

(ignore-relnotes)

PiperOrigin-RevId: 893090207
Change-Id: Idc90df60767576cf47d3e9faba36465da7534818
diff --git a/test/java/common/rules/common_launcher_java_library_tests.bzl b/test/java/common/rules/common_launcher_java_library_tests.bzl
index 8f0ed22..9690b2b 100644
--- a/test/java/common/rules/common_launcher_java_library_tests.bzl
+++ b/test/java/common/rules/common_launcher_java_library_tests.bzl
@@ -1,8 +1,13 @@
 """Parameterized tests for java_library with --java_launcher"""
 
+load("@bazel_features//:features.bzl", "bazel_features")
 load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
 load("@rules_testing//lib:util.bzl", "util")
+load("//java:java_import.bzl", "java_import")
 load("//java:java_library.bzl", "java_library")
+load("//test/java/testutil:helper.bzl", "always_passes")
+load("//test/java/testutil:java_info_subject.bzl", "java_info_subject")
+load("//test/java/testutil:javac_action_subject.bzl", "javac_action_subject")
 
 def _test_java_library_rule_outputs(name):
     util.helper_target(
@@ -22,6 +27,227 @@
         "{package}/lib{name}.jar",
     ])
 
+def _test_java_library_action_graph(name):
+    util.helper_target(
+        java_library,
+        name = name + "/test_lib",
+        srcs = [
+            "Util.java",
+            "Util2.java",
+        ],
+        javacopts = [
+            "-g",
+            "-encoding",
+            "utf8",
+        ],
+    )
+
+    analysis_test(
+        name = name,
+        impl = _test_java_library_action_graph_impl,
+        target = name + "/test_lib",
+    )
+
+def _test_java_library_action_graph_impl(env, target):
+    javac_action = javac_action_subject.of(env, target, "{package}/lib{name}.jar")
+    javac_action.inputs().contains_at_least([
+        "{package}/Util.java",
+        "{package}/Util2.java",
+    ])
+    javac_action.javacopts().contains_at_least([
+        "-g",
+        "-encoding",
+        "utf8",
+    ])
+
+def _test_java_library_deps_of_genrule_are_not_on_classpath(name):
+    util.helper_target(
+        java_library,
+        name = name + "/root_dep",
+        srcs = ["test.java"],
+    )
+    util.helper_target(
+        native.genrule,
+        name = name + "/has_java_dep",
+        outs = ["foo.jar"],
+        cmd = "echo NOT EXECUTED",
+        tools = [name + "/root_dep"],
+    )
+    util.helper_target(
+        java_import,
+        name = name + "/has_java_dep_import",
+        jars = [name + "/has_java_dep"],
+    )
+    util.helper_target(
+        java_library,
+        name = name + "/library",
+        srcs = ["dummy.java"],
+        deps = [name + "/has_java_dep_import"],
+    )
+
+    analysis_test(
+        name = name,
+        impl = _test_java_library_deps_of_genrule_are_not_on_classpath_impl,
+        target = name + "/library",
+    )
+
+def _test_java_library_deps_of_genrule_are_not_on_classpath_impl(env, target):
+    expected_classpath = "{bin_path}/{package}/_ijar/{test_name}/has_java_dep_import/{package}/foo-ijar.jar"
+    javac_action_subject.of(env, target, "{package}/lib{name}.jar").classpath().contains_exactly([expected_classpath])
+
+def _test_java_library_compile_and_run_time_paths(name):
+    util.helper_target(
+        java_library,
+        name = name + "/base",
+        srcs = ["Base.java"],
+    )
+    util.helper_target(
+        java_library,
+        name = name + "/specialization",
+        srcs = ["Specialization.java"],
+        deps = [name + "/base"],
+    )
+
+    analysis_test(
+        name = name,
+        impl = _test_java_library_compile_and_run_time_paths_impl,
+        targets = {
+            "base": name + "/base",
+            "specialization": name + "/specialization",
+        },
+    )
+
+def _test_java_library_compile_and_run_time_paths_impl(env, targets):
+    base_info = java_info_subject.from_target(env, targets.base)
+    base_info.compilation_args().transitive_runtime_jars().contains_exactly(["{package}/lib{name}.jar"])
+    base_info.compilation_args().transitive_compile_time_jars().contains_exactly(["{package}/lib{name}-hjar.jar"])
+    base_info.compilation_args().compile_jars().contains_exactly(["{package}/lib{name}-hjar.jar"])
+
+    base_jar = "{package}/lib{test_name}/base.jar"
+    base_hjar = "{package}/lib{test_name}/base-hjar.jar"
+
+    specialization_info = java_info_subject.from_target(env, targets.specialization)
+    specialization_info.compilation_args().transitive_runtime_jars().contains_exactly([
+        base_jar,
+        "{package}/lib{name}.jar",
+    ])
+    specialization_info.compilation_args().transitive_compile_time_jars().contains_exactly([
+        base_hjar,
+        "{package}/lib{name}-hjar.jar",
+    ])
+    specialization_info.compilation_args().compile_jars().contains_exactly(["{package}/lib{name}-hjar.jar"])
+
+def _test_java_library_files_to_compile(name):
+    util.helper_target(
+        java_library,
+        name = name + "/lib",
+        srcs = ["Lib.java"],
+    )
+
+    analysis_test(
+        name = name,
+        impl = _test_java_library_files_to_compile_impl,
+        target = name + "/lib",
+    )
+
+def _test_java_library_files_to_compile_impl(env, target):
+    env.expect.that_target(target).output_group("compilation_outputs").contains_exactly(["{package}/lib{name}.jar"])
+
+def _test_java_library_runtime_deps_are_not_on_classpath(name):
+    util.helper_target(
+        java_library,
+        name = name + "/runtime_java_dep",
+        srcs = ["test.java"],
+    )
+    util.helper_target(
+        java_library,
+        name = name + "/compile_dep",
+        srcs = ["compile.java"],
+    )
+    util.helper_target(
+        java_library,
+        name = name + "/depends_on_runtimedep",
+        srcs = ["dummy.java"],
+        runtime_deps = [name + "/runtime_java_dep"],
+        deps = [name + "/compile_dep"],
+    )
+
+    analysis_test(
+        name = name,
+        impl = _test_java_library_runtime_deps_are_not_on_classpath_impl,
+        target = name + "/depends_on_runtimedep",
+    )
+
+def _test_java_library_runtime_deps_are_not_on_classpath_impl(env, target):
+    expected_compile = "{bin_path}/{package}/lib{test_name}/compile_dep-hjar.jar"
+    javac_action_subject.of(env, target, "{package}/lib{name}.jar").classpath().contains_exactly([expected_compile])
+
+def _test_java_library_runtime_deps_are_not_on_classpath_with_header_compilation(name):
+    util.helper_target(
+        java_library,
+        name = name + "/runtime_java_dep",
+        srcs = ["test.java"],
+    )
+    util.helper_target(
+        java_library,
+        name = name + "/compile_dep",
+        srcs = ["compile.java"],
+    )
+    util.helper_target(
+        java_library,
+        name = name + "/depends_on_runtimedep",
+        srcs = ["dummy.java"],
+        runtime_deps = [name + "/runtime_java_dep"],
+        deps = [name + "/compile_dep"],
+    )
+
+    analysis_test(
+        name = name,
+        impl = _test_java_library_runtime_deps_are_not_on_classpath_with_header_compilation_impl,
+        config_settings = {
+            "//command_line_option:java_header_compilation": True,
+        },
+        target = name + "/depends_on_runtimedep",
+    )
+
+def _test_java_library_runtime_deps_are_not_on_classpath_with_header_compilation_impl(env, target):
+    expected_compile = "{bin_path}/{package}/lib{test_name}/compile_dep-hjar.jar"
+    javac_action_subject.of(env, target, "{package}/lib{name}.jar").classpath().contains_exactly([expected_compile])
+
+def _test_java_library_fix_deps_tool_written_to_params_file(name):
+    if not bazel_features.rules.analysis_tests_can_transition_on_experimental_incompatible_flags:
+        # Bazel 7 does not support transition on experimental_* flags.
+        # Exit early because this test case would be a loading phase error otherwise.
+        always_passes(name)
+        return
+    util.helper_target(
+        java_library,
+        name = name + "/base",
+        srcs = ["Base.java"],
+    )
+
+    analysis_test(
+        name = name,
+        impl = _test_java_library_fix_deps_tool_written_to_params_file_impl,
+        config_settings = {
+            "//command_line_option:experimental_fix_deps_tool": "fixer",
+        },
+        target = name + "/base",
+    )
+
+def _test_java_library_fix_deps_tool_written_to_params_file_impl(env, target):
+    javac_action_subject.of(env, target, "{package}/lib{name}.jar").argv().contains_at_least([
+        "--experimental_fix_deps_tool",
+        "fixer",
+    ]).in_order()
+
 JAVA_LIBRARY_LAUNCHER_TESTS = [
     _test_java_library_rule_outputs,
+    _test_java_library_action_graph,
+    _test_java_library_deps_of_genrule_are_not_on_classpath,
+    _test_java_library_compile_and_run_time_paths,
+    _test_java_library_files_to_compile,
+    _test_java_library_runtime_deps_are_not_on_classpath,
+    _test_java_library_runtime_deps_are_not_on_classpath_with_header_compilation,
+    _test_java_library_fix_deps_tool_written_to_params_file,
 ]
diff --git a/test/java/testutil/javac_action_subject.bzl b/test/java/testutil/javac_action_subject.bzl
index beb0ecb..057040b 100644
--- a/test/java/testutil/javac_action_subject.bzl
+++ b/test/java/testutil/javac_action_subject.bzl
@@ -25,6 +25,7 @@
         # An unset --strict_java_deps is equivalent to "OFF".
         strict_java_deps = lambda: _create_subject_for_flag("--strict_java_deps", self.parsed_flags, self.meta, default = ["OFF"]),
         sources = lambda: _create_subject_for_flag("--sources", self.parsed_flags, self.meta),
+        classpath = lambda: _create_subject_for_flag("--classpath", self.parsed_flags, self.meta),
         executable_file_name = lambda: subjects.str(action_subject.actual.argv[0], self.meta),
         inputs = action_subject.inputs,
         argv = action_subject.argv,