1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
33
34 public class TestXPathExpressionEngineInConfig {
35
36 private static final String KEY = "test/expression/xpath";
37
38
39 private static final String VALUE = "success";
40
41
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
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
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
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
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
106
107 @Test
108 public void testSetPropertyNewKey() {
109 config.setProperty(KEY, VALUE);
110 assertEquals(VALUE, config.getString(KEY));
111 }
112
113
114
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 }