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