aboutsummaryrefslogtreecommitdiffstats
path: root/java/sdk_test.go
blob: e446129a5682d7caed9c893fe0d6c7c446d0d7f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright 2019 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package java

import (
	"path/filepath"
	"reflect"
	"strings"
	"testing"

	"github.com/google/blueprint/proptools"

	"android/soong/android"
	"android/soong/java/config"
)

func TestClasspath(t *testing.T) {
	var classpathTestcases = []struct {
		name          string
		unbundled     bool
		pdk           bool
		moduleType    string
		host          android.OsClass
		properties    string
		bootclasspath []string
		system        string
		classpath     []string
		aidl          string
	}{
		{
			name:          "default",
			bootclasspath: config.DefaultBootclasspathLibraries,
			system:        config.DefaultSystemModules,
			classpath:     config.DefaultLibraries,
			aidl:          "-Iframework/aidl",
		},
		{
			name:          "blank sdk version",
			properties:    `sdk_version: "",`,
			bootclasspath: config.DefaultBootclasspathLibraries,
			system:        config.DefaultSystemModules,
			classpath:     config.DefaultLibraries,
			aidl:          "-Iframework/aidl",
		},
		{

			name:          "sdk v25",
			properties:    `sdk_version: "25",`,
			bootclasspath: []string{`""`},
			system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
			classpath:     []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
			aidl:          "-pprebuilts/sdk/25/public/framework.aidl",
		},
		{

			name:          "current",
			properties:    `sdk_version: "current",`,
			bootclasspath: []string{"android_stubs_current", "core-lambda-stubs"},
			system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
			aidl:          "-p" + buildDir + "/framework.aidl",
		},
		{

			name:          "system_current",
			properties:    `sdk_version: "system_current",`,
			bootclasspath: []string{"android_system_stubs_current", "core-lambda-stubs"},
			system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
			aidl:          "-p" + buildDir + "/framework.aidl",
		},
		{

			name:          "system_25",
			properties:    `sdk_version: "system_25",`,
			bootclasspath: []string{`""`},
			system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
			classpath:     []string{"prebuilts/sdk/25/system/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
			aidl:          "-pprebuilts/sdk/25/public/framework.aidl",
		},
		{

			name:          "test_current",
			properties:    `sdk_version: "test_current",`,
			bootclasspath: []string{"android_test_stubs_current", "core-lambda-stubs"},
			system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
			aidl:          "-p" + buildDir + "/framework.aidl",
		},
		{

			name:          "core_current",
			properties:    `sdk_version: "core_current",`,
			bootclasspath: []string{"core.current.stubs", "core-lambda-stubs"},
			system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
		},
		{

			name:          "nostdlib",
			properties:    `no_standard_libs: true, system_modules: "none"`,
			system:        "none",
			bootclasspath: []string{`""`},
			classpath:     []string{},
		},
		{

			name:          "nostdlib system_modules",
			properties:    `no_standard_libs: true, system_modules: "core-platform-api-stubs-system-modules"`,
			system:        "core-platform-api-stubs-system-modules",
			bootclasspath: []string{`""`},
			classpath:     []string{},
		},
		{

			name:          "host default",
			moduleType:    "java_library_host",
			properties:    ``,
			host:          android.Host,
			bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
			classpath:     []string{},
		},
		{
			name:       "host nostdlib",
			moduleType: "java_library_host",
			host:       android.Host,
			properties: `no_standard_libs: true`,
			classpath:  []string{},
		},
		{

			name:          "host supported default",
			host:          android.Host,
			properties:    `host_supported: true,`,
			classpath:     []string{},
			bootclasspath: []string{"jdk8/jre/lib/jce.jar", "jdk8/jre/lib/rt.jar"},
		},
		{
			name:       "host supported nostdlib",
			host:       android.Host,
			properties: `host_supported: true, no_standard_libs: true, system_modules: "none"`,
			classpath:  []string{},
		},
		{

			name:          "unbundled sdk v25",
			unbundled:     true,
			properties:    `sdk_version: "25",`,
			bootclasspath: []string{`""`},
			system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
			classpath:     []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
			aidl:          "-pprebuilts/sdk/25/public/framework.aidl",
		},
		{

			name:          "unbundled current",
			unbundled:     true,
			properties:    `sdk_version: "current",`,
			bootclasspath: []string{`""`},
			system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
			classpath:     []string{"prebuilts/sdk/current/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
			aidl:          "-pprebuilts/sdk/current/public/framework.aidl",
		},

		{
			name:          "pdk default",
			pdk:           true,
			bootclasspath: []string{`""`},
			system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
			classpath:     []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
			aidl:          "-pprebuilts/sdk/25/public/framework.aidl",
		},
		{
			name:          "pdk current",
			pdk:           true,
			properties:    `sdk_version: "current",`,
			bootclasspath: []string{`""`},
			system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
			classpath:     []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
			aidl:          "-pprebuilts/sdk/25/public/framework.aidl",
		},
		{
			name:          "pdk 25",
			pdk:           true,
			properties:    `sdk_version: "25",`,
			bootclasspath: []string{`""`},
			system:        "bootclasspath", // special value to tell 1.9 test to expect bootclasspath
			classpath:     []string{"prebuilts/sdk/25/public/android.jar", "prebuilts/sdk/tools/core-lambda-stubs.jar"},
			aidl:          "-pprebuilts/sdk/25/public/framework.aidl",
		},
	}

	for _, testcase := range classpathTestcases {
		t.Run(testcase.name, func(t *testing.T) {
			moduleType := "java_library"
			if testcase.moduleType != "" {
				moduleType = testcase.moduleType
			}

			bp := moduleType + ` {
				name: "foo",
				srcs: ["a.java"],
				target: {
					android: {
						srcs: ["bar-doc/IFoo.aidl"],
					},
				},
				` + testcase.properties + `
			}`

			variant := "android_common"
			if testcase.host == android.Host {
				variant = android.BuildOs.String() + "_common"
			}

			convertModulesToPaths := func(cp []string) []string {
				ret := make([]string, len(cp))
				for i, e := range cp {
					ret[i] = moduleToPath(e)
				}
				return ret
			}

			bootclasspath := convertModulesToPaths(testcase.bootclasspath)
			classpath := convertModulesToPaths(testcase.classpath)

			bc := strings.Join(bootclasspath, ":")
			if bc != "" {
				bc = "-bootclasspath " + bc
			}

			c := strings.Join(classpath, ":")
			if c != "" {
				c = "-classpath " + c
			}
			system := ""
			if testcase.system == "none" {
				system = "--system=none"
			} else if testcase.system != "" {
				system = "--system=" + filepath.Join(buildDir, ".intermediates", testcase.system, "android_common", "system") + "/"
			}

			checkClasspath := func(t *testing.T, ctx *android.TestContext) {
				javac := ctx.ModuleForTests("foo", variant).Rule("javac")

				got := javac.Args["bootClasspath"]
				if got != bc {
					t.Errorf("bootclasspath expected %q != got %q", bc, got)
				}

				got = javac.Args["classpath"]
				if got != c {
					t.Errorf("classpath expected %q != got %q", c, got)
				}

				var deps []string
				if len(bootclasspath) > 0 && bootclasspath[0] != `""` {
					deps = append(deps, bootclasspath...)
				}
				deps = append(deps, classpath...)

				if !reflect.DeepEqual(javac.Implicits.Strings(), deps) {
					t.Errorf("implicits expected %q != got %q", deps, javac.Implicits.Strings())
				}
			}

			t.Run("1.8", func(t *testing.T) {
				// Test default javac 1.8
				config := testConfig(nil)
				if testcase.unbundled {
					config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
				}
				if testcase.pdk {
					config.TestProductVariables.Pdk = proptools.BoolPtr(true)
				}
				ctx := testContext(config, bp, nil)
				run(t, ctx, config)

				checkClasspath(t, ctx)

				if testcase.host != android.Host {
					aidl := ctx.ModuleForTests("foo", variant).Rule("aidl")

					aidlFlags := aidl.Args["aidlFlags"]
					// Trim trailing "-I." to avoid having to specify it in every test
					aidlFlags = strings.TrimSpace(strings.TrimSuffix(aidlFlags, "-I."))

					if g, w := aidlFlags, testcase.aidl; g != w {
						t.Errorf("want aidl flags %q, got %q", w, g)
					}
				}
			})

			// Test again with javac 1.9
			t.Run("1.9", func(t *testing.T) {
				config := testConfig(map[string]string{"EXPERIMENTAL_USE_OPENJDK9": "true"})
				if testcase.unbundled {
					config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
				}
				if testcase.pdk {
					config.TestProductVariables.Pdk = proptools.BoolPtr(true)
				}
				ctx := testContext(config, bp, nil)
				run(t, ctx, config)

				javac := ctx.ModuleForTests("foo", variant).Rule("javac")
				got := javac.Args["bootClasspath"]
				expected := system
				if testcase.system == "bootclasspath" {
					expected = bc
				}
				if got != expected {
					t.Errorf("bootclasspath expected %q != got %q", expected, got)
				}
			})

			// Test again with PLATFORM_VERSION_CODENAME=REL
			t.Run("REL", func(t *testing.T) {
				config := testConfig(nil)
				config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("REL")
				config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(true)

				if testcase.unbundled {
					config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
				}
				if testcase.pdk {
					config.TestProductVariables.Pdk = proptools.BoolPtr(true)
				}
				ctx := testContext(config, bp, nil)
				run(t, ctx, config)

				checkClasspath(t, ctx)
			})
		})
	}

}