KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xdm > nodes > CDataTest


1 /*
2  * TextTest.java
3  * JUnit based test
4  *
5  * Created on October 21, 2005, 2:21 PM
6  */

7
8 package org.netbeans.modules.xml.xdm.nodes;
9
10 import java.io.IOException JavaDoc;
11 import junit.framework.*;
12 import org.netbeans.modules.xml.xdm.Util;
13 import org.netbeans.modules.xml.xdm.XDMModel;
14 import org.netbeans.modules.xml.xdm.visitor.PrintVisitor;
15 import org.w3c.dom.NodeList JavaDoc;
16
17 /**
18  *
19  * @author ajit
20  */

21 public class CDataTest extends TestCase {
22     
23     public CDataTest(String JavaDoc testName) {
24         super(testName);
25     }
26
27     protected void setUp() throws Exception JavaDoc {
28         baseDocument = Util.getResourceAsDocument("nodes/cdata.xml");
29         xmlModel = Util.loadXDMModel(baseDocument);
30         text = getCDataNode();
31     }
32
33     public static Test suite() {
34         TestSuite suite = new TestSuite(CDataTest.class);
35         
36         return suite;
37     }
38
39     /**
40      * Test of getNodeValue method, of class org.netbeans.modules.xml.xdm.nodes.Text.
41      */

42     public void testGetNodeValue() {
43         String JavaDoc expResult = " function match(a,b) if (a > 0 && b < 7) <a/> ";
44         String JavaDoc result = text.getNodeValue();
45         assertEquals(expResult, result);
46     }
47
48     /**
49      * Test of getNodeType method, of class org.netbeans.modules.xml.xdm.nodes.Text.
50      */

51     public void testGetNodeType() {
52         short expResult = org.w3c.dom.Node.CDATA_SECTION_NODE;
53         short result = text.getNodeType();
54         assertEquals("getNodeType must return CDATA_SECTION_NODE",expResult, result);
55     }
56
57     /**
58      * Test of getNodeName method, of class org.netbeans.modules.xml.xdm.nodes.Text.
59      */

60     public void testGetNodeName() {
61         String JavaDoc expResult = "#cdata-section";
62         String JavaDoc result = text.getNodeName();
63         assertEquals("getNodeName must return #cdata-section",expResult, result);
64     }
65
66     /**
67      * Test of getNamespaceURI method, of class org.netbeans.modules.xml.xdm.nodes.Text.
68      */

69     public void testGetNamespaceURI() {
70         String JavaDoc result = text.getNamespaceURI();
71         assertNull(result);
72     }
73
74     public void testMultiLineCData() {
75     CData c = getMultiLineCData();
76     final String JavaDoc expectedValue = "\n<!--line1-->\n<!--line2-->\n";
77     assertEquals(c.getData(),expectedValue);
78     }
79     
80     public void testGetData() {
81     testGetNodeValue();
82     }
83     
84     public void testSetData() {
85     String JavaDoc tValue = "<xslice>embedded values </xslice>";
86     try {
87         text.setData(tValue);
88         fail("node not cloned");
89     } catch (Exception JavaDoc e) {
90         
91     }
92     CData clone = (CData) text.cloneNode(true);
93     clone.setData(tValue);
94     assertEquals(tValue, clone.getData());
95     xmlModel.modify(text,clone);
96     xmlModel.flush();
97     try {
98         xmlModel.sync();
99     } catch (IOException JavaDoc ex) {
100         fail("sync threw exception");
101     }
102     assertEquals(tValue, getCDataNode().getNodeValue());
103     assertEquals(3,getCDataNode().getTokens().size());
104     }
105     
106      private CData getMultiLineCData() {
107     Element root = (Element) xmlModel.getDocument().getChildNodes().item(0);
108     org.w3c.dom.Node JavaDoc multiLineComment = root.getElementsByTagName("multi-line-cdata").item(0);
109     NodeList JavaDoc nl = multiLineComment.getChildNodes();
110     CData cdata = null;
111     for (int i = 0; i < nl.getLength(); i++) {
112         org.w3c.dom.Node JavaDoc n = nl.item(i);
113         if (n instanceof CData) {
114         cdata = (CData) n;
115         break;
116         }
117     }
118     return cdata;
119     }
120     
121     private CData getCDataNode() {
122     Element root = (Element) xmlModel.getDocument().getChildNodes().item(0);
123     Element script = (Element) root.getElementsByTagName("cdata").item(0);
124     NodeList JavaDoc nl = script.getChildNodes();
125     CData cdataSection = null;
126     for (int i = 0; i < nl.getLength(); i++) {
127         org.w3c.dom.Node JavaDoc n = nl.item(i);
128         if (n instanceof CData) {
129         cdataSection = (CData)n;
130         break;
131         }
132     }
133     return cdataSection;
134     }
135     
136     private XDMModel xmlModel;
137     private CData text;
138     private javax.swing.text.Document JavaDoc baseDocument;
139 }
140
Popular Tags