Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 1 | // Copyright 2017 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 | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 18 | "android/soong/android" |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 19 | "strconv" |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 20 | "strings" |
| 21 | "testing" |
| 22 | ) |
| 23 | |
| 24 | func TestKotlin(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 25 | ctx, _ := testJava(t, ` |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 26 | java_library { |
| 27 | name: "foo", |
| 28 | srcs: ["a.java", "b.kt"], |
| 29 | } |
| 30 | |
| 31 | java_library { |
| 32 | name: "bar", |
| 33 | srcs: ["b.kt"], |
| 34 | libs: ["foo"], |
| 35 | static_libs: ["baz"], |
| 36 | } |
| 37 | |
| 38 | java_library { |
| 39 | name: "baz", |
| 40 | srcs: ["c.java"], |
| 41 | } |
| 42 | `) |
| 43 | |
| 44 | fooKotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc") |
| 45 | fooJavac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 46 | fooJar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar") |
| 47 | |
| 48 | if len(fooKotlinc.Inputs) != 2 || fooKotlinc.Inputs[0].String() != "a.java" || |
| 49 | fooKotlinc.Inputs[1].String() != "b.kt" { |
| 50 | t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, fooKotlinc.Inputs) |
| 51 | } |
| 52 | |
| 53 | if len(fooJavac.Inputs) != 1 || fooJavac.Inputs[0].String() != "a.java" { |
| 54 | t.Errorf(`foo inputs %v != ["a.java"]`, fooJavac.Inputs) |
| 55 | } |
| 56 | |
| 57 | if !strings.Contains(fooJavac.Args["classpath"], fooKotlinc.Output.String()) { |
| 58 | t.Errorf("foo classpath %v does not contain %q", |
| 59 | fooJavac.Args["classpath"], fooKotlinc.Output.String()) |
| 60 | } |
| 61 | |
| 62 | if !inList(fooKotlinc.Output.String(), fooJar.Inputs.Strings()) { |
| 63 | t.Errorf("foo jar inputs %v does not contain %q", |
| 64 | fooJar.Inputs.Strings(), fooKotlinc.Output.String()) |
| 65 | } |
| 66 | |
| 67 | fooHeaderJar := ctx.ModuleForTests("foo", "android_common").Output("turbine-combined/foo.jar") |
| 68 | bazHeaderJar := ctx.ModuleForTests("baz", "android_common").Output("turbine-combined/baz.jar") |
| 69 | barKotlinc := ctx.ModuleForTests("bar", "android_common").Rule("kotlinc") |
| 70 | |
| 71 | if len(barKotlinc.Inputs) != 1 || barKotlinc.Inputs[0].String() != "b.kt" { |
| 72 | t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, barKotlinc.Inputs) |
| 73 | } |
| 74 | |
| 75 | if !inList(fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) { |
| 76 | t.Errorf(`expected %q in bar implicits %v`, |
| 77 | fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) |
| 78 | } |
| 79 | |
| 80 | if !inList(bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) { |
| 81 | t.Errorf(`expected %q in bar implicits %v`, |
| 82 | bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) |
| 83 | } |
| 84 | } |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 85 | |
| 86 | func TestKapt(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 87 | ctx, _ := testJava(t, ` |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 88 | java_library { |
| 89 | name: "foo", |
| 90 | srcs: ["a.java", "b.kt"], |
Colin Cross | 5a11686 | 2020-04-22 11:44:34 -0700 | [diff] [blame] | 91 | plugins: ["bar", "baz"], |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 94 | java_plugin { |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 95 | name: "bar", |
Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 96 | processor_class: "com.bar", |
| 97 | srcs: ["b.java"], |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 98 | } |
Colin Cross | 5a11686 | 2020-04-22 11:44:34 -0700 | [diff] [blame] | 99 | |
| 100 | java_plugin { |
| 101 | name: "baz", |
| 102 | processor_class: "com.baz", |
| 103 | srcs: ["b.java"], |
| 104 | } |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 105 | `) |
| 106 | |
Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 107 | buildOS := android.BuildOs.String() |
| 108 | |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 109 | kapt := ctx.ModuleForTests("foo", "android_common").Rule("kapt") |
| 110 | kotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc") |
| 111 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 112 | |
Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 113 | bar := ctx.ModuleForTests("bar", buildOS+"_common").Rule("javac").Output.String() |
Colin Cross | 5a11686 | 2020-04-22 11:44:34 -0700 | [diff] [blame] | 114 | baz := ctx.ModuleForTests("baz", buildOS+"_common").Rule("javac").Output.String() |
Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 115 | |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 116 | // Test that the kotlin and java sources are passed to kapt and kotlinc |
| 117 | if len(kapt.Inputs) != 2 || kapt.Inputs[0].String() != "a.java" || kapt.Inputs[1].String() != "b.kt" { |
| 118 | t.Errorf(`foo kapt inputs %v != ["a.java", "b.kt"]`, kapt.Inputs) |
| 119 | } |
| 120 | if len(kotlinc.Inputs) != 2 || kotlinc.Inputs[0].String() != "a.java" || kotlinc.Inputs[1].String() != "b.kt" { |
| 121 | t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, kotlinc.Inputs) |
| 122 | } |
| 123 | |
| 124 | // Test that only the java sources are passed to javac |
| 125 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 126 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 127 | } |
| 128 | |
| 129 | // Test that the kapt srcjar is a dependency of kotlinc and javac rules |
| 130 | if !inList(kapt.Output.String(), kotlinc.Implicits.Strings()) { |
| 131 | t.Errorf("expected %q in kotlinc implicits %v", kapt.Output.String(), kotlinc.Implicits.Strings()) |
| 132 | } |
| 133 | if !inList(kapt.Output.String(), javac.Implicits.Strings()) { |
| 134 | t.Errorf("expected %q in javac implicits %v", kapt.Output.String(), javac.Implicits.Strings()) |
| 135 | } |
| 136 | |
| 137 | // Test that the kapt srcjar is extracted by the kotlinc and javac rules |
| 138 | if kotlinc.Args["srcJars"] != kapt.Output.String() { |
| 139 | t.Errorf("expected %q in kotlinc srcjars %v", kapt.Output.String(), kotlinc.Args["srcJars"]) |
| 140 | } |
| 141 | if javac.Args["srcJars"] != kapt.Output.String() { |
| 142 | t.Errorf("expected %q in javac srcjars %v", kapt.Output.String(), kotlinc.Args["srcJars"]) |
| 143 | } |
Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 144 | |
| 145 | // Test that the processors are passed to kapt |
Colin Cross | 5a11686 | 2020-04-22 11:44:34 -0700 | [diff] [blame] | 146 | expectedProcessorPath := "-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + bar + |
| 147 | " -P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + baz |
Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 148 | if kapt.Args["kaptProcessorPath"] != expectedProcessorPath { |
| 149 | t.Errorf("expected kaptProcessorPath %q, got %q", expectedProcessorPath, kapt.Args["kaptProcessorPath"]) |
| 150 | } |
Colin Cross | 5a11686 | 2020-04-22 11:44:34 -0700 | [diff] [blame] | 151 | expectedProcessor := "-P plugin:org.jetbrains.kotlin.kapt3:processors=com.bar -P plugin:org.jetbrains.kotlin.kapt3:processors=com.baz" |
Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 152 | if kapt.Args["kaptProcessor"] != expectedProcessor { |
| 153 | t.Errorf("expected kaptProcessor %q, got %q", expectedProcessor, kapt.Args["kaptProcessor"]) |
| 154 | } |
| 155 | |
| 156 | // Test that the processors are not passed to javac |
| 157 | if javac.Args["processorPath"] != "" { |
| 158 | t.Errorf("expected processorPath '', got %q", javac.Args["processorPath"]) |
| 159 | } |
Colin Cross | 7788c12 | 2019-01-23 16:14:02 -0800 | [diff] [blame] | 160 | if javac.Args["processor"] != "-proc:none" { |
| 161 | t.Errorf("expected processor '-proc:none', got %q", javac.Args["processor"]) |
Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 162 | } |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | func TestKaptEncodeFlags(t *testing.T) { |
| 166 | // Compares the kaptEncodeFlags against the results of the example implementation at |
| 167 | // https://kotlinlang.org/docs/reference/kapt.html#apjavac-options-encoding |
| 168 | tests := []struct { |
| 169 | in [][2]string |
| 170 | out string |
| 171 | }{ |
| 172 | { |
| 173 | // empty input |
| 174 | in: [][2]string{}, |
| 175 | out: "rO0ABXcEAAAAAA==", |
| 176 | }, |
| 177 | { |
| 178 | // common input |
| 179 | in: [][2]string{ |
| 180 | {"-source", "1.8"}, |
| 181 | {"-target", "1.8"}, |
| 182 | }, |
| 183 | out: "rO0ABXcgAAAAAgAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjg=", |
| 184 | }, |
| 185 | { |
| 186 | // input that serializes to a 255 byte block |
| 187 | in: [][2]string{ |
| 188 | {"-source", "1.8"}, |
| 189 | {"-target", "1.8"}, |
| 190 | {"a", strings.Repeat("b", 218)}, |
| 191 | }, |
| 192 | out: "rO0ABXf/AAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA2mJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJi", |
| 193 | }, |
| 194 | { |
| 195 | // input that serializes to a 256 byte block |
| 196 | in: [][2]string{ |
| 197 | {"-source", "1.8"}, |
| 198 | {"-target", "1.8"}, |
| 199 | {"a", strings.Repeat("b", 219)}, |
| 200 | }, |
| 201 | out: "rO0ABXoAAAEAAAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA22JiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYg==", |
| 202 | }, |
| 203 | { |
| 204 | // input that serializes to a 257 byte block |
| 205 | in: [][2]string{ |
| 206 | {"-source", "1.8"}, |
| 207 | {"-target", "1.8"}, |
| 208 | {"a", strings.Repeat("b", 220)}, |
| 209 | }, |
| 210 | out: "rO0ABXoAAAEBAAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA3GJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmI=", |
| 211 | }, |
| 212 | } |
| 213 | |
| 214 | for i, test := range tests { |
| 215 | t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 216 | got := kaptEncodeFlags(test.in) |
| 217 | if got != test.out { |
| 218 | t.Errorf("\nwant %q\n got %q", test.out, got) |
| 219 | } |
| 220 | }) |
| 221 | } |
| 222 | } |