001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of 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,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jxpath.ri.model.dom;
017:
018: import junit.framework.Test;
019: import junit.framework.TestSuite;
020:
021: import org.apache.commons.jxpath.AbstractFactory;
022: import org.apache.commons.jxpath.ri.model.XMLModelTestCase;
023: import org.apache.commons.jxpath.xml.DocumentContainer;
024: import org.w3c.dom.Attr;
025: import org.w3c.dom.Document;
026: import org.w3c.dom.Element;
027: import org.w3c.dom.Node;
028: import org.w3c.dom.NodeList;
029:
030: /**
031: * Tests JXPath with DOM
032: *
033: * @author Dmitri Plotnikov
034: * @version $Revision: 1.13 $ $Date: 2004/03/02 01:32:20 $
035: */
036:
037: public class DOMModelTest extends XMLModelTestCase {
038:
039: /**
040: * Construct a new instance of this test case.
041: *
042: * @param name Name of the test case
043: */
044: public DOMModelTest(String name) {
045: super (name);
046: }
047:
048: /**
049: * Return the tests included in this test suite.
050: */
051: public static Test suite() {
052: return (new TestSuite(DOMModelTest.class));
053: }
054:
055: protected String getModel() {
056: return DocumentContainer.MODEL_DOM;
057: }
058:
059: protected AbstractFactory getAbstractFactory() {
060: return new TestDOMFactory();
061: }
062:
063: public void testGetNode() {
064: assertXPathNodeType(context, "/", Document.class);
065: assertXPathNodeType(context, "/vendor/location", Element.class);
066: assertXPathNodeType(context, "//location/@name", Attr.class);
067: }
068:
069: protected String getXMLSignature(Object node, boolean elements,
070: boolean attributes, boolean text, boolean pi) {
071: StringBuffer buffer = new StringBuffer();
072: appendXMLSignature(buffer, node, elements, attributes, text, pi);
073: return buffer.toString();
074: }
075:
076: private void appendXMLSignature(StringBuffer buffer, Object object,
077: boolean elements, boolean attributes, boolean text,
078: boolean pi) {
079: Node node = (Node) object;
080: int type = node.getNodeType();
081: switch (type) {
082: case Node.DOCUMENT_NODE:
083: buffer.append("<D>");
084: appendXMLSignature(buffer, node.getChildNodes(), elements,
085: attributes, text, pi);
086: buffer.append("</D");
087: break;
088:
089: case Node.ELEMENT_NODE:
090: String tag = elements ? ((Element) node).getTagName() : "E";
091: buffer.append("<");
092: buffer.append(tag);
093: buffer.append(">");
094: appendXMLSignature(buffer, node.getChildNodes(), elements,
095: attributes, text, pi);
096: buffer.append("</");
097: buffer.append(tag);
098: buffer.append(">");
099: break;
100:
101: case Node.TEXT_NODE:
102: case Node.CDATA_SECTION_NODE:
103: if (text) {
104: String string = node.getNodeValue();
105: string = string.replace('\n', '=');
106: buffer.append(string);
107: }
108: break;
109: }
110: }
111:
112: private void appendXMLSignature(StringBuffer buffer,
113: NodeList children, boolean elements, boolean attributes,
114: boolean text, boolean pi) {
115: for (int i = 0; i < children.getLength(); i++) {
116: appendXMLSignature(buffer, children.item(i), elements,
117: attributes, text, pi);
118: }
119: }
120: }
|