1 package com.intellij.psi;
3 import com.intellij.ide.highlighter.JavaFileType;
4 import com.intellij.openapi.command.WriteCommandAction;
5 import com.intellij.openapi.editor.SelectionModel;
6 import com.intellij.openapi.vfs.VirtualFile;
7 import com.intellij.psi.util.PsiModificationTracker;
8 import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase;
9 import com.intellij.util.Processor;
10 import org.jetbrains.annotations.NonNls;
12 import java.io.IOException;
15 * @author Dmitry Avdeev
17 public class PsiModificationTrackerTest extends LightPlatformCodeInsightFixtureTestCase {
18 public void testAnnotationNotChanged() throws Exception {
19 doReplaceTest("@SuppressWarnings(\"zz\")\n" +
20 "public class Foo { <selection></selection>}",
24 public void testAnnotationNameChanged() throws Exception {
25 doReplaceTest("@Suppr<selection>ess</selection>Warnings(\"zz\")\n" +
26 "public class Foo { }",
30 public void testAnnotationParameterChanged() throws Exception {
31 doReplaceTest("@SuppressWarnings(\"<selection>zz</selection>\")\n" +
32 "public class Foo { }",
36 public void testAnnotationRemoved() throws Exception {
37 doReplaceTest("<selection>@SuppressWarnings(\"zz\")</selection>\n" +
38 "public class Foo { }",
42 public void testAnnotationWithClassRemoved() throws Exception {
43 doReplaceTest("<selection>@SuppressWarnings(\"zz\")\n" +
44 "public </selection> class Foo { }",
48 public void testRemoveAnnotatedMethod() throws Exception {
49 doReplaceTest("public class Foo {\n" +
51 " @SuppressWarnings(\"\")\n" +
52 " public void method() {}\n" +
58 public void testRenameAnnotatedMethod() throws Exception {
59 doReplaceTest("public class Foo {\n" +
60 " @SuppressWarnings(\"\")\n" +
61 " public void me<selection>th</selection>od() {}\n" +
66 public void testRenameAnnotatedClass() throws Exception {
67 doReplaceTest(" @SuppressWarnings(\"\")\n" +
68 "public class F<selection>o</selection>o {\n" +
69 " public void method() {}\n" +
74 public void testRemoveAll() throws Exception {
75 doReplaceTest("<selection>@SuppressWarnings(\"zz\")\n" +
76 "public class Foo { }</selection>",
80 public void testRemoveFile() throws Exception {
81 doTest("<selection>@SuppressWarnings(\"zz\")\n" +
82 "public class Foo { }</selection>",
83 new Processor<PsiFile>() {
85 public boolean process(PsiFile psiFile) {
87 final VirtualFile vFile = psiFile.getVirtualFile();
88 assert vFile != null : psiFile;
91 catch (IOException e) {
99 private void doReplaceTest(@NonNls String text, @NonNls final String with) {
100 doTest(text, new Processor<PsiFile>() {
102 public boolean process(PsiFile psiFile) {
103 replaceSelection(with);
109 private void doTest(@NonNls String text, Processor<PsiFile> run) {
110 PsiFile file = myFixture.configureByText(JavaFileType.INSTANCE, text);
111 PsiModificationTracker modificationTracker = PsiManager.getInstance(getProject()).getModificationTracker();
112 long count = modificationTracker.getModificationCount();
114 assertFalse(modificationTracker.getModificationCount() == count);
117 private void replaceSelection(final String with) {
118 new WriteCommandAction.Simple(getProject()) {
120 protected void run() throws Throwable {
121 SelectionModel sel = myFixture.getEditor().getSelectionModel();
122 myFixture.getEditor().getDocument().replaceString(sel.getSelectionStart(), sel.getSelectionEnd(), with);
123 PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
128 public void testJavaStructureModificationChangesAfterPackageDelete() {
129 PsiFile file = myFixture.addFileToProject("/x/y/Z.java", "text");
130 PsiModificationTracker modificationTracker = PsiManager.getInstance(getProject()).getModificationTracker();
131 long count = modificationTracker.getJavaStructureModificationCount();
133 file.getContainingDirectory().delete();
135 assertEquals(count + 1, modificationTracker.getJavaStructureModificationCount());