• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// 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))
58	ctx.RegisterModuleType("java_prebuilt_library", android.ModuleFactoryAdaptor(JavaPrebuiltFactory))
59	ctx.Register()
60
61	extraModules := []string{"core-libart", "frameworks", "sdk_v14"}
62
63	for _, extra := range extraModules {
64		bp += fmt.Sprintf(`
65			java_library {
66				name: "%s",
67				no_standard_libraries: true,
68			}
69		`, extra)
70	}
71
72	ctx.MockFileSystem(map[string][]byte{
73		"Android.bp": []byte(bp),
74		"a.java":     nil,
75		"b.java":     nil,
76		"c.java":     nil,
77		"a.jar":      nil,
78		"b.jar":      nil,
79	})
80
81	_, errs := ctx.ParseBlueprintsFiles("Android.bp")
82	fail(t, errs)
83	_, errs = ctx.PrepareBuildActions(config)
84	fail(t, errs)
85
86	return ctx
87}
88
89func TestSimple(t *testing.T) {
90	ctx := testJava(t, `
91		java_library {
92			name: "foo",
93			srcs: ["a.java"],
94			libs: ["bar"],
95			static_libs: ["baz"],
96		}
97
98		java_library {
99			name: "bar",
100			srcs: ["b.java"],
101		}
102
103		java_library {
104			name: "baz",
105			srcs: ["c.java"],
106		}
107		`)
108
109	javac := ctx.ModuleForTests("foo", "").Rule("javac")
110	jar := ctx.ModuleForTests("foo", "").Rule("jar")
111
112	if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
113		t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
114	}
115
116	bar := filepath.Join(buildDir, ".intermediates", "bar", "classes-full-debug.jar")
117	if !strings.Contains(javac.Args["classpath"], bar) {
118		t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
119	}
120
121	baz := filepath.Join(buildDir, ".intermediates", "baz", "classes.list")
122	if !strings.Contains(jar.Args["jarArgs"], baz) {
123		t.Errorf("foo jarArgs %v does not contain %q", jar.Args["jarArgs"], baz)
124	}
125}
126
127func TestSdk(t *testing.T) {
128	ctx := testJava(t, `
129		java_library {
130			name: "foo1",
131			srcs: ["a.java"],
132		}
133
134		java_library {
135			name: "foo2",
136			srcs: ["a.java"],
137			sdk_version: "",
138		}
139
140		java_library {
141			name: "foo3",
142			srcs: ["a.java"],
143			sdk_version: "14",
144		}
145
146		java_library {
147			name: "foo4",
148			srcs: ["a.java"],
149			sdk_version: "current",
150		}
151
152		java_library {
153			name: "foo5",
154			srcs: ["a.java"],
155			sdk_version: "system_current",
156		}
157
158		java_library {
159			name: "foo6",
160			srcs: ["a.java"],
161			sdk_version: "test_current",
162		}
163		`)
164
165	type depType int
166	const (
167		staticLib = iota
168		classpathLib
169		bootclasspathLib
170	)
171
172	check := func(module, dep string, depType depType) {
173		if dep != "" {
174			dep = filepath.Join(buildDir, ".intermediates", dep, "classes-full-debug.jar")
175		}
176
177		javac := ctx.ModuleForTests(module, "").Rule("javac")
178
179		if depType == bootclasspathLib {
180			got := strings.TrimPrefix(javac.Args["bootClasspath"], "-bootclasspath ")
181			if got != dep {
182				t.Errorf("module %q bootclasspath %q != %q", module, got, dep)
183			}
184		} else if depType == classpathLib {
185			got := strings.TrimPrefix(javac.Args["classpath"], "-classpath ")
186			if got != dep {
187				t.Errorf("module %q classpath %q != %q", module, got, dep)
188			}
189		}
190
191		if len(javac.Implicits) != 1 || javac.Implicits[0].String() != dep {
192			t.Errorf("module %q implicits != [%q]", dep)
193		}
194	}
195
196	check("foo1", "core-libart", bootclasspathLib)
197}
198
199func TestPrebuilts(t *testing.T) {
200	ctx := testJava(t, `
201		java_library {
202			name: "foo",
203			srcs: ["a.java"],
204			libs: ["bar"],
205			static_libs: ["baz"],
206		}
207
208		java_prebuilt_library {
209			name: "bar",
210			srcs: ["a.jar"],
211		}
212
213		java_prebuilt_library {
214			name: "baz",
215			srcs: ["b.jar"],
216		}
217		`)
218
219	javac := ctx.ModuleForTests("foo", "").Rule("javac")
220	jar := ctx.ModuleForTests("foo", "").Rule("jar")
221
222	bar := "a.jar"
223	if !strings.Contains(javac.Args["classpath"], bar) {
224		t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
225	}
226
227	baz := filepath.Join(buildDir, ".intermediates", "baz", "extracted", "classes.list")
228	if !strings.Contains(jar.Args["jarArgs"], baz) {
229		t.Errorf("foo jarArgs %v does not contain %q", jar.Args["jarArgs"], baz)
230	}
231}
232
233func fail(t *testing.T, errs []error) {
234	if len(errs) > 0 {
235		for _, err := range errs {
236			t.Error(err)
237		}
238		t.FailNow()
239	}
240}
241