blob: 2b92f49e710a89b66f3059522b0945b998712780 [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}
53
54func testJava(t *testing.T, bp string) *android.TestContext {
Colin Cross4c428df2017-09-15 17:36:05 -070055 config := android.TestArchConfig(buildDir)
Colin Cross72bb3632017-07-13 16:23:21 -070056
Colin Cross4c428df2017-09-15 17:36:05 -070057 ctx := android.NewTestArchContext()
Colin Cross72bb3632017-07-13 16:23:21 -070058 ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory))
Colin Crossa60ead82017-10-02 18:10:21 -070059 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory(true)))
Colin Cross2acdae82017-09-15 19:44:24 -070060 ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070061 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross89536d42017-07-07 14:35:50 -070062 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross0f37af02017-09-27 17:42:05 -070063 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(genrule.FileGroupFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070064 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
65 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070066 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Colin Cross72bb3632017-07-13 16:23:21 -070067 ctx.Register()
68
Colin Cross4c428df2017-09-15 17:36:05 -070069 extraModules := []string{
70 "core-oj",
71 "core-libart",
72 "framework",
73 "ext",
74 "okhttp",
Colin Cross2acdae82017-09-15 19:44:24 -070075 "android_stubs_current",
76 "android_system_stubs_current",
77 "android_test_stubs_current",
Colin Cross4c428df2017-09-15 17:36:05 -070078 }
Colin Cross72bb3632017-07-13 16:23:21 -070079
80 for _, extra := range extraModules {
81 bp += fmt.Sprintf(`
82 java_library {
83 name: "%s",
Colin Cross0a6e0072017-08-30 14:24:55 -070084 srcs: ["a.java"],
Colin Cross76b5f0c2017-08-29 16:02:06 -070085 no_standard_libs: true,
Colin Cross72bb3632017-07-13 16:23:21 -070086 }
87 `, extra)
88 }
89
90 ctx.MockFileSystem(map[string][]byte{
Colin Crossfc3674a2017-09-18 17:41:52 -070091 "Android.bp": []byte(bp),
92 "a.java": nil,
93 "b.java": nil,
94 "c.java": nil,
95 "a.jar": nil,
96 "b.jar": nil,
Colin Cross0f37af02017-09-27 17:42:05 -070097 "res/a": nil,
98 "res/b": nil,
Colin Cross0532fb02017-10-02 16:57:40 -070099 "res2/a": nil,
100
Colin Crossfc3674a2017-09-18 17:41:52 -0700101 "prebuilts/sdk/14/android.jar": nil,
102 "prebuilts/sdk/14/framework.aidl": nil,
Colin Cross72bb3632017-07-13 16:23:21 -0700103 })
104
105 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
106 fail(t, errs)
107 _, errs = ctx.PrepareBuildActions(config)
108 fail(t, errs)
109
110 return ctx
111}
112
Colin Cross2acdae82017-09-15 19:44:24 -0700113func moduleToPath(name string) string {
114 switch {
115 case name == `""`:
116 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700117 case strings.HasSuffix(name, ".jar"):
118 return name
Colin Cross2acdae82017-09-15 19:44:24 -0700119 default:
Colin Cross8649b262017-09-27 18:03:17 -0700120 return filepath.Join(buildDir, ".intermediates", name, "android_common", "classes-compiled.jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700121 }
122}
123
Colin Cross72bb3632017-07-13 16:23:21 -0700124func TestSimple(t *testing.T) {
125 ctx := testJava(t, `
126 java_library {
127 name: "foo",
128 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700129 libs: ["bar"],
130 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700131 }
132
133 java_library {
134 name: "bar",
135 srcs: ["b.java"],
136 }
137
138 java_library {
139 name: "baz",
140 srcs: ["c.java"],
141 }
Colin Crossd5934c82017-10-02 13:55:26 -0700142 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700143
Colin Cross4c428df2017-09-15 17:36:05 -0700144 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
145 combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar")
Colin Cross72bb3632017-07-13 16:23:21 -0700146
147 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
148 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
149 }
150
Colin Cross8649b262017-09-27 18:03:17 -0700151 bar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "classes-compiled.jar")
152 baz := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "classes-compiled.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700153
Colin Cross72bb3632017-07-13 16:23:21 -0700154 if !strings.Contains(javac.Args["classpath"], bar) {
155 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
156 }
157
Colin Cross0a6e0072017-08-30 14:24:55 -0700158 if !strings.Contains(javac.Args["classpath"], baz) {
159 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], baz)
160 }
161
162 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
163 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700164 }
165}
166
Colin Crossd5934c82017-10-02 13:55:26 -0700167func TestArchSpecific(t *testing.T) {
168 ctx := testJava(t, `
169 java_library {
170 name: "foo",
171 srcs: ["a.java"],
172 target: {
173 android: {
174 srcs: ["b.java"],
175 },
176 },
177 }
178 `)
179
180 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
181 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
182 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
183 }
184}
185
Colin Cross2acdae82017-09-15 19:44:24 -0700186var classpathTestcases = []struct {
187 name string
188 host android.OsClass
189 properties string
190 bootclasspath []string
191 classpath []string
192}{
193 {
194 name: "default",
195 bootclasspath: []string{"core-oj", "core-libart"},
196 classpath: []string{"ext", "framework", "okhttp"},
197 },
198 {
199 name: "blank sdk version",
200 properties: `sdk_version: "",`,
201 bootclasspath: []string{"core-oj", "core-libart"},
202 classpath: []string{"ext", "framework", "okhttp"},
203 },
204 {
Colin Cross4c428df2017-09-15 17:36:05 -0700205
Colin Cross2acdae82017-09-15 19:44:24 -0700206 name: "sdk v14",
207 properties: `sdk_version: "14",`,
Colin Crossfc3674a2017-09-18 17:41:52 -0700208 bootclasspath: []string{`""`},
209 classpath: []string{"prebuilts/sdk/14/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700210 },
211 {
Colin Cross72bb3632017-07-13 16:23:21 -0700212
Colin Cross2acdae82017-09-15 19:44:24 -0700213 name: "current",
214 properties: `sdk_version: "current",`,
215 bootclasspath: []string{"android_stubs_current"},
216 classpath: []string{},
217 },
218 {
Colin Cross72bb3632017-07-13 16:23:21 -0700219
Colin Cross2acdae82017-09-15 19:44:24 -0700220 name: "system_current",
221 properties: `sdk_version: "system_current",`,
222 bootclasspath: []string{"android_system_stubs_current"},
223 classpath: []string{},
224 },
225 {
Colin Cross72bb3632017-07-13 16:23:21 -0700226
Colin Cross2acdae82017-09-15 19:44:24 -0700227 name: "test_current",
228 properties: `sdk_version: "test_current",`,
229 bootclasspath: []string{"android_test_stubs_current"},
230 classpath: []string{},
231 },
232 {
Colin Cross72bb3632017-07-13 16:23:21 -0700233
Colin Cross2acdae82017-09-15 19:44:24 -0700234 name: "nostdlib",
235 properties: `no_standard_libs: true`,
236 bootclasspath: []string{`""`},
237 classpath: []string{},
238 },
239 {
Colin Cross72bb3632017-07-13 16:23:21 -0700240
Colin Cross2acdae82017-09-15 19:44:24 -0700241 name: "host default",
242 host: android.Host,
243 properties: ``,
244 classpath: []string{},
245 },
246 {
247 name: "host nostdlib",
248 host: android.Host,
249 properties: `no_standard_libs: true`,
250 classpath: []string{},
251 },
252}
Colin Cross72bb3632017-07-13 16:23:21 -0700253
Colin Cross2acdae82017-09-15 19:44:24 -0700254func TestClasspath(t *testing.T) {
255 for _, testcase := range classpathTestcases {
256 t.Run(testcase.name, func(t *testing.T) {
257 hostExtra := ""
258 if testcase.host == android.Host {
259 hostExtra = "_host"
260 }
261 ctx := testJava(t, `
262 java_library`+hostExtra+` {
263 name: "foo",
264 srcs: ["a.java"],
265 `+testcase.properties+`
266 }
267 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700268
Colin Cross2acdae82017-09-15 19:44:24 -0700269 convertModulesToPaths := func(cp []string) []string {
270 ret := make([]string, len(cp))
271 for i, e := range cp {
272 ret[i] = moduleToPath(e)
273 }
274 return ret
275 }
Colin Cross72bb3632017-07-13 16:23:21 -0700276
Colin Cross2acdae82017-09-15 19:44:24 -0700277 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
278 classpath := convertModulesToPaths(testcase.classpath)
Colin Cross72bb3632017-07-13 16:23:21 -0700279
Colin Cross2acdae82017-09-15 19:44:24 -0700280 variant := "android_common"
281 if testcase.host == android.Host {
282 variant = android.BuildOs.String() + "_common"
283 }
284 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
285
Colin Cross72bb3632017-07-13 16:23:21 -0700286 got := strings.TrimPrefix(javac.Args["bootClasspath"], "-bootclasspath ")
Colin Cross2acdae82017-09-15 19:44:24 -0700287 bc := strings.Join(bootclasspath, ":")
288 if got != bc {
289 t.Errorf("bootclasspath expected %q != got %q", bc, got)
Colin Cross72bb3632017-07-13 16:23:21 -0700290 }
Colin Cross72bb3632017-07-13 16:23:21 -0700291
Colin Cross2acdae82017-09-15 19:44:24 -0700292 got = strings.TrimPrefix(javac.Args["classpath"], "-classpath ")
293 c := strings.Join(classpath, ":")
294 if got != c {
295 t.Errorf("classpath expected %q != got %q", c, got)
296 }
297
298 var deps []string
299 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
300 deps = append(deps, bootclasspath...)
301 }
302 deps = append(deps, classpath...)
303
304 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
305 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
306 }
307 })
Colin Cross72bb3632017-07-13 16:23:21 -0700308 }
309
Colin Cross72bb3632017-07-13 16:23:21 -0700310}
311
312func TestPrebuilts(t *testing.T) {
313 ctx := testJava(t, `
314 java_library {
315 name: "foo",
316 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700317 libs: ["bar"],
318 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700319 }
320
Colin Cross74d73e22017-08-02 11:05:49 -0700321 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700322 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700323 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700324 }
325
Colin Cross74d73e22017-08-02 11:05:49 -0700326 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700327 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700328 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700329 }
330 `)
331
Colin Cross4c428df2017-09-15 17:36:05 -0700332 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
333 combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar")
Colin Cross72bb3632017-07-13 16:23:21 -0700334
335 bar := "a.jar"
336 if !strings.Contains(javac.Args["classpath"], bar) {
337 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
338 }
339
Colin Cross0a6e0072017-08-30 14:24:55 -0700340 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" {
341 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, "b.jar")
Colin Cross72bb3632017-07-13 16:23:21 -0700342 }
343}
344
Colin Cross89536d42017-07-07 14:35:50 -0700345func TestDefaults(t *testing.T) {
346 ctx := testJava(t, `
347 java_defaults {
348 name: "defaults",
349 srcs: ["a.java"],
350 libs: ["bar"],
351 static_libs: ["baz"],
352 }
353
354 java_library {
355 name: "foo",
356 defaults: ["defaults"],
357 }
358
359 java_library {
360 name: "bar",
361 srcs: ["b.java"],
362 }
363
364 java_library {
365 name: "baz",
366 srcs: ["c.java"],
367 }
368 `)
369
Colin Cross4c428df2017-09-15 17:36:05 -0700370 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
371 combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar")
Colin Cross89536d42017-07-07 14:35:50 -0700372
373 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
374 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
375 }
376
Colin Cross8649b262017-09-27 18:03:17 -0700377 bar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "classes-compiled.jar")
Colin Cross89536d42017-07-07 14:35:50 -0700378 if !strings.Contains(javac.Args["classpath"], bar) {
379 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
380 }
381
Colin Cross8649b262017-09-27 18:03:17 -0700382 baz := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "classes-compiled.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700383 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
384 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700385 }
386}
387
Colin Cross0f37af02017-09-27 17:42:05 -0700388func TestResources(t *testing.T) {
389 var table = []struct {
390 name string
391 prop string
392 extra string
393 args string
394 }{
395 {
396 // Test that a module with java_resource_dirs includes a file list file
397 name: "resource dirs",
398 prop: `java_resource_dirs: ["res"]`,
399 args: "-C res -l ",
400 },
401 {
402 // Test that a module with java_resources includes the files
403 name: "resource files",
404 prop: `java_resources: ["res/a", "res/b"]`,
405 args: "-C . -f res/a -C . -f res/b",
406 },
407 {
408 // Test that a module with a filegroup in java_resources includes the files with the
409 // path prefix
410 name: "resource filegroup",
411 prop: `java_resources: [":foo-res"]`,
412 extra: `
413 filegroup {
414 name: "foo-res",
415 path: "res",
416 srcs: ["res/a", "res/b"],
417 }`,
418 args: "-C res -f res/a -C res -f res/b",
419 },
420 {
421 // Test that a module with "include_srcs: true" includes its source files in the resources jar
422 name: "include sources",
423 prop: `include_srcs: true`,
424 args: "-C . -f a.java -C . -f b.java -C . -f c.java",
425 },
426 }
427
428 for _, test := range table {
429 t.Run(test.name, func(t *testing.T) {
430 ctx := testJava(t, `
431 java_library {
432 name: "foo",
433 srcs: [
434 "a.java",
435 "b.java",
436 "c.java",
437 ],
438 `+test.prop+`,
439 }
440 `+test.extra)
441
442 foo := ctx.ModuleForTests("foo", "android_common").Output("classes.jar")
443 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res.jar")
444
445 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
446 t.Errorf("foo combined jars %v does not contain %q",
447 foo.Inputs.Strings(), fooRes.Output.String())
448 }
449
450 if !strings.Contains(fooRes.Args["jarArgs"], test.args) {
451 t.Errorf("foo resource jar args %q does not contain %q",
452 fooRes.Args["jarArgs"], test.args)
453 }
454 })
455 }
456}
457
Colin Cross0532fb02017-10-02 16:57:40 -0700458func TestExcludeResources(t *testing.T) {
459 ctx := testJava(t, `
460 java_library {
461 name: "foo",
462 srcs: ["a.java"],
463 java_resource_dirs: ["res", "res2"],
464 exclude_java_resource_dirs: ["res2"],
465 }
466
467 java_library {
468 name: "bar",
469 srcs: ["a.java"],
470 java_resources: ["res/*"],
471 exclude_java_resources: ["res/b"],
472 }
473 `)
474
475 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res.jar")
476
477 expected := "-C res -l " + fooRes.Implicits[0].String()
478 if fooRes.Args["jarArgs"] != expected {
479 t.Errorf("foo resource jar args %q is not %q",
480 fooRes.Args["jarArgs"], expected)
481
482 }
483
484 barRes := ctx.ModuleForTests("bar", "android_common").Output("res.jar")
485
486 expected = "-C . -f res/a"
487 if barRes.Args["jarArgs"] != expected {
488 t.Errorf("bar resource jar args %q is not %q",
489 barRes.Args["jarArgs"], expected)
490
491 }
492}
493
Colin Cross72bb3632017-07-13 16:23:21 -0700494func fail(t *testing.T, errs []error) {
495 if len(errs) > 0 {
496 for _, err := range errs {
497 t.Error(err)
498 }
499 t.FailNow()
500 }
501}