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