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  
22  import org.apache.commons.configuration2.BaseHierarchicalConfiguration;
23  import org.apache.commons.configuration2.ex.ConfigurationException;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Test class for UnionCombiner.
28   */
29  public class TestUnionCombiner extends AbstractCombinerTest {
30  
31      /**
32       * Creates the combiner.
33       *
34       * @return the combiner
35       */
36      @Override
37      protected NodeCombiner createCombiner() {
38          return new UnionCombiner();
39      }
40  
41      /**
42       * Tests combination of attributes.
43       */
44      @Test
45      void testAttributes() throws ConfigurationException {
46          final BaseHierarchicalConfiguration config = createCombinedConfiguration();
47          assertEquals(0, config.getMaxIndex("database.tables.table(0)[@id]"));
48          assertEquals(1, config.getInt("database.tables.table(0)[@id](0)"));
49      }
50  
51      /**
52       * Tests combination of lists.
53       */
54      @Test
55      void testLists() throws ConfigurationException {
56          final BaseHierarchicalConfiguration config = createCombinedConfiguration();
57          assertEquals(2, config.getMaxIndex("net.service.url"));
58          assertEquals("http://service1.org", config.getString("net.service.url(0)"));
59          assertEquals("http://service2.org", config.getString("net.service.url(1)"));
60          assertEquals(2, config.getInt("net.service.url(2)[@type]"));
61          assertEquals(3, config.getMaxIndex("net.server.url"));
62      }
63  
64      /**
65       * Tests combination of simple values (no lists).
66       */
67      @Test
68      void testSimpleValues() throws ConfigurationException {
69          final BaseHierarchicalConfiguration config = createCombinedConfiguration();
70          assertEquals(1, config.getMaxIndex("gui.bgcolor"));
71          assertEquals("green", config.getString("gui.bgcolor(0)"));
72          assertEquals("black", config.getString("gui.bgcolor(1)"));
73          assertEquals(0, config.getMaxIndex("gui.selcolor"));
74          assertEquals("yellow", config.getString("gui.selcolor"));
75      }
76  
77      /**
78       * Tests combinations of elements with attributes.
79       */
80      @Test
81      void testSimpleValuesWithAttributes() throws ConfigurationException {
82          final BaseHierarchicalConfiguration config = createCombinedConfiguration();
83          assertEquals(1, config.getMaxIndex("gui.level"));
84          assertEquals(1, config.getInt("gui.level(0)"));
85          assertEquals(4, config.getInt("gui.level(1)"));
86          assertEquals(2, config.getInt("gui.level(0)[@default]"));
87          assertFalse(config.containsKey("gui.level(0)[@min]"));
88          assertEquals(1, config.getInt("gui.level(1)[@min]"));
89      }
90  
91      /**
92       * Tests combining a list of tables. Per default the table elements will be combined. But if they are defined as list
93       * elements, the resulting tree should contain two table nodes.
94       */
95      @Test
96      void testTableList() throws ConfigurationException {
97          combiner.addListNode("table");
98          final BaseHierarchicalConfiguration config = createCombinedConfiguration();
99          assertEquals("documents", config.getString("database.tables.table(0).name"));
100         assertEquals(1, config.getInt("database.tables.table(0)[@id]"));
101         assertEquals("tasks", config.getString("database.tables.table(1).name"));
102         assertEquals(2, config.getInt("database.tables.table(1)[@id]"));
103     }
104 }