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