move some tests to the platform
[idea/community.git] / java / java-tests / testSrc / com / intellij / psi / PsiModificationTrackerTest.java
1 package com.intellij.psi;
2
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;
11
12 import java.io.IOException;
13
14 /**
15  * @author Dmitry Avdeev
16  */
17 public class PsiModificationTrackerTest extends LightPlatformCodeInsightFixtureTestCase {
18   public void testAnnotationNotChanged() throws Exception {
19     doReplaceTest("@SuppressWarnings(\"zz\")\n" +
20                   "public class Foo { <selection></selection>}",
21                   "hi");
22   }
23
24   public void testAnnotationNameChanged() throws Exception {
25     doReplaceTest("@Suppr<selection>ess</selection>Warnings(\"zz\")\n" +
26                   "public class Foo { }",
27                   "hi");
28   }
29
30   public void testAnnotationParameterChanged() throws Exception {
31     doReplaceTest("@SuppressWarnings(\"<selection>zz</selection>\")\n" +
32                   "public class Foo { }",
33                   "hi");
34   }
35
36   public void testAnnotationRemoved() throws Exception {
37     doReplaceTest("<selection>@SuppressWarnings(\"zz\")</selection>\n" +
38                   "public class Foo { }",
39                   "");
40   }
41
42   public void testAnnotationWithClassRemoved() throws Exception {
43     doReplaceTest("<selection>@SuppressWarnings(\"zz\")\n" +
44                   "public </selection> class Foo { }",
45                   "");
46   }
47
48   public void testRemoveAnnotatedMethod() throws Exception {
49     doReplaceTest("public class Foo {\n" +
50                   "  <selection>  " +
51                   "   @SuppressWarnings(\"\")\n" +
52                   "    public void method() {}\n" +
53                   "</selection>" +
54                   "}",
55                   "");
56   }
57
58   public void testRenameAnnotatedMethod() throws Exception {
59     doReplaceTest("public class Foo {\n" +
60                   "   @SuppressWarnings(\"\")\n" +
61                   "    public void me<selection>th</selection>od() {}\n" +
62                   "}",
63                   "zzz");
64   }
65
66   public void testRenameAnnotatedClass() throws Exception {
67     doReplaceTest("   @SuppressWarnings(\"\")\n" +
68                   "public class F<selection>o</selection>o {\n" +
69                   "    public void method() {}\n" +
70                   "}",
71                   "zzz");
72   }
73
74   public void testRemoveAll() throws Exception {
75     doReplaceTest("<selection>@SuppressWarnings(\"zz\")\n" +
76                   "public  class Foo { }</selection>",
77                   "");
78   }
79
80   public void testRemoveFile() throws Exception {
81     doTest("<selection>@SuppressWarnings(\"zz\")\n" +
82            "public  class Foo { }</selection>",
83            new Processor<PsiFile>() {
84              @Override
85              public boolean process(PsiFile psiFile) {
86                try {
87                  final VirtualFile vFile = psiFile.getVirtualFile();
88                  assert vFile != null : psiFile;
89                  vFile.delete(this);
90                }
91                catch (IOException e) {
92                  fail(e.getMessage());
93                }
94                return false;
95              }
96            });
97   }
98
99   private void doReplaceTest(@NonNls String text, @NonNls final String with) {
100     doTest(text, new Processor<PsiFile>() {
101       @Override
102       public boolean process(PsiFile psiFile) {
103         replaceSelection(with);
104         return false;
105       }
106     });
107   }
108
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();
113     run.process(file);
114     assertFalse(modificationTracker.getModificationCount() == count);
115   }
116
117   private void replaceSelection(final String with) {
118     new WriteCommandAction.Simple(getProject()) {
119       @Override
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();
124       }
125     }.execute();
126   }
127
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();
132
133     file.getContainingDirectory().delete();
134
135     assertEquals(count + 1, modificationTracker.getJavaStructureModificationCount());
136   }
137 }