blob: 7aa0164f1d20d7c52cbedfd40d9d5416744d97e9 [file] [log] [blame]
Colin Crossbe9cdb82019-01-21 21:37:16 -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 "testing"
20)
21
22func TestNoPlugin(t *testing.T) {
23 ctx := testJava(t, `
24 java_library {
25 name: "foo",
26 srcs: ["a.java"],
27 }
28 `)
29
30 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
31 turbine := ctx.ModuleForTests("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 // TODO(b/77284273): test for -processor:none if no plugins are enabled
42 if javac.Args["processor"] != "" {
43 t.Errorf("want no -processor argument, got %q", javac.Args["processor"])
44 }
45}
46
47func TestPlugin(t *testing.T) {
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 := android.BuildOs.String()
63
64 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
65 turbine := ctx.ModuleForTests("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("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 ctx := testJava(t, `
88 java_library {
89 name: "foo",
90 srcs: ["a.java"],
91 plugins: ["bar"],
92 }
93
94 java_plugin {
95 name: "bar",
96 processor_class: "com.bar",
97 generates_api: true,
98 srcs: ["b.java"],
99 }
100 `)
101
102 buildOS := android.BuildOs.String()
103
104 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
105 turbine := ctx.ModuleForTests("foo", "android_common").MaybeRule("turbine")
106
107 if turbine.Rule != nil {
108 t.Errorf("expected turbine to be disabled")
109 }
110
111 bar := ctx.ModuleForTests("bar", buildOS+"_common").Rule("javac").Output.String()
112
113 if !inList(bar, javac.Implicits.Strings()) {
114 t.Errorf("foo implicits %v does not contain %q", javac.Implicits.Strings(), bar)
115 }
116
117 if javac.Args["processorpath"] != "-processorpath "+bar {
118 t.Errorf("foo processorpath %q != '-processorpath %s'", javac.Args["processorpath"], bar)
119 }
120
121 if javac.Args["processor"] != "-processor com.bar" {
122 t.Errorf("foo processor %q != '-processor com.bar'", javac.Args["processor"])
123 }
124}