blob: 6dde93862e9bce66d253855e29b4bb07d8020841 [file] [log] [blame]
Colin Cross72bb3632017-07-13 16:23:21 -07001// Copyright 2017 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 java
16
17import (
18 "android/soong/android"
Colin Cross0f37af02017-09-27 17:42:05 -070019 "android/soong/genrule"
Colin Cross72bb3632017-07-13 16:23:21 -070020 "fmt"
21 "io/ioutil"
22 "os"
23 "path/filepath"
Colin Cross227d4362017-08-30 14:14:52 -070024 "reflect"
Colin Cross72bb3632017-07-13 16:23:21 -070025 "strings"
26 "testing"
27)
28
29var buildDir string
30
31func setUp() {
32 var err error
33 buildDir, err = ioutil.TempDir("", "soong_java_test")
34 if err != nil {
35 panic(err)
36 }
37}
38
39func tearDown() {
40 os.RemoveAll(buildDir)
41}
42
43func TestMain(m *testing.M) {
44 run := func() int {
45 setUp()
46 defer tearDown()
47
48 return m.Run()
49 }
50
51 os.Exit(run())
52}
Colin Cross72bb3632017-07-13 16:23:21 -070053func testJava(t *testing.T, bp string) *android.TestContext {
Colin Cross1369cdb2017-09-29 17:58:17 -070054 return testJavaWithEnv(t, bp, nil)
55}
56
57func testJavaWithEnv(t *testing.T, bp string, env map[string]string) *android.TestContext {
58 config := android.TestArchConfig(buildDir, env)
Colin Cross72bb3632017-07-13 16:23:21 -070059
Colin Cross4c428df2017-09-15 17:36:05 -070060 ctx := android.NewTestArchContext()
Colin Cross72bb3632017-07-13 16:23:21 -070061 ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory))
Colin Crossa60ead82017-10-02 18:10:21 -070062 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory(true)))
Colin Cross2acdae82017-09-15 19:44:24 -070063 ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070064 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross89536d42017-07-07 14:35:50 -070065 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross1369cdb2017-09-29 17:58:17 -070066 ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory))
Colin Cross0f37af02017-09-27 17:42:05 -070067 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(genrule.FileGroupFactory))
Colin Cross54190b32017-10-09 15:34:10 -070068 ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070069 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
70 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070071 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Colin Cross72bb3632017-07-13 16:23:21 -070072 ctx.Register()
73
Colin Cross4c428df2017-09-15 17:36:05 -070074 extraModules := []string{
75 "core-oj",
76 "core-libart",
77 "framework",
78 "ext",
79 "okhttp",
Colin Cross2acdae82017-09-15 19:44:24 -070080 "android_stubs_current",
81 "android_system_stubs_current",
82 "android_test_stubs_current",
Colin Cross93e85952017-08-15 13:34:18 -070083 "kotlin-stdlib",
Colin Cross4c428df2017-09-15 17:36:05 -070084 }
Colin Cross72bb3632017-07-13 16:23:21 -070085
86 for _, extra := range extraModules {
87 bp += fmt.Sprintf(`
88 java_library {
89 name: "%s",
Colin Cross0a6e0072017-08-30 14:24:55 -070090 srcs: ["a.java"],
Colin Cross76b5f0c2017-08-29 16:02:06 -070091 no_standard_libs: true,
Colin Cross1369cdb2017-09-29 17:58:17 -070092 system_modules: "core-system-modules",
Colin Cross72bb3632017-07-13 16:23:21 -070093 }
94 `, extra)
95 }
96
Colin Cross1369cdb2017-09-29 17:58:17 -070097 if config.TargetOpenJDK9() {
98 systemModules := []string{
99 "core-system-modules",
100 "android_stubs_current_system_modules",
101 "android_system_stubs_current_system_modules",
102 "android_test_stubs_current_system_modules",
103 }
104
105 for _, extra := range systemModules {
106 bp += fmt.Sprintf(`
107 java_system_modules {
108 name: "%s",
109 }
110 `, extra)
111 }
112 }
113
Colin Cross72bb3632017-07-13 16:23:21 -0700114 ctx.MockFileSystem(map[string][]byte{
Colin Crossfc3674a2017-09-18 17:41:52 -0700115 "Android.bp": []byte(bp),
116 "a.java": nil,
117 "b.java": nil,
118 "c.java": nil,
Colin Cross93e85952017-08-15 13:34:18 -0700119 "b.kt": nil,
Colin Crossfc3674a2017-09-18 17:41:52 -0700120 "a.jar": nil,
121 "b.jar": nil,
Colin Cross0f37af02017-09-27 17:42:05 -0700122 "res/a": nil,
123 "res/b": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700124 "res2/a": nil,
125
Colin Crossfc3674a2017-09-18 17:41:52 -0700126 "prebuilts/sdk/14/android.jar": nil,
127 "prebuilts/sdk/14/framework.aidl": nil,
Colin Cross72bb3632017-07-13 16:23:21 -0700128 })
129
130 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
131 fail(t, errs)
132 _, errs = ctx.PrepareBuildActions(config)
133 fail(t, errs)
134
135 return ctx
136}
137
Colin Cross2acdae82017-09-15 19:44:24 -0700138func moduleToPath(name string) string {
139 switch {
140 case name == `""`:
141 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700142 case strings.HasSuffix(name, ".jar"):
143 return name
Colin Cross2acdae82017-09-15 19:44:24 -0700144 default:
Colin Cross1ee23172017-10-18 14:44:18 -0700145 return filepath.Join(buildDir, ".intermediates", name, "android_common", "javac", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700146 }
147}
148
Colin Cross72bb3632017-07-13 16:23:21 -0700149func TestSimple(t *testing.T) {
150 ctx := testJava(t, `
151 java_library {
152 name: "foo",
153 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700154 libs: ["bar"],
155 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700156 }
157
158 java_library {
159 name: "bar",
160 srcs: ["b.java"],
161 }
162
163 java_library {
164 name: "baz",
165 srcs: ["c.java"],
166 }
Colin Crossd5934c82017-10-02 13:55:26 -0700167 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700168
Colin Cross4c428df2017-09-15 17:36:05 -0700169 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
170 combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar")
Colin Cross72bb3632017-07-13 16:23:21 -0700171
172 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
173 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
174 }
175
Colin Cross1ee23172017-10-18 14:44:18 -0700176 bar := ctx.ModuleForTests("bar", "android_common").Rule("javac").Output.String()
177 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700178
Colin Cross72bb3632017-07-13 16:23:21 -0700179 if !strings.Contains(javac.Args["classpath"], bar) {
180 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
181 }
182
Colin Cross0a6e0072017-08-30 14:24:55 -0700183 if !strings.Contains(javac.Args["classpath"], baz) {
184 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], baz)
185 }
186
187 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
188 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700189 }
190}
191
Colin Crossd5934c82017-10-02 13:55:26 -0700192func TestArchSpecific(t *testing.T) {
193 ctx := testJava(t, `
194 java_library {
195 name: "foo",
196 srcs: ["a.java"],
197 target: {
198 android: {
199 srcs: ["b.java"],
200 },
201 },
202 }
203 `)
204
205 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
206 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
207 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
208 }
209}
210
Colin Cross2acdae82017-09-15 19:44:24 -0700211var classpathTestcases = []struct {
212 name string
Colin Cross965714f2017-10-03 20:57:01 -0700213 moduleType string
Colin Cross2acdae82017-09-15 19:44:24 -0700214 host android.OsClass
215 properties string
216 bootclasspath []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700217 system string
Colin Cross2acdae82017-09-15 19:44:24 -0700218 classpath []string
219}{
220 {
221 name: "default",
222 bootclasspath: []string{"core-oj", "core-libart"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700223 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700224 classpath: []string{"ext", "framework", "okhttp"},
225 },
226 {
227 name: "blank sdk version",
228 properties: `sdk_version: "",`,
229 bootclasspath: []string{"core-oj", "core-libart"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700230 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700231 classpath: []string{"ext", "framework", "okhttp"},
232 },
233 {
Colin Cross4c428df2017-09-15 17:36:05 -0700234
Colin Cross2acdae82017-09-15 19:44:24 -0700235 name: "sdk v14",
236 properties: `sdk_version: "14",`,
Colin Crossfc3674a2017-09-18 17:41:52 -0700237 bootclasspath: []string{`""`},
Colin Cross1369cdb2017-09-29 17:58:17 -0700238 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossfc3674a2017-09-18 17:41:52 -0700239 classpath: []string{"prebuilts/sdk/14/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700240 },
241 {
Colin Cross72bb3632017-07-13 16:23:21 -0700242
Colin Cross2acdae82017-09-15 19:44:24 -0700243 name: "current",
244 properties: `sdk_version: "current",`,
245 bootclasspath: []string{"android_stubs_current"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700246 system: "android_stubs_current_system_modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700247 classpath: []string{},
248 },
249 {
Colin Cross72bb3632017-07-13 16:23:21 -0700250
Colin Cross2acdae82017-09-15 19:44:24 -0700251 name: "system_current",
252 properties: `sdk_version: "system_current",`,
253 bootclasspath: []string{"android_system_stubs_current"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700254 system: "android_system_stubs_current_system_modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700255 classpath: []string{},
256 },
257 {
Colin Cross72bb3632017-07-13 16:23:21 -0700258
Colin Cross2acdae82017-09-15 19:44:24 -0700259 name: "test_current",
260 properties: `sdk_version: "test_current",`,
261 bootclasspath: []string{"android_test_stubs_current"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700262 system: "android_test_stubs_current_system_modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700263 classpath: []string{},
264 },
265 {
Colin Cross72bb3632017-07-13 16:23:21 -0700266
Colin Cross2acdae82017-09-15 19:44:24 -0700267 name: "nostdlib",
Colin Cross1369cdb2017-09-29 17:58:17 -0700268 properties: `no_standard_libs: true, system_modules: "none"`,
269 system: "none",
270 bootclasspath: []string{`""`},
271 classpath: []string{},
272 },
273 {
274
275 name: "nostdlib system_modules",
276 properties: `no_standard_libs: true, system_modules: "core-system-modules"`,
277 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700278 bootclasspath: []string{`""`},
279 classpath: []string{},
280 },
281 {
Colin Cross72bb3632017-07-13 16:23:21 -0700282
Colin Cross2acdae82017-09-15 19:44:24 -0700283 name: "host default",
Colin Cross965714f2017-10-03 20:57:01 -0700284 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700285 properties: ``,
Colin Cross965714f2017-10-03 20:57:01 -0700286 host: android.Host,
Colin Cross2acdae82017-09-15 19:44:24 -0700287 classpath: []string{},
288 },
289 {
290 name: "host nostdlib",
Colin Cross965714f2017-10-03 20:57:01 -0700291 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700292 host: android.Host,
293 properties: `no_standard_libs: true`,
294 classpath: []string{},
295 },
Colin Cross965714f2017-10-03 20:57:01 -0700296 {
297
298 name: "host supported default",
299 host: android.Host,
300 properties: `host_supported: true,`,
301 classpath: []string{},
302 },
303 {
304 name: "host supported nostdlib",
305 host: android.Host,
Colin Cross1369cdb2017-09-29 17:58:17 -0700306 properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
Colin Cross965714f2017-10-03 20:57:01 -0700307 classpath: []string{},
308 },
Colin Cross2acdae82017-09-15 19:44:24 -0700309}
Colin Cross72bb3632017-07-13 16:23:21 -0700310
Colin Cross2acdae82017-09-15 19:44:24 -0700311func TestClasspath(t *testing.T) {
312 for _, testcase := range classpathTestcases {
313 t.Run(testcase.name, func(t *testing.T) {
Colin Cross965714f2017-10-03 20:57:01 -0700314 moduleType := "java_library"
315 if testcase.moduleType != "" {
316 moduleType = testcase.moduleType
Colin Cross2acdae82017-09-15 19:44:24 -0700317 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700318
319 bp := moduleType + ` {
Colin Cross2acdae82017-09-15 19:44:24 -0700320 name: "foo",
321 srcs: ["a.java"],
Colin Cross1369cdb2017-09-29 17:58:17 -0700322 ` + testcase.properties + `
323 }`
324
325 variant := "android_common"
326 if testcase.host == android.Host {
327 variant = android.BuildOs.String() + "_common"
Colin Cross2acdae82017-09-15 19:44:24 -0700328 }
Colin Cross72bb3632017-07-13 16:23:21 -0700329
Colin Cross2acdae82017-09-15 19:44:24 -0700330 convertModulesToPaths := func(cp []string) []string {
331 ret := make([]string, len(cp))
332 for i, e := range cp {
333 ret[i] = moduleToPath(e)
334 }
335 return ret
336 }
Colin Cross72bb3632017-07-13 16:23:21 -0700337
Colin Cross2acdae82017-09-15 19:44:24 -0700338 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
339 classpath := convertModulesToPaths(testcase.classpath)
Colin Cross72bb3632017-07-13 16:23:21 -0700340
Colin Cross2acdae82017-09-15 19:44:24 -0700341 bc := strings.Join(bootclasspath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700342 if bc != "" {
343 bc = "-bootclasspath " + bc
Colin Cross72bb3632017-07-13 16:23:21 -0700344 }
Colin Cross72bb3632017-07-13 16:23:21 -0700345
Colin Cross2acdae82017-09-15 19:44:24 -0700346 c := strings.Join(classpath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700347 if c != "" {
348 c = "-classpath " + c
349 }
350 system := ""
351 if testcase.system == "none" {
352 system = "--system=none"
353 } else if testcase.system != "" {
354 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
Colin Cross2acdae82017-09-15 19:44:24 -0700355 }
356
Colin Cross1369cdb2017-09-29 17:58:17 -0700357 t.Run("1.8", func(t *testing.T) {
358 // Test default javac 1.8
359 ctx := testJava(t, bp)
Colin Cross2acdae82017-09-15 19:44:24 -0700360
Colin Cross1369cdb2017-09-29 17:58:17 -0700361 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
362
363 got := javac.Args["bootClasspath"]
364 if got != bc {
365 t.Errorf("bootclasspath expected %q != got %q", bc, got)
366 }
367
368 got = javac.Args["classpath"]
369 if got != c {
370 t.Errorf("classpath expected %q != got %q", c, got)
371 }
372
373 var deps []string
374 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
375 deps = append(deps, bootclasspath...)
376 }
377 deps = append(deps, classpath...)
378
379 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
380 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
381 }
382 })
383
384 // Test again with javac 1.9
385 t.Run("1.9", func(t *testing.T) {
386 ctx := testJavaWithEnv(t, bp, map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
387
388 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
389 got := javac.Args["bootClasspath"]
390 expected := system
391 if testcase.system == "bootclasspath" {
392 expected = bc
393 }
394 if got != expected {
395 t.Errorf("bootclasspath expected %q != got %q", expected, got)
396 }
397 })
Colin Cross2acdae82017-09-15 19:44:24 -0700398 })
Colin Cross72bb3632017-07-13 16:23:21 -0700399 }
400
Colin Cross72bb3632017-07-13 16:23:21 -0700401}
402
403func TestPrebuilts(t *testing.T) {
404 ctx := testJava(t, `
405 java_library {
406 name: "foo",
407 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700408 libs: ["bar"],
409 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700410 }
411
Colin Cross74d73e22017-08-02 11:05:49 -0700412 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700413 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700414 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700415 }
416
Colin Cross74d73e22017-08-02 11:05:49 -0700417 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700418 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700419 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700420 }
421 `)
422
Colin Cross4c428df2017-09-15 17:36:05 -0700423 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
424 combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar")
Colin Cross72bb3632017-07-13 16:23:21 -0700425
426 bar := "a.jar"
427 if !strings.Contains(javac.Args["classpath"], bar) {
428 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
429 }
430
Colin Cross0a6e0072017-08-30 14:24:55 -0700431 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" {
432 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, "b.jar")
Colin Cross72bb3632017-07-13 16:23:21 -0700433 }
434}
435
Colin Cross89536d42017-07-07 14:35:50 -0700436func TestDefaults(t *testing.T) {
437 ctx := testJava(t, `
438 java_defaults {
439 name: "defaults",
440 srcs: ["a.java"],
441 libs: ["bar"],
442 static_libs: ["baz"],
443 }
444
445 java_library {
446 name: "foo",
447 defaults: ["defaults"],
448 }
449
450 java_library {
451 name: "bar",
452 srcs: ["b.java"],
453 }
454
455 java_library {
456 name: "baz",
457 srcs: ["c.java"],
458 }
459 `)
460
Colin Cross4c428df2017-09-15 17:36:05 -0700461 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
462 combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar")
Colin Cross89536d42017-07-07 14:35:50 -0700463
464 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
465 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
466 }
467
Colin Cross1ee23172017-10-18 14:44:18 -0700468 bar := ctx.ModuleForTests("bar", "android_common").Rule("javac").Output.String()
Colin Cross89536d42017-07-07 14:35:50 -0700469 if !strings.Contains(javac.Args["classpath"], bar) {
470 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
471 }
472
Colin Cross1ee23172017-10-18 14:44:18 -0700473 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700474 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
475 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700476 }
477}
478
Colin Cross0f37af02017-09-27 17:42:05 -0700479func TestResources(t *testing.T) {
480 var table = []struct {
481 name string
482 prop string
483 extra string
484 args string
485 }{
486 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700487 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700488 name: "resource dirs",
489 prop: `java_resource_dirs: ["res"]`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700490 args: "-C res -f res/a -f res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700491 },
492 {
493 // Test that a module with java_resources includes the files
494 name: "resource files",
495 prop: `java_resources: ["res/a", "res/b"]`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700496 args: "-C . -f res/a -f res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700497 },
498 {
499 // Test that a module with a filegroup in java_resources includes the files with the
500 // path prefix
501 name: "resource filegroup",
502 prop: `java_resources: [":foo-res"]`,
503 extra: `
504 filegroup {
505 name: "foo-res",
506 path: "res",
507 srcs: ["res/a", "res/b"],
508 }`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700509 args: "-C res -f res/a -f res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700510 },
511 {
512 // Test that a module with "include_srcs: true" includes its source files in the resources jar
513 name: "include sources",
514 prop: `include_srcs: true`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700515 args: "-C . -f a.java -f b.java -f c.java",
Colin Cross0f37af02017-09-27 17:42:05 -0700516 },
517 }
518
519 for _, test := range table {
520 t.Run(test.name, func(t *testing.T) {
521 ctx := testJava(t, `
522 java_library {
523 name: "foo",
524 srcs: [
525 "a.java",
526 "b.java",
527 "c.java",
528 ],
529 `+test.prop+`,
530 }
531 `+test.extra)
532
Colin Cross1ee23172017-10-18 14:44:18 -0700533 foo := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
534 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700535
536 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
537 t.Errorf("foo combined jars %v does not contain %q",
538 foo.Inputs.Strings(), fooRes.Output.String())
539 }
540
Colin Crossaf9c55b2017-10-03 14:50:08 -0700541 if fooRes.Args["jarArgs"] != test.args {
542 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700543 fooRes.Args["jarArgs"], test.args)
544 }
545 })
546 }
547}
548
Colin Cross0532fb02017-10-02 16:57:40 -0700549func TestExcludeResources(t *testing.T) {
550 ctx := testJava(t, `
551 java_library {
552 name: "foo",
553 srcs: ["a.java"],
554 java_resource_dirs: ["res", "res2"],
555 exclude_java_resource_dirs: ["res2"],
556 }
557
558 java_library {
559 name: "bar",
560 srcs: ["a.java"],
561 java_resources: ["res/*"],
562 exclude_java_resources: ["res/b"],
563 }
564 `)
565
Colin Cross1ee23172017-10-18 14:44:18 -0700566 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700567
Colin Crossaf9c55b2017-10-03 14:50:08 -0700568 expected := "-C res -f res/a -f res/b"
Colin Cross0532fb02017-10-02 16:57:40 -0700569 if fooRes.Args["jarArgs"] != expected {
570 t.Errorf("foo resource jar args %q is not %q",
571 fooRes.Args["jarArgs"], expected)
572
573 }
574
Colin Cross1ee23172017-10-18 14:44:18 -0700575 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700576
577 expected = "-C . -f res/a"
578 if barRes.Args["jarArgs"] != expected {
579 t.Errorf("bar resource jar args %q is not %q",
580 barRes.Args["jarArgs"], expected)
581
582 }
583}
584
Colin Cross54190b32017-10-09 15:34:10 -0700585func TestGeneratedSources(t *testing.T) {
586 ctx := testJava(t, `
587 java_library {
588 name: "foo",
589 srcs: [
590 "a*.java",
591 ":gen",
592 "b*.java",
593 ],
594 }
595
596 genrule {
597 name: "gen",
598 tool_files: ["res/a"],
599 out: ["gen.java"],
600 }
601 `)
602
603 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
604 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
605
606 if len(genrule.Outputs) != 1 || filepath.Base(genrule.Outputs[0].String()) != "gen.java" {
607 t.Fatalf(`gen output file %v is not [".../gen.java"]`, genrule.Outputs.Strings())
608 }
609
610 if len(javac.Inputs) != 3 ||
611 javac.Inputs[0].String() != "a.java" ||
612 javac.Inputs[1].String() != genrule.Outputs[0].String() ||
613 javac.Inputs[2].String() != "b.java" {
614 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
615 }
616}
617
Colin Cross93e85952017-08-15 13:34:18 -0700618func TestKotlin(t *testing.T) {
619 ctx := testJava(t, `
620 java_library {
621 name: "foo",
622 srcs: ["a.java", "b.kt"],
623 }
624 `)
625
626 kotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
627 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Colin Cross1ee23172017-10-18 14:44:18 -0700628 jar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
Colin Cross93e85952017-08-15 13:34:18 -0700629
630 if len(kotlinc.Inputs) != 2 || kotlinc.Inputs[0].String() != "a.java" ||
631 kotlinc.Inputs[1].String() != "b.kt" {
632 t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, kotlinc.Inputs)
633 }
634
635 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
636 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
637 }
638
639 if !strings.Contains(javac.Args["classpath"], kotlinc.Output.String()) {
640 t.Errorf("foo classpath %v does not contain %q",
641 javac.Args["classpath"], kotlinc.Output.String())
642 }
643
644 if !inList(kotlinc.Output.String(), jar.Inputs.Strings()) {
645 t.Errorf("foo jar inputs %v does not contain %q",
646 jar.Inputs.Strings(), kotlinc.Output.String())
647 }
648}
649
Colin Cross72bb3632017-07-13 16:23:21 -0700650func fail(t *testing.T, errs []error) {
651 if len(errs) > 0 {
652 for _, err := range errs {
653 t.Error(err)
654 }
655 t.FailNow()
656 }
657}