blob: 3b66369759d7d8925ea48214690ad1001abfe28c [file] [log] [blame]
Wei Libafb6d62021-12-10 03:14:59 -08001// Copyright 2021 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 (
Sam Delmericoc0161432022-02-25 21:34:51 +000018 "fmt"
Wei Libafb6d62021-12-10 03:14:59 -080019 "testing"
20
21 "android/soong/android"
22 "android/soong/java"
23)
24
Sam Delmerico77267c72022-03-18 14:11:07 +000025func runJavaLibraryTestCaseWithRegistrationCtxFunc(t *testing.T, tc bp2buildTestCase, registrationCtxFunc func(ctx android.RegistrationContext)) {
Wei Libafb6d62021-12-10 03:14:59 -080026 t.Helper()
27 (&tc).moduleTypeUnderTest = "java_library"
28 (&tc).moduleTypeUnderTestFactory = java.LibraryFactory
Sam Delmerico77267c72022-03-18 14:11:07 +000029 runBp2BuildTestCase(t, registrationCtxFunc, tc)
30}
31
32func runJavaLibraryTestCase(t *testing.T, tc bp2buildTestCase) {
Sam Delmerico58614c02022-03-15 21:02:09 +000033 t.Helper()
Sam Delmerico77267c72022-03-18 14:11:07 +000034 runJavaLibraryTestCaseWithRegistrationCtxFunc(t, tc, func(ctx android.RegistrationContext) {})
Wei Libafb6d62021-12-10 03:14:59 -080035}
36
37func TestJavaLibrary(t *testing.T) {
38 runJavaLibraryTestCase(t, bp2buildTestCase{
39 description: "java_library with srcs, exclude_srcs and libs",
40 blueprint: `java_library {
41 name: "java-lib-1",
42 srcs: ["a.java", "b.java"],
43 exclude_srcs: ["b.java"],
44 libs: ["java-lib-2"],
45 bazel_module: { bp2build_available: true },
46}
47
48java_library {
49 name: "java-lib-2",
50 srcs: ["b.java"],
51 bazel_module: { bp2build_available: true },
52}`,
53 expectedBazelTargets: []string{
54 makeBazelTarget("java_library", "java-lib-1", attrNameToString{
55 "srcs": `["a.java"]`,
56 "deps": `[":java-lib-2"]`,
57 }),
58 makeBazelTarget("java_library", "java-lib-2", attrNameToString{
59 "srcs": `["b.java"]`,
60 }),
61 },
62 })
63}
Sam Delmericoc0161432022-02-25 21:34:51 +000064
65func TestJavaLibraryConvertsStaticLibsToDepsAndExports(t *testing.T) {
66 runJavaLibraryTestCase(t, bp2buildTestCase{
67 blueprint: `java_library {
68 name: "java-lib-1",
69 srcs: ["a.java"],
70 libs: ["java-lib-2"],
71 static_libs: ["java-lib-3"],
72 bazel_module: { bp2build_available: true },
73}
74
75java_library {
76 name: "java-lib-2",
77 srcs: ["b.java"],
78 bazel_module: { bp2build_available: false },
79}
80
81java_library {
82 name: "java-lib-3",
83 srcs: ["c.java"],
84 bazel_module: { bp2build_available: false },
85}`,
86 expectedBazelTargets: []string{
87 makeBazelTarget("java_library", "java-lib-1", attrNameToString{
88 "srcs": `["a.java"]`,
89 "deps": `[
90 ":java-lib-2",
91 ":java-lib-3",
92 ]`,
93 "exports": `[":java-lib-3"]`,
94 }),
95 },
96 })
97}
98
99func TestJavaLibraryConvertsStaticLibsToExportsIfNoSrcs(t *testing.T) {
100 runJavaLibraryTestCase(t, bp2buildTestCase{
101 blueprint: `java_library {
102 name: "java-lib-1",
103 static_libs: ["java-lib-2"],
104 bazel_module: { bp2build_available: true },
105}
106
107java_library {
108 name: "java-lib-2",
109 srcs: ["a.java"],
110 bazel_module: { bp2build_available: false },
111}`,
112 expectedBazelTargets: []string{
113 makeBazelTarget("java_library", "java-lib-1", attrNameToString{
114 "exports": `[":java-lib-2"]`,
115 }),
116 },
117 })
118}
119
120func TestJavaLibraryFailsToConvertLibsWithNoSrcs(t *testing.T) {
121 runJavaLibraryTestCase(t, bp2buildTestCase{
122 expectedErr: fmt.Errorf("Module has direct dependencies but no sources. Bazel will not allow this."),
123 blueprint: `java_library {
124 name: "java-lib-1",
125 libs: ["java-lib-2"],
126 bazel_module: { bp2build_available: true },
127}
128
129java_library {
130 name: "java-lib-2",
131 srcs: ["a.java"],
132 bazel_module: { bp2build_available: false },
133}`,
134 expectedBazelTargets: []string{},
135 })
136}
Sam Delmerico77267c72022-03-18 14:11:07 +0000137
138func TestJavaLibraryPlugins(t *testing.T) {
139 runJavaLibraryTestCaseWithRegistrationCtxFunc(t, bp2buildTestCase{
140 blueprint: `java_library {
141 name: "java-lib-1",
142 plugins: ["java-plugin-1"],
143 bazel_module: { bp2build_available: true },
144}
145
146java_plugin {
147 name: "java-plugin-1",
148 srcs: ["a.java"],
149 bazel_module: { bp2build_available: false },
150}`,
151 expectedBazelTargets: []string{
152 makeBazelTarget("java_library", "java-lib-1", attrNameToString{
153 "plugins": `[":java-plugin-1"]`,
154 }),
155 },
156 }, func(ctx android.RegistrationContext) {
157 ctx.RegisterModuleType("java_plugin", java.PluginFactory)
158 })
159}
Sam Delmerico58614c02022-03-15 21:02:09 +0000160
Vinh Tran3ac6daf2022-04-22 19:09:58 -0400161func TestJavaLibraryJavaVersion(t *testing.T) {
162 runJavaLibraryTestCase(t, bp2buildTestCase{
163 blueprint: `java_library {
164 name: "java-lib-1",
165 srcs: ["a.java"],
166 java_version: "11",
167}`,
168 expectedBazelTargets: []string{
169 makeBazelTarget("java_library", "java-lib-1", attrNameToString{
170 "srcs": `["a.java"]`,
171 "javacopts": `["-source 11 -target 11"]`,
172 }),
173 },
174 })
175}
176
Sam Delmerico58614c02022-03-15 21:02:09 +0000177func TestJavaLibraryErrorproneJavacflagsEnabledManually(t *testing.T) {
178 runJavaLibraryTestCase(t, bp2buildTestCase{
179 blueprint: `java_library {
180 name: "java-lib-1",
181 srcs: ["a.java"],
182 javacflags: ["-Xsuper-fast"],
183 errorprone: {
184 enabled: true,
185 javacflags: ["-Xep:SpeedLimit:OFF"],
186 },
187}`,
188 expectedBazelTargets: []string{
189 makeBazelTarget("java_library", "java-lib-1", attrNameToString{
190 "javacopts": `[
191 "-Xsuper-fast",
192 "-Xep:SpeedLimit:OFF",
193 ]`,
194 "srcs": `["a.java"]`,
195 }),
196 },
197 })
198}
199
200func TestJavaLibraryErrorproneJavacflagsErrorproneDisabledByDefault(t *testing.T) {
201 runJavaLibraryTestCase(t, bp2buildTestCase{
202 blueprint: `java_library {
203 name: "java-lib-1",
204 srcs: ["a.java"],
205 javacflags: ["-Xsuper-fast"],
206 errorprone: {
207 javacflags: ["-Xep:SpeedLimit:OFF"],
208 },
209}`,
210 expectedBazelTargets: []string{
211 makeBazelTarget("java_library", "java-lib-1", attrNameToString{
212 "javacopts": `["-Xsuper-fast"]`,
213 "srcs": `["a.java"]`,
214 }),
215 },
216 })
217}
218
219func TestJavaLibraryErrorproneJavacflagsErrorproneDisabledManually(t *testing.T) {
220 runJavaLibraryTestCase(t, bp2buildTestCase{
221 blueprint: `java_library {
222 name: "java-lib-1",
223 srcs: ["a.java"],
224 javacflags: ["-Xsuper-fast"],
225 errorprone: {
226 enabled: false,
227 javacflags: ["-Xep:SpeedLimit:OFF"],
228 },
229}`,
230 expectedBazelTargets: []string{
231 makeBazelTarget("java_library", "java-lib-1", attrNameToString{
232 "javacopts": `["-Xsuper-fast"]`,
233 "srcs": `["a.java"]`,
234 }),
235 },
236 })
237}
Sam Delmerico24da73c2022-03-16 20:36:54 +0000238
239func TestJavaLibraryLogTags(t *testing.T) {
240 runJavaLibraryTestCase(t, bp2buildTestCase{
241 description: "Java library - logtags creates separate dependency",
242 moduleTypeUnderTest: "java_library",
243 moduleTypeUnderTestFactory: java.LibraryFactory,
244 blueprint: `java_library {
245 name: "example_lib",
246 srcs: [
247 "a.java",
248 "b.java",
249 "a.logtag",
250 "b.logtag",
251 ],
252 bazel_module: { bp2build_available: true },
253}`,
254 expectedBazelTargets: []string{
255 makeBazelTarget("event_log_tags", "example_lib_logtags", attrNameToString{
256 "srcs": `[
257 "a.logtag",
258 "b.logtag",
259 ]`,
260 }),
261 makeBazelTarget("java_library", "example_lib", attrNameToString{
262 "srcs": `[
263 "a.java",
264 "b.java",
265 ":example_lib_logtags",
266 ]`,
267 }),
268 }})
269}