blob: f41345e87b208096a5ed6850b7bd39a98bcdcf1b [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"]`,
Zi Wang7873f612023-07-17 16:36:19 -070074 "deps": `[
75 ":lib_a-neverlink",
76 ":static_libs_a",
77 ]`,
78 "srcs": `["a.java"]`,
Zi Wang65b36722023-05-23 15:18:33 -070079 "target_compatible_with": `select({
80 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
81 "//conditions:default": [],
82 })`,
83 }),
84 },
85 })
86}
87
88func TestJavaTestHostNoSrcs(t *testing.T) {
89 runJavaTestHostTestCase(t, Bp2buildTestCase{
90 Description: "java_test_host without srcs",
91 Filesystem: map[string]string{},
92 Blueprint: `
93java_test_host {
94 name: "java_test_host-1",
95 libs: ["lib_a"],
96 static_libs: ["static_libs_a"],
97}
98
99java_library {
100 name: "lib_a",
101 bazel_module: { bp2build_available: false },
102}
103
104java_library {
105 name: "static_libs_a",
106 bazel_module: { bp2build_available: false },
107}
108`,
109 ExpectedBazelTargets: []string{
110 MakeBazelTarget("java_test", "java_test_host-1", AttrNameToString{
111 "runtime_deps": `[
112 ":lib_a-neverlink",
113 ":static_libs_a",
114 ]`,
115 "target_compatible_with": `select({
116 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
117 "//conditions:default": [],
118 })`,
119 }),
120 },
121 })
122}
123
124func TestJavaTestHostKotlinSrcs(t *testing.T) {
125 runJavaTestHostTestCase(t, Bp2buildTestCase{
126 Description: "java_test_host with .kt in srcs",
127 Filesystem: map[string]string{},
128 Blueprint: `
129java_test_host {
130 name: "java_test_host-1",
131 srcs: ["a.java", "b.kt"],
132}
133`,
134 ExpectedBazelTargets: []string{
135 MakeBazelTarget("java_test", "java_test_host-1", AttrNameToString{
Zi Wang7873f612023-07-17 16:36:19 -0700136 "srcs": `[
137 "a.java",
138 "b.kt",
139 ]`,
Zi Wang65b36722023-05-23 15:18:33 -0700140 "runtime_deps": `[":java_test_host-1_lib"]`,
141 "target_compatible_with": `select({
142 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
143 "//conditions:default": [],
144 })`,
145 }),
146 MakeBazelTarget("kt_jvm_library", "java_test_host-1_lib", AttrNameToString{
147 "srcs": `[
148 "a.java",
149 "b.kt",
150 ]`,
151 "target_compatible_with": `select({
152 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
153 "//conditions:default": [],
154 })`,
155 }),
156 },
157 })
158}