diff --git a/xml/tests/src/com/intellij/codeInsight/XmlEventsTest.java b/xml/tests/src/com/intellij/codeInsight/XmlEventsTest.java index 049cc14ed864..0f6cc9882f7a 100644 --- a/xml/tests/src/com/intellij/codeInsight/XmlEventsTest.java +++ b/xml/tests/src/com/intellij/codeInsight/XmlEventsTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-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. @@ -21,8 +21,7 @@ import com.intellij.openapi.command.CommandProcessor; import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.ex.DocumentEx; -import com.intellij.openapi.util.io.FileUtil; -import com.intellij.openapi.util.text.StringUtil; +import com.intellij.openapi.util.TextRange; import com.intellij.pom.PomManager; import com.intellij.pom.PomModel; import com.intellij.pom.PomModelAspect; @@ -34,12 +33,8 @@ import com.intellij.psi.*; import com.intellij.psi.impl.source.PsiFileImpl; import com.intellij.psi.xml.*; import com.intellij.testFramework.LightCodeInsightTestCase; -import com.intellij.testFramework.PlatformTestUtil; import org.jetbrains.annotations.NotNull; -import java.io.File; -import java.io.FileNotFoundException; - public class XmlEventsTest extends LightCodeInsightTestCase { public void test1() throws Exception{ final Listener listener = addPomListener(); @@ -51,7 +46,7 @@ public class XmlEventsTest extends LightCodeInsightTestCase { } }); - assertFileTextEquals(getTestName(false) + ".txt", listener.getEventString()); + assertEquals("(Attribute \"a\" for tag \"a\" set to \"b\")\n", listener.getEventString()); } private Listener addPomListener() { @@ -73,7 +68,7 @@ public class XmlEventsTest extends LightCodeInsightTestCase { } }); - assertFileTextEquals(getTestName(false) + ".txt", listener.getEventString()); + assertEquals("(text changed to 'aa' was: 'aaa'), (child added to a child: XmlText), (child added to a child: XmlTag:a)\n", listener.getEventString()); } public void test3() throws Exception{ @@ -87,7 +82,39 @@ public class XmlEventsTest extends LightCodeInsightTestCase { } }); - assertFileTextEquals(getTestName(false) + ".txt", listener.getEventString()); + assertEquals("(text changed to 'aabba' was: 'aaa')\n", listener.getEventString()); + } + + public void testTagDelete() throws Exception{ + final Listener listener = addPomListener(); + configureFromFileText("x.xml", "aaa\nxxx\nyyy"); + final XmlTag x = ((XmlFile)getFile()).getRootTag().findSubTags("x")[0]; + WriteCommandAction.runWriteCommandAction(null, new Runnable() { + @Override + public void run() { + TextRange range = x.getTextRange(); + getEditor().getDocument().deleteString(range.getStartOffset(), range.getEndOffset() + 1); // plus \n + PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); + } + }); + + assertEquals("(child removed from a child: XmlTag:x), (child removed from a child: XmlText)\n", listener.getEventString()); + } + + public void testTagInsert() throws Exception{ + final Listener listener = addPomListener(); + configureFromFileText("x.xml", "aaa\nxxx\nyyy"); + final XmlTag x = ((XmlFile)getFile()).getRootTag().findSubTags("x")[0]; + WriteCommandAction.runWriteCommandAction(null, new Runnable() { + @Override + public void run() { + TextRange range = x.getTextRange(); + getEditor().getDocument().insertString(range.getEndOffset() + 1, "zxzz\n"); // plus \n + PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); + } + }); + + assertEquals("(child added to a child: XmlText), (child added to a child: XmlTag:z)\n", listener.getEventString()); } public void test4() throws Exception{ @@ -100,20 +127,20 @@ public class XmlEventsTest extends LightCodeInsightTestCase { } }); - assertFileTextEquals(getTestName(false) + ".txt", listener.getEventString()); + assertEquals("(text changed to 'a a ' was: 'a ')\n", listener.getEventString()); } public void test5() throws Exception{ final Listener listener = addPomListener(); final XmlTag tagFromText = XmlElementFactory.getInstance(getProject()).createTagFromText("aaa"); - WriteCommandAction.runWriteCommandAction(null, new Runnable(){ + WriteCommandAction.runWriteCommandAction(null, new Runnable() { @Override public void run() { tagFromText.delete(); } }); - assertFileTextEquals(getTestName(false) + ".txt", listener.getEventString()); + assertEquals("(Xml document changed)\n", listener.getEventString()); } public void testBulkUpdate() throws Exception{ @@ -121,7 +148,7 @@ public class XmlEventsTest extends LightCodeInsightTestCase { final PsiFile file = createFile("a.xml", ""); new WriteCommandAction(getProject()) { @Override - protected void run(Result result) throws Throwable { + protected void run(@NotNull Result result) throws Throwable { final Document document = PsiDocumentManager.getInstance(getProject()).getDocument(file); ((DocumentEx)document).setInBulkUpdate(true); document.insertString(0, " "); @@ -129,7 +156,7 @@ public class XmlEventsTest extends LightCodeInsightTestCase { ((DocumentEx)document).setInBulkUpdate(false); } }.execute(); - assertFileTextEquals(getTestName(false) + ".txt", listener.getEventString()); + assertEquals("(Xml document changed)\n", listener.getEventString()); } public void testDocumentChange1() throws Exception{ @@ -137,7 +164,7 @@ public class XmlEventsTest extends LightCodeInsightTestCase { final String stringToInsert = "b=\"c\""; final int positionToInsert = 2; - checkEventsByDocumentChange(rootTagText, positionToInsert, stringToInsert); + checkEventsByDocumentChange(rootTagText, positionToInsert, stringToInsert, "(Xml document changed)\n"); } public void testDocumentChange2() throws Exception{ @@ -145,7 +172,7 @@ public class XmlEventsTest extends LightCodeInsightTestCase { final String stringToInsert = "b=\"c\""; final int positionToInsert = 3; - checkEventsByDocumentChange(rootTagText, positionToInsert, stringToInsert); + checkEventsByDocumentChange(rootTagText, positionToInsert, stringToInsert, "(Xml document changed)\n"); } @@ -154,7 +181,7 @@ public class XmlEventsTest extends LightCodeInsightTestCase { final String stringToInsert = "b=\"c\""; final int positionToInsert = 6; - checkEventsByDocumentChange(rootTagText, positionToInsert, stringToInsert); + checkEventsByDocumentChange(rootTagText, positionToInsert, stringToInsert, "(child changed in b child: XmlTag:a)\n"); } public void testAttributeValueReplace() throws Exception { @@ -176,7 +203,7 @@ public class XmlEventsTest extends LightCodeInsightTestCase { assertEquals("(Attribute \"name\" for tag \"target\" set to \"\"new\"\")\n", listener.getEventString()); } - private void checkEventsByDocumentChange(final String rootTagText, final int positionToInsert, final String stringToInsert) + private void checkEventsByDocumentChange(final String rootTagText, final int positionToInsert, final String stringToInsert, String events) throws Exception { final Listener listener = addPomListener(); final XmlTag tagFromText = ((XmlFile)createFile("file.xml", rootTagText)).getDocument().getRootTag(); @@ -191,14 +218,14 @@ public class XmlEventsTest extends LightCodeInsightTestCase { } }); - assertFileTextEquals(getTestName(false) + ".txt", listener.getEventString()); + assertEquals(events, listener.getEventString()); } private static class Listener implements PomModelListener{ private final XmlAspect myAspect; private final StringBuffer myBuffer = new StringBuffer(); - public Listener(XmlAspect modelAspect) { + private Listener(XmlAspect modelAspect) { myAspect = modelAspect; } @@ -215,30 +242,11 @@ public class XmlEventsTest extends LightCodeInsightTestCase { return aspect == myAspect; } - String getEventString(){ + private String getEventString(){ return myBuffer.toString(); } } - private static void assertFileTextEquals(String targetDataName, String treeText) throws Exception { - String fullName = PlatformTestUtil.getCommunityPath().replace(File.separatorChar, '/')+ "/xml/tests/testData/psi/events" + File.separatorChar + targetDataName; - try{ - String expectedText = loadFile(fullName); - assertEquals(expectedText.trim(), treeText.trim()); - } - catch(FileNotFoundException e){ - //FileUtil.writeToFile(new File(fullName), StringUtil.convertLineSeparators(treeText).getBytes("UTF-8")); - fail("No output file found. Created file "+fullName); - } - } - - - protected static String loadFile(String fullName) throws Exception { - String text = FileUtil.loadFile(new File(fullName)).trim(); - text = StringUtil.convertLineSeparators(text); - return text; - } - public void testDocumentChange() throws Exception { final String xml = "" + "\n" + diff --git a/xml/tests/testData/psi/events/1.txt b/xml/tests/testData/psi/events/1.txt deleted file mode 100644 index b02964d479b1..000000000000 --- a/xml/tests/testData/psi/events/1.txt +++ /dev/null @@ -1 +0,0 @@ -(Attribute "a" for tag "a" set to "b") diff --git a/xml/tests/testData/psi/events/2.txt b/xml/tests/testData/psi/events/2.txt deleted file mode 100644 index 82453a841ec0..000000000000 --- a/xml/tests/testData/psi/events/2.txt +++ /dev/null @@ -1 +0,0 @@ -(text changed to 'aa' was: 'aaa'), (child added to a child: XmlText), (child added to a child: XmlTag:a) \ No newline at end of file diff --git a/xml/tests/testData/psi/events/3.txt b/xml/tests/testData/psi/events/3.txt deleted file mode 100644 index 954dbafd7fae..000000000000 --- a/xml/tests/testData/psi/events/3.txt +++ /dev/null @@ -1 +0,0 @@ -(text changed to 'aabba' was: 'aaa') diff --git a/xml/tests/testData/psi/events/4.txt b/xml/tests/testData/psi/events/4.txt deleted file mode 100644 index d0470d3dec94..000000000000 --- a/xml/tests/testData/psi/events/4.txt +++ /dev/null @@ -1 +0,0 @@ -(text changed to 'a a ' was: 'a ') diff --git a/xml/tests/testData/psi/events/5.txt b/xml/tests/testData/psi/events/5.txt deleted file mode 100644 index ad835b557ef4..000000000000 --- a/xml/tests/testData/psi/events/5.txt +++ /dev/null @@ -1 +0,0 @@ -(Xml document changed) diff --git a/xml/tests/testData/psi/events/BulkUpdate.txt b/xml/tests/testData/psi/events/BulkUpdate.txt deleted file mode 100644 index ad835b557ef4..000000000000 --- a/xml/tests/testData/psi/events/BulkUpdate.txt +++ /dev/null @@ -1 +0,0 @@ -(Xml document changed) diff --git a/xml/tests/testData/psi/events/DocumentChange1.txt b/xml/tests/testData/psi/events/DocumentChange1.txt deleted file mode 100644 index ad835b557ef4..000000000000 --- a/xml/tests/testData/psi/events/DocumentChange1.txt +++ /dev/null @@ -1 +0,0 @@ -(Xml document changed) diff --git a/xml/tests/testData/psi/events/DocumentChange2.txt b/xml/tests/testData/psi/events/DocumentChange2.txt deleted file mode 100644 index ad835b557ef4..000000000000 --- a/xml/tests/testData/psi/events/DocumentChange2.txt +++ /dev/null @@ -1 +0,0 @@ -(Xml document changed) diff --git a/xml/tests/testData/psi/events/DocumentChange3.txt b/xml/tests/testData/psi/events/DocumentChange3.txt deleted file mode 100644 index f572e6333826..000000000000 --- a/xml/tests/testData/psi/events/DocumentChange3.txt +++ /dev/null @@ -1 +0,0 @@ -(child changed in b child: XmlTag:a) diff --git a/xml/tests/testData/psi/events/TagRemove.txt b/xml/tests/testData/psi/events/TagRemove.txt deleted file mode 100644 index 9c546956e886..000000000000 --- a/xml/tests/testData/psi/events/TagRemove.txt +++ /dev/null @@ -1 +0,0 @@ -(child changed in html child: XmlText), (child removed from html child: XmlTag:b), (child removed from html child: XmlText)