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