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;
19  
20  import org.apache.commons.jxpath.AbstractJXPathTest;
21  import org.apache.commons.jxpath.JXPathContext;
22  import org.apache.commons.jxpath.xml.DocumentContainer;
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * Test externally registered XML namespaces; JXPATH-97.
27   */
28  public class ExternalXMLNamespaceTest extends AbstractJXPathTest {
29  
30      protected JXPathContext context;
31  
32      protected JXPathContext createContext(final String model) {
33          final JXPathContext context = JXPathContext.newContext(createDocumentContainer(model));
34          context.registerNamespace("A", "foo");
35          context.registerNamespace("B", "bar");
36          return context;
37      }
38  
39      protected DocumentContainer createDocumentContainer(final String model) {
40          final DocumentContainer result = new DocumentContainer(AbstractJXPathTest.class.getResource("ExternalNS.xml"), model);
41          // this setting only works for DOM, so no JDOM tests :|
42          result.setNamespaceAware(false);
43          return result;
44      }
45  
46      protected void doTest(final String xpath, final String model, final String expected) {
47          assertXPathValue(createContext(model), xpath, expected);
48      }
49  
50      protected void doTestAttribute(final String model) {
51          doTest("/ElementA/@A:myAttr", model, "Mytype");
52      }
53  
54      protected void doTestCreateAndSetAttribute(final String model) {
55          assertXPathCreatePathAndSetValue(createContext(model), "/ElementA/@A:newAttr", "newValue", "/ElementA[1]/@A:newAttr");
56      }
57  
58      protected void doTestElement(final String model) {
59          doTest("/ElementA/B:ElementB", model, "MY VALUE");
60      }
61  
62      @Test
63      public void testAttributeDOM() {
64          doTestAttribute(DocumentContainer.MODEL_DOM);
65      }
66  
67      @Test
68      public void testCreateAndSetAttributeDOM() {
69          doTestCreateAndSetAttribute(DocumentContainer.MODEL_DOM);
70      }
71  
72      @Test
73      public void testElementDOM() {
74          doTestElement(DocumentContainer.MODEL_DOM);
75      }
76  }