blob: a8cbfd1ded59f18da1813db004526069eec5feea [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 (
Colin Cross72bb3632017-07-13 16:23:21 -070018 "fmt"
19 "io/ioutil"
20 "os"
21 "path/filepath"
Nan Zhang61eaedb2017-11-02 13:28:15 -070022 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070023 "strings"
24 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070025
Colin Crossa4f08812018-10-02 22:03:40 -070026 "android/soong/android"
27 "android/soong/cc"
28 "android/soong/genrule"
Colin Cross72bb3632017-07-13 16:23:21 -070029)
30
31var buildDir string
32
33func setUp() {
34 var err error
35 buildDir, err = ioutil.TempDir("", "soong_java_test")
36 if err != nil {
37 panic(err)
38 }
39}
40
41func tearDown() {
42 os.RemoveAll(buildDir)
43}
44
45func TestMain(m *testing.M) {
46 run := func() int {
47 setUp()
48 defer tearDown()
49
50 return m.Run()
51 }
52
53 os.Exit(run())
54}
Colin Cross527012a2017-11-30 22:56:16 -080055
56func testConfig(env map[string]string) android.Config {
Colin Cross7fdd2b72018-01-02 18:14:25 -080057 if env == nil {
58 env = make(map[string]string)
59 }
60 if env["ANDROID_JAVA8_HOME"] == "" {
61 env["ANDROID_JAVA8_HOME"] = "jdk8"
62 }
Jiyong Park1a5d7b12018-01-15 15:05:10 +090063 config := android.TestArchConfig(buildDir, env)
Dan Willemsen674dc7f2018-03-12 18:06:05 -070064 config.TestProductVariables.DeviceSystemSdkVersions = &[]string{"14", "15"}
Jiyong Park1a5d7b12018-01-15 15:05:10 +090065 return config
Colin Cross527012a2017-11-30 22:56:16 -080066
Colin Cross1369cdb2017-09-29 17:58:17 -070067}
68
Colin Cross527012a2017-11-30 22:56:16 -080069func testContext(config android.Config, bp string,
70 fs map[string][]byte) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070071
Colin Cross4c428df2017-09-15 17:36:05 -070072 ctx := android.NewTestArchContext()
Colin Cross72bb3632017-07-13 16:23:21 -070073 ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory))
Jaewoong Jung2ad817c2019-01-18 14:27:16 -080074 ctx.RegisterModuleType("android_app_certificate", android.ModuleFactoryAdaptor(AndroidAppCertificateFactory))
Colin Crossa97c5d32018-03-28 14:58:31 -070075 ctx.RegisterModuleType("android_library", android.ModuleFactoryAdaptor(AndroidLibraryFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070076 ctx.RegisterModuleType("android_test", android.ModuleFactoryAdaptor(AndroidTestFactory))
Colin Cross638149e2019-01-05 22:12:12 -080077 ctx.RegisterModuleType("android_test_helper_app", android.ModuleFactoryAdaptor(AndroidTestHelperAppFactory))
78 ctx.RegisterModuleType("java_binary", android.ModuleFactoryAdaptor(BinaryFactory))
Colin Cross6b4a32d2017-12-05 13:42:45 -080079 ctx.RegisterModuleType("java_binary_host", android.ModuleFactoryAdaptor(BinaryHostFactory))
Colin Cross9ae1b922018-06-26 17:59:05 -070080 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory))
Colin Cross2acdae82017-09-15 19:44:24 -070081 ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
Colin Cross638149e2019-01-05 22:12:12 -080082 ctx.RegisterModuleType("java_test", android.ModuleFactoryAdaptor(TestFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070083 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross89536d42017-07-07 14:35:50 -070084 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross1369cdb2017-09-29 17:58:17 -070085 ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory))
Colin Cross54250902017-12-05 09:28:08 -080086 ctx.RegisterModuleType("java_genrule", android.ModuleFactoryAdaptor(genRuleFactory))
Colin Crossbe9cdb82019-01-21 21:37:16 -080087 ctx.RegisterModuleType("java_plugin", android.ModuleFactoryAdaptor(PluginFactory))
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070088 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Colin Cross54190b32017-10-09 15:34:10 -070089 ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory))
Nan Zhang581fd212018-01-10 16:06:12 -080090 ctx.RegisterModuleType("droiddoc", android.ModuleFactoryAdaptor(DroiddocFactory))
91 ctx.RegisterModuleType("droiddoc_host", android.ModuleFactoryAdaptor(DroiddocHostFactory))
Nan Zhangf4936b02018-08-01 15:00:28 -070092 ctx.RegisterModuleType("droiddoc_template", android.ModuleFactoryAdaptor(ExportedDroiddocDirFactory))
Jiyong Parkc678ad32018-04-10 13:07:10 +090093 ctx.RegisterModuleType("java_sdk_library", android.ModuleFactoryAdaptor(sdkLibraryFactory))
Jiyong Park58c518b2018-05-12 22:29:12 +090094 ctx.RegisterModuleType("prebuilt_apis", android.ModuleFactoryAdaptor(prebuiltApisFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070095 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
96 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070097 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Parkc678ad32018-04-10 13:07:10 +090098 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Park58c518b2018-05-12 22:29:12 +090099 ctx.TopDown("prebuilt_apis", prebuiltApisMutator).Parallel()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900100 ctx.TopDown("java_sdk_library", sdkLibraryMutator).Parallel()
101 })
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800102 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross98fd5742019-01-09 23:04:25 -0800103 ctx.RegisterPreSingletonType("sdk", android.SingletonFactoryAdaptor(sdkSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -0700104
105 // Register module types and mutators from cc needed for JNI testing
106 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
107 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
108 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
109 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
110 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
111 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
112 })
113
Colin Cross72bb3632017-07-13 16:23:21 -0700114 ctx.Register()
115
Colin Cross4c428df2017-09-15 17:36:05 -0700116 extraModules := []string{
Colin Cross86a60ae2018-05-29 14:44:55 -0700117 "core-lambda-stubs",
Colin Cross4c428df2017-09-15 17:36:05 -0700118 "framework",
119 "ext",
Insun Kang23433862019-01-24 20:46:28 +0900120 "updatable_media_stubs",
Colin Cross2acdae82017-09-15 19:44:24 -0700121 "android_stubs_current",
122 "android_system_stubs_current",
123 "android_test_stubs_current",
Colin Crossf19b9bb2018-03-26 14:42:44 -0700124 "core.current.stubs",
Neil Fullerbf210f72018-10-09 16:19:19 +0100125 "core.platform.api.stubs",
Colin Cross93e85952017-08-15 13:34:18 -0700126 "kotlin-stdlib",
Colin Crossafbb1732019-01-17 15:42:52 -0800127 "kotlin-annotations",
Colin Cross4c428df2017-09-15 17:36:05 -0700128 }
Colin Cross72bb3632017-07-13 16:23:21 -0700129
130 for _, extra := range extraModules {
131 bp += fmt.Sprintf(`
132 java_library {
133 name: "%s",
Colin Cross0a6e0072017-08-30 14:24:55 -0700134 srcs: ["a.java"],
Colin Cross76b5f0c2017-08-29 16:02:06 -0700135 no_standard_libs: true,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700136 sdk_version: "core_current",
Neil Fullerbf210f72018-10-09 16:19:19 +0100137 system_modules: "core-platform-api-stubs-system-modules",
Colin Cross72bb3632017-07-13 16:23:21 -0700138 }
139 `, extra)
140 }
141
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800142 bp += `
143 android_app {
144 name: "framework-res",
145 no_framework_libs: true,
146 }
147 `
148
Colin Cross527012a2017-11-30 22:56:16 -0800149 systemModules := []string{
150 "core-system-modules",
Neil Fullerbf210f72018-10-09 16:19:19 +0100151 "core-platform-api-stubs-system-modules",
Colin Cross527012a2017-11-30 22:56:16 -0800152 "android_stubs_current_system_modules",
153 "android_system_stubs_current_system_modules",
154 "android_test_stubs_current_system_modules",
155 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700156
Colin Cross527012a2017-11-30 22:56:16 -0800157 for _, extra := range systemModules {
158 bp += fmt.Sprintf(`
Colin Cross1369cdb2017-09-29 17:58:17 -0700159 java_system_modules {
160 name: "%s",
161 }
162 `, extra)
Colin Cross1369cdb2017-09-29 17:58:17 -0700163 }
164
Colin Cross824bee32017-11-22 17:27:51 -0800165 mockFS := map[string][]byte{
Jiyong Park58c518b2018-05-12 22:29:12 +0900166 "Android.bp": []byte(bp),
167 "a.java": nil,
168 "b.java": nil,
169 "c.java": nil,
170 "b.kt": nil,
171 "a.jar": nil,
172 "b.jar": nil,
173 "java-res/a/a": nil,
174 "java-res/b/b": nil,
175 "java-res2/a": nil,
176 "java-fg/a.java": nil,
177 "java-fg/b.java": nil,
178 "java-fg/c.java": nil,
179 "api/current.txt": nil,
180 "api/removed.txt": nil,
181 "api/system-current.txt": nil,
182 "api/system-removed.txt": nil,
183 "api/test-current.txt": nil,
184 "api/test-removed.txt": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700185
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100186 "prebuilts/sdk/14/public/android.jar": nil,
187 "prebuilts/sdk/14/public/framework.aidl": nil,
188 "prebuilts/sdk/14/system/android.jar": nil,
Colin Cross98fd5742019-01-09 23:04:25 -0800189 "prebuilts/sdk/17/public/android.jar": nil,
190 "prebuilts/sdk/17/public/framework.aidl": nil,
191 "prebuilts/sdk/17/system/android.jar": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700192 "prebuilts/sdk/current/core/android.jar": nil,
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100193 "prebuilts/sdk/current/public/android.jar": nil,
194 "prebuilts/sdk/current/public/framework.aidl": nil,
195 "prebuilts/sdk/current/public/core.jar": nil,
196 "prebuilts/sdk/current/system/android.jar": nil,
197 "prebuilts/sdk/current/test/android.jar": nil,
Jiyong Park58c518b2018-05-12 22:29:12 +0900198 "prebuilts/sdk/28/public/api/foo.txt": nil,
199 "prebuilts/sdk/28/system/api/foo.txt": nil,
200 "prebuilts/sdk/28/test/api/foo.txt": nil,
201 "prebuilts/sdk/28/public/api/foo-removed.txt": nil,
202 "prebuilts/sdk/28/system/api/foo-removed.txt": nil,
203 "prebuilts/sdk/28/test/api/foo-removed.txt": nil,
204 "prebuilts/sdk/28/public/api/bar.txt": nil,
205 "prebuilts/sdk/28/system/api/bar.txt": nil,
206 "prebuilts/sdk/28/test/api/bar.txt": nil,
207 "prebuilts/sdk/28/public/api/bar-removed.txt": nil,
208 "prebuilts/sdk/28/system/api/bar-removed.txt": nil,
209 "prebuilts/sdk/28/test/api/bar-removed.txt": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700210 "prebuilts/sdk/tools/core-lambda-stubs.jar": nil,
Sundong Ahn27eecb92018-06-21 13:47:17 +0900211 "prebuilts/sdk/Android.bp": []byte(`prebuilt_apis { name: "sdk", api_dirs: ["14", "28", "current"],}`),
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800212
213 // For framework-res, which is an implicit dependency for framework
214 "AndroidManifest.xml": nil,
215 "build/target/product/security/testkey": nil,
Colin Cross6b4a32d2017-12-05 13:42:45 -0800216
217 "build/soong/scripts/jar-wrapper.sh": nil,
Colin Cross7fdd2b72018-01-02 18:14:25 -0800218
Colin Cross66dbc0b2017-12-28 12:23:20 -0800219 "build/make/core/proguard.flags": nil,
220 "build/make/core/proguard_basic_keeps.flags": nil,
221
Colin Cross7fdd2b72018-01-02 18:14:25 -0800222 "jdk8/jre/lib/jce.jar": nil,
223 "jdk8/jre/lib/rt.jar": nil,
Nan Zhang61819ce2018-05-04 18:49:16 -0700224 "jdk8/lib/tools.jar": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800225
226 "bar-doc/a.java": nil,
227 "bar-doc/b.java": nil,
Jiyong Park1e440682018-05-23 18:42:04 +0900228 "bar-doc/IFoo.aidl": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800229 "bar-doc/known_oj_tags.txt": nil,
230 "external/doclava/templates-sdk": nil,
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800231
232 "cert/new_cert.x509.pem": nil,
233 "cert/new_cert.pk8": nil,
Colin Cross824bee32017-11-22 17:27:51 -0800234 }
235
236 for k, v := range fs {
237 mockFS[k] = v
238 }
239
240 ctx.MockFileSystem(mockFS)
Colin Cross72bb3632017-07-13 16:23:21 -0700241
Colin Cross527012a2017-11-30 22:56:16 -0800242 return ctx
243}
244
245func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800246 t.Helper()
Jiyong Park58c518b2018-05-12 22:29:12 +0900247 _, errs := ctx.ParseFileList(".", []string{"Android.bp", "prebuilts/sdk/Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +0800248 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700249 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800250 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800251}
252
253func testJava(t *testing.T, bp string) *android.TestContext {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800254 t.Helper()
Colin Cross527012a2017-11-30 22:56:16 -0800255 config := testConfig(nil)
256 ctx := testContext(config, bp, nil)
257 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700258
259 return ctx
260}
261
Colin Cross2acdae82017-09-15 19:44:24 -0700262func moduleToPath(name string) string {
263 switch {
264 case name == `""`:
265 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700266 case strings.HasSuffix(name, ".jar"):
267 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700268 default:
269 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700270 }
271}
272
Colin Cross72bb3632017-07-13 16:23:21 -0700273func TestSimple(t *testing.T) {
274 ctx := testJava(t, `
275 java_library {
276 name: "foo",
277 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700278 libs: ["bar"],
279 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700280 }
281
282 java_library {
283 name: "bar",
284 srcs: ["b.java"],
285 }
286
287 java_library {
288 name: "baz",
289 srcs: ["c.java"],
290 }
Colin Crossd5934c82017-10-02 13:55:26 -0700291 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700292
Colin Cross4c428df2017-09-15 17:36:05 -0700293 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700294 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700295
296 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
297 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
298 }
299
Colin Cross1ee23172017-10-18 14:44:18 -0700300 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700301 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
302 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700303
Nan Zhanged19fc32017-10-19 13:06:22 -0700304 if !strings.Contains(javac.Args["classpath"], barTurbine) {
305 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700306 }
307
Nan Zhanged19fc32017-10-19 13:06:22 -0700308 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
309 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700310 }
311
312 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
313 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700314 }
315}
316
Colin Crossd5934c82017-10-02 13:55:26 -0700317func TestArchSpecific(t *testing.T) {
318 ctx := testJava(t, `
319 java_library {
320 name: "foo",
321 srcs: ["a.java"],
322 target: {
323 android: {
324 srcs: ["b.java"],
325 },
326 },
327 }
328 `)
329
330 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
331 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
332 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
333 }
334}
335
Colin Cross6b4a32d2017-12-05 13:42:45 -0800336func TestBinary(t *testing.T) {
337 ctx := testJava(t, `
338 java_library_host {
339 name: "foo",
340 srcs: ["a.java"],
341 }
342
343 java_binary_host {
344 name: "bar",
345 srcs: ["b.java"],
346 static_libs: ["foo"],
347 }
348 `)
349
350 buildOS := android.BuildOs.String()
351
352 bar := ctx.ModuleForTests("bar", buildOS+"_common")
353 barJar := bar.Output("bar.jar").Output.String()
354 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
355 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
356
357 // Test that the install binary wrapper depends on the installed jar file
358 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
359 t.Errorf("expected binary wrapper implicits [%q], got %v",
360 barJar, barWrapperDeps)
361 }
362
363}
364
Colin Cross72bb3632017-07-13 16:23:21 -0700365func TestPrebuilts(t *testing.T) {
366 ctx := testJava(t, `
367 java_library {
368 name: "foo",
369 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700370 libs: ["bar"],
371 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700372 }
373
Colin Cross74d73e22017-08-02 11:05:49 -0700374 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700375 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700376 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700377 }
378
Colin Cross74d73e22017-08-02 11:05:49 -0700379 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700380 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700381 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700382 }
383 `)
384
Colin Cross4c428df2017-09-15 17:36:05 -0700385 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700386 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross37f6d792018-07-12 12:28:41 -0700387 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
388 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700389
Colin Cross37f6d792018-07-12 12:28:41 -0700390 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
391 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700392 }
393
Colin Cross37f6d792018-07-12 12:28:41 -0700394 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
395 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700396 }
397}
398
Colin Cross89536d42017-07-07 14:35:50 -0700399func TestDefaults(t *testing.T) {
400 ctx := testJava(t, `
401 java_defaults {
402 name: "defaults",
403 srcs: ["a.java"],
404 libs: ["bar"],
405 static_libs: ["baz"],
406 }
407
408 java_library {
409 name: "foo",
410 defaults: ["defaults"],
411 }
412
413 java_library {
414 name: "bar",
415 srcs: ["b.java"],
416 }
417
418 java_library {
419 name: "baz",
420 srcs: ["c.java"],
421 }
422 `)
423
Colin Cross4c428df2017-09-15 17:36:05 -0700424 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700425 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700426
427 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
428 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
429 }
430
Nan Zhanged19fc32017-10-19 13:06:22 -0700431 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
432 if !strings.Contains(javac.Args["classpath"], barTurbine) {
433 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700434 }
435
Colin Cross1ee23172017-10-18 14:44:18 -0700436 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700437 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
438 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700439 }
440}
441
Colin Cross0f37af02017-09-27 17:42:05 -0700442func TestResources(t *testing.T) {
443 var table = []struct {
444 name string
445 prop string
446 extra string
447 args string
448 }{
449 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700450 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700451 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800452 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700453 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700454 },
455 {
456 // Test that a module with java_resources includes the files
457 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700458 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
459 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700460 },
461 {
462 // Test that a module with a filegroup in java_resources includes the files with the
463 // path prefix
464 name: "resource filegroup",
465 prop: `java_resources: [":foo-res"]`,
466 extra: `
467 filegroup {
468 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800469 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700470 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700471 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700472 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700473 },
474 {
475 // Test that a module with "include_srcs: true" includes its source files in the resources jar
476 name: "include sources",
477 prop: `include_srcs: true`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700478 args: "-C . -f a.java -f b.java -f c.java",
Colin Cross0f37af02017-09-27 17:42:05 -0700479 },
Colin Cross0ead1d72018-04-10 13:07:42 -0700480 {
481 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
482 name: "wildcard dirs",
483 prop: `java_resource_dirs: ["java-res/*"]`,
484 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
485 },
486 {
487 // Test that a module exclude_java_resource_dirs excludes the files
488 name: "wildcard dirs",
489 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
490 args: "-C java-res/a -f java-res/a/a",
491 },
Colin Crosscedd4762018-09-13 11:26:19 -0700492 {
493 // Test wildcards in java_resources
494 name: "wildcard files",
495 prop: `java_resources: ["java-res/**/*"]`,
496 args: "-C . -f java-res/a/a -f java-res/b/b",
497 },
498 {
499 // Test exclude_java_resources with java_resources
500 name: "wildcard files with exclude",
501 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
502 args: "-C . -f java-res/a/a",
503 },
504 {
505 // Test exclude_java_resources with java_resource_dirs
506 name: "resource dirs with exclude files",
507 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
508 args: "-C java-res -f java-res/a/a",
509 },
510 {
511 // Test exclude_java_resource_dirs with java_resource_dirs
512 name: "resource dirs with exclude files",
513 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
514 args: "-C java-res -f java-res/a/a -f java-res/b/b",
515 },
Colin Cross0f37af02017-09-27 17:42:05 -0700516 }
517
518 for _, test := range table {
519 t.Run(test.name, func(t *testing.T) {
520 ctx := testJava(t, `
521 java_library {
522 name: "foo",
523 srcs: [
524 "a.java",
525 "b.java",
526 "c.java",
527 ],
528 `+test.prop+`,
529 }
530 `+test.extra)
531
Colin Cross331a1212018-08-15 20:40:52 -0700532 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700533 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700534
535 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
536 t.Errorf("foo combined jars %v does not contain %q",
537 foo.Inputs.Strings(), fooRes.Output.String())
538 }
539
Colin Crossaf9c55b2017-10-03 14:50:08 -0700540 if fooRes.Args["jarArgs"] != test.args {
541 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700542 fooRes.Args["jarArgs"], test.args)
543 }
544 })
545 }
546}
547
Colin Cross54190b32017-10-09 15:34:10 -0700548func TestGeneratedSources(t *testing.T) {
549 ctx := testJava(t, `
550 java_library {
551 name: "foo",
552 srcs: [
553 "a*.java",
554 ":gen",
555 "b*.java",
556 ],
557 }
558
559 genrule {
560 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800561 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700562 out: ["gen.java"],
563 }
564 `)
565
566 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
567 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
568
Colin Cross15e86d92017-10-20 15:07:08 -0700569 if filepath.Base(genrule.Output.String()) != "gen.java" {
570 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700571 }
572
573 if len(javac.Inputs) != 3 ||
574 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700575 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700576 javac.Inputs[2].String() != "b.java" {
577 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
578 }
579}
580
Nan Zhang61eaedb2017-11-02 13:28:15 -0700581func TestTurbine(t *testing.T) {
582 ctx := testJava(t, `
583 java_library {
584 name: "foo",
585 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900586 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700587 }
588
589 java_library {
590 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800591 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700592 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900593 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700594 }
595
596 java_library {
597 name: "baz",
598 srcs: ["c.java"],
599 libs: ["bar"],
600 sdk_version: "14",
601 }
602 `)
603
604 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
605 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
606 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
607 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
608 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
609
610 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
611 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
612 }
613
614 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
615 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
616 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
617 }
618 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
619 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
620 }
621 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
622 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
623 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100624 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700625 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100626 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -0700627 }
628}
629
630func TestSharding(t *testing.T) {
631 ctx := testJava(t, `
632 java_library {
633 name: "bar",
634 srcs: ["a.java","b.java","c.java"],
635 javac_shard_size: 1
636 }
637 `)
638
639 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
640 for i := 0; i < 3; i++ {
641 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
642 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
643 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
644 }
645 }
646}
647
Nan Zhang581fd212018-01-10 16:06:12 -0800648func TestDroiddoc(t *testing.T) {
649 ctx := testJava(t, `
Dan Willemsencc090972018-02-26 14:33:31 -0800650 droiddoc_template {
651 name: "droiddoc-templates-sdk",
652 path: ".",
653 }
Nan Zhang581fd212018-01-10 16:06:12 -0800654 droiddoc {
655 name: "bar-doc",
656 srcs: [
657 "bar-doc/*.java",
Jiyong Park1e440682018-05-23 18:42:04 +0900658 "bar-doc/IFoo.aidl",
Nan Zhang581fd212018-01-10 16:06:12 -0800659 ],
660 exclude_srcs: [
661 "bar-doc/b.java"
662 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800663 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800664 hdf: [
665 "android.whichdoc offline",
666 ],
667 knowntags: [
668 "bar-doc/known_oj_tags.txt",
669 ],
670 proofread_file: "libcore-proofread.txt",
671 todo_file: "libcore-docs-todo.html",
672 args: "-offlinemode -title \"libcore\"",
673 }
674 `)
675
Nan Zhang3bb14202018-02-28 10:14:25 -0800676 stubsJar := filepath.Join(buildDir, ".intermediates", "bar-doc", "android_common", "bar-doc-stubs.srcjar")
677 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Output("bar-doc-stubs.srcjar")
Nan Zhang581fd212018-01-10 16:06:12 -0800678 if stubsJar != barDoc.Output.String() {
679 t.Errorf("expected stubs Jar [%q], got %q", stubsJar, barDoc.Output.String())
680 }
Jiyong Park1e440682018-05-23 18:42:04 +0900681 inputs := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc").Inputs
682 var javaSrcs []string
683 for _, i := range inputs {
684 javaSrcs = append(javaSrcs, i.Base())
685 }
686 if len(javaSrcs) != 2 || javaSrcs[0] != "a.java" || javaSrcs[1] != "IFoo.java" {
687 t.Errorf("inputs of bar-doc must be []string{\"a.java\", \"IFoo.java\", but was %#v.", javaSrcs)
688 }
Nan Zhang581fd212018-01-10 16:06:12 -0800689}
690
Colin Cross54250902017-12-05 09:28:08 -0800691func TestJarGenrules(t *testing.T) {
692 ctx := testJava(t, `
693 java_library {
694 name: "foo",
695 srcs: ["a.java"],
696 }
697
698 java_genrule {
699 name: "jargen",
700 tool_files: ["b.java"],
701 cmd: "$(location b.java) $(in) $(out)",
702 out: ["jargen.jar"],
703 srcs: [":foo"],
704 }
705
706 java_library {
707 name: "bar",
708 static_libs: ["jargen"],
709 srcs: ["c.java"],
710 }
711
712 java_library {
713 name: "baz",
714 libs: ["jargen"],
715 srcs: ["c.java"],
716 }
717 `)
718
719 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
720 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
721 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
722 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
723 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
724
725 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
726 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
727 }
728
729 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
730 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
731 }
732
733 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
734 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
735 }
736
737 if len(barCombined.Inputs) != 2 ||
738 barCombined.Inputs[0].String() != bar.Output.String() ||
739 barCombined.Inputs[1].String() != jargen.Output.String() {
740 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
741 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
742 }
743}
744
Nan Zhang27e284d2018-02-09 21:03:53 +0000745func TestExcludeFileGroupInSrcs(t *testing.T) {
746 ctx := testJava(t, `
747 java_library {
748 name: "foo",
749 srcs: ["a.java", ":foo-srcs"],
750 exclude_srcs: ["a.java", ":foo-excludes"],
751 }
752
753 filegroup {
754 name: "foo-srcs",
755 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
756 }
757
758 filegroup {
759 name: "foo-excludes",
760 srcs: ["java-fg/a.java", "java-fg/b.java"],
761 }
762 `)
763
764 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
765
766 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
767 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
768 }
769}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900770
771func TestJavaSdkLibrary(t *testing.T) {
772 ctx := testJava(t, `
773 droiddoc_template {
774 name: "droiddoc-templates-sdk",
775 path: ".",
776 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900777 java_sdk_library {
778 name: "foo",
779 srcs: ["a.java", "b.java"],
780 api_packages: ["foo"],
781 }
782 java_sdk_library {
783 name: "bar",
784 srcs: ["a.java", "b.java"],
785 api_packages: ["bar"],
786 }
787 java_library {
788 name: "baz",
789 srcs: ["c.java"],
790 libs: ["foo", "bar"],
791 sdk_version: "system_current",
792 }
Jiyong Park1be96912018-05-28 18:02:19 +0900793 java_library {
794 name: "qux",
795 srcs: ["c.java"],
796 libs: ["baz"],
797 sdk_version: "system_current",
798 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900799 `)
800
801 // check the existence of the internal modules
802 ctx.ModuleForTests("foo", "android_common")
803 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix, "android_common")
804 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +0900805 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkTestApiSuffix, "android_common")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900806 ctx.ModuleForTests("foo"+sdkDocsSuffix, "android_common")
807 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +0900808 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkTestApiSuffix, "android_common")
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800809 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_arm64_armv8-a")
Jiyong Park58c518b2018-05-12 22:29:12 +0900810 ctx.ModuleForTests("foo.api.public.28", "")
811 ctx.ModuleForTests("foo.api.system.28", "")
812 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900813
814 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
815 // tests if baz is actually linked to the stubs lib
816 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
817 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
818 "foo.stubs.system.jar")
819 }
820 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +0900821 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900822 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +0900823 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900824 }
825 // test if baz is not linked to the system variant of foo
826 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
827 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
828 "foo.stubs.jar")
829 }
Jiyong Park1be96912018-05-28 18:02:19 +0900830
831 // test if baz has exported SDK lib names foo and bar to qux
832 qux := ctx.ModuleForTests("qux", "android_common")
833 if quxLib, ok := qux.Module().(*Library); ok {
834 sdkLibs := quxLib.ExportedSdkLibs()
835 if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) {
836 t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs)
837 }
838 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900839}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200840
841var compilerFlagsTestCases = []struct {
842 in string
843 out bool
844}{
845 {
846 in: "a",
847 out: false,
848 },
849 {
850 in: "-a",
851 out: true,
852 },
853 {
854 in: "-no-jdk",
855 out: false,
856 },
857 {
858 in: "-no-stdlib",
859 out: false,
860 },
861 {
862 in: "-kotlin-home",
863 out: false,
864 },
865 {
866 in: "-kotlin-home /some/path",
867 out: false,
868 },
869 {
870 in: "-include-runtime",
871 out: false,
872 },
873 {
874 in: "-Xintellij-plugin-root",
875 out: false,
876 },
877}
878
879type mockContext struct {
880 android.ModuleContext
881 result bool
882}
883
884func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
885 // CheckBadCompilerFlags calls this function when the flag should be rejected
886 ctx.result = false
887}
888
889func TestCompilerFlags(t *testing.T) {
890 for _, testCase := range compilerFlagsTestCases {
891 ctx := &mockContext{result: true}
892 CheckKotlincFlags(ctx, []string{testCase.in})
893 if ctx.result != testCase.out {
894 t.Errorf("incorrect output:")
895 t.Errorf(" input: %#v", testCase.in)
896 t.Errorf(" expected: %#v", testCase.out)
897 t.Errorf(" got: %#v", ctx.result)
898 }
899 }
900}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800901
902// TODO(jungjw): Consider making this more robust by ignoring path order.
903func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
904 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
905 flags := strings.Split(variables["javacFlags"], " ")
906 got := ""
907 for _, flag := range flags {
908 keyEnd := strings.Index(flag, "=")
909 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
910 got = flag[keyEnd+1:]
911 break
912 }
913 }
914 if expected != got {
915 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
916 }
917}
918
919func TestPatchModule(t *testing.T) {
920 bp := `
921 java_library {
922 name: "foo",
923 srcs: ["a.java"],
924 }
925
926 java_library {
927 name: "bar",
928 srcs: ["b.java"],
929 no_standard_libs: true,
930 system_modules: "none",
931 patch_module: "java.base",
932 }
933
934 java_library {
935 name: "baz",
936 srcs: ["c.java"],
937 patch_module: "java.base",
938 }
939 `
940
941 t.Run("1.8", func(t *testing.T) {
942 // Test default javac 1.8
943 ctx := testJava(t, bp)
944
945 checkPatchModuleFlag(t, ctx, "foo", "")
946 checkPatchModuleFlag(t, ctx, "bar", "")
947 checkPatchModuleFlag(t, ctx, "baz", "")
948 })
949
950 t.Run("1.9", func(t *testing.T) {
951 // Test again with javac 1.9
952 config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
953 ctx := testContext(config, bp, nil)
954 run(t, ctx, config)
955
956 checkPatchModuleFlag(t, ctx, "foo", "")
957 expected := "java.base=.:" + buildDir
958 checkPatchModuleFlag(t, ctx, "bar", expected)
Insun Kang23433862019-01-24 20:46:28 +0900959 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework"), moduleToPath("updatable_media_stubs")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800960 checkPatchModuleFlag(t, ctx, "baz", expected)
961 })
962}