blob: f2b6f20a45ca343d8b6c0638a2eb2e19bb84e934 [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{
35 Description: "java_plugin with srcs, libs, static_libs",
36 Blueprint: `java_plugin {
Sam Delmericoc06ea032022-01-28 21:11:03 +000037 name: "java-plug-1",
38 srcs: ["a.java", "b.java"],
39 libs: ["java-lib-1"],
40 static_libs: ["java-lib-2"],
41 bazel_module: { bp2build_available: true },
Vinh Tran3ac6daf2022-04-22 19:09:58 -040042 java_version: "7",
Sam Delmericoc06ea032022-01-28 21:11:03 +000043}
44
45java_library {
46 name: "java-lib-1",
47 srcs: ["b.java"],
48 bazel_module: { bp2build_available: false },
49}
50
51java_library {
52 name: "java-lib-2",
53 srcs: ["c.java"],
54 bazel_module: { bp2build_available: false },
55}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000056 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000057 MakeBazelTarget("java_plugin", "java-plug-1", AttrNameToString{
Sam Delmericoc06ea032022-01-28 21:11:03 +000058 "target_compatible_with": `select({
59 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
60 "//conditions:default": [],
61 })`,
62 "deps": `[
Alix53fae382022-11-08 17:22:18 +000063 ":java-lib-1-neverlink",
Sam Delmericoc06ea032022-01-28 21:11:03 +000064 ":java-lib-2",
65 ]`,
66 "srcs": `[
67 "a.java",
68 "b.java",
69 ]`,
Romain Jobredeaux2eef2e12023-02-24 12:07:08 -050070 "java_version": `"7"`,
Sam Delmericoc06ea032022-01-28 21:11:03 +000071 }),
72 },
73 })
74}
Sam Delmericoc0161432022-02-25 21:34:51 +000075
76func TestJavaPluginNoSrcs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000077 runJavaPluginTestCase(t, Bp2buildTestCase{
78 Description: "java_plugin without srcs converts (static) libs to deps",
79 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"],
89 bazel_module: { bp2build_available: false },
90}
91
92java_library {
93 name: "java-lib-2",
94 srcs: ["c.java"],
95 bazel_module: { bp2build_available: false },
96}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000097 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000098 MakeBazelTarget("java_plugin", "java-plug-1", AttrNameToString{
Sam Delmericoc0161432022-02-25 21:34:51 +000099 "target_compatible_with": `select({
100 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
101 "//conditions:default": [],
102 })`,
103 "deps": `[
Alix53fae382022-11-08 17:22:18 +0000104 ":java-lib-1-neverlink",
Sam Delmericoc0161432022-02-25 21:34:51 +0000105 ":java-lib-2",
106 ]`,
107 }),
108 },
109 })
110}