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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.configuration.tree.xpath;
19
import junit.framework.TestCase;
21
import org.apache.commons.configuration.XMLConfiguration;
24
* A test class for XPathExpressionEngine that tests the engine integrated into
25
* a hierarchical configuration.
28
* href="http://commons.apache.org/configuration/team-list.html">Commons
29
* Configuration team</a>
30
* @version $Id: TestXPathExpressionEngineInConfig.java 1152343 2011-07-29 19:23:09Z oheger $
32
public class TestXPathExpressionEngineInConfig extends TestCase
34
/** Constant for a test key. */
35
private static final String KEY = "test/expression/xpath";
37
/** Constant for a value for test properties. */
38
private static final String VALUE = "success";
40
/** The test configuration. */
41
private XMLConfiguration config;
43
protected void setUp() throws Exception
46
config = new XMLConfiguration();
47
config.setExpressionEngine(new XPathExpressionEngine());
51
* Tests whether an already existing property can be changed using
54
public void testSetPropertyExisting()
56
config.addProperty(" " + KEY, "failure");
57
config.setProperty(KEY, VALUE);
58
assertEquals("Value not changed", VALUE, config.getString(KEY));
62
* Tests setProperty() if the specified path partly exists.
64
public void testSetPropertyPartlyExisting()
66
final String testKey = KEY + "/sub";
67
config.addProperty(" " + KEY, "test");
68
config.setProperty(testKey, VALUE);
69
assertEquals("Value not set", VALUE, config.getString(testKey));
73
* Tests whether setProperty() can be used to add a new attribute.
75
public void testSetPropertyNewAttribute()
77
final String keyAttr = KEY + "/@attr";
78
config.addProperty(" " + KEY, "test");
79
config.setProperty(keyAttr, VALUE);
80
assertEquals("Value not set", VALUE, config.getString(keyAttr));
84
* Tests whether setProperty() can be used to create a completely new key.
86
public void testSetPropertyNewKey()
88
config.setProperty(KEY, VALUE);
89
assertEquals("Value not set", VALUE, config.getString(KEY));
93
* Tests whether addProperty() can be used to create more complex
94
* hierarchical structures.
96
public void testAddPropertyComplexStructures()
98
config.addProperty("tables/table/name", "tasks");
99
config.addProperty("tables/table[last()]/@type", "system");
100
config.addProperty("tables/table[last()]/fields/field/name", "taskid");
101
config.addProperty("tables/table[last()]/fields/field[last()]/@type",
103
config.addProperty("tables table/name", "documents");
104
assertEquals("Wrong table 1", "tasks",
105
config.getString("tables/table[1]/name"));
106
assertEquals("Wrong table 2", "documents",
107
config.getString("tables/table[2]/name"));
108
assertEquals("Wrong field type", "int",
109
config.getString("tables/table[1]/fields/field[1]/@type"));