blob: c6feeb8dc97479666ee24eeda3f4b1032efc27e1 [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{
Sam Delmericoc0161432022-02-25 21:34:51 +000092 "exports": fmt.Sprintf(`[":%s"]`, javaLibraryName),
Sam Delmericoc7681022022-02-04 21:01:20 +000093 }),
94 },
95 })
96 }
97}
98
99func TestJavaProtoDefault(t *testing.T) {
100 runJavaProtoTestCase(t, bp2buildTestCase{
Sam Delmericoc0161432022-02-25 21:34:51 +0000101 description: "java_library proto default",
Sam Delmericoc7681022022-02-04 21:01:20 +0000102 blueprint: `java_library_static {
103 name: "java-protos",
104 srcs: ["a.proto"],
Vinh Tran3ac6daf2022-04-22 19:09:58 -0400105 java_version: "7",
Sam Delmericoc7681022022-02-04 21:01:20 +0000106}
107`,
108 expectedBazelTargets: []string{
109 makeBazelTarget("proto_library", "java-protos_proto", attrNameToString{
Sam Delmerico7a629092022-03-07 16:20:54 +0000110 "srcs": `["a.proto"]`,
Sam Delmericoc7681022022-02-04 21:01:20 +0000111 }),
112 makeBazelTarget(
113 "java_lite_proto_library",
114 "java-protos_java_proto_lite",
115 attrNameToString{
116 "deps": `[":java-protos_proto"]`,
117 }),
118 makeBazelTarget("java_library", "java-protos", attrNameToString{
Vinh Tran3ac6daf2022-04-22 19:09:58 -0400119 "exports": `[":java-protos_java_proto_lite"]`,
120 "javacopts": `["-source 1.7 -target 1.7"]`,
Sam Delmericoc7681022022-02-04 21:01:20 +0000121 }),
122 },
123 })
124}