java_library_conversion_test.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 runJavaLibraryTestCaseWithRegistrationCtxFunc(t *testing.T, tc bp2buildTestCase, registrationCtxFunc func(ctx android.RegistrationContext)) {
  22. t.Helper()
  23. (&tc).moduleTypeUnderTest = "java_library"
  24. (&tc).moduleTypeUnderTestFactory = java.LibraryFactory
  25. runBp2BuildTestCase(t, registrationCtxFunc, tc)
  26. }
  27. func runJavaLibraryTestCase(t *testing.T, tc bp2buildTestCase) {
  28. t.Helper()
  29. runJavaLibraryTestCaseWithRegistrationCtxFunc(t, tc, func(ctx android.RegistrationContext) {})
  30. }
  31. func TestJavaLibrary(t *testing.T) {
  32. runJavaLibraryTestCase(t, bp2buildTestCase{
  33. description: "java_library with srcs, exclude_srcs and libs",
  34. blueprint: `java_library {
  35. name: "java-lib-1",
  36. srcs: ["a.java", "b.java"],
  37. exclude_srcs: ["b.java"],
  38. libs: ["java-lib-2"],
  39. bazel_module: { bp2build_available: true },
  40. }
  41. java_library {
  42. name: "java-lib-2",
  43. srcs: ["b.java"],
  44. bazel_module: { bp2build_available: true },
  45. }`,
  46. expectedBazelTargets: []string{
  47. makeBazelTarget("java_library", "java-lib-1", attrNameToString{
  48. "srcs": `["a.java"]`,
  49. "deps": `[":java-lib-2"]`,
  50. }),
  51. makeBazelTarget("java_library", "java-lib-2", attrNameToString{
  52. "srcs": `["b.java"]`,
  53. }),
  54. },
  55. })
  56. }
  57. func TestJavaLibraryConvertsStaticLibsToDepsAndExports(t *testing.T) {
  58. runJavaLibraryTestCase(t, bp2buildTestCase{
  59. blueprint: `java_library {
  60. name: "java-lib-1",
  61. srcs: ["a.java"],
  62. libs: ["java-lib-2"],
  63. static_libs: ["java-lib-3"],
  64. bazel_module: { bp2build_available: true },
  65. }
  66. java_library {
  67. name: "java-lib-2",
  68. srcs: ["b.java"],
  69. bazel_module: { bp2build_available: false },
  70. }
  71. java_library {
  72. name: "java-lib-3",
  73. srcs: ["c.java"],
  74. bazel_module: { bp2build_available: false },
  75. }`,
  76. expectedBazelTargets: []string{
  77. makeBazelTarget("java_library", "java-lib-1", attrNameToString{
  78. "srcs": `["a.java"]`,
  79. "deps": `[
  80. ":java-lib-2",
  81. ":java-lib-3",
  82. ]`,
  83. "exports": `[":java-lib-3"]`,
  84. }),
  85. },
  86. })
  87. }
  88. func TestJavaLibraryConvertsStaticLibsToExportsIfNoSrcs(t *testing.T) {
  89. runJavaLibraryTestCase(t, bp2buildTestCase{
  90. blueprint: `java_library {
  91. name: "java-lib-1",
  92. static_libs: ["java-lib-2"],
  93. bazel_module: { bp2build_available: true },
  94. }
  95. java_library {
  96. name: "java-lib-2",
  97. srcs: ["a.java"],
  98. bazel_module: { bp2build_available: false },
  99. }`,
  100. expectedBazelTargets: []string{
  101. makeBazelTarget("java_library", "java-lib-1", attrNameToString{
  102. "exports": `[":java-lib-2"]`,
  103. }),
  104. },
  105. })
  106. }
  107. func TestJavaLibraryFailsToConvertLibsWithNoSrcs(t *testing.T) {
  108. runJavaLibraryTestCase(t, bp2buildTestCase{
  109. expectedErr: fmt.Errorf("Module has direct dependencies but no sources. Bazel will not allow this."),
  110. blueprint: `java_library {
  111. name: "java-lib-1",
  112. libs: ["java-lib-2"],
  113. bazel_module: { bp2build_available: true },
  114. }
  115. java_library {
  116. name: "java-lib-2",
  117. srcs: ["a.java"],
  118. bazel_module: { bp2build_available: false },
  119. }`,
  120. expectedBazelTargets: []string{},
  121. })
  122. }
  123. func TestJavaLibraryPlugins(t *testing.T) {
  124. runJavaLibraryTestCaseWithRegistrationCtxFunc(t, bp2buildTestCase{
  125. blueprint: `java_library {
  126. name: "java-lib-1",
  127. plugins: ["java-plugin-1"],
  128. bazel_module: { bp2build_available: true },
  129. }
  130. java_plugin {
  131. name: "java-plugin-1",
  132. srcs: ["a.java"],
  133. bazel_module: { bp2build_available: false },
  134. }`,
  135. expectedBazelTargets: []string{
  136. makeBazelTarget("java_library", "java-lib-1", attrNameToString{
  137. "plugins": `[":java-plugin-1"]`,
  138. }),
  139. },
  140. }, func(ctx android.RegistrationContext) {
  141. ctx.RegisterModuleType("java_plugin", java.PluginFactory)
  142. })
  143. }
  144. func TestJavaLibraryJavaVersion(t *testing.T) {
  145. runJavaLibraryTestCase(t, bp2buildTestCase{
  146. blueprint: `java_library {
  147. name: "java-lib-1",
  148. srcs: ["a.java"],
  149. java_version: "11",
  150. }`,
  151. expectedBazelTargets: []string{
  152. makeBazelTarget("java_library", "java-lib-1", attrNameToString{
  153. "srcs": `["a.java"]`,
  154. "javacopts": `["-source 11 -target 11"]`,
  155. }),
  156. },
  157. })
  158. }
  159. func TestJavaLibraryErrorproneJavacflagsEnabledManually(t *testing.T) {
  160. runJavaLibraryTestCase(t, bp2buildTestCase{
  161. blueprint: `java_library {
  162. name: "java-lib-1",
  163. srcs: ["a.java"],
  164. javacflags: ["-Xsuper-fast"],
  165. errorprone: {
  166. enabled: true,
  167. javacflags: ["-Xep:SpeedLimit:OFF"],
  168. },
  169. }`,
  170. expectedBazelTargets: []string{
  171. makeBazelTarget("java_library", "java-lib-1", attrNameToString{
  172. "javacopts": `[
  173. "-Xsuper-fast",
  174. "-Xep:SpeedLimit:OFF",
  175. ]`,
  176. "srcs": `["a.java"]`,
  177. }),
  178. },
  179. })
  180. }
  181. func TestJavaLibraryErrorproneJavacflagsErrorproneDisabledByDefault(t *testing.T) {
  182. runJavaLibraryTestCase(t, bp2buildTestCase{
  183. blueprint: `java_library {
  184. name: "java-lib-1",
  185. srcs: ["a.java"],
  186. javacflags: ["-Xsuper-fast"],
  187. errorprone: {
  188. javacflags: ["-Xep:SpeedLimit:OFF"],
  189. },
  190. }`,
  191. expectedBazelTargets: []string{
  192. makeBazelTarget("java_library", "java-lib-1", attrNameToString{
  193. "javacopts": `["-Xsuper-fast"]`,
  194. "srcs": `["a.java"]`,
  195. }),
  196. },
  197. })
  198. }
  199. func TestJavaLibraryErrorproneJavacflagsErrorproneDisabledManually(t *testing.T) {
  200. runJavaLibraryTestCase(t, bp2buildTestCase{
  201. blueprint: `java_library {
  202. name: "java-lib-1",
  203. srcs: ["a.java"],
  204. javacflags: ["-Xsuper-fast"],
  205. errorprone: {
  206. enabled: false,
  207. javacflags: ["-Xep:SpeedLimit:OFF"],
  208. },
  209. }`,
  210. expectedBazelTargets: []string{
  211. makeBazelTarget("java_library", "java-lib-1", attrNameToString{
  212. "javacopts": `["-Xsuper-fast"]`,
  213. "srcs": `["a.java"]`,
  214. }),
  215. },
  216. })
  217. }
  218. func TestJavaLibraryLogTags(t *testing.T) {
  219. runJavaLibraryTestCase(t, bp2buildTestCase{
  220. description: "Java library - logtags creates separate dependency",
  221. moduleTypeUnderTest: "java_library",
  222. moduleTypeUnderTestFactory: java.LibraryFactory,
  223. blueprint: `java_library {
  224. name: "example_lib",
  225. srcs: [
  226. "a.java",
  227. "b.java",
  228. "a.logtag",
  229. "b.logtag",
  230. ],
  231. bazel_module: { bp2build_available: true },
  232. }`,
  233. expectedBazelTargets: []string{
  234. makeBazelTarget("event_log_tags", "example_lib_logtags", attrNameToString{
  235. "srcs": `[
  236. "a.logtag",
  237. "b.logtag",
  238. ]`,
  239. }),
  240. makeBazelTarget("java_library", "example_lib", attrNameToString{
  241. "srcs": `[
  242. "a.java",
  243. "b.java",
  244. ":example_lib_logtags",
  245. ]`,
  246. }),
  247. }})
  248. }
  249. func TestJavaLibraryResources(t *testing.T) {
  250. runJavaLibraryTestCase(t, bp2buildTestCase{
  251. filesystem: map[string]string{
  252. "res/a.res": "",
  253. "res/b.res": "",
  254. "res/dir1/b.res": "",
  255. },
  256. blueprint: `java_library {
  257. name: "java-lib-1",
  258. java_resources: ["res/a.res", "res/b.res"],
  259. }`,
  260. expectedBazelTargets: []string{
  261. makeBazelTarget("java_library", "java-lib-1", attrNameToString{
  262. "resources": `[
  263. "res/a.res",
  264. "res/b.res",
  265. ]`,
  266. }),
  267. },
  268. })
  269. }
  270. func TestJavaLibraryResourceDirs(t *testing.T) {
  271. runJavaLibraryTestCase(t, bp2buildTestCase{
  272. filesystem: map[string]string{
  273. "res/a.res": "",
  274. "res/b.res": "",
  275. "res/dir1/b.res": "",
  276. },
  277. blueprint: `java_library {
  278. name: "java-lib-1",
  279. java_resource_dirs: ["res"],
  280. }`,
  281. expectedBazelTargets: []string{
  282. makeBazelTarget("java_library", "java-lib-1", attrNameToString{
  283. "resource_strip_prefix": `"res"`,
  284. "resources": `[
  285. "res/a.res",
  286. "res/b.res",
  287. "res/dir1/b.res",
  288. ]`,
  289. }),
  290. },
  291. })
  292. }
  293. func TestJavaLibraryResourcesExcludeDir(t *testing.T) {
  294. runJavaLibraryTestCase(t, bp2buildTestCase{
  295. filesystem: map[string]string{
  296. "res/a.res": "",
  297. "res/exclude/b.res": "",
  298. },
  299. blueprint: `java_library {
  300. name: "java-lib-1",
  301. java_resource_dirs: ["res"],
  302. exclude_java_resource_dirs: ["res/exclude"],
  303. }`,
  304. expectedBazelTargets: []string{
  305. makeBazelTarget("java_library", "java-lib-1", attrNameToString{
  306. "resource_strip_prefix": `"res"`,
  307. "resources": `["res/a.res"]`,
  308. }),
  309. },
  310. })
  311. }
  312. func TestJavaLibraryResourcesExcludeFile(t *testing.T) {
  313. runJavaLibraryTestCase(t, bp2buildTestCase{
  314. filesystem: map[string]string{
  315. "res/a.res": "",
  316. "res/dir1/b.res": "",
  317. "res/dir1/exclude.res": "",
  318. },
  319. blueprint: `java_library {
  320. name: "java-lib-1",
  321. java_resource_dirs: ["res"],
  322. exclude_java_resources: ["res/dir1/exclude.res"],
  323. }`,
  324. expectedBazelTargets: []string{
  325. makeBazelTarget("java_library", "java-lib-1", attrNameToString{
  326. "resource_strip_prefix": `"res"`,
  327. "resources": `[
  328. "res/a.res",
  329. "res/dir1/b.res",
  330. ]`,
  331. }),
  332. },
  333. })
  334. }
  335. func TestJavaLibraryResourcesFailsWithMultipleDirs(t *testing.T) {
  336. runJavaLibraryTestCase(t, bp2buildTestCase{
  337. filesystem: map[string]string{
  338. "res/a.res": "",
  339. "res1/a.res": "",
  340. },
  341. blueprint: `java_library {
  342. name: "java-lib-1",
  343. java_resource_dirs: ["res", "res1"],
  344. }`,
  345. expectedErr: fmt.Errorf("bp2build does not support more than one directory in java_resource_dirs (b/226423379)"),
  346. expectedBazelTargets: []string{},
  347. })
  348. }