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
37 private static final String KEY = "test/expression/xpath";
38
39
40 private static final String VALUE = "success";
41
42
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
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
68
69 @Test
70 void testPropertiesWithNamespace() throws ConfigurationException {
71
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
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
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
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
112
113 @Test
114 void testSetPropertyNewKey() {
115 config.setProperty(KEY, VALUE);
116 assertEquals(VALUE, config.getString(KEY));
117 }
118
119
120
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 }