xref: /aosp_15_r20/external/turbine/javatests/com/google/turbine/lower/LowerIntegrationTest.java (revision f7d94438c8bcdfdbf0d5a2a5e40120d0696e7088)
1 /*
2  * Copyright 2016 Google Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.turbine.lower;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static com.google.turbine.testing.TestResources.getResource;
21 import static java.util.stream.Collectors.toList;
22 import static org.junit.Assume.assumeTrue;
23 
24 import com.google.common.collect.ImmutableList;
25 import com.google.common.collect.ImmutableMap;
26 import com.google.common.collect.ImmutableSet;
27 import com.google.common.collect.Lists;
28 import java.io.IOError;
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.jar.JarEntry;
36 import java.util.jar.JarOutputStream;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.TemporaryFolder;
40 import org.junit.runner.RunWith;
41 import org.junit.runners.Parameterized;
42 import org.junit.runners.Parameterized.Parameters;
43 
44 @RunWith(Parameterized.class)
45 public class LowerIntegrationTest {
46 
47   private static final ImmutableMap<String, Integer> SOURCE_VERSION =
48       ImmutableMap.of(
49           "record.test", 16, //
50           "record2.test", 16,
51           "record_tostring.test", 16,
52           "record_ctor.test", 16,
53           "sealed.test", 17,
54           "sealed_nested.test", 17,
55           "textblock.test", 15);
56 
57   @Parameters(name = "{index}: {0}")
parameters()58   public static Iterable<Object[]> parameters() {
59     String[] testCases = {
60       // keep-sorted start
61       "B33513475.test",
62       "B33513475b.test",
63       "B33513475c.test",
64       "B70953542.test",
65       "B8056066.test",
66       "B8056066b.test",
67       "B8075274.test",
68       "B8148131.test",
69       "abstractenum.test",
70       "access1.test",
71       "ambiguous_identifier.test",
72       "anno_const_coerce.test",
73       "anno_const_scope.test",
74       "anno_nested.test",
75       "anno_repeated.test",
76       "anno_self_const.test",
77       "anno_void.test",
78       "annoconstvis.test",
79       "annotation_bool_default.test",
80       "annotation_class_default.test",
81       "annotation_clinit.test",
82       "annotation_declaration.test",
83       "annotation_enum_default.test",
84       "annotation_scope.test",
85       "annotations_default.test",
86       "annouse.test",
87       "annouse10.test",
88       "annouse11.test",
89       "annouse12.test",
90       "annouse13.test",
91       "annouse14.test",
92       "annouse15.test",
93       "annouse16.test",
94       "annouse17.test",
95       "annouse2.test",
96       "annouse3.test",
97       "annouse4.test",
98       "annouse5.test",
99       "annouse6.test",
100       "annouse7.test",
101       "annouse8.test",
102       "annouse9.test",
103       "annovis.test",
104       "anonymous.test",
105       "array_class_literal.test",
106       "ascii_sub.test",
107       "asset.test",
108       "basic_field.test",
109       "basic_nested.test",
110       "bcp.test",
111       "bmethod.test",
112       "bounds.test",
113       "boxed_const.test",
114       "builder.test",
115       "byte.test",
116       "byte2.test",
117       "bytecode_boolean_const.test",
118       "bytenoncanon.test",
119       "c_array.test",
120       "canon.test",
121       "canon_class_header.test",
122       "canon_recursive.test",
123       "cast_tail.test",
124       "circ_cvar.test",
125       "clash.test",
126       "complex_param_anno.test",
127       "concat.test",
128       "const.test",
129       "const_all.test",
130       "const_arith.test",
131       "const_boxed.test",
132       "const_byte.test",
133       "const_char.test",
134       "const_conditional.test",
135       "const_conv.test",
136       "const_field.test",
137       "const_hiding.test",
138       "const_moreexpr.test",
139       "const_multi.test",
140       "const_nonfinal.test",
141       "const_octal_underscore.test",
142       "const_operation_order.test",
143       "const_types.test",
144       "const_underscore.test",
145       "constlevel.test",
146       "constpack.test",
147       "ctor_anno.test",
148       "ctorvis.test",
149       "cvar_qualified.test",
150       "cycle.test",
151       "default_fbound.test",
152       "default_rawfbound.test",
153       "default_simple.test",
154       "deficient_types_classfile.test",
155       "dollar.test",
156       "empty_package_info.test",
157       "enum1.test",
158       "enum_abstract.test",
159       "enum_final.test",
160       "enumctor.test",
161       "enumctor2.test",
162       "enumimpl.test",
163       "enumingeneric.test",
164       "enuminner.test",
165       "enumint.test",
166       "enumint2.test",
167       "enumint3.test",
168       "enumint_byte.test",
169       "enumint_objectmethod.test",
170       "enumint_objectmethod2.test",
171       "enumint_objectmethod_raw.test",
172       "enuminthacks.test",
173       "enummemberanno.test",
174       "enumstat.test",
175       "erasurebound.test",
176       "existingctor.test",
177       "extend_inner.test",
178       "extends_bound.test",
179       "extends_otherbound.test",
180       "extendsandimplements.test",
181       "extrainnerclass.test",
182       "fbound.test",
183       "field_anno.test",
184       "firstcomparator.test",
185       "float_exponent.test",
186       "fuse.test",
187       "genericarrayfield.test",
188       "genericexn.test",
189       "genericexn2.test",
190       "genericnoncanon.test",
191       "genericnoncanon1.test",
192       "genericnoncanon10.test",
193       "genericnoncanon2.test",
194       "genericnoncanon3.test",
195       "genericnoncanon4.test",
196       "genericnoncanon5.test",
197       "genericnoncanon6.test",
198       "genericnoncanon8.test",
199       "genericnoncanon9.test",
200       "genericnoncanon_byte.test",
201       "genericnoncanon_method3.test",
202       "genericret.test",
203       "hex_int.test",
204       "hierarchy.test",
205       "ibound.test",
206       "icu.test",
207       "icu2.test",
208       "import_wild_order.test",
209       "importconst.test",
210       "importinner.test",
211       "inner_static.test",
212       "innerannodecl.test",
213       "innerclassanno.test",
214       "innerctor.test",
215       "innerenum.test",
216       "innerint.test",
217       "innerstaticgeneric.test",
218       "interface_field.test",
219       "interface_member_public.test",
220       "interface_method.test",
221       "interfacemem.test",
222       "interfaces.test",
223       // TODO(cushon): crashes ASM, see:
224       // https://gitlab.ow2.org/asm/asm/issues/317776
225       // "canon_array.test",
226       "java_lang_object.test",
227       "javadoc_deprecated.test",
228       "lexical.test",
229       "lexical2.test",
230       "lexical4.test",
231       "list.test",
232       "local.test",
233       "long_expression.test",
234       "loopthroughb.test",
235       "mapentry.test",
236       "marker.test",
237       "member.test",
238       "member_import_clash.test",
239       // TODO(cushon): support for source level 9 in integration tests
240       // "B74332665.test",
241       "memberimport.test",
242       "mods.test",
243       "morefields.test",
244       "moremethods.test",
245       "multifield.test",
246       "nested.test",
247       "nested2.test",
248       "nested_member_import.test",
249       "nested_member_import_noncanon.test",
250       "non_const.test",
251       "noncanon.test",
252       "noncanon_static_wild.test",
253       "nonconst_unary_expression.test",
254       "one.test",
255       "outer.test",
256       "outerparam.test",
257       "package_info.test",
258       "packagedecl.test",
259       "packageprivateprotectedinner.test",
260       "param_bound.test",
261       "prim_class.test",
262       "private_member.test",
263       "privateinner.test",
264       "proto.test",
265       "proto2.test",
266       "qual.test",
267       "raw.test",
268       "raw2.test",
269       "raw_canon.test",
270       "rawcanon.test",
271       "rawfbound.test",
272       "receiver_param.test",
273       "record.test",
274       "record2.test",
275       "record_ctor.test",
276       "record_tostring.test",
277       "rek.test",
278       "samepkg.test",
279       "sealed.test",
280       "sealed_nested.test",
281       "self.test",
282       "semi.test",
283       // https://bugs.openjdk.java.net/browse/JDK-8054064 ?
284       "shadow_inherited.test",
285       "simple.test",
286       "simplemethod.test",
287       "source_anno_retention.test",
288       "source_bootclasspath_order.test",
289       "static_final_boxed.test",
290       "static_member_type_import.test",
291       "static_member_type_import_recursive.test",
292       "static_type_import.test",
293       "strictfp.test",
294       "string.test",
295       "string_const.test",
296       "superabstract.test",
297       "supplierfunction.test",
298       "tbound.test",
299       "textblock.test",
300       "tyanno_inner.test",
301       "tyanno_varargs.test",
302       "typaram.test",
303       "typaram_lookup.test",
304       "typaram_lookup_enclosing.test",
305       "type_anno_ambiguous.test",
306       "type_anno_ambiguous_param.test",
307       "type_anno_ambiguous_qualified.test",
308       "type_anno_array_bound.test",
309       "type_anno_array_dims.test",
310       "type_anno_c_array.test",
311       "type_anno_cstyle_array_dims.test",
312       "type_anno_hello.test",
313       "type_anno_order.test",
314       "type_anno_parameter_index.test",
315       "type_anno_qual.test",
316       "type_anno_raw.test",
317       "type_anno_receiver.test",
318       "type_anno_retention.test",
319       "type_anno_return.test",
320       "tyvar_bound.test",
321       "tyvarfield.test",
322       "unary.test",
323       "underscore_literal.test",
324       "unicode.test",
325       "unicode_pkg.test",
326       "useextend.test",
327       "vanillaexception.test",
328       "varargs.test",
329       "visible_nested.test",
330       "visible_package.test",
331       "visible_package_private_toplevel.test",
332       "visible_private.test",
333       "visible_qualified.test",
334       "visible_same_package.test",
335       "wild.test",
336       "wild2.test",
337       "wild3.test",
338       "wildboundcanon.test",
339       "wildcanon.test",
340       // keep-sorted end
341     };
342     ImmutableSet<String> cases = ImmutableSet.copyOf(testCases);
343     assertThat(cases).containsAtLeastElementsIn(SOURCE_VERSION.keySet());
344     List<Object[]> tests = cases.stream().map(x -> new Object[] {x}).collect(toList());
345     String testShardIndex = System.getenv("TEST_SHARD_INDEX");
346     String testTotalShards = System.getenv("TEST_TOTAL_SHARDS");
347     if (testShardIndex == null || testTotalShards == null) {
348       return tests;
349     }
350     String shardFile = System.getenv("TEST_SHARD_STATUS_FILE");
351     if (shardFile != null) {
352       try {
353         Files.write(Paths.get(shardFile), new byte[0]);
354       } catch (IOException e) {
355         throw new IOError(e);
356       }
357     }
358     int index = Integer.parseInt(testShardIndex);
359     int shards = Integer.parseInt(testTotalShards);
360     return Lists.partition(tests, (tests.size() + shards - 1) / shards).get(index);
361   }
362 
363   final String test;
364 
LowerIntegrationTest(String test)365   public LowerIntegrationTest(String test) {
366     this.test = test;
367   }
368 
369   @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder();
370 
371   @Test
test()372   public void test() throws Exception {
373 
374     IntegrationTestSupport.TestInput input =
375         IntegrationTestSupport.TestInput.parse(getResource(getClass(), "testdata/" + test));
376 
377     ImmutableList<Path> classpathJar = ImmutableList.of();
378     if (!input.classes.isEmpty()) {
379       Map<String, byte[]> classpath =
380           IntegrationTestSupport.runJavac(input.classes, ImmutableList.of());
381       Path lib = temporaryFolder.newFile("lib.jar").toPath();
382       try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(lib))) {
383         for (Map.Entry<String, byte[]> entry : classpath.entrySet()) {
384           jos.putNextEntry(new JarEntry(entry.getKey() + ".class"));
385           jos.write(entry.getValue());
386         }
387       }
388       classpathJar = ImmutableList.of(lib);
389     }
390 
391     int version = SOURCE_VERSION.getOrDefault(test, 8);
392     assumeTrue(version <= Runtime.version().feature());
393     ImmutableList<String> javacopts =
394         ImmutableList.of(
395             "-source",
396             String.valueOf(version),
397             "-target",
398             String.valueOf(version),
399             "-Xpkginfo:always");
400 
401     Map<String, byte[]> expected =
402         IntegrationTestSupport.runJavac(input.sources, classpathJar, javacopts);
403 
404     Map<String, byte[]> actual =
405         IntegrationTestSupport.runTurbine(input.sources, classpathJar, javacopts);
406 
407     assertThat(IntegrationTestSupport.dump(IntegrationTestSupport.sortMembers(actual)))
408         .isEqualTo(IntegrationTestSupport.dump(IntegrationTestSupport.canonicalize(expected)));
409   }
410 }
411