1   /*
2    * Copyright 1999-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jxpath.ri.model.jdom;
17  
18  import java.util.List;
19  
20  import junit.framework.Test;
21  import junit.framework.TestSuite;
22  
23  import org.apache.commons.jxpath.AbstractFactory;
24  import org.apache.commons.jxpath.ri.model.XMLModelTestCase;
25  import org.apache.commons.jxpath.xml.DocumentContainer;
26  import org.jdom.Attribute;
27  import org.jdom.CDATA;
28  import org.jdom.Document;
29  import org.jdom.Element;
30  import org.jdom.Text;
31  
32  /***
33   * Tests JXPath with JDOM
34   *
35   * @author Dmitri Plotnikov
36   * @version $Revision: 1.12 $ $Date: 2004/02/29 14:17:43 $
37   */
38  
39  public class JDOMModelTest extends XMLModelTestCase {
40      /***
41       * Construct a new instance of this test case.
42       *
43       * @param name Name of the test case
44       */
45      public JDOMModelTest(String name) {
46          super(name);
47      }
48  
49      /***
50       * Return the tests included in this test suite.
51       */
52      public static Test suite() {
53          return (new TestSuite(JDOMModelTest.class));
54      }
55  
56      protected String getModel() {
57          return DocumentContainer.MODEL_JDOM;
58      }
59      
60      public void testGetNode() {
61          assertXPathNodeType(context, "/", Document.class);
62          assertXPathNodeType(context, "/vendor/location", Element.class);
63          assertXPathNodeType(context, "//location/@name", Attribute.class);
64      }    
65      
66      public void testID() {
67          // id() is not supported by JDOM
68      }
69  
70      protected AbstractFactory getAbstractFactory() {
71          return new TestJDOMFactory();
72      }
73  
74      protected String getXMLSignature(
75          Object node,
76          boolean elements,
77          boolean attributes,
78          boolean text,
79          boolean pi) 
80      {
81          StringBuffer buffer = new StringBuffer();
82          appendXMLSignature(buffer, node, elements, attributes, text, pi);
83          return buffer.toString();
84      }
85  
86      private void appendXMLSignature(
87          StringBuffer buffer,
88          Object object,
89          boolean elements,
90          boolean attributes,
91          boolean text,
92          boolean pi) 
93      {
94          if (object instanceof Document) {
95              buffer.append("<D>");
96              appendXMLSignature(
97                  buffer,
98                  ((Document) object).getContent(),
99                  elements,
100                 attributes,
101                 text,
102                 pi);
103             buffer.append("</D");
104         }
105         else if (object instanceof Element) {
106             String tag = elements ? ((Element) object).getName() : "E";
107             buffer.append("<");
108             buffer.append(tag);
109             buffer.append(">");
110             appendXMLSignature(
111                 buffer,
112                 ((Element) object).getContent(),
113                 elements,
114                 attributes,
115                 text,
116                 pi);
117             buffer.append("</");
118             buffer.append(tag);
119             buffer.append(">");
120         }
121         else if (object instanceof Text || object instanceof CDATA) {
122             if (text) {
123                 String string = ((Text) object).getText();
124                 string = string.replace('\n', '=');
125                 buffer.append(string);
126             }
127         }
128     }
129 
130     private void appendXMLSignature(
131         StringBuffer buffer,
132         List children,
133         boolean elements,
134         boolean attributes,
135         boolean text,
136         boolean pi) 
137     {
138         for (int i = 0; i < children.size(); i++) {
139             appendXMLSignature(
140                 buffer,
141                 children.get(i),
142                 elements,
143                 attributes,
144                 text,
145                 pi);
146         }
147     }
148 }