Rewrite old exception filter test with library sources
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
fun box() {
|
||||
test.Foo().member()
|
||||
}
|
||||
|
||||
// WITH_MOCK_LIBRARY: true
|
||||
// FILE: lib.kt
|
||||
// LINE: 5
|
||||
@@ -0,0 +1,7 @@
|
||||
fun box() {
|
||||
test.foo()
|
||||
}
|
||||
|
||||
// WITH_MOCK_LIBRARY: true
|
||||
// FILE: lib.kt
|
||||
// LINE: 10
|
||||
@@ -0,0 +1,7 @@
|
||||
fun box() {
|
||||
test.other()
|
||||
}
|
||||
|
||||
// WITH_MOCK_LIBRARY: true
|
||||
// FILE: other.kt
|
||||
// LINE: 4
|
||||
@@ -0,0 +1,7 @@
|
||||
fun box() {
|
||||
renamed.foo()
|
||||
}
|
||||
|
||||
// WITH_MOCK_LIBRARY: true
|
||||
// FILE: jvmClassNameSameName.kt
|
||||
// LINE: 5
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
package renamed
|
||||
|
||||
fun bar() { }
|
||||
+1
-1
@@ -2,5 +2,5 @@
|
||||
package renamed
|
||||
|
||||
fun foo() {
|
||||
|
||||
null!!
|
||||
}
|
||||
+2
-1
@@ -2,9 +2,10 @@ package test
|
||||
|
||||
class Foo {
|
||||
fun member() {
|
||||
null!!
|
||||
}
|
||||
}
|
||||
|
||||
fun foo() {
|
||||
println()
|
||||
null!!
|
||||
}
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
package test
|
||||
|
||||
fun other() {
|
||||
println("")
|
||||
null!!
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
This test doesn't have sources, but IDEA test framework needs sources dir
|
||||
@@ -17,7 +17,11 @@
|
||||
package org.jetbrains.kotlin.idea.filters
|
||||
|
||||
import com.intellij.execution.filters.FileHyperlinkInfo
|
||||
import com.intellij.openapi.application.ApplicationManager
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager
|
||||
import com.intellij.openapi.roots.ModuleRootManager
|
||||
import com.intellij.openapi.roots.OrderRootType
|
||||
import com.intellij.openapi.util.io.FileUtilRt
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import com.intellij.testFramework.PsiTestUtil
|
||||
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime
|
||||
@@ -25,6 +29,7 @@ import org.jetbrains.kotlin.fileClasses.NoResolveFileClassesProvider
|
||||
import org.jetbrains.kotlin.fileClasses.getFileClassFqName
|
||||
import org.jetbrains.kotlin.idea.core.refactoring.toVirtualFile
|
||||
import org.jetbrains.kotlin.idea.test.KotlinCodeInsightTestCase
|
||||
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.test.InTextDirectivesUtils
|
||||
import org.jetbrains.kotlin.test.MockLibraryUtil
|
||||
@@ -33,6 +38,9 @@ import java.lang.reflect.InvocationTargetException
|
||||
import java.net.URL
|
||||
import java.net.URLClassLoader
|
||||
|
||||
private var MOCK_LIBRARY_JAR: File? = null
|
||||
private val MOCK_LIBRARY_SOURCES = PluginTestCaseBase.getTestDataPathBase() + "/debugger/mockLibraryForExceptionFilter"
|
||||
|
||||
public abstract class AbstractKotlinExceptionFilterTest: KotlinCodeInsightTestCase() {
|
||||
override fun getTestDataPath() = ""
|
||||
|
||||
@@ -44,14 +52,43 @@ public abstract class AbstractKotlinExceptionFilterTest: KotlinCodeInsightTestCa
|
||||
|
||||
val fileText = file.text
|
||||
|
||||
val outDir = project.baseDir.createChildDirectory(this, "out")
|
||||
val outDir = project.baseDir.findChild("out") ?: project.baseDir.createChildDirectory(this, "out")
|
||||
PsiTestUtil.setCompilerOutputPath(module, outDir.url, false)
|
||||
|
||||
MockLibraryUtil.compileKotlin(path, File(outDir.path))
|
||||
val classLoader: URLClassLoader
|
||||
if (InTextDirectivesUtils.getPrefixedBoolean(fileText, "// WITH_MOCK_LIBRARY: ") ?: false) {
|
||||
if (MOCK_LIBRARY_JAR == null) {
|
||||
MOCK_LIBRARY_JAR = MockLibraryUtil.compileLibraryToJar(MOCK_LIBRARY_SOURCES, "mockLibrary", true)
|
||||
}
|
||||
|
||||
val mockLibraryJar = MOCK_LIBRARY_JAR
|
||||
val mockLibraryPath = FileUtilRt.toSystemIndependentName(mockLibraryJar.canonicalPath)
|
||||
val libRootUrl = "jar://$mockLibraryPath!/"
|
||||
|
||||
ApplicationManager.getApplication().runWriteAction {
|
||||
val moduleModel = ModuleRootManager.getInstance(myModule).modifiableModel
|
||||
with(moduleModel.moduleLibraryTable.modifiableModel.createLibrary("mockLibrary").modifiableModel) {
|
||||
addRoot(libRootUrl, OrderRootType.CLASSES)
|
||||
addRoot(libRootUrl + "src/", OrderRootType.SOURCES)
|
||||
commit()
|
||||
}
|
||||
moduleModel.commit()
|
||||
}
|
||||
MockLibraryUtil.compileKotlin(path, File(outDir.path), mockLibraryPath)
|
||||
classLoader = URLClassLoader(
|
||||
arrayOf(URL(outDir.url + "/"), mockLibraryJar.toURI().toURL()),
|
||||
ForTestCompileRuntime.runtimeJarClassLoader())
|
||||
}
|
||||
else {
|
||||
MockLibraryUtil.compileKotlin(path, File(outDir.path))
|
||||
classLoader = URLClassLoader(
|
||||
arrayOf(URL(outDir.url + "/")),
|
||||
ForTestCompileRuntime.runtimeJarClassLoader())
|
||||
}
|
||||
|
||||
val stackTraceElement = try {
|
||||
val className = NoResolveFileClassesProvider.getFileClassFqName(file as KtFile)
|
||||
val clazz = URLClassLoader(arrayOf(URL(outDir.url + "/")), ForTestCompileRuntime.runtimeJarClassLoader()).loadClass(className.asString())
|
||||
val clazz = classLoader.loadClass(className.asString())
|
||||
clazz.getMethod("box")?.invoke(null)
|
||||
throw AssertionError("class ${className.asString()} should have box() method and throw exception")
|
||||
}
|
||||
@@ -67,14 +104,16 @@ public abstract class AbstractKotlinExceptionFilterTest: KotlinCodeInsightTestCa
|
||||
val descriptor = info.descriptor!!
|
||||
|
||||
val expectedFileName = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// FILE: ")!!
|
||||
val expectedVirtualFile = File(rootDir, expectedFileName).toVirtualFile() ?: throw AssertionError("Couldn't find file: name = $expectedFileName")
|
||||
val expectedVirtualFile = File(rootDir, expectedFileName).toVirtualFile()
|
||||
?: File(MOCK_LIBRARY_SOURCES, expectedFileName).toVirtualFile()
|
||||
?: throw AssertionError("Couldn't find file: name = $expectedFileName")
|
||||
val expectedLineNumber = InTextDirectivesUtils.getPrefixedInt(fileText, "// LINE: ")!!
|
||||
|
||||
// TODO compare virtual files
|
||||
assertEquals(expectedFileName, descriptor.file.name)
|
||||
assertEquals("Wrong fileName for line $stackTraceElement", expectedFileName, descriptor.file.name)
|
||||
|
||||
val document = FileDocumentManager.getInstance().getDocument(expectedVirtualFile)!!
|
||||
val expectedOffset = document.getLineStartOffset(expectedLineNumber - 1)
|
||||
assertEquals(expectedOffset, descriptor.offset)
|
||||
assertEquals("Wrong offset for line $stackTraceElement", expectedOffset, descriptor.offset)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,132 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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.kotlin.idea.filters
|
||||
|
||||
import com.intellij.execution.filters.FileHyperlinkInfo
|
||||
import com.intellij.openapi.application.ApplicationManager
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager
|
||||
import com.intellij.openapi.roots.ModuleRootManager
|
||||
import com.intellij.openapi.roots.OrderRootType
|
||||
import com.intellij.openapi.util.io.FileUtilRt
|
||||
import com.intellij.openapi.vfs.VfsUtilCore
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.openapi.vfs.VirtualFileManager
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import com.intellij.refactoring.MultiFileTestCase
|
||||
import com.intellij.testFramework.PlatformTestCase
|
||||
import com.intellij.testFramework.PsiTestUtil
|
||||
import junit.framework.TestCase
|
||||
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase
|
||||
import org.jetbrains.kotlin.load.kotlin.PackagePartClassUtils
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.test.MockLibraryUtil
|
||||
import java.io.File
|
||||
|
||||
public class KotlinExceptionFilterTest : MultiFileTestCase() {
|
||||
private var rootDir: VirtualFile? = null
|
||||
|
||||
override fun tearDown() {
|
||||
rootDir = null
|
||||
super.tearDown()
|
||||
}
|
||||
|
||||
override fun getTestDataPath() = PluginTestCaseBase.getTestDataPathBase()
|
||||
override fun getTestRoot() = "/filters/exceptionFilter/"
|
||||
|
||||
private fun configure() {
|
||||
val path = getTestDataPath() + getTestRoot() + getTestName(true)
|
||||
|
||||
rootDir = PsiTestUtil.createTestProjectStructure(myProject, myModule, path, PlatformTestCase.myFilesToDelete, false)
|
||||
prepareProject(rootDir)
|
||||
PsiDocumentManager.getInstance(myProject).commitAllDocuments()
|
||||
}
|
||||
|
||||
private fun createStackTraceElementLine(prefix: String, fileName: String, className: String, lineNumber: Int): String {
|
||||
// Method name doesn't matter
|
||||
val methodName = "foo"
|
||||
|
||||
// File's last name appears in stack trace
|
||||
val fileLastName = File(fileName).getName()
|
||||
|
||||
val element = StackTraceElement(className, methodName, fileLastName, lineNumber)
|
||||
return prefix + element + "\n"
|
||||
}
|
||||
|
||||
private fun doTest(relativePath: String, lineNumber: Int, className: (VirtualFile) -> String, linePrefix: String = "\tat ", libRootUrl: String? = null) {
|
||||
if (rootDir == null) {
|
||||
configure()
|
||||
}
|
||||
assert(rootDir != null)
|
||||
|
||||
val filter = KotlinExceptionFilterFactory().create(GlobalSearchScope.allScope(myProject))
|
||||
|
||||
val expectedFile = if (libRootUrl != null) {
|
||||
VirtualFileManager.getInstance().findFileByUrl(libRootUrl + relativePath);
|
||||
}
|
||||
else {
|
||||
VfsUtilCore.findRelativeFile(relativePath, rootDir);
|
||||
}
|
||||
TestCase.assertNotNull(expectedFile)
|
||||
expectedFile!!
|
||||
|
||||
val line = createStackTraceElementLine(linePrefix, relativePath, className(expectedFile), lineNumber)
|
||||
val result = filter.applyFilter(line, 0)
|
||||
|
||||
TestCase.assertNotNull(result)
|
||||
result!!
|
||||
val info = result.getFirstHyperlinkInfo()
|
||||
TestCase.assertNotNull(info)
|
||||
info as FileHyperlinkInfo
|
||||
val descriptor = info.getDescriptor()
|
||||
TestCase.assertNotNull(descriptor)
|
||||
descriptor!!
|
||||
|
||||
TestCase.assertEquals(expectedFile, descriptor.getFile())
|
||||
|
||||
val document = FileDocumentManager.getInstance().getDocument(expectedFile)
|
||||
TestCase.assertNotNull(document)
|
||||
document!!
|
||||
|
||||
val expectedOffset = document.getLineStartOffset(lineNumber - 1)
|
||||
TestCase.assertEquals(expectedOffset, descriptor.getOffset())
|
||||
}
|
||||
|
||||
public fun testLibrarySources() {
|
||||
val mockLibrary = MockLibraryUtil.compileLibraryToJar(getTestDataPath() + getTestRoot() + "mockLibrary", "mockLibrary", true)
|
||||
|
||||
val libRootUrl = "jar://" + FileUtilRt.toSystemIndependentName(mockLibrary.getAbsolutePath()) + "!/"
|
||||
|
||||
ApplicationManager.getApplication().runWriteAction {
|
||||
val moduleModel = ModuleRootManager.getInstance(myModule).getModifiableModel()
|
||||
with(moduleModel.getModuleLibraryTable().getModifiableModel().createLibrary("mockLibrary").getModifiableModel()) {
|
||||
addRoot(libRootUrl, OrderRootType.CLASSES)
|
||||
addRoot(libRootUrl + "src/", OrderRootType.SOURCES)
|
||||
commit()
|
||||
}
|
||||
moduleModel.commit()
|
||||
}
|
||||
|
||||
val packageClassFqName = FqName("test.TestPackage")
|
||||
|
||||
doTest("src/lib.kt", 3, { "test.Foo" }, libRootUrl = libRootUrl)
|
||||
doTest("src/lib.kt", 4, { "test.Foo" }, libRootUrl = libRootUrl)
|
||||
doTest("src/lib.kt", 9, { "" + PackagePartClassUtils.getDefaultPartFqName(packageClassFqName, it) }, libRootUrl = libRootUrl)
|
||||
doTest("src/other.kt", 4, { "" + PackagePartClassUtils.getDefaultPartFqName(packageClassFqName, it) }, libRootUrl = libRootUrl)
|
||||
doTest("src/jvmClassNameSingleFile.kt", 4, { "renamed.NewName" }, libRootUrl = libRootUrl)
|
||||
}
|
||||
}
|
||||
@@ -71,6 +71,30 @@ public class KotlinExceptionFilterTestGenerated extends AbstractKotlinExceptionF
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("librarySources1")
|
||||
public void testLibrarySources1() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/debugger/exceptionFilter/librarySources1/");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("librarySources2")
|
||||
public void testLibrarySources2() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/debugger/exceptionFilter/librarySources2/");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("librarySources3")
|
||||
public void testLibrarySources3() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/debugger/exceptionFilter/librarySources3/");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("librarySources4")
|
||||
public void testLibrarySources4() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/debugger/exceptionFilter/librarySources4/");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("multiSamePackage")
|
||||
public void testMultiSamePackage() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/debugger/exceptionFilter/multiSamePackage/");
|
||||
|
||||
Reference in New Issue
Block a user