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 "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 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 ctx, _ := testJava(t, ` 48 java_library { 49 name: "foo", 50 srcs: ["a.java"], 51 plugins: ["bar"], 52 } 53 54 java_plugin { 55 name: "bar", 56 processor_class: "com.bar", 57 srcs: ["b.java"], 58 } 59 `) 60 61 buildOS := android.BuildOs.String() 62 63 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") 64 turbine := ctx.ModuleForTests("foo", "android_common").MaybeRule("turbine") 65 66 if turbine.Rule == nil { 67 t.Errorf("expected turbine to be enabled") 68 } 69 70 bar := ctx.ModuleForTests("bar", buildOS+"_common").Rule("javac").Output.String() 71 72 if !inList(bar, javac.Implicits.Strings()) { 73 t.Errorf("foo implicits %v does not contain %q", javac.Implicits.Strings(), bar) 74 } 75 76 if javac.Args["processorpath"] != "-processorpath "+bar { 77 t.Errorf("foo processorpath %q != '-processorpath %s'", javac.Args["processorpath"], bar) 78 } 79 80 if javac.Args["processor"] != "-processor com.bar" { 81 t.Errorf("foo processor %q != '-processor com.bar'", javac.Args["processor"]) 82 } 83} 84 85func TestPluginGeneratesApi(t *testing.T) { 86 ctx, _ := testJava(t, ` 87 java_library { 88 name: "foo", 89 srcs: ["a.java"], 90 plugins: ["bar"], 91 } 92 93 java_plugin { 94 name: "bar", 95 processor_class: "com.bar", 96 generates_api: true, 97 srcs: ["b.java"], 98 } 99 `) 100 101 buildOS := android.BuildOs.String() 102 103 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") 104 turbine := ctx.ModuleForTests("foo", "android_common").MaybeRule("turbine") 105 106 if turbine.Rule != nil { 107 t.Errorf("expected turbine to be disabled") 108 } 109 110 bar := ctx.ModuleForTests("bar", buildOS+"_common").Rule("javac").Output.String() 111 112 if !inList(bar, javac.Implicits.Strings()) { 113 t.Errorf("foo implicits %v does not contain %q", javac.Implicits.Strings(), bar) 114 } 115 116 if javac.Args["processorpath"] != "-processorpath "+bar { 117 t.Errorf("foo processorpath %q != '-processorpath %s'", javac.Args["processorpath"], bar) 118 } 119 120 if javac.Args["processor"] != "-processor com.bar" { 121 t.Errorf("foo processor %q != '-processor com.bar'", javac.Args["processor"]) 122 } 123} 124