blob: bf839835d1d60ff67a7b1fcc5eb12fad77e49a13 [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
Nan Zhanged19fc32017-10-19 13:06:22 -0700144 case name == "android_stubs_current" || name == "android_system_stubs_current" ||
145 name == "android_test_stubs_current":
Colin Cross1ee23172017-10-18 14:44:18 -0700146 return filepath.Join(buildDir, ".intermediates", name, "android_common", "javac", name+".jar")
Nan Zhanged19fc32017-10-19 13:06:22 -0700147 default:
148 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700149 }
150}
151
Colin Cross72bb3632017-07-13 16:23:21 -0700152func TestSimple(t *testing.T) {
153 ctx := testJava(t, `
154 java_library {
155 name: "foo",
156 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700157 libs: ["bar"],
158 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700159 }
160
161 java_library {
162 name: "bar",
163 srcs: ["b.java"],
164 }
165
166 java_library {
167 name: "baz",
168 srcs: ["c.java"],
169 }
Colin Crossd5934c82017-10-02 13:55:26 -0700170 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700171
Colin Cross4c428df2017-09-15 17:36:05 -0700172 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700173 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700174
175 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
176 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
177 }
178
Colin Cross1ee23172017-10-18 14:44:18 -0700179 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700180 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
181 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700182
Nan Zhanged19fc32017-10-19 13:06:22 -0700183 if !strings.Contains(javac.Args["classpath"], barTurbine) {
184 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700185 }
186
Nan Zhanged19fc32017-10-19 13:06:22 -0700187 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
188 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700189 }
190
191 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
192 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700193 }
194}
195
Colin Crossd5934c82017-10-02 13:55:26 -0700196func TestArchSpecific(t *testing.T) {
197 ctx := testJava(t, `
198 java_library {
199 name: "foo",
200 srcs: ["a.java"],
201 target: {
202 android: {
203 srcs: ["b.java"],
204 },
205 },
206 }
207 `)
208
209 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
210 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
211 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
212 }
213}
214
Colin Cross2acdae82017-09-15 19:44:24 -0700215var classpathTestcases = []struct {
216 name string
Colin Cross965714f2017-10-03 20:57:01 -0700217 moduleType string
Colin Cross2acdae82017-09-15 19:44:24 -0700218 host android.OsClass
219 properties string
220 bootclasspath []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700221 system string
Colin Cross2acdae82017-09-15 19:44:24 -0700222 classpath []string
223}{
224 {
225 name: "default",
226 bootclasspath: []string{"core-oj", "core-libart"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700227 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700228 classpath: []string{"ext", "framework", "okhttp"},
229 },
230 {
231 name: "blank sdk version",
232 properties: `sdk_version: "",`,
233 bootclasspath: []string{"core-oj", "core-libart"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700234 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700235 classpath: []string{"ext", "framework", "okhttp"},
236 },
237 {
Colin Cross4c428df2017-09-15 17:36:05 -0700238
Colin Cross2acdae82017-09-15 19:44:24 -0700239 name: "sdk v14",
240 properties: `sdk_version: "14",`,
Colin Crossfc3674a2017-09-18 17:41:52 -0700241 bootclasspath: []string{`""`},
Colin Cross1369cdb2017-09-29 17:58:17 -0700242 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossfc3674a2017-09-18 17:41:52 -0700243 classpath: []string{"prebuilts/sdk/14/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700244 },
245 {
Colin Cross72bb3632017-07-13 16:23:21 -0700246
Colin Cross2acdae82017-09-15 19:44:24 -0700247 name: "current",
248 properties: `sdk_version: "current",`,
249 bootclasspath: []string{"android_stubs_current"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700250 system: "android_stubs_current_system_modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700251 classpath: []string{},
252 },
253 {
Colin Cross72bb3632017-07-13 16:23:21 -0700254
Colin Cross2acdae82017-09-15 19:44:24 -0700255 name: "system_current",
256 properties: `sdk_version: "system_current",`,
257 bootclasspath: []string{"android_system_stubs_current"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700258 system: "android_system_stubs_current_system_modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700259 classpath: []string{},
260 },
261 {
Colin Cross72bb3632017-07-13 16:23:21 -0700262
Colin Cross2acdae82017-09-15 19:44:24 -0700263 name: "test_current",
264 properties: `sdk_version: "test_current",`,
265 bootclasspath: []string{"android_test_stubs_current"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700266 system: "android_test_stubs_current_system_modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700267 classpath: []string{},
268 },
269 {
Colin Cross72bb3632017-07-13 16:23:21 -0700270
Colin Cross2acdae82017-09-15 19:44:24 -0700271 name: "nostdlib",
Colin Cross1369cdb2017-09-29 17:58:17 -0700272 properties: `no_standard_libs: true, system_modules: "none"`,
273 system: "none",
274 bootclasspath: []string{`""`},
275 classpath: []string{},
276 },
277 {
278
279 name: "nostdlib system_modules",
280 properties: `no_standard_libs: true, system_modules: "core-system-modules"`,
281 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700282 bootclasspath: []string{`""`},
283 classpath: []string{},
284 },
285 {
Colin Cross72bb3632017-07-13 16:23:21 -0700286
Colin Cross2acdae82017-09-15 19:44:24 -0700287 name: "host default",
Colin Cross965714f2017-10-03 20:57:01 -0700288 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700289 properties: ``,
Colin Cross965714f2017-10-03 20:57:01 -0700290 host: android.Host,
Colin Cross2acdae82017-09-15 19:44:24 -0700291 classpath: []string{},
292 },
293 {
294 name: "host nostdlib",
Colin Cross965714f2017-10-03 20:57:01 -0700295 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700296 host: android.Host,
297 properties: `no_standard_libs: true`,
298 classpath: []string{},
299 },
Colin Cross965714f2017-10-03 20:57:01 -0700300 {
301
302 name: "host supported default",
303 host: android.Host,
304 properties: `host_supported: true,`,
305 classpath: []string{},
306 },
307 {
308 name: "host supported nostdlib",
309 host: android.Host,
Colin Cross1369cdb2017-09-29 17:58:17 -0700310 properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
Colin Cross965714f2017-10-03 20:57:01 -0700311 classpath: []string{},
312 },
Colin Cross2acdae82017-09-15 19:44:24 -0700313}
Colin Cross72bb3632017-07-13 16:23:21 -0700314
Colin Cross2acdae82017-09-15 19:44:24 -0700315func TestClasspath(t *testing.T) {
316 for _, testcase := range classpathTestcases {
317 t.Run(testcase.name, func(t *testing.T) {
Colin Cross965714f2017-10-03 20:57:01 -0700318 moduleType := "java_library"
319 if testcase.moduleType != "" {
320 moduleType = testcase.moduleType
Colin Cross2acdae82017-09-15 19:44:24 -0700321 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700322
323 bp := moduleType + ` {
Colin Cross2acdae82017-09-15 19:44:24 -0700324 name: "foo",
325 srcs: ["a.java"],
Colin Cross1369cdb2017-09-29 17:58:17 -0700326 ` + testcase.properties + `
327 }`
328
329 variant := "android_common"
330 if testcase.host == android.Host {
331 variant = android.BuildOs.String() + "_common"
Colin Cross2acdae82017-09-15 19:44:24 -0700332 }
Colin Cross72bb3632017-07-13 16:23:21 -0700333
Colin Cross2acdae82017-09-15 19:44:24 -0700334 convertModulesToPaths := func(cp []string) []string {
335 ret := make([]string, len(cp))
336 for i, e := range cp {
337 ret[i] = moduleToPath(e)
338 }
339 return ret
340 }
Colin Cross72bb3632017-07-13 16:23:21 -0700341
Colin Cross2acdae82017-09-15 19:44:24 -0700342 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
343 classpath := convertModulesToPaths(testcase.classpath)
Colin Cross72bb3632017-07-13 16:23:21 -0700344
Colin Cross2acdae82017-09-15 19:44:24 -0700345 bc := strings.Join(bootclasspath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700346 if bc != "" {
347 bc = "-bootclasspath " + bc
Colin Cross72bb3632017-07-13 16:23:21 -0700348 }
Colin Cross72bb3632017-07-13 16:23:21 -0700349
Colin Cross2acdae82017-09-15 19:44:24 -0700350 c := strings.Join(classpath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700351 if c != "" {
352 c = "-classpath " + c
353 }
354 system := ""
355 if testcase.system == "none" {
356 system = "--system=none"
357 } else if testcase.system != "" {
358 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
Colin Cross2acdae82017-09-15 19:44:24 -0700359 }
360
Colin Cross1369cdb2017-09-29 17:58:17 -0700361 t.Run("1.8", func(t *testing.T) {
362 // Test default javac 1.8
363 ctx := testJava(t, bp)
Colin Cross2acdae82017-09-15 19:44:24 -0700364
Colin Cross1369cdb2017-09-29 17:58:17 -0700365 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
366
367 got := javac.Args["bootClasspath"]
368 if got != bc {
369 t.Errorf("bootclasspath expected %q != got %q", bc, got)
370 }
371
372 got = javac.Args["classpath"]
373 if got != c {
374 t.Errorf("classpath expected %q != got %q", c, got)
375 }
376
377 var deps []string
378 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
379 deps = append(deps, bootclasspath...)
380 }
381 deps = append(deps, classpath...)
382
383 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
384 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
385 }
386 })
387
388 // Test again with javac 1.9
389 t.Run("1.9", func(t *testing.T) {
390 ctx := testJavaWithEnv(t, bp, map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
391
392 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
393 got := javac.Args["bootClasspath"]
394 expected := system
395 if testcase.system == "bootclasspath" {
396 expected = bc
397 }
398 if got != expected {
399 t.Errorf("bootclasspath expected %q != got %q", expected, got)
400 }
401 })
Colin Cross2acdae82017-09-15 19:44:24 -0700402 })
Colin Cross72bb3632017-07-13 16:23:21 -0700403 }
404
Colin Cross72bb3632017-07-13 16:23:21 -0700405}
406
407func TestPrebuilts(t *testing.T) {
408 ctx := testJava(t, `
409 java_library {
410 name: "foo",
411 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700412 libs: ["bar"],
413 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700414 }
415
Colin Cross74d73e22017-08-02 11:05:49 -0700416 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700417 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700418 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700419 }
420
Colin Cross74d73e22017-08-02 11:05:49 -0700421 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700422 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700423 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700424 }
425 `)
426
Colin Cross4c428df2017-09-15 17:36:05 -0700427 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700428 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700429
430 bar := "a.jar"
431 if !strings.Contains(javac.Args["classpath"], bar) {
432 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
433 }
434
Colin Cross0a6e0072017-08-30 14:24:55 -0700435 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" {
436 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, "b.jar")
Colin Cross72bb3632017-07-13 16:23:21 -0700437 }
438}
439
Colin Cross89536d42017-07-07 14:35:50 -0700440func TestDefaults(t *testing.T) {
441 ctx := testJava(t, `
442 java_defaults {
443 name: "defaults",
444 srcs: ["a.java"],
445 libs: ["bar"],
446 static_libs: ["baz"],
447 }
448
449 java_library {
450 name: "foo",
451 defaults: ["defaults"],
452 }
453
454 java_library {
455 name: "bar",
456 srcs: ["b.java"],
457 }
458
459 java_library {
460 name: "baz",
461 srcs: ["c.java"],
462 }
463 `)
464
Colin Cross4c428df2017-09-15 17:36:05 -0700465 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700466 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700467
468 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
469 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
470 }
471
Nan Zhanged19fc32017-10-19 13:06:22 -0700472 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
473 if !strings.Contains(javac.Args["classpath"], barTurbine) {
474 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700475 }
476
Colin Cross1ee23172017-10-18 14:44:18 -0700477 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700478 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
479 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700480 }
481}
482
Colin Cross0f37af02017-09-27 17:42:05 -0700483func TestResources(t *testing.T) {
484 var table = []struct {
485 name string
486 prop string
487 extra string
488 args string
489 }{
490 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700491 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700492 name: "resource dirs",
493 prop: `java_resource_dirs: ["res"]`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700494 args: "-C res -f res/a -f res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700495 },
496 {
497 // Test that a module with java_resources includes the files
498 name: "resource files",
499 prop: `java_resources: ["res/a", "res/b"]`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700500 args: "-C . -f res/a -f res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700501 },
502 {
503 // Test that a module with a filegroup in java_resources includes the files with the
504 // path prefix
505 name: "resource filegroup",
506 prop: `java_resources: [":foo-res"]`,
507 extra: `
508 filegroup {
509 name: "foo-res",
510 path: "res",
511 srcs: ["res/a", "res/b"],
512 }`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700513 args: "-C res -f res/a -f res/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700514 },
515 {
516 // Test that a module with "include_srcs: true" includes its source files in the resources jar
517 name: "include sources",
518 prop: `include_srcs: true`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700519 args: "-C . -f a.java -f b.java -f c.java",
Colin Cross0f37af02017-09-27 17:42:05 -0700520 },
521 }
522
523 for _, test := range table {
524 t.Run(test.name, func(t *testing.T) {
525 ctx := testJava(t, `
526 java_library {
527 name: "foo",
528 srcs: [
529 "a.java",
530 "b.java",
531 "c.java",
532 ],
533 `+test.prop+`,
534 }
535 `+test.extra)
536
Colin Cross1ee23172017-10-18 14:44:18 -0700537 foo := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
538 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700539
540 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
541 t.Errorf("foo combined jars %v does not contain %q",
542 foo.Inputs.Strings(), fooRes.Output.String())
543 }
544
Colin Crossaf9c55b2017-10-03 14:50:08 -0700545 if fooRes.Args["jarArgs"] != test.args {
546 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700547 fooRes.Args["jarArgs"], test.args)
548 }
549 })
550 }
551}
552
Colin Cross0532fb02017-10-02 16:57:40 -0700553func TestExcludeResources(t *testing.T) {
554 ctx := testJava(t, `
555 java_library {
556 name: "foo",
557 srcs: ["a.java"],
558 java_resource_dirs: ["res", "res2"],
559 exclude_java_resource_dirs: ["res2"],
560 }
561
562 java_library {
563 name: "bar",
564 srcs: ["a.java"],
565 java_resources: ["res/*"],
566 exclude_java_resources: ["res/b"],
567 }
568 `)
569
Colin Cross1ee23172017-10-18 14:44:18 -0700570 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700571
Colin Crossaf9c55b2017-10-03 14:50:08 -0700572 expected := "-C res -f res/a -f res/b"
Colin Cross0532fb02017-10-02 16:57:40 -0700573 if fooRes.Args["jarArgs"] != expected {
574 t.Errorf("foo resource jar args %q is not %q",
575 fooRes.Args["jarArgs"], expected)
576
577 }
578
Colin Cross1ee23172017-10-18 14:44:18 -0700579 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700580
581 expected = "-C . -f res/a"
582 if barRes.Args["jarArgs"] != expected {
583 t.Errorf("bar resource jar args %q is not %q",
584 barRes.Args["jarArgs"], expected)
585
586 }
587}
588
Colin Cross54190b32017-10-09 15:34:10 -0700589func TestGeneratedSources(t *testing.T) {
590 ctx := testJava(t, `
591 java_library {
592 name: "foo",
593 srcs: [
594 "a*.java",
595 ":gen",
596 "b*.java",
597 ],
598 }
599
600 genrule {
601 name: "gen",
602 tool_files: ["res/a"],
603 out: ["gen.java"],
604 }
605 `)
606
607 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
608 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
609
610 if len(genrule.Outputs) != 1 || filepath.Base(genrule.Outputs[0].String()) != "gen.java" {
611 t.Fatalf(`gen output file %v is not [".../gen.java"]`, genrule.Outputs.Strings())
612 }
613
614 if len(javac.Inputs) != 3 ||
615 javac.Inputs[0].String() != "a.java" ||
616 javac.Inputs[1].String() != genrule.Outputs[0].String() ||
617 javac.Inputs[2].String() != "b.java" {
618 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
619 }
620}
621
Colin Cross93e85952017-08-15 13:34:18 -0700622func TestKotlin(t *testing.T) {
623 ctx := testJava(t, `
624 java_library {
625 name: "foo",
626 srcs: ["a.java", "b.kt"],
627 }
628 `)
629
630 kotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
631 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Colin Cross1ee23172017-10-18 14:44:18 -0700632 jar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
Colin Cross93e85952017-08-15 13:34:18 -0700633
634 if len(kotlinc.Inputs) != 2 || kotlinc.Inputs[0].String() != "a.java" ||
635 kotlinc.Inputs[1].String() != "b.kt" {
636 t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, kotlinc.Inputs)
637 }
638
639 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
640 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
641 }
642
643 if !strings.Contains(javac.Args["classpath"], kotlinc.Output.String()) {
644 t.Errorf("foo classpath %v does not contain %q",
645 javac.Args["classpath"], kotlinc.Output.String())
646 }
647
648 if !inList(kotlinc.Output.String(), jar.Inputs.Strings()) {
649 t.Errorf("foo jar inputs %v does not contain %q",
650 jar.Inputs.Strings(), kotlinc.Output.String())
651 }
652}
653
Colin Cross72bb3632017-07-13 16:23:21 -0700654func fail(t *testing.T, errs []error) {
655 if len(errs) > 0 {
656 for _, err := range errs {
657 t.Error(err)
658 }
659 t.FailNow()
660 }
661}