blob: 9406ef9d7affb26bcdbb45405607fe678e0e64f3 [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) {
25 ctx := testJava(t, `
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 Crossafbb1732019-01-17 15:42:52 -080085
86func TestKapt(t *testing.T) {
87 ctx := testJava(t, `
88 java_library {
89 name: "foo",
90 srcs: ["a.java", "b.kt"],
Colin Crossbe9cdb82019-01-21 21:37:16 -080091 plugins: ["bar"],
Colin Crossafbb1732019-01-17 15:42:52 -080092 }
93
Colin Crossbe9cdb82019-01-21 21:37:16 -080094 java_plugin {
Colin Crossafbb1732019-01-17 15:42:52 -080095 name: "bar",
Colin Cross3a3e94c2019-01-23 15:39:50 -080096 processor_class: "com.bar",
97 srcs: ["b.java"],
Colin Crossafbb1732019-01-17 15:42:52 -080098 }
99 `)
100
Colin Cross3a3e94c2019-01-23 15:39:50 -0800101 buildOS := android.BuildOs.String()
102
Colin Crossafbb1732019-01-17 15:42:52 -0800103 kapt := ctx.ModuleForTests("foo", "android_common").Rule("kapt")
104 kotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc")
105 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
106
Colin Cross3a3e94c2019-01-23 15:39:50 -0800107 bar := ctx.ModuleForTests("bar", buildOS+"_common").Rule("javac").Output.String()
108
Colin Crossafbb1732019-01-17 15:42:52 -0800109 // Test that the kotlin and java sources are passed to kapt and kotlinc
110 if len(kapt.Inputs) != 2 || kapt.Inputs[0].String() != "a.java" || kapt.Inputs[1].String() != "b.kt" {
111 t.Errorf(`foo kapt inputs %v != ["a.java", "b.kt"]`, kapt.Inputs)
112 }
113 if len(kotlinc.Inputs) != 2 || kotlinc.Inputs[0].String() != "a.java" || kotlinc.Inputs[1].String() != "b.kt" {
114 t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, kotlinc.Inputs)
115 }
116
117 // Test that only the java sources are passed to javac
118 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
119 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
120 }
121
122 // Test that the kapt srcjar is a dependency of kotlinc and javac rules
123 if !inList(kapt.Output.String(), kotlinc.Implicits.Strings()) {
124 t.Errorf("expected %q in kotlinc implicits %v", kapt.Output.String(), kotlinc.Implicits.Strings())
125 }
126 if !inList(kapt.Output.String(), javac.Implicits.Strings()) {
127 t.Errorf("expected %q in javac implicits %v", kapt.Output.String(), javac.Implicits.Strings())
128 }
129
130 // Test that the kapt srcjar is extracted by the kotlinc and javac rules
131 if kotlinc.Args["srcJars"] != kapt.Output.String() {
132 t.Errorf("expected %q in kotlinc srcjars %v", kapt.Output.String(), kotlinc.Args["srcJars"])
133 }
134 if javac.Args["srcJars"] != kapt.Output.String() {
135 t.Errorf("expected %q in javac srcjars %v", kapt.Output.String(), kotlinc.Args["srcJars"])
136 }
Colin Cross3a3e94c2019-01-23 15:39:50 -0800137
138 // Test that the processors are passed to kapt
139 expectedProcessorPath := "-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + bar
140 if kapt.Args["kaptProcessorPath"] != expectedProcessorPath {
141 t.Errorf("expected kaptProcessorPath %q, got %q", expectedProcessorPath, kapt.Args["kaptProcessorPath"])
142 }
143 expectedProcessor := "-P plugin:org.jetbrains.kotlin.kapt3:processors=com.bar"
144 if kapt.Args["kaptProcessor"] != expectedProcessor {
145 t.Errorf("expected kaptProcessor %q, got %q", expectedProcessor, kapt.Args["kaptProcessor"])
146 }
147
148 // Test that the processors are not passed to javac
149 if javac.Args["processorPath"] != "" {
150 t.Errorf("expected processorPath '', got %q", javac.Args["processorPath"])
151 }
152 // TODO(b/77284273): test for -processor:none
153 if javac.Args["processor"] != "" {
154 t.Errorf("expected processor '', got %q", javac.Args["processor"])
155 }
Colin Crossafbb1732019-01-17 15:42:52 -0800156}
157
158func TestKaptEncodeFlags(t *testing.T) {
159 // Compares the kaptEncodeFlags against the results of the example implementation at
160 // https://kotlinlang.org/docs/reference/kapt.html#apjavac-options-encoding
161 tests := []struct {
162 in [][2]string
163 out string
164 }{
165 {
166 // empty input
167 in: [][2]string{},
168 out: "rO0ABXcEAAAAAA==",
169 },
170 {
171 // common input
172 in: [][2]string{
173 {"-source", "1.8"},
174 {"-target", "1.8"},
175 },
176 out: "rO0ABXcgAAAAAgAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjg=",
177 },
178 {
179 // input that serializes to a 255 byte block
180 in: [][2]string{
181 {"-source", "1.8"},
182 {"-target", "1.8"},
183 {"a", strings.Repeat("b", 218)},
184 },
185 out: "rO0ABXf/AAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA2mJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJi",
186 },
187 {
188 // input that serializes to a 256 byte block
189 in: [][2]string{
190 {"-source", "1.8"},
191 {"-target", "1.8"},
192 {"a", strings.Repeat("b", 219)},
193 },
194 out: "rO0ABXoAAAEAAAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA22JiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYg==",
195 },
196 {
197 // input that serializes to a 257 byte block
198 in: [][2]string{
199 {"-source", "1.8"},
200 {"-target", "1.8"},
201 {"a", strings.Repeat("b", 220)},
202 },
203 out: "rO0ABXoAAAEBAAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA3GJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmI=",
204 },
205 }
206
207 for i, test := range tests {
208 t.Run(strconv.Itoa(i), func(t *testing.T) {
209 got := kaptEncodeFlags(test.in)
210 if got != test.out {
211 t.Errorf("\nwant %q\n got %q", test.out, got)
212 }
213 })
214 }
215}