1// 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 24func runJavaPluginTestCase(t *testing.T, tc Bp2buildTestCase) { 25 t.Helper() 26 (&tc).ModuleTypeUnderTest = "java_plugin" 27 (&tc).ModuleTypeUnderTestFactory = java.PluginFactory 28 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) { 29 ctx.RegisterModuleType("java_library", java.LibraryFactory) 30 }, tc) 31} 32 33func TestJavaPlugin(t *testing.T) { 34 runJavaPluginTestCase(t, Bp2buildTestCase{ 35 Description: "java_plugin with srcs, libs, static_libs", 36 Blueprint: `java_plugin { 37 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 }, 42 java_version: "7", 43} 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}`, 56 ExpectedBazelTargets: []string{ 57 MakeBazelTarget("java_plugin", "java-plug-1", AttrNameToString{ 58 "target_compatible_with": `select({ 59 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], 60 "//conditions:default": [], 61 })`, 62 "deps": `[ 63 ":java-lib-1-neverlink", 64 ":java-lib-2", 65 ]`, 66 "srcs": `[ 67 "a.java", 68 "b.java", 69 ]`, 70 "java_version": `"7"`, 71 }), 72 }, 73 }) 74} 75 76func TestJavaPluginNoSrcs(t *testing.T) { 77 runJavaPluginTestCase(t, Bp2buildTestCase{ 78 Description: "java_plugin without srcs converts (static) libs to deps", 79 Blueprint: `java_plugin { 80 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}`, 97 ExpectedBazelTargets: []string{ 98 MakeBazelTarget("java_plugin", "java-plug-1", AttrNameToString{ 99 "target_compatible_with": `select({ 100 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], 101 "//conditions:default": [], 102 })`, 103 "deps": `[ 104 ":java-lib-1-neverlink", 105 ":java-lib-2", 106 ]`, 107 }), 108 }, 109 }) 110} 111