blob: bba2f5077bda3f9cfb9b98ae154f78ed8815dc28 [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{
Alixe06d75b2022-08-31 18:28:19 +000055 MakeBazelTarget("java_import", "example_import", AttrNameToString{
Romain Jobredeaux428a3662022-01-28 11:12:52 -050056 "jars": `["import.jar"]`,
57 }),
Alixb4e09a02022-09-27 15:36:01 +000058 MakeBazelTarget("java_library", "example_import-neverlink", AttrNameToString{
Romain Jobredeaux2eef2e12023-02-24 12:07:08 -050059 "exports": `[":example_import"]`,
60 "neverlink": `True`,
61 "sdk_version": `"none"`,
Alixb4e09a02022-09-27 15:36:01 +000062 }),
Romain Jobredeaux428a3662022-01-28 11:12:52 -050063 }})
64}
Sam Delmerico48983162022-02-22 21:41:33 +000065
66func TestJavaImportArchVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000067 runJavaImportTestCase(t, Bp2buildTestCase{
68 Description: "Java import - simple example",
69 ModuleTypeUnderTest: "java_import",
70 ModuleTypeUnderTestFactory: java.ImportFactory,
71 Filesystem: map[string]string{
Sam Delmerico48983162022-02-22 21:41:33 +000072 "import.jar": "",
73 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +000074 Blueprint: `
Sam Delmerico48983162022-02-22 21:41:33 +000075java_import {
76 name: "example_import",
77 target: {
78 android: {
79 jars: ["android.jar"],
80 },
81 linux_glibc: {
82 jars: ["linux.jar"],
83 },
84 },
85 bazel_module: { bp2build_available: true },
86}
87`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000088 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000089 MakeBazelTarget("java_import", "example_import", AttrNameToString{
Sam Delmerico48983162022-02-22 21:41:33 +000090 "jars": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +000091 "//build/bazel_common_rules/platforms/os:android": ["android.jar"],
92 "//build/bazel_common_rules/platforms/os:linux_glibc": ["linux.jar"],
Sam Delmerico48983162022-02-22 21:41:33 +000093 "//conditions:default": [],
94 })`,
95 }),
Alixb4e09a02022-09-27 15:36:01 +000096 MakeBazelTarget("java_library", "example_import-neverlink", AttrNameToString{
Romain Jobredeaux2eef2e12023-02-24 12:07:08 -050097 "exports": `[":example_import"]`,
98 "neverlink": `True`,
99 "sdk_version": `"none"`,
Alixb4e09a02022-09-27 15:36:01 +0000100 }),
101 }})
102}
103
104func TestJavaImportHost(t *testing.T) {
105 runJavaImportTestCase(t, Bp2buildTestCase{
106 Description: "Java import host- simple example",
107 ModuleTypeUnderTest: "java_import_host",
108 ModuleTypeUnderTestFactory: java.ImportFactory,
109 Filesystem: map[string]string{
110 "import.jar": "",
111 },
112 Blueprint: `
113java_import_host {
114 name: "example_import",
115 jars: ["import.jar"],
116 bazel_module: { bp2build_available: true },
117}
118`,
119 ExpectedBazelTargets: []string{
120 MakeBazelTarget("java_import", "example_import", AttrNameToString{
121 "jars": `["import.jar"]`,
122 }),
123 MakeBazelTarget("java_library", "example_import-neverlink", AttrNameToString{
Romain Jobredeaux2eef2e12023-02-24 12:07:08 -0500124 "exports": `[":example_import"]`,
125 "neverlink": `True`,
126 "sdk_version": `"none"`,
Alixb4e09a02022-09-27 15:36:01 +0000127 }),
Sam Delmerico48983162022-02-22 21:41:33 +0000128 }})
129}
Zi Wang734266b2023-10-06 11:25:06 -0700130
131func TestJavaImportSameNameAsJavaLibrary(t *testing.T) {
132 runJavaImportTestCaseWithRegistrationCtxFunc(t, Bp2buildTestCase{
133 Description: "java_import has the same name as other package java_library's",
134 Filesystem: map[string]string{
135 "foo/bar/Android.bp": simpleModule("java_library", "test_lib"),
136 "test.jar": "",
137 },
138 Blueprint: `java_import {
139 name: "test_lib",
140 jars: ["test.jar"],
141 bazel_module: { bp2build_available: true },
142}
143`,
144 ExpectedBazelTargets: []string{
145 MakeBazelTarget("java_import", "test_lib", AttrNameToString{
146 "jars": `["test.jar"]`,
147 }),
148 MakeBazelTarget("java_library", "test_lib-neverlink", AttrNameToString{
149 "exports": `[":test_lib"]`,
150 "neverlink": `True`,
151 "sdk_version": `"none"`,
152 }),
153 },
154 }, func(ctx android.RegistrationContext) {
155 ctx.RegisterModuleType("java_library", java.LibraryFactory)
156 })
157}