blob: 87f35f6bcb285af1e9d286c754767f9271dffaea [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",
Zi Wang65b36722023-05-23 15:18:33 -0700100}
101
102java_library {
103 name: "static_libs_a",
Zi Wang65b36722023-05-23 15:18:33 -0700104}
105`,
Chris Parsons8a532b72023-09-27 23:11:26 +0000106 StubbedBuildDefinitions: []string{"lib_a", "static_libs_a"},
Zi Wang65b36722023-05-23 15:18:33 -0700107 ExpectedBazelTargets: []string{
108 MakeBazelTarget("java_test", "java_test_host-1", AttrNameToString{
109 "runtime_deps": `[
110 ":lib_a-neverlink",
111 ":static_libs_a",
112 ]`,
113 "target_compatible_with": `select({
114 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
115 "//conditions:default": [],
116 })`,
117 }),
118 },
119 })
120}
121
122func TestJavaTestHostKotlinSrcs(t *testing.T) {
123 runJavaTestHostTestCase(t, Bp2buildTestCase{
124 Description: "java_test_host with .kt in srcs",
125 Filesystem: map[string]string{},
126 Blueprint: `
127java_test_host {
128 name: "java_test_host-1",
129 srcs: ["a.java", "b.kt"],
130}
131`,
132 ExpectedBazelTargets: []string{
133 MakeBazelTarget("java_test", "java_test_host-1", AttrNameToString{
Zi Wang7873f612023-07-17 16:36:19 -0700134 "srcs": `[
135 "a.java",
136 "b.kt",
137 ]`,
Zi Wang65b36722023-05-23 15:18:33 -0700138 "runtime_deps": `[":java_test_host-1_lib"]`,
139 "target_compatible_with": `select({
140 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
141 "//conditions:default": [],
142 })`,
143 }),
144 MakeBazelTarget("kt_jvm_library", "java_test_host-1_lib", AttrNameToString{
145 "srcs": `[
146 "a.java",
147 "b.kt",
148 ]`,
149 "target_compatible_with": `select({
150 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
151 "//conditions:default": [],
152 })`,
153 }),
154 },
155 })
156}