• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// 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	"testing"
19)
20
21func TestNoPlugin(t *testing.T) {
22	t.Parallel()
23	ctx, _ := testJava(t, `
24		java_library {
25			name: "foo",
26			srcs: ["a.java"],
27		}
28	`)
29
30	javac := ctx.ModuleForTests(t, "foo", "android_common").Rule("javac")
31	turbine := ctx.ModuleForTests(t, "foo", "android_common").MaybeRule("turbine")
32
33	if turbine.Rule == nil {
34		t.Errorf("expected turbine to be enabled")
35	}
36
37	if javac.Args["processsorpath"] != "" {
38		t.Errorf("want empty processorpath, got %q", javac.Args["processorpath"])
39	}
40
41	if javac.Args["processor"] != "-proc:none" {
42		t.Errorf("want '-proc:none' argument, got %q", javac.Args["processor"])
43	}
44}
45
46func TestPlugin(t *testing.T) {
47	t.Parallel()
48	ctx, _ := testJava(t, `
49		java_library {
50			name: "foo",
51			srcs: ["a.java"],
52			plugins: ["bar"],
53		}
54
55		java_plugin {
56			name: "bar",
57			processor_class: "com.bar",
58			srcs: ["b.java"],
59		}
60	`)
61
62	buildOS := ctx.Config().BuildOS.String()
63
64	javac := ctx.ModuleForTests(t, "foo", "android_common").Rule("javac")
65	turbine := ctx.ModuleForTests(t, "foo", "android_common").MaybeRule("turbine")
66
67	if turbine.Rule == nil {
68		t.Errorf("expected turbine to be enabled")
69	}
70
71	bar := ctx.ModuleForTests(t, "bar", buildOS+"_common").Rule("javac").Output.String()
72
73	if !inList(bar, javac.Implicits.Strings()) {
74		t.Errorf("foo implicits %v does not contain %q", javac.Implicits.Strings(), bar)
75	}
76
77	if javac.Args["processorpath"] != "-processorpath "+bar {
78		t.Errorf("foo processorpath %q != '-processorpath %s'", javac.Args["processorpath"], bar)
79	}
80
81	if javac.Args["processor"] != "-processor com.bar" {
82		t.Errorf("foo processor %q != '-processor com.bar'", javac.Args["processor"])
83	}
84}
85
86func TestPluginGeneratesApi(t *testing.T) {
87	t.Parallel()
88	ctx, _ := testJava(t, `
89		java_library {
90			name: "foo",
91			srcs: ["a.java"],
92			plugins: ["bar"],
93		}
94
95		java_plugin {
96			name: "bar",
97			processor_class: "com.bar",
98			generates_api: true,
99			srcs: ["b.java"],
100		}
101	`)
102
103	buildOS := ctx.Config().BuildOS.String()
104
105	javac := ctx.ModuleForTests(t, "foo", "android_common").Rule("javac")
106	turbine := ctx.ModuleForTests(t, "foo", "android_common").MaybeRule("turbine")
107
108	if turbine.Rule != nil {
109		t.Errorf("expected turbine to be disabled")
110	}
111
112	bar := ctx.ModuleForTests(t, "bar", buildOS+"_common").Rule("javac").Output.String()
113
114	if !inList(bar, javac.Implicits.Strings()) {
115		t.Errorf("foo implicits %v does not contain %q", javac.Implicits.Strings(), bar)
116	}
117
118	if javac.Args["processorpath"] != "-processorpath "+bar {
119		t.Errorf("foo processorpath %q != '-processorpath %s'", javac.Args["processorpath"], bar)
120	}
121
122	if javac.Args["processor"] != "-processor com.bar" {
123		t.Errorf("foo processor %q != '-processor com.bar'", javac.Args["processor"])
124	}
125}
126