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 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 void testPropertiesWithNamespace() throws ConfigurationException {
70
71 final String xml = "<Config>\n"
72 + "<dsig:Transforms xmlns:dsig=\"http://www.w3.org/2000/09/xmldsig#\">\n"
73 + " <dsig:Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xpath-19991116\">\n"
74 + " <dsig:XPath xmlns:ietf=\"http://www.ietf.org\" xmlns:pl=\"http://test.test\">self::pl:policy1</dsig:XPath>\n"
75 + " </dsig:Transform>\n"
76 + " <dsig:Transform Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\"/>\n"
77 + "</dsig:Transforms></Config>";
78
79 final FileHandler handler = new FileHandler(config);
80 handler.load(new StringReader(xml));
81
82 for (final Iterator<String> it = config.getKeys(); it.hasNext();) {
83 final String key = it.next();
84 assertNotNull(config.getString(key), "No value for " + key);
85 }
86 }
87
88
89
90
91 @Test
92 void testSetPropertyExisting() {
93 config.addProperty(" " + KEY, "failure");
94 config.setProperty(KEY, VALUE);
95 assertEquals(VALUE, config.getString(KEY));
96 }
97
98
99
100
101 @Test
102 void testSetPropertyNewAttribute() {
103 final String keyAttr = KEY + "/@attr";
104 config.addProperty(" " + KEY, "test");
105 config.setProperty(keyAttr, VALUE);
106 assertEquals(VALUE, config.getString(keyAttr));
107 }
108
109
110
111
112 @Test
113 void testSetPropertyNewKey() {
114 config.setProperty(KEY, VALUE);
115 assertEquals(VALUE, config.getString(KEY));
116 }
117
118
119
120
121 @Test
122 void testSetPropertyPartlyExisting() {
123 final String testKey = KEY + "/sub";
124 config.addProperty(" " + KEY, "test");
125 config.setProperty(testKey, VALUE);
126 assertEquals(VALUE, config.getString(testKey));
127 }
128 }