From 4d895a4c31959f1381c381514b8c1d4bb0cef186 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 18 Jun 2015 19:44:36 +0300 Subject: [PATCH] Sealed classes: extra java against kotlin tests, together with an implementation of Java diagnostics inside these tests --- .../sealed/Derived.java | 6 +++++ .../sealed/Derived.javaerr.txt | 1 + .../sealed/Derived.kt | 6 +++++ .../sealed/Derived.txt | 13 +++++++++++ .../sealed/Instance.java | 8 +++++++ .../sealed/Instance.javaerr.txt | 1 + .../sealed/Instance.kt | 6 +++++ .../sealed/Instance.txt | 13 +++++++++++ .../AbstractCompileJavaAgainstKotlinTest.java | 4 +++- .../jvm/compiler/AbstractLoadJavaTest.java | 3 ++- ...CompileJavaAgainstKotlinTestGenerated.java | 21 ++++++++++++++++++ .../jvm/compiler/KotlinClassFinderTest.kt | 2 +- .../jetbrains/kotlin/test/JetTestUtils.java | 22 +++++++++++++++---- 13 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 compiler/testData/compileJavaAgainstKotlin/sealed/Derived.java create mode 100644 compiler/testData/compileJavaAgainstKotlin/sealed/Derived.javaerr.txt create mode 100644 compiler/testData/compileJavaAgainstKotlin/sealed/Derived.kt create mode 100644 compiler/testData/compileJavaAgainstKotlin/sealed/Derived.txt create mode 100644 compiler/testData/compileJavaAgainstKotlin/sealed/Instance.java create mode 100644 compiler/testData/compileJavaAgainstKotlin/sealed/Instance.javaerr.txt create mode 100644 compiler/testData/compileJavaAgainstKotlin/sealed/Instance.kt create mode 100644 compiler/testData/compileJavaAgainstKotlin/sealed/Instance.txt diff --git a/compiler/testData/compileJavaAgainstKotlin/sealed/Derived.java b/compiler/testData/compileJavaAgainstKotlin/sealed/Derived.java new file mode 100644 index 00000000000..bb17a9adc6b --- /dev/null +++ b/compiler/testData/compileJavaAgainstKotlin/sealed/Derived.java @@ -0,0 +1,6 @@ +package test; + +// It's not possible to inherit from Season (it's sealed in Kotlin) +public class Derived extends Season { + +} \ No newline at end of file diff --git a/compiler/testData/compileJavaAgainstKotlin/sealed/Derived.javaerr.txt b/compiler/testData/compileJavaAgainstKotlin/sealed/Derived.javaerr.txt new file mode 100644 index 00000000000..3a8b1d81501 --- /dev/null +++ b/compiler/testData/compileJavaAgainstKotlin/sealed/Derived.javaerr.txt @@ -0,0 +1 @@ +Derived.java:4:8:compiler.err.report.access diff --git a/compiler/testData/compileJavaAgainstKotlin/sealed/Derived.kt b/compiler/testData/compileJavaAgainstKotlin/sealed/Derived.kt new file mode 100644 index 00000000000..347c8ebc430 --- /dev/null +++ b/compiler/testData/compileJavaAgainstKotlin/sealed/Derived.kt @@ -0,0 +1,6 @@ +package test + +public sealed class Season { + class Warm: Season() + class Cold: Season() +} diff --git a/compiler/testData/compileJavaAgainstKotlin/sealed/Derived.txt b/compiler/testData/compileJavaAgainstKotlin/sealed/Derived.txt new file mode 100644 index 00000000000..3fb04502803 --- /dev/null +++ b/compiler/testData/compileJavaAgainstKotlin/sealed/Derived.txt @@ -0,0 +1,13 @@ +package test + +public sealed class Season { + private constructor Season() + + internal final class Cold : test.Season { + public constructor Cold() + } + + internal final class Warm : test.Season { + public constructor Warm() + } +} diff --git a/compiler/testData/compileJavaAgainstKotlin/sealed/Instance.java b/compiler/testData/compileJavaAgainstKotlin/sealed/Instance.java new file mode 100644 index 00000000000..10bf366617a --- /dev/null +++ b/compiler/testData/compileJavaAgainstKotlin/sealed/Instance.java @@ -0,0 +1,8 @@ +package test; + +public class Instance { + // It is not possible to create Season instance (it's sealed in Kotlin) + static Season create() { + return new Season(); + } +} \ No newline at end of file diff --git a/compiler/testData/compileJavaAgainstKotlin/sealed/Instance.javaerr.txt b/compiler/testData/compileJavaAgainstKotlin/sealed/Instance.javaerr.txt new file mode 100644 index 00000000000..fae063fb37c --- /dev/null +++ b/compiler/testData/compileJavaAgainstKotlin/sealed/Instance.javaerr.txt @@ -0,0 +1 @@ +Instance.java:6:16:compiler.err.abstract.cant.be.instantiated diff --git a/compiler/testData/compileJavaAgainstKotlin/sealed/Instance.kt b/compiler/testData/compileJavaAgainstKotlin/sealed/Instance.kt new file mode 100644 index 00000000000..347c8ebc430 --- /dev/null +++ b/compiler/testData/compileJavaAgainstKotlin/sealed/Instance.kt @@ -0,0 +1,6 @@ +package test + +public sealed class Season { + class Warm: Season() + class Cold: Season() +} diff --git a/compiler/testData/compileJavaAgainstKotlin/sealed/Instance.txt b/compiler/testData/compileJavaAgainstKotlin/sealed/Instance.txt new file mode 100644 index 00000000000..3fb04502803 --- /dev/null +++ b/compiler/testData/compileJavaAgainstKotlin/sealed/Instance.txt @@ -0,0 +1,13 @@ +package test + +public sealed class Season { + private constructor Season() + + internal final class Cold : test.Season { + public constructor Cold() + } + + internal final class Warm : test.Season { + public constructor Warm() + } +} diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractCompileJavaAgainstKotlinTest.java b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractCompileJavaAgainstKotlinTest.java index b3f5f3b8fbe..eccfaa7ed35 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractCompileJavaAgainstKotlinTest.java +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractCompileJavaAgainstKotlinTest.java @@ -64,9 +64,11 @@ public abstract class AbstractCompileJavaAgainstKotlinTest extends TestCaseWithT File ktFile = new File(ktFilePath); File javaFile = new File(ktFilePath.replaceFirst("\\.kt$", ".java")); File expectedFile = new File(ktFilePath.replaceFirst("\\.kt$", ".txt")); + File javaErrorFile = new File(ktFilePath.replaceFirst("\\.kt$", ".javaerr.txt")); File out = new File(tmpdir, "out"); - compileKotlinWithJava(Collections.singletonList(javaFile), Collections.singletonList(ktFile), out, getTestRootDisposable()); + compileKotlinWithJava(Collections.singletonList(javaFile), Collections.singletonList(ktFile), + out, getTestRootDisposable(), javaErrorFile); KotlinCoreEnvironment environment = KotlinCoreEnvironment.createForTests( getTestRootDisposable(), diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractLoadJavaTest.java b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractLoadJavaTest.java index 38a40d66b05..b09723a7586 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractLoadJavaTest.java +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/AbstractLoadJavaTest.java @@ -170,7 +170,8 @@ public abstract class AbstractLoadJavaTest extends TestCaseWithTmpdir { FileUtil.findFilesByMask(Pattern.compile(".+\\.java$"), librarySrc), FileUtil.findFilesByMask(Pattern.compile(".+\\.kt$"), librarySrc), libraryOut, - getTestRootDisposable() + getTestRootDisposable(), + null ); KotlinCoreEnvironment environment = KotlinCoreEnvironment.createForTests( diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileJavaAgainstKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileJavaAgainstKotlinTestGenerated.java index 310d6f16f6e..3498cf44ba1 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileJavaAgainstKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileJavaAgainstKotlinTestGenerated.java @@ -494,6 +494,27 @@ public class CompileJavaAgainstKotlinTestGenerated extends AbstractCompileJavaAg } } + @TestMetadata("compiler/testData/compileJavaAgainstKotlin/sealed") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Sealed extends AbstractCompileJavaAgainstKotlinTest { + public void testAllFilesPresentInSealed() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/compileJavaAgainstKotlin/sealed"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("Derived.kt") + public void testDerived() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/compileJavaAgainstKotlin/sealed/Derived.kt"); + doTest(fileName); + } + + @TestMetadata("Instance.kt") + public void testInstance() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/compileJavaAgainstKotlin/sealed/Instance.kt"); + doTest(fileName); + } + } + @TestMetadata("compiler/testData/compileJavaAgainstKotlin/staticFields") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/KotlinClassFinderTest.kt b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/KotlinClassFinderTest.kt index 0b2606088b2..f0975b16a4c 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/KotlinClassFinderTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/KotlinClassFinderTest.kt @@ -49,7 +49,7 @@ public class KotlinClassFinderTest : KotlinTestWithEnvironmentManagement() { fun testNestedClass() { val tmpdir = JetTestUtils.tmpDirForTest(this) JetTestUtils.compileKotlinWithJava( - listOf(), listOf(File("compiler/testData/kotlinClassFinder/nestedClass.kt")), tmpdir, getTestRootDisposable()!! + listOf(), listOf(File("compiler/testData/kotlinClassFinder/nestedClass.kt")), tmpdir, getTestRootDisposable()!!, null ) val environment = createEnvironment(tmpdir) diff --git a/compiler/tests/org/jetbrains/kotlin/test/JetTestUtils.java b/compiler/tests/org/jetbrains/kotlin/test/JetTestUtils.java index 61c2f015a56..0f7c49b48cb 100644 --- a/compiler/tests/org/jetbrains/kotlin/test/JetTestUtils.java +++ b/compiler/tests/org/jetbrains/kotlin/test/JetTestUtils.java @@ -526,7 +526,8 @@ public class JetTestUtils { @NotNull List javaFiles, @NotNull List ktFiles, @NotNull File outDir, - @NotNull Disposable disposable + @NotNull Disposable disposable, + @Nullable File javaErrorFile ) throws IOException { if (!ktFiles.isEmpty()) { compileKotlinToDirAndGetAnalysisResult(ktFiles, outDir, disposable, ALL); @@ -539,7 +540,7 @@ public class JetTestUtils { compileJavaFiles(javaFiles, Arrays.asList( "-classpath", outDir.getPath() + File.pathSeparator + ForTestCompileRuntime.runtimeJarForTests(), "-d", outDir.getPath() - )); + ), javaErrorFile); } } @@ -742,6 +743,10 @@ public class JetTestUtils { } public static void compileJavaFiles(@NotNull Collection files, List options) throws IOException { + compileJavaFiles(files, options, null); + } + + public static void compileJavaFiles(@NotNull Collection files, List options, @Nullable File javaErrorFile) throws IOException { JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector diagnosticCollector = new DiagnosticCollector(); StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager(diagnosticCollector, Locale.ENGLISH, Charset.forName("utf-8")); @@ -757,7 +762,13 @@ public class JetTestUtils { javaFileObjectsFromFiles); Boolean success = task.call(); // do NOT inline this variable, call() should complete before errorsToString() - Assert.assertTrue(errorsToString(diagnosticCollector), success); + String diagnosticString = errorsToString(diagnosticCollector); + if (javaErrorFile == null || !javaErrorFile.exists()) { + Assert.assertTrue(diagnosticString, success); + } + else { + assertEqualsToFile(javaErrorFile, diagnosticString); + } } finally { fileManager.close(); } @@ -767,7 +778,10 @@ public class JetTestUtils { StringBuilder builder = new StringBuilder(); for (javax.tools.Diagnostic diagnostic : diagnosticCollector.getDiagnostics()) { if (diagnostic.getKind() == javax.tools.Diagnostic.Kind.ERROR) { - builder.append(diagnostic).append("\n"); + builder.append(diagnostic.getSource().getName()).append(":") + .append(diagnostic.getLineNumber()).append(":") + .append(diagnostic.getColumnNumber()).append(":") + .append(diagnostic.getCode()).append("\n"); } } return builder.toString();