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 "fmt" 19 "testing" 20 21 "android/soong/android" 22 "android/soong/java" 23) 24 25func runJavaProtoTestCase(t *testing.T, tc Bp2buildTestCase) { 26 t.Helper() 27 (&tc).ModuleTypeUnderTest = "java_library_static" 28 (&tc).ModuleTypeUnderTestFactory = java.LibraryFactory 29 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc) 30} 31 32func TestJavaProto(t *testing.T) { 33 testCases := []struct { 34 protoType string 35 javaLibraryType string 36 javaLibraryNameExtension string 37 }{ 38 { 39 protoType: "nano", 40 javaLibraryType: "java_nano_proto_library", 41 javaLibraryNameExtension: "java_proto_nano", 42 }, 43 { 44 protoType: "micro", 45 javaLibraryType: "java_micro_proto_library", 46 javaLibraryNameExtension: "java_proto_micro", 47 }, 48 { 49 protoType: "lite", 50 javaLibraryType: "java_lite_proto_library", 51 javaLibraryNameExtension: "java_proto_lite", 52 }, 53 { 54 protoType: "stream", 55 javaLibraryType: "java_stream_proto_library", 56 javaLibraryNameExtension: "java_proto_stream", 57 }, 58 { 59 protoType: "full", 60 javaLibraryType: "java_proto_library", 61 javaLibraryNameExtension: "java_proto", 62 }, 63 } 64 65 bp := `java_library_static { 66 name: "java-protos", 67 proto: { 68 type: "%s", 69 }, 70 srcs: ["a.proto"], 71}` 72 73 protoLibrary := MakeBazelTarget("proto_library", "java-protos_proto", AttrNameToString{ 74 "srcs": `["a.proto"]`, 75 }) 76 77 for _, tc := range testCases { 78 javaLibraryName := fmt.Sprintf("java-protos_%s", tc.javaLibraryNameExtension) 79 80 runJavaProtoTestCase(t, Bp2buildTestCase{ 81 Description: fmt.Sprintf("java_proto %s", tc.protoType), 82 Blueprint: fmt.Sprintf(bp, tc.protoType), 83 ExpectedBazelTargets: []string{ 84 protoLibrary, 85 MakeBazelTarget( 86 tc.javaLibraryType, 87 javaLibraryName, 88 AttrNameToString{ 89 "deps": `[":java-protos_proto"]`, 90 }), 91 MakeBazelTarget("java_library", "java-protos", AttrNameToString{ 92 "exports": fmt.Sprintf(`[":%s"]`, javaLibraryName), 93 }), 94 MakeNeverlinkDuplicateTarget("java_library", "java-protos"), 95 }, 96 }) 97 } 98} 99 100func TestJavaProtoDefault(t *testing.T) { 101 runJavaProtoTestCase(t, Bp2buildTestCase{ 102 Description: "java_library proto default", 103 Blueprint: `java_library_static { 104 name: "java-protos", 105 srcs: ["a.proto"], 106 java_version: "7", 107} 108`, 109 ExpectedBazelTargets: []string{ 110 MakeBazelTarget("proto_library", "java-protos_proto", AttrNameToString{ 111 "srcs": `["a.proto"]`, 112 }), 113 MakeBazelTarget( 114 "java_lite_proto_library", 115 "java-protos_java_proto_lite", 116 AttrNameToString{ 117 "deps": `[":java-protos_proto"]`, 118 "java_version": `"7"`, 119 }), 120 MakeBazelTarget("java_library", "java-protos", AttrNameToString{ 121 "exports": `[":java-protos_java_proto_lite"]`, 122 "java_version": `"7"`, 123 }), 124 MakeNeverlinkDuplicateTargetWithAttrs( 125 "java_library", 126 "java-protos", 127 AttrNameToString{"java_version": `"7"`}), 128 }, 129 }) 130} 131