blob: ad69ed5ad5e33b40c61417e86f2dfeafb20b2972 [file] [log] [blame]
Romain Jobredeaux428a3662022-01-28 11:12:52 -05001// Copyright 2022 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 "android/soong/android"
19 "android/soong/java"
20
21 "testing"
22)
23
Zi Wang734266b2023-10-06 11:25:06 -070024func runJavaImportTestCaseWithRegistrationCtxFunc(t *testing.T, tc Bp2buildTestCase, registrationCtxFunc func(ctx android.RegistrationContext)) {
25 t.Helper()
26 (&tc).ModuleTypeUnderTest = "java_import"
27 (&tc).ModuleTypeUnderTestFactory = java.ImportFactory
28 RunBp2BuildTestCase(t, registrationCtxFunc, tc)
29}
30
Sam Delmerico3177a6e2022-06-21 19:28:33 +000031func runJavaImportTestCase(t *testing.T, tc Bp2buildTestCase) {
Romain Jobredeaux428a3662022-01-28 11:12:52 -050032 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000033 RunBp2BuildTestCase(t, registerJavaImportModuleTypes, tc)
Romain Jobredeaux428a3662022-01-28 11:12:52 -050034}
35
36func registerJavaImportModuleTypes(ctx android.RegistrationContext) {
37}
38
Sam Delmerico48983162022-02-22 21:41:33 +000039func TestJavaImportMinimal(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000040 runJavaImportTestCase(t, Bp2buildTestCase{
41 Description: "Java import - simple example",
42 ModuleTypeUnderTest: "java_import",
43 ModuleTypeUnderTestFactory: java.ImportFactory,
44 Filesystem: map[string]string{
Romain Jobredeaux428a3662022-01-28 11:12:52 -050045 "import.jar": "",
46 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +000047 Blueprint: `
Romain Jobredeaux428a3662022-01-28 11:12:52 -050048java_import {
49 name: "example_import",
50 jars: ["import.jar"],
51 bazel_module: { bp2build_available: true },
52}
53`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000054 ExpectedBazelTargets: []string{
Liz Kammer4e00b092023-10-11 15:24:24 -040055 MakeBazelTarget("filegroup", "example_import-jars", AttrNameToString{
56 "srcs": `["import.jar"]`,
Romain Jobredeaux428a3662022-01-28 11:12:52 -050057 }),
Liz Kammer4e00b092023-10-11 15:24:24 -040058 MakeBazelTarget("java_import", "example_import", AttrNameToString{
59 "jars": `[":example_import-jars"]`,
60 }),
61 MakeBazelTarget("java_import", "example_import-neverlink", AttrNameToString{
62 "jars": `[":example_import-jars"]`,
63 "neverlink": `True`,
Alixb4e09a02022-09-27 15:36:01 +000064 }),
Romain Jobredeaux428a3662022-01-28 11:12:52 -050065 }})
66}
Sam Delmerico48983162022-02-22 21:41:33 +000067
68func TestJavaImportArchVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000069 runJavaImportTestCase(t, Bp2buildTestCase{
70 Description: "Java import - simple example",
71 ModuleTypeUnderTest: "java_import",
72 ModuleTypeUnderTestFactory: java.ImportFactory,
73 Filesystem: map[string]string{
Sam Delmerico48983162022-02-22 21:41:33 +000074 "import.jar": "",
75 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +000076 Blueprint: `
Sam Delmerico48983162022-02-22 21:41:33 +000077java_import {
78 name: "example_import",
79 target: {
80 android: {
81 jars: ["android.jar"],
82 },
83 linux_glibc: {
84 jars: ["linux.jar"],
85 },
86 },
87 bazel_module: { bp2build_available: true },
88}
89`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000090 ExpectedBazelTargets: []string{
Liz Kammer4e00b092023-10-11 15:24:24 -040091 MakeBazelTarget("filegroup", "example_import-jars", AttrNameToString{
92 "srcs": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +000093 "//build/bazel_common_rules/platforms/os:android": ["android.jar"],
94 "//build/bazel_common_rules/platforms/os:linux_glibc": ["linux.jar"],
Sam Delmerico48983162022-02-22 21:41:33 +000095 "//conditions:default": [],
96 })`,
97 }),
Liz Kammer4e00b092023-10-11 15:24:24 -040098 MakeBazelTarget("java_import", "example_import", AttrNameToString{
99 "jars": `[":example_import-jars"]`,
100 }),
101 MakeBazelTarget("java_import", "example_import-neverlink", AttrNameToString{
102 "jars": `[":example_import-jars"]`,
103 "neverlink": `True`,
Alixb4e09a02022-09-27 15:36:01 +0000104 }),
105 }})
106}
107
108func TestJavaImportHost(t *testing.T) {
109 runJavaImportTestCase(t, Bp2buildTestCase{
110 Description: "Java import host- simple example",
111 ModuleTypeUnderTest: "java_import_host",
112 ModuleTypeUnderTestFactory: java.ImportFactory,
113 Filesystem: map[string]string{
114 "import.jar": "",
115 },
116 Blueprint: `
117java_import_host {
118 name: "example_import",
119 jars: ["import.jar"],
120 bazel_module: { bp2build_available: true },
121}
122`,
123 ExpectedBazelTargets: []string{
Liz Kammer4e00b092023-10-11 15:24:24 -0400124 MakeBazelTarget("filegroup", "example_import-jars", AttrNameToString{
125 "srcs": `["import.jar"]`,
Alixb4e09a02022-09-27 15:36:01 +0000126 }),
Liz Kammer4e00b092023-10-11 15:24:24 -0400127 MakeBazelTarget("java_import", "example_import", AttrNameToString{
128 "jars": `[":example_import-jars"]`,
129 }),
130 MakeBazelTarget("java_import", "example_import-neverlink", AttrNameToString{
131 "jars": `[":example_import-jars"]`,
132 "neverlink": `True`,
Alixb4e09a02022-09-27 15:36:01 +0000133 }),
Sam Delmerico48983162022-02-22 21:41:33 +0000134 }})
135}
Zi Wang734266b2023-10-06 11:25:06 -0700136
137func TestJavaImportSameNameAsJavaLibrary(t *testing.T) {
138 runJavaImportTestCaseWithRegistrationCtxFunc(t, Bp2buildTestCase{
139 Description: "java_import has the same name as other package java_library's",
140 Filesystem: map[string]string{
141 "foo/bar/Android.bp": simpleModule("java_library", "test_lib"),
142 "test.jar": "",
143 },
144 Blueprint: `java_import {
145 name: "test_lib",
146 jars: ["test.jar"],
147 bazel_module: { bp2build_available: true },
148}
149`,
150 ExpectedBazelTargets: []string{
Liz Kammer4e00b092023-10-11 15:24:24 -0400151 MakeBazelTarget("filegroup", "test_lib-jars", AttrNameToString{
152 "srcs": `["test.jar"]`,
Zi Wang734266b2023-10-06 11:25:06 -0700153 }),
Liz Kammer4e00b092023-10-11 15:24:24 -0400154 MakeBazelTarget("java_import", "test_lib", AttrNameToString{
155 "jars": `[":test_lib-jars"]`,
156 }),
157 MakeBazelTarget("java_import", "test_lib-neverlink", AttrNameToString{
158 "jars": `[":test_lib-jars"]`,
159 "neverlink": `True`,
Zi Wang734266b2023-10-06 11:25:06 -0700160 }),
161 },
162 }, func(ctx android.RegistrationContext) {
163 ctx.RegisterModuleType("java_library", java.LibraryFactory)
164 })
165}