blob: eb116c994435f7f16f03fdf1ef81342bcc9d0001 [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))
Colin Crossf506d872017-07-19 15:53:04 -070057 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070058 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross89536d42017-07-07 14:35:50 -070059 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070060 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
61 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070062 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Colin Cross72bb3632017-07-13 16:23:21 -070063 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
93func TestSimple(t *testing.T) {
94 ctx := testJava(t, `
95 java_library {
96 name: "foo",
97 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -070098 libs: ["bar"],
99 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700100 }
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
131func 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
203func TestPrebuilts(t *testing.T) {
204 ctx := testJava(t, `
205 java_library {
206 name: "foo",
207 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700208 libs: ["bar"],
209 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700210 }
211
Colin Cross74d73e22017-08-02 11:05:49 -0700212 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700213 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700214 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700215 }
216
Colin Cross74d73e22017-08-02 11:05:49 -0700217 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700218 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700219 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700220 }
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
Colin Cross74d73e22017-08-02 11:05:49 -0700231 baz := filepath.Join(buildDir, ".intermediates", "baz", "extracted0", "classes.list")
Colin Cross72bb3632017-07-13 16:23:21 -0700232 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 Cross89536d42017-07-07 14:35:50 -0700237func 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 Cross72bb3632017-07-13 16:23:21 -0700280func 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}