diff --git a/compiler/tests/org/jetbrains/jet/jvm/compiler/AbstractLoadJavaTest.java b/compiler/tests/org/jetbrains/jet/jvm/compiler/AbstractLoadJavaTest.java index 01747aa5efc..d084ac21976 100644 --- a/compiler/tests/org/jetbrains/jet/jvm/compiler/AbstractLoadJavaTest.java +++ b/compiler/tests/org/jetbrains/jet/jvm/compiler/AbstractLoadJavaTest.java @@ -16,23 +16,41 @@ package org.jetbrains.jet.jvm.compiler; +import com.google.common.base.Predicates; import com.intellij.openapi.util.Pair; +import com.intellij.psi.PsiFile; +import com.intellij.util.Function; +import com.intellij.util.containers.ContainerUtil; import junit.framework.ComparisonFailure; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.ConfigurationKind; +import org.jetbrains.jet.JetTestUtils; +import org.jetbrains.jet.TestJdkKind; +import org.jetbrains.jet.cli.jvm.compiler.CliLightClassGenerationSupport; +import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment; +import org.jetbrains.jet.config.CommonConfigurationKeys; +import org.jetbrains.jet.config.CompilerConfiguration; +import org.jetbrains.jet.di.InjectorForJavaDescriptorResolver; +import org.jetbrains.jet.di.InjectorForTopDownAnalyzerForJvm; +import org.jetbrains.jet.lang.descriptors.ModuleDescriptor; import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor; +import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter; import org.jetbrains.jet.lang.resolve.BindingContext; +import org.jetbrains.jet.lang.resolve.BindingTrace; +import org.jetbrains.jet.lang.resolve.TopDownAnalysisParameters; +import org.jetbrains.jet.lang.resolve.java.DescriptorSearchRule; +import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver; +import org.jetbrains.jet.lang.resolve.name.Name; import org.jetbrains.jet.test.TestCaseWithTmpdir; import org.junit.Assert; import java.io.File; import java.util.Arrays; +import java.util.Collections; +import java.util.List; -import static org.jetbrains.jet.jvm.compiler.LoadDescriptorUtil.analyzeKotlinAndLoadTestNamespace; -import static org.jetbrains.jet.jvm.compiler.LoadDescriptorUtil.compileJavaAndLoadTestNamespaceAndBindingContextFromBinary; -import static org.jetbrains.jet.test.util.NamespaceComparator.DONT_INCLUDE_METHODS_OF_OBJECT; -import static org.jetbrains.jet.test.util.NamespaceComparator.compareNamespaceWithFile; -import static org.jetbrains.jet.test.util.NamespaceComparator.compareNamespaces; +import static org.jetbrains.jet.jvm.compiler.LoadDescriptorUtil.*; +import static org.jetbrains.jet.test.util.NamespaceComparator.*; /* The generated test compares namespace descriptors loaded from kotlin sources and read from compiled java. @@ -51,6 +69,72 @@ public abstract class AbstractLoadJavaTest extends TestCaseWithTmpdir { checkLoadedNamespaces(txtFile, kotlinNamespace, javaNamespaceAndContext); } + protected void doTestCompiledJava(@NotNull String expectedFileName, @NotNull String... javaFileNames) throws Exception { + JetTestUtils.createEnvironmentWithJdkAndNullabilityAnnotationsFromIdea( + getTestRootDisposable(), ConfigurationKind.JDK_AND_ANNOTATIONS, TestJdkKind.MOCK_JDK); + + List files = ContainerUtil.map(Arrays.asList(javaFileNames), new Function() { + @Override + public File fun(String s) { + return new File(s); + } + }); + File expectedFile = new File(expectedFileName); + File tmpDir = JetTestUtils.tmpDir(expectedFile.getName()); + + Pair javaNamespaceAndBindingContext + = compileJavaAndLoadTestNamespaceAndBindingContextFromBinary(files, tmpDir, getTestRootDisposable(), + ConfigurationKind.JDK_ONLY); + + checkJavaNamespace(expectedFile, javaNamespaceAndBindingContext); + } + + protected void doTestSourceJava(@NotNull String expectedFileName, @NotNull String javaRoot) throws Exception { + File expectedFile = new File(expectedFileName); + + Pair javaNamespaceAndBindingContext + = loadTestNamespaceAndBindingContextFromJavaRoot(new File(javaRoot), getTestRootDisposable(), ConfigurationKind.JDK_ONLY); + + checkJavaNamespace(expectedFile, javaNamespaceAndBindingContext); + } + + protected void doTestJavaAgainstKotlin(String path) throws Exception { + File dir = new File(path); + + CompilerConfiguration configuration = JetTestUtils.compilerConfigurationForTests( + ConfigurationKind.JDK_ONLY, TestJdkKind.MOCK_JDK, new File(dir, "java")); + configuration.put(CommonConfigurationKeys.SOURCE_ROOTS_KEY, Arrays.asList(new File(dir, "kotlin").getAbsolutePath())); + JetCoreEnvironment environment = new JetCoreEnvironment(getTestRootDisposable(), configuration); + + ModuleDescriptor moduleDescriptor = new ModuleDescriptor(Name.special("")); + + // we need the same binding trace for resolve from Java and Kotlin + BindingTrace trace = CliLightClassGenerationSupport.getInstanceForCli(environment.getProject()).getTrace(); + + InjectorForJavaDescriptorResolver injectorForJava = new InjectorForJavaDescriptorResolver(environment.getProject(), + trace, + moduleDescriptor); + + InjectorForTopDownAnalyzerForJvm injectorForAnalyzer = new InjectorForTopDownAnalyzerForJvm( + environment.getProject(), + new TopDownAnalysisParameters( + Predicates.alwaysFalse(), false, false, Collections.emptyList()), + trace, + moduleDescriptor); + + injectorForAnalyzer.getTopDownAnalyzer().analyzeFiles(environment.getSourceFiles(), Collections.emptyList()); + + JavaDescriptorResolver javaDescriptorResolver = injectorForJava.getJavaDescriptorResolver(); + NamespaceDescriptor namespaceDescriptor = javaDescriptorResolver.resolveNamespace( + LoadDescriptorUtil.TEST_PACKAGE_FQNAME, DescriptorSearchRule.INCLUDE_KOTLIN); + assert namespaceDescriptor != null; + + compareNamespaceWithFile(namespaceDescriptor, DONT_INCLUDE_METHODS_OF_OBJECT, + new File(dir, "expected.txt")); + + ExpectedLoadErrorsUtil.checkForLoadErrors(namespaceDescriptor, trace.getBindingContext()); + } + private static void checkForLoadErrorsAndCompare( @NotNull Pair javaNamespaceAndContext, @NotNull Runnable compareNamespacesRunnable diff --git a/compiler/tests/org/jetbrains/jet/jvm/compiler/LoadJavaCustomTest.java b/compiler/tests/org/jetbrains/jet/jvm/compiler/LoadJavaCustomTest.java index f14cc90530b..4637c8e4bb1 100644 --- a/compiler/tests/org/jetbrains/jet/jvm/compiler/LoadJavaCustomTest.java +++ b/compiler/tests/org/jetbrains/jet/jvm/compiler/LoadJavaCustomTest.java @@ -16,84 +16,21 @@ package org.jetbrains.jet.jvm.compiler; -import com.google.common.base.Predicates; -import com.intellij.openapi.util.Pair; -import com.intellij.psi.PsiFile; -import com.intellij.util.Function; -import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.ConfigurationKind; -import org.jetbrains.jet.JetTestUtils; -import org.jetbrains.jet.TestJdkKind; -import org.jetbrains.jet.cli.jvm.compiler.CliLightClassGenerationSupport; -import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment; -import org.jetbrains.jet.config.CommonConfigurationKeys; -import org.jetbrains.jet.config.CompilerConfiguration; -import org.jetbrains.jet.di.InjectorForJavaDescriptorResolver; -import org.jetbrains.jet.di.InjectorForTopDownAnalyzerForJvm; -import org.jetbrains.jet.lang.descriptors.ModuleDescriptor; -import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor; -import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter; -import org.jetbrains.jet.lang.resolve.BindingContext; -import org.jetbrains.jet.lang.resolve.BindingTrace; -import org.jetbrains.jet.lang.resolve.TopDownAnalysisParameters; -import org.jetbrains.jet.lang.resolve.java.DescriptorSearchRule; -import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver; -import org.jetbrains.jet.lang.resolve.name.Name; -import org.jetbrains.jet.test.TestCaseWithTmpdir; -import org.jetbrains.jet.test.util.NamespaceComparator; - -import java.io.File; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import static org.jetbrains.jet.jvm.compiler.LoadDescriptorUtil.compileJavaAndLoadTestNamespaceAndBindingContextFromBinary; -import static org.jetbrains.jet.jvm.compiler.LoadDescriptorUtil.loadTestNamespaceAndBindingContextFromJavaRoot; -import static org.jetbrains.jet.test.util.NamespaceComparator.compareNamespaceWithFile; /* LoadJavaTestGenerated should be used instead if possible. */ -public final class LoadJavaCustomTest extends TestCaseWithTmpdir { +public final class LoadJavaCustomTest extends AbstractLoadJavaTest { @NotNull private static final String PATH = "compiler/testData/loadJavaCustom"; - private void doTest(@NotNull String expectedFileName, @NotNull String... javaFileNames) throws Exception { - JetTestUtils.createEnvironmentWithJdkAndNullabilityAnnotationsFromIdea( - getTestRootDisposable(), ConfigurationKind.JDK_AND_ANNOTATIONS, TestJdkKind.MOCK_JDK); - - List files = ContainerUtil.map(Arrays.asList(javaFileNames), new Function() { - @Override - public File fun(String s) { - return new File(s); - } - }); - File expectedFile = new File(expectedFileName); - File tmpDir = JetTestUtils.tmpDir(expectedFile.getName()); - - Pair javaNamespaceAndBindingContext - = compileJavaAndLoadTestNamespaceAndBindingContextFromBinary(files, tmpDir, getTestRootDisposable(), - ConfigurationKind.JDK_ONLY); - - AbstractLoadJavaTest.checkJavaNamespace(expectedFile, javaNamespaceAndBindingContext); - } - - private void doTestNoCompile(@NotNull String expectedFileName, @NotNull String javaRoot) throws Exception { - File expectedFile = new File(expectedFileName); - - Pair javaNamespaceAndBindingContext - = loadTestNamespaceAndBindingContextFromJavaRoot(new File(javaRoot), getTestRootDisposable(), ConfigurationKind.JDK_ONLY); - - AbstractLoadJavaTest.checkJavaNamespace(expectedFile, javaNamespaceAndBindingContext); - } - public void testPackageLocalVisibility() throws Exception { String dir = PATH + "/packageLocalVisibility/simple/"; String javaDir = dir + "/java"; - doTest(dir + "/expected.txt", - javaDir + "/test/JFrame.java", - javaDir + "/awt/Frame.java"); + doTestCompiledJava(dir + "/expected.txt", + javaDir + "/test/JFrame.java", + javaDir + "/awt/Frame.java"); } public void testInner() throws Exception { @@ -102,156 +39,120 @@ public final class LoadJavaCustomTest extends TestCaseWithTmpdir { public void testProtectedStaticVisibility() throws Exception { String dir = PATH + "/protectedStaticVisibility/constructor/"; - doTest(dir + "ConstructorInProtectedStaticNestedClass.txt", - dir + "ConstructorInProtectedStaticNestedClass.java"); + doTestCompiledJava(dir + "ConstructorInProtectedStaticNestedClass.txt", + dir + "ConstructorInProtectedStaticNestedClass.java"); } public void testProtectedPackageFun() throws Exception { String dir = PATH + "/protectedPackageVisibility/"; - doTest(dir + "ProtectedPackageFun.txt", - dir + "ProtectedPackageFun.java"); + doTestCompiledJava(dir + "ProtectedPackageFun.txt", + dir + "ProtectedPackageFun.java"); } public void testProtectedPackageConstructor() throws Exception { String dir = PATH + "/protectedPackageVisibility/"; - doTest(dir + "ProtectedPackageConstructor.txt", - dir + "ProtectedPackageConstructor.java"); + doTestCompiledJava(dir + "ProtectedPackageConstructor.txt", + dir + "ProtectedPackageConstructor.java"); } public void testProtectedPackageProperty() throws Exception { String dir = PATH + "/protectedPackageVisibility/"; - doTest(dir + "ProtectedPackageProperty.txt", - dir + "ProtectedPackageProperty.java"); + doTestCompiledJava(dir + "ProtectedPackageProperty.txt", + dir + "ProtectedPackageProperty.java"); } public void testStaticFinal() throws Exception { String dir = "/staticFinal/"; - doTest(PATH + dir + "expected.txt", + doTestCompiledJava(PATH + dir + "expected.txt", PATH + dir + "test.java"); } private void doSimpleTest() throws Exception { - doTest(PATH + "/" + getTestName(true) + ".txt", + doTestCompiledJava(PATH + "/" + getTestName(true) + ".txt", PATH + "/" + getTestName(true) + ".java"); } public void testKotlinSignatureTwoSuperclassesInconsistentGenericTypes() throws Exception { String dir = PATH + "/kotlinSignature/"; - doTest(dir + "TwoSuperclassesInconsistentGenericTypes.txt", - dir + "TwoSuperclassesInconsistentGenericTypes.java"); + doTestCompiledJava(dir + "TwoSuperclassesInconsistentGenericTypes.txt", + dir + "TwoSuperclassesInconsistentGenericTypes.java"); } public void testKotlinSignatureTwoSuperclassesVarargAndNot() throws Exception { String dir = PATH + "/kotlinSignature/"; - doTest(dir + "TwoSuperclassesVarargAndNot.txt", - dir + "TwoSuperclassesVarargAndNot.java"); + doTestCompiledJava(dir + "TwoSuperclassesVarargAndNot.txt", + dir + "TwoSuperclassesVarargAndNot.java"); } //TODO: move to LoadJavaTestGenerated when possible public void testEnum() throws Exception { String dir = PATH + "/enum"; String javaDir = dir + "/java"; - doTest(dir + "/expected.txt", - javaDir + "/MyEnum.java"); + doTestCompiledJava(dir + "/expected.txt", + javaDir + "/MyEnum.java"); } public void testRawSuperType() throws Exception { String dir = PATH + "/rawSuperType/"; - doTest(dir + "RawSuperType.txt", - dir + "RawSuperType.java"); + doTestCompiledJava(dir + "RawSuperType.txt", + dir + "RawSuperType.java"); } public void testSubclassWithRawType() throws Exception { String dir = PATH + "/subclassWithRawType/"; - doTest(dir + "SubclassWithRawType.txt", - dir + "SubclassWithRawType.java"); + doTestCompiledJava(dir + "SubclassWithRawType.txt", + dir + "SubclassWithRawType.java"); } public void testArraysInSubtypes() throws Exception { String dir = PATH + "/arraysInSubtypes/"; - doTest(dir + "ArraysInSubtypes.txt", - dir + "ArraysInSubtypes.java"); + doTestCompiledJava(dir + "ArraysInSubtypes.txt", + dir + "ArraysInSubtypes.java"); } public void testMethodTypeParameterErased() throws Exception { String dir = PATH + "/methodTypeParameterErased/"; - doTest(dir + "MethodTypeParameterErased.txt", - dir + "MethodTypeParameterErased.java"); + doTestCompiledJava(dir + "MethodTypeParameterErased.txt", + dir + "MethodTypeParameterErased.java"); } public void testReturnInnerSubclassOfSupersInner() throws Exception { String dir = PATH + "/returnInnerSubclassOfSupersInner/"; - doTest(dir + "ReturnInnerSubclassOfSupersInner.txt", - dir + "test/ReturnInnerSubclassOfSupersInner.java"); + doTestCompiledJava(dir + "ReturnInnerSubclassOfSupersInner.txt", + dir + "test/ReturnInnerSubclassOfSupersInner.java"); } public void testReturnInnerSubclassOfSupersInnerNoCompile() throws Exception { // Test is here because Java PSI used to have some differences when loading parallel generic hierarchies from cls and source code. String dir = PATH + "/returnInnerSubclassOfSupersInner/"; - doTestNoCompile(dir + "ReturnInnerSubclassOfSupersInner.txt", dir); + doTestSourceJava(dir + "ReturnInnerSubclassOfSupersInner.txt", dir); } public void testReturnNotSubtype() throws Exception { String dir = PATH + "/returnNotSubtype/"; - doTestNoCompile(dir + "ReturnNotSubtype.txt", dir); + doTestSourceJava(dir + "ReturnNotSubtype.txt", dir); } public void testErrorTypes() throws Exception { String dir = PATH + "/errorTypes/"; - doTestNoCompile(dir + "ErrorTypes.txt", dir); + doTestSourceJava(dir + "ErrorTypes.txt", dir); } - public static class SubclassingKotlinInJavaTest extends TestCaseWithTmpdir { + public static class SubclassingKotlinInJavaTest extends AbstractLoadJavaTest { public void testSubclassingKotlinInJava() throws Exception { - doTest(PATH + "/" + getTestName(true)); + doTestJavaAgainstKotlin(PATH + "/" + getTestName(true)); } public void testDeepSubclassingKotlinInJava() throws Exception { - doTest(PATH + "/" + getTestName(true)); + doTestJavaAgainstKotlin(PATH + "/" + getTestName(true)); } public void testPackageInheritance() throws Exception { - doTest(PATH + "/packageLocalVisibility/inheritance"); + doTestJavaAgainstKotlin(PATH + "/packageLocalVisibility/inheritance"); } public void testProtectedPackageInheritance() throws Exception { - doTest(PATH + "/protectedPackageVisibility/inheritance"); - } - - public void doTest(String path) throws Exception { - File dir = new File(path); - - CompilerConfiguration configuration = JetTestUtils.compilerConfigurationForTests( - ConfigurationKind.JDK_ONLY, TestJdkKind.MOCK_JDK, new File(dir, "java")); - configuration.put(CommonConfigurationKeys.SOURCE_ROOTS_KEY, Arrays.asList(new File(dir, "kotlin").getAbsolutePath())); - JetCoreEnvironment environment = new JetCoreEnvironment(getTestRootDisposable(), configuration); - - ModuleDescriptor moduleDescriptor = new ModuleDescriptor(Name.special("")); - - // we need the same binding trace for resolve from Java and Kotlin - BindingTrace trace = CliLightClassGenerationSupport.getInstanceForCli(environment.getProject()).getTrace(); - - InjectorForJavaDescriptorResolver injectorForJava = new InjectorForJavaDescriptorResolver(environment.getProject(), - trace, - moduleDescriptor); - - InjectorForTopDownAnalyzerForJvm injectorForAnalyzer = new InjectorForTopDownAnalyzerForJvm( - environment.getProject(), - new TopDownAnalysisParameters(Predicates.alwaysFalse(), false, false, Collections.emptyList()), - trace, - moduleDescriptor); - - injectorForAnalyzer.getTopDownAnalyzer().analyzeFiles(environment.getSourceFiles(), Collections.emptyList()); - - JavaDescriptorResolver javaDescriptorResolver = injectorForJava.getJavaDescriptorResolver(); - NamespaceDescriptor namespaceDescriptor = javaDescriptorResolver.resolveNamespace( - LoadDescriptorUtil.TEST_PACKAGE_FQNAME, DescriptorSearchRule.INCLUDE_KOTLIN); - assert namespaceDescriptor != null; - - compareNamespaceWithFile(namespaceDescriptor, NamespaceComparator.DONT_INCLUDE_METHODS_OF_OBJECT, - new File(dir, "expected.txt")); - - ExpectedLoadErrorsUtil.checkForLoadErrors(namespaceDescriptor, trace.getBindingContext()); + doTestJavaAgainstKotlin(PATH + "/protectedPackageVisibility/inheritance"); } } }