blob: 4f5c0ecc741047821a8a4826b217a8fd27096715 [file] [log] [blame]
Colin Cross72bb3632017-07-13 16:23:21 -07001// 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
15package java
16
17import (
18 "android/soong/android"
19 "fmt"
20 "io/ioutil"
21 "os"
22 "path/filepath"
Colin Cross227d4362017-08-30 14:14:52 -070023 "reflect"
Colin Cross72bb3632017-07-13 16:23:21 -070024 "strings"
25 "testing"
26)
27
28var buildDir string
29
30func setUp() {
31 var err error
32 buildDir, err = ioutil.TempDir("", "soong_java_test")
33 if err != nil {
34 panic(err)
35 }
36}
37
38func tearDown() {
39 os.RemoveAll(buildDir)
40}
41
42func 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
53func 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 Crossf506d872017-07-19 15:53:04 -070058 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070059 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross89536d42017-07-07 14:35:50 -070060 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070061 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
62 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070063 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Colin Cross72bb3632017-07-13 16:23:21 -070064 ctx.Register()
65
Colin Cross227d4362017-08-30 14:14:52 -070066 extraModules := []string{"core-oj", "core-libart", "frameworks", "sdk_v14"}
Colin Cross72bb3632017-07-13 16:23:21 -070067
68 for _, extra := range extraModules {
69 bp += fmt.Sprintf(`
70 java_library {
71 name: "%s",
Colin Cross0a6e0072017-08-30 14:24:55 -070072 srcs: ["a.java"],
Colin Cross76b5f0c2017-08-29 16:02:06 -070073 no_standard_libs: true,
Colin Cross72bb3632017-07-13 16:23:21 -070074 }
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
95func TestSimple(t *testing.T) {
96 ctx := testJava(t, `
97 java_library {
98 name: "foo",
99 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700100 libs: ["bar"],
101 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700102 }
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 Cross0a6e0072017-08-30 14:24:55 -0700116 combineJar := ctx.ModuleForTests("foo", "").Rule("combineJar")
Colin Cross72bb3632017-07-13 16:23:21 -0700117
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 Cross0a6e0072017-08-30 14:24:55 -0700122 bar := filepath.Join(buildDir, ".intermediates", "bar", "classes.jar")
123 baz := filepath.Join(buildDir, ".intermediates", "baz", "classes.jar")
124
Colin Cross72bb3632017-07-13 16:23:21 -0700125 if !strings.Contains(javac.Args["classpath"], bar) {
126 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
127 }
128
Colin Cross0a6e0072017-08-30 14:24:55 -0700129 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 Cross72bb3632017-07-13 16:23:21 -0700135 }
136}
137
138func 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 Cross227d4362017-08-30 14:14:52 -0700183 check := func(module string, depType depType, deps ...string) {
184 for i := range deps {
Colin Cross0a6e0072017-08-30 14:24:55 -0700185 deps[i] = filepath.Join(buildDir, ".intermediates", deps[i], "classes.jar")
Colin Cross72bb3632017-07-13 16:23:21 -0700186 }
Colin Cross227d4362017-08-30 14:14:52 -0700187 dep := strings.Join(deps, ":")
Colin Cross72bb3632017-07-13 16:23:21 -0700188
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 Cross227d4362017-08-30 14:14:52 -0700203 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
204 t.Errorf("module %q implicits %q != %q", module, javac.Implicits.Strings(), deps)
Colin Cross72bb3632017-07-13 16:23:21 -0700205 }
206 }
207
Colin Cross227d4362017-08-30 14:14:52 -0700208 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 Cross72bb3632017-07-13 16:23:21 -0700215}
216
217func TestPrebuilts(t *testing.T) {
218 ctx := testJava(t, `
219 java_library {
220 name: "foo",
221 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700222 libs: ["bar"],
223 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700224 }
225
Colin Cross74d73e22017-08-02 11:05:49 -0700226 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700227 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700228 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700229 }
230
Colin Cross74d73e22017-08-02 11:05:49 -0700231 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700232 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700233 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700234 }
235 `)
236
237 javac := ctx.ModuleForTests("foo", "").Rule("javac")
Colin Cross0a6e0072017-08-30 14:24:55 -0700238 combineJar := ctx.ModuleForTests("foo", "").Rule("combineJar")
Colin Cross72bb3632017-07-13 16:23:21 -0700239
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 Cross0a6e0072017-08-30 14:24:55 -0700245 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 Cross72bb3632017-07-13 16:23:21 -0700247 }
248}
249
Colin Cross89536d42017-07-07 14:35:50 -0700250func 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 Cross0a6e0072017-08-30 14:24:55 -0700276 combineJar := ctx.ModuleForTests("foo", "").Rule("combineJar")
Colin Cross89536d42017-07-07 14:35:50 -0700277
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 Cross0a6e0072017-08-30 14:24:55 -0700282 bar := filepath.Join(buildDir, ".intermediates", "bar", "classes.jar")
Colin Cross89536d42017-07-07 14:35:50 -0700283 if !strings.Contains(javac.Args["classpath"], bar) {
284 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
285 }
286
Colin Cross0a6e0072017-08-30 14:24:55 -0700287 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 Cross89536d42017-07-07 14:35:50 -0700290 }
291}
292
Colin Cross72bb3632017-07-13 16:23:21 -0700293func 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}