From 7de6c30506ef8c684ecbc4e87548aa648bb6dafc Mon Sep 17 00:00:00 2001 From: "Natalia.Ukhorskaya" Date: Fri, 5 Oct 2012 11:51:48 +0400 Subject: [PATCH] Add tests for ClassInheritorsSearch and AnnotatedMembersSearch Add test for junit --- .../annotations/testAnnotationsOnClass.kt | 16 ++++ .../annotations/testAnnotationsOnFunction.kt | 30 ++++++ .../testAnnotationsWithParameters.kt | 8 ++ .../testInheritanceFromJavaClass.kt | 4 + .../testInheritanceFromKotlinClass.kt | 6 ++ idea/testData/search/junit/testJunit3.kt | 12 +++ idea/testData/search/junit/testJunit4.kt | 20 ++++ .../jet/search/AbstractSearcherTest.java | 76 +++++++++++++++ .../search/AnnotatedMembersSearchTest.java | 57 +++++++++++ .../jet/search/ClassInheritorsSearchTest.java | 53 +++++++++++ .../jet/search/JUnitMembersSearcherTest.java | 94 +++++++++++++++++++ 11 files changed, 376 insertions(+) create mode 100644 idea/testData/search/annotations/testAnnotationsOnClass.kt create mode 100644 idea/testData/search/annotations/testAnnotationsOnFunction.kt create mode 100644 idea/testData/search/annotations/testAnnotationsWithParameters.kt create mode 100644 idea/testData/search/inheritance/testInheritanceFromJavaClass.kt create mode 100644 idea/testData/search/inheritance/testInheritanceFromKotlinClass.kt create mode 100644 idea/testData/search/junit/testJunit3.kt create mode 100644 idea/testData/search/junit/testJunit4.kt create mode 100644 idea/tests/org/jetbrains/jet/search/AbstractSearcherTest.java create mode 100644 idea/tests/org/jetbrains/jet/search/AnnotatedMembersSearchTest.java create mode 100644 idea/tests/org/jetbrains/jet/search/ClassInheritorsSearchTest.java create mode 100644 idea/tests/org/jetbrains/jet/search/JUnitMembersSearcherTest.java diff --git a/idea/testData/search/annotations/testAnnotationsOnClass.kt b/idea/testData/search/annotations/testAnnotationsOnClass.kt new file mode 100644 index 00000000000..4740f2e7a43 --- /dev/null +++ b/idea/testData/search/annotations/testAnnotationsOnClass.kt @@ -0,0 +1,16 @@ +[Deprecated] class TestClass1 {} + +[java.lang.Deprecated] class TestClass2 {} + +Deprecated class TestClass3 {} + +java.lang.Deprecated class TestClass4 {} + +class TestClass5 { + Deprecated class innerTestClass5() {} +} + +// ANNOTATION: java.lang.Deprecated +// SEARCH: JetLightClass:TestClass1, JetLightClass:TestClass2, JetLightClass:TestClass3 +// SEARCH: JetLightClass:TestClass4 +// SEARCH: JetLightClass:TestClass5.innerTestClass5 \ No newline at end of file diff --git a/idea/testData/search/annotations/testAnnotationsOnFunction.kt b/idea/testData/search/annotations/testAnnotationsOnFunction.kt new file mode 100644 index 00000000000..6f76080360a --- /dev/null +++ b/idea/testData/search/annotations/testAnnotationsOnFunction.kt @@ -0,0 +1,30 @@ +Deprecated fun test1() {} + +java.lang.Deprecated fun test2() {} + +fun test3() { + Deprecated fun test3inner() {} +} + +class Test4() { + Deprecated fun test4() {} +} + +class Test5() { + fun test5() { + Deprecated fun test5inner() {} + } +} + +class Test6() { + class object { + Deprecated fun test6() {} + } +} + +object Test7 { + Deprecated fun test7() {} +} + +// ANNOTATION: java.lang.Deprecated +// SEARCH: PsiMethod:test1, PsiMethod:test2, PsiMethod:test4, PsiMethod:test6, PsiMethod:test7 \ No newline at end of file diff --git a/idea/testData/search/annotations/testAnnotationsWithParameters.kt b/idea/testData/search/annotations/testAnnotationsWithParameters.kt new file mode 100644 index 00000000000..e717bbe982f --- /dev/null +++ b/idea/testData/search/annotations/testAnnotationsWithParameters.kt @@ -0,0 +1,8 @@ +MyAnnotation("f", "s") fun test1() {} + +MyAnnotation("f", "s") class Test1() {} + +annotation class MyAnnotation(val first: String, val second: String) + +// ANNOTATION: MyAnnotation +// SEARCH: PsiMethod:test1, JetLightClass:Test1 \ No newline at end of file diff --git a/idea/testData/search/inheritance/testInheritanceFromJavaClass.kt b/idea/testData/search/inheritance/testInheritanceFromJavaClass.kt new file mode 100644 index 00000000000..cbd2cb6e764 --- /dev/null +++ b/idea/testData/search/inheritance/testInheritanceFromJavaClass.kt @@ -0,0 +1,4 @@ +class A: Object() {} + +// CLASS: java.lang.Object +// SEARCH: JetLightClass:A diff --git a/idea/testData/search/inheritance/testInheritanceFromKotlinClass.kt b/idea/testData/search/inheritance/testInheritanceFromKotlinClass.kt new file mode 100644 index 00000000000..9f9119c54af --- /dev/null +++ b/idea/testData/search/inheritance/testInheritanceFromKotlinClass.kt @@ -0,0 +1,6 @@ +class A: Base() {} + +open class Base() {} + +// CLASS: Base +// SEARCH: JetLightClass:A \ No newline at end of file diff --git a/idea/testData/search/junit/testJunit3.kt b/idea/testData/search/junit/testJunit3.kt new file mode 100644 index 00000000000..fdde65f785b --- /dev/null +++ b/idea/testData/search/junit/testJunit3.kt @@ -0,0 +1,12 @@ +import junit.framework.TestCase + +class Test1: TestCase() { + fun test1() {} +} + +class Test2: junit.framework.TestCase() { + fun test2() {} +} + +// CLASS: junit.framework.TestCase +// SEARCH: JetLightClass:Test1, JetLightClass:Test2 \ No newline at end of file diff --git a/idea/testData/search/junit/testJunit4.kt b/idea/testData/search/junit/testJunit4.kt new file mode 100644 index 00000000000..a949d4d5ddc --- /dev/null +++ b/idea/testData/search/junit/testJunit4.kt @@ -0,0 +1,20 @@ +import org.junit.Test + +class MyTestClass { + [org.junit.Test] fun test1() {} + + org.junit.Test fun test2() {} + + [Test] fun test3() {} + + Test fun test4() {} + + [Deprecated org.junit.Test] fun test5() {} + + [Deprecated Test] fun test6() {} + + fun test7() {} +} + +// ANNOTATION: org.junit.Test +// SEARCH: PsiMethod:test1, PsiMethod:test2, PsiMethod:test3, PsiMethod:test4, PsiMethod:test5, PsiMethod:test6 \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/search/AbstractSearcherTest.java b/idea/tests/org/jetbrains/jet/search/AbstractSearcherTest.java new file mode 100644 index 00000000000..b9403703b7d --- /dev/null +++ b/idea/tests/org/jetbrains/jet/search/AbstractSearcherTest.java @@ -0,0 +1,76 @@ +/* + * Copyright 2010-2012 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.search; + +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.psi.JavaPsiFacade; +import com.intellij.psi.PsiClass; +import com.intellij.psi.search.GlobalSearchScope; +import com.intellij.testFramework.LightProjectDescriptor; +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase; +import com.intellij.util.Query; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.plugin.JetLightProjectDescriptor; +import org.jetbrains.jet.testing.InTextDirectivesUtils; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public abstract class AbstractSearcherTest extends LightCodeInsightFixtureTestCase { + + protected PsiClass getPsiClass(String className) { + PsiClass psiClass = JavaPsiFacade.getInstance(getProject()).findClass(className, getGlobalScope()); + assertNotNull("Couldn't find a psiClass: " + className, psiClass); + return psiClass; + } + + private GlobalSearchScope getGlobalScope() { + return GlobalSearchScope.allScope(getProject()); + } + + protected GlobalSearchScope getProjectScope() { + return GlobalSearchScope.projectScope(getProject()); + } + + protected void checkResult(Query actual) throws IOException { + List expected = InTextDirectivesUtils.findListWithPrefix("// SEARCH: ", FileUtil.loadFile(new File(getPathToFile()))); + List actualModified = new ArrayList(); + for (Object member : actual) { + actualModified.add(member.toString()); + } + Collections.sort(expected); + Collections.sort(actualModified); + assertOrderedEquals(actualModified, expected); + } + + protected String getPathToFile() { + return getTestDataPath() + File.separator + getName() + ".kt"; + } + + protected String getFileName() { + return getName() + ".kt"; + } + + @NotNull + @Override + protected LightProjectDescriptor getProjectDescriptor() { + return JetLightProjectDescriptor.INSTANCE; + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/search/AnnotatedMembersSearchTest.java b/idea/tests/org/jetbrains/jet/search/AnnotatedMembersSearchTest.java new file mode 100644 index 00000000000..9c16360ad04 --- /dev/null +++ b/idea/tests/org/jetbrains/jet/search/AnnotatedMembersSearchTest.java @@ -0,0 +1,57 @@ +/* + * Copyright 2010-2012 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.search; + +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.psi.PsiClass; +import com.intellij.psi.search.searches.AnnotatedMembersSearch; +import org.jetbrains.jet.plugin.PluginTestCaseBase; +import org.jetbrains.jet.testing.InTextDirectivesUtils; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +public class AnnotatedMembersSearchTest extends AbstractSearcherTest { + + public void testAnnotationsOnClass() throws IOException { + doTest(); + } + + public void testAnnotationsOnFunction() throws IOException { + doTest(); + } + + public void testAnnotationsWithParameters() throws IOException { + doTest(); + } + + private void doTest() throws IOException { + myFixture.configureByFile(getFileName()); + List directives = InTextDirectivesUtils.findListWithPrefix("// ANNOTATION: ", FileUtil.loadFile(new File(getPathToFile()))); + assertFalse("Specify ANNOTATION directive in test file", directives.isEmpty()); + String annotationClassName = directives.get(0); + PsiClass psiClass = getPsiClass(annotationClassName); + checkResult(AnnotatedMembersSearch.search(psiClass, getProjectScope())); + } + + @Override + protected String getTestDataPath() { + return new File(PluginTestCaseBase.getTestDataPathBase(), "/search/annotations").getPath() + File.separator; + } + +} diff --git a/idea/tests/org/jetbrains/jet/search/ClassInheritorsSearchTest.java b/idea/tests/org/jetbrains/jet/search/ClassInheritorsSearchTest.java new file mode 100644 index 00000000000..f8f51342f20 --- /dev/null +++ b/idea/tests/org/jetbrains/jet/search/ClassInheritorsSearchTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2010-2012 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.search; + +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.psi.PsiClass; +import com.intellij.psi.search.searches.ClassInheritorsSearch; +import org.jetbrains.jet.plugin.PluginTestCaseBase; +import org.jetbrains.jet.testing.InTextDirectivesUtils; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +public class ClassInheritorsSearchTest extends AbstractSearcherTest { + + public void testInheritanceFromKotlinClass() throws IOException { + doTest(); + } + + public void testInheritanceFromJavaClass() throws IOException { + doTest(); + } + + private void doTest() throws IOException { + myFixture.configureByFile(getFileName()); + List directives = InTextDirectivesUtils.findListWithPrefix("// CLASS: ", FileUtil.loadFile(new File(getPathToFile()))); + assertFalse("Specify CLASS directive in test file", directives.isEmpty()); + String superClassName = directives.get(0); + PsiClass psiClass = getPsiClass(superClassName); + checkResult(ClassInheritorsSearch.search(psiClass, getProjectScope(), false)); + } + + @Override + protected String getTestDataPath() { + return new File(PluginTestCaseBase.getTestDataPathBase(), "/search/inheritance").getPath() + File.separator; + } + +} diff --git a/idea/tests/org/jetbrains/jet/search/JUnitMembersSearcherTest.java b/idea/tests/org/jetbrains/jet/search/JUnitMembersSearcherTest.java new file mode 100644 index 00000000000..5bf8e2e864b --- /dev/null +++ b/idea/tests/org/jetbrains/jet/search/JUnitMembersSearcherTest.java @@ -0,0 +1,94 @@ +/* + * Copyright 2010-2012 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.search; + +import com.intellij.openapi.application.PathManager; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.module.ModuleType; +import com.intellij.openapi.module.StdModuleTypes; +import com.intellij.openapi.projectRoots.Sdk; +import com.intellij.openapi.roots.ContentEntry; +import com.intellij.openapi.roots.ModifiableRootModel; +import com.intellij.openapi.roots.OrderRootType; +import com.intellij.openapi.roots.libraries.Library; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.vfs.VfsUtil; +import com.intellij.psi.PsiClass; +import com.intellij.psi.search.searches.AnnotatedMembersSearch; +import com.intellij.psi.search.searches.ClassInheritorsSearch; +import com.intellij.testFramework.LightProjectDescriptor; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.plugin.PluginTestCaseBase; +import org.jetbrains.jet.testing.InTextDirectivesUtils; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +public class JUnitMembersSearcherTest extends AbstractSearcherTest { + private static final LightProjectDescriptor junitProjectDescriptor = new LightProjectDescriptor() { + @Override + public ModuleType getModuleType() { + return StdModuleTypes.JAVA; + } + + @Override + public Sdk getSdk() { + return PluginTestCaseBase.jdkFromIdeaHome(); + } + + @Override + public void configureModule(Module module, ModifiableRootModel model, ContentEntry contentEntry) { + Library library = model.getModuleLibraryTable().createLibrary("junit"); + Library.ModifiableModel modifiableModel = library.getModifiableModel(); + modifiableModel.addRoot(VfsUtil.getUrlForLibraryRoot( + new File(PathManager.getHomePath().replace(File.separatorChar, '/') + "/lib/junit-4.10.jar")), + OrderRootType.CLASSES); + modifiableModel.commit(); + } + }; + + public void testJunit3() throws IOException { + myFixture.configureByFile(getFileName()); + List directives = InTextDirectivesUtils.findListWithPrefix("// CLASS: ", FileUtil.loadFile(new File(getPathToFile()))); + assertFalse("Specify CLASS directive in test file", directives.isEmpty()); + String superClassName = directives.get(0); + PsiClass psiClass = getPsiClass(superClassName); + checkResult(ClassInheritorsSearch.search(psiClass, getProjectScope(), false)); + } + + public void testJunit4() throws IOException { + myFixture.configureByFile(getFileName()); + List directives = InTextDirectivesUtils.findListWithPrefix("// ANNOTATION: ", FileUtil.loadFile(new File(getPathToFile()))); + assertFalse("Specify ANNOTATION directive in test file", directives.isEmpty()); + String annotationClassName = directives.get(0); + PsiClass psiClass = getPsiClass(annotationClassName); + checkResult(AnnotatedMembersSearch.search(psiClass, getProjectScope())); + } + + @Override + protected String getTestDataPath() { + return new File(PluginTestCaseBase.getTestDataPathBase(), "/search/junit").getPath() + File.separator; + } + + @NotNull + @Override + protected LightProjectDescriptor getProjectDescriptor() { + + return junitProjectDescriptor; + } +}