blob: f411ffb078da6aae2a27790cf95ab03634522b1b [file] [log] [blame]
Zi Wang65b36722023-05-23 15:18:33 -07001// Copyright 2023 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package bp2build
16
17import (
18 "testing"
19
20 "android/soong/android"
21 "android/soong/java"
22)
23
24func runJavaTestHostTestCase(t *testing.T, tc Bp2buildTestCase) {
25 t.Helper()
26 (&tc).ModuleTypeUnderTest = "java_test_host"
27 (&tc).ModuleTypeUnderTestFactory = java.TestHostFactory
28 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
29 ctx.RegisterModuleType("java_library", java.LibraryFactory)
30 }, tc)
31}
32
33func TestJavaTestHostGeneral(t *testing.T) {
34 runJavaTestHostTestCase(t, Bp2buildTestCase{
35 Description: "java_test_host general",
36 Filesystem: map[string]string{},
37 Blueprint: `
38java_test_host {
39 name: "java_test_host-1",
40 srcs: ["a.java", "b.java"],
41 libs: ["lib_a"],
42 static_libs: ["static_libs_a"],
43 exclude_srcs: ["b.java"],
44 javacflags: ["-Xdoclint:all/protected"],
45 java_version: "8",
46}
47
48java_library {
49 name: "lib_a",
50 bazel_module: { bp2build_available: false },
51}
52
53java_library {
54 name: "static_libs_a",
55 bazel_module: { bp2build_available: false },
56}
57`,
58 ExpectedBazelTargets: []string{
59 MakeBazelTarget("java_library", "java_test_host-1_lib", AttrNameToString{
60 "deps": `[
61 ":lib_a-neverlink",
62 ":static_libs_a",
63 ]`,
64 "java_version": `"8"`,
65 "javacopts": `["-Xdoclint:all/protected"]`,
66 "srcs": `["a.java"]`,
67 "target_compatible_with": `select({
68 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
69 "//conditions:default": [],
70 })`,
71 }),
72 MakeBazelTarget("java_test", "java_test_host-1", AttrNameToString{
73 "runtime_deps": `[":java_test_host-1_lib"]`,
74 "target_compatible_with": `select({
75 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
76 "//conditions:default": [],
77 })`,
78 }),
79 },
80 })
81}
82
83func TestJavaTestHostNoSrcs(t *testing.T) {
84 runJavaTestHostTestCase(t, Bp2buildTestCase{
85 Description: "java_test_host without srcs",
86 Filesystem: map[string]string{},
87 Blueprint: `
88java_test_host {
89 name: "java_test_host-1",
90 libs: ["lib_a"],
91 static_libs: ["static_libs_a"],
92}
93
94java_library {
95 name: "lib_a",
96 bazel_module: { bp2build_available: false },
97}
98
99java_library {
100 name: "static_libs_a",
101 bazel_module: { bp2build_available: false },
102}
103`,
104 ExpectedBazelTargets: []string{
105 MakeBazelTarget("java_test", "java_test_host-1", AttrNameToString{
106 "runtime_deps": `[
107 ":lib_a-neverlink",
108 ":static_libs_a",
109 ]`,
110 "target_compatible_with": `select({
111 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
112 "//conditions:default": [],
113 })`,
114 }),
115 },
116 })
117}
118
119func TestJavaTestHostKotlinSrcs(t *testing.T) {
120 runJavaTestHostTestCase(t, Bp2buildTestCase{
121 Description: "java_test_host with .kt in srcs",
122 Filesystem: map[string]string{},
123 Blueprint: `
124java_test_host {
125 name: "java_test_host-1",
126 srcs: ["a.java", "b.kt"],
127}
128`,
129 ExpectedBazelTargets: []string{
130 MakeBazelTarget("java_test", "java_test_host-1", AttrNameToString{
131 "runtime_deps": `[":java_test_host-1_lib"]`,
132 "target_compatible_with": `select({
133 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
134 "//conditions:default": [],
135 })`,
136 }),
137 MakeBazelTarget("kt_jvm_library", "java_test_host-1_lib", AttrNameToString{
138 "srcs": `[
139 "a.java",
140 "b.kt",
141 ]`,
142 "target_compatible_with": `select({
143 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
144 "//conditions:default": [],
145 })`,
146 }),
147 },
148 })
149}