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) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame] | 22 | t.Parallel() |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 23 | ctx, _ := testJava(t, ` |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 24 | java_library { |
| 25 | name: "foo", |
| 26 | srcs: ["a.java"], |
| 27 | } |
| 28 | `) |
| 29 | |
Colin Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame] | 30 | javac := ctx.ModuleForTests(t, "foo", "android_common").Rule("javac") |
| 31 | turbine := ctx.ModuleForTests(t, "foo", "android_common").MaybeRule("turbine") |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 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 | |
Colin Cross | 7788c12 | 2019-01-23 16:14:02 -0800 | [diff] [blame] | 41 | if javac.Args["processor"] != "-proc:none" { |
| 42 | t.Errorf("want '-proc:none' argument, got %q", javac.Args["processor"]) |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
| 46 | func TestPlugin(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame] | 47 | t.Parallel() |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 48 | ctx, _ := testJava(t, ` |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 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 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 62 | buildOS := ctx.Config().BuildOS.String() |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 63 | |
Colin Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame] | 64 | javac := ctx.ModuleForTests(t, "foo", "android_common").Rule("javac") |
| 65 | turbine := ctx.ModuleForTests(t, "foo", "android_common").MaybeRule("turbine") |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 66 | |
| 67 | if turbine.Rule == nil { |
| 68 | t.Errorf("expected turbine to be enabled") |
| 69 | } |
| 70 | |
Colin Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame] | 71 | bar := ctx.ModuleForTests(t, "bar", buildOS+"_common").Rule("javac").Output.String() |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 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 | |
| 86 | func TestPluginGeneratesApi(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame] | 87 | t.Parallel() |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 88 | ctx, _ := testJava(t, ` |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 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 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 103 | buildOS := ctx.Config().BuildOS.String() |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 104 | |
Colin Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame] | 105 | javac := ctx.ModuleForTests(t, "foo", "android_common").Rule("javac") |
| 106 | turbine := ctx.ModuleForTests(t, "foo", "android_common").MaybeRule("turbine") |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 107 | |
| 108 | if turbine.Rule != nil { |
| 109 | t.Errorf("expected turbine to be disabled") |
| 110 | } |
| 111 | |
Colin Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame] | 112 | bar := ctx.ModuleForTests(t, "bar", buildOS+"_common").Rule("javac").Output.String() |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 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 | } |