Add a JDK 15 test for sealed classes
Bug: 169692487
Bug: 169645291
Change-Id: I5fb0b015df67b93760ef7107fda1aa76696d3760
diff --git a/src/test/examplesJava15/sealed/Compiler.java b/src/test/examplesJava15/sealed/Compiler.java
new file mode 100644
index 0000000..8d29eea
--- /dev/null
+++ b/src/test/examplesJava15/sealed/Compiler.java
@@ -0,0 +1,10 @@
+// Copyright (c) 2020, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package sealed;
+
+public sealed abstract class Compiler permits R8Compiler, D8Compiler {
+
+ public abstract void run();
+}
\ No newline at end of file
diff --git a/src/test/examplesJava15/sealed/D8Compiler.java b/src/test/examplesJava15/sealed/D8Compiler.java
new file mode 100644
index 0000000..e86df73
--- /dev/null
+++ b/src/test/examplesJava15/sealed/D8Compiler.java
@@ -0,0 +1,12 @@
+// Copyright (c) 2020, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package sealed;
+
+public final class D8Compiler extends Compiler {
+
+ public void run() {
+ System.out.println("D8 compiler");
+ }
+}
diff --git a/src/test/examplesJava15/sealed/Main.java b/src/test/examplesJava15/sealed/Main.java
new file mode 100644
index 0000000..f163a21
--- /dev/null
+++ b/src/test/examplesJava15/sealed/Main.java
@@ -0,0 +1,13 @@
+// Copyright (c) 2020, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package sealed;
+
+public class Main {
+
+ public static void main(String[] args) {
+ new R8Compiler().run();
+ new D8Compiler().run();
+ }
+}
diff --git a/src/test/examplesJava15/sealed/R8Compiler.java b/src/test/examplesJava15/sealed/R8Compiler.java
new file mode 100644
index 0000000..72a522a
--- /dev/null
+++ b/src/test/examplesJava15/sealed/R8Compiler.java
@@ -0,0 +1,12 @@
+// Copyright (c) 2020, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package sealed;
+
+public final class R8Compiler extends Compiler {
+
+ public void run() {
+ System.out.println("R8 compiler");
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/desugar/sealed/SealedAttributeTest.java b/src/test/java/com/android/tools/r8/desugar/sealed/SealedAttributeTest.java
new file mode 100644
index 0000000..8aa79eb
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/desugar/sealed/SealedAttributeTest.java
@@ -0,0 +1,80 @@
+// Copyright (c) 2020, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package com.android.tools.r8.desugar.sealed;
+
+import static com.android.tools.r8.DiagnosticsMatcher.diagnosticMessage;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assume.assumeTrue;
+
+import com.android.tools.r8.CompilationFailedException;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestRuntime;
+import com.android.tools.r8.examples.jdk15.Sealed;
+import com.android.tools.r8.utils.AndroidApiLevel;
+import java.util.List;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class SealedAttributeTest extends TestBase {
+
+ private final Backend backend;
+
+ @Parameters(name = "{0}")
+ public static List<Object[]> data() {
+ return buildParameters(getTestParameters().withNoneRuntime().build(), Backend.values());
+ }
+
+ public SealedAttributeTest(TestParameters parameters, Backend backend) {
+ this.backend = backend;
+ }
+
+ @Test
+ public void testJvm() throws Exception {
+ assumeTrue(backend == Backend.CF);
+ testForJvm()
+ .addRunClasspathFiles(Sealed.jar())
+ .addVmArguments("--enable-preview")
+ .run(TestRuntime.getCheckedInJdk15(), Sealed.Main.typeName())
+ .assertSuccessWithOutputLines("R8 compiler", "D8 compiler");
+ }
+
+ @Test
+ public void testD8() throws Exception {
+ assertThrows(
+ CompilationFailedException.class,
+ () -> {
+ testForD8(backend)
+ .addProgramFiles(Sealed.jar())
+ .setMinApi(AndroidApiLevel.B)
+ .compileWithExpectedDiagnostics(
+ diagnostics -> {
+ diagnostics.assertErrorThatMatches(
+ diagnosticMessage(containsString("Unsupported class file version: 59")));
+ });
+ });
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ assertThrows(
+ CompilationFailedException.class,
+ () -> {
+ testForR8(backend)
+ .addProgramFiles(Sealed.jar())
+ .setMinApi(AndroidApiLevel.B)
+ .addKeepMainRule(Sealed.Main.typeName())
+ .compileWithExpectedDiagnostics(
+ diagnostics -> {
+ diagnostics.assertErrorThatMatches(
+ diagnosticMessage(containsString("Unsupported class file version: 59")));
+ });
+ });
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/examples/jdk15/Sealed.java b/src/test/java/com/android/tools/r8/examples/jdk15/Sealed.java
new file mode 100644
index 0000000..1450ee8
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/examples/jdk15/Sealed.java
@@ -0,0 +1,26 @@
+// Copyright (c) 2020, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package com.android.tools.r8.examples.jdk15;
+
+import com.android.tools.r8.examples.JavaExampleClassProxy;
+import java.nio.file.Path;
+
+public class Sealed {
+
+ private static final String EXAMPLE_FILE = "examplesJava15/sealed";
+
+ public static final JavaExampleClassProxy Compiler =
+ new JavaExampleClassProxy(EXAMPLE_FILE, "sealed/Compiler");
+ public static final JavaExampleClassProxy R8Compiler =
+ new JavaExampleClassProxy(EXAMPLE_FILE, "sealed/R8Compiler");
+ public static final JavaExampleClassProxy D8Compiler =
+ new JavaExampleClassProxy(EXAMPLE_FILE, "sealed/D8Compiler");
+ public static final JavaExampleClassProxy Main =
+ new JavaExampleClassProxy(EXAMPLE_FILE, "sealed/Main");
+
+ public static Path jar() {
+ return JavaExampleClassProxy.examplesJar(EXAMPLE_FILE);
+ }
+}