1 7 8 package org.netbeans.modules.xml.xdm.visitor; 9 10 import junit.framework.Test; 11 import junit.framework.TestCase; 12 import junit.framework.TestSuite; 13 import org.netbeans.modules.xml.xdm.Util; 14 import org.netbeans.modules.xml.xdm.XDMModel; 15 import org.netbeans.modules.xml.xdm.nodes.Document; 16 import org.netbeans.modules.xml.xdm.nodes.Element; 17 import org.netbeans.modules.xml.xdm.nodes.Node; 18 import org.netbeans.modules.xml.xdm.nodes.Text; 19 import org.w3c.dom.NodeList ; 20 21 25 public class NodeByPositionVisitorTest extends TestCase{ 26 27 28 public NodeByPositionVisitorTest() { 29 } 30 31 public static Test suite() { 32 TestSuite suite = new TestSuite(NodeByPositionVisitorTest.class); 33 34 return suite; 35 } 36 37 public void testFindPosition(){ 38 FindVisitor instance = new FindVisitor(); 39 40 Document root = xmlModel.getDocument(); 41 42 NodeByPositionVisitor pfVisitor = new NodeByPositionVisitor(root); 43 44 Element company = (Element)root.getChildNodes().item(0); 46 Node result = instance.find(root, company.getId()); 47 assertEquals(company, result); 48 49 Node findCompany = pfVisitor.getContainingElement(83); 51 this.assertEquals("Found company by position",company, findCompany); 52 53 Element firstEmployee = (Element) company.getChildNodes().item(1); 55 Node empNameText = pfVisitor.getContainingNode(213); this.assertEquals("Found first employee by position", 57 firstEmployee.getChildNodes().item(0), 58 empNameText); 59 Node findFirstEmployee = pfVisitor.getContainingElement(213); this.assertEquals("Found first employee by position",firstEmployee, 62 findFirstEmployee); 63 64 Node phoneNumber = pfVisitor.getContainingNode(193); 66 this.assertEquals("Found first employee by position", 67 firstEmployee.getAttributes().item(3), 68 phoneNumber); 69 Node findFirstEmployeeAgain = pfVisitor.getContainingElement(193); 71 this.assertEquals("Found first employee by another position again", 72 findFirstEmployee, findFirstEmployeeAgain); 73 74 Element secondEmployee = (Element) company.getChildNodes().item(5); 76 empNameText = pfVisitor.getContainingNode(326); this.assertEquals("Found first employee by position", 78 secondEmployee.getChildNodes().item(0), 79 empNameText); 80 Node findSecondEmployee = pfVisitor.getContainingElement(326); 82 this.assertEquals("Found second employee by position", 83 secondEmployee, findSecondEmployee); 84 85 Node findSecondEmployeeAgain = pfVisitor.getContainingElement(337); 87 this.assertEquals("Found second employee by another position again", 88 findSecondEmployee, findSecondEmployeeAgain); 89 } 90 91 protected void setUp() throws Exception { 92 xmlModel = Util.loadXDMModel("visitor/testPosition.xml"); 93 xmlModel.sync(); 94 } 95 96 private void dumpTextNodes(Node node){ 97 NodeList nodes = node.getChildNodes(); 98 System.out.println("number of children: " + nodes.getLength()); 99 int counter = 0; 100 for(int i = 0; i < nodes.getLength(); i++){ 101 Node n = (Node)nodes.item(i); 102 System.out.println("child " + ++counter + ": "+ n.getClass().getName()); 103 if(n instanceof Text){ 104 Text t = ((Text)n); 105 System.out.println("text length: " + t.getText().length()); 106 System.out.println("text:" + t.getText() +"++"); 107 108 } 109 } 110 } 111 112 private XDMModel xmlModel; 113 114 } 115 | Popular Tags |