KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xdm > visitor > NodeByPositionVisitorTest


1 /*
2  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
4  *
5  * $Id: NodeByPositionVisitorTest.java,v 1.3 2006/10/12 14:22:08 abadea Exp $
6  */

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 JavaDoc;
20
21 /**
22  *
23  * @author Ayub Khan
24  */

25 public class NodeByPositionVisitorTest extends TestCase{
26     
27     /** Creates a new instance of NodeByPositionVisitorTest */
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
45
Element company = (Element)root.getChildNodes().item(0);
46         Node result = instance.find(root, company.getId());
47         assertEquals(company, result);
48         
49         //start-tag:25-82, end-tag:513-522
50
Node findCompany = pfVisitor.getContainingElement(83);
51         this.assertEquals("Found company by position",company, findCompany);
52         
53         //start-tag:89-194, end-tag:219-228
54
Element firstEmployee = (Element) company.getChildNodes().item(1);
55         Node empNameText = pfVisitor.getContainingNode(213);//Vidhya Narayanan
56
this.assertEquals("Found first employee by position",
57                 firstEmployee.getChildNodes().item(0),
58                 empNameText);
59         //Demonstrates that we are returning the containing element of text node "Vidhya Narayanan"
60
Node findFirstEmployee = pfVisitor.getContainingElement(213);//Vidhya Narayanan
61
this.assertEquals("Found first employee by position",firstEmployee,
62                 findFirstEmployee);
63         
64         //start-tag:89-194, end-tag:219-228
65
Node phoneNumber = pfVisitor.getContainingNode(193);
66         this.assertEquals("Found first employee by position",
67                 firstEmployee.getAttributes().item(3),
68                 phoneNumber);
69         //Demonstrates that we are returning the containing element of attribute phone
70
Node findFirstEmployeeAgain = pfVisitor.getContainingElement(193);
71         this.assertEquals("Found first employee by another position again",
72                 findFirstEmployee, findFirstEmployeeAgain);
73         
74         //start-tag:259-318, end-tag:327-337
75
Element secondEmployee = (Element) company.getChildNodes().item(5);
76         empNameText = pfVisitor.getContainingNode(326);//A Person
77
this.assertEquals("Found first employee by position",
78                 secondEmployee.getChildNodes().item(0),
79                 empNameText);
80         //Demonstrates that we are returning the containing element of text node "Vidhya Narayanan"
81
Node findSecondEmployee = pfVisitor.getContainingElement(326);
82         this.assertEquals("Found second employee by position",
83                 secondEmployee, findSecondEmployee);
84         
85         //start-tag:259-318, end-tag:327-337
86
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 JavaDoc {
92         xmlModel = Util.loadXDMModel("visitor/testPosition.xml");
93         xmlModel.sync();
94     }
95     
96     private void dumpTextNodes(Node node){
97         NodeList JavaDoc 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