View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jxpath.ri.model.jdom;
19  
20  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
21  
22  import java.util.List;
23  
24  import org.apache.commons.jxpath.AbstractFactory;
25  import org.apache.commons.jxpath.JXPathContext;
26  import org.apache.commons.jxpath.ri.model.AbstractXMLModelTest;
27  import org.apache.commons.jxpath.xml.DocumentContainer;
28  import org.jdom.Attribute;
29  import org.jdom.CDATA;
30  import org.jdom.Document;
31  import org.jdom.Element;
32  import org.jdom.Text;
33  import org.junit.jupiter.api.Disabled;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Tests JXPath with JDOM
38   */
39  public class JDOMModelTest extends AbstractXMLModelTest {
40  
41      private void appendXMLSignature(final StringBuilder buffer, final List children, final boolean elements, final boolean attributes, final boolean text,
42              final boolean pi) {
43          for (final Object child : children) {
44              appendXMLSignature(buffer, child, elements, attributes, text, pi);
45          }
46      }
47  
48      private void appendXMLSignature(final StringBuilder buffer, final Object object, final boolean elements, final boolean attributes, final boolean text,
49              final boolean pi) {
50          if (object instanceof Document) {
51              buffer.append("<D>");
52              appendXMLSignature(buffer, ((Document) object).getContent(), elements, attributes, text, pi);
53              buffer.append("</D");
54          } else if (object instanceof Element) {
55              final String tag = elements ? ((Element) object).getName() : "E";
56              buffer.append("<");
57              buffer.append(tag);
58              buffer.append(">");
59              appendXMLSignature(buffer, ((Element) object).getContent(), elements, attributes, text, pi);
60              buffer.append("</");
61              buffer.append(tag);
62              buffer.append(">");
63          } else if ((object instanceof Text || object instanceof CDATA) && text) {
64              String string = ((Text) object).getText();
65              string = string.replace('\n', '=');
66              buffer.append(string);
67          }
68      }
69  
70      @Override
71      protected AbstractFactory getAbstractFactory() {
72          return new TestJDOMFactory();
73      }
74  
75      @Override
76      protected String getModel() {
77          return DocumentContainer.MODEL_JDOM;
78      }
79  
80      @Override
81      protected String getXMLSignature(final Object node, final boolean elements, final boolean attributes, final boolean text, final boolean pi) {
82          final StringBuilder buffer = new StringBuilder();
83          appendXMLSignature(buffer, node, elements, attributes, text, pi);
84          return buffer.toString();
85      }
86  
87      @Test
88      public void testGetElementDescendantOrSelf() {
89          final JXPathContext childContext = context.getRelativeContext(context.getPointer("/vendor"));
90          assertInstanceOf(Element.class, childContext.getContextBean());
91          assertXPathNodeType(childContext, "//vendor", Element.class);
92      }
93  
94      @Test
95      public void testGetNode() {
96          assertXPathNodeType(context, "/", Document.class);
97          assertXPathNodeType(context, "/vendor/location", Element.class);
98          assertXPathNodeType(context, "//location/@name", Attribute.class);
99          assertXPathNodeType(context, "//vendor", Element.class); // bugzilla #38586
100     }
101 
102     @Override
103     @Test
104     @Disabled("id() is not supported by JDOM")
105     public void testID() {
106         // id() is not supported by JDOM
107     }
108 }