001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * PositionFinderVisitorTest.java
044: *
045: * Created on October 26, 2005, 8:14 PM
046: *
047: * To change this template, choose Tools | Template Manager
048: * and open the template in the editor.
049: */
050:
051: package org.netbeans.modules.xml.xdm.visitor;
052:
053: import java.io.BufferedReader;
054: import java.io.IOException;
055: import java.io.InputStreamReader;
056: import junit.framework.Test;
057: import junit.framework.TestCase;
058: import junit.framework.TestSuite;
059: import org.netbeans.modules.xml.xdm.Util;
060: import org.netbeans.modules.xml.xdm.XDMModel;
061: import org.netbeans.modules.xml.xdm.nodes.Attribute;
062: import org.netbeans.modules.xml.xdm.nodes.Document;
063: import org.netbeans.modules.xml.xdm.nodes.Element;
064: import org.netbeans.modules.xml.xdm.nodes.Node;
065: import org.netbeans.modules.xml.xdm.nodes.Text;
066: import org.w3c.dom.NodeList;
067:
068: /**
069: *
070: * @author rico
071: */
072: public class PositionFinderVisitorTest extends TestCase {
073:
074: /** Creates a new instance of PositionFinderVisitorTest */
075: public PositionFinderVisitorTest() {
076: }
077:
078: public static Test suite() {
079: TestSuite suite = new TestSuite(PositionFinderVisitorTest.class);
080:
081: return suite;
082: }
083:
084: public void testFindPosition() {
085: FindVisitor instance = new FindVisitor();
086:
087: Document root = xmlModel.getDocument();
088:
089: PositionFinderVisitor pfVisitor = new PositionFinderVisitor();
090:
091: //element company
092: Element company = (Element) root.getChildNodes().item(0);
093: Node result = instance.find(root, company.getId());
094: assertEquals(company, result);
095:
096: int position = pfVisitor.findPosition(root, company);
097: this .assertEquals("Position of root element", 25, position);
098:
099: //newline char
100: Text t = (Text) company.getChildNodes().item(0);
101: position = pfVisitor.findPosition(root, t);
102: this .assertEquals("Position of newline ", 83, position);
103:
104: //employee element
105: Element employee = (Element) company.getChildNodes().item(1);
106: position = pfVisitor.findPosition(root, employee);
107: assertEquals("Position of employee element", 89, position);
108:
109: //ssn attribute
110: Attribute attr = (Attribute) employee.getAttributes().item(0);
111: position = pfVisitor.findPosition(root, attr);
112: assertEquals("Position of ssn attribute", 99, position);
113:
114: //id attribute
115: attr = (Attribute) employee.getAttributes().item(1);
116: position = pfVisitor.findPosition(root, attr);
117: assertEquals("Position of id attribute", 119, position);
118:
119: //address attribute
120: attr = (Attribute) employee.getAttributes().item(2);
121: position = pfVisitor.findPosition(root, attr);
122: assertEquals("Position of address attribute", 136, position);
123:
124: //phone attribute
125: attr = (Attribute) employee.getAttributes().item(3);
126: position = pfVisitor.findPosition(root, attr);
127: assertEquals(
128: "Position of phone attribute with embedded whitespaces",
129: 172, position);
130:
131: //text value child node of employee
132: Text txt = (Text) employee.getChildNodes().item(0);
133: position = pfVisitor.findPosition(root, txt);
134: assertEquals("Position of text child node", 195, position);
135:
136: //comment
137: txt = (Text) company.getChildNodes().item(3);
138: position = pfVisitor.findPosition(root, txt);
139: assertEquals("Position of comment (after newline)", 238,
140: position);
141:
142: //second employee element
143: Element employee2 = (Element) company.getChildNodes().item(5);
144: position = pfVisitor.findPosition(root, employee2);
145: assertEquals("Position of second employee element", 259,
146: position);
147: }
148:
149: public void testFindPosition2() throws IOException, Exception {
150:
151: xmlModel = Util.loadXDMModel("visitor/testPosition.xsd");
152: xmlModel.sync();
153:
154: FindVisitor instance = new FindVisitor();
155:
156: Document root = xmlModel.getDocument();
157:
158: PositionFinderVisitor pfVisitor = new PositionFinderVisitor();
159:
160: //element schema
161: Element schema = (Element) root.getChildNodes().item(2);
162: Node result = instance.find(root, schema.getId());
163: assertEquals(schema, result);
164:
165: int position = pfVisitor.findPosition(root, schema);
166: this .assertEquals("Position of schema element", 215, position);
167:
168: //global element
169: Element ge = (Element) schema.getChildNodes().item(4);
170: position = pfVisitor.findPosition(root, ge);
171: assertEquals("Position of employee element", 588, position);
172: }
173:
174: public void testFindPosition3() throws IOException, Exception {
175: PositionFinderVisitor pfVisitor = new PositionFinderVisitor();
176: xmlModel = Util.loadXDMModel("visitor/testPosition3.wsdl");
177: Document root = xmlModel.getDocument();
178: Element definitions = (Element) root.getDocumentElement();
179:
180: int position = pfVisitor.findPosition(root, definitions);
181: assertEquals("Position of root element", 83, position);
182:
183: Element types = (Element) definitions.getChildNodes().item(1);
184: assertEquals("types", types.getTagName());
185: position = pfVisitor.findPosition(root, types);
186: assertEquals("Position of types", 464, position);
187:
188: Element schema = (Element) types.getChildNodes().item(1);
189: assertEquals("xsd:schema", schema.getTagName());
190: position = pfVisitor.findPosition(root, schema);
191: assertEquals("Position of types", 480, position);
192:
193: }
194:
195: public void testFindPosition4() throws IOException, Exception {
196: PositionFinderVisitor pfVisitor = new PositionFinderVisitor();
197: xmlModel = Util.loadXDMModel("visitor/testPosition4.wsdl");
198: Document root = xmlModel.getDocument();
199: Element definitions = (Element) root.getDocumentElement();
200: Element types = (Element) definitions.getChildNodes().item(1);
201: Element foobar = (Element) types.getChildNodes().item(3);
202: assertEquals("foobar", foobar.getTagName());
203: int position = pfVisitor.findPosition(root, foobar);
204: assertEquals("Position of types", 899, position);
205: }
206:
207: protected void setUp() throws Exception {
208: xmlModel = Util.loadXDMModel("visitor/testPosition.xml");
209: xmlModel.sync();
210: }
211:
212: private void dumpTextNodes(Node node) {
213: NodeList nodes = node.getChildNodes();
214: int counter = 0;
215: for (int i = 0; i < nodes.getLength(); i++) {
216: Node n = (Node) nodes.item(i);
217: if (n instanceof Text) {
218: Text t = ((Text) n);
219:
220: }
221: }
222: }
223:
224: private XDMModel xmlModel;
225:
226: }
|