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.assertNull;
22  
23  import java.util.List;
24  
25  import org.apache.commons.configuration2.BaseHierarchicalConfiguration;
26  import org.apache.commons.configuration2.HierarchicalConfiguration;
27  import org.apache.commons.configuration2.ex.ConfigurationException;
28  import org.apache.commons.configuration2.tree.xpath.XPathExpressionEngine;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Test class for MergeCombiner.
33   */
34  public class TestMergeCombiner extends AbstractCombinerTest {
35  
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 HierarchicalConfiguration<ImmutableNode> config) {
43          assertEquals(1, config.getMaxIndex("database.tables.table"));
44          final HierarchicalConfiguration<ImmutableNode> c = config.configurationAt("database.tables.table(0)");
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 MergeCombiner();
64      }
65  
66      /**
67       * Tests combination of attributes.
68       */
69      @Test
70      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. With the merge combiner both table 1 and table 2 should be present.
80       */
81      @Test
82      void testCombinedTable() throws ConfigurationException {
83          checkTable(createCombinedConfiguration());
84      }
85  
86      /**
87       * Tests if a list from the first node structure overrides a list in the second structure.
88       */
89      @Test
90      void testListFromFirstStructure() throws ConfigurationException {
91          final BaseHierarchicalConfiguration config = createCombinedConfiguration();
92          assertEquals(0, config.getMaxIndex("net.service.url"));
93          assertEquals("http://service1.org", config.getString("net.service.url"));
94          assertFalse(config.containsKey("net.service.url[@type]"));
95      }
96  
97      /**
98       * Tests if a list from the second structure is added if it is not defined in the first structure.
99       */
100     @Test
101     void testListFromSecondStructure() throws ConfigurationException {
102         final BaseHierarchicalConfiguration config = createCombinedConfiguration();
103         assertEquals(3, config.getMaxIndex("net.server.url"));
104         assertEquals("http://testsvr.com", config.getString("net.server.url(2)"));
105     }
106 
107     @Test
108     void testMerge() throws ConfigurationException {
109         // combiner.setDebugStream(System.out);
110         final BaseHierarchicalConfiguration config = createCombinedConfiguration();
111         config.setExpressionEngine(new XPathExpressionEngine());
112         assertEquals(3, config.getMaxIndex("Channels/Channel"));
113         assertEquals("My Channel", config.getString("Channels/Channel[@id='1']/Name"));
114         assertEquals("half", config.getString("Channels/Channel[@id='1']/@type"));
115         assertEquals("Channel 2", config.getString("Channels/Channel[@id='2']/Name"));
116         assertEquals("full", config.getString("Channels/Channel[@id='2']/@type"));
117         assertEquals("test 1 data", config.getString("Channels/Channel[@id='1']/ChannelData"));
118         assertEquals("test 2 data", config.getString("Channels/Channel[@id='2']/ChannelData"));
119         assertEquals("more test 2 data", config.getString("Channels/Channel[@id='2']/MoreChannelData"));
120 
121     }
122 
123     /**
124      * Tests whether property values are correctly overridden.
125      */
126     @Test
127     void testOverrideValues() throws ConfigurationException {
128         final BaseHierarchicalConfiguration config = createCombinedConfiguration();
129         assertEquals("Admin", config.getString("base.services.security.login.user"));
130         assertEquals("default", config.getString("base.services.security.login.user[@type]"));
131         assertNull(config.getString("base.services.security.login.passwd"));
132         assertEquals("secret", config.getString("base.services.security.login.passwd[@type]"));
133     }
134 
135     /**
136      * Tests combination of simple elements.
137      */
138     @Test
139     void testSimpleValues() throws ConfigurationException {
140         final BaseHierarchicalConfiguration config = createCombinedConfiguration();
141         assertEquals(0, config.getMaxIndex("gui.bgcolor"));
142         assertEquals("green", config.getString("gui.bgcolor"));
143         assertEquals("yellow", config.getString("gui.selcolor"));
144         assertEquals("blue", config.getString("gui.fgcolor"));
145         assertEquals(1, config.getInt("gui.level"));
146     }
147 }