blob: 64b6bf8d3eea270343281957421a9b7300e5d29e [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"
Nan Zhang61eaedb2017-11-02 13:28:15 -070025 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070026 "strings"
27 "testing"
28)
29
30var buildDir string
31
32func setUp() {
33 var err error
34 buildDir, err = ioutil.TempDir("", "soong_java_test")
35 if err != nil {
36 panic(err)
37 }
38}
39
40func tearDown() {
41 os.RemoveAll(buildDir)
42}
43
44func TestMain(m *testing.M) {
45 run := func() int {
46 setUp()
47 defer tearDown()
48
49 return m.Run()
50 }
51
52 os.Exit(run())
53}
Colin Cross527012a2017-11-30 22:56:16 -080054
55func testConfig(env map[string]string) android.Config {
Colin Cross7fdd2b72018-01-02 18:14:25 -080056 if env == nil {
57 env = make(map[string]string)
58 }
59 if env["ANDROID_JAVA8_HOME"] == "" {
60 env["ANDROID_JAVA8_HOME"] = "jdk8"
61 }
Jiyong Park1a5d7b12018-01-15 15:05:10 +090062 config := android.TestArchConfig(buildDir, env)
63 config.ProductVariables.DeviceSystemSdkVersions = &[]string{"14", "15"}
64 return config
Colin Cross527012a2017-11-30 22:56:16 -080065
Colin Cross1369cdb2017-09-29 17:58:17 -070066}
67
Colin Cross527012a2017-11-30 22:56:16 -080068func testContext(config android.Config, bp string,
69 fs map[string][]byte) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070070
Colin Cross4c428df2017-09-15 17:36:05 -070071 ctx := android.NewTestArchContext()
Colin Cross72bb3632017-07-13 16:23:21 -070072 ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory))
Colin Cross6b4a32d2017-12-05 13:42:45 -080073 ctx.RegisterModuleType("java_binary_host", android.ModuleFactoryAdaptor(BinaryHostFactory))
Colin Crossa60ead82017-10-02 18:10:21 -070074 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory(true)))
Colin Cross2acdae82017-09-15 19:44:24 -070075 ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070076 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross89536d42017-07-07 14:35:50 -070077 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross1369cdb2017-09-29 17:58:17 -070078 ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory))
Colin Cross54250902017-12-05 09:28:08 -080079 ctx.RegisterModuleType("java_genrule", android.ModuleFactoryAdaptor(genRuleFactory))
Colin Cross0f37af02017-09-27 17:42:05 -070080 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(genrule.FileGroupFactory))
Colin Cross54190b32017-10-09 15:34:10 -070081 ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory))
Nan Zhang581fd212018-01-10 16:06:12 -080082 ctx.RegisterModuleType("droiddoc", android.ModuleFactoryAdaptor(DroiddocFactory))
83 ctx.RegisterModuleType("droiddoc_host", android.ModuleFactoryAdaptor(DroiddocHostFactory))
Dan Willemsencc090972018-02-26 14:33:31 -080084 ctx.RegisterModuleType("droiddoc_template", android.ModuleFactoryAdaptor(DroiddocTemplateFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070085 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
86 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070087 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080088 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross72bb3632017-07-13 16:23:21 -070089 ctx.Register()
90
Colin Cross4c428df2017-09-15 17:36:05 -070091 extraModules := []string{
92 "core-oj",
93 "core-libart",
94 "framework",
95 "ext",
96 "okhttp",
Colin Cross2acdae82017-09-15 19:44:24 -070097 "android_stubs_current",
98 "android_system_stubs_current",
99 "android_test_stubs_current",
Colin Crossf19b9bb2018-03-26 14:42:44 -0700100 "core.current.stubs",
Colin Cross93e85952017-08-15 13:34:18 -0700101 "kotlin-stdlib",
Colin Cross4c428df2017-09-15 17:36:05 -0700102 }
Colin Cross72bb3632017-07-13 16:23:21 -0700103
104 for _, extra := range extraModules {
105 bp += fmt.Sprintf(`
106 java_library {
107 name: "%s",
Colin Cross0a6e0072017-08-30 14:24:55 -0700108 srcs: ["a.java"],
Colin Cross76b5f0c2017-08-29 16:02:06 -0700109 no_standard_libs: true,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700110 sdk_version: "core_current",
Colin Cross1369cdb2017-09-29 17:58:17 -0700111 system_modules: "core-system-modules",
Colin Cross72bb3632017-07-13 16:23:21 -0700112 }
113 `, extra)
114 }
115
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800116 bp += `
117 android_app {
118 name: "framework-res",
119 no_framework_libs: true,
120 }
121 `
122
Colin Cross527012a2017-11-30 22:56:16 -0800123 systemModules := []string{
124 "core-system-modules",
125 "android_stubs_current_system_modules",
126 "android_system_stubs_current_system_modules",
127 "android_test_stubs_current_system_modules",
128 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700129
Colin Cross527012a2017-11-30 22:56:16 -0800130 for _, extra := range systemModules {
131 bp += fmt.Sprintf(`
Colin Cross1369cdb2017-09-29 17:58:17 -0700132 java_system_modules {
133 name: "%s",
134 }
135 `, extra)
Colin Cross1369cdb2017-09-29 17:58:17 -0700136 }
137
Colin Cross824bee32017-11-22 17:27:51 -0800138 mockFS := map[string][]byte{
Nan Zhang27e284d2018-02-09 21:03:53 +0000139 "Android.bp": []byte(bp),
140 "a.java": nil,
141 "b.java": nil,
142 "c.java": nil,
143 "b.kt": nil,
144 "a.jar": nil,
145 "b.jar": nil,
Colin Cross0ead1d72018-04-10 13:07:42 -0700146 "java-res/a/a": nil,
147 "java-res/b/b": nil,
Nan Zhang27e284d2018-02-09 21:03:53 +0000148 "java-res2/a": nil,
149 "java-fg/a.java": nil,
150 "java-fg/b.java": nil,
151 "java-fg/c.java": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700152
Colin Cross2ebc4762017-10-20 14:00:31 -0700153 "prebuilts/sdk/14/android.jar": nil,
154 "prebuilts/sdk/14/framework.aidl": nil,
155 "prebuilts/sdk/current/android.jar": nil,
156 "prebuilts/sdk/current/framework.aidl": nil,
Jiyong Park750e5572018-01-31 00:20:13 +0900157 "prebuilts/sdk/current/core.jar": nil,
Colin Cross2ebc4762017-10-20 14:00:31 -0700158 "prebuilts/sdk/system_current/android.jar": nil,
159 "prebuilts/sdk/system_current/framework.aidl": nil,
Sundong Ahn0926fae2017-10-17 16:34:51 +0900160 "prebuilts/sdk/system_14/android.jar": nil,
161 "prebuilts/sdk/system_14/framework.aidl": nil,
Colin Cross2ebc4762017-10-20 14:00:31 -0700162 "prebuilts/sdk/test_current/android.jar": nil,
163 "prebuilts/sdk/test_current/framework.aidl": nil,
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800164
165 // For framework-res, which is an implicit dependency for framework
166 "AndroidManifest.xml": nil,
167 "build/target/product/security/testkey": nil,
Colin Cross6b4a32d2017-12-05 13:42:45 -0800168
169 "build/soong/scripts/jar-wrapper.sh": nil,
Colin Cross7fdd2b72018-01-02 18:14:25 -0800170
Colin Cross66dbc0b2017-12-28 12:23:20 -0800171 "build/make/core/proguard.flags": nil,
172 "build/make/core/proguard_basic_keeps.flags": nil,
173
Colin Cross7fdd2b72018-01-02 18:14:25 -0800174 "jdk8/jre/lib/jce.jar": nil,
175 "jdk8/jre/lib/rt.jar": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800176
177 "bar-doc/a.java": nil,
178 "bar-doc/b.java": nil,
179 "bar-doc/known_oj_tags.txt": nil,
180 "external/doclava/templates-sdk": nil,
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000181
182 "external/kotlinc/jarjar-rules.txt": nil,
Colin Cross824bee32017-11-22 17:27:51 -0800183 }
184
185 for k, v := range fs {
186 mockFS[k] = v
187 }
188
189 ctx.MockFileSystem(mockFS)
Colin Cross72bb3632017-07-13 16:23:21 -0700190
Colin Cross527012a2017-11-30 22:56:16 -0800191 return ctx
192}
193
194func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800195 t.Helper()
Jeff Gastond3e141d2017-08-08 17:46:01 -0700196 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +0800197 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700198 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800199 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800200}
201
202func testJava(t *testing.T, bp string) *android.TestContext {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800203 t.Helper()
Colin Cross527012a2017-11-30 22:56:16 -0800204 config := testConfig(nil)
205 ctx := testContext(config, bp, nil)
206 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700207
208 return ctx
209}
210
Colin Cross2acdae82017-09-15 19:44:24 -0700211func moduleToPath(name string) string {
212 switch {
213 case name == `""`:
214 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700215 case strings.HasSuffix(name, ".jar"):
216 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700217 default:
218 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700219 }
220}
221
Colin Cross72bb3632017-07-13 16:23:21 -0700222func TestSimple(t *testing.T) {
223 ctx := testJava(t, `
224 java_library {
225 name: "foo",
226 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700227 libs: ["bar"],
228 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700229 }
230
231 java_library {
232 name: "bar",
233 srcs: ["b.java"],
234 }
235
236 java_library {
237 name: "baz",
238 srcs: ["c.java"],
239 }
Colin Crossd5934c82017-10-02 13:55:26 -0700240 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700241
Colin Cross4c428df2017-09-15 17:36:05 -0700242 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700243 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700244
245 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
246 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
247 }
248
Colin Cross1ee23172017-10-18 14:44:18 -0700249 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700250 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
251 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700252
Nan Zhanged19fc32017-10-19 13:06:22 -0700253 if !strings.Contains(javac.Args["classpath"], barTurbine) {
254 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700255 }
256
Nan Zhanged19fc32017-10-19 13:06:22 -0700257 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
258 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700259 }
260
261 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
262 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700263 }
264}
265
Colin Crossd5934c82017-10-02 13:55:26 -0700266func TestArchSpecific(t *testing.T) {
267 ctx := testJava(t, `
268 java_library {
269 name: "foo",
270 srcs: ["a.java"],
271 target: {
272 android: {
273 srcs: ["b.java"],
274 },
275 },
276 }
277 `)
278
279 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
280 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
281 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
282 }
283}
284
Colin Cross6b4a32d2017-12-05 13:42:45 -0800285func TestBinary(t *testing.T) {
286 ctx := testJava(t, `
287 java_library_host {
288 name: "foo",
289 srcs: ["a.java"],
290 }
291
292 java_binary_host {
293 name: "bar",
294 srcs: ["b.java"],
295 static_libs: ["foo"],
296 }
297 `)
298
299 buildOS := android.BuildOs.String()
300
301 bar := ctx.ModuleForTests("bar", buildOS+"_common")
302 barJar := bar.Output("bar.jar").Output.String()
303 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
304 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
305
306 // Test that the install binary wrapper depends on the installed jar file
307 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
308 t.Errorf("expected binary wrapper implicits [%q], got %v",
309 barJar, barWrapperDeps)
310 }
311
312}
313
Colin Cross2acdae82017-09-15 19:44:24 -0700314var classpathTestcases = []struct {
315 name string
Colin Cross965714f2017-10-03 20:57:01 -0700316 moduleType string
Colin Cross2acdae82017-09-15 19:44:24 -0700317 host android.OsClass
318 properties string
319 bootclasspath []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700320 system string
Colin Cross2acdae82017-09-15 19:44:24 -0700321 classpath []string
322}{
323 {
324 name: "default",
325 bootclasspath: []string{"core-oj", "core-libart"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700326 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700327 classpath: []string{"ext", "framework", "okhttp"},
328 },
329 {
330 name: "blank sdk version",
331 properties: `sdk_version: "",`,
332 bootclasspath: []string{"core-oj", "core-libart"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700333 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700334 classpath: []string{"ext", "framework", "okhttp"},
335 },
336 {
Colin Cross4c428df2017-09-15 17:36:05 -0700337
Colin Cross2acdae82017-09-15 19:44:24 -0700338 name: "sdk v14",
339 properties: `sdk_version: "14",`,
Colin Crossfc3674a2017-09-18 17:41:52 -0700340 bootclasspath: []string{`""`},
Colin Cross1369cdb2017-09-29 17:58:17 -0700341 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossfc3674a2017-09-18 17:41:52 -0700342 classpath: []string{"prebuilts/sdk/14/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700343 },
344 {
Colin Cross72bb3632017-07-13 16:23:21 -0700345
Colin Cross2acdae82017-09-15 19:44:24 -0700346 name: "current",
347 properties: `sdk_version: "current",`,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700348 bootclasspath: []string{"android_stubs_current"},
Colin Cross2ebc4762017-10-20 14:00:31 -0700349 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross2acdae82017-09-15 19:44:24 -0700350 },
351 {
Colin Cross72bb3632017-07-13 16:23:21 -0700352
Colin Cross2acdae82017-09-15 19:44:24 -0700353 name: "system_current",
354 properties: `sdk_version: "system_current",`,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700355 bootclasspath: []string{"android_system_stubs_current"},
Colin Cross2ebc4762017-10-20 14:00:31 -0700356 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross2acdae82017-09-15 19:44:24 -0700357 },
358 {
Colin Cross72bb3632017-07-13 16:23:21 -0700359
Sundong Ahn0926fae2017-10-17 16:34:51 +0900360 name: "system_14",
361 properties: `sdk_version: "system_14",`,
362 bootclasspath: []string{`""`},
363 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
364 classpath: []string{"prebuilts/sdk/system_14/android.jar"},
365 },
366 {
367
Colin Cross2acdae82017-09-15 19:44:24 -0700368 name: "test_current",
369 properties: `sdk_version: "test_current",`,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700370 bootclasspath: []string{"android_test_stubs_current"},
Colin Cross2ebc4762017-10-20 14:00:31 -0700371 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross2acdae82017-09-15 19:44:24 -0700372 },
373 {
Colin Cross72bb3632017-07-13 16:23:21 -0700374
Jiyong Park750e5572018-01-31 00:20:13 +0900375 name: "core_current",
376 properties: `sdk_version: "core_current",`,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700377 bootclasspath: []string{"core.current.stubs"},
Jiyong Park750e5572018-01-31 00:20:13 +0900378 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Jiyong Park750e5572018-01-31 00:20:13 +0900379 },
380 {
381
Colin Cross2acdae82017-09-15 19:44:24 -0700382 name: "nostdlib",
Colin Cross1369cdb2017-09-29 17:58:17 -0700383 properties: `no_standard_libs: true, system_modules: "none"`,
384 system: "none",
385 bootclasspath: []string{`""`},
386 classpath: []string{},
387 },
388 {
389
390 name: "nostdlib system_modules",
391 properties: `no_standard_libs: true, system_modules: "core-system-modules"`,
392 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700393 bootclasspath: []string{`""`},
394 classpath: []string{},
395 },
396 {
Colin Cross72bb3632017-07-13 16:23:21 -0700397
Colin Cross7fdd2b72018-01-02 18:14:25 -0800398 name: "host default",
399 moduleType: "java_library_host",
400 properties: ``,
401 host: android.Host,
402 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
403 classpath: []string{},
Colin Cross2acdae82017-09-15 19:44:24 -0700404 },
405 {
406 name: "host nostdlib",
Colin Cross965714f2017-10-03 20:57:01 -0700407 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700408 host: android.Host,
409 properties: `no_standard_libs: true`,
410 classpath: []string{},
411 },
Colin Cross965714f2017-10-03 20:57:01 -0700412 {
413
Colin Cross7fdd2b72018-01-02 18:14:25 -0800414 name: "host supported default",
415 host: android.Host,
416 properties: `host_supported: true,`,
417 classpath: []string{},
418 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
Colin Cross965714f2017-10-03 20:57:01 -0700419 },
420 {
421 name: "host supported nostdlib",
422 host: android.Host,
Colin Cross1369cdb2017-09-29 17:58:17 -0700423 properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
Colin Cross965714f2017-10-03 20:57:01 -0700424 classpath: []string{},
425 },
Colin Cross2acdae82017-09-15 19:44:24 -0700426}
Colin Cross72bb3632017-07-13 16:23:21 -0700427
Colin Cross2acdae82017-09-15 19:44:24 -0700428func TestClasspath(t *testing.T) {
429 for _, testcase := range classpathTestcases {
430 t.Run(testcase.name, func(t *testing.T) {
Colin Cross965714f2017-10-03 20:57:01 -0700431 moduleType := "java_library"
432 if testcase.moduleType != "" {
433 moduleType = testcase.moduleType
Colin Cross2acdae82017-09-15 19:44:24 -0700434 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700435
436 bp := moduleType + ` {
Colin Cross2acdae82017-09-15 19:44:24 -0700437 name: "foo",
438 srcs: ["a.java"],
Colin Cross1369cdb2017-09-29 17:58:17 -0700439 ` + testcase.properties + `
440 }`
441
442 variant := "android_common"
443 if testcase.host == android.Host {
444 variant = android.BuildOs.String() + "_common"
Colin Cross2acdae82017-09-15 19:44:24 -0700445 }
Colin Cross72bb3632017-07-13 16:23:21 -0700446
Colin Cross2acdae82017-09-15 19:44:24 -0700447 convertModulesToPaths := func(cp []string) []string {
448 ret := make([]string, len(cp))
449 for i, e := range cp {
450 ret[i] = moduleToPath(e)
451 }
452 return ret
453 }
Colin Cross72bb3632017-07-13 16:23:21 -0700454
Colin Cross2acdae82017-09-15 19:44:24 -0700455 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
456 classpath := convertModulesToPaths(testcase.classpath)
Colin Cross72bb3632017-07-13 16:23:21 -0700457
Colin Cross2acdae82017-09-15 19:44:24 -0700458 bc := strings.Join(bootclasspath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700459 if bc != "" {
460 bc = "-bootclasspath " + bc
Colin Cross72bb3632017-07-13 16:23:21 -0700461 }
Colin Cross72bb3632017-07-13 16:23:21 -0700462
Colin Cross2acdae82017-09-15 19:44:24 -0700463 c := strings.Join(classpath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700464 if c != "" {
465 c = "-classpath " + c
466 }
467 system := ""
468 if testcase.system == "none" {
469 system = "--system=none"
470 } else if testcase.system != "" {
471 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
Colin Cross2acdae82017-09-15 19:44:24 -0700472 }
473
Colin Cross1369cdb2017-09-29 17:58:17 -0700474 t.Run("1.8", func(t *testing.T) {
475 // Test default javac 1.8
476 ctx := testJava(t, bp)
Colin Cross2acdae82017-09-15 19:44:24 -0700477
Colin Cross1369cdb2017-09-29 17:58:17 -0700478 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
479
480 got := javac.Args["bootClasspath"]
481 if got != bc {
482 t.Errorf("bootclasspath expected %q != got %q", bc, got)
483 }
484
485 got = javac.Args["classpath"]
486 if got != c {
487 t.Errorf("classpath expected %q != got %q", c, got)
488 }
489
490 var deps []string
491 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
492 deps = append(deps, bootclasspath...)
493 }
494 deps = append(deps, classpath...)
495
496 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
497 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
498 }
499 })
500
501 // Test again with javac 1.9
502 t.Run("1.9", func(t *testing.T) {
Colin Cross527012a2017-11-30 22:56:16 -0800503 config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
504 ctx := testContext(config, bp, nil)
505 run(t, ctx, config)
Colin Cross1369cdb2017-09-29 17:58:17 -0700506
507 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
508 got := javac.Args["bootClasspath"]
509 expected := system
510 if testcase.system == "bootclasspath" {
511 expected = bc
512 }
513 if got != expected {
514 t.Errorf("bootclasspath expected %q != got %q", expected, got)
515 }
516 })
Colin Cross2acdae82017-09-15 19:44:24 -0700517 })
Colin Cross72bb3632017-07-13 16:23:21 -0700518 }
519
Colin Cross72bb3632017-07-13 16:23:21 -0700520}
521
522func TestPrebuilts(t *testing.T) {
523 ctx := testJava(t, `
524 java_library {
525 name: "foo",
526 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700527 libs: ["bar"],
528 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700529 }
530
Colin Cross74d73e22017-08-02 11:05:49 -0700531 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700532 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700533 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700534 }
535
Colin Cross74d73e22017-08-02 11:05:49 -0700536 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700537 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700538 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700539 }
540 `)
541
Colin Cross4c428df2017-09-15 17:36:05 -0700542 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700543 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700544
545 bar := "a.jar"
546 if !strings.Contains(javac.Args["classpath"], bar) {
547 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
548 }
549
Colin Cross0a6e0072017-08-30 14:24:55 -0700550 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" {
551 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, "b.jar")
Colin Cross72bb3632017-07-13 16:23:21 -0700552 }
553}
554
Colin Cross89536d42017-07-07 14:35:50 -0700555func TestDefaults(t *testing.T) {
556 ctx := testJava(t, `
557 java_defaults {
558 name: "defaults",
559 srcs: ["a.java"],
560 libs: ["bar"],
561 static_libs: ["baz"],
562 }
563
564 java_library {
565 name: "foo",
566 defaults: ["defaults"],
567 }
568
569 java_library {
570 name: "bar",
571 srcs: ["b.java"],
572 }
573
574 java_library {
575 name: "baz",
576 srcs: ["c.java"],
577 }
578 `)
579
Colin Cross4c428df2017-09-15 17:36:05 -0700580 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700581 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700582
583 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
584 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
585 }
586
Nan Zhanged19fc32017-10-19 13:06:22 -0700587 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
588 if !strings.Contains(javac.Args["classpath"], barTurbine) {
589 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700590 }
591
Colin Cross1ee23172017-10-18 14:44:18 -0700592 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700593 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
594 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700595 }
596}
597
Colin Cross0f37af02017-09-27 17:42:05 -0700598func TestResources(t *testing.T) {
599 var table = []struct {
600 name string
601 prop string
602 extra string
603 args string
604 }{
605 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700606 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700607 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800608 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700609 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700610 },
611 {
612 // Test that a module with java_resources includes the files
613 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700614 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
615 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700616 },
617 {
618 // Test that a module with a filegroup in java_resources includes the files with the
619 // path prefix
620 name: "resource filegroup",
621 prop: `java_resources: [":foo-res"]`,
622 extra: `
623 filegroup {
624 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800625 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700626 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700627 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700628 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700629 },
630 {
631 // Test that a module with "include_srcs: true" includes its source files in the resources jar
632 name: "include sources",
633 prop: `include_srcs: true`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700634 args: "-C . -f a.java -f b.java -f c.java",
Colin Cross0f37af02017-09-27 17:42:05 -0700635 },
Colin Cross0ead1d72018-04-10 13:07:42 -0700636 {
637 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
638 name: "wildcard dirs",
639 prop: `java_resource_dirs: ["java-res/*"]`,
640 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
641 },
642 {
643 // Test that a module exclude_java_resource_dirs excludes the files
644 name: "wildcard dirs",
645 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
646 args: "-C java-res/a -f java-res/a/a",
647 },
Colin Cross0f37af02017-09-27 17:42:05 -0700648 }
649
650 for _, test := range table {
651 t.Run(test.name, func(t *testing.T) {
652 ctx := testJava(t, `
653 java_library {
654 name: "foo",
655 srcs: [
656 "a.java",
657 "b.java",
658 "c.java",
659 ],
660 `+test.prop+`,
661 }
662 `+test.extra)
663
Colin Cross1ee23172017-10-18 14:44:18 -0700664 foo := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
665 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700666
667 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
668 t.Errorf("foo combined jars %v does not contain %q",
669 foo.Inputs.Strings(), fooRes.Output.String())
670 }
671
Colin Crossaf9c55b2017-10-03 14:50:08 -0700672 if fooRes.Args["jarArgs"] != test.args {
673 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700674 fooRes.Args["jarArgs"], test.args)
675 }
676 })
677 }
678}
679
Colin Cross0532fb02017-10-02 16:57:40 -0700680func TestExcludeResources(t *testing.T) {
681 ctx := testJava(t, `
682 java_library {
683 name: "foo",
684 srcs: ["a.java"],
Colin Cross824bee32017-11-22 17:27:51 -0800685 java_resource_dirs: ["java-res", "java-res2"],
686 exclude_java_resource_dirs: ["java-res2"],
Colin Cross0532fb02017-10-02 16:57:40 -0700687 }
688
689 java_library {
690 name: "bar",
691 srcs: ["a.java"],
Colin Cross0ead1d72018-04-10 13:07:42 -0700692 java_resources: ["java-res/*/*"],
693 exclude_java_resources: ["java-res/b/*"],
Colin Cross0532fb02017-10-02 16:57:40 -0700694 }
695 `)
696
Colin Cross1ee23172017-10-18 14:44:18 -0700697 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700698
Colin Cross0ead1d72018-04-10 13:07:42 -0700699 expected := "-C java-res -f java-res/a/a -f java-res/b/b"
Colin Cross0532fb02017-10-02 16:57:40 -0700700 if fooRes.Args["jarArgs"] != expected {
701 t.Errorf("foo resource jar args %q is not %q",
702 fooRes.Args["jarArgs"], expected)
703
704 }
705
Colin Cross1ee23172017-10-18 14:44:18 -0700706 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700707
Colin Cross0ead1d72018-04-10 13:07:42 -0700708 expected = "-C . -f java-res/a/a"
Colin Cross0532fb02017-10-02 16:57:40 -0700709 if barRes.Args["jarArgs"] != expected {
710 t.Errorf("bar resource jar args %q is not %q",
711 barRes.Args["jarArgs"], expected)
712
713 }
714}
715
Colin Cross54190b32017-10-09 15:34:10 -0700716func TestGeneratedSources(t *testing.T) {
717 ctx := testJava(t, `
718 java_library {
719 name: "foo",
720 srcs: [
721 "a*.java",
722 ":gen",
723 "b*.java",
724 ],
725 }
726
727 genrule {
728 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800729 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700730 out: ["gen.java"],
731 }
732 `)
733
734 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
735 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
736
Colin Cross15e86d92017-10-20 15:07:08 -0700737 if filepath.Base(genrule.Output.String()) != "gen.java" {
738 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700739 }
740
741 if len(javac.Inputs) != 3 ||
742 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700743 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700744 javac.Inputs[2].String() != "b.java" {
745 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
746 }
747}
748
Colin Cross93e85952017-08-15 13:34:18 -0700749func TestKotlin(t *testing.T) {
750 ctx := testJava(t, `
751 java_library {
752 name: "foo",
Colin Cross9bc43432017-12-15 20:20:39 -0800753 srcs: ["a.java", "b.kt"],
Colin Cross93e85952017-08-15 13:34:18 -0700754 }
Colin Cross715d7112017-10-25 20:32:18 -0700755
756 java_library {
757 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800758 srcs: ["b.kt"],
759 libs: ["foo"],
760 static_libs: ["baz"],
761 }
762
763 java_library {
764 name: "baz",
765 srcs: ["c.java"],
Colin Cross715d7112017-10-25 20:32:18 -0700766 }
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000767
768 java_library {
769 name: "blorg",
770 renamed_kotlin_stdlib: true,
771 srcs: ["b.kt"],
772 }
Colin Cross93e85952017-08-15 13:34:18 -0700773 `)
774
Colin Cross9bc43432017-12-15 20:20:39 -0800775 fooKotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
776 fooJavac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
777 fooJar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
Colin Cross93e85952017-08-15 13:34:18 -0700778
Colin Cross9bc43432017-12-15 20:20:39 -0800779 if len(fooKotlinc.Inputs) != 2 || fooKotlinc.Inputs[0].String() != "a.java" ||
780 fooKotlinc.Inputs[1].String() != "b.kt" {
781 t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, fooKotlinc.Inputs)
Colin Cross93e85952017-08-15 13:34:18 -0700782 }
783
Colin Cross9bc43432017-12-15 20:20:39 -0800784 if len(fooJavac.Inputs) != 1 || fooJavac.Inputs[0].String() != "a.java" {
785 t.Errorf(`foo inputs %v != ["a.java"]`, fooJavac.Inputs)
Colin Cross93e85952017-08-15 13:34:18 -0700786 }
787
Colin Cross9bc43432017-12-15 20:20:39 -0800788 if !strings.Contains(fooJavac.Args["classpath"], fooKotlinc.Output.String()) {
Colin Cross93e85952017-08-15 13:34:18 -0700789 t.Errorf("foo classpath %v does not contain %q",
Colin Cross9bc43432017-12-15 20:20:39 -0800790 fooJavac.Args["classpath"], fooKotlinc.Output.String())
Colin Cross93e85952017-08-15 13:34:18 -0700791 }
792
Colin Cross9bc43432017-12-15 20:20:39 -0800793 if !inList(fooKotlinc.Output.String(), fooJar.Inputs.Strings()) {
Colin Cross93e85952017-08-15 13:34:18 -0700794 t.Errorf("foo jar inputs %v does not contain %q",
Colin Cross9bc43432017-12-15 20:20:39 -0800795 fooJar.Inputs.Strings(), fooKotlinc.Output.String())
Colin Cross93e85952017-08-15 13:34:18 -0700796 }
Colin Cross715d7112017-10-25 20:32:18 -0700797
Colin Cross9bc43432017-12-15 20:20:39 -0800798 fooHeaderJar := ctx.ModuleForTests("foo", "android_common").Output("turbine-combined/foo.jar")
799 bazHeaderJar := ctx.ModuleForTests("baz", "android_common").Output("turbine-combined/baz.jar")
800 barKotlinc := ctx.ModuleForTests("bar", "android_common").Rule("kotlinc")
Colin Cross715d7112017-10-25 20:32:18 -0700801
Colin Cross9bc43432017-12-15 20:20:39 -0800802 if len(barKotlinc.Inputs) != 1 || barKotlinc.Inputs[0].String() != "b.kt" {
803 t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, barKotlinc.Inputs)
804 }
805
806 if !inList(fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
807 t.Errorf(`expected %q in bar implicits %v`,
808 fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
809 }
810
811 if !inList(bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
812 t.Errorf(`expected %q in bar implicits %v`,
813 bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
Colin Cross715d7112017-10-25 20:32:18 -0700814 }
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000815
816 blorgRenamedJar := ctx.ModuleForTests("blorg", "android_common").Output("kotlin-renamed/blorg.jar")
817 if blorgRenamedJar.Implicit.String() != "external/kotlinc/jarjar-rules.txt" {
818 t.Errorf(`expected external/kotlinc/jarjar-rules.txt in blorg implicit %q`,
819 blorgRenamedJar.Implicit.String())
820 }
Colin Cross93e85952017-08-15 13:34:18 -0700821}
822
Nan Zhang61eaedb2017-11-02 13:28:15 -0700823func TestTurbine(t *testing.T) {
824 ctx := testJava(t, `
825 java_library {
826 name: "foo",
827 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900828 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700829 }
830
831 java_library {
832 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800833 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700834 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900835 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700836 }
837
838 java_library {
839 name: "baz",
840 srcs: ["c.java"],
841 libs: ["bar"],
842 sdk_version: "14",
843 }
844 `)
845
846 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
847 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
848 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
849 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
850 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
851
852 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
853 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
854 }
855
856 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
857 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
858 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
859 }
860 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
861 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
862 }
863 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
864 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
865 }
866 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/android.jar") {
867 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
868 "prebuilts/sdk/14/android.jar")
869 }
870}
871
872func TestSharding(t *testing.T) {
873 ctx := testJava(t, `
874 java_library {
875 name: "bar",
876 srcs: ["a.java","b.java","c.java"],
877 javac_shard_size: 1
878 }
879 `)
880
881 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
882 for i := 0; i < 3; i++ {
883 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
884 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
885 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
886 }
887 }
888}
889
Nan Zhang581fd212018-01-10 16:06:12 -0800890func TestDroiddoc(t *testing.T) {
891 ctx := testJava(t, `
Dan Willemsencc090972018-02-26 14:33:31 -0800892 droiddoc_template {
893 name: "droiddoc-templates-sdk",
894 path: ".",
895 }
Nan Zhang581fd212018-01-10 16:06:12 -0800896 droiddoc {
897 name: "bar-doc",
898 srcs: [
899 "bar-doc/*.java",
900 ],
901 exclude_srcs: [
902 "bar-doc/b.java"
903 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800904 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800905 hdf: [
906 "android.whichdoc offline",
907 ],
908 knowntags: [
909 "bar-doc/known_oj_tags.txt",
910 ],
911 proofread_file: "libcore-proofread.txt",
912 todo_file: "libcore-docs-todo.html",
913 args: "-offlinemode -title \"libcore\"",
914 }
915 `)
916
Nan Zhang3bb14202018-02-28 10:14:25 -0800917 stubsJar := filepath.Join(buildDir, ".intermediates", "bar-doc", "android_common", "bar-doc-stubs.srcjar")
918 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Output("bar-doc-stubs.srcjar")
Nan Zhang581fd212018-01-10 16:06:12 -0800919 if stubsJar != barDoc.Output.String() {
920 t.Errorf("expected stubs Jar [%q], got %q", stubsJar, barDoc.Output.String())
921 }
922}
923
Colin Cross54250902017-12-05 09:28:08 -0800924func TestJarGenrules(t *testing.T) {
925 ctx := testJava(t, `
926 java_library {
927 name: "foo",
928 srcs: ["a.java"],
929 }
930
931 java_genrule {
932 name: "jargen",
933 tool_files: ["b.java"],
934 cmd: "$(location b.java) $(in) $(out)",
935 out: ["jargen.jar"],
936 srcs: [":foo"],
937 }
938
939 java_library {
940 name: "bar",
941 static_libs: ["jargen"],
942 srcs: ["c.java"],
943 }
944
945 java_library {
946 name: "baz",
947 libs: ["jargen"],
948 srcs: ["c.java"],
949 }
950 `)
951
952 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
953 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
954 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
955 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
956 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
957
958 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
959 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
960 }
961
962 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
963 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
964 }
965
966 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
967 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
968 }
969
970 if len(barCombined.Inputs) != 2 ||
971 barCombined.Inputs[0].String() != bar.Output.String() ||
972 barCombined.Inputs[1].String() != jargen.Output.String() {
973 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
974 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
975 }
976}
977
Nan Zhang27e284d2018-02-09 21:03:53 +0000978func TestExcludeFileGroupInSrcs(t *testing.T) {
979 ctx := testJava(t, `
980 java_library {
981 name: "foo",
982 srcs: ["a.java", ":foo-srcs"],
983 exclude_srcs: ["a.java", ":foo-excludes"],
984 }
985
986 filegroup {
987 name: "foo-srcs",
988 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
989 }
990
991 filegroup {
992 name: "foo-excludes",
993 srcs: ["java-fg/a.java", "java-fg/b.java"],
994 }
995 `)
996
997 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
998
999 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1000 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1001 }
1002}