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