More tests for decompiling classes with wrong abi version

This commit is contained in:
Nikolay Krasko
2014-11-10 18:22:03 +03:00
parent 43eb94726c
commit d6ffd22533
9 changed files with 143 additions and 81 deletions
@@ -0,0 +1,8 @@
package wrong
class ClassWithInnerLambda {
fun test(a: () -> Unit) = a
fun other() {
test({})
}
}
@@ -0,0 +1,5 @@
package wrong
trait TraitWithDefault {
fun foo() = 1
}
@@ -0,0 +1,99 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.plugin.decompiler
import org.jetbrains.jet.plugin.JetLightCodeInsightFixtureTestCase
import com.intellij.testFramework.LightProjectDescriptor
import org.jetbrains.jet.plugin.JdkAndMockLibraryProjectDescriptor
import org.jetbrains.jet.plugin.PluginTestCaseBase
import com.intellij.openapi.vfs.VirtualFile
import org.jetbrains.jet.lang.resolve.java.JvmAbi
import com.intellij.psi.PsiManager
import org.junit.Assert
import com.intellij.psi.ClassFileViewProvider
import com.intellij.psi.impl.compiled.ClsFileImpl
import com.intellij.psi.PsiCompiledFile
import com.intellij.psi.PsiJavaFile
import org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames.KotlinSyntheticClass
import org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames.KotlinSyntheticClass.Kind.*
import org.jetbrains.jet.lang.resolve.kotlin.KotlinBinaryClassCache
import org.jetbrains.jet.plugin.decompiler.navigation.NavigateToDecompiledLibraryTest
public abstract class AbstractInternalCompiledClassesTest : JetLightCodeInsightFixtureTestCase() {
private fun isSyntheticClassOfKind(kind: KotlinSyntheticClass.Kind) : VirtualFile.() -> Boolean = {
val header = KotlinBinaryClassCache.getKotlinBinaryClass(this)?.getClassHeader()
header?.syntheticClassKind == kind
}
protected fun doTestTraitImplClassIsVisibleAsJavaClass() {
val project = getProject()!!
doTest("trait impl", isSyntheticClassOfKind(TRAIT_IMPL)) {
val psiFile = PsiManager.getInstance(project).findFile(this)!!
Assert.assertTrue("Should not be kotlin file",
psiFile !is JetClsFile)
Assert.assertTrue("Should be java file, was ${psiFile.javaClass.getSimpleName()}",
psiFile is ClsFileImpl)
val decompiledPsiFile = (psiFile as PsiCompiledFile).getDecompiledPsiFile()!!
Assert.assertTrue("Should be java decompiled file, was ${decompiledPsiFile.javaClass.getSimpleName()}",
decompiledPsiFile is PsiJavaFile)
val classes = (decompiledPsiFile as PsiJavaFile).getClasses()
Assert.assertTrue("Should have some decompiled text",
classes.size == 1 && classes[0].getName()!!.endsWith(JvmAbi.TRAIT_IMPL_SUFFIX))
}
}
protected fun doTestNoPsiFilesAreBuiltForSyntheticClass(kind: KotlinSyntheticClass.Kind): Unit =
doTestNoPsiFilesAreBuiltFor(kind.toString(), isSyntheticClassOfKind(kind))
protected fun doTestNoPsiFilesAreBuiltFor(fileKind: String, acceptFile: VirtualFile.() -> Boolean) {
val project = getProject()!!
doTest(fileKind, acceptFile) {
val psiFile = PsiManager.getInstance(project).findFile(this)
Assert.assertNull("PSI files for $fileKind classes should not be build, is was build for: ${this.getPresentableName()}",
psiFile)
}
}
protected fun doTest(fileKind: String, acceptFile: VirtualFile.() -> Boolean, performTest: VirtualFile.() -> Unit) {
val root = NavigateToDecompiledLibraryTest.findTestLibraryRoot(myModule!!)!!
var foundAtLeastOneFile = false
root.checkRecursively {
if (acceptFile()) {
foundAtLeastOneFile = true
performTest()
}
}
Assert.assertTrue("Should find at least one file of kind ($fileKind). This assertion can fail in following scenarios:\n" +
"1. Test data is bad and doesn't cover this case.\n2. ABI has changed and test no longer checks anything.",
foundAtLeastOneFile)
}
protected fun VirtualFile.checkRecursively(body: VirtualFile.() -> Unit) {
if (!isDirectory()) {
body()
}
else {
for (file in getChildren()!!) {
file.checkRecursively(body)
}
}
}
}
@@ -16,25 +16,13 @@
package org.jetbrains.jet.plugin.decompiler
import org.jetbrains.jet.plugin.JetLightCodeInsightFixtureTestCase
import com.intellij.testFramework.LightProjectDescriptor
import org.jetbrains.jet.plugin.JdkAndMockLibraryProjectDescriptor
import org.jetbrains.jet.plugin.PluginTestCaseBase
import com.intellij.openapi.vfs.VirtualFile
import org.jetbrains.jet.lang.resolve.java.JvmAbi
import com.intellij.psi.PsiManager
import org.junit.Assert
import com.intellij.psi.ClassFileViewProvider
import com.intellij.psi.impl.compiled.ClsFileImpl
import com.intellij.psi.PsiCompiledFile
import com.intellij.psi.PsiJavaFile
import org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames.KotlinSyntheticClass
import org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames.KotlinSyntheticClass.Kind.*
import org.jetbrains.jet.lang.resolve.kotlin.KotlinBinaryClassCache
import org.jetbrains.jet.plugin.decompiler.navigation.NavigateToDecompiledLibraryTest
public class InternalCompiledClassesTest : JetLightCodeInsightFixtureTestCase() {
public class InternalCompiledClassesTest : AbstractInternalCompiledClassesTest() {
private val TEST_DATA_PATH = PluginTestCaseBase.getTestDataPathBase() + "/decompiler/internalClasses"
fun testPackagePartIsInvisible() = doTestNoPsiFilesAreBuiltForSyntheticClass(PACKAGE_PART)
@@ -57,68 +45,9 @@ public class InternalCompiledClassesTest : JetLightCodeInsightFixtureTestCase()
ClassFileViewProvider.isInnerClass(this)
}
fun testTraitImplClassIsVisibleAsJavaClass() {
val project = getProject()!!
doTest("trait impl", isSyntheticClassOfKind(TRAIT_IMPL)) {
val psiFile = PsiManager.getInstance(project).findFile(this)!!
Assert.assertTrue("Should not be kotlin file",
psiFile !is JetClsFile)
Assert.assertTrue("Should be java file, was ${psiFile.javaClass.getSimpleName()}",
psiFile is ClsFileImpl)
val decompiledPsiFile = (psiFile as PsiCompiledFile).getDecompiledPsiFile()!!
Assert.assertTrue("Should be java decompiled file, was ${decompiledPsiFile.javaClass.getSimpleName()}",
decompiledPsiFile is PsiJavaFile)
val classes = (decompiledPsiFile as PsiJavaFile).getClasses()
Assert.assertTrue("Should have some decompiled text",
classes.size == 1 && classes[0].getName()!!.endsWith(JvmAbi.TRAIT_IMPL_SUFFIX))
}
}
fun testTraitImplClassIsVisibleAsJavaClass() = doTestTraitImplClassIsVisibleAsJavaClass()
override fun getProjectDescriptor(): LightProjectDescriptor {
return JdkAndMockLibraryProjectDescriptor(TEST_DATA_PATH, /* withSources = */ false)
}
private fun isSyntheticClassOfKind(kind: KotlinSyntheticClass.Kind) : VirtualFile.() -> Boolean = {
val header = KotlinBinaryClassCache.getKotlinBinaryClass(this)?.getClassHeader()
header?.syntheticClassKind == kind
}
private fun doTestNoPsiFilesAreBuiltForSyntheticClass(kind: KotlinSyntheticClass.Kind) =
doTestNoPsiFilesAreBuiltFor(kind.toString(), isSyntheticClassOfKind(kind))
private fun doTestNoPsiFilesAreBuiltFor(fileKind: String, acceptFile: VirtualFile.() -> Boolean) {
val project = getProject()!!
doTest(fileKind, acceptFile) {
val psiFile = PsiManager.getInstance(project).findFile(this)
Assert.assertNull("PSI files for $fileKind classes should not be build, is was build for: ${this.getPresentableName()}",
psiFile)
}
}
private fun doTest(fileKind: String, acceptFile: VirtualFile.() -> Boolean, performTest: VirtualFile.() -> Unit) {
val root = NavigateToDecompiledLibraryTest.findTestLibraryRoot(myModule!!)!!
var foundAtLeastOneFile = false
root.checkRecursively {
if (acceptFile()) {
foundAtLeastOneFile = true
performTest()
}
}
Assert.assertTrue("Should find at least one file of kind ($fileKind). This assertion can fail in following scenarios:\n" +
"1. Test data is bad and doesn't cover this case.\n2. ABI has changed and test no longer checks anything.",
foundAtLeastOneFile)
}
private fun VirtualFile.checkRecursively(body: VirtualFile.() -> Unit) {
if (!isDirectory()) {
body()
}
else {
for (file in getChildren()!!) {
file.checkRecursively(body)
}
}
}
}
@@ -16,7 +16,6 @@
package org.jetbrains.jet.plugin.decompiler.textBuilder
import org.jetbrains.jet.plugin.JetLightCodeInsightFixtureTestCase
import com.intellij.testFramework.LightProjectDescriptor
import org.jetbrains.jet.JetTestCaseBuilder
import com.intellij.psi.PsiManager
@@ -28,21 +27,43 @@ import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.impl.compiled.ClsFileImpl
import org.jetbrains.jet.plugin.decompiler.navigation.NavigateToDecompiledLibraryTest
import org.jetbrains.jet.lang.psi.JetFile
import com.intellij.openapi.vfs.VfsUtilCore
import org.jetbrains.jet.utils.addIfNotNull
import java.util.LinkedHashSet
import java.util.regex.Pattern
import org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames.KotlinSyntheticClass.Kind.PACKAGE_PART
import org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames.KotlinSyntheticClass.Kind.ANONYMOUS_FUNCTION
import org.jetbrains.jet.plugin.decompiler.AbstractInternalCompiledClassesTest
public class DecompiledTextForWrongAbiVersionTest : JetLightCodeInsightFixtureTestCase() {
public class DecompiledTextForWrongAbiVersionTest : AbstractInternalCompiledClassesTest() {
override fun getProjectDescriptor(): LightProjectDescriptor {
return JetJdkAndLibraryProjectDescriptor(File(JetTestCaseBuilder.getTestDataPathBase() + "/cli/jvm/wrongAbiVersionLib/bin"))
}
fun testClassWithWrongAbiVersion() {
val root = NavigateToDecompiledLibraryTest.findTestLibraryRoot(myModule!!)
checkFileWithWrongAbiVersion(root!!.findChild("ClassWithWrongAbiVersion.class")!!)
}
fun testPackagePartIsInvisibleWrongAbiVersion() = doTestNoPsiFilesAreBuiltForSyntheticClass(PACKAGE_PART)
fun testPackageFacadeWithWrongAbiVersion() {
fun testTraitImplClassIsVisibleAsJavaClassWrongAbiVersion() = doTestTraitImplClassIsVisibleAsJavaClass()
fun testAnonymousFunctionIsInvisibleWrongAbiVersion() = doTestNoPsiFilesAreBuiltForSyntheticClass(ANONYMOUS_FUNCTION)
fun testClassWithWrongAbiVersion() = doTest("ClassWithWrongAbiVersion\\.class")
fun testPackageFacadeWithWrongAbiVersion() = doTest("WrongPackage\\.class")
fun doTest(namePattern: String) {
val root = NavigateToDecompiledLibraryTest.findTestLibraryRoot(myModule!!)
checkFileWithWrongAbiVersion(root!!.findChild("wrong")!!.findChild("WrongPackage.class")!!)
val pattern = Pattern.compile(namePattern)
val files = LinkedHashSet<VirtualFile>()
VfsUtilCore.iterateChildrenRecursively(
root,
{ virtualFile -> virtualFile.isDirectory() || pattern.matcher(virtualFile.getName()).matches() },
{ virtualFile -> if (!virtualFile.isDirectory()) files.addIfNotNull(virtualFile); true })
Assert.assertTrue("Only file should matches the pattern '$namePattern', but found: $files", files.size == 1)
checkFileWithWrongAbiVersion(files.single())
}
private fun checkFileWithWrongAbiVersion(file: VirtualFile) {