Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 19 | "android/soong/genrule" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 20 | "fmt" |
| 21 | "io/ioutil" |
| 22 | "os" |
| 23 | "path/filepath" |
Colin Cross | 227d436 | 2017-08-30 14:14:52 -0700 | [diff] [blame] | 24 | "reflect" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 25 | "strings" |
| 26 | "testing" |
| 27 | ) |
| 28 | |
| 29 | var buildDir string |
| 30 | |
| 31 | func setUp() { |
| 32 | var err error |
| 33 | buildDir, err = ioutil.TempDir("", "soong_java_test") |
| 34 | if err != nil { |
| 35 | panic(err) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func tearDown() { |
| 40 | os.RemoveAll(buildDir) |
| 41 | } |
| 42 | |
| 43 | func TestMain(m *testing.M) { |
| 44 | run := func() int { |
| 45 | setUp() |
| 46 | defer tearDown() |
| 47 | |
| 48 | return m.Run() |
| 49 | } |
| 50 | |
| 51 | os.Exit(run()) |
| 52 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 53 | func testJava(t *testing.T, bp string) *android.TestContext { |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 54 | return testJavaWithEnv(t, bp, nil) |
| 55 | } |
| 56 | |
| 57 | func testJavaWithEnv(t *testing.T, bp string, env map[string]string) *android.TestContext { |
| 58 | config := android.TestArchConfig(buildDir, env) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 59 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 60 | ctx := android.NewTestArchContext() |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 61 | ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory)) |
Colin Cross | a60ead8 | 2017-10-02 18:10:21 -0700 | [diff] [blame] | 62 | ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory(true))) |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 63 | ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory)) |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 64 | ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory)) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 65 | ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory)) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 66 | ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory)) |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 67 | ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(genrule.FileGroupFactory)) |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 68 | ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory)) |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 69 | ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators) |
| 70 | ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 71 | ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 72 | ctx.Register() |
| 73 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 74 | extraModules := []string{ |
| 75 | "core-oj", |
| 76 | "core-libart", |
| 77 | "framework", |
| 78 | "ext", |
| 79 | "okhttp", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 80 | "android_stubs_current", |
| 81 | "android_system_stubs_current", |
| 82 | "android_test_stubs_current", |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 83 | "kotlin-stdlib", |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 84 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 85 | |
| 86 | for _, extra := range extraModules { |
| 87 | bp += fmt.Sprintf(` |
| 88 | java_library { |
| 89 | name: "%s", |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 90 | srcs: ["a.java"], |
Colin Cross | 76b5f0c | 2017-08-29 16:02:06 -0700 | [diff] [blame] | 91 | no_standard_libs: true, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 92 | system_modules: "core-system-modules", |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 93 | } |
| 94 | `, extra) |
| 95 | } |
| 96 | |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 97 | if config.TargetOpenJDK9() { |
| 98 | systemModules := []string{ |
| 99 | "core-system-modules", |
| 100 | "android_stubs_current_system_modules", |
| 101 | "android_system_stubs_current_system_modules", |
| 102 | "android_test_stubs_current_system_modules", |
| 103 | } |
| 104 | |
| 105 | for _, extra := range systemModules { |
| 106 | bp += fmt.Sprintf(` |
| 107 | java_system_modules { |
| 108 | name: "%s", |
| 109 | } |
| 110 | `, extra) |
| 111 | } |
| 112 | } |
| 113 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 114 | ctx.MockFileSystem(map[string][]byte{ |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 115 | "Android.bp": []byte(bp), |
| 116 | "a.java": nil, |
| 117 | "b.java": nil, |
| 118 | "c.java": nil, |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 119 | "b.kt": nil, |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 120 | "a.jar": nil, |
| 121 | "b.jar": nil, |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 122 | "res/a": nil, |
| 123 | "res/b": nil, |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 124 | "res2/a": nil, |
| 125 | |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 126 | "prebuilts/sdk/14/android.jar": nil, |
| 127 | "prebuilts/sdk/14/framework.aidl": nil, |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 128 | }) |
| 129 | |
| 130 | _, errs := ctx.ParseBlueprintsFiles("Android.bp") |
| 131 | fail(t, errs) |
| 132 | _, errs = ctx.PrepareBuildActions(config) |
| 133 | fail(t, errs) |
| 134 | |
| 135 | return ctx |
| 136 | } |
| 137 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 138 | func moduleToPath(name string) string { |
| 139 | switch { |
| 140 | case name == `""`: |
| 141 | return name |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 142 | case strings.HasSuffix(name, ".jar"): |
| 143 | return name |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame^] | 144 | case name == "android_stubs_current" || name == "android_system_stubs_current" || |
| 145 | name == "android_test_stubs_current": |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 146 | return filepath.Join(buildDir, ".intermediates", name, "android_common", "javac", name+".jar") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame^] | 147 | default: |
| 148 | return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar") |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 152 | func TestSimple(t *testing.T) { |
| 153 | ctx := testJava(t, ` |
| 154 | java_library { |
| 155 | name: "foo", |
| 156 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 157 | libs: ["bar"], |
| 158 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | java_library { |
| 162 | name: "bar", |
| 163 | srcs: ["b.java"], |
| 164 | } |
| 165 | |
| 166 | java_library { |
| 167 | name: "baz", |
| 168 | srcs: ["c.java"], |
| 169 | } |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 170 | `) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 171 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 172 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame^] | 173 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 174 | |
| 175 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 176 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 177 | } |
| 178 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 179 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame^] | 180 | barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 181 | bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar") |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 182 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame^] | 183 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 184 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame^] | 187 | if !strings.Contains(javac.Args["classpath"], bazTurbine) { |
| 188 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine) |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 192 | t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 196 | func TestArchSpecific(t *testing.T) { |
| 197 | ctx := testJava(t, ` |
| 198 | java_library { |
| 199 | name: "foo", |
| 200 | srcs: ["a.java"], |
| 201 | target: { |
| 202 | android: { |
| 203 | srcs: ["b.java"], |
| 204 | }, |
| 205 | }, |
| 206 | } |
| 207 | `) |
| 208 | |
| 209 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 210 | if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" { |
| 211 | t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs) |
| 212 | } |
| 213 | } |
| 214 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 215 | var classpathTestcases = []struct { |
| 216 | name string |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 217 | moduleType string |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 218 | host android.OsClass |
| 219 | properties string |
| 220 | bootclasspath []string |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 221 | system string |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 222 | classpath []string |
| 223 | }{ |
| 224 | { |
| 225 | name: "default", |
| 226 | bootclasspath: []string{"core-oj", "core-libart"}, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 227 | system: "core-system-modules", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 228 | classpath: []string{"ext", "framework", "okhttp"}, |
| 229 | }, |
| 230 | { |
| 231 | name: "blank sdk version", |
| 232 | properties: `sdk_version: "",`, |
| 233 | bootclasspath: []string{"core-oj", "core-libart"}, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 234 | system: "core-system-modules", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 235 | classpath: []string{"ext", "framework", "okhttp"}, |
| 236 | }, |
| 237 | { |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 238 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 239 | name: "sdk v14", |
| 240 | properties: `sdk_version: "14",`, |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 241 | bootclasspath: []string{`""`}, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 242 | system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 243 | classpath: []string{"prebuilts/sdk/14/android.jar"}, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 244 | }, |
| 245 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 246 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 247 | name: "current", |
| 248 | properties: `sdk_version: "current",`, |
| 249 | bootclasspath: []string{"android_stubs_current"}, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 250 | system: "android_stubs_current_system_modules", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 251 | classpath: []string{}, |
| 252 | }, |
| 253 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 254 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 255 | name: "system_current", |
| 256 | properties: `sdk_version: "system_current",`, |
| 257 | bootclasspath: []string{"android_system_stubs_current"}, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 258 | system: "android_system_stubs_current_system_modules", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 259 | classpath: []string{}, |
| 260 | }, |
| 261 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 262 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 263 | name: "test_current", |
| 264 | properties: `sdk_version: "test_current",`, |
| 265 | bootclasspath: []string{"android_test_stubs_current"}, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 266 | system: "android_test_stubs_current_system_modules", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 267 | classpath: []string{}, |
| 268 | }, |
| 269 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 270 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 271 | name: "nostdlib", |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 272 | properties: `no_standard_libs: true, system_modules: "none"`, |
| 273 | system: "none", |
| 274 | bootclasspath: []string{`""`}, |
| 275 | classpath: []string{}, |
| 276 | }, |
| 277 | { |
| 278 | |
| 279 | name: "nostdlib system_modules", |
| 280 | properties: `no_standard_libs: true, system_modules: "core-system-modules"`, |
| 281 | system: "core-system-modules", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 282 | bootclasspath: []string{`""`}, |
| 283 | classpath: []string{}, |
| 284 | }, |
| 285 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 286 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 287 | name: "host default", |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 288 | moduleType: "java_library_host", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 289 | properties: ``, |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 290 | host: android.Host, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 291 | classpath: []string{}, |
| 292 | }, |
| 293 | { |
| 294 | name: "host nostdlib", |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 295 | moduleType: "java_library_host", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 296 | host: android.Host, |
| 297 | properties: `no_standard_libs: true`, |
| 298 | classpath: []string{}, |
| 299 | }, |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 300 | { |
| 301 | |
| 302 | name: "host supported default", |
| 303 | host: android.Host, |
| 304 | properties: `host_supported: true,`, |
| 305 | classpath: []string{}, |
| 306 | }, |
| 307 | { |
| 308 | name: "host supported nostdlib", |
| 309 | host: android.Host, |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 310 | properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`, |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 311 | classpath: []string{}, |
| 312 | }, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 313 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 314 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 315 | func TestClasspath(t *testing.T) { |
| 316 | for _, testcase := range classpathTestcases { |
| 317 | t.Run(testcase.name, func(t *testing.T) { |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 318 | moduleType := "java_library" |
| 319 | if testcase.moduleType != "" { |
| 320 | moduleType = testcase.moduleType |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 321 | } |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 322 | |
| 323 | bp := moduleType + ` { |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 324 | name: "foo", |
| 325 | srcs: ["a.java"], |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 326 | ` + testcase.properties + ` |
| 327 | }` |
| 328 | |
| 329 | variant := "android_common" |
| 330 | if testcase.host == android.Host { |
| 331 | variant = android.BuildOs.String() + "_common" |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 332 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 333 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 334 | convertModulesToPaths := func(cp []string) []string { |
| 335 | ret := make([]string, len(cp)) |
| 336 | for i, e := range cp { |
| 337 | ret[i] = moduleToPath(e) |
| 338 | } |
| 339 | return ret |
| 340 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 341 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 342 | bootclasspath := convertModulesToPaths(testcase.bootclasspath) |
| 343 | classpath := convertModulesToPaths(testcase.classpath) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 344 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 345 | bc := strings.Join(bootclasspath, ":") |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 346 | if bc != "" { |
| 347 | bc = "-bootclasspath " + bc |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 348 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 349 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 350 | c := strings.Join(classpath, ":") |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 351 | if c != "" { |
| 352 | c = "-classpath " + c |
| 353 | } |
| 354 | system := "" |
| 355 | if testcase.system == "none" { |
| 356 | system = "--system=none" |
| 357 | } else if testcase.system != "" { |
| 358 | system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/" |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 361 | t.Run("1.8", func(t *testing.T) { |
| 362 | // Test default javac 1.8 |
| 363 | ctx := testJava(t, bp) |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 364 | |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 365 | javac := ctx.ModuleForTests("foo", variant).Rule("javac") |
| 366 | |
| 367 | got := javac.Args["bootClasspath"] |
| 368 | if got != bc { |
| 369 | t.Errorf("bootclasspath expected %q != got %q", bc, got) |
| 370 | } |
| 371 | |
| 372 | got = javac.Args["classpath"] |
| 373 | if got != c { |
| 374 | t.Errorf("classpath expected %q != got %q", c, got) |
| 375 | } |
| 376 | |
| 377 | var deps []string |
| 378 | if len(bootclasspath) > 0 && bootclasspath[0] != `""` { |
| 379 | deps = append(deps, bootclasspath...) |
| 380 | } |
| 381 | deps = append(deps, classpath...) |
| 382 | |
| 383 | if !reflect.DeepEqual(javac.Implicits.Strings(), deps) { |
| 384 | t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings()) |
| 385 | } |
| 386 | }) |
| 387 | |
| 388 | // Test again with javac 1.9 |
| 389 | t.Run("1.9", func(t *testing.T) { |
| 390 | ctx := testJavaWithEnv(t, bp, map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"}) |
| 391 | |
| 392 | javac := ctx.ModuleForTests("foo", variant).Rule("javac") |
| 393 | got := javac.Args["bootClasspath"] |
| 394 | expected := system |
| 395 | if testcase.system == "bootclasspath" { |
| 396 | expected = bc |
| 397 | } |
| 398 | if got != expected { |
| 399 | t.Errorf("bootclasspath expected %q != got %q", expected, got) |
| 400 | } |
| 401 | }) |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 402 | }) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | func TestPrebuilts(t *testing.T) { |
| 408 | ctx := testJava(t, ` |
| 409 | java_library { |
| 410 | name: "foo", |
| 411 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 412 | libs: ["bar"], |
| 413 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 416 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 417 | name: "bar", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 418 | jars: ["a.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 421 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 422 | name: "baz", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 423 | jars: ["b.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 424 | } |
| 425 | `) |
| 426 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 427 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame^] | 428 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 429 | |
| 430 | bar := "a.jar" |
| 431 | if !strings.Contains(javac.Args["classpath"], bar) { |
| 432 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar) |
| 433 | } |
| 434 | |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 435 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" { |
| 436 | t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, "b.jar") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 440 | func TestDefaults(t *testing.T) { |
| 441 | ctx := testJava(t, ` |
| 442 | java_defaults { |
| 443 | name: "defaults", |
| 444 | srcs: ["a.java"], |
| 445 | libs: ["bar"], |
| 446 | static_libs: ["baz"], |
| 447 | } |
| 448 | |
| 449 | java_library { |
| 450 | name: "foo", |
| 451 | defaults: ["defaults"], |
| 452 | } |
| 453 | |
| 454 | java_library { |
| 455 | name: "bar", |
| 456 | srcs: ["b.java"], |
| 457 | } |
| 458 | |
| 459 | java_library { |
| 460 | name: "baz", |
| 461 | srcs: ["c.java"], |
| 462 | } |
| 463 | `) |
| 464 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 465 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame^] | 466 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 467 | |
| 468 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 469 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 470 | } |
| 471 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame^] | 472 | barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 473 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 474 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 477 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 478 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 479 | t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 483 | func TestResources(t *testing.T) { |
| 484 | var table = []struct { |
| 485 | name string |
| 486 | prop string |
| 487 | extra string |
| 488 | args string |
| 489 | }{ |
| 490 | { |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 491 | // Test that a module with java_resource_dirs includes the files |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 492 | name: "resource dirs", |
| 493 | prop: `java_resource_dirs: ["res"]`, |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 494 | args: "-C res -f res/a -f res/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 495 | }, |
| 496 | { |
| 497 | // Test that a module with java_resources includes the files |
| 498 | name: "resource files", |
| 499 | prop: `java_resources: ["res/a", "res/b"]`, |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 500 | args: "-C . -f res/a -f res/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 501 | }, |
| 502 | { |
| 503 | // Test that a module with a filegroup in java_resources includes the files with the |
| 504 | // path prefix |
| 505 | name: "resource filegroup", |
| 506 | prop: `java_resources: [":foo-res"]`, |
| 507 | extra: ` |
| 508 | filegroup { |
| 509 | name: "foo-res", |
| 510 | path: "res", |
| 511 | srcs: ["res/a", "res/b"], |
| 512 | }`, |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 513 | args: "-C res -f res/a -f res/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 514 | }, |
| 515 | { |
| 516 | // Test that a module with "include_srcs: true" includes its source files in the resources jar |
| 517 | name: "include sources", |
| 518 | prop: `include_srcs: true`, |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 519 | args: "-C . -f a.java -f b.java -f c.java", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 520 | }, |
| 521 | } |
| 522 | |
| 523 | for _, test := range table { |
| 524 | t.Run(test.name, func(t *testing.T) { |
| 525 | ctx := testJava(t, ` |
| 526 | java_library { |
| 527 | name: "foo", |
| 528 | srcs: [ |
| 529 | "a.java", |
| 530 | "b.java", |
| 531 | "c.java", |
| 532 | ], |
| 533 | `+test.prop+`, |
| 534 | } |
| 535 | `+test.extra) |
| 536 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 537 | foo := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar") |
| 538 | fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar") |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 539 | |
| 540 | if !inList(fooRes.Output.String(), foo.Inputs.Strings()) { |
| 541 | t.Errorf("foo combined jars %v does not contain %q", |
| 542 | foo.Inputs.Strings(), fooRes.Output.String()) |
| 543 | } |
| 544 | |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 545 | if fooRes.Args["jarArgs"] != test.args { |
| 546 | t.Errorf("foo resource jar args %q is not %q", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 547 | fooRes.Args["jarArgs"], test.args) |
| 548 | } |
| 549 | }) |
| 550 | } |
| 551 | } |
| 552 | |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 553 | func TestExcludeResources(t *testing.T) { |
| 554 | ctx := testJava(t, ` |
| 555 | java_library { |
| 556 | name: "foo", |
| 557 | srcs: ["a.java"], |
| 558 | java_resource_dirs: ["res", "res2"], |
| 559 | exclude_java_resource_dirs: ["res2"], |
| 560 | } |
| 561 | |
| 562 | java_library { |
| 563 | name: "bar", |
| 564 | srcs: ["a.java"], |
| 565 | java_resources: ["res/*"], |
| 566 | exclude_java_resources: ["res/b"], |
| 567 | } |
| 568 | `) |
| 569 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 570 | fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar") |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 571 | |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 572 | expected := "-C res -f res/a -f res/b" |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 573 | if fooRes.Args["jarArgs"] != expected { |
| 574 | t.Errorf("foo resource jar args %q is not %q", |
| 575 | fooRes.Args["jarArgs"], expected) |
| 576 | |
| 577 | } |
| 578 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 579 | barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar") |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 580 | |
| 581 | expected = "-C . -f res/a" |
| 582 | if barRes.Args["jarArgs"] != expected { |
| 583 | t.Errorf("bar resource jar args %q is not %q", |
| 584 | barRes.Args["jarArgs"], expected) |
| 585 | |
| 586 | } |
| 587 | } |
| 588 | |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 589 | func TestGeneratedSources(t *testing.T) { |
| 590 | ctx := testJava(t, ` |
| 591 | java_library { |
| 592 | name: "foo", |
| 593 | srcs: [ |
| 594 | "a*.java", |
| 595 | ":gen", |
| 596 | "b*.java", |
| 597 | ], |
| 598 | } |
| 599 | |
| 600 | genrule { |
| 601 | name: "gen", |
| 602 | tool_files: ["res/a"], |
| 603 | out: ["gen.java"], |
| 604 | } |
| 605 | `) |
| 606 | |
| 607 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 608 | genrule := ctx.ModuleForTests("gen", "").Rule("generator") |
| 609 | |
| 610 | if len(genrule.Outputs) != 1 || filepath.Base(genrule.Outputs[0].String()) != "gen.java" { |
| 611 | t.Fatalf(`gen output file %v is not [".../gen.java"]`, genrule.Outputs.Strings()) |
| 612 | } |
| 613 | |
| 614 | if len(javac.Inputs) != 3 || |
| 615 | javac.Inputs[0].String() != "a.java" || |
| 616 | javac.Inputs[1].String() != genrule.Outputs[0].String() || |
| 617 | javac.Inputs[2].String() != "b.java" { |
| 618 | t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs) |
| 619 | } |
| 620 | } |
| 621 | |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 622 | func TestKotlin(t *testing.T) { |
| 623 | ctx := testJava(t, ` |
| 624 | java_library { |
| 625 | name: "foo", |
| 626 | srcs: ["a.java", "b.kt"], |
| 627 | } |
| 628 | `) |
| 629 | |
| 630 | kotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc") |
| 631 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 632 | jar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar") |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 633 | |
| 634 | if len(kotlinc.Inputs) != 2 || kotlinc.Inputs[0].String() != "a.java" || |
| 635 | kotlinc.Inputs[1].String() != "b.kt" { |
| 636 | t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, kotlinc.Inputs) |
| 637 | } |
| 638 | |
| 639 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 640 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 641 | } |
| 642 | |
| 643 | if !strings.Contains(javac.Args["classpath"], kotlinc.Output.String()) { |
| 644 | t.Errorf("foo classpath %v does not contain %q", |
| 645 | javac.Args["classpath"], kotlinc.Output.String()) |
| 646 | } |
| 647 | |
| 648 | if !inList(kotlinc.Output.String(), jar.Inputs.Strings()) { |
| 649 | t.Errorf("foo jar inputs %v does not contain %q", |
| 650 | jar.Inputs.Strings(), kotlinc.Output.String()) |
| 651 | } |
| 652 | } |
| 653 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 654 | func fail(t *testing.T, errs []error) { |
| 655 | if len(errs) > 0 { |
| 656 | for _, err := range errs { |
| 657 | t.Error(err) |
| 658 | } |
| 659 | t.FailNow() |
| 660 | } |
| 661 | } |