blob: e63cc55ab196104528999085cf535e60a01091a2 [file] [log] [blame]
Sam Delmericoc06ea032022-01-28 21:11:03 +00001// 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 (
18 "testing"
19
20 "android/soong/android"
21 "android/soong/java"
22)
23
Sam Delmerico3177a6e2022-06-21 19:28:33 +000024func runJavaPluginTestCase(t *testing.T, tc Bp2buildTestCase) {
Sam Delmericoc06ea032022-01-28 21:11:03 +000025 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000026 (&tc).ModuleTypeUnderTest = "java_plugin"
27 (&tc).ModuleTypeUnderTestFactory = java.PluginFactory
28 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
Sam Delmericoc06ea032022-01-28 21:11:03 +000029 ctx.RegisterModuleType("java_library", java.LibraryFactory)
30 }, tc)
31}
32
33func TestJavaPlugin(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000034 runJavaPluginTestCase(t, Bp2buildTestCase{
Chris Parsonscd209032023-09-19 01:12:48 +000035 Description: "java_plugin with srcs, libs, static_libs",
36 StubbedBuildDefinitions: []string{"java-lib-1", "java-lib-2"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +000037 Blueprint: `java_plugin {
Sam Delmericoc06ea032022-01-28 21:11:03 +000038 name: "java-plug-1",
39 srcs: ["a.java", "b.java"],
40 libs: ["java-lib-1"],
41 static_libs: ["java-lib-2"],
42 bazel_module: { bp2build_available: true },
Vinh Tran3ac6daf2022-04-22 19:09:58 -040043 java_version: "7",
Sam Delmericoc06ea032022-01-28 21:11:03 +000044}
45
46java_library {
47 name: "java-lib-1",
48 srcs: ["b.java"],
Sam Delmericoc06ea032022-01-28 21:11:03 +000049}
50
51java_library {
52 name: "java-lib-2",
53 srcs: ["c.java"],
Sam Delmericoc06ea032022-01-28 21:11:03 +000054}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000055 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000056 MakeBazelTarget("java_plugin", "java-plug-1", AttrNameToString{
Sam Delmericoc06ea032022-01-28 21:11:03 +000057 "target_compatible_with": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +000058 "//build/bazel_common_rules/platforms/os:android": ["@platforms//:incompatible"],
Sam Delmericoc06ea032022-01-28 21:11:03 +000059 "//conditions:default": [],
60 })`,
61 "deps": `[
Alix53fae382022-11-08 17:22:18 +000062 ":java-lib-1-neverlink",
Sam Delmericoc06ea032022-01-28 21:11:03 +000063 ":java-lib-2",
64 ]`,
65 "srcs": `[
66 "a.java",
67 "b.java",
68 ]`,
Romain Jobredeaux2eef2e12023-02-24 12:07:08 -050069 "java_version": `"7"`,
Sam Delmericoc06ea032022-01-28 21:11:03 +000070 }),
71 },
72 })
73}
Sam Delmericoc0161432022-02-25 21:34:51 +000074
75func TestJavaPluginNoSrcs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000076 runJavaPluginTestCase(t, Bp2buildTestCase{
Chris Parsonscd209032023-09-19 01:12:48 +000077 Description: "java_plugin without srcs converts (static) libs to deps",
78 StubbedBuildDefinitions: []string{"java-lib-1", "java-lib-2"},
Sam Delmerico3177a6e2022-06-21 19:28:33 +000079 Blueprint: `java_plugin {
Sam Delmericoc0161432022-02-25 21:34:51 +000080 name: "java-plug-1",
81 libs: ["java-lib-1"],
82 static_libs: ["java-lib-2"],
83 bazel_module: { bp2build_available: true },
84}
85
86java_library {
87 name: "java-lib-1",
88 srcs: ["b.java"],
Sam Delmericoc0161432022-02-25 21:34:51 +000089}
90
91java_library {
92 name: "java-lib-2",
93 srcs: ["c.java"],
Sam Delmericoc0161432022-02-25 21:34:51 +000094}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000095 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000096 MakeBazelTarget("java_plugin", "java-plug-1", AttrNameToString{
Sam Delmericoc0161432022-02-25 21:34:51 +000097 "target_compatible_with": `select({
Jingwen Chen9c2e3ee2023-10-11 10:51:28 +000098 "//build/bazel_common_rules/platforms/os:android": ["@platforms//:incompatible"],
Sam Delmericoc0161432022-02-25 21:34:51 +000099 "//conditions:default": [],
100 })`,
101 "deps": `[
Alix53fae382022-11-08 17:22:18 +0000102 ":java-lib-1-neverlink",
Sam Delmericoc0161432022-02-25 21:34:51 +0000103 ":java-lib-2",
104 ]`,
105 }),
106 },
107 })
108}