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