blob: 4a0229eb022ae100010835477be470e469a2a838 [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)
Dan Willemsen674dc7f2018-03-12 18:06:05 -070063 config.TestProductVariables.DeviceSystemSdkVersions = &[]string{"14", "15"}
Jiyong Park1a5d7b12018-01-15 15:05:10 +090064 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 Crossa97c5d32018-03-28 14:58:31 -070073 ctx.RegisterModuleType("android_library", android.ModuleFactoryAdaptor(AndroidLibraryFactory))
Colin Cross6b4a32d2017-12-05 13:42:45 -080074 ctx.RegisterModuleType("java_binary_host", android.ModuleFactoryAdaptor(BinaryHostFactory))
Colin Crossa60ead82017-10-02 18:10:21 -070075 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory(true)))
Colin Cross2acdae82017-09-15 19:44:24 -070076 ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070077 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross89536d42017-07-07 14:35:50 -070078 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross1369cdb2017-09-29 17:58:17 -070079 ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory))
Colin Cross54250902017-12-05 09:28:08 -080080 ctx.RegisterModuleType("java_genrule", android.ModuleFactoryAdaptor(genRuleFactory))
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070081 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Colin Cross54190b32017-10-09 15:34:10 -070082 ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory))
Nan Zhang581fd212018-01-10 16:06:12 -080083 ctx.RegisterModuleType("droiddoc", android.ModuleFactoryAdaptor(DroiddocFactory))
84 ctx.RegisterModuleType("droiddoc_host", android.ModuleFactoryAdaptor(DroiddocHostFactory))
Dan Willemsencc090972018-02-26 14:33:31 -080085 ctx.RegisterModuleType("droiddoc_template", android.ModuleFactoryAdaptor(DroiddocTemplateFactory))
Jiyong Parkc678ad32018-04-10 13:07:10 +090086 ctx.RegisterModuleType("java_sdk_library", android.ModuleFactoryAdaptor(sdkLibraryFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070087 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
88 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070089 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Parkc678ad32018-04-10 13:07:10 +090090 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
91 ctx.TopDown("java_sdk_library", sdkLibraryMutator).Parallel()
92 })
Colin Cross3bc7ffa2017-11-22 16:19:37 -080093 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross72bb3632017-07-13 16:23:21 -070094 ctx.Register()
95
Colin Cross4c428df2017-09-15 17:36:05 -070096 extraModules := []string{
97 "core-oj",
98 "core-libart",
99 "framework",
100 "ext",
101 "okhttp",
Colin Cross2acdae82017-09-15 19:44:24 -0700102 "android_stubs_current",
103 "android_system_stubs_current",
104 "android_test_stubs_current",
Colin Crossf19b9bb2018-03-26 14:42:44 -0700105 "core.current.stubs",
Colin Cross93e85952017-08-15 13:34:18 -0700106 "kotlin-stdlib",
Colin Cross4c428df2017-09-15 17:36:05 -0700107 }
Colin Cross72bb3632017-07-13 16:23:21 -0700108
109 for _, extra := range extraModules {
110 bp += fmt.Sprintf(`
111 java_library {
112 name: "%s",
Colin Cross0a6e0072017-08-30 14:24:55 -0700113 srcs: ["a.java"],
Colin Cross76b5f0c2017-08-29 16:02:06 -0700114 no_standard_libs: true,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700115 sdk_version: "core_current",
Colin Cross1369cdb2017-09-29 17:58:17 -0700116 system_modules: "core-system-modules",
Colin Cross72bb3632017-07-13 16:23:21 -0700117 }
118 `, extra)
119 }
120
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800121 bp += `
122 android_app {
123 name: "framework-res",
124 no_framework_libs: true,
125 }
126 `
127
Colin Cross527012a2017-11-30 22:56:16 -0800128 systemModules := []string{
129 "core-system-modules",
130 "android_stubs_current_system_modules",
131 "android_system_stubs_current_system_modules",
132 "android_test_stubs_current_system_modules",
133 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700134
Colin Cross527012a2017-11-30 22:56:16 -0800135 for _, extra := range systemModules {
136 bp += fmt.Sprintf(`
Colin Cross1369cdb2017-09-29 17:58:17 -0700137 java_system_modules {
138 name: "%s",
139 }
140 `, extra)
Colin Cross1369cdb2017-09-29 17:58:17 -0700141 }
142
Colin Cross824bee32017-11-22 17:27:51 -0800143 mockFS := map[string][]byte{
Nan Zhang27e284d2018-02-09 21:03:53 +0000144 "Android.bp": []byte(bp),
145 "a.java": nil,
146 "b.java": nil,
147 "c.java": nil,
148 "b.kt": nil,
149 "a.jar": nil,
150 "b.jar": nil,
Colin Cross0ead1d72018-04-10 13:07:42 -0700151 "java-res/a/a": nil,
152 "java-res/b/b": nil,
Nan Zhang27e284d2018-02-09 21:03:53 +0000153 "java-res2/a": nil,
154 "java-fg/a.java": nil,
155 "java-fg/b.java": nil,
156 "java-fg/c.java": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700157
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100158 "prebuilts/sdk/14/public/android.jar": nil,
159 "prebuilts/sdk/14/public/framework.aidl": nil,
160 "prebuilts/sdk/14/system/android.jar": nil,
161 "prebuilts/sdk/current/public/android.jar": nil,
162 "prebuilts/sdk/current/public/framework.aidl": nil,
163 "prebuilts/sdk/current/public/core.jar": nil,
164 "prebuilts/sdk/current/system/android.jar": nil,
165 "prebuilts/sdk/current/test/android.jar": nil,
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800166
167 // For framework-res, which is an implicit dependency for framework
168 "AndroidManifest.xml": nil,
169 "build/target/product/security/testkey": nil,
Colin Cross6b4a32d2017-12-05 13:42:45 -0800170
171 "build/soong/scripts/jar-wrapper.sh": nil,
Colin Cross7fdd2b72018-01-02 18:14:25 -0800172
Colin Cross66dbc0b2017-12-28 12:23:20 -0800173 "build/make/core/proguard.flags": nil,
174 "build/make/core/proguard_basic_keeps.flags": nil,
175
Colin Cross7fdd2b72018-01-02 18:14:25 -0800176 "jdk8/jre/lib/jce.jar": nil,
177 "jdk8/jre/lib/rt.jar": nil,
Nan Zhang61819ce2018-05-04 18:49:16 -0700178 "jdk8/lib/tools.jar": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800179
180 "bar-doc/a.java": nil,
181 "bar-doc/b.java": nil,
182 "bar-doc/known_oj_tags.txt": nil,
183 "external/doclava/templates-sdk": nil,
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000184
185 "external/kotlinc/jarjar-rules.txt": nil,
Colin Cross824bee32017-11-22 17:27:51 -0800186 }
187
188 for k, v := range fs {
189 mockFS[k] = v
190 }
191
192 ctx.MockFileSystem(mockFS)
Colin Cross72bb3632017-07-13 16:23:21 -0700193
Colin Cross527012a2017-11-30 22:56:16 -0800194 return ctx
195}
196
197func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800198 t.Helper()
Jeff Gastond3e141d2017-08-08 17:46:01 -0700199 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +0800200 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700201 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800202 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800203}
204
205func testJava(t *testing.T, bp string) *android.TestContext {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800206 t.Helper()
Colin Cross527012a2017-11-30 22:56:16 -0800207 config := testConfig(nil)
208 ctx := testContext(config, bp, nil)
209 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700210
211 return ctx
212}
213
Colin Cross2acdae82017-09-15 19:44:24 -0700214func moduleToPath(name string) string {
215 switch {
216 case name == `""`:
217 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700218 case strings.HasSuffix(name, ".jar"):
219 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700220 default:
221 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700222 }
223}
224
Colin Cross72bb3632017-07-13 16:23:21 -0700225func TestSimple(t *testing.T) {
226 ctx := testJava(t, `
227 java_library {
228 name: "foo",
229 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700230 libs: ["bar"],
231 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700232 }
233
234 java_library {
235 name: "bar",
236 srcs: ["b.java"],
237 }
238
239 java_library {
240 name: "baz",
241 srcs: ["c.java"],
242 }
Colin Crossd5934c82017-10-02 13:55:26 -0700243 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700244
Colin Cross4c428df2017-09-15 17:36:05 -0700245 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700246 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700247
248 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
249 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
250 }
251
Colin Cross1ee23172017-10-18 14:44:18 -0700252 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700253 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
254 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700255
Nan Zhanged19fc32017-10-19 13:06:22 -0700256 if !strings.Contains(javac.Args["classpath"], barTurbine) {
257 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700258 }
259
Nan Zhanged19fc32017-10-19 13:06:22 -0700260 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
261 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700262 }
263
264 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
265 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700266 }
267}
268
Colin Crossd5934c82017-10-02 13:55:26 -0700269func TestArchSpecific(t *testing.T) {
270 ctx := testJava(t, `
271 java_library {
272 name: "foo",
273 srcs: ["a.java"],
274 target: {
275 android: {
276 srcs: ["b.java"],
277 },
278 },
279 }
280 `)
281
282 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
283 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
284 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
285 }
286}
287
Colin Cross6b4a32d2017-12-05 13:42:45 -0800288func TestBinary(t *testing.T) {
289 ctx := testJava(t, `
290 java_library_host {
291 name: "foo",
292 srcs: ["a.java"],
293 }
294
295 java_binary_host {
296 name: "bar",
297 srcs: ["b.java"],
298 static_libs: ["foo"],
299 }
300 `)
301
302 buildOS := android.BuildOs.String()
303
304 bar := ctx.ModuleForTests("bar", buildOS+"_common")
305 barJar := bar.Output("bar.jar").Output.String()
306 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
307 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
308
309 // Test that the install binary wrapper depends on the installed jar file
310 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
311 t.Errorf("expected binary wrapper implicits [%q], got %v",
312 barJar, barWrapperDeps)
313 }
314
315}
316
Colin Cross2acdae82017-09-15 19:44:24 -0700317var classpathTestcases = []struct {
318 name string
Colin Cross965714f2017-10-03 20:57:01 -0700319 moduleType string
Colin Cross2acdae82017-09-15 19:44:24 -0700320 host android.OsClass
321 properties string
322 bootclasspath []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700323 system string
Colin Cross2acdae82017-09-15 19:44:24 -0700324 classpath []string
325}{
326 {
327 name: "default",
328 bootclasspath: []string{"core-oj", "core-libart"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700329 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700330 classpath: []string{"ext", "framework", "okhttp"},
331 },
332 {
333 name: "blank sdk version",
334 properties: `sdk_version: "",`,
335 bootclasspath: []string{"core-oj", "core-libart"},
Colin Cross1369cdb2017-09-29 17:58:17 -0700336 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700337 classpath: []string{"ext", "framework", "okhttp"},
338 },
339 {
Colin Cross4c428df2017-09-15 17:36:05 -0700340
Colin Cross2acdae82017-09-15 19:44:24 -0700341 name: "sdk v14",
342 properties: `sdk_version: "14",`,
Colin Crossfc3674a2017-09-18 17:41:52 -0700343 bootclasspath: []string{`""`},
Colin Cross1369cdb2017-09-29 17:58:17 -0700344 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100345 classpath: []string{"prebuilts/sdk/14/public/android.jar"},
Colin Cross2acdae82017-09-15 19:44:24 -0700346 },
347 {
Colin Cross72bb3632017-07-13 16:23:21 -0700348
Colin Cross2acdae82017-09-15 19:44:24 -0700349 name: "current",
350 properties: `sdk_version: "current",`,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700351 bootclasspath: []string{"android_stubs_current"},
Colin Cross2ebc4762017-10-20 14:00:31 -0700352 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross2acdae82017-09-15 19:44:24 -0700353 },
354 {
Colin Cross72bb3632017-07-13 16:23:21 -0700355
Colin Cross2acdae82017-09-15 19:44:24 -0700356 name: "system_current",
357 properties: `sdk_version: "system_current",`,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700358 bootclasspath: []string{"android_system_stubs_current"},
Colin Cross2ebc4762017-10-20 14:00:31 -0700359 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross2acdae82017-09-15 19:44:24 -0700360 },
361 {
Colin Cross72bb3632017-07-13 16:23:21 -0700362
Sundong Ahn0926fae2017-10-17 16:34:51 +0900363 name: "system_14",
364 properties: `sdk_version: "system_14",`,
365 bootclasspath: []string{`""`},
366 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100367 classpath: []string{"prebuilts/sdk/14/system/android.jar"},
Sundong Ahn0926fae2017-10-17 16:34:51 +0900368 },
369 {
370
Colin Cross2acdae82017-09-15 19:44:24 -0700371 name: "test_current",
372 properties: `sdk_version: "test_current",`,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700373 bootclasspath: []string{"android_test_stubs_current"},
Colin Cross2ebc4762017-10-20 14:00:31 -0700374 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Cross2acdae82017-09-15 19:44:24 -0700375 },
376 {
Colin Cross72bb3632017-07-13 16:23:21 -0700377
Jiyong Park750e5572018-01-31 00:20:13 +0900378 name: "core_current",
379 properties: `sdk_version: "core_current",`,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700380 bootclasspath: []string{"core.current.stubs"},
Jiyong Park750e5572018-01-31 00:20:13 +0900381 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Jiyong Park750e5572018-01-31 00:20:13 +0900382 },
383 {
384
Colin Cross2acdae82017-09-15 19:44:24 -0700385 name: "nostdlib",
Colin Cross1369cdb2017-09-29 17:58:17 -0700386 properties: `no_standard_libs: true, system_modules: "none"`,
387 system: "none",
388 bootclasspath: []string{`""`},
389 classpath: []string{},
390 },
391 {
392
393 name: "nostdlib system_modules",
394 properties: `no_standard_libs: true, system_modules: "core-system-modules"`,
395 system: "core-system-modules",
Colin Cross2acdae82017-09-15 19:44:24 -0700396 bootclasspath: []string{`""`},
397 classpath: []string{},
398 },
399 {
Colin Cross72bb3632017-07-13 16:23:21 -0700400
Colin Cross7fdd2b72018-01-02 18:14:25 -0800401 name: "host default",
402 moduleType: "java_library_host",
403 properties: ``,
404 host: android.Host,
405 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
406 classpath: []string{},
Colin Cross2acdae82017-09-15 19:44:24 -0700407 },
408 {
409 name: "host nostdlib",
Colin Cross965714f2017-10-03 20:57:01 -0700410 moduleType: "java_library_host",
Colin Cross2acdae82017-09-15 19:44:24 -0700411 host: android.Host,
412 properties: `no_standard_libs: true`,
413 classpath: []string{},
414 },
Colin Cross965714f2017-10-03 20:57:01 -0700415 {
416
Colin Cross7fdd2b72018-01-02 18:14:25 -0800417 name: "host supported default",
418 host: android.Host,
419 properties: `host_supported: true,`,
420 classpath: []string{},
421 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
Colin Cross965714f2017-10-03 20:57:01 -0700422 },
423 {
424 name: "host supported nostdlib",
425 host: android.Host,
Colin Cross1369cdb2017-09-29 17:58:17 -0700426 properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
Colin Cross965714f2017-10-03 20:57:01 -0700427 classpath: []string{},
428 },
Colin Cross2acdae82017-09-15 19:44:24 -0700429}
Colin Cross72bb3632017-07-13 16:23:21 -0700430
Colin Cross2acdae82017-09-15 19:44:24 -0700431func TestClasspath(t *testing.T) {
432 for _, testcase := range classpathTestcases {
433 t.Run(testcase.name, func(t *testing.T) {
Colin Cross965714f2017-10-03 20:57:01 -0700434 moduleType := "java_library"
435 if testcase.moduleType != "" {
436 moduleType = testcase.moduleType
Colin Cross2acdae82017-09-15 19:44:24 -0700437 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700438
439 bp := moduleType + ` {
Colin Cross2acdae82017-09-15 19:44:24 -0700440 name: "foo",
441 srcs: ["a.java"],
Colin Cross1369cdb2017-09-29 17:58:17 -0700442 ` + testcase.properties + `
443 }`
444
445 variant := "android_common"
446 if testcase.host == android.Host {
447 variant = android.BuildOs.String() + "_common"
Colin Cross2acdae82017-09-15 19:44:24 -0700448 }
Colin Cross72bb3632017-07-13 16:23:21 -0700449
Colin Cross2acdae82017-09-15 19:44:24 -0700450 convertModulesToPaths := func(cp []string) []string {
451 ret := make([]string, len(cp))
452 for i, e := range cp {
453 ret[i] = moduleToPath(e)
454 }
455 return ret
456 }
Colin Cross72bb3632017-07-13 16:23:21 -0700457
Colin Cross2acdae82017-09-15 19:44:24 -0700458 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
459 classpath := convertModulesToPaths(testcase.classpath)
Colin Cross72bb3632017-07-13 16:23:21 -0700460
Colin Cross2acdae82017-09-15 19:44:24 -0700461 bc := strings.Join(bootclasspath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700462 if bc != "" {
463 bc = "-bootclasspath " + bc
Colin Cross72bb3632017-07-13 16:23:21 -0700464 }
Colin Cross72bb3632017-07-13 16:23:21 -0700465
Colin Cross2acdae82017-09-15 19:44:24 -0700466 c := strings.Join(classpath, ":")
Colin Cross1369cdb2017-09-29 17:58:17 -0700467 if c != "" {
468 c = "-classpath " + c
469 }
470 system := ""
471 if testcase.system == "none" {
472 system = "--system=none"
473 } else if testcase.system != "" {
474 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
Colin Cross2acdae82017-09-15 19:44:24 -0700475 }
476
Colin Cross1369cdb2017-09-29 17:58:17 -0700477 t.Run("1.8", func(t *testing.T) {
478 // Test default javac 1.8
479 ctx := testJava(t, bp)
Colin Cross2acdae82017-09-15 19:44:24 -0700480
Colin Cross1369cdb2017-09-29 17:58:17 -0700481 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
482
483 got := javac.Args["bootClasspath"]
484 if got != bc {
485 t.Errorf("bootclasspath expected %q != got %q", bc, got)
486 }
487
488 got = javac.Args["classpath"]
489 if got != c {
490 t.Errorf("classpath expected %q != got %q", c, got)
491 }
492
493 var deps []string
494 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
495 deps = append(deps, bootclasspath...)
496 }
497 deps = append(deps, classpath...)
498
499 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
500 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
501 }
502 })
503
504 // Test again with javac 1.9
505 t.Run("1.9", func(t *testing.T) {
Colin Cross527012a2017-11-30 22:56:16 -0800506 config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
507 ctx := testContext(config, bp, nil)
508 run(t, ctx, config)
Colin Cross1369cdb2017-09-29 17:58:17 -0700509
510 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
511 got := javac.Args["bootClasspath"]
512 expected := system
513 if testcase.system == "bootclasspath" {
514 expected = bc
515 }
516 if got != expected {
517 t.Errorf("bootclasspath expected %q != got %q", expected, got)
518 }
519 })
Colin Cross2acdae82017-09-15 19:44:24 -0700520 })
Colin Cross72bb3632017-07-13 16:23:21 -0700521 }
522
Colin Cross72bb3632017-07-13 16:23:21 -0700523}
524
525func TestPrebuilts(t *testing.T) {
526 ctx := testJava(t, `
527 java_library {
528 name: "foo",
529 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700530 libs: ["bar"],
531 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700532 }
533
Colin Cross74d73e22017-08-02 11:05:49 -0700534 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700535 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700536 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700537 }
538
Colin Cross74d73e22017-08-02 11:05:49 -0700539 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700540 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700541 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700542 }
543 `)
544
Colin Cross4c428df2017-09-15 17:36:05 -0700545 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700546 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700547
548 bar := "a.jar"
549 if !strings.Contains(javac.Args["classpath"], bar) {
550 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
551 }
552
Colin Cross0a6e0072017-08-30 14:24:55 -0700553 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" {
554 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, "b.jar")
Colin Cross72bb3632017-07-13 16:23:21 -0700555 }
556}
557
Colin Cross89536d42017-07-07 14:35:50 -0700558func TestDefaults(t *testing.T) {
559 ctx := testJava(t, `
560 java_defaults {
561 name: "defaults",
562 srcs: ["a.java"],
563 libs: ["bar"],
564 static_libs: ["baz"],
565 }
566
567 java_library {
568 name: "foo",
569 defaults: ["defaults"],
570 }
571
572 java_library {
573 name: "bar",
574 srcs: ["b.java"],
575 }
576
577 java_library {
578 name: "baz",
579 srcs: ["c.java"],
580 }
581 `)
582
Colin Cross4c428df2017-09-15 17:36:05 -0700583 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700584 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700585
586 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
587 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
588 }
589
Nan Zhanged19fc32017-10-19 13:06:22 -0700590 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
591 if !strings.Contains(javac.Args["classpath"], barTurbine) {
592 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700593 }
594
Colin Cross1ee23172017-10-18 14:44:18 -0700595 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700596 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
597 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700598 }
599}
600
Colin Cross0f37af02017-09-27 17:42:05 -0700601func TestResources(t *testing.T) {
602 var table = []struct {
603 name string
604 prop string
605 extra string
606 args string
607 }{
608 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700609 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700610 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800611 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700612 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700613 },
614 {
615 // Test that a module with java_resources includes the files
616 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700617 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
618 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700619 },
620 {
621 // Test that a module with a filegroup in java_resources includes the files with the
622 // path prefix
623 name: "resource filegroup",
624 prop: `java_resources: [":foo-res"]`,
625 extra: `
626 filegroup {
627 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800628 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700629 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700630 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700631 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700632 },
633 {
634 // Test that a module with "include_srcs: true" includes its source files in the resources jar
635 name: "include sources",
636 prop: `include_srcs: true`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700637 args: "-C . -f a.java -f b.java -f c.java",
Colin Cross0f37af02017-09-27 17:42:05 -0700638 },
Colin Cross0ead1d72018-04-10 13:07:42 -0700639 {
640 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
641 name: "wildcard dirs",
642 prop: `java_resource_dirs: ["java-res/*"]`,
643 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
644 },
645 {
646 // Test that a module exclude_java_resource_dirs excludes the files
647 name: "wildcard dirs",
648 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
649 args: "-C java-res/a -f java-res/a/a",
650 },
Colin Cross0f37af02017-09-27 17:42:05 -0700651 }
652
653 for _, test := range table {
654 t.Run(test.name, func(t *testing.T) {
655 ctx := testJava(t, `
656 java_library {
657 name: "foo",
658 srcs: [
659 "a.java",
660 "b.java",
661 "c.java",
662 ],
663 `+test.prop+`,
664 }
665 `+test.extra)
666
Colin Cross1ee23172017-10-18 14:44:18 -0700667 foo := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
668 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700669
670 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
671 t.Errorf("foo combined jars %v does not contain %q",
672 foo.Inputs.Strings(), fooRes.Output.String())
673 }
674
Colin Crossaf9c55b2017-10-03 14:50:08 -0700675 if fooRes.Args["jarArgs"] != test.args {
676 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700677 fooRes.Args["jarArgs"], test.args)
678 }
679 })
680 }
681}
682
Colin Cross0532fb02017-10-02 16:57:40 -0700683func TestExcludeResources(t *testing.T) {
684 ctx := testJava(t, `
685 java_library {
686 name: "foo",
687 srcs: ["a.java"],
Colin Cross824bee32017-11-22 17:27:51 -0800688 java_resource_dirs: ["java-res", "java-res2"],
689 exclude_java_resource_dirs: ["java-res2"],
Colin Cross0532fb02017-10-02 16:57:40 -0700690 }
691
692 java_library {
693 name: "bar",
694 srcs: ["a.java"],
Colin Cross0ead1d72018-04-10 13:07:42 -0700695 java_resources: ["java-res/*/*"],
696 exclude_java_resources: ["java-res/b/*"],
Colin Cross0532fb02017-10-02 16:57:40 -0700697 }
698 `)
699
Colin Cross1ee23172017-10-18 14:44:18 -0700700 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700701
Colin Cross0ead1d72018-04-10 13:07:42 -0700702 expected := "-C java-res -f java-res/a/a -f java-res/b/b"
Colin Cross0532fb02017-10-02 16:57:40 -0700703 if fooRes.Args["jarArgs"] != expected {
704 t.Errorf("foo resource jar args %q is not %q",
705 fooRes.Args["jarArgs"], expected)
706
707 }
708
Colin Cross1ee23172017-10-18 14:44:18 -0700709 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
Colin Cross0532fb02017-10-02 16:57:40 -0700710
Colin Cross0ead1d72018-04-10 13:07:42 -0700711 expected = "-C . -f java-res/a/a"
Colin Cross0532fb02017-10-02 16:57:40 -0700712 if barRes.Args["jarArgs"] != expected {
713 t.Errorf("bar resource jar args %q is not %q",
714 barRes.Args["jarArgs"], expected)
715
716 }
717}
718
Colin Cross54190b32017-10-09 15:34:10 -0700719func TestGeneratedSources(t *testing.T) {
720 ctx := testJava(t, `
721 java_library {
722 name: "foo",
723 srcs: [
724 "a*.java",
725 ":gen",
726 "b*.java",
727 ],
728 }
729
730 genrule {
731 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800732 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700733 out: ["gen.java"],
734 }
735 `)
736
737 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
738 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
739
Colin Cross15e86d92017-10-20 15:07:08 -0700740 if filepath.Base(genrule.Output.String()) != "gen.java" {
741 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700742 }
743
744 if len(javac.Inputs) != 3 ||
745 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700746 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700747 javac.Inputs[2].String() != "b.java" {
748 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
749 }
750}
751
Colin Cross93e85952017-08-15 13:34:18 -0700752func TestKotlin(t *testing.T) {
753 ctx := testJava(t, `
754 java_library {
755 name: "foo",
Colin Cross9bc43432017-12-15 20:20:39 -0800756 srcs: ["a.java", "b.kt"],
Colin Cross93e85952017-08-15 13:34:18 -0700757 }
Colin Cross715d7112017-10-25 20:32:18 -0700758
759 java_library {
760 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800761 srcs: ["b.kt"],
762 libs: ["foo"],
763 static_libs: ["baz"],
764 }
765
766 java_library {
767 name: "baz",
768 srcs: ["c.java"],
Colin Cross715d7112017-10-25 20:32:18 -0700769 }
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000770
771 java_library {
772 name: "blorg",
773 renamed_kotlin_stdlib: true,
774 srcs: ["b.kt"],
775 }
Colin Cross93e85952017-08-15 13:34:18 -0700776 `)
777
Colin Cross9bc43432017-12-15 20:20:39 -0800778 fooKotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
779 fooJavac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
780 fooJar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
Colin Cross93e85952017-08-15 13:34:18 -0700781
Colin Cross9bc43432017-12-15 20:20:39 -0800782 if len(fooKotlinc.Inputs) != 2 || fooKotlinc.Inputs[0].String() != "a.java" ||
783 fooKotlinc.Inputs[1].String() != "b.kt" {
784 t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, fooKotlinc.Inputs)
Colin Cross93e85952017-08-15 13:34:18 -0700785 }
786
Colin Cross9bc43432017-12-15 20:20:39 -0800787 if len(fooJavac.Inputs) != 1 || fooJavac.Inputs[0].String() != "a.java" {
788 t.Errorf(`foo inputs %v != ["a.java"]`, fooJavac.Inputs)
Colin Cross93e85952017-08-15 13:34:18 -0700789 }
790
Colin Cross9bc43432017-12-15 20:20:39 -0800791 if !strings.Contains(fooJavac.Args["classpath"], fooKotlinc.Output.String()) {
Colin Cross93e85952017-08-15 13:34:18 -0700792 t.Errorf("foo classpath %v does not contain %q",
Colin Cross9bc43432017-12-15 20:20:39 -0800793 fooJavac.Args["classpath"], fooKotlinc.Output.String())
Colin Cross93e85952017-08-15 13:34:18 -0700794 }
795
Colin Cross9bc43432017-12-15 20:20:39 -0800796 if !inList(fooKotlinc.Output.String(), fooJar.Inputs.Strings()) {
Colin Cross93e85952017-08-15 13:34:18 -0700797 t.Errorf("foo jar inputs %v does not contain %q",
Colin Cross9bc43432017-12-15 20:20:39 -0800798 fooJar.Inputs.Strings(), fooKotlinc.Output.String())
Colin Cross93e85952017-08-15 13:34:18 -0700799 }
Colin Cross715d7112017-10-25 20:32:18 -0700800
Colin Cross9bc43432017-12-15 20:20:39 -0800801 fooHeaderJar := ctx.ModuleForTests("foo", "android_common").Output("turbine-combined/foo.jar")
802 bazHeaderJar := ctx.ModuleForTests("baz", "android_common").Output("turbine-combined/baz.jar")
803 barKotlinc := ctx.ModuleForTests("bar", "android_common").Rule("kotlinc")
Colin Cross715d7112017-10-25 20:32:18 -0700804
Colin Cross9bc43432017-12-15 20:20:39 -0800805 if len(barKotlinc.Inputs) != 1 || barKotlinc.Inputs[0].String() != "b.kt" {
806 t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, barKotlinc.Inputs)
807 }
808
809 if !inList(fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
810 t.Errorf(`expected %q in bar implicits %v`,
811 fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
812 }
813
814 if !inList(bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
815 t.Errorf(`expected %q in bar implicits %v`,
816 bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
Colin Cross715d7112017-10-25 20:32:18 -0700817 }
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +0000818
819 blorgRenamedJar := ctx.ModuleForTests("blorg", "android_common").Output("kotlin-renamed/blorg.jar")
820 if blorgRenamedJar.Implicit.String() != "external/kotlinc/jarjar-rules.txt" {
821 t.Errorf(`expected external/kotlinc/jarjar-rules.txt in blorg implicit %q`,
822 blorgRenamedJar.Implicit.String())
823 }
Colin Cross93e85952017-08-15 13:34:18 -0700824}
825
Nan Zhang61eaedb2017-11-02 13:28:15 -0700826func TestTurbine(t *testing.T) {
827 ctx := testJava(t, `
828 java_library {
829 name: "foo",
830 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900831 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700832 }
833
834 java_library {
835 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800836 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700837 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900838 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700839 }
840
841 java_library {
842 name: "baz",
843 srcs: ["c.java"],
844 libs: ["bar"],
845 sdk_version: "14",
846 }
847 `)
848
849 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
850 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
851 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
852 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
853 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
854
855 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
856 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
857 }
858
859 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
860 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
861 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
862 }
863 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
864 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
865 }
866 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
867 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
868 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100869 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700870 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100871 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -0700872 }
873}
874
875func TestSharding(t *testing.T) {
876 ctx := testJava(t, `
877 java_library {
878 name: "bar",
879 srcs: ["a.java","b.java","c.java"],
880 javac_shard_size: 1
881 }
882 `)
883
884 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
885 for i := 0; i < 3; i++ {
886 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
887 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
888 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
889 }
890 }
891}
892
Nan Zhang581fd212018-01-10 16:06:12 -0800893func TestDroiddoc(t *testing.T) {
894 ctx := testJava(t, `
Dan Willemsencc090972018-02-26 14:33:31 -0800895 droiddoc_template {
896 name: "droiddoc-templates-sdk",
897 path: ".",
898 }
Nan Zhang581fd212018-01-10 16:06:12 -0800899 droiddoc {
900 name: "bar-doc",
901 srcs: [
902 "bar-doc/*.java",
903 ],
904 exclude_srcs: [
905 "bar-doc/b.java"
906 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800907 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800908 hdf: [
909 "android.whichdoc offline",
910 ],
911 knowntags: [
912 "bar-doc/known_oj_tags.txt",
913 ],
914 proofread_file: "libcore-proofread.txt",
915 todo_file: "libcore-docs-todo.html",
916 args: "-offlinemode -title \"libcore\"",
917 }
918 `)
919
Nan Zhang3bb14202018-02-28 10:14:25 -0800920 stubsJar := filepath.Join(buildDir, ".intermediates", "bar-doc", "android_common", "bar-doc-stubs.srcjar")
921 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Output("bar-doc-stubs.srcjar")
Nan Zhang581fd212018-01-10 16:06:12 -0800922 if stubsJar != barDoc.Output.String() {
923 t.Errorf("expected stubs Jar [%q], got %q", stubsJar, barDoc.Output.String())
924 }
925}
926
Colin Cross54250902017-12-05 09:28:08 -0800927func TestJarGenrules(t *testing.T) {
928 ctx := testJava(t, `
929 java_library {
930 name: "foo",
931 srcs: ["a.java"],
932 }
933
934 java_genrule {
935 name: "jargen",
936 tool_files: ["b.java"],
937 cmd: "$(location b.java) $(in) $(out)",
938 out: ["jargen.jar"],
939 srcs: [":foo"],
940 }
941
942 java_library {
943 name: "bar",
944 static_libs: ["jargen"],
945 srcs: ["c.java"],
946 }
947
948 java_library {
949 name: "baz",
950 libs: ["jargen"],
951 srcs: ["c.java"],
952 }
953 `)
954
955 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
956 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
957 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
958 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
959 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
960
961 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
962 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
963 }
964
965 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
966 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
967 }
968
969 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
970 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
971 }
972
973 if len(barCombined.Inputs) != 2 ||
974 barCombined.Inputs[0].String() != bar.Output.String() ||
975 barCombined.Inputs[1].String() != jargen.Output.String() {
976 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
977 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
978 }
979}
980
Nan Zhang27e284d2018-02-09 21:03:53 +0000981func TestExcludeFileGroupInSrcs(t *testing.T) {
982 ctx := testJava(t, `
983 java_library {
984 name: "foo",
985 srcs: ["a.java", ":foo-srcs"],
986 exclude_srcs: ["a.java", ":foo-excludes"],
987 }
988
989 filegroup {
990 name: "foo-srcs",
991 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
992 }
993
994 filegroup {
995 name: "foo-excludes",
996 srcs: ["java-fg/a.java", "java-fg/b.java"],
997 }
998 `)
999
1000 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1001
1002 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1003 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1004 }
1005}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001006
1007func TestJavaSdkLibrary(t *testing.T) {
1008 ctx := testJava(t, `
1009 droiddoc_template {
1010 name: "droiddoc-templates-sdk",
1011 path: ".",
1012 }
1013 java_library {
1014 name: "conscrypt",
1015 }
1016 java_library {
1017 name: "bouncycastle",
1018 }
1019 java_sdk_library {
1020 name: "foo",
1021 srcs: ["a.java", "b.java"],
1022 api_packages: ["foo"],
1023 }
1024 java_sdk_library {
1025 name: "bar",
1026 srcs: ["a.java", "b.java"],
1027 api_packages: ["bar"],
1028 }
1029 java_library {
1030 name: "baz",
1031 srcs: ["c.java"],
1032 libs: ["foo", "bar"],
1033 sdk_version: "system_current",
1034 }
1035 `)
1036
1037 // check the existence of the internal modules
1038 ctx.ModuleForTests("foo", "android_common")
1039 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix, "android_common")
1040 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkSystemApiSuffix, "android_common")
1041 ctx.ModuleForTests("foo"+sdkDocsSuffix, "android_common")
1042 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkSystemApiSuffix, "android_common")
1043 ctx.ModuleForTests("foo"+sdkImplLibrarySuffix, "android_common")
Jiyong Park5a8d1be2018-04-25 22:57:34 +09001044 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001045
1046 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1047 // tests if baz is actually linked to the stubs lib
1048 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1049 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1050 "foo.stubs.system.jar")
1051 }
1052 // ... and not to the impl lib
1053 if strings.Contains(bazJavac.Args["classpath"], "foo.impl.jar") {
1054 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1055 "foo.impl.jar")
1056 }
1057 // test if baz is not linked to the system variant of foo
1058 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1059 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1060 "foo.stubs.jar")
1061 }
1062}