blob: 3eb0215141f24ee176ba4845b8314c99c8953056 [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) {
Colin Cross323dc602020-09-18 14:25:31 -070023 t.Parallel()
Jaewoong Jungf9a04432019-07-17 11:15:09 -070024 ctx, _ := testJava(t, `
Colin Crossbe9cdb82019-01-21 21:37:16 -080025 java_library {
26 name: "foo",
27 srcs: ["a.java"],
28 }
29 `)
30
31 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
32 turbine := ctx.ModuleForTests("foo", "android_common").MaybeRule("turbine")
33
34 if turbine.Rule == nil {
35 t.Errorf("expected turbine to be enabled")
36 }
37
38 if javac.Args["processsorpath"] != "" {
39 t.Errorf("want empty processorpath, got %q", javac.Args["processorpath"])
40 }
41
Colin Cross7788c122019-01-23 16:14:02 -080042 if javac.Args["processor"] != "-proc:none" {
43 t.Errorf("want '-proc:none' argument, got %q", javac.Args["processor"])
Colin Crossbe9cdb82019-01-21 21:37:16 -080044 }
45}
46
47func TestPlugin(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -070048 t.Parallel()
Jaewoong Jungf9a04432019-07-17 11:15:09 -070049 ctx, _ := testJava(t, `
Colin Crossbe9cdb82019-01-21 21:37:16 -080050 java_library {
51 name: "foo",
52 srcs: ["a.java"],
53 plugins: ["bar"],
54 }
55
56 java_plugin {
57 name: "bar",
58 processor_class: "com.bar",
59 srcs: ["b.java"],
60 }
61 `)
62
63 buildOS := android.BuildOs.String()
64
65 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
66 turbine := ctx.ModuleForTests("foo", "android_common").MaybeRule("turbine")
67
68 if turbine.Rule == nil {
69 t.Errorf("expected turbine to be enabled")
70 }
71
72 bar := ctx.ModuleForTests("bar", buildOS+"_common").Rule("javac").Output.String()
73
74 if !inList(bar, javac.Implicits.Strings()) {
75 t.Errorf("foo implicits %v does not contain %q", javac.Implicits.Strings(), bar)
76 }
77
78 if javac.Args["processorpath"] != "-processorpath "+bar {
79 t.Errorf("foo processorpath %q != '-processorpath %s'", javac.Args["processorpath"], bar)
80 }
81
82 if javac.Args["processor"] != "-processor com.bar" {
83 t.Errorf("foo processor %q != '-processor com.bar'", javac.Args["processor"])
84 }
85}
86
87func TestPluginGeneratesApi(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -070088 t.Parallel()
Jaewoong Jungf9a04432019-07-17 11:15:09 -070089 ctx, _ := testJava(t, `
Colin Crossbe9cdb82019-01-21 21:37:16 -080090 java_library {
91 name: "foo",
92 srcs: ["a.java"],
93 plugins: ["bar"],
94 }
95
96 java_plugin {
97 name: "bar",
98 processor_class: "com.bar",
99 generates_api: true,
100 srcs: ["b.java"],
101 }
102 `)
103
104 buildOS := android.BuildOs.String()
105
106 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
107 turbine := ctx.ModuleForTests("foo", "android_common").MaybeRule("turbine")
108
109 if turbine.Rule != nil {
110 t.Errorf("expected turbine to be disabled")
111 }
112
113 bar := ctx.ModuleForTests("bar", buildOS+"_common").Rule("javac").Output.String()
114
115 if !inList(bar, javac.Implicits.Strings()) {
116 t.Errorf("foo implicits %v does not contain %q", javac.Implicits.Strings(), bar)
117 }
118
119 if javac.Args["processorpath"] != "-processorpath "+bar {
120 t.Errorf("foo processorpath %q != '-processorpath %s'", javac.Args["processorpath"], bar)
121 }
122
123 if javac.Args["processor"] != "-processor com.bar" {
124 t.Errorf("foo processor %q != '-processor com.bar'", javac.Args["processor"])
125 }
126}