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.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       * Helper method for checking the combined table structure.
37       *
38       * @param config the config
39       * @return the node for the table element
40       */
41      private ImmutableNode checkTable(final HierarchicalConfiguration<ImmutableNode> config) {
42          assertEquals(1, config.getMaxIndex("database.tables.table"));
43          final HierarchicalConfiguration<ImmutableNode> c = config.configurationAt("database.tables.table(0)");
44          assertEquals("documents", c.getString("name"));
45          assertEquals(2, c.getMaxIndex("fields.field.name"));
46          assertEquals("docname", c.getString("fields.field(1).name"));
47  
48          final NodeHandler<ImmutableNode> nodeHandler = config.getNodeModel().getNodeHandler();
49          final List<QueryResult<ImmutableNode>> nds = config.getExpressionEngine().query(nodeHandler.getRootNode(), "database.tables.table", nodeHandler);
50          assertFalse(nds.isEmpty());
51          assertFalse(nds.get(0).isAttributeResult());
52          return nds.get(0).getNode();
53      }
54  
55      /**
56       * Creates the combiner.
57       *
58       * @return the combiner
59       */
60      @Override
61      protected NodeCombiner createCombiner() {
62          return new MergeCombiner();
63      }
64  
65      /**
66       * Tests combination of attributes.
67       */
68      @Test
69      public void testAttributes() throws ConfigurationException {
70          final BaseHierarchicalConfiguration config = createCombinedConfiguration();
71          assertEquals(1, config.getInt("gui.level[@min]"));
72          assertEquals(2, config.getInt("gui.level[@default]"));
73          assertEquals(0, config.getMaxIndex("database.tables.table(0)[@id]"));
74          assertEquals(1, config.getInt("database.tables.table(0)[@id]"));
75      }
76  
77      /**
78       * Tests the combination of the table structure. With the merge combiner both table 1 and table 2 should be present.
79       */
80      @Test
81      public void testCombinedTable() throws ConfigurationException {
82          checkTable(createCombinedConfiguration());
83      }
84  
85      /**
86       * Tests if a list from the first node structure overrides a list in the second structure.
87       */
88      @Test
89      public void testListFromFirstStructure() throws ConfigurationException {
90          final BaseHierarchicalConfiguration config = createCombinedConfiguration();
91          assertEquals(0, config.getMaxIndex("net.service.url"));
92          assertEquals("http://service1.org", config.getString("net.service.url"));
93          assertFalse(config.containsKey("net.service.url[@type]"));
94      }
95  
96      /**
97       * Tests if a list from the second structure is added if it is not defined in the first structure.
98       */
99      @Test
100     public void testListFromSecondStructure() throws ConfigurationException {
101         final BaseHierarchicalConfiguration config = createCombinedConfiguration();
102         assertEquals(3, config.getMaxIndex("net.server.url"));
103         assertEquals("http://testsvr.com", config.getString("net.server.url(2)"));
104     }
105 
106     @Test
107     public void testMerge() throws ConfigurationException {
108         // combiner.setDebugStream(System.out);
109         final BaseHierarchicalConfiguration config = createCombinedConfiguration();
110         config.setExpressionEngine(new XPathExpressionEngine());
111         assertEquals(3, config.getMaxIndex("Channels/Channel"));
112         assertEquals("My Channel", config.getString("Channels/Channel[@id='1']/Name"));
113         assertEquals("half", config.getString("Channels/Channel[@id='1']/@type"));
114         assertEquals("Channel 2", config.getString("Channels/Channel[@id='2']/Name"));
115         assertEquals("full", config.getString("Channels/Channel[@id='2']/@type"));
116         assertEquals("test 1 data", config.getString("Channels/Channel[@id='1']/ChannelData"));
117         assertEquals("test 2 data", config.getString("Channels/Channel[@id='2']/ChannelData"));
118         assertEquals("more test 2 data", config.getString("Channels/Channel[@id='2']/MoreChannelData"));
119 
120     }
121 
122     /**
123      * Tests whether property values are correctly overridden.
124      */
125     @Test
126     public void testOverrideValues() throws ConfigurationException {
127         final BaseHierarchicalConfiguration config = createCombinedConfiguration();
128         assertEquals("Admin", config.getString("base.services.security.login.user"));
129         assertEquals("default", config.getString("base.services.security.login.user[@type]"));
130         assertNull(config.getString("base.services.security.login.passwd"));
131         assertEquals("secret", config.getString("base.services.security.login.passwd[@type]"));
132     }
133 
134     /**
135      * Tests combination of simple elements.
136      */
137     @Test
138     public void testSimpleValues() throws ConfigurationException {
139         final BaseHierarchicalConfiguration config = createCombinedConfiguration();
140         assertEquals(0, config.getMaxIndex("gui.bgcolor"));
141         assertEquals("green", config.getString("gui.bgcolor"));
142         assertEquals("yellow", config.getString("gui.selcolor"));
143         assertEquals("blue", config.getString("gui.fgcolor"));
144         assertEquals(1, config.getInt("gui.level"));
145     }
146 }