blob: a0d962e3ed6567b00761651e20f1ed9480c5e4c1 [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))
Colin Crossa97c5d32018-03-28 14:58:31 -070074 ctx.RegisterModuleType("android_library", android.ModuleFactoryAdaptor(AndroidLibraryFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070075 ctx.RegisterModuleType("android_test", android.ModuleFactoryAdaptor(AndroidTestFactory))
Colin Cross638149e2019-01-05 22:12:12 -080076 ctx.RegisterModuleType("android_test_helper_app", android.ModuleFactoryAdaptor(AndroidTestHelperAppFactory))
77 ctx.RegisterModuleType("java_binary", android.ModuleFactoryAdaptor(BinaryFactory))
Colin Cross6b4a32d2017-12-05 13:42:45 -080078 ctx.RegisterModuleType("java_binary_host", android.ModuleFactoryAdaptor(BinaryHostFactory))
Colin Cross9ae1b922018-06-26 17:59:05 -070079 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory))
Colin Cross2acdae82017-09-15 19:44:24 -070080 ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
Colin Cross638149e2019-01-05 22:12:12 -080081 ctx.RegisterModuleType("java_test", android.ModuleFactoryAdaptor(TestFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070082 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross89536d42017-07-07 14:35:50 -070083 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross1369cdb2017-09-29 17:58:17 -070084 ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory))
Colin Cross54250902017-12-05 09:28:08 -080085 ctx.RegisterModuleType("java_genrule", android.ModuleFactoryAdaptor(genRuleFactory))
Colin Crossbe9cdb82019-01-21 21:37:16 -080086 ctx.RegisterModuleType("java_plugin", android.ModuleFactoryAdaptor(PluginFactory))
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070087 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Colin Cross54190b32017-10-09 15:34:10 -070088 ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory))
Nan Zhang581fd212018-01-10 16:06:12 -080089 ctx.RegisterModuleType("droiddoc", android.ModuleFactoryAdaptor(DroiddocFactory))
90 ctx.RegisterModuleType("droiddoc_host", android.ModuleFactoryAdaptor(DroiddocHostFactory))
Nan Zhangf4936b02018-08-01 15:00:28 -070091 ctx.RegisterModuleType("droiddoc_template", android.ModuleFactoryAdaptor(ExportedDroiddocDirFactory))
Jiyong Parkc678ad32018-04-10 13:07:10 +090092 ctx.RegisterModuleType("java_sdk_library", android.ModuleFactoryAdaptor(sdkLibraryFactory))
Jiyong Park58c518b2018-05-12 22:29:12 +090093 ctx.RegisterModuleType("prebuilt_apis", android.ModuleFactoryAdaptor(prebuiltApisFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070094 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
95 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070096 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Parkc678ad32018-04-10 13:07:10 +090097 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Park58c518b2018-05-12 22:29:12 +090098 ctx.TopDown("prebuilt_apis", prebuiltApisMutator).Parallel()
Jiyong Parkc678ad32018-04-10 13:07:10 +090099 ctx.TopDown("java_sdk_library", sdkLibraryMutator).Parallel()
100 })
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800101 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross98fd5742019-01-09 23:04:25 -0800102 ctx.RegisterPreSingletonType("sdk", android.SingletonFactoryAdaptor(sdkSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -0700103
104 // Register module types and mutators from cc needed for JNI testing
105 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
106 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
107 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
108 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
109 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
110 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
111 })
112
Colin Cross72bb3632017-07-13 16:23:21 -0700113 ctx.Register()
114
Colin Cross4c428df2017-09-15 17:36:05 -0700115 extraModules := []string{
Colin Cross86a60ae2018-05-29 14:44:55 -0700116 "core-lambda-stubs",
Colin Cross4c428df2017-09-15 17:36:05 -0700117 "framework",
118 "ext",
Colin Cross2acdae82017-09-15 19:44:24 -0700119 "android_stubs_current",
120 "android_system_stubs_current",
121 "android_test_stubs_current",
Colin Crossf19b9bb2018-03-26 14:42:44 -0700122 "core.current.stubs",
Neil Fullerbf210f72018-10-09 16:19:19 +0100123 "core.platform.api.stubs",
Colin Cross93e85952017-08-15 13:34:18 -0700124 "kotlin-stdlib",
Colin Crossafbb1732019-01-17 15:42:52 -0800125 "kotlin-annotations",
Colin Cross4c428df2017-09-15 17:36:05 -0700126 }
Colin Cross72bb3632017-07-13 16:23:21 -0700127
128 for _, extra := range extraModules {
129 bp += fmt.Sprintf(`
130 java_library {
131 name: "%s",
Colin Cross0a6e0072017-08-30 14:24:55 -0700132 srcs: ["a.java"],
Colin Cross76b5f0c2017-08-29 16:02:06 -0700133 no_standard_libs: true,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700134 sdk_version: "core_current",
Neil Fullerbf210f72018-10-09 16:19:19 +0100135 system_modules: "core-platform-api-stubs-system-modules",
Colin Cross72bb3632017-07-13 16:23:21 -0700136 }
137 `, extra)
138 }
139
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800140 bp += `
141 android_app {
142 name: "framework-res",
143 no_framework_libs: true,
144 }
145 `
146
Colin Cross527012a2017-11-30 22:56:16 -0800147 systemModules := []string{
148 "core-system-modules",
Neil Fullerbf210f72018-10-09 16:19:19 +0100149 "core-platform-api-stubs-system-modules",
Colin Cross527012a2017-11-30 22:56:16 -0800150 "android_stubs_current_system_modules",
151 "android_system_stubs_current_system_modules",
152 "android_test_stubs_current_system_modules",
153 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700154
Colin Cross527012a2017-11-30 22:56:16 -0800155 for _, extra := range systemModules {
156 bp += fmt.Sprintf(`
Colin Cross1369cdb2017-09-29 17:58:17 -0700157 java_system_modules {
158 name: "%s",
159 }
160 `, extra)
Colin Cross1369cdb2017-09-29 17:58:17 -0700161 }
162
Colin Cross824bee32017-11-22 17:27:51 -0800163 mockFS := map[string][]byte{
Jiyong Park58c518b2018-05-12 22:29:12 +0900164 "Android.bp": []byte(bp),
165 "a.java": nil,
166 "b.java": nil,
167 "c.java": nil,
168 "b.kt": nil,
169 "a.jar": nil,
170 "b.jar": nil,
171 "java-res/a/a": nil,
172 "java-res/b/b": nil,
173 "java-res2/a": nil,
174 "java-fg/a.java": nil,
175 "java-fg/b.java": nil,
176 "java-fg/c.java": nil,
177 "api/current.txt": nil,
178 "api/removed.txt": nil,
179 "api/system-current.txt": nil,
180 "api/system-removed.txt": nil,
181 "api/test-current.txt": nil,
182 "api/test-removed.txt": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700183
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100184 "prebuilts/sdk/14/public/android.jar": nil,
185 "prebuilts/sdk/14/public/framework.aidl": nil,
186 "prebuilts/sdk/14/system/android.jar": nil,
Colin Cross98fd5742019-01-09 23:04:25 -0800187 "prebuilts/sdk/17/public/android.jar": nil,
188 "prebuilts/sdk/17/public/framework.aidl": nil,
189 "prebuilts/sdk/17/system/android.jar": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700190 "prebuilts/sdk/current/core/android.jar": nil,
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100191 "prebuilts/sdk/current/public/android.jar": nil,
192 "prebuilts/sdk/current/public/framework.aidl": nil,
193 "prebuilts/sdk/current/public/core.jar": nil,
194 "prebuilts/sdk/current/system/android.jar": nil,
195 "prebuilts/sdk/current/test/android.jar": nil,
Jiyong Park58c518b2018-05-12 22:29:12 +0900196 "prebuilts/sdk/28/public/api/foo.txt": nil,
197 "prebuilts/sdk/28/system/api/foo.txt": nil,
198 "prebuilts/sdk/28/test/api/foo.txt": nil,
199 "prebuilts/sdk/28/public/api/foo-removed.txt": nil,
200 "prebuilts/sdk/28/system/api/foo-removed.txt": nil,
201 "prebuilts/sdk/28/test/api/foo-removed.txt": nil,
202 "prebuilts/sdk/28/public/api/bar.txt": nil,
203 "prebuilts/sdk/28/system/api/bar.txt": nil,
204 "prebuilts/sdk/28/test/api/bar.txt": nil,
205 "prebuilts/sdk/28/public/api/bar-removed.txt": nil,
206 "prebuilts/sdk/28/system/api/bar-removed.txt": nil,
207 "prebuilts/sdk/28/test/api/bar-removed.txt": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700208 "prebuilts/sdk/tools/core-lambda-stubs.jar": nil,
Sundong Ahn27eecb92018-06-21 13:47:17 +0900209 "prebuilts/sdk/Android.bp": []byte(`prebuilt_apis { name: "sdk", api_dirs: ["14", "28", "current"],}`),
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800210
211 // For framework-res, which is an implicit dependency for framework
212 "AndroidManifest.xml": nil,
213 "build/target/product/security/testkey": nil,
Colin Cross6b4a32d2017-12-05 13:42:45 -0800214
215 "build/soong/scripts/jar-wrapper.sh": nil,
Colin Cross7fdd2b72018-01-02 18:14:25 -0800216
Colin Cross66dbc0b2017-12-28 12:23:20 -0800217 "build/make/core/proguard.flags": nil,
218 "build/make/core/proguard_basic_keeps.flags": nil,
219
Colin Cross7fdd2b72018-01-02 18:14:25 -0800220 "jdk8/jre/lib/jce.jar": nil,
221 "jdk8/jre/lib/rt.jar": nil,
Nan Zhang61819ce2018-05-04 18:49:16 -0700222 "jdk8/lib/tools.jar": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800223
224 "bar-doc/a.java": nil,
225 "bar-doc/b.java": nil,
Jiyong Park1e440682018-05-23 18:42:04 +0900226 "bar-doc/IFoo.aidl": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800227 "bar-doc/known_oj_tags.txt": nil,
228 "external/doclava/templates-sdk": nil,
Colin Cross824bee32017-11-22 17:27:51 -0800229 }
230
231 for k, v := range fs {
232 mockFS[k] = v
233 }
234
235 ctx.MockFileSystem(mockFS)
Colin Cross72bb3632017-07-13 16:23:21 -0700236
Colin Cross527012a2017-11-30 22:56:16 -0800237 return ctx
238}
239
240func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800241 t.Helper()
Jiyong Park58c518b2018-05-12 22:29:12 +0900242 _, errs := ctx.ParseFileList(".", []string{"Android.bp", "prebuilts/sdk/Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +0800243 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700244 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800245 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800246}
247
248func testJava(t *testing.T, bp string) *android.TestContext {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800249 t.Helper()
Colin Cross527012a2017-11-30 22:56:16 -0800250 config := testConfig(nil)
251 ctx := testContext(config, bp, nil)
252 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700253
254 return ctx
255}
256
Colin Cross2acdae82017-09-15 19:44:24 -0700257func moduleToPath(name string) string {
258 switch {
259 case name == `""`:
260 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700261 case strings.HasSuffix(name, ".jar"):
262 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700263 default:
264 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700265 }
266}
267
Colin Cross72bb3632017-07-13 16:23:21 -0700268func TestSimple(t *testing.T) {
269 ctx := testJava(t, `
270 java_library {
271 name: "foo",
272 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700273 libs: ["bar"],
274 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700275 }
276
277 java_library {
278 name: "bar",
279 srcs: ["b.java"],
280 }
281
282 java_library {
283 name: "baz",
284 srcs: ["c.java"],
285 }
Colin Crossd5934c82017-10-02 13:55:26 -0700286 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700287
Colin Cross4c428df2017-09-15 17:36:05 -0700288 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700289 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700290
291 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
292 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
293 }
294
Colin Cross1ee23172017-10-18 14:44:18 -0700295 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700296 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
297 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700298
Nan Zhanged19fc32017-10-19 13:06:22 -0700299 if !strings.Contains(javac.Args["classpath"], barTurbine) {
300 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700301 }
302
Nan Zhanged19fc32017-10-19 13:06:22 -0700303 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
304 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700305 }
306
307 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
308 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700309 }
310}
311
Colin Crossd5934c82017-10-02 13:55:26 -0700312func TestArchSpecific(t *testing.T) {
313 ctx := testJava(t, `
314 java_library {
315 name: "foo",
316 srcs: ["a.java"],
317 target: {
318 android: {
319 srcs: ["b.java"],
320 },
321 },
322 }
323 `)
324
325 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
326 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
327 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
328 }
329}
330
Colin Cross6b4a32d2017-12-05 13:42:45 -0800331func TestBinary(t *testing.T) {
332 ctx := testJava(t, `
333 java_library_host {
334 name: "foo",
335 srcs: ["a.java"],
336 }
337
338 java_binary_host {
339 name: "bar",
340 srcs: ["b.java"],
341 static_libs: ["foo"],
342 }
343 `)
344
345 buildOS := android.BuildOs.String()
346
347 bar := ctx.ModuleForTests("bar", buildOS+"_common")
348 barJar := bar.Output("bar.jar").Output.String()
349 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
350 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
351
352 // Test that the install binary wrapper depends on the installed jar file
353 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
354 t.Errorf("expected binary wrapper implicits [%q], got %v",
355 barJar, barWrapperDeps)
356 }
357
358}
359
Colin Cross72bb3632017-07-13 16:23:21 -0700360func TestPrebuilts(t *testing.T) {
361 ctx := testJava(t, `
362 java_library {
363 name: "foo",
364 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700365 libs: ["bar"],
366 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700367 }
368
Colin Cross74d73e22017-08-02 11:05:49 -0700369 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700370 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700371 jars: ["a.jar"],
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: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700376 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700377 }
378 `)
379
Colin Cross4c428df2017-09-15 17:36:05 -0700380 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700381 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross37f6d792018-07-12 12:28:41 -0700382 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
383 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700384
Colin Cross37f6d792018-07-12 12:28:41 -0700385 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
386 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700387 }
388
Colin Cross37f6d792018-07-12 12:28:41 -0700389 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
390 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700391 }
392}
393
Colin Cross89536d42017-07-07 14:35:50 -0700394func TestDefaults(t *testing.T) {
395 ctx := testJava(t, `
396 java_defaults {
397 name: "defaults",
398 srcs: ["a.java"],
399 libs: ["bar"],
400 static_libs: ["baz"],
401 }
402
403 java_library {
404 name: "foo",
405 defaults: ["defaults"],
406 }
407
408 java_library {
409 name: "bar",
410 srcs: ["b.java"],
411 }
412
413 java_library {
414 name: "baz",
415 srcs: ["c.java"],
416 }
417 `)
418
Colin Cross4c428df2017-09-15 17:36:05 -0700419 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700420 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700421
422 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
423 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
424 }
425
Nan Zhanged19fc32017-10-19 13:06:22 -0700426 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
427 if !strings.Contains(javac.Args["classpath"], barTurbine) {
428 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700429 }
430
Colin Cross1ee23172017-10-18 14:44:18 -0700431 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700432 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
433 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700434 }
435}
436
Colin Cross0f37af02017-09-27 17:42:05 -0700437func TestResources(t *testing.T) {
438 var table = []struct {
439 name string
440 prop string
441 extra string
442 args string
443 }{
444 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700445 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700446 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800447 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700448 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700449 },
450 {
451 // Test that a module with java_resources includes the files
452 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700453 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
454 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700455 },
456 {
457 // Test that a module with a filegroup in java_resources includes the files with the
458 // path prefix
459 name: "resource filegroup",
460 prop: `java_resources: [":foo-res"]`,
461 extra: `
462 filegroup {
463 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800464 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700465 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700466 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700467 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700468 },
469 {
470 // Test that a module with "include_srcs: true" includes its source files in the resources jar
471 name: "include sources",
472 prop: `include_srcs: true`,
Colin Crossaf9c55b2017-10-03 14:50:08 -0700473 args: "-C . -f a.java -f b.java -f c.java",
Colin Cross0f37af02017-09-27 17:42:05 -0700474 },
Colin Cross0ead1d72018-04-10 13:07:42 -0700475 {
476 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
477 name: "wildcard dirs",
478 prop: `java_resource_dirs: ["java-res/*"]`,
479 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
480 },
481 {
482 // Test that a module exclude_java_resource_dirs excludes the files
483 name: "wildcard dirs",
484 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
485 args: "-C java-res/a -f java-res/a/a",
486 },
Colin Crosscedd4762018-09-13 11:26:19 -0700487 {
488 // Test wildcards in java_resources
489 name: "wildcard files",
490 prop: `java_resources: ["java-res/**/*"]`,
491 args: "-C . -f java-res/a/a -f java-res/b/b",
492 },
493 {
494 // Test exclude_java_resources with java_resources
495 name: "wildcard files with exclude",
496 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
497 args: "-C . -f java-res/a/a",
498 },
499 {
500 // Test exclude_java_resources with java_resource_dirs
501 name: "resource dirs with exclude files",
502 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
503 args: "-C java-res -f java-res/a/a",
504 },
505 {
506 // Test exclude_java_resource_dirs with java_resource_dirs
507 name: "resource dirs with exclude files",
508 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
509 args: "-C java-res -f java-res/a/a -f java-res/b/b",
510 },
Colin Cross0f37af02017-09-27 17:42:05 -0700511 }
512
513 for _, test := range table {
514 t.Run(test.name, func(t *testing.T) {
515 ctx := testJava(t, `
516 java_library {
517 name: "foo",
518 srcs: [
519 "a.java",
520 "b.java",
521 "c.java",
522 ],
523 `+test.prop+`,
524 }
525 `+test.extra)
526
Colin Cross331a1212018-08-15 20:40:52 -0700527 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700528 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700529
530 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
531 t.Errorf("foo combined jars %v does not contain %q",
532 foo.Inputs.Strings(), fooRes.Output.String())
533 }
534
Colin Crossaf9c55b2017-10-03 14:50:08 -0700535 if fooRes.Args["jarArgs"] != test.args {
536 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700537 fooRes.Args["jarArgs"], test.args)
538 }
539 })
540 }
541}
542
Colin Cross54190b32017-10-09 15:34:10 -0700543func TestGeneratedSources(t *testing.T) {
544 ctx := testJava(t, `
545 java_library {
546 name: "foo",
547 srcs: [
548 "a*.java",
549 ":gen",
550 "b*.java",
551 ],
552 }
553
554 genrule {
555 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800556 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700557 out: ["gen.java"],
558 }
559 `)
560
561 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
562 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
563
Colin Cross15e86d92017-10-20 15:07:08 -0700564 if filepath.Base(genrule.Output.String()) != "gen.java" {
565 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700566 }
567
568 if len(javac.Inputs) != 3 ||
569 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700570 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700571 javac.Inputs[2].String() != "b.java" {
572 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
573 }
574}
575
Nan Zhang61eaedb2017-11-02 13:28:15 -0700576func TestTurbine(t *testing.T) {
577 ctx := testJava(t, `
578 java_library {
579 name: "foo",
580 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900581 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700582 }
583
584 java_library {
585 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800586 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700587 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900588 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700589 }
590
591 java_library {
592 name: "baz",
593 srcs: ["c.java"],
594 libs: ["bar"],
595 sdk_version: "14",
596 }
597 `)
598
599 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
600 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
601 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
602 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
603 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
604
605 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
606 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
607 }
608
609 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
610 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
611 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
612 }
613 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
614 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
615 }
616 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
617 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
618 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100619 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700620 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100621 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -0700622 }
623}
624
625func TestSharding(t *testing.T) {
626 ctx := testJava(t, `
627 java_library {
628 name: "bar",
629 srcs: ["a.java","b.java","c.java"],
630 javac_shard_size: 1
631 }
632 `)
633
634 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
635 for i := 0; i < 3; i++ {
636 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
637 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
638 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
639 }
640 }
641}
642
Nan Zhang581fd212018-01-10 16:06:12 -0800643func TestDroiddoc(t *testing.T) {
644 ctx := testJava(t, `
Dan Willemsencc090972018-02-26 14:33:31 -0800645 droiddoc_template {
646 name: "droiddoc-templates-sdk",
647 path: ".",
648 }
Nan Zhang581fd212018-01-10 16:06:12 -0800649 droiddoc {
650 name: "bar-doc",
651 srcs: [
652 "bar-doc/*.java",
Jiyong Park1e440682018-05-23 18:42:04 +0900653 "bar-doc/IFoo.aidl",
Nan Zhang581fd212018-01-10 16:06:12 -0800654 ],
655 exclude_srcs: [
656 "bar-doc/b.java"
657 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800658 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800659 hdf: [
660 "android.whichdoc offline",
661 ],
662 knowntags: [
663 "bar-doc/known_oj_tags.txt",
664 ],
665 proofread_file: "libcore-proofread.txt",
666 todo_file: "libcore-docs-todo.html",
667 args: "-offlinemode -title \"libcore\"",
668 }
669 `)
670
Nan Zhang3bb14202018-02-28 10:14:25 -0800671 stubsJar := filepath.Join(buildDir, ".intermediates", "bar-doc", "android_common", "bar-doc-stubs.srcjar")
672 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Output("bar-doc-stubs.srcjar")
Nan Zhang581fd212018-01-10 16:06:12 -0800673 if stubsJar != barDoc.Output.String() {
674 t.Errorf("expected stubs Jar [%q], got %q", stubsJar, barDoc.Output.String())
675 }
Jiyong Park1e440682018-05-23 18:42:04 +0900676 inputs := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc").Inputs
677 var javaSrcs []string
678 for _, i := range inputs {
679 javaSrcs = append(javaSrcs, i.Base())
680 }
681 if len(javaSrcs) != 2 || javaSrcs[0] != "a.java" || javaSrcs[1] != "IFoo.java" {
682 t.Errorf("inputs of bar-doc must be []string{\"a.java\", \"IFoo.java\", but was %#v.", javaSrcs)
683 }
Nan Zhang581fd212018-01-10 16:06:12 -0800684}
685
Colin Cross54250902017-12-05 09:28:08 -0800686func TestJarGenrules(t *testing.T) {
687 ctx := testJava(t, `
688 java_library {
689 name: "foo",
690 srcs: ["a.java"],
691 }
692
693 java_genrule {
694 name: "jargen",
695 tool_files: ["b.java"],
696 cmd: "$(location b.java) $(in) $(out)",
697 out: ["jargen.jar"],
698 srcs: [":foo"],
699 }
700
701 java_library {
702 name: "bar",
703 static_libs: ["jargen"],
704 srcs: ["c.java"],
705 }
706
707 java_library {
708 name: "baz",
709 libs: ["jargen"],
710 srcs: ["c.java"],
711 }
712 `)
713
714 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
715 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
716 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
717 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
718 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
719
720 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
721 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
722 }
723
724 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
725 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
726 }
727
728 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
729 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
730 }
731
732 if len(barCombined.Inputs) != 2 ||
733 barCombined.Inputs[0].String() != bar.Output.String() ||
734 barCombined.Inputs[1].String() != jargen.Output.String() {
735 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
736 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
737 }
738}
739
Nan Zhang27e284d2018-02-09 21:03:53 +0000740func TestExcludeFileGroupInSrcs(t *testing.T) {
741 ctx := testJava(t, `
742 java_library {
743 name: "foo",
744 srcs: ["a.java", ":foo-srcs"],
745 exclude_srcs: ["a.java", ":foo-excludes"],
746 }
747
748 filegroup {
749 name: "foo-srcs",
750 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
751 }
752
753 filegroup {
754 name: "foo-excludes",
755 srcs: ["java-fg/a.java", "java-fg/b.java"],
756 }
757 `)
758
759 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
760
761 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
762 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
763 }
764}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900765
766func TestJavaSdkLibrary(t *testing.T) {
767 ctx := testJava(t, `
768 droiddoc_template {
769 name: "droiddoc-templates-sdk",
770 path: ".",
771 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900772 java_sdk_library {
773 name: "foo",
774 srcs: ["a.java", "b.java"],
775 api_packages: ["foo"],
776 }
777 java_sdk_library {
778 name: "bar",
779 srcs: ["a.java", "b.java"],
780 api_packages: ["bar"],
781 }
782 java_library {
783 name: "baz",
784 srcs: ["c.java"],
785 libs: ["foo", "bar"],
786 sdk_version: "system_current",
787 }
Jiyong Park1be96912018-05-28 18:02:19 +0900788 java_library {
789 name: "qux",
790 srcs: ["c.java"],
791 libs: ["baz"],
792 sdk_version: "system_current",
793 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900794 `)
795
796 // check the existence of the internal modules
797 ctx.ModuleForTests("foo", "android_common")
798 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix, "android_common")
799 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +0900800 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkTestApiSuffix, "android_common")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900801 ctx.ModuleForTests("foo"+sdkDocsSuffix, "android_common")
802 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +0900803 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkTestApiSuffix, "android_common")
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800804 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_arm64_armv8-a")
Jiyong Park58c518b2018-05-12 22:29:12 +0900805 ctx.ModuleForTests("foo.api.public.28", "")
806 ctx.ModuleForTests("foo.api.system.28", "")
807 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900808
809 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
810 // tests if baz is actually linked to the stubs lib
811 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
812 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
813 "foo.stubs.system.jar")
814 }
815 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +0900816 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900817 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +0900818 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900819 }
820 // test if baz is not linked to the system variant of foo
821 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
822 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
823 "foo.stubs.jar")
824 }
Jiyong Park1be96912018-05-28 18:02:19 +0900825
826 // test if baz has exported SDK lib names foo and bar to qux
827 qux := ctx.ModuleForTests("qux", "android_common")
828 if quxLib, ok := qux.Module().(*Library); ok {
829 sdkLibs := quxLib.ExportedSdkLibs()
830 if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) {
831 t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs)
832 }
833 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900834}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200835
836var compilerFlagsTestCases = []struct {
837 in string
838 out bool
839}{
840 {
841 in: "a",
842 out: false,
843 },
844 {
845 in: "-a",
846 out: true,
847 },
848 {
849 in: "-no-jdk",
850 out: false,
851 },
852 {
853 in: "-no-stdlib",
854 out: false,
855 },
856 {
857 in: "-kotlin-home",
858 out: false,
859 },
860 {
861 in: "-kotlin-home /some/path",
862 out: false,
863 },
864 {
865 in: "-include-runtime",
866 out: false,
867 },
868 {
869 in: "-Xintellij-plugin-root",
870 out: false,
871 },
872}
873
874type mockContext struct {
875 android.ModuleContext
876 result bool
877}
878
879func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
880 // CheckBadCompilerFlags calls this function when the flag should be rejected
881 ctx.result = false
882}
883
884func TestCompilerFlags(t *testing.T) {
885 for _, testCase := range compilerFlagsTestCases {
886 ctx := &mockContext{result: true}
887 CheckKotlincFlags(ctx, []string{testCase.in})
888 if ctx.result != testCase.out {
889 t.Errorf("incorrect output:")
890 t.Errorf(" input: %#v", testCase.in)
891 t.Errorf(" expected: %#v", testCase.out)
892 t.Errorf(" got: %#v", ctx.result)
893 }
894 }
895}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800896
897// TODO(jungjw): Consider making this more robust by ignoring path order.
898func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
899 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
900 flags := strings.Split(variables["javacFlags"], " ")
901 got := ""
902 for _, flag := range flags {
903 keyEnd := strings.Index(flag, "=")
904 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
905 got = flag[keyEnd+1:]
906 break
907 }
908 }
909 if expected != got {
910 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
911 }
912}
913
914func TestPatchModule(t *testing.T) {
915 bp := `
916 java_library {
917 name: "foo",
918 srcs: ["a.java"],
919 }
920
921 java_library {
922 name: "bar",
923 srcs: ["b.java"],
924 no_standard_libs: true,
925 system_modules: "none",
926 patch_module: "java.base",
927 }
928
929 java_library {
930 name: "baz",
931 srcs: ["c.java"],
932 patch_module: "java.base",
933 }
934 `
935
936 t.Run("1.8", func(t *testing.T) {
937 // Test default javac 1.8
938 ctx := testJava(t, bp)
939
940 checkPatchModuleFlag(t, ctx, "foo", "")
941 checkPatchModuleFlag(t, ctx, "bar", "")
942 checkPatchModuleFlag(t, ctx, "baz", "")
943 })
944
945 t.Run("1.9", func(t *testing.T) {
946 // Test again with javac 1.9
947 config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
948 ctx := testContext(config, bp, nil)
949 run(t, ctx, config)
950
951 checkPatchModuleFlag(t, ctx, "foo", "")
952 expected := "java.base=.:" + buildDir
953 checkPatchModuleFlag(t, ctx, "bar", expected)
954 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
955 checkPatchModuleFlag(t, ctx, "baz", expected)
956 })
957}