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" |
| 19 | "fmt" |
| 20 | "io/ioutil" |
| 21 | "os" |
| 22 | "path/filepath" |
Colin Cross | 227d436 | 2017-08-30 14:14:52 -0700 | [diff] [blame] | 23 | "reflect" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 24 | "strings" |
| 25 | "testing" |
| 26 | ) |
| 27 | |
| 28 | var buildDir string |
| 29 | |
| 30 | func setUp() { |
| 31 | var err error |
| 32 | buildDir, err = ioutil.TempDir("", "soong_java_test") |
| 33 | if err != nil { |
| 34 | panic(err) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func tearDown() { |
| 39 | os.RemoveAll(buildDir) |
| 40 | } |
| 41 | |
| 42 | func TestMain(m *testing.M) { |
| 43 | run := func() int { |
| 44 | setUp() |
| 45 | defer tearDown() |
| 46 | |
| 47 | return m.Run() |
| 48 | } |
| 49 | |
| 50 | os.Exit(run()) |
| 51 | } |
| 52 | |
| 53 | func testJava(t *testing.T, bp string) *android.TestContext { |
| 54 | config := android.TestConfig(buildDir) |
| 55 | |
| 56 | ctx := android.NewTestContext() |
| 57 | ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory)) |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 58 | ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory)) |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 59 | ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory)) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 60 | ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory)) |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 61 | ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators) |
| 62 | ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 63 | ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 64 | ctx.Register() |
| 65 | |
Colin Cross | 227d436 | 2017-08-30 14:14:52 -0700 | [diff] [blame] | 66 | extraModules := []string{"core-oj", "core-libart", "frameworks", "sdk_v14"} |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 67 | |
| 68 | for _, extra := range extraModules { |
| 69 | bp += fmt.Sprintf(` |
| 70 | java_library { |
| 71 | name: "%s", |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame^] | 72 | srcs: ["a.java"], |
Colin Cross | 76b5f0c | 2017-08-29 16:02:06 -0700 | [diff] [blame] | 73 | no_standard_libs: true, |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 74 | } |
| 75 | `, extra) |
| 76 | } |
| 77 | |
| 78 | ctx.MockFileSystem(map[string][]byte{ |
| 79 | "Android.bp": []byte(bp), |
| 80 | "a.java": nil, |
| 81 | "b.java": nil, |
| 82 | "c.java": nil, |
| 83 | "a.jar": nil, |
| 84 | "b.jar": nil, |
| 85 | }) |
| 86 | |
| 87 | _, errs := ctx.ParseBlueprintsFiles("Android.bp") |
| 88 | fail(t, errs) |
| 89 | _, errs = ctx.PrepareBuildActions(config) |
| 90 | fail(t, errs) |
| 91 | |
| 92 | return ctx |
| 93 | } |
| 94 | |
| 95 | func TestSimple(t *testing.T) { |
| 96 | ctx := testJava(t, ` |
| 97 | java_library { |
| 98 | name: "foo", |
| 99 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 100 | libs: ["bar"], |
| 101 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | java_library { |
| 105 | name: "bar", |
| 106 | srcs: ["b.java"], |
| 107 | } |
| 108 | |
| 109 | java_library { |
| 110 | name: "baz", |
| 111 | srcs: ["c.java"], |
| 112 | } |
| 113 | `) |
| 114 | |
| 115 | javac := ctx.ModuleForTests("foo", "").Rule("javac") |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame^] | 116 | combineJar := ctx.ModuleForTests("foo", "").Rule("combineJar") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 117 | |
| 118 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 119 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 120 | } |
| 121 | |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame^] | 122 | bar := filepath.Join(buildDir, ".intermediates", "bar", "classes.jar") |
| 123 | baz := filepath.Join(buildDir, ".intermediates", "baz", "classes.jar") |
| 124 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 125 | if !strings.Contains(javac.Args["classpath"], bar) { |
| 126 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar) |
| 127 | } |
| 128 | |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame^] | 129 | if !strings.Contains(javac.Args["classpath"], baz) { |
| 130 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], baz) |
| 131 | } |
| 132 | |
| 133 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 134 | 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] | 135 | } |
| 136 | } |
| 137 | |
| 138 | func TestSdk(t *testing.T) { |
| 139 | ctx := testJava(t, ` |
| 140 | java_library { |
| 141 | name: "foo1", |
| 142 | srcs: ["a.java"], |
| 143 | } |
| 144 | |
| 145 | java_library { |
| 146 | name: "foo2", |
| 147 | srcs: ["a.java"], |
| 148 | sdk_version: "", |
| 149 | } |
| 150 | |
| 151 | java_library { |
| 152 | name: "foo3", |
| 153 | srcs: ["a.java"], |
| 154 | sdk_version: "14", |
| 155 | } |
| 156 | |
| 157 | java_library { |
| 158 | name: "foo4", |
| 159 | srcs: ["a.java"], |
| 160 | sdk_version: "current", |
| 161 | } |
| 162 | |
| 163 | java_library { |
| 164 | name: "foo5", |
| 165 | srcs: ["a.java"], |
| 166 | sdk_version: "system_current", |
| 167 | } |
| 168 | |
| 169 | java_library { |
| 170 | name: "foo6", |
| 171 | srcs: ["a.java"], |
| 172 | sdk_version: "test_current", |
| 173 | } |
| 174 | `) |
| 175 | |
| 176 | type depType int |
| 177 | const ( |
| 178 | staticLib = iota |
| 179 | classpathLib |
| 180 | bootclasspathLib |
| 181 | ) |
| 182 | |
Colin Cross | 227d436 | 2017-08-30 14:14:52 -0700 | [diff] [blame] | 183 | check := func(module string, depType depType, deps ...string) { |
| 184 | for i := range deps { |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame^] | 185 | deps[i] = filepath.Join(buildDir, ".intermediates", deps[i], "classes.jar") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 186 | } |
Colin Cross | 227d436 | 2017-08-30 14:14:52 -0700 | [diff] [blame] | 187 | dep := strings.Join(deps, ":") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 188 | |
| 189 | javac := ctx.ModuleForTests(module, "").Rule("javac") |
| 190 | |
| 191 | if depType == bootclasspathLib { |
| 192 | got := strings.TrimPrefix(javac.Args["bootClasspath"], "-bootclasspath ") |
| 193 | if got != dep { |
| 194 | t.Errorf("module %q bootclasspath %q != %q", module, got, dep) |
| 195 | } |
| 196 | } else if depType == classpathLib { |
| 197 | got := strings.TrimPrefix(javac.Args["classpath"], "-classpath ") |
| 198 | if got != dep { |
| 199 | t.Errorf("module %q classpath %q != %q", module, got, dep) |
| 200 | } |
| 201 | } |
| 202 | |
Colin Cross | 227d436 | 2017-08-30 14:14:52 -0700 | [diff] [blame] | 203 | if !reflect.DeepEqual(javac.Implicits.Strings(), deps) { |
| 204 | t.Errorf("module %q implicits %q != %q", module, javac.Implicits.Strings(), deps) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Colin Cross | 227d436 | 2017-08-30 14:14:52 -0700 | [diff] [blame] | 208 | check("foo1", bootclasspathLib, "core-oj", "core-libart") |
| 209 | check("foo2", bootclasspathLib, "core-oj", "core-libart") |
| 210 | // TODO(ccross): these need the arch mutator to run to work correctly |
| 211 | //check("foo3", bootclasspathLib, "sdk_v14") |
| 212 | //check("foo4", bootclasspathLib, "android_stubs_current") |
| 213 | //check("foo5", bootclasspathLib, "android_system_stubs_current") |
| 214 | //check("foo6", bootclasspathLib, "android_test_stubs_current") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | func TestPrebuilts(t *testing.T) { |
| 218 | ctx := testJava(t, ` |
| 219 | java_library { |
| 220 | name: "foo", |
| 221 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 222 | libs: ["bar"], |
| 223 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 226 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 227 | name: "bar", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 228 | jars: ["a.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 231 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 232 | name: "baz", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 233 | jars: ["b.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 234 | } |
| 235 | `) |
| 236 | |
| 237 | javac := ctx.ModuleForTests("foo", "").Rule("javac") |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame^] | 238 | combineJar := ctx.ModuleForTests("foo", "").Rule("combineJar") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 239 | |
| 240 | bar := "a.jar" |
| 241 | if !strings.Contains(javac.Args["classpath"], bar) { |
| 242 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar) |
| 243 | } |
| 244 | |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame^] | 245 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" { |
| 246 | 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] | 247 | } |
| 248 | } |
| 249 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 250 | func TestDefaults(t *testing.T) { |
| 251 | ctx := testJava(t, ` |
| 252 | java_defaults { |
| 253 | name: "defaults", |
| 254 | srcs: ["a.java"], |
| 255 | libs: ["bar"], |
| 256 | static_libs: ["baz"], |
| 257 | } |
| 258 | |
| 259 | java_library { |
| 260 | name: "foo", |
| 261 | defaults: ["defaults"], |
| 262 | } |
| 263 | |
| 264 | java_library { |
| 265 | name: "bar", |
| 266 | srcs: ["b.java"], |
| 267 | } |
| 268 | |
| 269 | java_library { |
| 270 | name: "baz", |
| 271 | srcs: ["c.java"], |
| 272 | } |
| 273 | `) |
| 274 | |
| 275 | javac := ctx.ModuleForTests("foo", "").Rule("javac") |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame^] | 276 | combineJar := ctx.ModuleForTests("foo", "").Rule("combineJar") |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 277 | |
| 278 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 279 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 280 | } |
| 281 | |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame^] | 282 | bar := filepath.Join(buildDir, ".intermediates", "bar", "classes.jar") |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 283 | if !strings.Contains(javac.Args["classpath"], bar) { |
| 284 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar) |
| 285 | } |
| 286 | |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame^] | 287 | baz := filepath.Join(buildDir, ".intermediates", "baz", "classes.jar") |
| 288 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 289 | 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] | 290 | } |
| 291 | } |
| 292 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 293 | func fail(t *testing.T, errs []error) { |
| 294 | if len(errs) > 0 { |
| 295 | for _, err := range errs { |
| 296 | t.Error(err) |
| 297 | } |
| 298 | t.FailNow() |
| 299 | } |
| 300 | } |