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