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.configuration.tree;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  
22  import org.apache.commons.configuration.ConfigurationException;
23  import org.apache.commons.configuration.HierarchicalConfiguration;
24  import org.junit.Test;
25  
26  /**
27   * Test class for UnionCombiner.
28   *
29   * @version $Id: TestUnionCombiner.java 1225911 2011-12-30 20:19:10Z oheger $
30   */
31  public class TestUnionCombiner extends AbstractCombinerTest
32  {
33      /**
34       * Creates the combiner.
35       *
36       * @return the combiner
37       */
38      @Override
39      protected NodeCombiner createCombiner()
40      {
41          return new UnionCombiner();
42      }
43  
44      /**
45       * Tests combination of simple values (no lists).
46       */
47      @Test
48      public void testSimpleValues() throws ConfigurationException
49      {
50          HierarchicalConfiguration config = createCombinedConfiguration();
51          assertEquals("Too few bgcolors", 1, config.getMaxIndex("gui.bgcolor"));
52          assertEquals("Wrong first color", "green", config
53                  .getString("gui.bgcolor(0)"));
54          assertEquals("Wrong second color", "black", config
55                  .getString("gui.bgcolor(1)"));
56          assertEquals("Wrong number of selcolors", 0, config
57                  .getMaxIndex("gui.selcolor"));
58          assertEquals("Wrong selcolor", "yellow", config
59                  .getString("gui.selcolor"));
60      }
61  
62      /**
63       * Tests combinations of elements with attributes.
64       */
65      @Test
66      public void testSimpleValuesWithAttributes() throws ConfigurationException
67      {
68          HierarchicalConfiguration config = createCombinedConfiguration();
69          assertEquals("Too few level elements", 1, config
70                  .getMaxIndex("gui.level"));
71          assertEquals("Wrong value of first element", 1, config
72                  .getInt("gui.level(0)"));
73          assertEquals("Wrong value of second element", 4, config
74                  .getInt("gui.level(1)"));
75          assertEquals("Wrong value of first attribute", 2, config
76                  .getInt("gui.level(0)[@default]"));
77          assertFalse("Found wrong attribute", config
78                  .containsKey("gui.level(0)[@min]"));
79          assertEquals("Wrong value of second attribute", 1, config
80                  .getInt("gui.level(1)[@min]"));
81      }
82  
83      /**
84       * Tests combination of attributes.
85       */
86      @Test
87      public void testAttributes() throws ConfigurationException
88      {
89          HierarchicalConfiguration config = createCombinedConfiguration();
90          assertEquals("Too few attributes", 1, config
91                  .getMaxIndex("database.tables.table(0)[@id]"));
92          assertEquals("Wrong value of first attribute", 1, config
93                  .getInt("database.tables.table(0)[@id](0)"));
94          assertEquals("Wrong value of second attribute", 2, config
95                  .getInt("database.tables.table(0)[@id](1)"));
96      }
97  
98      /**
99       * Tests combination of lists.
100      */
101     @Test
102     public void testLists() throws ConfigurationException
103     {
104         HierarchicalConfiguration config = createCombinedConfiguration();
105         assertEquals("Too few list elements", 2, config
106                 .getMaxIndex("net.service.url"));
107         assertEquals("Wrong first service", "http://service1.org", config
108                 .getString("net.service.url(0)"));
109         assertEquals("Wrong second service", "http://service2.org", config
110                 .getString("net.service.url(1)"));
111         assertEquals("Wrong service attribute", 2, config
112                 .getInt("net.service.url(2)[@type]"));
113         assertEquals("Wrong number of server elements", 3, config
114                 .getMaxIndex("net.server.url"));
115     }
116 
117     /**
118      * Tests combining a list of tables. Per default the table elements will be
119      * combined. But if they are defined as list elements, the resulting tree
120      * should contain two table nodes.
121      */
122     @Test
123     public void testTableList() throws ConfigurationException
124     {
125         combiner.addListNode("table");
126         HierarchicalConfiguration config = createCombinedConfiguration();
127         assertEquals("Wrong name of first table", "documents", config
128                 .getString("database.tables.table(0).name"));
129         assertEquals("Wrong id of first table", 1, config
130                 .getInt("database.tables.table(0)[@id]"));
131         assertEquals("Wrong name of second table", "tasks", config
132                 .getString("database.tables.table(1).name"));
133         assertEquals("Wrong id of second table", 2, config
134                 .getInt("database.tables.table(1)[@id]"));
135     }
136 }