Add a test for `java_common.compile(sourcepath = ...)`

PiperOrigin-RevId: 726076270
Change-Id: I1c58dde4a30e964e7570c3d6367fb069b8314e5f
diff --git a/java/test/common/BUILD b/java/test/common/BUILD
new file mode 100644
index 0000000..18ab4f8
--- /dev/null
+++ b/java/test/common/BUILD
@@ -0,0 +1,3 @@
+load(":java_common_tests.bzl", "java_common_tests")
+
+java_common_tests(name = "java_common_tests")
diff --git a/java/test/common/java_common_tests.bzl b/java/test/common/java_common_tests.bzl
new file mode 100644
index 0000000..4cc21ed
--- /dev/null
+++ b/java/test/common/java_common_tests.bzl
@@ -0,0 +1,34 @@
+"""Tests for java_common APIs"""
+
+load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
+load("@rules_testing//lib:util.bzl", "util")
+load("//java/test/testutil:rules/custom_library_with_sourcepaths.bzl", "custom_library_with_sourcepaths")
+
+def _test_compile_sourcepath(name):
+    util.helper_target(
+        custom_library_with_sourcepaths,
+        name = "custom",
+        srcs = ["Main.java"],
+        sourcepath = [":B.jar"],
+    )
+
+    analysis_test(
+        name = name,
+        impl = _test_compile_sourcepath_impl,
+        target = ":custom",
+    )
+
+def _test_compile_sourcepath_impl(env, target):
+    assert_compile_action = env.expect.that_target(target).action_generating("{package}/libcustom.jar")
+
+    assert_compile_action.contains_flag_values([
+        ("--sourcepath", "{package}/B.jar".format(package = target.label.package)),
+    ])
+
+def java_common_tests(name):
+    test_suite(
+        name = name,
+        tests = [
+            _test_compile_sourcepath,
+        ],
+    )
diff --git a/java/test/testutil/rules/custom_library_with_sourcepaths.bzl b/java/test/testutil/rules/custom_library_with_sourcepaths.bzl
new file mode 100644
index 0000000..05768c4
--- /dev/null
+++ b/java/test/testutil/rules/custom_library_with_sourcepaths.bzl
@@ -0,0 +1,25 @@
+"""Helper rule for testing compilation with `sourcepath`s"""
+
+load("//java/common:java_common.bzl", "java_common")
+load("//java/common:java_semantics.bzl", "semantics")
+
+def _custom_library_with_sourcepaths_impl(ctx):
+    output_jar = ctx.actions.declare_file("lib" + ctx.label.name + ".jar")
+    compilation_provider = java_common.compile(
+        ctx,
+        source_files = ctx.files.srcs,
+        output = output_jar,
+        sourcepath = ctx.files.sourcepath,
+        java_toolchain = semantics.find_java_toolchain(ctx),
+    )
+    return [DefaultInfo(files = depset([output_jar])), compilation_provider]
+
+custom_library_with_sourcepaths = rule(
+    _custom_library_with_sourcepaths_impl,
+    attrs = {
+        "srcs": attr.label_list(allow_files = [".java"]),
+        "sourcepath": attr.label_list(allow_files = [".jar"]),
+    },
+    toolchains = [semantics.JAVA_TOOLCHAIN_TYPE],
+    fragments = ["java"],
+)