AbstractCompileJavaAgainstKotlinTest: J2K
This commit is contained in:
committed by
Mikhail Glukhikh
parent
be41364fdd
commit
5c01b6fa20
+52
-61
@@ -14,75 +14,66 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.jetbrains.kotlin.jvm.compiler;
|
package org.jetbrains.kotlin.jvm.compiler
|
||||||
|
|
||||||
import kotlin.Unit;
|
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
|
||||||
import org.jetbrains.kotlin.analyzer.AnalysisResult;
|
import org.jetbrains.kotlin.name.FqName
|
||||||
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles;
|
import org.jetbrains.kotlin.renderer.AnnotationArgumentsRenderingPolicy
|
||||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
|
import org.jetbrains.kotlin.renderer.DescriptorRenderer
|
||||||
import org.jetbrains.kotlin.descriptors.PackageViewDescriptor;
|
import org.jetbrains.kotlin.renderer.DescriptorRendererModifier
|
||||||
import org.jetbrains.kotlin.name.FqName;
|
import org.jetbrains.kotlin.renderer.ParameterNameRenderingPolicy
|
||||||
import org.jetbrains.kotlin.renderer.AnnotationArgumentsRenderingPolicy;
|
import org.jetbrains.kotlin.resolve.lazy.JvmResolveUtil
|
||||||
import org.jetbrains.kotlin.renderer.DescriptorRenderer;
|
import org.jetbrains.kotlin.test.ConfigurationKind
|
||||||
import org.jetbrains.kotlin.renderer.DescriptorRendererModifier;
|
import org.jetbrains.kotlin.test.KotlinTestUtils.*
|
||||||
import org.jetbrains.kotlin.renderer.ParameterNameRenderingPolicy;
|
import org.jetbrains.kotlin.test.TestCaseWithTmpdir
|
||||||
import org.jetbrains.kotlin.resolve.lazy.JvmResolveUtil;
|
import org.jetbrains.kotlin.test.TestJdkKind
|
||||||
import org.jetbrains.kotlin.test.ConfigurationKind;
|
import org.jetbrains.kotlin.test.util.RecursiveDescriptorComparator.validateAndCompareDescriptorWithFile
|
||||||
import org.jetbrains.kotlin.test.TestCaseWithTmpdir;
|
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||||
import org.jetbrains.kotlin.test.TestJdkKind;
|
import org.junit.Assert
|
||||||
import org.jetbrains.kotlin.test.util.RecursiveDescriptorComparator;
|
import java.io.File
|
||||||
import org.junit.Assert;
|
import java.io.IOException
|
||||||
|
import java.lang.annotation.Retention
|
||||||
|
|
||||||
import java.io.File;
|
abstract class AbstractCompileJavaAgainstKotlinTest : TestCaseWithTmpdir() {
|
||||||
import java.io.IOException;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
import static org.jetbrains.kotlin.test.KotlinTestUtils.*;
|
@Throws(IOException::class)
|
||||||
import static org.jetbrains.kotlin.test.util.RecursiveDescriptorComparator.validateAndCompareDescriptorWithFile;
|
protected fun doTest(ktFilePath: String) {
|
||||||
|
Assert.assertTrue(ktFilePath.endsWith(".kt"))
|
||||||
|
val ktFile = File(ktFilePath)
|
||||||
|
val javaFile = File(ktFilePath.replaceFirst("\\.kt$".toRegex(), ".java"))
|
||||||
|
val javaErrorFile = File(ktFilePath.replaceFirst("\\.kt$".toRegex(), ".javaerr.txt"))
|
||||||
|
|
||||||
@SuppressWarnings({"JUnitTestCaseWithNonTrivialConstructors", "JUnitTestCaseWithNoTests"})
|
val out = File(tmpdir, "out")
|
||||||
public abstract class AbstractCompileJavaAgainstKotlinTest extends TestCaseWithTmpdir {
|
val compiledSuccessfully = compileKotlinWithJava(listOf(javaFile), listOf(ktFile),
|
||||||
// Do not render parameter names because there are test cases where classes inherit from JDK collections,
|
out, testRootDisposable, javaErrorFile)
|
||||||
// and some versions of JDK have debug information in the class files (including parameter names), and some don't
|
if (!compiledSuccessfully) return
|
||||||
private static final RecursiveDescriptorComparator.Configuration CONFIGURATION =
|
|
||||||
AbstractLoadJavaTest.COMPARATOR_CONFIGURATION.withRenderer(
|
|
||||||
DescriptorRenderer.Companion.withOptions(
|
|
||||||
options -> {
|
|
||||||
options.setWithDefinedIn(false);
|
|
||||||
options.setParameterNameRenderingPolicy(ParameterNameRenderingPolicy.NONE);
|
|
||||||
options.setVerbose(true);
|
|
||||||
options.setAnnotationArgumentsRenderingPolicy(AnnotationArgumentsRenderingPolicy.UNLESS_EMPTY);
|
|
||||||
options.setExcludedAnnotationClasses(Collections.singleton(new FqName(Retention.class.getName())));
|
|
||||||
options.setModifiers(DescriptorRendererModifier.ALL);
|
|
||||||
return Unit.INSTANCE;
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
protected void doTest(String ktFilePath) throws IOException {
|
val environment = KotlinCoreEnvironment.createForTests(
|
||||||
Assert.assertTrue(ktFilePath.endsWith(".kt"));
|
testRootDisposable,
|
||||||
File ktFile = new File(ktFilePath);
|
|
||||||
File javaFile = new File(ktFilePath.replaceFirst("\\.kt$", ".java"));
|
|
||||||
File javaErrorFile = new File(ktFilePath.replaceFirst("\\.kt$", ".javaerr.txt"));
|
|
||||||
|
|
||||||
File out = new File(tmpdir, "out");
|
|
||||||
boolean compiledSuccessfully =
|
|
||||||
compileKotlinWithJava(Collections.singletonList(javaFile), Collections.singletonList(ktFile),
|
|
||||||
out, getTestRootDisposable(), javaErrorFile);
|
|
||||||
if (!compiledSuccessfully) return;
|
|
||||||
|
|
||||||
KotlinCoreEnvironment environment = KotlinCoreEnvironment.createForTests(
|
|
||||||
getTestRootDisposable(),
|
|
||||||
newConfiguration(ConfigurationKind.ALL, TestJdkKind.MOCK_JDK, getAnnotationsJar(), out),
|
newConfiguration(ConfigurationKind.ALL, TestJdkKind.MOCK_JDK, getAnnotationsJar(), out),
|
||||||
EnvironmentConfigFiles.JVM_CONFIG_FILES
|
EnvironmentConfigFiles.JVM_CONFIG_FILES
|
||||||
);
|
)
|
||||||
|
|
||||||
AnalysisResult analysisResult = JvmResolveUtil.analyze(environment);
|
val analysisResult = JvmResolveUtil.analyze(environment)
|
||||||
PackageViewDescriptor packageView = analysisResult.getModuleDescriptor().getPackage(LoadDescriptorUtil.TEST_PACKAGE_FQNAME);
|
val packageView = analysisResult.moduleDescriptor.getPackage(LoadDescriptorUtil.TEST_PACKAGE_FQNAME)
|
||||||
assertFalse("Nothing found in package " + LoadDescriptorUtil.TEST_PACKAGE_FQNAME, packageView.isEmpty());
|
assertFalse("Nothing found in package " + LoadDescriptorUtil.TEST_PACKAGE_FQNAME, packageView.isEmpty())
|
||||||
|
|
||||||
File expectedFile = new File(ktFilePath.replaceFirst("\\.kt$", ".txt"));
|
val expectedFile = File(ktFilePath.replaceFirst("\\.kt$".toRegex(), ".txt"))
|
||||||
validateAndCompareDescriptorWithFile(packageView, CONFIGURATION, expectedFile);
|
validateAndCompareDescriptorWithFile(packageView, CONFIGURATION, expectedFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
// Do not render parameter names because there are test cases where classes inherit from JDK collections,
|
||||||
|
// and some versions of JDK have debug information in the class files (including parameter names), and some don't
|
||||||
|
private val CONFIGURATION = AbstractLoadJavaTest.COMPARATOR_CONFIGURATION.withRenderer(
|
||||||
|
DescriptorRenderer.withOptions {
|
||||||
|
withDefinedIn = false
|
||||||
|
parameterNameRenderingPolicy = ParameterNameRenderingPolicy.NONE
|
||||||
|
verbose = true
|
||||||
|
annotationArgumentsRenderingPolicy = AnnotationArgumentsRenderingPolicy.UNLESS_EMPTY
|
||||||
|
excludedAnnotationClasses = setOf(FqName(Retention::class.java.name))
|
||||||
|
modifiers = DescriptorRendererModifier.ALL
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user