KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * PositionFinderVisitorTest.java
3  *
4  * Created on October 26, 2005, 8:14 PM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package org.netbeans.modules.xml.xdm.visitor;
11
12 import java.io.BufferedReader JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.InputStreamReader JavaDoc;
15 import junit.framework.Test;
16 import junit.framework.TestCase;
17 import junit.framework.TestSuite;
18 import org.netbeans.modules.xml.xdm.Util;
19 import org.netbeans.modules.xml.xdm.XDMModel;
20 import org.netbeans.modules.xml.xdm.nodes.Attribute;
21 import org.netbeans.modules.xml.xdm.nodes.Document;
22 import org.netbeans.modules.xml.xdm.nodes.Element;
23 import org.netbeans.modules.xml.xdm.nodes.Node;
24 import org.netbeans.modules.xml.xdm.nodes.Text;
25 import org.w3c.dom.NodeList JavaDoc;
26
27 /**
28  *
29  * @author rico
30  */

31 public class PositionFinderVisitorTest extends TestCase{
32     
33     /** Creates a new instance of PositionFinderVisitorTest */
34     public PositionFinderVisitorTest() {
35     }
36     
37     public static Test suite() {
38         TestSuite suite = new TestSuite(PositionFinderVisitorTest.class);
39         
40         return suite;
41     }
42  
43     public void testFindPosition(){
44         FindVisitor instance = new FindVisitor();
45         
46         Document root = xmlModel.getDocument();
47         
48         PositionFinderVisitor pfVisitor = new PositionFinderVisitor();
49         
50         //element company
51
Element company = (Element)root.getChildNodes().item(0);
52         Node result = instance.find(root, company.getId());
53         assertEquals(company, result);
54         
55         int position = pfVisitor.findPosition(root, company);
56         this.assertEquals("Position of root element",25, position);
57         
58         //newline char
59
Text t = (Text)company.getChildNodes().item(0);
60         position = pfVisitor.findPosition(root, t);
61         this.assertEquals("Position of newline ", 83, position);
62         
63         //employee element
64
Element employee = (Element)company.getChildNodes().item(1);
65         position = pfVisitor.findPosition(root, employee);
66         assertEquals("Position of employee element",89, position);
67         
68         //ssn attribute
69
Attribute attr = (Attribute)employee.getAttributes().item(0);
70         position = pfVisitor.findPosition(root, attr);
71         assertEquals("Position of ssn attribute",99, position);
72         
73         //id attribute
74
attr = (Attribute)employee.getAttributes().item(1);
75         position = pfVisitor.findPosition(root, attr);
76         assertEquals("Position of id attribute",119, position);
77         
78         //address attribute
79
attr = (Attribute)employee.getAttributes().item(2);
80         position = pfVisitor.findPosition(root, attr);
81         assertEquals("Position of address attribute",136, position);
82         
83         //phone attribute
84
attr = (Attribute)employee.getAttributes().item(3);
85         position = pfVisitor.findPosition(root, attr);
86         assertEquals("Position of phone attribute with embedded whitespaces",172, position);
87         
88         //text value child node of employee
89
Text txt = (Text)employee.getChildNodes().item(0);
90         position = pfVisitor.findPosition(root, txt);
91         assertEquals("Position of text child node",195, position);
92         
93         //comment
94
txt = (Text)company.getChildNodes().item(3);
95         position = pfVisitor.findPosition(root, txt);
96         assertEquals("Position of comment (after newline)",238, position);
97         
98         //second employee element
99
Element employee2 = (Element)company.getChildNodes().item(5);
100         position = pfVisitor.findPosition(root, employee2);
101         assertEquals("Position of second employee element",259, position);
102     }
103     
104     public void testFindPosition2() throws IOException JavaDoc, Exception JavaDoc {
105         
106         xmlModel = Util.loadXDMModel("visitor/testPosition.xsd");
107         xmlModel.sync();
108         
109         FindVisitor instance = new FindVisitor();
110         
111         Document root = xmlModel.getDocument();
112         
113         PositionFinderVisitor pfVisitor = new PositionFinderVisitor();
114         
115         //element schema
116
Element schema = (Element)root.getChildNodes().item(2);
117         Node result = instance.find(root, schema.getId());
118         assertEquals(schema, result);
119         
120         int position = pfVisitor.findPosition(root, schema);
121         this.assertEquals("Position of schema element",215, position);
122         
123         //global element
124
Element ge = (Element)schema.getChildNodes().item(4);
125         position = pfVisitor.findPosition(root, ge);
126         assertEquals("Position of employee element",588, position);
127     }
128     
129     public void testFindPosition3() throws IOException JavaDoc, Exception JavaDoc {
130         PositionFinderVisitor pfVisitor = new PositionFinderVisitor();
131         xmlModel = Util.loadXDMModel("visitor/testPosition3.wsdl");
132         Document root = xmlModel.getDocument();
133         Element definitions = (Element) root.getDocumentElement();
134         
135         int position = pfVisitor.findPosition(root, definitions);
136         assertEquals("Position of root element", 83, position);
137         
138         Element types = (Element) definitions.getChildNodes().item(1);
139         assertEquals("types", types.getTagName());
140         position = pfVisitor.findPosition(root, types);
141         assertEquals("Position of types", 464, position);
142         
143         Element schema = (Element) types.getChildNodes().item(1);
144         assertEquals("xsd:schema", schema.getTagName());
145         position = pfVisitor.findPosition(root, schema);
146         assertEquals("Position of types", 480, position);
147         
148     }
149
150     public void testFindPosition4() throws IOException JavaDoc, Exception JavaDoc {
151         PositionFinderVisitor pfVisitor = new PositionFinderVisitor();
152         xmlModel = Util.loadXDMModel("visitor/testPosition4.wsdl");
153         Document root = xmlModel.getDocument();
154         Element definitions = (Element) root.getDocumentElement();
155         Element types = (Element) definitions.getChildNodes().item(1);
156         Element foobar = (Element) types.getChildNodes().item(3);
157         assertEquals("foobar", foobar.getTagName());
158         int position = pfVisitor.findPosition(root, foobar);
159         assertEquals("Position of types", 899, position);
160     }
161     
162     protected void setUp() throws Exception JavaDoc {
163         xmlModel = Util.loadXDMModel("visitor/testPosition.xml");
164         xmlModel.sync();
165     }
166     
167     private void dumpTextNodes(Node node){
168         NodeList JavaDoc nodes = node.getChildNodes();
169         int counter = 0;
170         for(int i = 0; i < nodes.getLength(); i++){
171             Node n = (Node)nodes.item(i);
172             if(n instanceof Text){
173                 Text t = ((Text)n);
174                 
175             }
176         }
177     }
178     
179     private XDMModel xmlModel;
180     
181 }
182
Popular Tags