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;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.util.List;
24  
25  import org.apache.commons.configuration.ConfigurationException;
26  import org.apache.commons.configuration.HierarchicalConfiguration;
27  import org.junit.Test;
28  
29  /**
30   * Test class for OverrideCombiner.
31   *
32   * @version $Id: TestOverrideCombiner.java 1225911 2011-12-30 20:19:10Z oheger $
33   */
34  public class TestOverrideCombiner extends AbstractCombinerTest
35  {
36      /**
37       * Creates the combiner.
38       *
39       * @return the combiner
40       */
41      @Override
42      protected NodeCombiner createCombiner()
43      {
44          return new OverrideCombiner();
45      }
46  
47      /**
48       * Tests combination of simple elements.
49       */
50      @Test
51      public void testSimpleValues() throws ConfigurationException
52      {
53          HierarchicalConfiguration config = createCombinedConfiguration();
54          assertEquals("Wrong number of bgcolors", 0, config
55                  .getMaxIndex("gui.bgcolor"));
56          assertEquals("Wrong bgcolor", "green", config.getString("gui.bgcolor"));
57          assertEquals("Wrong selcolor", "yellow", config
58                  .getString("gui.selcolor"));
59          assertEquals("Wrong fgcolor", "blue", config.getString("gui.fgcolor"));
60          assertEquals("Wrong level", 1, config.getInt("gui.level"));
61      }
62  
63      /**
64       * Tests combination of attributes.
65       */
66      @Test
67      public void testAttributes() throws ConfigurationException
68      {
69          HierarchicalConfiguration config = createCombinedConfiguration();
70          assertEquals("Wrong value of min attribute", 1, config
71                  .getInt("gui.level[@min]"));
72          assertEquals("Wrong value of default attribute", 2, config
73                  .getInt("gui.level[@default]"));
74          assertEquals("Wrong number of id attributes", 0, config
75                  .getMaxIndex("database.tables.table(0)[@id]"));
76          assertEquals("Wrong value of table id", 1, config
77                  .getInt("database.tables.table(0)[@id]"));
78      }
79  
80      /**
81       * Tests whether property values are correctly overridden.
82       */
83      @Test
84      public void testOverrideValues() throws ConfigurationException
85      {
86          HierarchicalConfiguration config = createCombinedConfiguration();
87          assertEquals("Wrong user", "Admin", config
88                  .getString("base.services.security.login.user"));
89          assertEquals("Wrong user type", "default", config
90                  .getString("base.services.security.login.user[@type]"));
91          assertEquals("Wrong password", "BeamMeUp", config
92                  .getString("base.services.security.login.passwd"));
93          assertEquals("Wrong password type", "secret", config
94                  .getString("base.services.security.login.passwd[@type]"));
95      }
96  
97      /**
98       * Tests if a list from the first node structure overrides a list in the
99       * second structure.
100      */
101     @Test
102     public void testListFromFirstStructure() throws ConfigurationException
103     {
104         HierarchicalConfiguration config = createCombinedConfiguration();
105         assertEquals("Wrong number of services", 0, config
106                 .getMaxIndex("net.service.url"));
107         assertEquals("Wrong service", "http://service1.org", config
108                 .getString("net.service.url"));
109         assertFalse("Type attribute available", config
110                 .containsKey("net.service.url[@type]"));
111     }
112 
113     /**
114      * Tests if a list from the second structure is added if it is not defined
115      * in the first structure.
116      */
117     @Test
118     public void testListFromSecondStructure() throws ConfigurationException
119     {
120         HierarchicalConfiguration config = createCombinedConfiguration();
121         assertEquals("Wrong number of servers", 3, config
122                 .getMaxIndex("net.server.url"));
123         assertEquals("Wrong server", "http://testsvr.com", config
124                 .getString("net.server.url(2)"));
125     }
126 
127     /**
128      * Tests the combination of the table structure. Because the table node is
129      * not declared as a list node the structures will be combined. But this
130      * won't make any difference because the values in the first table override
131      * the values in the second table. Only the node for the table element will
132      * be a ViewNode.
133      */
134     @Test
135     public void testCombinedTableNoList() throws ConfigurationException
136     {
137         ConfigurationNode tabNode = checkTable(createCombinedConfiguration());
138         assertTrue("Node is not a view node", tabNode instanceof ViewNode);
139     }
140 
141     /**
142      * Tests the combination of the table structure when the table node is
143      * declared as a list node. In this case the first table structure
144      * completely overrides the second and will be directly added to the
145      * resulting structure.
146      */
147     @Test
148     public void testCombinedTableList() throws ConfigurationException
149     {
150         combiner.addListNode("table");
151         ConfigurationNode tabNode = checkTable(createCombinedConfiguration());
152         assertFalse("Node is a view node", tabNode instanceof ViewNode);
153     }
154 
155     /**
156      * Helper method for checking the combined table structure.
157      *
158      * @param config the config
159      * @return the node for the table element
160      */
161     private ConfigurationNode checkTable(HierarchicalConfiguration config)
162     {
163         assertEquals("Wrong number of tables", 0, config
164                 .getMaxIndex("database.tables.table"));
165         HierarchicalConfiguration c = config
166                 .configurationAt("database.tables.table");
167         assertEquals("Wrong table name", "documents", c.getString("name"));
168         assertEquals("Wrong number of fields", 2, c
169                 .getMaxIndex("fields.field.name"));
170         assertEquals("Wrong field", "docname", c
171                 .getString("fields.field(1).name"));
172 
173         List<ConfigurationNode> nds = config.getExpressionEngine().query(config.getRoot(),
174                 "database.tables.table");
175         assertFalse("No node found", nds.isEmpty());
176         return nds.get(0);
177     }
178 }