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