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.dom;
19  
20  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
21  
22  import org.apache.commons.jxpath.AbstractFactory;
23  import org.apache.commons.jxpath.JXPathContext;
24  import org.apache.commons.jxpath.ri.model.AbstractXMLModelTest;
25  import org.apache.commons.jxpath.xml.DocumentContainer;
26  import org.junit.jupiter.api.Test;
27  import org.w3c.dom.Attr;
28  import org.w3c.dom.Document;
29  import org.w3c.dom.Element;
30  import org.w3c.dom.Node;
31  import org.w3c.dom.NodeList;
32  
33  /**
34   * Tests JXPath with DOM
35   */
36  public class DOMModelTest extends AbstractXMLModelTest {
37  
38      private void appendXMLSignature(final StringBuilder buffer, final NodeList children, final boolean elements, final boolean attributes, final boolean text,
39              final boolean pi) {
40          for (int i = 0; i < children.getLength(); i++) {
41              appendXMLSignature(buffer, children.item(i), elements, attributes, text, pi);
42          }
43      }
44  
45      private void appendXMLSignature(final StringBuilder buffer, final Object object, final boolean elements, final boolean attributes, final boolean text,
46              final boolean pi) {
47          final Node node = (Node) object;
48          final int type = node.getNodeType();
49          switch (type) {
50          case Node.DOCUMENT_NODE:
51              buffer.append("<D>");
52              appendXMLSignature(buffer, node.getChildNodes(), elements, attributes, text, pi);
53              buffer.append("</D");
54              break;
55          case Node.ELEMENT_NODE:
56              final String tag = elements ? ((Element) node).getTagName() : "E";
57              buffer.append("<");
58              buffer.append(tag);
59              buffer.append(">");
60              appendXMLSignature(buffer, node.getChildNodes(), elements, attributes, text, pi);
61              buffer.append("</");
62              buffer.append(tag);
63              buffer.append(">");
64              break;
65          case Node.TEXT_NODE:
66          case Node.CDATA_SECTION_NODE:
67              if (text) {
68                  String string = node.getNodeValue();
69                  string = string.replace('\n', '=');
70                  buffer.append(string);
71              }
72              break;
73          }
74      }
75  
76      @Override
77      protected AbstractFactory getAbstractFactory() {
78          return new TestDOMFactory();
79      }
80  
81      @Override
82      protected String getModel() {
83          return DocumentContainer.MODEL_DOM;
84      }
85  
86      @Override
87      protected String getXMLSignature(final Object node, final boolean elements, final boolean attributes, final boolean text, final boolean pi) {
88          final StringBuilder buffer = new StringBuilder();
89          appendXMLSignature(buffer, node, elements, attributes, text, pi);
90          return buffer.toString();
91      }
92  
93      @Test
94      public void testGetElementDescendantOrSelf() {
95          final JXPathContext childContext = context.getRelativeContext(context.getPointer("/vendor"));
96          assertInstanceOf(Element.class, childContext.getContextBean());
97          assertXPathNodeType(childContext, "//vendor", Element.class);
98      }
99  
100     @Test
101     public void testGetNode() {
102         assertXPathNodeType(context, "/", Document.class);
103         assertXPathNodeType(context, "/vendor/location", Element.class);
104         assertXPathNodeType(context, "//location/@name", Attr.class);
105         assertXPathNodeType(context, "//vendor", Element.class);
106     }
107 }