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