blob: 7fa40a34bba3015d4ff30d46763f877c63b6840e [file] [log] [blame]
Colin Crossfb6d7812019-01-09 22:17:55 -08001// Copyright 2019 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 "path/filepath"
20 "reflect"
21 "strings"
22 "testing"
23
24 "github.com/google/blueprint/proptools"
25)
26
27var classpathTestcases = []struct {
28 name string
29 unbundled bool
Colin Cross98fd5742019-01-09 23:04:25 -080030 pdk bool
Colin Crossfb6d7812019-01-09 22:17:55 -080031 moduleType string
32 host android.OsClass
33 properties string
34 bootclasspath []string
35 system string
36 classpath []string
37}{
38 {
39 name: "default",
40 bootclasspath: []string{"core.platform.api.stubs", "core-lambda-stubs"},
41 system: "core-platform-api-stubs-system-modules",
42 classpath: []string{"ext", "framework"},
43 },
44 {
45 name: "blank sdk version",
46 properties: `sdk_version: "",`,
47 bootclasspath: []string{"core.platform.api.stubs", "core-lambda-stubs"},
48 system: "core-platform-api-stubs-system-modules",
49 classpath: []string{"ext", "framework"},
50 },
51 {
52
Colin Crossff0daf42019-04-02 16:10:56 -070053 name: "sdk v25",
54 properties: `sdk_version: "25",`,
Colin Crossfb6d7812019-01-09 22:17:55 -080055 bootclasspath: []string{`""`},
56 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossff0daf42019-04-02 16:10:56 -070057 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
Colin Crossfb6d7812019-01-09 22:17:55 -080058 },
59 {
60
61 name: "current",
62 properties: `sdk_version: "current",`,
63 bootclasspath: []string{"android_stubs_current", "core-lambda-stubs"},
64 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
65 },
66 {
67
68 name: "system_current",
69 properties: `sdk_version: "system_current",`,
70 bootclasspath: []string{"android_system_stubs_current", "core-lambda-stubs"},
71 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
72 },
73 {
74
Colin Crossff0daf42019-04-02 16:10:56 -070075 name: "system_25",
76 properties: `sdk_version: "system_25",`,
Colin Crossfb6d7812019-01-09 22:17:55 -080077 bootclasspath: []string{`""`},
78 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossff0daf42019-04-02 16:10:56 -070079 classpath: []string{"prebuilts/sdk/25/system/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
Colin Crossfb6d7812019-01-09 22:17:55 -080080 },
81 {
82
83 name: "test_current",
84 properties: `sdk_version: "test_current",`,
85 bootclasspath: []string{"android_test_stubs_current", "core-lambda-stubs"},
86 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
87 },
88 {
89
90 name: "core_current",
91 properties: `sdk_version: "core_current",`,
92 bootclasspath: []string{"core.current.stubs", "core-lambda-stubs"},
93 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
94 },
95 {
96
97 name: "nostdlib",
98 properties: `no_standard_libs: true, system_modules: "none"`,
99 system: "none",
100 bootclasspath: []string{`""`},
101 classpath: []string{},
102 },
103 {
104
105 name: "nostdlib system_modules",
106 properties: `no_standard_libs: true, system_modules: "core-platform-api-stubs-system-modules"`,
107 system: "core-platform-api-stubs-system-modules",
108 bootclasspath: []string{`""`},
109 classpath: []string{},
110 },
111 {
112
113 name: "host default",
114 moduleType: "java_library_host",
115 properties: ``,
116 host: android.Host,
117 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
118 classpath: []string{},
119 },
120 {
121 name: "host nostdlib",
122 moduleType: "java_library_host",
123 host: android.Host,
124 properties: `no_standard_libs: true`,
125 classpath: []string{},
126 },
127 {
128
129 name: "host supported default",
130 host: android.Host,
131 properties: `host_supported: true,`,
132 classpath: []string{},
133 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
134 },
135 {
136 name: "host supported nostdlib",
137 host: android.Host,
138 properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
139 classpath: []string{},
140 },
141 {
142
Colin Crossff0daf42019-04-02 16:10:56 -0700143 name: "unbundled sdk v25",
Colin Crossfb6d7812019-01-09 22:17:55 -0800144 unbundled: true,
Colin Crossff0daf42019-04-02 16:10:56 -0700145 properties: `sdk_version: "25",`,
Colin Crossfb6d7812019-01-09 22:17:55 -0800146 bootclasspath: []string{`""`},
147 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossff0daf42019-04-02 16:10:56 -0700148 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
Colin Crossfb6d7812019-01-09 22:17:55 -0800149 },
150 {
151
152 name: "unbundled current",
153 unbundled: true,
154 properties: `sdk_version: "current",`,
155 bootclasspath: []string{`""`},
156 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
157 classpath: []string{"prebuilts/sdk/current/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
158 },
Colin Cross98fd5742019-01-09 23:04:25 -0800159
160 {
161 name: "pdk default",
162 pdk: true,
163 bootclasspath: []string{`""`},
164 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossff0daf42019-04-02 16:10:56 -0700165 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
Colin Cross98fd5742019-01-09 23:04:25 -0800166 },
167 {
168 name: "pdk current",
169 pdk: true,
170 properties: `sdk_version: "current",`,
171 bootclasspath: []string{`""`},
172 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossff0daf42019-04-02 16:10:56 -0700173 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
Colin Cross98fd5742019-01-09 23:04:25 -0800174 },
175 {
Colin Crossff0daf42019-04-02 16:10:56 -0700176 name: "pdk 25",
Colin Cross98fd5742019-01-09 23:04:25 -0800177 pdk: true,
Colin Crossff0daf42019-04-02 16:10:56 -0700178 properties: `sdk_version: "25",`,
Colin Cross98fd5742019-01-09 23:04:25 -0800179 bootclasspath: []string{`""`},
180 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
Colin Crossff0daf42019-04-02 16:10:56 -0700181 classpath: []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
Colin Cross98fd5742019-01-09 23:04:25 -0800182 },
Colin Crossfb6d7812019-01-09 22:17:55 -0800183}
184
185func TestClasspath(t *testing.T) {
186 for _, testcase := range classpathTestcases {
187 t.Run(testcase.name, func(t *testing.T) {
188 moduleType := "java_library"
189 if testcase.moduleType != "" {
190 moduleType = testcase.moduleType
191 }
192
193 bp := moduleType + ` {
194 name: "foo",
195 srcs: ["a.java"],
196 ` + testcase.properties + `
197 }`
198
199 variant := "android_common"
200 if testcase.host == android.Host {
201 variant = android.BuildOs.String() + "_common"
202 }
203
204 convertModulesToPaths := func(cp []string) []string {
205 ret := make([]string, len(cp))
206 for i, e := range cp {
207 ret[i] = moduleToPath(e)
208 }
209 return ret
210 }
211
212 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
213 classpath := convertModulesToPaths(testcase.classpath)
214
215 bc := strings.Join(bootclasspath, ":")
216 if bc != "" {
217 bc = "-bootclasspath " + bc
218 }
219
220 c := strings.Join(classpath, ":")
221 if c != "" {
222 c = "-classpath " + c
223 }
224 system := ""
225 if testcase.system == "none" {
226 system = "--system=none"
227 } else if testcase.system != "" {
228 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
229 }
230
231 t.Run("1.8", func(t *testing.T) {
232 // Test default javac 1.8
233 config := testConfig(nil)
234 if testcase.unbundled {
235 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
236 }
Colin Cross98fd5742019-01-09 23:04:25 -0800237 if testcase.pdk {
238 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
239 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800240 ctx := testContext(config, bp, nil)
241 run(t, ctx, config)
242
243 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
244
245 got := javac.Args["bootClasspath"]
246 if got != bc {
247 t.Errorf("bootclasspath expected %q != got %q", bc, got)
248 }
249
250 got = javac.Args["classpath"]
251 if got != c {
252 t.Errorf("classpath expected %q != got %q", c, got)
253 }
254
255 var deps []string
256 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
257 deps = append(deps, bootclasspath...)
258 }
259 deps = append(deps, classpath...)
260
261 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
262 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
263 }
264 })
265
266 // Test again with javac 1.9
267 t.Run("1.9", func(t *testing.T) {
268 config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
269 if testcase.unbundled {
270 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
271 }
Colin Cross98fd5742019-01-09 23:04:25 -0800272 if testcase.pdk {
273 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
274 }
Colin Crossfb6d7812019-01-09 22:17:55 -0800275 ctx := testContext(config, bp, nil)
276 run(t, ctx, config)
277
278 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
279 got := javac.Args["bootClasspath"]
280 expected := system
281 if testcase.system == "bootclasspath" {
282 expected = bc
283 }
284 if got != expected {
285 t.Errorf("bootclasspath expected %q != got %q", expected, got)
286 }
287 })
Colin Crossff0daf42019-04-02 16:10:56 -0700288
289 // Test again with PLATFORM_VERSION_CODENAME=REL
290 t.Run("REL", func(t *testing.T) {
291 config := testConfig(nil)
292 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("REL")
293 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(true)
294
295 if testcase.unbundled {
296 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
297 }
298 if testcase.pdk {
299 config.TestProductVariables.Pdk = proptools.BoolPtr(true)
300 }
301 ctx := testContext(config, bp, nil)
302 run(t, ctx, config)
303
304 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
305
306 got := javac.Args["bootClasspath"]
307 if got != bc {
308 t.Errorf("bootclasspath expected %q != got %q", bc, got)
309 }
310
311 got = javac.Args["classpath"]
312 if got != c {
313 t.Errorf("classpath expected %q != got %q", c, got)
314 }
315
316 var deps []string
317 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
318 deps = append(deps, bootclasspath...)
319 }
320 deps = append(deps, classpath...)
321
322 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
323 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
324 }
325 })
Colin Crossfb6d7812019-01-09 22:17:55 -0800326 })
327 }
328
329}