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.configuration2.tree.xpath;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  
22  import java.io.StringReader;
23  import java.util.Iterator;
24  
25  import org.apache.commons.configuration2.XMLConfiguration;
26  import org.apache.commons.configuration2.ex.ConfigurationException;
27  import org.apache.commons.configuration2.io.FileHandler;
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * A test class for XPathExpressionEngine that tests the engine integrated into a hierarchical configuration.
33   */
34  public class TestXPathExpressionEngineInConfig {
35      /** Constant for a test key. */
36      private static final String KEY = "test/expression/xpath";
37  
38      /** Constant for a value for test properties. */
39      private static final String VALUE = "success";
40  
41      /** The test configuration. */
42      private XMLConfiguration config;
43  
44      @BeforeEach
45      public void setUp() throws Exception {
46          config = new XMLConfiguration();
47          config.setExpressionEngine(new XPathExpressionEngine());
48      }
49  
50      /**
51       * Tests whether addProperty() can be used to create more complex hierarchical structures.
52       */
53      @Test
54      public void testAddPropertyComplexStructures() {
55          config.addProperty("tables/table/name", "tasks");
56          config.addProperty("tables/table[last()]/@type", "system");
57          config.addProperty("tables/table[last()]/fields/field/name", "taskid");
58          config.addProperty("tables/table[last()]/fields/field[last()]/@type", "int");
59          config.addProperty("tables table/name", "documents");
60          assertEquals("tasks", config.getString("tables/table[1]/name"));
61          assertEquals("documents", config.getString("tables/table[2]/name"));
62          assertEquals("int", config.getString("tables/table[1]/fields/field[1]/@type"));
63      }
64  
65      /**
66       * Tests whether configuration properties with a namespace can be handled.
67       */
68      @Test
69      public void testPropertiesWithNamespace() throws ConfigurationException {
70          final String xml = "<Config>\n" + "<dsig:Transforms xmlns:dsig=\"http://www.w3.org/2000/09/xmldsig#\">\n"
71              + "  <dsig:Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xpath-19991116\">\n"
72              + "    <dsig:XPath xmlns:ietf=\"http://www.ietf.org\" xmlns:pl=\"http://test.test\">self::pl:policy1</dsig:XPath>\n" + "  </dsig:Transform>\n"
73              + "  <dsig:Transform Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\"/>\n" + "</dsig:Transforms>" + "</Config>";
74          final FileHandler handler = new FileHandler(config);
75          handler.load(new StringReader(xml));
76  
77          for (final Iterator<String> it = config.getKeys(); it.hasNext();) {
78              final String key = it.next();
79              assertNotNull(config.getString(key), "No value for " + key);
80          }
81      }
82  
83      /**
84       * Tests whether an already existing property can be changed using setProperty().
85       */
86      @Test
87      public void testSetPropertyExisting() {
88          config.addProperty(" " + KEY, "failure");
89          config.setProperty(KEY, VALUE);
90          assertEquals(VALUE, config.getString(KEY));
91      }
92  
93      /**
94       * Tests whether setProperty() can be used to add a new attribute.
95       */
96      @Test
97      public void testSetPropertyNewAttribute() {
98          final String keyAttr = KEY + "/@attr";
99          config.addProperty(" " + KEY, "test");
100         config.setProperty(keyAttr, VALUE);
101         assertEquals(VALUE, config.getString(keyAttr));
102     }
103 
104     /**
105      * Tests whether setProperty() can be used to create a completely new key.
106      */
107     @Test
108     public void testSetPropertyNewKey() {
109         config.setProperty(KEY, VALUE);
110         assertEquals(VALUE, config.getString(KEY));
111     }
112 
113     /**
114      * Tests setProperty() if the specified path partly exists.
115      */
116     @Test
117     public void testSetPropertyPartlyExisting() {
118         final String testKey = KEY + "/sub";
119         config.addProperty(" " + KEY, "test");
120         config.setProperty(testKey, VALUE);
121         assertEquals(VALUE, config.getString(testKey));
122     }
123 }