blob: 2f6bce20f2fed9bb63fd76464c02186731596d79 [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) {
33 runJavaLibraryTestCaseWithRegistrationCtxFunc(t, tc, func(ctx android.RegistrationContext) {})
Wei Libafb6d62021-12-10 03:14:59 -080034}
35
36func TestJavaLibrary(t *testing.T) {
37 runJavaLibraryTestCase(t, bp2buildTestCase{
38 description: "java_library with srcs, exclude_srcs and libs",
39 blueprint: `java_library {
40 name: "java-lib-1",
41 srcs: ["a.java", "b.java"],
42 exclude_srcs: ["b.java"],
43 libs: ["java-lib-2"],
44 bazel_module: { bp2build_available: true },
45}
46
47java_library {
48 name: "java-lib-2",
49 srcs: ["b.java"],
50 bazel_module: { bp2build_available: true },
51}`,
52 expectedBazelTargets: []string{
53 makeBazelTarget("java_library", "java-lib-1", attrNameToString{
54 "srcs": `["a.java"]`,
55 "deps": `[":java-lib-2"]`,
56 }),
57 makeBazelTarget("java_library", "java-lib-2", attrNameToString{
58 "srcs": `["b.java"]`,
59 }),
60 },
61 })
62}
Sam Delmericoc0161432022-02-25 21:34:51 +000063
64func TestJavaLibraryConvertsStaticLibsToDepsAndExports(t *testing.T) {
65 runJavaLibraryTestCase(t, bp2buildTestCase{
66 blueprint: `java_library {
67 name: "java-lib-1",
68 srcs: ["a.java"],
69 libs: ["java-lib-2"],
70 static_libs: ["java-lib-3"],
71 bazel_module: { bp2build_available: true },
72}
73
74java_library {
75 name: "java-lib-2",
76 srcs: ["b.java"],
77 bazel_module: { bp2build_available: false },
78}
79
80java_library {
81 name: "java-lib-3",
82 srcs: ["c.java"],
83 bazel_module: { bp2build_available: false },
84}`,
85 expectedBazelTargets: []string{
86 makeBazelTarget("java_library", "java-lib-1", attrNameToString{
87 "srcs": `["a.java"]`,
88 "deps": `[
89 ":java-lib-2",
90 ":java-lib-3",
91 ]`,
92 "exports": `[":java-lib-3"]`,
93 }),
94 },
95 })
96}
97
98func TestJavaLibraryConvertsStaticLibsToExportsIfNoSrcs(t *testing.T) {
99 runJavaLibraryTestCase(t, bp2buildTestCase{
100 blueprint: `java_library {
101 name: "java-lib-1",
102 static_libs: ["java-lib-2"],
103 bazel_module: { bp2build_available: true },
104}
105
106java_library {
107 name: "java-lib-2",
108 srcs: ["a.java"],
109 bazel_module: { bp2build_available: false },
110}`,
111 expectedBazelTargets: []string{
112 makeBazelTarget("java_library", "java-lib-1", attrNameToString{
113 "exports": `[":java-lib-2"]`,
114 }),
115 },
116 })
117}
118
119func TestJavaLibraryFailsToConvertLibsWithNoSrcs(t *testing.T) {
120 runJavaLibraryTestCase(t, bp2buildTestCase{
121 expectedErr: fmt.Errorf("Module has direct dependencies but no sources. Bazel will not allow this."),
122 blueprint: `java_library {
123 name: "java-lib-1",
124 libs: ["java-lib-2"],
125 bazel_module: { bp2build_available: true },
126}
127
128java_library {
129 name: "java-lib-2",
130 srcs: ["a.java"],
131 bazel_module: { bp2build_available: false },
132}`,
133 expectedBazelTargets: []string{},
134 })
135}
Sam Delmerico77267c72022-03-18 14:11:07 +0000136
137func TestJavaLibraryPlugins(t *testing.T) {
138 runJavaLibraryTestCaseWithRegistrationCtxFunc(t, bp2buildTestCase{
139 blueprint: `java_library {
140 name: "java-lib-1",
141 plugins: ["java-plugin-1"],
142 bazel_module: { bp2build_available: true },
143}
144
145java_plugin {
146 name: "java-plugin-1",
147 srcs: ["a.java"],
148 bazel_module: { bp2build_available: false },
149}`,
150 expectedBazelTargets: []string{
151 makeBazelTarget("java_library", "java-lib-1", attrNameToString{
152 "plugins": `[":java-plugin-1"]`,
153 }),
154 },
155 }, func(ctx android.RegistrationContext) {
156 ctx.RegisterModuleType("java_plugin", java.PluginFactory)
157 })
158}