summaryrefslogtreecommitdiff
path: root/xml/tests/src/com/intellij/codeInsight/XmlTagWriteTest.java
blob: 39c77f1ff37b69ab8b8cec67be7f7f952976ef1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright 2000-2014 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 com.intellij.codeInsight;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.psi.XmlElementFactory;
import com.intellij.psi.xml.XmlTag;
import com.intellij.psi.xml.XmlText;
import com.intellij.testFramework.LightCodeInsightTestCase;
import com.intellij.util.IncorrectOperationException;
import com.intellij.xml.util.XmlUtil;

/**
 * @author lesya, dsl
 */
public class XmlTagWriteTest extends LightCodeInsightTestCase{
  public void test1() throws IncorrectOperationException {
    XmlElementFactory elementFactory = XmlElementFactory.getInstance(getProject());
    final XmlTag xmlTag = XmlElementFactory.getInstance(getProject()).createTagFromText("<tag1/>");
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {
      @Override
      public void run() {
        xmlTag.add(xmlTag.createChildTag("tag2", XmlUtil.EMPTY_URI, null, false));
      }
    });


    assertEquals("<tag1><tag2/></tag1>", xmlTag.getText());
    XmlTag createdFromText = elementFactory.createTagFromText(xmlTag.getText());
    assertEquals("tag1", createdFromText.getName());
    assertEquals(1, createdFromText.getSubTags().length);
    assertEquals("tag2", createdFromText.getSubTags()[0].getName());
  }

  public void test2() throws IncorrectOperationException {
    final XmlTag xmlTag = XmlElementFactory.getInstance(getProject()).createTagFromText("<tag1></tag1>");
    WriteCommandAction.runWriteCommandAction(null, new Runnable(){
      @Override
      public void run() {
        xmlTag.add(xmlTag.createChildTag("tag2", XmlUtil.EMPTY_URI, null, false));
      }
    });


    assertEquals("<tag1><tag2/></tag1>", xmlTag.getText());
    XmlTag createdFromText = XmlElementFactory.getInstance(getProject()).createTagFromText(xmlTag.getText());
    assertEquals("tag1", createdFromText.getName());
    assertEquals(1, createdFromText.getSubTags().length);
    assertEquals("tag2", createdFromText.getSubTags()[0].getName());
  }

  public void test3() throws Exception{
    final XmlTag tag = XmlElementFactory.getInstance(getProject()).createTagFromText("<b>\n0123456</b>");
    final XmlText text = (XmlText) tag.getValue().getChildren()[0];
    String textS = text.getText();
    ApplicationManager.getApplication().runWriteAction(new Runnable() {
      @Override
      public void run() {
        text.insertText("lala", 2);
      }
    });

    XmlText text2 = (XmlText)tag.getValue().getChildren()[0];
    assertEquals(textS.substring(0, 2) + "lala" + textS.substring(2), text2.getText());
  }
}