blob: 95c239dbf97a443eebb9c2b59bbedf49ec194a43 [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{
Chris Parsons4c81ce02023-09-21 15:30:27 +000035 Description: "java_test_host general",
36 Filesystem: map[string]string{},
37 StubbedBuildDefinitions: []string{"lib_a", "lib_b"},
Zi Wang65b36722023-05-23 15:18:33 -070038 Blueprint: `
39java_test_host {
40 name: "java_test_host-1",
41 srcs: ["a.java", "b.java"],
42 libs: ["lib_a"],
43 static_libs: ["static_libs_a"],
44 exclude_srcs: ["b.java"],
45 javacflags: ["-Xdoclint:all/protected"],
46 java_version: "8",
47}
48
49java_library {
50 name: "lib_a",
Zi Wang65b36722023-05-23 15:18:33 -070051}
52
53java_library {
54 name: "static_libs_a",
Zi Wang65b36722023-05-23 15:18:33 -070055}
56`,
57 ExpectedBazelTargets: []string{
58 MakeBazelTarget("java_library", "java_test_host-1_lib", AttrNameToString{
59 "deps": `[
60 ":lib_a-neverlink",
61 ":static_libs_a",
62 ]`,
63 "java_version": `"8"`,
64 "javacopts": `["-Xdoclint:all/protected"]`,
65 "srcs": `["a.java"]`,
66 "target_compatible_with": `select({
67 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
68 "//conditions:default": [],
69 })`,
70 }),
71 MakeBazelTarget("java_test", "java_test_host-1", AttrNameToString{
72 "runtime_deps": `[":java_test_host-1_lib"]`,
Zi Wang7873f612023-07-17 16:36:19 -070073 "deps": `[
74 ":lib_a-neverlink",
75 ":static_libs_a",
76 ]`,
77 "srcs": `["a.java"]`,
Zi Wang65b36722023-05-23 15:18:33 -070078 "target_compatible_with": `select({
79 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
80 "//conditions:default": [],
81 })`,
82 }),
83 },
84 })
85}
86
87func TestJavaTestHostNoSrcs(t *testing.T) {
88 runJavaTestHostTestCase(t, Bp2buildTestCase{
89 Description: "java_test_host without srcs",
90 Filesystem: map[string]string{},
91 Blueprint: `
92java_test_host {
93 name: "java_test_host-1",
94 libs: ["lib_a"],
95 static_libs: ["static_libs_a"],
96}
97
98java_library {
99 name: "lib_a",
100 bazel_module: { bp2build_available: false },
101}
102
103java_library {
104 name: "static_libs_a",
105 bazel_module: { bp2build_available: false },
106}
107`,
108 ExpectedBazelTargets: []string{
109 MakeBazelTarget("java_test", "java_test_host-1", AttrNameToString{
110 "runtime_deps": `[
111 ":lib_a-neverlink",
112 ":static_libs_a",
113 ]`,
114 "target_compatible_with": `select({
115 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
116 "//conditions:default": [],
117 })`,
118 }),
119 },
120 })
121}
122
123func TestJavaTestHostKotlinSrcs(t *testing.T) {
124 runJavaTestHostTestCase(t, Bp2buildTestCase{
125 Description: "java_test_host with .kt in srcs",
126 Filesystem: map[string]string{},
127 Blueprint: `
128java_test_host {
129 name: "java_test_host-1",
130 srcs: ["a.java", "b.kt"],
131}
132`,
133 ExpectedBazelTargets: []string{
134 MakeBazelTarget("java_test", "java_test_host-1", AttrNameToString{
Zi Wang7873f612023-07-17 16:36:19 -0700135 "srcs": `[
136 "a.java",
137 "b.kt",
138 ]`,
Zi Wang65b36722023-05-23 15:18:33 -0700139 "runtime_deps": `[":java_test_host-1_lib"]`,
140 "target_compatible_with": `select({
141 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
142 "//conditions:default": [],
143 })`,
144 }),
145 MakeBazelTarget("kt_jvm_library", "java_test_host-1_lib", AttrNameToString{
146 "srcs": `[
147 "a.java",
148 "b.kt",
149 ]`,
150 "target_compatible_with": `select({
151 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
152 "//conditions:default": [],
153 })`,
154 }),
155 },
156 })
157}