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 |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 188 | moduleType string |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 189 | host android.OsClass |
| 190 | properties string |
| 191 | bootclasspath []string |
| 192 | classpath []string |
| 193 | }{ |
| 194 | { |
| 195 | name: "default", |
| 196 | bootclasspath: []string{"core-oj", "core-libart"}, |
| 197 | classpath: []string{"ext", "framework", "okhttp"}, |
| 198 | }, |
| 199 | { |
| 200 | name: "blank sdk version", |
| 201 | properties: `sdk_version: "",`, |
| 202 | bootclasspath: []string{"core-oj", "core-libart"}, |
| 203 | classpath: []string{"ext", "framework", "okhttp"}, |
| 204 | }, |
| 205 | { |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 206 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 207 | name: "sdk v14", |
| 208 | properties: `sdk_version: "14",`, |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 209 | bootclasspath: []string{`""`}, |
| 210 | classpath: []string{"prebuilts/sdk/14/android.jar"}, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 211 | }, |
| 212 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 213 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 214 | name: "current", |
| 215 | properties: `sdk_version: "current",`, |
| 216 | bootclasspath: []string{"android_stubs_current"}, |
| 217 | classpath: []string{}, |
| 218 | }, |
| 219 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 220 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 221 | name: "system_current", |
| 222 | properties: `sdk_version: "system_current",`, |
| 223 | bootclasspath: []string{"android_system_stubs_current"}, |
| 224 | classpath: []string{}, |
| 225 | }, |
| 226 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 227 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 228 | name: "test_current", |
| 229 | properties: `sdk_version: "test_current",`, |
| 230 | bootclasspath: []string{"android_test_stubs_current"}, |
| 231 | classpath: []string{}, |
| 232 | }, |
| 233 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 234 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 235 | name: "nostdlib", |
| 236 | properties: `no_standard_libs: true`, |
| 237 | bootclasspath: []string{`""`}, |
| 238 | classpath: []string{}, |
| 239 | }, |
| 240 | { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 241 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 242 | name: "host default", |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 243 | moduleType: "java_library_host", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 244 | properties: ``, |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 245 | host: android.Host, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 246 | classpath: []string{}, |
| 247 | }, |
| 248 | { |
| 249 | name: "host nostdlib", |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 250 | moduleType: "java_library_host", |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 251 | host: android.Host, |
| 252 | properties: `no_standard_libs: true`, |
| 253 | classpath: []string{}, |
| 254 | }, |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 255 | { |
| 256 | |
| 257 | name: "host supported default", |
| 258 | host: android.Host, |
| 259 | properties: `host_supported: true,`, |
| 260 | classpath: []string{}, |
| 261 | }, |
| 262 | { |
| 263 | name: "host supported nostdlib", |
| 264 | host: android.Host, |
| 265 | properties: `host_supported: true, no_standard_libs: true`, |
| 266 | classpath: []string{}, |
| 267 | }, |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 268 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 269 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 270 | func TestClasspath(t *testing.T) { |
| 271 | for _, testcase := range classpathTestcases { |
| 272 | t.Run(testcase.name, func(t *testing.T) { |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 273 | moduleType := "java_library" |
| 274 | if testcase.moduleType != "" { |
| 275 | moduleType = testcase.moduleType |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 276 | } |
Colin Cross | 965714f | 2017-10-03 20:57:01 -0700 | [diff] [blame] | 277 | ctx := testJava(t, moduleType+` { |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 278 | name: "foo", |
| 279 | srcs: ["a.java"], |
| 280 | `+testcase.properties+` |
| 281 | } |
| 282 | `) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 283 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 284 | convertModulesToPaths := func(cp []string) []string { |
| 285 | ret := make([]string, len(cp)) |
| 286 | for i, e := range cp { |
| 287 | ret[i] = moduleToPath(e) |
| 288 | } |
| 289 | return ret |
| 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 | bootclasspath := convertModulesToPaths(testcase.bootclasspath) |
| 293 | classpath := convertModulesToPaths(testcase.classpath) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 294 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 295 | variant := "android_common" |
| 296 | if testcase.host == android.Host { |
| 297 | variant = android.BuildOs.String() + "_common" |
| 298 | } |
| 299 | javac := ctx.ModuleForTests("foo", variant).Rule("javac") |
| 300 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 301 | got := strings.TrimPrefix(javac.Args["bootClasspath"], "-bootclasspath ") |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 302 | bc := strings.Join(bootclasspath, ":") |
| 303 | if got != bc { |
| 304 | t.Errorf("bootclasspath expected %q != got %q", bc, got) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 305 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 306 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 307 | got = strings.TrimPrefix(javac.Args["classpath"], "-classpath ") |
| 308 | c := strings.Join(classpath, ":") |
| 309 | if got != c { |
| 310 | t.Errorf("classpath expected %q != got %q", c, got) |
| 311 | } |
| 312 | |
| 313 | var deps []string |
| 314 | if len(bootclasspath) > 0 && bootclasspath[0] != `""` { |
| 315 | deps = append(deps, bootclasspath...) |
| 316 | } |
| 317 | deps = append(deps, classpath...) |
| 318 | |
| 319 | if !reflect.DeepEqual(javac.Implicits.Strings(), deps) { |
| 320 | t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings()) |
| 321 | } |
| 322 | }) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | func TestPrebuilts(t *testing.T) { |
| 328 | ctx := testJava(t, ` |
| 329 | java_library { |
| 330 | name: "foo", |
| 331 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 332 | libs: ["bar"], |
| 333 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 336 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 337 | name: "bar", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 338 | jars: ["a.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 341 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 342 | name: "baz", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 343 | jars: ["b.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 344 | } |
| 345 | `) |
| 346 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 347 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 348 | combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 349 | |
| 350 | bar := "a.jar" |
| 351 | if !strings.Contains(javac.Args["classpath"], bar) { |
| 352 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar) |
| 353 | } |
| 354 | |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 355 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" { |
| 356 | 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] | 357 | } |
| 358 | } |
| 359 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 360 | func TestDefaults(t *testing.T) { |
| 361 | ctx := testJava(t, ` |
| 362 | java_defaults { |
| 363 | name: "defaults", |
| 364 | srcs: ["a.java"], |
| 365 | libs: ["bar"], |
| 366 | static_libs: ["baz"], |
| 367 | } |
| 368 | |
| 369 | java_library { |
| 370 | name: "foo", |
| 371 | defaults: ["defaults"], |
| 372 | } |
| 373 | |
| 374 | java_library { |
| 375 | name: "bar", |
| 376 | srcs: ["b.java"], |
| 377 | } |
| 378 | |
| 379 | java_library { |
| 380 | name: "baz", |
| 381 | srcs: ["c.java"], |
| 382 | } |
| 383 | `) |
| 384 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 385 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 386 | combineJar := ctx.ModuleForTests("foo", "android_common").Rule("combineJar") |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 387 | |
| 388 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 389 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 390 | } |
| 391 | |
Colin Cross | 8649b26 | 2017-09-27 18:03:17 -0700 | [diff] [blame] | 392 | bar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "classes-compiled.jar") |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 393 | if !strings.Contains(javac.Args["classpath"], bar) { |
| 394 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar) |
| 395 | } |
| 396 | |
Colin Cross | 8649b26 | 2017-09-27 18:03:17 -0700 | [diff] [blame] | 397 | baz := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "classes-compiled.jar") |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 398 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 399 | 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] | 400 | } |
| 401 | } |
| 402 | |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 403 | func TestResources(t *testing.T) { |
| 404 | var table = []struct { |
| 405 | name string |
| 406 | prop string |
| 407 | extra string |
| 408 | args string |
| 409 | }{ |
| 410 | { |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame^] | 411 | // Test that a module with java_resource_dirs includes the files |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 412 | name: "resource dirs", |
| 413 | prop: `java_resource_dirs: ["res"]`, |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame^] | 414 | args: "-C res -f res/a -f res/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 415 | }, |
| 416 | { |
| 417 | // Test that a module with java_resources includes the files |
| 418 | name: "resource files", |
| 419 | prop: `java_resources: ["res/a", "res/b"]`, |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame^] | 420 | args: "-C . -f res/a -f res/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 421 | }, |
| 422 | { |
| 423 | // Test that a module with a filegroup in java_resources includes the files with the |
| 424 | // path prefix |
| 425 | name: "resource filegroup", |
| 426 | prop: `java_resources: [":foo-res"]`, |
| 427 | extra: ` |
| 428 | filegroup { |
| 429 | name: "foo-res", |
| 430 | path: "res", |
| 431 | srcs: ["res/a", "res/b"], |
| 432 | }`, |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame^] | 433 | args: "-C res -f res/a -f res/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 434 | }, |
| 435 | { |
| 436 | // Test that a module with "include_srcs: true" includes its source files in the resources jar |
| 437 | name: "include sources", |
| 438 | prop: `include_srcs: true`, |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame^] | 439 | args: "-C . -f a.java -f b.java -f c.java", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 440 | }, |
| 441 | } |
| 442 | |
| 443 | for _, test := range table { |
| 444 | t.Run(test.name, func(t *testing.T) { |
| 445 | ctx := testJava(t, ` |
| 446 | java_library { |
| 447 | name: "foo", |
| 448 | srcs: [ |
| 449 | "a.java", |
| 450 | "b.java", |
| 451 | "c.java", |
| 452 | ], |
| 453 | `+test.prop+`, |
| 454 | } |
| 455 | `+test.extra) |
| 456 | |
| 457 | foo := ctx.ModuleForTests("foo", "android_common").Output("classes.jar") |
| 458 | fooRes := ctx.ModuleForTests("foo", "android_common").Output("res.jar") |
| 459 | |
| 460 | if !inList(fooRes.Output.String(), foo.Inputs.Strings()) { |
| 461 | t.Errorf("foo combined jars %v does not contain %q", |
| 462 | foo.Inputs.Strings(), fooRes.Output.String()) |
| 463 | } |
| 464 | |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame^] | 465 | if fooRes.Args["jarArgs"] != test.args { |
| 466 | t.Errorf("foo resource jar args %q is not %q", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 467 | fooRes.Args["jarArgs"], test.args) |
| 468 | } |
| 469 | }) |
| 470 | } |
| 471 | } |
| 472 | |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 473 | func TestExcludeResources(t *testing.T) { |
| 474 | ctx := testJava(t, ` |
| 475 | java_library { |
| 476 | name: "foo", |
| 477 | srcs: ["a.java"], |
| 478 | java_resource_dirs: ["res", "res2"], |
| 479 | exclude_java_resource_dirs: ["res2"], |
| 480 | } |
| 481 | |
| 482 | java_library { |
| 483 | name: "bar", |
| 484 | srcs: ["a.java"], |
| 485 | java_resources: ["res/*"], |
| 486 | exclude_java_resources: ["res/b"], |
| 487 | } |
| 488 | `) |
| 489 | |
| 490 | fooRes := ctx.ModuleForTests("foo", "android_common").Output("res.jar") |
| 491 | |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame^] | 492 | expected := "-C res -f res/a -f res/b" |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 493 | if fooRes.Args["jarArgs"] != expected { |
| 494 | t.Errorf("foo resource jar args %q is not %q", |
| 495 | fooRes.Args["jarArgs"], expected) |
| 496 | |
| 497 | } |
| 498 | |
| 499 | barRes := ctx.ModuleForTests("bar", "android_common").Output("res.jar") |
| 500 | |
| 501 | expected = "-C . -f res/a" |
| 502 | if barRes.Args["jarArgs"] != expected { |
| 503 | t.Errorf("bar resource jar args %q is not %q", |
| 504 | barRes.Args["jarArgs"], expected) |
| 505 | |
| 506 | } |
| 507 | } |
| 508 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 509 | func fail(t *testing.T, errs []error) { |
| 510 | if len(errs) > 0 { |
| 511 | for _, err := range errs { |
| 512 | t.Error(err) |
| 513 | } |
| 514 | t.FailNow() |
| 515 | } |
| 516 | } |