Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 1 | // 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 | |
| 15 | package bp2build |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "android/soong/java" |
| 20 | |
| 21 | "testing" |
| 22 | ) |
| 23 | |
Zi Wang | 734266b | 2023-10-06 11:25:06 -0700 | [diff] [blame] | 24 | func 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 Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 31 | func runJavaImportTestCase(t *testing.T, tc Bp2buildTestCase) { |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 32 | t.Helper() |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 33 | RunBp2BuildTestCase(t, registerJavaImportModuleTypes, tc) |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | func registerJavaImportModuleTypes(ctx android.RegistrationContext) { |
| 37 | } |
| 38 | |
Sam Delmerico | 4898316 | 2022-02-22 21:41:33 +0000 | [diff] [blame] | 39 | func TestJavaImportMinimal(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 40 | runJavaImportTestCase(t, Bp2buildTestCase{ |
| 41 | Description: "Java import - simple example", |
| 42 | ModuleTypeUnderTest: "java_import", |
| 43 | ModuleTypeUnderTestFactory: java.ImportFactory, |
| 44 | Filesystem: map[string]string{ |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 45 | "import.jar": "", |
| 46 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 47 | Blueprint: ` |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 48 | java_import { |
| 49 | name: "example_import", |
| 50 | jars: ["import.jar"], |
| 51 | bazel_module: { bp2build_available: true }, |
| 52 | } |
| 53 | `, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 54 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 55 | MakeBazelTarget("java_import", "example_import", AttrNameToString{ |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 56 | "jars": `["import.jar"]`, |
| 57 | }), |
Alix | b4e09a0 | 2022-09-27 15:36:01 +0000 | [diff] [blame] | 58 | MakeBazelTarget("java_library", "example_import-neverlink", AttrNameToString{ |
Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 59 | "exports": `[":example_import"]`, |
| 60 | "neverlink": `True`, |
| 61 | "sdk_version": `"none"`, |
Alix | b4e09a0 | 2022-09-27 15:36:01 +0000 | [diff] [blame] | 62 | }), |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 63 | }}) |
| 64 | } |
Sam Delmerico | 4898316 | 2022-02-22 21:41:33 +0000 | [diff] [blame] | 65 | |
| 66 | func TestJavaImportArchVariant(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 67 | runJavaImportTestCase(t, Bp2buildTestCase{ |
| 68 | Description: "Java import - simple example", |
| 69 | ModuleTypeUnderTest: "java_import", |
| 70 | ModuleTypeUnderTestFactory: java.ImportFactory, |
| 71 | Filesystem: map[string]string{ |
Sam Delmerico | 4898316 | 2022-02-22 21:41:33 +0000 | [diff] [blame] | 72 | "import.jar": "", |
| 73 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 74 | Blueprint: ` |
Sam Delmerico | 4898316 | 2022-02-22 21:41:33 +0000 | [diff] [blame] | 75 | java_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 Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 88 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 89 | MakeBazelTarget("java_import", "example_import", AttrNameToString{ |
Sam Delmerico | 4898316 | 2022-02-22 21:41:33 +0000 | [diff] [blame] | 90 | "jars": `select({ |
Jingwen Chen | 9c2e3ee | 2023-10-11 10:51:28 +0000 | [diff] [blame] | 91 | "//build/bazel_common_rules/platforms/os:android": ["android.jar"], |
| 92 | "//build/bazel_common_rules/platforms/os:linux_glibc": ["linux.jar"], |
Sam Delmerico | 4898316 | 2022-02-22 21:41:33 +0000 | [diff] [blame] | 93 | "//conditions:default": [], |
| 94 | })`, |
| 95 | }), |
Alix | b4e09a0 | 2022-09-27 15:36:01 +0000 | [diff] [blame] | 96 | MakeBazelTarget("java_library", "example_import-neverlink", AttrNameToString{ |
Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 97 | "exports": `[":example_import"]`, |
| 98 | "neverlink": `True`, |
| 99 | "sdk_version": `"none"`, |
Alix | b4e09a0 | 2022-09-27 15:36:01 +0000 | [diff] [blame] | 100 | }), |
| 101 | }}) |
| 102 | } |
| 103 | |
| 104 | func 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: ` |
| 113 | java_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 Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 124 | "exports": `[":example_import"]`, |
| 125 | "neverlink": `True`, |
| 126 | "sdk_version": `"none"`, |
Alix | b4e09a0 | 2022-09-27 15:36:01 +0000 | [diff] [blame] | 127 | }), |
Sam Delmerico | 4898316 | 2022-02-22 21:41:33 +0000 | [diff] [blame] | 128 | }}) |
| 129 | } |
Zi Wang | 734266b | 2023-10-06 11:25:06 -0700 | [diff] [blame] | 130 | |
| 131 | func 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 | } |