View Javadoc

1   /*
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
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
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.
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   * A test class for XPathExpressionEngine that tests the engine integrated into
27   * a hierarchical configuration.
28   *
29   * @author <a
30   *         href="http://commons.apache.org/configuration/team-list.html">Commons
31   *         Configuration team</a>
32   * @version $Id: TestXPathExpressionEngineInConfig.java 1226113 2011-12-31 15:46:53Z oheger $
33   */
34  public class TestXPathExpressionEngineInConfig
35  {
36      /** Constant for a test key. */
37      private static final String KEY = "test/expression/xpath";
38  
39      /** Constant for a value for test properties. */
40      private static final String VALUE = "success";
41  
42      /** The test configuration. */
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       * Tests whether an already existing property can be changed using
54       * setProperty().
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       * Tests setProperty() if the specified path partly exists.
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       * Tests whether setProperty() can be used to add a new attribute.
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       * Tests whether setProperty() can be used to create a completely new key.
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       * Tests whether addProperty() can be used to create more complex
100      * hierarchical structures.
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 }