blob: 93b0677a623b93f699a3e61528539c09d45ec77d [file] [log] [blame]
Sam Delmericoc7681022022-02-04 21:01:20 +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 "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 "strip_import_prefix": `""`,
76 })
77
78 for _, tc := range testCases {
79 javaLibraryName := fmt.Sprintf("java-protos_%s", tc.javaLibraryNameExtension)
80
81 runJavaProtoTestCase(t, bp2buildTestCase{
82 description: fmt.Sprintf("java_proto %s", tc.protoType),
83 blueprint: fmt.Sprintf(bp, tc.protoType),
84 expectedBazelTargets: []string{
85 protoLibrary,
86 makeBazelTarget(
87 tc.javaLibraryType,
88 javaLibraryName,
89 attrNameToString{
90 "deps": `[":java-protos_proto"]`,
91 }),
92 makeBazelTarget("java_library", "java-protos", attrNameToString{
93 "deps": fmt.Sprintf(`[":%s"]`, javaLibraryName),
94 }),
95 },
96 })
97 }
98}
99
100func TestJavaProtoDefault(t *testing.T) {
101 runJavaProtoTestCase(t, bp2buildTestCase{
102 description: "java_proto",
103 blueprint: `java_library_static {
104 name: "java-protos",
105 srcs: ["a.proto"],
106}
107`,
108 expectedBazelTargets: []string{
109 makeBazelTarget("proto_library", "java-protos_proto", attrNameToString{
110 "srcs": `["a.proto"]`,
111 "strip_import_prefix": `""`,
112 }),
113 makeBazelTarget(
114 "java_lite_proto_library",
115 "java-protos_java_proto_lite",
116 attrNameToString{
117 "deps": `[":java-protos_proto"]`,
118 }),
119 makeBazelTarget("java_library", "java-protos", attrNameToString{
120 "deps": `[":java-protos_java_proto_lite"]`,
121 }),
122 },
123 })
124}