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.assertNull;
22  
23  import java.util.List;
24  
25  import org.apache.commons.configuration.ConfigurationException;
26  import org.apache.commons.configuration.HierarchicalConfiguration;
27  import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
28  import org.junit.Test;
29  
30  /**
31   * Test class for MergeCombiner.
32   *
33   * @version $Id: TestMergeCombiner.java 1225911 2011-12-30 20:19:10Z oheger $
34   */
35  public class TestMergeCombiner extends AbstractCombinerTest
36  {
37      /**
38       * Creates the combiner.
39       *
40       * @return the combiner
41       */
42      @Override
43      protected NodeCombiner createCombiner()
44      {
45          return new MergeCombiner();
46      }
47  
48      /**
49       * Tests combination of simple elements.
50       */
51      @Test
52      public void testSimpleValues() throws ConfigurationException
53      {
54          HierarchicalConfiguration config = createCombinedConfiguration();
55          assertEquals("Wrong number of bgcolors", 0, config
56                  .getMaxIndex("gui.bgcolor"));
57          assertEquals("Wrong bgcolor", "green", config.getString("gui.bgcolor"));
58          assertEquals("Wrong selcolor", "yellow", config
59                  .getString("gui.selcolor"));
60          assertEquals("Wrong fgcolor", "blue", config.getString("gui.fgcolor"));
61          assertEquals("Wrong level", 1, config.getInt("gui.level"));
62      }
63  
64      /**
65       * Tests combination of attributes.
66       */
67      @Test
68      public void testAttributes() throws ConfigurationException
69      {
70          HierarchicalConfiguration config = createCombinedConfiguration();
71          assertEquals("Wrong value of min attribute", 1, config
72                  .getInt("gui.level[@min]"));
73          assertEquals("Wrong value of default attribute", 2, config
74                  .getInt("gui.level[@default]"));
75          assertEquals("Wrong number of id attributes", 0, config
76                  .getMaxIndex("database.tables.table(0)[@id]"));
77          assertEquals("Wrong value of table id", 1, config
78                  .getInt("database.tables.table(0)[@id]"));
79      }
80  
81      /**
82       * Tests whether property values are correctly overridden.
83       */
84      @Test
85      public void testOverrideValues() throws ConfigurationException
86      {
87          HierarchicalConfiguration config = createCombinedConfiguration();
88          assertEquals("Wrong user", "Admin", config
89                  .getString("base.services.security.login.user"));
90          assertEquals("Wrong user type", "default", config
91                  .getString("base.services.security.login.user[@type]"));
92          assertNull("Wrong password", config.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. With the merge combiner
129      * both table 1 and table 2 should be present.
130      */
131     @Test
132     public void testCombinedTable() throws ConfigurationException
133     {
134         checkTable(createCombinedConfiguration());
135     }
136 
137     @Test
138     public void testMerge() throws ConfigurationException
139     {
140         //combiner.setDebugStream(System.out);
141         HierarchicalConfiguration config = createCombinedConfiguration();
142         config.setExpressionEngine(new XPathExpressionEngine());
143         assertEquals("Wrong number of Channels", 3, config.getMaxIndex("Channels/Channel"));
144         assertEquals("Bad Channel 1 Name", "My Channel",
145                 config.getString("Channels/Channel[@id='1']/Name"));
146         assertEquals("Bad Channel Type", "half",
147                 config.getString("Channels/Channel[@id='1']/@type"));
148         assertEquals("Bad Channel 2 Name", "Channel 2",
149                 config.getString("Channels/Channel[@id='2']/Name"));
150         assertEquals("Bad Channel Type", "full",
151                 config.getString("Channels/Channel[@id='2']/@type"));
152         assertEquals("Bad Channel Data", "test 1 data",
153                 config.getString("Channels/Channel[@id='1']/ChannelData"));
154         assertEquals("Bad Channel Data", "test 2 data",
155                 config.getString("Channels/Channel[@id='2']/ChannelData"));
156         assertEquals("Bad Channel Data", "more test 2 data",
157                 config.getString("Channels/Channel[@id='2']/MoreChannelData"));
158 
159     }
160 
161     /**
162      * Helper method for checking the combined table structure.
163      *
164      * @param config the config
165      * @return the node for the table element
166      */
167     private ConfigurationNode checkTable(HierarchicalConfiguration config)
168     {
169         assertEquals("Wrong number of tables", 1, config
170                 .getMaxIndex("database.tables.table"));
171         HierarchicalConfiguration c = config
172                 .configurationAt("database.tables.table(0)");
173         assertEquals("Wrong table name", "documents", c.getString("name"));
174         assertEquals("Wrong number of fields", 2, c
175                 .getMaxIndex("fields.field.name"));
176         assertEquals("Wrong field", "docname", c
177                 .getString("fields.field(1).name"));
178 
179         List<ConfigurationNode> nds = config.getExpressionEngine().query(config.getRoot(),
180                 "database.tables.table");
181         assertFalse("No node found", nds.isEmpty());
182         return nds.get(0);
183     }
184 }