blob: 530f7fe5cc7f50ed37fb1d2d5282e98c697b8960 [file] [log] [blame]
Colin Cross21fc9bb2019-01-18 15:05:09 -08001// 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
15package java
16
17import (
Colin Cross3a3e94c2019-01-23 15:39:50 -080018 "android/soong/android"
Colin Crossafbb1732019-01-17 15:42:52 -080019 "strconv"
Colin Cross21fc9bb2019-01-18 15:05:09 -080020 "strings"
21 "testing"
22)
23
24func TestKotlin(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -070025 t.Parallel()
Jaewoong Jungf9a04432019-07-17 11:15:09 -070026 ctx, _ := testJava(t, `
Colin Cross21fc9bb2019-01-18 15:05:09 -080027 java_library {
28 name: "foo",
29 srcs: ["a.java", "b.kt"],
30 }
31
32 java_library {
33 name: "bar",
34 srcs: ["b.kt"],
35 libs: ["foo"],
36 static_libs: ["baz"],
37 }
38
39 java_library {
40 name: "baz",
41 srcs: ["c.java"],
42 }
43 `)
44
45 fooKotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
46 fooJavac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
47 fooJar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar")
48
49 if len(fooKotlinc.Inputs) != 2 || fooKotlinc.Inputs[0].String() != "a.java" ||
50 fooKotlinc.Inputs[1].String() != "b.kt" {
51 t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, fooKotlinc.Inputs)
52 }
53
54 if len(fooJavac.Inputs) != 1 || fooJavac.Inputs[0].String() != "a.java" {
55 t.Errorf(`foo inputs %v != ["a.java"]`, fooJavac.Inputs)
56 }
57
58 if !strings.Contains(fooJavac.Args["classpath"], fooKotlinc.Output.String()) {
59 t.Errorf("foo classpath %v does not contain %q",
60 fooJavac.Args["classpath"], fooKotlinc.Output.String())
61 }
62
63 if !inList(fooKotlinc.Output.String(), fooJar.Inputs.Strings()) {
64 t.Errorf("foo jar inputs %v does not contain %q",
65 fooJar.Inputs.Strings(), fooKotlinc.Output.String())
66 }
67
68 fooHeaderJar := ctx.ModuleForTests("foo", "android_common").Output("turbine-combined/foo.jar")
69 bazHeaderJar := ctx.ModuleForTests("baz", "android_common").Output("turbine-combined/baz.jar")
70 barKotlinc := ctx.ModuleForTests("bar", "android_common").Rule("kotlinc")
71
72 if len(barKotlinc.Inputs) != 1 || barKotlinc.Inputs[0].String() != "b.kt" {
73 t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, barKotlinc.Inputs)
74 }
75
76 if !inList(fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
77 t.Errorf(`expected %q in bar implicits %v`,
78 fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
79 }
80
81 if !inList(bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) {
82 t.Errorf(`expected %q in bar implicits %v`,
83 bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings())
84 }
85}
Colin Crossafbb1732019-01-17 15:42:52 -080086
87func TestKapt(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 Crossafbb1732019-01-17 15:42:52 -080090 java_library {
91 name: "foo",
92 srcs: ["a.java", "b.kt"],
Colin Cross5a116862020-04-22 11:44:34 -070093 plugins: ["bar", "baz"],
Colin Crossafbb1732019-01-17 15:42:52 -080094 }
95
Colin Crossbe9cdb82019-01-21 21:37:16 -080096 java_plugin {
Colin Crossafbb1732019-01-17 15:42:52 -080097 name: "bar",
Colin Cross3a3e94c2019-01-23 15:39:50 -080098 processor_class: "com.bar",
99 srcs: ["b.java"],
Colin Crossafbb1732019-01-17 15:42:52 -0800100 }
Colin Cross5a116862020-04-22 11:44:34 -0700101
102 java_plugin {
103 name: "baz",
104 processor_class: "com.baz",
105 srcs: ["b.java"],
106 }
Colin Crossafbb1732019-01-17 15:42:52 -0800107 `)
108
Colin Cross3a3e94c2019-01-23 15:39:50 -0800109 buildOS := android.BuildOs.String()
110
Colin Crossafbb1732019-01-17 15:42:52 -0800111 kapt := ctx.ModuleForTests("foo", "android_common").Rule("kapt")
112 kotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
113 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
114
Colin Cross3a3e94c2019-01-23 15:39:50 -0800115 bar := ctx.ModuleForTests("bar", buildOS+"_common").Rule("javac").Output.String()
Colin Cross5a116862020-04-22 11:44:34 -0700116 baz := ctx.ModuleForTests("baz", buildOS+"_common").Rule("javac").Output.String()
Colin Cross3a3e94c2019-01-23 15:39:50 -0800117
Colin Crossafbb1732019-01-17 15:42:52 -0800118 // Test that the kotlin and java sources are passed to kapt and kotlinc
119 if len(kapt.Inputs) != 2 || kapt.Inputs[0].String() != "a.java" || kapt.Inputs[1].String() != "b.kt" {
120 t.Errorf(`foo kapt inputs %v != ["a.java", "b.kt"]`, kapt.Inputs)
121 }
122 if len(kotlinc.Inputs) != 2 || kotlinc.Inputs[0].String() != "a.java" || kotlinc.Inputs[1].String() != "b.kt" {
123 t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, kotlinc.Inputs)
124 }
125
126 // Test that only the java sources are passed to javac
127 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
128 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
129 }
130
131 // Test that the kapt srcjar is a dependency of kotlinc and javac rules
132 if !inList(kapt.Output.String(), kotlinc.Implicits.Strings()) {
133 t.Errorf("expected %q in kotlinc implicits %v", kapt.Output.String(), kotlinc.Implicits.Strings())
134 }
135 if !inList(kapt.Output.String(), javac.Implicits.Strings()) {
136 t.Errorf("expected %q in javac implicits %v", kapt.Output.String(), javac.Implicits.Strings())
137 }
138
139 // Test that the kapt srcjar is extracted by the kotlinc and javac rules
140 if kotlinc.Args["srcJars"] != kapt.Output.String() {
141 t.Errorf("expected %q in kotlinc srcjars %v", kapt.Output.String(), kotlinc.Args["srcJars"])
142 }
143 if javac.Args["srcJars"] != kapt.Output.String() {
144 t.Errorf("expected %q in javac srcjars %v", kapt.Output.String(), kotlinc.Args["srcJars"])
145 }
Colin Cross3a3e94c2019-01-23 15:39:50 -0800146
147 // Test that the processors are passed to kapt
Colin Cross5a116862020-04-22 11:44:34 -0700148 expectedProcessorPath := "-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + bar +
149 " -P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + baz
Colin Cross3a3e94c2019-01-23 15:39:50 -0800150 if kapt.Args["kaptProcessorPath"] != expectedProcessorPath {
151 t.Errorf("expected kaptProcessorPath %q, got %q", expectedProcessorPath, kapt.Args["kaptProcessorPath"])
152 }
Colin Cross5a116862020-04-22 11:44:34 -0700153 expectedProcessor := "-P plugin:org.jetbrains.kotlin.kapt3:processors=com.bar -P plugin:org.jetbrains.kotlin.kapt3:processors=com.baz"
Colin Cross3a3e94c2019-01-23 15:39:50 -0800154 if kapt.Args["kaptProcessor"] != expectedProcessor {
155 t.Errorf("expected kaptProcessor %q, got %q", expectedProcessor, kapt.Args["kaptProcessor"])
156 }
157
158 // Test that the processors are not passed to javac
159 if javac.Args["processorPath"] != "" {
160 t.Errorf("expected processorPath '', got %q", javac.Args["processorPath"])
161 }
Colin Cross7788c122019-01-23 16:14:02 -0800162 if javac.Args["processor"] != "-proc:none" {
163 t.Errorf("expected processor '-proc:none', got %q", javac.Args["processor"])
Colin Cross3a3e94c2019-01-23 15:39:50 -0800164 }
Colin Crossafbb1732019-01-17 15:42:52 -0800165}
166
167func TestKaptEncodeFlags(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700168 t.Parallel()
Colin Crossafbb1732019-01-17 15:42:52 -0800169 // Compares the kaptEncodeFlags against the results of the example implementation at
170 // https://kotlinlang.org/docs/reference/kapt.html#apjavac-options-encoding
171 tests := []struct {
172 in [][2]string
173 out string
174 }{
175 {
176 // empty input
177 in: [][2]string{},
178 out: "rO0ABXcEAAAAAA==",
179 },
180 {
181 // common input
182 in: [][2]string{
183 {"-source", "1.8"},
184 {"-target", "1.8"},
185 },
186 out: "rO0ABXcgAAAAAgAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjg=",
187 },
188 {
189 // input that serializes to a 255 byte block
190 in: [][2]string{
191 {"-source", "1.8"},
192 {"-target", "1.8"},
193 {"a", strings.Repeat("b", 218)},
194 },
195 out: "rO0ABXf/AAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA2mJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJi",
196 },
197 {
198 // input that serializes to a 256 byte block
199 in: [][2]string{
200 {"-source", "1.8"},
201 {"-target", "1.8"},
202 {"a", strings.Repeat("b", 219)},
203 },
204 out: "rO0ABXoAAAEAAAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA22JiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYg==",
205 },
206 {
207 // input that serializes to a 257 byte block
208 in: [][2]string{
209 {"-source", "1.8"},
210 {"-target", "1.8"},
211 {"a", strings.Repeat("b", 220)},
212 },
213 out: "rO0ABXoAAAEBAAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA3GJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmI=",
214 },
215 }
216
217 for i, test := range tests {
218 t.Run(strconv.Itoa(i), func(t *testing.T) {
219 got := kaptEncodeFlags(test.in)
220 if got != test.out {
221 t.Errorf("\nwant %q\n got %q", test.out, got)
222 }
223 })
224 }
225}