201: Convert AbstractHierarchyTest.java because of typealiases

This commit is contained in:
Nikolay Krasko
2020-01-28 21:55:18 +03:00
committed by Nikolay Krasko
parent 9f6f6838b8
commit 27467e4db7
@@ -2,23 +2,32 @@
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
package org.jetbrains.kotlin.idea.hierarchy
package org.jetbrains.kotlin.idea.hierarchy;
import com.intellij.ide.hierarchy.* import com.intellij.ide.hierarchy.*
import com.intellij.ide.hierarchy.actions.BrowseHierarchyActionBase
import com.intellij.ide.hierarchy.call.CallerMethodsTreeStructure
import com.intellij.ide.hierarchy.type.SubtypesHierarchyTreeStructure
import com.intellij.ide.hierarchy.type.SupertypesHierarchyTreeStructure
import com.intellij.ide.hierarchy.type.TypeHierarchyTreeStructure
import com.intellij.lang.LanguageExtension import com.intellij.lang.LanguageExtension
import com.intellij.openapi.actionSystem.CommonDataKeys import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.editor.Editor import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.fileEditor.impl.text.TextEditorPsiDataProvider import com.intellij.openapi.fileEditor.impl.text.TextEditorPsiDataProvider
import com.intellij.openapi.util.Computable import com.intellij.openapi.util.Computable
import com.intellij.openapi.util.io.FileUtil import com.intellij.openapi.util.io.FileUtil
import com.intellij.psi.PsiClass import com.intellij.psi.*
import com.intellij.psi.PsiElement import com.intellij.refactoring.util.CommonRefactoringUtil.RefactoringErrorHintException
import com.intellij.psi.PsiFile import com.intellij.rt.execution.junit.ComparisonDetailsExtractor
import com.intellij.psi.PsiMethod import com.intellij.testFramework.LightProjectDescriptor
import com.intellij.testFramework.MapDataContext import com.intellij.testFramework.MapDataContext
import com.intellij.util.ArrayUtil import com.intellij.util.ArrayUtil
import org.jetbrains.annotations.NotNull import junit.framework.ComparisonFailure
import junit.framework.TestCase
import org.jetbrains.kotlin.idea.KotlinHierarchyViewTestBase
import org.jetbrains.kotlin.idea.hierarchy.calls.KotlinCalleeTreeStructure
import org.jetbrains.kotlin.idea.hierarchy.calls.KotlinCallerTreeStructure
import org.jetbrains.kotlin.idea.hierarchy.overrides.KotlinOverrideTreeStructure
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
import org.jetbrains.kotlin.psi.KtCallableDeclaration import org.jetbrains.kotlin.psi.KtCallableDeclaration
import org.jetbrains.kotlin.psi.KtElement import org.jetbrains.kotlin.psi.KtElement
@@ -31,207 +40,179 @@ Test Hierarchy view
Format: test build hierarchy for element at caret, file with caret should be the first in the sorted list of files. Format: test build hierarchy for element at caret, file with caret should be the first in the sorted list of files.
Test accept more than one file, file extension should be .java or .kt Test accept more than one file, file extension should be .java or .kt
*/ */
public abstract class AbstractHierarchyTest extends KotlinHierarchyViewTestBase { abstract class AbstractHierarchyTest : KotlinHierarchyViewTestBase() {
protected var folderName: String? = null
protected String folderName; @Throws(Exception::class)
protected fun doTypeClassHierarchyTest(folderName: String) {
protected void doTypeClassHierarchyTest(@NotNull String folderName) throws Exception { this.folderName = folderName
this.folderName = folderName; doHierarchyTest(typeHierarchyStructure, *filesToConfigure)
doHierarchyTest(getTypeHierarchyStructure(), getFilesToConfigure());
} }
protected void doSuperClassHierarchyTest(@NotNull String folderName) throws Exception { @Throws(Exception::class)
this.folderName = folderName; protected fun doSuperClassHierarchyTest(folderName: String) {
doHierarchyTest(getSuperTypesHierarchyStructure(), getFilesToConfigure()); this.folderName = folderName
doHierarchyTest(superTypesHierarchyStructure, *filesToConfigure)
} }
protected void doSubClassHierarchyTest(@NotNull String folderName) throws Exception { @Throws(Exception::class)
this.folderName = folderName; protected fun doSubClassHierarchyTest(folderName: String) {
doHierarchyTest(getSubTypesHierarchyStructure(), getFilesToConfigure()); this.folderName = folderName
doHierarchyTest(subTypesHierarchyStructure, *filesToConfigure)
} }
protected void doCallerHierarchyTest(@NotNull String folderName) throws Exception { @Throws(Exception::class)
this.folderName = folderName; protected fun doCallerHierarchyTest(folderName: String) {
doHierarchyTest(getCallerHierarchyStructure(), getFilesToConfigure()); this.folderName = folderName
doHierarchyTest(callerHierarchyStructure, *filesToConfigure)
} }
protected void doCallerJavaHierarchyTest(@NotNull String folderName) throws Exception { @Throws(Exception::class)
this.folderName = folderName; protected fun doCallerJavaHierarchyTest(folderName: String) {
doHierarchyTest(getCallerJavaHierarchyStructure(), getFilesToConfigure()); this.folderName = folderName
doHierarchyTest(callerJavaHierarchyStructure, *filesToConfigure)
} }
protected void doCalleeHierarchyTest(@NotNull String folderName) throws Exception { @Throws(Exception::class)
this.folderName = folderName; protected fun doCalleeHierarchyTest(folderName: String) {
doHierarchyTest(getCalleeHierarchyStructure(), getFilesToConfigure()); this.folderName = folderName
doHierarchyTest(calleeHierarchyStructure, *filesToConfigure)
} }
protected void doOverrideHierarchyTest(@NotNull String folderName) throws Exception { @Throws(Exception::class)
this.folderName = folderName; protected fun doOverrideHierarchyTest(folderName: String) {
doHierarchyTest(getOverrideHierarchyStructure(), getFilesToConfigure()); this.folderName = folderName
doHierarchyTest(overrideHierarchyStructure, *filesToConfigure)
} }
private Computable<HierarchyTreeStructure> getSuperTypesHierarchyStructure() { private val superTypesHierarchyStructure: Computable<HierarchyTreeStructure>
return new Computable<HierarchyTreeStructure>() { private get() = Computable {
@Override SupertypesHierarchyTreeStructure(
public HierarchyTreeStructure compute() { project,
return new SupertypesHierarchyTreeStructure( getElementAtCaret(LanguageTypeHierarchy.INSTANCE) as PsiClass
getProject(), )
(PsiClass) getElementAtCaret(LanguageTypeHierarchy.INSTANCE) }
);
} private val subTypesHierarchyStructure: Computable<HierarchyTreeStructure>
}; private get() = Computable {
SubtypesHierarchyTreeStructure(
project,
getElementAtCaret(LanguageTypeHierarchy.INSTANCE) as PsiClass,
HierarchyBrowserBaseEx.SCOPE_PROJECT
)
}
private val typeHierarchyStructure: Computable<HierarchyTreeStructure>
private get() = Computable {
TypeHierarchyTreeStructure(
project,
getElementAtCaret(LanguageTypeHierarchy.INSTANCE) as PsiClass,
HierarchyBrowserBaseEx.SCOPE_PROJECT
)
}
private val callerHierarchyStructure: Computable<HierarchyTreeStructure>
private get() = Computable {
KotlinCallerTreeStructure(
(getElementAtCaret(LanguageCallHierarchy.INSTANCE) as KtElement),
HierarchyBrowserBaseEx.SCOPE_PROJECT
)
}
private val callerJavaHierarchyStructure: Computable<HierarchyTreeStructure>
private get() = Computable {
CallerMethodsTreeStructure(
project,
(getElementAtCaret(LanguageCallHierarchy.INSTANCE) as PsiMember),
HierarchyBrowserBaseEx.SCOPE_PROJECT
)
}
private val calleeHierarchyStructure: Computable<HierarchyTreeStructure>
private get() = Computable {
KotlinCalleeTreeStructure(
(getElementAtCaret(LanguageCallHierarchy.INSTANCE) as KtElement),
HierarchyBrowserBaseEx.SCOPE_PROJECT
)
}
private val overrideHierarchyStructure: Computable<HierarchyTreeStructure>
private get() = Computable {
KotlinOverrideTreeStructure(
project,
(getElementAtCaret(LanguageMethodHierarchy.INSTANCE) as KtCallableDeclaration)
)
}
private fun getElementAtCaret(extension: LanguageExtension<HierarchyProvider>): PsiElement {
val file =
PsiDocumentManager.getInstance(project).getPsiFile(editor.document)
val provider =
BrowseHierarchyActionBase.findProvider(extension, file, file, dataContext)
return provider?.getTarget(dataContext)
?: throw RefactoringErrorHintException("Cannot apply action for element at caret")
} }
private Computable<HierarchyTreeStructure> getSubTypesHierarchyStructure() { private val dataContext: DataContext
return new Computable<HierarchyTreeStructure>() { private get() {
@Override val editor = editor
public HierarchyTreeStructure compute() { val context = MapDataContext()
return new SubtypesHierarchyTreeStructure( context.put(CommonDataKeys.PROJECT, project)
getProject(), context.put(CommonDataKeys.EDITOR, editor)
(PsiClass) getElementAtCaret(LanguageTypeHierarchy.INSTANCE), val targetElement = TextEditorPsiDataProvider().getData(
HierarchyBrowserBaseEx.SCOPE_PROJECT CommonDataKeys.PSI_ELEMENT.name,
);
}
};
}
private Computable<HierarchyTreeStructure> getTypeHierarchyStructure() {
return new Computable<HierarchyTreeStructure>() {
@Override
public HierarchyTreeStructure compute() {
return new TypeHierarchyTreeStructure(
getProject(),
(PsiClass) getElementAtCaret(LanguageTypeHierarchy.INSTANCE),
HierarchyBrowserBaseEx.SCOPE_PROJECT
);
}
};
}
private Computable<HierarchyTreeStructure> getCallerHierarchyStructure() {
return new Computable<HierarchyTreeStructure>() {
@Override
public HierarchyTreeStructure compute() {
return new KotlinCallerTreeStructure(
(KtElement) getElementAtCaret(LanguageCallHierarchy.INSTANCE),
HierarchyBrowserBaseEx.SCOPE_PROJECT
);
}
};
}
private Computable<HierarchyTreeStructure> getCallerJavaHierarchyStructure() {
return new Computable<HierarchyTreeStructure>() {
@Override
public HierarchyTreeStructure compute() {
return new CallerMethodsTreeStructure(
getProject(),
(PsiMember) getElementAtCaret(LanguageCallHierarchy.INSTANCE),
HierarchyBrowserBaseEx.SCOPE_PROJECT
);
}
};
}
private Computable<HierarchyTreeStructure> getCalleeHierarchyStructure() {
return new Computable<HierarchyTreeStructure>() {
@Override
public HierarchyTreeStructure compute() {
return new KotlinCalleeTreeStructure(
(KtElement) getElementAtCaret(LanguageCallHierarchy.INSTANCE),
HierarchyBrowserBaseEx.SCOPE_PROJECT
);
}
};
}
private Computable<HierarchyTreeStructure> getOverrideHierarchyStructure() {
return new Computable<HierarchyTreeStructure>() {
@Override
public HierarchyTreeStructure compute() {
return new KotlinOverrideTreeStructure(
getProject(),
(KtCallableDeclaration) getElementAtCaret(LanguageMethodHierarchy.INSTANCE)
);
}
};
}
private PsiElement getElementAtCaret(LanguageExtension<HierarchyProvider> extension) {
PsiFile file = PsiDocumentManager.getInstance(getProject()).getPsiFile(getEditor().getDocument());
HierarchyProvider provider = BrowseHierarchyActionBase.findProvider(extension, file, file, getDataContext());
PsiElement target = provider != null ? provider.getTarget(getDataContext()) : null;
if (target == null) throw new RefactoringErrorHintException("Cannot apply action for element at caret");
return target;
}
private DataContext getDataContext() {
Editor editor = getEditor();
MapDataContext context = new MapDataContext();
context.put(CommonDataKeys.PROJECT, getProject());
context.put(CommonDataKeys.EDITOR, editor);
PsiElement targetElement = (PsiElement) new TextEditorPsiDataProvider().getData(
CommonDataKeys.PSI_ELEMENT.getName(),
editor, editor,
editor.getCaretModel().getCurrentCaret() editor.caretModel.currentCaret
); ) as PsiElement?
context.put(CommonDataKeys.PSI_ELEMENT, targetElement); context.put(CommonDataKeys.PSI_ELEMENT, targetElement)
return context; return context
} }
protected String[] getFilesToConfigure() { protected val filesToConfigure: Array<String>
final List<String> files = new ArrayList<String>(2); protected get() {
FileUtil.processFilesRecursively(new File(folderName), new Processor<File>() { val files: MutableList<String> = ArrayList(2)
@Override FileUtil.processFilesRecursively(
public boolean process(File file) { File(folderName)
String fileName = file.getName(); ) { file ->
val fileName = file.name
if (fileName.endsWith(".kt") || fileName.endsWith(".java")) { if (fileName.endsWith(".kt") || fileName.endsWith(".java")) {
files.add(fileName); files.add(fileName)
} }
return true; true
} }
}); Collections.sort(files)
Collections.sort(files); return ArrayUtil.toStringArray(files)
return ArrayUtil.toStringArray(files); }
}
@Override @Throws(Exception::class)
protected void doHierarchyTest( override fun doHierarchyTest(
@NotNull Computable<? extends HierarchyTreeStructure> treeStructureComputable, @NotNull String... fileNames treeStructureComputable: Computable<out HierarchyTreeStructure>,
) throws Exception { vararg fileNames: String
) {
try { try {
super.doHierarchyTest(treeStructureComputable, fileNames); super.doHierarchyTest(treeStructureComputable, *fileNames)
} } catch (e: RefactoringErrorHintException) {
catch (RefactoringErrorHintException e) { val file = File(folderName, "messages.txt")
File file = new File(folderName, "messages.txt");
if (file.exists()) { if (file.exists()) {
String expectedMessage = FileUtil.loadFile(file, true); val expectedMessage = FileUtil.loadFile(file, true)
assertEquals(expectedMessage, e.getLocalizedMessage()); TestCase.assertEquals(expectedMessage, e.localizedMessage)
} else {
TestCase.fail("Unexpected error: " + e.localizedMessage)
} }
else { } catch (failure: ComparisonFailure) {
fail("Unexpected error: " + e.getLocalizedMessage()); val actual = ComparisonDetailsExtractor.getActual(failure)
} val verificationFilePath = testDataPath + "/" + getTestName(false) + "_verification.xml"
} KotlinTestUtils.assertEqualsToFile(File(verificationFilePath), actual)
catch (ComparisonFailure failure) {
String actual = ComparisonDetailsExtractor.getActual(failure);
String verificationFilePath =
getTestDataPath() + "/" + getTestName(false) + "_verification.xml";
KotlinTestUtils.assertEqualsToFile(new File(verificationFilePath), actual);
} }
} }
@Override override fun getProjectDescriptor(): LightProjectDescriptor {
@NotNull return KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE
protected LightProjectDescriptor getProjectDescriptor() {
return KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE;
} }
@NotNull override fun getTestDataPath(): String {
@Override val testRoot = super.getTestDataPath()
protected String getTestDataPath() { val testDir = KotlinTestUtils.getTestDataFileName(this.javaClass, name)
String testRoot = super.getTestDataPath(); return "$testRoot/$testDir"
String testDir = KotlinTestUtils.getTestDataFileName(this.getClass(), getName());
return testRoot + "/" + testDir;
} }
} }