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 | } |
| 53 | |
| 54 | func testJava(t *testing.T, bp string) *android.TestContext { |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 55 | config := android.TestArchConfig(buildDir) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 56 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 57 | ctx := android.NewTestArchContext() |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 58 | ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory)) |
Colin Cross | a60ead8 | 2017-10-02 18:10:21 -0700 | [diff] [blame] | 59 | ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory(true))) |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 60 | ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory)) |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 61 | ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory)) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 62 | ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory)) |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 63 | ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(genrule.FileGroupFactory)) |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 64 | ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators) |
| 65 | ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 66 | ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 67 | ctx.Register() |
| 68 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 69 | extraModules := []string{ |
| 70 | "core-oj", |
| 71 | "core-libart", |
| 72 | "framework", |
| 73 | "ext", |
| 74 | "okhttp", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 75 | "android_stubs_current", |
| 76 | "android_system_stubs_current", |
| 77 | "android_test_stubs_current", |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 78 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 79 | |
| 80 | for _, extra := range extraModules { |
| 81 | bp += fmt.Sprintf(` |
| 82 | java_library { |
| 83 | name: "%s", |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 84 | srcs: ["a.java"], |
Colin Cross | 76b5f0c | 2017-08-29 16:02:06 -0700 | [diff] [blame] | 85 | no_standard_libs: true, |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 86 | } |
| 87 | `, extra) |
| 88 | } |
| 89 | |
| 90 | ctx.MockFileSystem(map[string][]byte{ |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 91 | "Android.bp": []byte(bp), |
| 92 | "a.java": nil, |
| 93 | "b.java": nil, |
| 94 | "c.java": nil, |
| 95 | "a.jar": nil, |
| 96 | "b.jar": nil, |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 97 | "res/a": nil, |
| 98 | "res/b": nil, |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 99 | "res2/a": nil, |
| 100 | |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 101 | "prebuilts/sdk/14/android.jar": nil, |
| 102 | "prebuilts/sdk/14/framework.aidl": nil, |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 103 | }) |
| 104 | |
| 105 | _, errs := ctx.ParseBlueprintsFiles("Android.bp") |
| 106 | fail(t, errs) |
| 107 | _, errs = ctx.PrepareBuildActions(config) |
| 108 | fail(t, errs) |
| 109 | |
| 110 | return ctx |
| 111 | } |
| 112 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 113 | func moduleToPath(name string) string { |
| 114 | switch { |
| 115 | case name == `""`: |
| 116 | return name |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 117 | case strings.HasSuffix(name, ".jar"): |
| 118 | return name |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 119 | default: |
Colin Cross | 8649b26 | 2017-09-27 18:03:17 -0700 | [diff] [blame] | 120 | return filepath.Join(buildDir, ".intermediates", name, "android_common", "classes-compiled.jar") |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 124 | func TestSimple(t *testing.T) { |
| 125 | ctx := testJava(t, ` |
| 126 | java_library { |
| 127 | name: "foo", |
| 128 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 129 | libs: ["bar"], |
| 130 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | java_library { |
| 134 | name: "bar", |
| 135 | srcs: ["b.java"], |
| 136 | } |
| 137 | |
| 138 | java_library { |
| 139 | name: "baz", |
| 140 | srcs: ["c.java"], |
| 141 | } |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame^] | 142 | `) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 143 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 144 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 145 | combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 146 | |
| 147 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 148 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 149 | } |
| 150 | |
Colin Cross | 8649b26 | 2017-09-27 18:03:17 -0700 | [diff] [blame] | 151 | bar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "classes-compiled.jar") |
| 152 | baz := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "classes-compiled.jar") |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 153 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 154 | if !strings.Contains(javac.Args["classpath"], bar) { |
| 155 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar) |
| 156 | } |
| 157 | |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 158 | if !strings.Contains(javac.Args["classpath"], baz) { |
| 159 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], baz) |
| 160 | } |
| 161 | |
| 162 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 163 | 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] | 164 | } |
| 165 | } |
| 166 | |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame^] | 167 | func TestArchSpecific(t *testing.T) { |
| 168 | ctx := testJava(t, ` |
| 169 | java_library { |
| 170 | name: "foo", |
| 171 | srcs: ["a.java"], |
| 172 | target: { |
| 173 | android: { |
| 174 | srcs: ["b.java"], |
| 175 | }, |
| 176 | }, |
| 177 | } |
| 178 | `) |
| 179 | |
| 180 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 181 | if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" { |
| 182 | t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs) |
| 183 | } |
| 184 | } |
| 185 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 186 | var classpathTestcases = []struct { |
| 187 | name string |
| 188 | host android.OsClass |
| 189 | properties string |
| 190 | bootclasspath []string |
| 191 | classpath []string |
| 192 | }{ |
| 193 | { |
| 194 | name: "default", |
| 195 | bootclasspath: []string{"core-oj", "core-libart"}, |
| 196 | classpath: []string{"ext", "framework", "okhttp"}, |
| 197 | }, |
| 198 | { |
| 199 | name: "blank sdk version", |
| 200 | properties: `sdk_version: "",`, |
| 201 | bootclasspath: []string{"core-oj", "core-libart"}, |
| 202 | classpath: []string{"ext", "framework", "okhttp"}, |
| 203 | }, |
| 204 | { |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 205 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 206 | name: "sdk v14", |
| 207 | properties: `sdk_version: "14",`, |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 208 | bootclasspath: []string{`""`}, |
| 209 | classpath: []string{"prebuilts/sdk/14/android.jar"}, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 210 | }, |
| 211 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 212 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 213 | name: "current", |
| 214 | properties: `sdk_version: "current",`, |
| 215 | bootclasspath: []string{"android_stubs_current"}, |
| 216 | classpath: []string{}, |
| 217 | }, |
| 218 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 219 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 220 | name: "system_current", |
| 221 | properties: `sdk_version: "system_current",`, |
| 222 | bootclasspath: []string{"android_system_stubs_current"}, |
| 223 | classpath: []string{}, |
| 224 | }, |
| 225 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 226 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 227 | name: "test_current", |
| 228 | properties: `sdk_version: "test_current",`, |
| 229 | bootclasspath: []string{"android_test_stubs_current"}, |
| 230 | classpath: []string{}, |
| 231 | }, |
| 232 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 233 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 234 | name: "nostdlib", |
| 235 | properties: `no_standard_libs: true`, |
| 236 | bootclasspath: []string{`""`}, |
| 237 | classpath: []string{}, |
| 238 | }, |
| 239 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 240 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 241 | name: "host default", |
| 242 | host: android.Host, |
| 243 | properties: ``, |
| 244 | classpath: []string{}, |
| 245 | }, |
| 246 | { |
| 247 | name: "host nostdlib", |
| 248 | host: android.Host, |
| 249 | properties: `no_standard_libs: true`, |
| 250 | classpath: []string{}, |
| 251 | }, |
| 252 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 253 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 254 | func TestClasspath(t *testing.T) { |
| 255 | for _, testcase := range classpathTestcases { |
| 256 | t.Run(testcase.name, func(t *testing.T) { |
| 257 | hostExtra := "" |
| 258 | if testcase.host == android.Host { |
| 259 | hostExtra = "_host" |
| 260 | } |
| 261 | ctx := testJava(t, ` |
| 262 | java_library`+hostExtra+` { |
| 263 | name: "foo", |
| 264 | srcs: ["a.java"], |
| 265 | `+testcase.properties+` |
| 266 | } |
| 267 | `) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 268 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 269 | convertModulesToPaths := func(cp []string) []string { |
| 270 | ret := make([]string, len(cp)) |
| 271 | for i, e := range cp { |
| 272 | ret[i] = moduleToPath(e) |
| 273 | } |
| 274 | return ret |
| 275 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 276 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 277 | bootclasspath := convertModulesToPaths(testcase.bootclasspath) |
| 278 | classpath := convertModulesToPaths(testcase.classpath) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 279 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 280 | variant := "android_common" |
| 281 | if testcase.host == android.Host { |
| 282 | variant = android.BuildOs.String() + "_common" |
| 283 | } |
| 284 | javac := ctx.ModuleForTests("foo", variant).Rule("javac") |
| 285 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 286 | got := strings.TrimPrefix(javac.Args["bootClasspath"], "-bootclasspath ") |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 287 | bc := strings.Join(bootclasspath, ":") |
| 288 | if got != bc { |
| 289 | t.Errorf("bootclasspath expected %q != got %q", bc, got) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 290 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 291 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 292 | got = strings.TrimPrefix(javac.Args["classpath"], "-classpath ") |
| 293 | c := strings.Join(classpath, ":") |
| 294 | if got != c { |
| 295 | t.Errorf("classpath expected %q != got %q", c, got) |
| 296 | } |
| 297 | |
| 298 | var deps []string |
| 299 | if len(bootclasspath) > 0 && bootclasspath[0] != `""` { |
| 300 | deps = append(deps, bootclasspath...) |
| 301 | } |
| 302 | deps = append(deps, classpath...) |
| 303 | |
| 304 | if !reflect.DeepEqual(javac.Implicits.Strings(), deps) { |
| 305 | t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings()) |
| 306 | } |
| 307 | }) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | func TestPrebuilts(t *testing.T) { |
| 313 | ctx := testJava(t, ` |
| 314 | java_library { |
| 315 | name: "foo", |
| 316 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 317 | libs: ["bar"], |
| 318 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 321 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 322 | name: "bar", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 323 | jars: ["a.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 326 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 327 | name: "baz", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 328 | jars: ["b.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 329 | } |
| 330 | `) |
| 331 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 332 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 333 | combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 334 | |
| 335 | bar := "a.jar" |
| 336 | if !strings.Contains(javac.Args["classpath"], bar) { |
| 337 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar) |
| 338 | } |
| 339 | |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 340 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" { |
| 341 | 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] | 342 | } |
| 343 | } |
| 344 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 345 | func TestDefaults(t *testing.T) { |
| 346 | ctx := testJava(t, ` |
| 347 | java_defaults { |
| 348 | name: "defaults", |
| 349 | srcs: ["a.java"], |
| 350 | libs: ["bar"], |
| 351 | static_libs: ["baz"], |
| 352 | } |
| 353 | |
| 354 | java_library { |
| 355 | name: "foo", |
| 356 | defaults: ["defaults"], |
| 357 | } |
| 358 | |
| 359 | java_library { |
| 360 | name: "bar", |
| 361 | srcs: ["b.java"], |
| 362 | } |
| 363 | |
| 364 | java_library { |
| 365 | name: "baz", |
| 366 | srcs: ["c.java"], |
| 367 | } |
| 368 | `) |
| 369 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 370 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 371 | combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar") |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 372 | |
| 373 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 374 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 375 | } |
| 376 | |
Colin Cross | 8649b26 | 2017-09-27 18:03:17 -0700 | [diff] [blame] | 377 | bar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "classes-compiled.jar") |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 378 | if !strings.Contains(javac.Args["classpath"], bar) { |
| 379 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar) |
| 380 | } |
| 381 | |
Colin Cross | 8649b26 | 2017-09-27 18:03:17 -0700 | [diff] [blame] | 382 | baz := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "classes-compiled.jar") |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 383 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 384 | 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] | 385 | } |
| 386 | } |
| 387 | |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 388 | func TestResources(t *testing.T) { |
| 389 | var table = []struct { |
| 390 | name string |
| 391 | prop string |
| 392 | extra string |
| 393 | args string |
| 394 | }{ |
| 395 | { |
| 396 | // Test that a module with java_resource_dirs includes a file list file |
| 397 | name: "resource dirs", |
| 398 | prop: `java_resource_dirs: ["res"]`, |
| 399 | args: "-C res -l ", |
| 400 | }, |
| 401 | { |
| 402 | // Test that a module with java_resources includes the files |
| 403 | name: "resource files", |
| 404 | prop: `java_resources: ["res/a", "res/b"]`, |
| 405 | args: "-C . -f res/a -C . -f res/b", |
| 406 | }, |
| 407 | { |
| 408 | // Test that a module with a filegroup in java_resources includes the files with the |
| 409 | // path prefix |
| 410 | name: "resource filegroup", |
| 411 | prop: `java_resources: [":foo-res"]`, |
| 412 | extra: ` |
| 413 | filegroup { |
| 414 | name: "foo-res", |
| 415 | path: "res", |
| 416 | srcs: ["res/a", "res/b"], |
| 417 | }`, |
| 418 | args: "-C res -f res/a -C res -f res/b", |
| 419 | }, |
| 420 | { |
| 421 | // Test that a module with "include_srcs: true" includes its source files in the resources jar |
| 422 | name: "include sources", |
| 423 | prop: `include_srcs: true`, |
| 424 | args: "-C . -f a.java -C . -f b.java -C . -f c.java", |
| 425 | }, |
| 426 | } |
| 427 | |
| 428 | for _, test := range table { |
| 429 | t.Run(test.name, func(t *testing.T) { |
| 430 | ctx := testJava(t, ` |
| 431 | java_library { |
| 432 | name: "foo", |
| 433 | srcs: [ |
| 434 | "a.java", |
| 435 | "b.java", |
| 436 | "c.java", |
| 437 | ], |
| 438 | `+test.prop+`, |
| 439 | } |
| 440 | `+test.extra) |
| 441 | |
| 442 | foo := ctx.ModuleForTests("foo", "android_common").Output("classes.jar") |
| 443 | fooRes := ctx.ModuleForTests("foo", "android_common").Output("res.jar") |
| 444 | |
| 445 | if !inList(fooRes.Output.String(), foo.Inputs.Strings()) { |
| 446 | t.Errorf("foo combined jars %v does not contain %q", |
| 447 | foo.Inputs.Strings(), fooRes.Output.String()) |
| 448 | } |
| 449 | |
| 450 | if !strings.Contains(fooRes.Args["jarArgs"], test.args) { |
| 451 | t.Errorf("foo resource jar args %q does not contain %q", |
| 452 | fooRes.Args["jarArgs"], test.args) |
| 453 | } |
| 454 | }) |
| 455 | } |
| 456 | } |
| 457 | |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 458 | func TestExcludeResources(t *testing.T) { |
| 459 | ctx := testJava(t, ` |
| 460 | java_library { |
| 461 | name: "foo", |
| 462 | srcs: ["a.java"], |
| 463 | java_resource_dirs: ["res", "res2"], |
| 464 | exclude_java_resource_dirs: ["res2"], |
| 465 | } |
| 466 | |
| 467 | java_library { |
| 468 | name: "bar", |
| 469 | srcs: ["a.java"], |
| 470 | java_resources: ["res/*"], |
| 471 | exclude_java_resources: ["res/b"], |
| 472 | } |
| 473 | `) |
| 474 | |
| 475 | fooRes := ctx.ModuleForTests("foo", "android_common").Output("res.jar") |
| 476 | |
| 477 | expected := "-C res -l " + fooRes.Implicits[0].String() |
| 478 | if fooRes.Args["jarArgs"] != expected { |
| 479 | t.Errorf("foo resource jar args %q is not %q", |
| 480 | fooRes.Args["jarArgs"], expected) |
| 481 | |
| 482 | } |
| 483 | |
| 484 | barRes := ctx.ModuleForTests("bar", "android_common").Output("res.jar") |
| 485 | |
| 486 | expected = "-C . -f res/a" |
| 487 | if barRes.Args["jarArgs"] != expected { |
| 488 | t.Errorf("bar resource jar args %q is not %q", |
| 489 | barRes.Args["jarArgs"], expected) |
| 490 | |
| 491 | } |
| 492 | } |
| 493 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 494 | func fail(t *testing.T, errs []error) { |
| 495 | if len(errs) > 0 { |
| 496 | for _, err := range errs { |
| 497 | t.Error(err) |
| 498 | } |
| 499 | t.FailNow() |
| 500 | } |
| 501 | } |