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.jdom;
017:
018: import java.util.List;
019:
020: import junit.framework.Test;
021: import junit.framework.TestSuite;
022:
023: import org.apache.commons.jxpath.AbstractFactory;
024: import org.apache.commons.jxpath.ri.model.XMLModelTestCase;
025: import org.apache.commons.jxpath.xml.DocumentContainer;
026: import org.jdom.Attribute;
027: import org.jdom.CDATA;
028: import org.jdom.Document;
029: import org.jdom.Element;
030: import org.jdom.Text;
031:
032: /**
033: * Tests JXPath with JDOM
034: *
035: * @author Dmitri Plotnikov
036: * @version $Revision: 1.12 $ $Date: 2004/02/29 14:17:43 $
037: */
038:
039: public class JDOMModelTest extends XMLModelTestCase {
040: /**
041: * Construct a new instance of this test case.
042: *
043: * @param name Name of the test case
044: */
045: public JDOMModelTest(String name) {
046: super (name);
047: }
048:
049: /**
050: * Return the tests included in this test suite.
051: */
052: public static Test suite() {
053: return (new TestSuite(JDOMModelTest.class));
054: }
055:
056: protected String getModel() {
057: return DocumentContainer.MODEL_JDOM;
058: }
059:
060: public void testGetNode() {
061: assertXPathNodeType(context, "/", Document.class);
062: assertXPathNodeType(context, "/vendor/location", Element.class);
063: assertXPathNodeType(context, "//location/@name",
064: Attribute.class);
065: }
066:
067: public void testID() {
068: // id() is not supported by JDOM
069: }
070:
071: protected AbstractFactory getAbstractFactory() {
072: return new TestJDOMFactory();
073: }
074:
075: protected String getXMLSignature(Object node, boolean elements,
076: boolean attributes, boolean text, boolean pi) {
077: StringBuffer buffer = new StringBuffer();
078: appendXMLSignature(buffer, node, elements, attributes, text, pi);
079: return buffer.toString();
080: }
081:
082: private void appendXMLSignature(StringBuffer buffer, Object object,
083: boolean elements, boolean attributes, boolean text,
084: boolean pi) {
085: if (object instanceof Document) {
086: buffer.append("<D>");
087: appendXMLSignature(buffer,
088: ((Document) object).getContent(), elements,
089: attributes, text, pi);
090: buffer.append("</D");
091: } else if (object instanceof Element) {
092: String tag = elements ? ((Element) object).getName() : "E";
093: buffer.append("<");
094: buffer.append(tag);
095: buffer.append(">");
096: appendXMLSignature(buffer, ((Element) object).getContent(),
097: elements, attributes, text, pi);
098: buffer.append("</");
099: buffer.append(tag);
100: buffer.append(">");
101: } else if (object instanceof Text || object instanceof CDATA) {
102: if (text) {
103: String string = ((Text) object).getText();
104: string = string.replace('\n', '=');
105: buffer.append(string);
106: }
107: }
108: }
109:
110: private void appendXMLSignature(StringBuffer buffer, List children,
111: boolean elements, boolean attributes, boolean text,
112: boolean pi) {
113: for (int i = 0; i < children.size(); i++) {
114: appendXMLSignature(buffer, children.get(i), elements,
115: attributes, text, pi);
116: }
117: }
118: }
|