1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration.tree.xpath;
18
19 import static org.junit.Assert.assertEquals;
20
21 import org.apache.commons.configuration.XMLConfiguration;
22 import org.junit.Before;
23 import org.junit.Test;
24
25
26
27
28
29
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 @Before
46 public void setUp() throws Exception
47 {
48 config = new XMLConfiguration();
49 config.setExpressionEngine(new XPathExpressionEngine());
50 }
51
52
53
54
55
56 @Test
57 public void testSetPropertyExisting()
58 {
59 config.addProperty(" " + KEY, "failure");
60 config.setProperty(KEY, VALUE);
61 assertEquals("Value not changed", VALUE, config.getString(KEY));
62 }
63
64
65
66
67 @Test
68 public void testSetPropertyPartlyExisting()
69 {
70 final String testKey = KEY + "/sub";
71 config.addProperty(" " + KEY, "test");
72 config.setProperty(testKey, VALUE);
73 assertEquals("Value not set", VALUE, config.getString(testKey));
74 }
75
76
77
78
79 @Test
80 public void testSetPropertyNewAttribute()
81 {
82 final String keyAttr = KEY + "/@attr";
83 config.addProperty(" " + KEY, "test");
84 config.setProperty(keyAttr, VALUE);
85 assertEquals("Value not set", VALUE, config.getString(keyAttr));
86 }
87
88
89
90
91 @Test
92 public void testSetPropertyNewKey()
93 {
94 config.setProperty(KEY, VALUE);
95 assertEquals("Value not set", VALUE, config.getString(KEY));
96 }
97
98
99
100
101
102 @Test
103 public void testAddPropertyComplexStructures()
104 {
105 config.addProperty("tables/table/name", "tasks");
106 config.addProperty("tables/table[last()]/@type", "system");
107 config.addProperty("tables/table[last()]/fields/field/name", "taskid");
108 config.addProperty("tables/table[last()]/fields/field[last()]/@type",
109 "int");
110 config.addProperty("tables table/name", "documents");
111 assertEquals("Wrong table 1", "tasks",
112 config.getString("tables/table[1]/name"));
113 assertEquals("Wrong table 2", "documents",
114 config.getString("tables/table[2]/name"));
115 assertEquals("Wrong field type", "int",
116 config.getString("tables/table[1]/fields/field[1]/@type"));
117 }
118 }