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 CDataTest extends TestCase { 22 23 public CDataTest(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 = getCDataNode(); 31 } 32 33 public static Test suite() { 34 TestSuite suite = new TestSuite(CDataTest.class); 35 36 return suite; 37 } 38 39 42 public void testGetNodeValue() { 43 String expResult = " function match(a,b) if (a > 0 && b < 7) <a/> "; 44 String result = text.getNodeValue(); 45 assertEquals(expResult, result); 46 } 47 48 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 60 public void testGetNodeName() { 61 String expResult = "#cdata-section"; 62 String result = text.getNodeName(); 63 assertEquals("getNodeName must return #cdata-section",expResult, result); 64 } 65 66 69 public void testGetNamespaceURI() { 70 String result = text.getNamespaceURI(); 71 assertNull(result); 72 } 73 74 public void testMultiLineCData() { 75 CData c = getMultiLineCData(); 76 final String 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 tValue = "<xslice>embedded values </xslice>"; 86 try { 87 text.setData(tValue); 88 fail("node not cloned"); 89 } catch (Exception 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 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 multiLineComment = root.getElementsByTagName("multi-line-cdata").item(0); 109 NodeList nl = multiLineComment.getChildNodes(); 110 CData cdata = null; 111 for (int i = 0; i < nl.getLength(); i++) { 112 org.w3c.dom.Node 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 nl = script.getChildNodes(); 125 CData cdataSection = null; 126 for (int i = 0; i < nl.getLength(); i++) { 127 org.w3c.dom.Node 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 baseDocument; 139 } 140 | Popular Tags |