java_proto_conversion_test.go 3.6 KB

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