KickJava   Java API By Example, From Geeks To Geeks.

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


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 CommentTest extends TestCase {
22     
23     public CommentTest(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 = getCommentNode();
31     }
32
33     public static Test suite() {
34         TestSuite suite = new TestSuite(CommentTest.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 = " I am a comment ";
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.COMMENT_NODE;
53         short result = text.getNodeType();
54         assertEquals("getNodeType must return COMMENT_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 = "#comment";
62         String JavaDoc result = text.getNodeName();
63         assertEquals("getNodeName must return #comment",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     
75     public void testGetData() {
76     testGetNodeValue();
77     }
78     
79     public void testMultiLineComment() {
80     Comment c = getMultiLineComment();
81     final String JavaDoc expectedValue = " line 1\nline 2\nline 3\n";
82     assertEquals(c.getData(),expectedValue);
83     }
84     
85     public void testSetData() {
86     String JavaDoc tValue = "CBW #1";
87     try {
88         text.setData(tValue);
89         fail("node not cloned");
90     } catch (Exception JavaDoc e) {
91         
92     }
93     Comment clone = (Comment) text.cloneNode(true);
94     clone.setData(tValue);
95     assertEquals(3,clone.getTokens().size());
96     assertEquals(tValue, clone.getData());
97     
98     xmlModel.modify(text,clone);
99     xmlModel.flush();
100     try {
101         xmlModel.sync();
102     } catch (IOException JavaDoc ex) {
103         fail("sync failed");
104     }
105     assertEquals(tValue, getCommentNode().getNodeValue());
106     assertEquals(3,getCommentNode().getTokens().size());
107     }
108     
109     private Comment getMultiLineComment() {
110     Element root = (Element) xmlModel.getDocument().getChildNodes().item(0);
111     org.w3c.dom.Node JavaDoc multiLineComment = root.getElementsByTagName("multi-line-comment").item(0);
112     NodeList JavaDoc nl = multiLineComment.getChildNodes();
113     Comment comment = null;
114     for (int i = 0; i < nl.getLength(); i++) {
115         org.w3c.dom.Node JavaDoc n = nl.item(i);
116         if (n instanceof Comment) {
117         comment = (Comment) n;
118         break;
119         }
120     }
121     return comment;
122     }
123     
124     private Comment getCommentNode() {
125     Element root = (Element) xmlModel.getDocument().getChildNodes().item(0);
126     NodeList JavaDoc nl = root.getChildNodes();
127     Comment comment = null;
128     for (int i = 0; i < nl.getLength(); i++) {
129         org.w3c.dom.Node JavaDoc n = nl.item(i);
130         if (n instanceof Comment) {
131         comment = (Comment) n;
132         break;
133         }
134     }
135     return comment;
136     }
137     
138     private XDMModel xmlModel;
139     private Comment text;
140     private javax.swing.text.Document JavaDoc baseDocument;
141 }
142
Popular Tags