blob: 57b2a5936cc54533c62dcb8bb79911019366dbbd [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)
Colin Crossa74ca042019-01-31 14:31:51 -080064 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",
Colin Cross2acdae82017-09-15 19:44:24 -0700120 "android_stubs_current",
121 "android_system_stubs_current",
122 "android_test_stubs_current",
Colin Crossf19b9bb2018-03-26 14:42:44 -0700123 "core.current.stubs",
Neil Fullerbf210f72018-10-09 16:19:19 +0100124 "core.platform.api.stubs",
Colin Cross93e85952017-08-15 13:34:18 -0700125 "kotlin-stdlib",
Colin Crossafbb1732019-01-17 15:42:52 -0800126 "kotlin-annotations",
Colin Cross4c428df2017-09-15 17:36:05 -0700127 }
Colin Cross72bb3632017-07-13 16:23:21 -0700128
129 for _, extra := range extraModules {
130 bp += fmt.Sprintf(`
131 java_library {
132 name: "%s",
Colin Cross0a6e0072017-08-30 14:24:55 -0700133 srcs: ["a.java"],
Colin Cross76b5f0c2017-08-29 16:02:06 -0700134 no_standard_libs: true,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700135 sdk_version: "core_current",
Neil Fullerbf210f72018-10-09 16:19:19 +0100136 system_modules: "core-platform-api-stubs-system-modules",
Colin Cross72bb3632017-07-13 16:23:21 -0700137 }
138 `, extra)
139 }
140
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800141 bp += `
142 android_app {
143 name: "framework-res",
144 no_framework_libs: true,
145 }
146 `
147
Colin Cross527012a2017-11-30 22:56:16 -0800148 systemModules := []string{
149 "core-system-modules",
Neil Fullerbf210f72018-10-09 16:19:19 +0100150 "core-platform-api-stubs-system-modules",
Colin Cross527012a2017-11-30 22:56:16 -0800151 "android_stubs_current_system_modules",
152 "android_system_stubs_current_system_modules",
153 "android_test_stubs_current_system_modules",
154 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700155
Colin Cross527012a2017-11-30 22:56:16 -0800156 for _, extra := range systemModules {
157 bp += fmt.Sprintf(`
Colin Cross1369cdb2017-09-29 17:58:17 -0700158 java_system_modules {
159 name: "%s",
160 }
161 `, extra)
Colin Cross1369cdb2017-09-29 17:58:17 -0700162 }
163
Colin Cross824bee32017-11-22 17:27:51 -0800164 mockFS := map[string][]byte{
Jiyong Park58c518b2018-05-12 22:29:12 +0900165 "Android.bp": []byte(bp),
166 "a.java": nil,
167 "b.java": nil,
168 "c.java": nil,
169 "b.kt": nil,
170 "a.jar": nil,
171 "b.jar": nil,
172 "java-res/a/a": nil,
173 "java-res/b/b": nil,
174 "java-res2/a": nil,
175 "java-fg/a.java": nil,
176 "java-fg/b.java": nil,
177 "java-fg/c.java": nil,
178 "api/current.txt": nil,
179 "api/removed.txt": nil,
180 "api/system-current.txt": nil,
181 "api/system-removed.txt": nil,
182 "api/test-current.txt": nil,
183 "api/test-removed.txt": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700184
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100185 "prebuilts/sdk/14/public/android.jar": nil,
186 "prebuilts/sdk/14/public/framework.aidl": nil,
187 "prebuilts/sdk/14/system/android.jar": nil,
Colin Cross98fd5742019-01-09 23:04:25 -0800188 "prebuilts/sdk/17/public/android.jar": nil,
189 "prebuilts/sdk/17/public/framework.aidl": nil,
190 "prebuilts/sdk/17/system/android.jar": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700191 "prebuilts/sdk/current/core/android.jar": nil,
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100192 "prebuilts/sdk/current/public/android.jar": nil,
193 "prebuilts/sdk/current/public/framework.aidl": nil,
194 "prebuilts/sdk/current/public/core.jar": nil,
195 "prebuilts/sdk/current/system/android.jar": nil,
196 "prebuilts/sdk/current/test/android.jar": nil,
Jiyong Park58c518b2018-05-12 22:29:12 +0900197 "prebuilts/sdk/28/public/api/foo.txt": nil,
198 "prebuilts/sdk/28/system/api/foo.txt": nil,
199 "prebuilts/sdk/28/test/api/foo.txt": nil,
200 "prebuilts/sdk/28/public/api/foo-removed.txt": nil,
201 "prebuilts/sdk/28/system/api/foo-removed.txt": nil,
202 "prebuilts/sdk/28/test/api/foo-removed.txt": nil,
203 "prebuilts/sdk/28/public/api/bar.txt": nil,
204 "prebuilts/sdk/28/system/api/bar.txt": nil,
205 "prebuilts/sdk/28/test/api/bar.txt": nil,
206 "prebuilts/sdk/28/public/api/bar-removed.txt": nil,
207 "prebuilts/sdk/28/system/api/bar-removed.txt": nil,
208 "prebuilts/sdk/28/test/api/bar-removed.txt": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700209 "prebuilts/sdk/tools/core-lambda-stubs.jar": nil,
Sundong Ahn27eecb92018-06-21 13:47:17 +0900210 "prebuilts/sdk/Android.bp": []byte(`prebuilt_apis { name: "sdk", api_dirs: ["14", "28", "current"],}`),
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800211
212 // For framework-res, which is an implicit dependency for framework
213 "AndroidManifest.xml": nil,
214 "build/target/product/security/testkey": nil,
Colin Cross6b4a32d2017-12-05 13:42:45 -0800215
216 "build/soong/scripts/jar-wrapper.sh": nil,
Colin Cross7fdd2b72018-01-02 18:14:25 -0800217
Colin Cross66dbc0b2017-12-28 12:23:20 -0800218 "build/make/core/proguard.flags": nil,
219 "build/make/core/proguard_basic_keeps.flags": nil,
220
Colin Cross7fdd2b72018-01-02 18:14:25 -0800221 "jdk8/jre/lib/jce.jar": nil,
222 "jdk8/jre/lib/rt.jar": nil,
Nan Zhang61819ce2018-05-04 18:49:16 -0700223 "jdk8/lib/tools.jar": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800224
225 "bar-doc/a.java": nil,
226 "bar-doc/b.java": nil,
Jiyong Park1e440682018-05-23 18:42:04 +0900227 "bar-doc/IFoo.aidl": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800228 "bar-doc/known_oj_tags.txt": nil,
229 "external/doclava/templates-sdk": nil,
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800230
231 "cert/new_cert.x509.pem": nil,
232 "cert/new_cert.pk8": nil,
Colin Cross824bee32017-11-22 17:27:51 -0800233 }
234
235 for k, v := range fs {
236 mockFS[k] = v
237 }
238
239 ctx.MockFileSystem(mockFS)
Colin Cross72bb3632017-07-13 16:23:21 -0700240
Colin Cross527012a2017-11-30 22:56:16 -0800241 return ctx
242}
243
244func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800245 t.Helper()
Jiyong Park58c518b2018-05-12 22:29:12 +0900246 _, errs := ctx.ParseFileList(".", []string{"Android.bp", "prebuilts/sdk/Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +0800247 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700248 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800249 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800250}
251
252func testJava(t *testing.T, bp string) *android.TestContext {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800253 t.Helper()
Colin Cross527012a2017-11-30 22:56:16 -0800254 config := testConfig(nil)
255 ctx := testContext(config, bp, nil)
256 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700257
258 return ctx
259}
260
Colin Cross2acdae82017-09-15 19:44:24 -0700261func moduleToPath(name string) string {
262 switch {
263 case name == `""`:
264 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700265 case strings.HasSuffix(name, ".jar"):
266 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700267 default:
268 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700269 }
270}
271
Colin Cross72bb3632017-07-13 16:23:21 -0700272func TestSimple(t *testing.T) {
273 ctx := testJava(t, `
274 java_library {
275 name: "foo",
276 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700277 libs: ["bar"],
278 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700279 }
280
281 java_library {
282 name: "bar",
283 srcs: ["b.java"],
284 }
285
286 java_library {
287 name: "baz",
288 srcs: ["c.java"],
289 }
Colin Crossd5934c82017-10-02 13:55:26 -0700290 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700291
Colin Cross4c428df2017-09-15 17:36:05 -0700292 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700293 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700294
295 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
296 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
297 }
298
Colin Cross1ee23172017-10-18 14:44:18 -0700299 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700300 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
301 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700302
Nan Zhanged19fc32017-10-19 13:06:22 -0700303 if !strings.Contains(javac.Args["classpath"], barTurbine) {
304 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700305 }
306
Nan Zhanged19fc32017-10-19 13:06:22 -0700307 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
308 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700309 }
310
311 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
312 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700313 }
314}
315
Colin Crossd5934c82017-10-02 13:55:26 -0700316func TestArchSpecific(t *testing.T) {
317 ctx := testJava(t, `
318 java_library {
319 name: "foo",
320 srcs: ["a.java"],
321 target: {
322 android: {
323 srcs: ["b.java"],
324 },
325 },
326 }
327 `)
328
329 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
330 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
331 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
332 }
333}
334
Colin Cross6b4a32d2017-12-05 13:42:45 -0800335func TestBinary(t *testing.T) {
336 ctx := testJava(t, `
337 java_library_host {
338 name: "foo",
339 srcs: ["a.java"],
340 }
341
342 java_binary_host {
343 name: "bar",
344 srcs: ["b.java"],
345 static_libs: ["foo"],
346 }
347 `)
348
349 buildOS := android.BuildOs.String()
350
351 bar := ctx.ModuleForTests("bar", buildOS+"_common")
352 barJar := bar.Output("bar.jar").Output.String()
353 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
354 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
355
356 // Test that the install binary wrapper depends on the installed jar file
357 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
358 t.Errorf("expected binary wrapper implicits [%q], got %v",
359 barJar, barWrapperDeps)
360 }
361
362}
363
Colin Cross72bb3632017-07-13 16:23:21 -0700364func TestPrebuilts(t *testing.T) {
365 ctx := testJava(t, `
366 java_library {
367 name: "foo",
368 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700369 libs: ["bar"],
370 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700371 }
372
Colin Cross74d73e22017-08-02 11:05:49 -0700373 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700374 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700375 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700376 }
377
Colin Cross74d73e22017-08-02 11:05:49 -0700378 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700379 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700380 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700381 }
382 `)
383
Colin Cross4c428df2017-09-15 17:36:05 -0700384 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700385 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross37f6d792018-07-12 12:28:41 -0700386 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
387 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700388
Colin Cross37f6d792018-07-12 12:28:41 -0700389 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
390 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700391 }
392
Colin Cross37f6d792018-07-12 12:28:41 -0700393 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
394 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700395 }
396}
397
Colin Cross89536d42017-07-07 14:35:50 -0700398func TestDefaults(t *testing.T) {
399 ctx := testJava(t, `
400 java_defaults {
401 name: "defaults",
402 srcs: ["a.java"],
403 libs: ["bar"],
404 static_libs: ["baz"],
405 }
406
407 java_library {
408 name: "foo",
409 defaults: ["defaults"],
410 }
411
412 java_library {
413 name: "bar",
414 srcs: ["b.java"],
415 }
416
417 java_library {
418 name: "baz",
419 srcs: ["c.java"],
420 }
421 `)
422
Colin Cross4c428df2017-09-15 17:36:05 -0700423 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700424 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700425
426 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
427 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
428 }
429
Nan Zhanged19fc32017-10-19 13:06:22 -0700430 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
431 if !strings.Contains(javac.Args["classpath"], barTurbine) {
432 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700433 }
434
Colin Cross1ee23172017-10-18 14:44:18 -0700435 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700436 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
437 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700438 }
439}
440
Colin Cross0f37af02017-09-27 17:42:05 -0700441func TestResources(t *testing.T) {
442 var table = []struct {
443 name string
444 prop string
445 extra string
446 args string
447 }{
448 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700449 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700450 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800451 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700452 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700453 },
454 {
455 // Test that a module with java_resources includes the files
456 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700457 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
458 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700459 },
460 {
461 // Test that a module with a filegroup in java_resources includes the files with the
462 // path prefix
463 name: "resource filegroup",
464 prop: `java_resources: [":foo-res"]`,
465 extra: `
466 filegroup {
467 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800468 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700469 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700470 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700471 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700472 },
473 {
474 // Test that a module with "include_srcs: true" includes its source files in the resources jar
475 name: "include sources",
476 prop: `include_srcs: true`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700477 args: "-C . -f a.java -f b.java -f c.java",
Colin Cross0f37af02017-09-27 17:42:05 -0700478 },
Colin Cross0ead1d72018-04-10 13:07:42 -0700479 {
480 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
481 name: "wildcard dirs",
482 prop: `java_resource_dirs: ["java-res/*"]`,
483 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
484 },
485 {
486 // Test that a module exclude_java_resource_dirs excludes the files
487 name: "wildcard dirs",
488 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
489 args: "-C java-res/a -f java-res/a/a",
490 },
Colin Crosscedd4762018-09-13 11:26:19 -0700491 {
492 // Test wildcards in java_resources
493 name: "wildcard files",
494 prop: `java_resources: ["java-res/**/*"]`,
495 args: "-C . -f java-res/a/a -f java-res/b/b",
496 },
497 {
498 // Test exclude_java_resources with java_resources
499 name: "wildcard files with exclude",
500 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
501 args: "-C . -f java-res/a/a",
502 },
503 {
504 // Test exclude_java_resources with java_resource_dirs
505 name: "resource dirs with exclude files",
506 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
507 args: "-C java-res -f java-res/a/a",
508 },
509 {
510 // Test exclude_java_resource_dirs with java_resource_dirs
511 name: "resource dirs with exclude files",
512 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
513 args: "-C java-res -f java-res/a/a -f java-res/b/b",
514 },
Colin Cross0f37af02017-09-27 17:42:05 -0700515 }
516
517 for _, test := range table {
518 t.Run(test.name, func(t *testing.T) {
519 ctx := testJava(t, `
520 java_library {
521 name: "foo",
522 srcs: [
523 "a.java",
524 "b.java",
525 "c.java",
526 ],
527 `+test.prop+`,
528 }
529 `+test.extra)
530
Colin Cross331a1212018-08-15 20:40:52 -0700531 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700532 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700533
534 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
535 t.Errorf("foo combined jars %v does not contain %q",
536 foo.Inputs.Strings(), fooRes.Output.String())
537 }
538
Colin Crossaf9c55b2017-10-03 14:50:08 -0700539 if fooRes.Args["jarArgs"] != test.args {
540 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700541 fooRes.Args["jarArgs"], test.args)
542 }
543 })
544 }
545}
546
Colin Cross54190b32017-10-09 15:34:10 -0700547func TestGeneratedSources(t *testing.T) {
548 ctx := testJava(t, `
549 java_library {
550 name: "foo",
551 srcs: [
552 "a*.java",
553 ":gen",
554 "b*.java",
555 ],
556 }
557
558 genrule {
559 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800560 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700561 out: ["gen.java"],
562 }
563 `)
564
565 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
566 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
567
Colin Cross15e86d92017-10-20 15:07:08 -0700568 if filepath.Base(genrule.Output.String()) != "gen.java" {
569 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700570 }
571
572 if len(javac.Inputs) != 3 ||
573 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700574 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700575 javac.Inputs[2].String() != "b.java" {
576 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
577 }
578}
579
Nan Zhang61eaedb2017-11-02 13:28:15 -0700580func TestTurbine(t *testing.T) {
581 ctx := testJava(t, `
582 java_library {
583 name: "foo",
584 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900585 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700586 }
587
588 java_library {
589 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800590 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700591 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900592 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700593 }
594
595 java_library {
596 name: "baz",
597 srcs: ["c.java"],
598 libs: ["bar"],
599 sdk_version: "14",
600 }
601 `)
602
603 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
604 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
605 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
606 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
607 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
608
609 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
610 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
611 }
612
613 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
614 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
615 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
616 }
617 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
618 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
619 }
620 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
621 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
622 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100623 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700624 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100625 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -0700626 }
627}
628
629func TestSharding(t *testing.T) {
630 ctx := testJava(t, `
631 java_library {
632 name: "bar",
633 srcs: ["a.java","b.java","c.java"],
634 javac_shard_size: 1
635 }
636 `)
637
638 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
639 for i := 0; i < 3; i++ {
640 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
641 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
642 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
643 }
644 }
645}
646
Nan Zhang581fd212018-01-10 16:06:12 -0800647func TestDroiddoc(t *testing.T) {
648 ctx := testJava(t, `
Dan Willemsencc090972018-02-26 14:33:31 -0800649 droiddoc_template {
650 name: "droiddoc-templates-sdk",
651 path: ".",
652 }
Nan Zhang581fd212018-01-10 16:06:12 -0800653 droiddoc {
654 name: "bar-doc",
655 srcs: [
656 "bar-doc/*.java",
Jiyong Park1e440682018-05-23 18:42:04 +0900657 "bar-doc/IFoo.aidl",
Nan Zhang581fd212018-01-10 16:06:12 -0800658 ],
659 exclude_srcs: [
660 "bar-doc/b.java"
661 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800662 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800663 hdf: [
664 "android.whichdoc offline",
665 ],
666 knowntags: [
667 "bar-doc/known_oj_tags.txt",
668 ],
669 proofread_file: "libcore-proofread.txt",
670 todo_file: "libcore-docs-todo.html",
671 args: "-offlinemode -title \"libcore\"",
672 }
673 `)
674
Nan Zhang3bb14202018-02-28 10:14:25 -0800675 stubsJar := filepath.Join(buildDir, ".intermediates", "bar-doc", "android_common", "bar-doc-stubs.srcjar")
676 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Output("bar-doc-stubs.srcjar")
Nan Zhang581fd212018-01-10 16:06:12 -0800677 if stubsJar != barDoc.Output.String() {
678 t.Errorf("expected stubs Jar [%q], got %q", stubsJar, barDoc.Output.String())
679 }
Jiyong Park1e440682018-05-23 18:42:04 +0900680 inputs := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc").Inputs
681 var javaSrcs []string
682 for _, i := range inputs {
683 javaSrcs = append(javaSrcs, i.Base())
684 }
685 if len(javaSrcs) != 2 || javaSrcs[0] != "a.java" || javaSrcs[1] != "IFoo.java" {
686 t.Errorf("inputs of bar-doc must be []string{\"a.java\", \"IFoo.java\", but was %#v.", javaSrcs)
687 }
Nan Zhang581fd212018-01-10 16:06:12 -0800688}
689
Colin Cross54250902017-12-05 09:28:08 -0800690func TestJarGenrules(t *testing.T) {
691 ctx := testJava(t, `
692 java_library {
693 name: "foo",
694 srcs: ["a.java"],
695 }
696
697 java_genrule {
698 name: "jargen",
699 tool_files: ["b.java"],
700 cmd: "$(location b.java) $(in) $(out)",
701 out: ["jargen.jar"],
702 srcs: [":foo"],
703 }
704
705 java_library {
706 name: "bar",
707 static_libs: ["jargen"],
708 srcs: ["c.java"],
709 }
710
711 java_library {
712 name: "baz",
713 libs: ["jargen"],
714 srcs: ["c.java"],
715 }
716 `)
717
718 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
719 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
720 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
721 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
722 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
723
724 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
725 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
726 }
727
728 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
729 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
730 }
731
732 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
733 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
734 }
735
736 if len(barCombined.Inputs) != 2 ||
737 barCombined.Inputs[0].String() != bar.Output.String() ||
738 barCombined.Inputs[1].String() != jargen.Output.String() {
739 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
740 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
741 }
742}
743
Nan Zhang27e284d2018-02-09 21:03:53 +0000744func TestExcludeFileGroupInSrcs(t *testing.T) {
745 ctx := testJava(t, `
746 java_library {
747 name: "foo",
748 srcs: ["a.java", ":foo-srcs"],
749 exclude_srcs: ["a.java", ":foo-excludes"],
750 }
751
752 filegroup {
753 name: "foo-srcs",
754 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
755 }
756
757 filegroup {
758 name: "foo-excludes",
759 srcs: ["java-fg/a.java", "java-fg/b.java"],
760 }
761 `)
762
763 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
764
765 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
766 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
767 }
768}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900769
770func TestJavaSdkLibrary(t *testing.T) {
771 ctx := testJava(t, `
772 droiddoc_template {
773 name: "droiddoc-templates-sdk",
774 path: ".",
775 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900776 java_sdk_library {
777 name: "foo",
778 srcs: ["a.java", "b.java"],
779 api_packages: ["foo"],
780 }
781 java_sdk_library {
782 name: "bar",
783 srcs: ["a.java", "b.java"],
784 api_packages: ["bar"],
785 }
786 java_library {
787 name: "baz",
788 srcs: ["c.java"],
789 libs: ["foo", "bar"],
790 sdk_version: "system_current",
791 }
Jiyong Park1be96912018-05-28 18:02:19 +0900792 java_library {
793 name: "qux",
794 srcs: ["c.java"],
795 libs: ["baz"],
796 sdk_version: "system_current",
797 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900798 `)
799
800 // check the existence of the internal modules
801 ctx.ModuleForTests("foo", "android_common")
802 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix, "android_common")
803 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +0900804 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkTestApiSuffix, "android_common")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900805 ctx.ModuleForTests("foo"+sdkDocsSuffix, "android_common")
806 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +0900807 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkTestApiSuffix, "android_common")
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800808 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_arm64_armv8-a")
Jiyong Park58c518b2018-05-12 22:29:12 +0900809 ctx.ModuleForTests("foo.api.public.28", "")
810 ctx.ModuleForTests("foo.api.system.28", "")
811 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900812
813 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
814 // tests if baz is actually linked to the stubs lib
815 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
816 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
817 "foo.stubs.system.jar")
818 }
819 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +0900820 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900821 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +0900822 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900823 }
824 // test if baz is not linked to the system variant of foo
825 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
826 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
827 "foo.stubs.jar")
828 }
Jiyong Park1be96912018-05-28 18:02:19 +0900829
830 // test if baz has exported SDK lib names foo and bar to qux
831 qux := ctx.ModuleForTests("qux", "android_common")
832 if quxLib, ok := qux.Module().(*Library); ok {
833 sdkLibs := quxLib.ExportedSdkLibs()
834 if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) {
835 t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs)
836 }
837 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900838}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200839
840var compilerFlagsTestCases = []struct {
841 in string
842 out bool
843}{
844 {
845 in: "a",
846 out: false,
847 },
848 {
849 in: "-a",
850 out: true,
851 },
852 {
853 in: "-no-jdk",
854 out: false,
855 },
856 {
857 in: "-no-stdlib",
858 out: false,
859 },
860 {
861 in: "-kotlin-home",
862 out: false,
863 },
864 {
865 in: "-kotlin-home /some/path",
866 out: false,
867 },
868 {
869 in: "-include-runtime",
870 out: false,
871 },
872 {
873 in: "-Xintellij-plugin-root",
874 out: false,
875 },
876}
877
878type mockContext struct {
879 android.ModuleContext
880 result bool
881}
882
883func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
884 // CheckBadCompilerFlags calls this function when the flag should be rejected
885 ctx.result = false
886}
887
888func TestCompilerFlags(t *testing.T) {
889 for _, testCase := range compilerFlagsTestCases {
890 ctx := &mockContext{result: true}
891 CheckKotlincFlags(ctx, []string{testCase.in})
892 if ctx.result != testCase.out {
893 t.Errorf("incorrect output:")
894 t.Errorf(" input: %#v", testCase.in)
895 t.Errorf(" expected: %#v", testCase.out)
896 t.Errorf(" got: %#v", ctx.result)
897 }
898 }
899}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800900
901// TODO(jungjw): Consider making this more robust by ignoring path order.
902func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
903 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
904 flags := strings.Split(variables["javacFlags"], " ")
905 got := ""
906 for _, flag := range flags {
907 keyEnd := strings.Index(flag, "=")
908 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
909 got = flag[keyEnd+1:]
910 break
911 }
912 }
913 if expected != got {
914 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
915 }
916}
917
918func TestPatchModule(t *testing.T) {
919 bp := `
920 java_library {
921 name: "foo",
922 srcs: ["a.java"],
923 }
924
925 java_library {
926 name: "bar",
927 srcs: ["b.java"],
928 no_standard_libs: true,
929 system_modules: "none",
930 patch_module: "java.base",
931 }
932
933 java_library {
934 name: "baz",
935 srcs: ["c.java"],
936 patch_module: "java.base",
937 }
938 `
939
940 t.Run("1.8", func(t *testing.T) {
941 // Test default javac 1.8
942 ctx := testJava(t, bp)
943
944 checkPatchModuleFlag(t, ctx, "foo", "")
945 checkPatchModuleFlag(t, ctx, "bar", "")
946 checkPatchModuleFlag(t, ctx, "baz", "")
947 })
948
949 t.Run("1.9", func(t *testing.T) {
950 // Test again with javac 1.9
951 config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
952 ctx := testContext(config, bp, nil)
953 run(t, ctx, config)
954
955 checkPatchModuleFlag(t, ctx, "foo", "")
956 expected := "java.base=.:" + buildDir
957 checkPatchModuleFlag(t, ctx, "bar", expected)
958 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
959 checkPatchModuleFlag(t, ctx, "baz", expected)
960 })
961}