blob: 61a398cd92966dd542c62a997cb9b67b0bd4204a [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{
Sam Delmerico7a629092022-03-07 16:20:54 +000074 "srcs": `["a.proto"]`,
Sam Delmericoc7681022022-02-04 21:01:20 +000075 })
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 "deps": fmt.Sprintf(`[":%s"]`, javaLibraryName),
93 }),
94 },
95 })
96 }
97}
98
99func TestJavaProtoDefault(t *testing.T) {
100 runJavaProtoTestCase(t, bp2buildTestCase{
101 description: "java_proto",
102 blueprint: `java_library_static {
103 name: "java-protos",
104 srcs: ["a.proto"],
105}
106`,
107 expectedBazelTargets: []string{
108 makeBazelTarget("proto_library", "java-protos_proto", attrNameToString{
Sam Delmerico7a629092022-03-07 16:20:54 +0000109 "srcs": `["a.proto"]`,
Sam Delmericoc7681022022-02-04 21:01:20 +0000110 }),
111 makeBazelTarget(
112 "java_lite_proto_library",
113 "java-protos_java_proto_lite",
114 attrNameToString{
115 "deps": `[":java-protos_proto"]`,
116 }),
117 makeBazelTarget("java_library", "java-protos", attrNameToString{
118 "deps": `[":java-protos_java_proto_lite"]`,
119 }),
120 },
121 })
122}