blob: ced772908e19830ac678a7ca17fc406844f60782 [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
30 moduleType string
31 host android.OsClass
32 properties string
33 bootclasspath []string
34 system string
35 classpath []string
36}{
37 {
38 name: "default",
39 bootclasspath: []string{"core.platform.api.stubs", "core-lambda-stubs"},
40 system: "core-platform-api-stubs-system-modules",
41 classpath: []string{"ext", "framework"},
42 },
43 {
44 name: "blank sdk version",
45 properties: `sdk_version: "",`,
46 bootclasspath: []string{"core.platform.api.stubs", "core-lambda-stubs"},
47 system: "core-platform-api-stubs-system-modules",
48 classpath: []string{"ext", "framework"},
49 },
50 {
51
52 name: "sdk v14",
53 properties: `sdk_version: "14",`,
54 bootclasspath: []string{`""`},
55 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
56 classpath: []string{"prebuilts/sdk/14/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
57 },
58 {
59
60 name: "current",
61 properties: `sdk_version: "current",`,
62 bootclasspath: []string{"android_stubs_current", "core-lambda-stubs"},
63 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
64 },
65 {
66
67 name: "system_current",
68 properties: `sdk_version: "system_current",`,
69 bootclasspath: []string{"android_system_stubs_current", "core-lambda-stubs"},
70 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
71 },
72 {
73
74 name: "system_14",
75 properties: `sdk_version: "system_14",`,
76 bootclasspath: []string{`""`},
77 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
78 classpath: []string{"prebuilts/sdk/14/system/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
79 },
80 {
81
82 name: "test_current",
83 properties: `sdk_version: "test_current",`,
84 bootclasspath: []string{"android_test_stubs_current", "core-lambda-stubs"},
85 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
86 },
87 {
88
89 name: "core_current",
90 properties: `sdk_version: "core_current",`,
91 bootclasspath: []string{"core.current.stubs", "core-lambda-stubs"},
92 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
93 },
94 {
95
96 name: "nostdlib",
97 properties: `no_standard_libs: true, system_modules: "none"`,
98 system: "none",
99 bootclasspath: []string{`""`},
100 classpath: []string{},
101 },
102 {
103
104 name: "nostdlib system_modules",
105 properties: `no_standard_libs: true, system_modules: "core-platform-api-stubs-system-modules"`,
106 system: "core-platform-api-stubs-system-modules",
107 bootclasspath: []string{`""`},
108 classpath: []string{},
109 },
110 {
111
112 name: "host default",
113 moduleType: "java_library_host",
114 properties: ``,
115 host: android.Host,
116 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
117 classpath: []string{},
118 },
119 {
120 name: "host nostdlib",
121 moduleType: "java_library_host",
122 host: android.Host,
123 properties: `no_standard_libs: true`,
124 classpath: []string{},
125 },
126 {
127
128 name: "host supported default",
129 host: android.Host,
130 properties: `host_supported: true,`,
131 classpath: []string{},
132 bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
133 },
134 {
135 name: "host supported nostdlib",
136 host: android.Host,
137 properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
138 classpath: []string{},
139 },
140 {
141
142 name: "unbundled sdk v14",
143 unbundled: true,
144 properties: `sdk_version: "14",`,
145 bootclasspath: []string{`""`},
146 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
147 classpath: []string{"prebuilts/sdk/14/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
148 },
149 {
150
151 name: "unbundled current",
152 unbundled: true,
153 properties: `sdk_version: "current",`,
154 bootclasspath: []string{`""`},
155 system: "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
156 classpath: []string{"prebuilts/sdk/current/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
157 },
158}
159
160func TestClasspath(t *testing.T) {
161 for _, testcase := range classpathTestcases {
162 t.Run(testcase.name, func(t *testing.T) {
163 moduleType := "java_library"
164 if testcase.moduleType != "" {
165 moduleType = testcase.moduleType
166 }
167
168 bp := moduleType + ` {
169 name: "foo",
170 srcs: ["a.java"],
171 ` + testcase.properties + `
172 }`
173
174 variant := "android_common"
175 if testcase.host == android.Host {
176 variant = android.BuildOs.String() + "_common"
177 }
178
179 convertModulesToPaths := func(cp []string) []string {
180 ret := make([]string, len(cp))
181 for i, e := range cp {
182 ret[i] = moduleToPath(e)
183 }
184 return ret
185 }
186
187 bootclasspath := convertModulesToPaths(testcase.bootclasspath)
188 classpath := convertModulesToPaths(testcase.classpath)
189
190 bc := strings.Join(bootclasspath, ":")
191 if bc != "" {
192 bc = "-bootclasspath " + bc
193 }
194
195 c := strings.Join(classpath, ":")
196 if c != "" {
197 c = "-classpath " + c
198 }
199 system := ""
200 if testcase.system == "none" {
201 system = "--system=none"
202 } else if testcase.system != "" {
203 system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
204 }
205
206 t.Run("1.8", func(t *testing.T) {
207 // Test default javac 1.8
208 config := testConfig(nil)
209 if testcase.unbundled {
210 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
211 }
212 ctx := testContext(config, bp, nil)
213 run(t, ctx, config)
214
215 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
216
217 got := javac.Args["bootClasspath"]
218 if got != bc {
219 t.Errorf("bootclasspath expected %q != got %q", bc, got)
220 }
221
222 got = javac.Args["classpath"]
223 if got != c {
224 t.Errorf("classpath expected %q != got %q", c, got)
225 }
226
227 var deps []string
228 if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
229 deps = append(deps, bootclasspath...)
230 }
231 deps = append(deps, classpath...)
232
233 if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
234 t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
235 }
236 })
237
238 // Test again with javac 1.9
239 t.Run("1.9", func(t *testing.T) {
240 config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
241 if testcase.unbundled {
242 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
243 }
244 ctx := testContext(config, bp, nil)
245 run(t, ctx, config)
246
247 javac := ctx.ModuleForTests("foo", variant).Rule("javac")
248 got := javac.Args["bootClasspath"]
249 expected := system
250 if testcase.system == "bootclasspath" {
251 expected = bc
252 }
253 if got != expected {
254 t.Errorf("bootclasspath expected %q != got %q", expected, got)
255 }
256 })
257 })
258 }
259
260}