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