001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.xml.client;
017:
018: import com.google.gwt.junit.client.GWTTestCase;
019: import com.google.gwt.xml.client.impl.DOMParseException;
020:
021: import java.util.Arrays;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: /**
026: * This class tests all the methods in the GWT XML parser.
027: */
028: public class XMLTest extends GWTTestCase {
029:
030: /**
031: * Returns the module name for GWT unit test running.
032: */
033: public String getModuleName() {
034: return "com.google.gwt.xml.XML";
035: }
036:
037: public void testAttr() {
038: Document d = createTestDocument();
039: Element de = d.getDocumentElement();
040: de.setAttribute("created", "true");
041: assertEquals("true", de.getAttribute("created"));
042: de.setAttribute("set", "toAValue");
043: assertEquals(de.getAttribute("set"), "toAValue");
044: assertTrue(de.getAttributeNode("set").getSpecified());
045: assertEquals(de.getAttributeNode("unset"), null);
046: }
047:
048: public void testCreate() {
049: Document d = XMLParser.createDocument();
050: CDATASection createCDATA;
051: if (XMLParser.supportsCDATASection()) {
052: createCDATA = d.createCDATASection("sampl<<< >>e data");
053: } else {
054: createCDATA = d.createCDATASection("sample data");
055: }
056: Comment createComment = d.createComment("a sample comment");
057: DocumentFragment createDocumentFragment = d
058: .createDocumentFragment();
059: Element elementWithChildren = d
060: .createElement("elementWithChildren");
061: ProcessingInstruction createProcessingInstruction = d
062: .createProcessingInstruction("target",
063: "processing instruction data");
064: Text createTextNode = d.createTextNode("sample text node");
065: // TODO: what is "all" for?
066: List all = asList(new Node[] { createCDATA, createComment,
067: createDocumentFragment, elementWithChildren,
068: createProcessingInstruction, createTextNode });
069: List canHaveChildren = asList(new Node[] {
070: createDocumentFragment, elementWithChildren });
071: List canBeChildren = asList(new Node[] { createCDATA,
072: createComment, elementWithChildren,
073: createProcessingInstruction, createTextNode });
074:
075: for (int i = 0; i < canHaveChildren.size(); i++) {
076: Node parent = (Node) canHaveChildren.get(i);
077: Node cloneParent = parent.cloneNode(false);
078: if (canBeChildren.contains(parent)) {
079: d.appendChild(cloneParent);
080: }
081: for (int j = 0; j < canBeChildren.size(); j++) {
082: Node child = (Node) canBeChildren.get(j);
083: cloneParent.appendChild(child.cloneNode(false));
084: }
085: for (int j = 0; j < canBeChildren.size(); j++) {
086: Node clonedChild = cloneParent.getChildNodes().item(j);
087: Node hopefullySameChild = (Node) canBeChildren.get(j);
088: assertEquals(hopefullySameChild.cloneNode(false)
089: .toString(), clonedChild.toString());
090: }
091: Node deepClonedNode = parent.cloneNode(true);
092: assertEquals(parent.toString(), deepClonedNode.toString());
093: }
094: XMLParser.removeWhitespace(d);
095: if (XMLParser.supportsCDATASection()) {
096: assertEquals("<elementWithChildren>"
097: + "<![CDATA[sampl<<< >>e data]]>"
098: + "<!--a sample comment-->"
099: + "<elementWithChildren/>"
100: + "<?target processing instruction data?>"
101: + "sample text node" + "</elementWithChildren>", d
102: .toString());
103: } else {
104: // Opera does not support CDATASection nodes
105: assertEquals("<elementWithChildren>" + "sample data"
106: + "<!--a sample comment-->"
107: + "<elementWithChildren/>"
108: + "<?target processing instruction data?>"
109: + "sample text node" + "</elementWithChildren>", d
110: .toString());
111: }
112: }
113:
114: public void testDocument() {
115: Document d = createTestDocument();
116: NodeList e1Nodes = d.getElementsByTagName("e1");
117: assertEquals(e1Nodes.getLength(), 1);
118: Node e1Node = e1Nodes.item(0);
119: assertEquals(((Element) e1Node).getTagName(), "e1");
120: Element e1NodeDirect = d.getElementById("e1Id");
121: assertEquals(e1NodeDirect, null);
122: // we didn't define a dtd, so no id for us
123: Document alienDoc = XMLParser.createDocument();
124: Node alienNode11 = alienDoc.importNode(e1Node, true);
125: alienDoc.appendChild(alienNode11);
126: assertNotSame(e1Node, alienNode11);
127: assertEquals(e1Node.toString(), alienNode11.toString());
128: }
129:
130: public void testElement() {
131: Document d = createTestDocument();
132: Element top = d.getDocumentElement();
133: NodeList el = top.getElementsByTagName("e1");
134: assertEquals(el.getLength(), 1);
135: NodeList deepNodes = top.getElementsByTagName("deep");
136: assertEquals(deepNodes.getLength(), 2);
137: Element d1 = (Element) deepNodes.item(0);
138: assertTrue(d1.hasAttribute("depth"));
139: assertFalse(d1.hasAttribute("height"));
140: d1.removeAttribute("depth");
141: assertFalse(d1.hasAttribute("depth"));
142: Element d2 = (Element) deepNodes.item(1);
143: assertTrue(d2.hasAttribute("depth"));
144: Attr depthAttr = d2.getAttributeNode("depth");
145: d2.removeAttribute("depth");
146: assertFalse(d2.hasAttribute("depth"));
147: }
148:
149: public void testNamedNodeMap() {
150: Document d = createTestDocument();
151: NamedNodeMap m = d.getDocumentElement().getAttributes();
152: assertEquals(((Attr) m.getNamedItem("fluffy")).getValue(),
153: "true");
154: assertEquals(m.getLength(), 2);
155: }
156:
157: public void testPrefix() {
158: Document d = XMLParser
159: .parse("<?xml version=\"1.0\"?>\r\n"
160: + "<!-- both namespace prefixes are available throughout -->\r\n"
161: + "<bk:book xmlns:bk=\'urn:loc.gov:books\'\r\n"
162: + " xmlns:isbn=\'urn:ISBN:0-395-36341-6\'>\r\n"
163: + " <bk:title>Cheaper by the Dozen</bk:title>\r\n"
164: + " <isbn:number>1568491379</isbn:number>\r\n"
165: + "</bk:book>");
166: assertEquals(d.getDocumentElement().getNodeName(), "bk:book");
167: assertEquals(d.getDocumentElement().getPrefix(), "bk");
168: assertEquals(d.getElementsByTagName("book").getLength(), 1);
169: assertEquals(d.getElementsByTagName("book").item(0), d
170: .getDocumentElement());
171: }
172:
173: public void testNavigation() {
174: Document d = createTestDocument();
175: Element documentElement = d.getDocumentElement();
176: assertEquals("getPreviousSibling", documentElement
177: .getPreviousSibling(), d.getChildNodes().item(0));
178: assertEquals("getNextSibling",
179: documentElement.getNextSibling(), d.getChildNodes()
180: .item(2));
181: assertEquals("getDocumentElement", documentElement, d
182: .getChildNodes().item(1));
183: assertEquals("getTagName", documentElement.getTagName(), "doc");
184: NodeList documentElementChildNodes = documentElement
185: .getChildNodes();
186: int deChildNodesLength = documentElementChildNodes.getLength();
187: assertEquals("getFirstChild", documentElement.getFirstChild(),
188: documentElementChildNodes.item(0));
189: assertEquals("getLastChild", documentElement.getLastChild(),
190: documentElementChildNodes.item(deChildNodesLength - 1));
191: assertEquals("getNextSibling2", documentElement.getFirstChild()
192: .getNextSibling(), documentElementChildNodes.item(1));
193:
194: assertEquals("getPreviousSibling2", documentElement
195: .getLastChild().getPreviousSibling(),
196: documentElementChildNodes.item(deChildNodesLength - 2));
197: }
198:
199: public void testNode() {
200: Document ns = XMLParser.parse("<x:doc xmlns:x=\"http://x\"/>");
201: String xUrl = "http://x";
202: assertEquals(xUrl, ns.getFirstChild().getNamespaceURI());
203: Element top = ns.getDocumentElement();
204: Text xxx = ns.createTextNode("xxx");
205: top.appendChild(xxx);
206: Text yyy = ns.createTextNode("yyy");
207: top.appendChild(yyy);
208: assertEquals(xxx.getNodeValue(), "xxx");
209: assertEquals(xxx.getParentNode(), top);
210: xxx.setNodeValue("x");
211: assertEquals(xxx.getData(), "x");
212: assertEquals(xxx.getOwnerDocument(), ns);
213: top.removeChild(xxx);
214: assertEquals(top.getChildNodes().getLength(), 1);
215: Comment commentNode = ns.createComment("comment ccc");
216: top.replaceChild(commentNode, yyy);
217: assertEquals(top.getFirstChild(), commentNode);
218: assertEquals(top.getChildNodes().getLength(), 1);
219: }
220:
221: public void testParse() {
222: Document d = XMLParser
223: .parse("<!--hello--> <a spam=\"ham\">\n <?pi hello ?>dfgdfg <b/>\t</a>");
224: XMLParser.removeWhitespace(d);
225: assertEquals(
226: "<!--hello--><a spam=\"ham\"><?pi hello ?>dfgdfg <b/></a>",
227: d.toString());
228: try {
229: XMLParser.parse("<<<");
230: fail();
231: } catch (DOMParseException e) {
232: }
233: }
234:
235: public void testProcessingInstruction() {
236: Document d = createTestDocument();
237: ProcessingInstruction pi = (ProcessingInstruction) d
238: .getChildNodes().item(0);
239: assertEquals(pi.getTarget(), "target");
240: assertEquals(pi.getData(), "some data");
241: pi.setData("other data");
242: assertEquals(pi.getData(), "other data");
243: }
244:
245: public void testText() {
246: Document d = createTestDocument();
247: List textLikeNodes = Arrays.asList(new Node[] {
248: d.createTextNode(""), d.createCDATASection(""),
249: d.createComment("") });
250: StringBuffer b = new StringBuffer();
251: for (char i = 32; i < 30000; i++) {
252: b.append(i);
253: }
254: for (Iterator iter = textLikeNodes.iterator(); iter.hasNext();) {
255: CharacterData textLike = (CharacterData) iter.next();
256: textLike.setData(b.toString());
257: assertEquals(
258: "initialLength type:" + textLike.getNodeType(),
259: 30000 - 32, textLike.getLength());
260: assertEquals("initialEquals", textLike.getData(), b
261: .toString());
262: }
263: for (int i = 32; i < 29900; i += 100) {
264: for (Iterator iter = textLikeNodes.iterator(); iter
265: .hasNext();) {
266: CharacterData textLike = (CharacterData) iter.next();
267: assertEquals("substring type:" + textLike.getNodeType()
268: + " count: " + i, b.substring(i, i + 100),
269: textLike.substringData(i, 100));
270: }
271: }
272: for (Iterator iter = textLikeNodes.iterator(); iter.hasNext();) {
273: StringBuffer bTemp = new StringBuffer(b.toString());
274: CharacterData textLike = (CharacterData) iter.next();
275: textLike.deleteData(100, 100);
276: bTemp.delete(100, 200);
277: assertEquals("deleteLength type:" + textLike.getNodeType(),
278: bTemp.length(), textLike.getData().length());
279: assertEquals("deleteEquals type:" + textLike.getNodeType(),
280: bTemp.toString(), textLike.getData());
281: bTemp.setLength(0);
282: }
283: for (Iterator iter = textLikeNodes.iterator(); iter.hasNext();) {
284: StringBuffer bTemp = new StringBuffer(b.toString());
285: CharacterData textLike = (CharacterData) iter.next();
286: textLike.setData(bTemp.toString());
287: textLike.replaceData(50, 100, " ");
288: bTemp.replace(50, 150, " ");
289: assertEquals(
290: "replaceLength type:" + textLike.getNodeType(),
291: bTemp.length(), textLike.getData().length());
292: assertEquals(
293: "replaceEquals type:" + textLike.getNodeType(),
294: bTemp.toString(), textLike.getData());
295: bTemp.setLength(0);
296: }
297: for (Iterator iter = textLikeNodes.iterator(); iter.hasNext();) {
298: CharacterData textLike = (CharacterData) iter.next();
299: textLike.appendData("!!!");
300: assertTrue("endswith!!!", textLike.getData()
301: .endsWith("!!!"));
302: textLike.insertData(0, "!");
303: textLike.insertData(1, "@@");
304: assertTrue("startsWith !@@", textLike.getData().startsWith(
305: "!@@"));
306: }
307: Text t = (Text) d.getDocumentElement().getChildNodes().item(3);
308: Text rightT = t.splitText(5);
309: assertEquals("t and leftT parent equality", t.getParentNode(),
310: rightT.getParentNode());
311: assertEquals("leftT.getPreviousSibling", rightT
312: .getPreviousSibling(), t);
313: assertEquals("t.length", t.getData().length(), 5);
314: assertEquals("leftT.length", rightT.getData().length(), 5);
315: assertEquals("t data", t.getData(), "01234");
316: assertEquals("LeftT data", rightT.getData(), "56789");
317: CDATASection cd = (CDATASection) d.getDocumentElement()
318: .getChildNodes().item(5);
319: Text rightCD = cd.splitText(5);
320: assertEquals("cd and leftCd parent equality", cd
321: .getParentNode(), rightCD.getParentNode());
322: assertEquals("leftCD.getPreviousSibling", rightCD
323: .getPreviousSibling(), cd);
324: assertEquals("cd length", cd.getData().length(), 5);
325: assertEquals("leftCD.length", rightCD.getData().length(), 5);
326: assertEquals("cd data", cd.getData(), "abcde");
327: assertEquals("leftCD data", rightCD.getData(), "fghij");
328: d.getDocumentElement().normalize();
329: if (XMLParser.supportsCDATASection()) {
330: assertEquals("normalized t", d.getDocumentElement()
331: .getChildNodes().item(3).toString(), "0123456789");
332: } else {
333: assertEquals("normalized t", d.getDocumentElement()
334: .getChildNodes().item(3).toString(),
335: "0123456789abcdefghij");
336: }
337: }
338:
339: private List asList(Node[] nodes) {
340: return Arrays.asList(nodes);
341: }
342:
343: private Document createTestDocument() {
344: Document d = XMLParser.createDocument();
345: Element top = d.createElement("doc");
346: top.setAttribute("fluffy", "true");
347: top.setAttribute("numAttributes", "2");
348: d.appendChild(top);
349: ProcessingInstruction commentBefore = d
350: .createProcessingInstruction("target", "some data");
351: d.insertBefore(commentBefore, top);
352: Comment commentAfter = d.createComment("after the element");
353: d.insertBefore(commentAfter, null);
354: for (int i = 0; i < 3; i++) {
355: Element e = d.createElement("e" + i);
356: e.setAttribute("id", "e" + i + "Id");
357: top.appendChild(e);
358: }
359: Element deep = d.createElement("deep");
360: top.getFirstChild().appendChild(deep);
361: deep.setAttribute("depth", "1 foot");
362: Element deep2 = d.createElement("deep");
363: deep2.setAttribute("depth", "2 feet");
364: top.getFirstChild().getFirstChild().appendChild(deep2);
365:
366: top.appendChild(d.createTextNode("0123456789"));
367: top.appendChild(d.createCDATASection("abcdefghij"));
368: return d;
369: }
370:
371: }
|