1 7 8 package org.netbeans.modules.xml.xdm.nodes; 9 10 import java.io.IOException ; 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 ; 16 17 21 public class CommentTest extends TestCase { 22 23 public CommentTest(String testName) { 24 super(testName); 25 } 26 27 protected void setUp() throws Exception { 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 42 public void testGetNodeValue() { 43 String expResult = " I am a comment "; 44 String result = text.getNodeValue(); 45 assertEquals(expResult, result); 46 } 47 48 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 60 public void testGetNodeName() { 61 String expResult = "#comment"; 62 String result = text.getNodeName(); 63 assertEquals("getNodeName must return #comment",expResult, result); 64 } 65 66 69 public void testGetNamespaceURI() { 70 String 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 expectedValue = " line 1\nline 2\nline 3\n"; 82 assertEquals(c.getData(),expectedValue); 83 } 84 85 public void testSetData() { 86 String tValue = "CBW #1"; 87 try { 88 text.setData(tValue); 89 fail("node not cloned"); 90 } catch (Exception 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 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 multiLineComment = root.getElementsByTagName("multi-line-comment").item(0); 112 NodeList nl = multiLineComment.getChildNodes(); 113 Comment comment = null; 114 for (int i = 0; i < nl.getLength(); i++) { 115 org.w3c.dom.Node 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 nl = root.getChildNodes(); 127 Comment comment = null; 128 for (int i = 0; i < nl.getLength(); i++) { 129 org.w3c.dom.Node 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 baseDocument; 141 } 142 | Popular Tags |