001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.configuration.tree;
018
019 import static org.junit.Assert.assertEquals;
020 import static org.junit.Assert.assertFalse;
021
022 import org.apache.commons.configuration.ConfigurationException;
023 import org.apache.commons.configuration.HierarchicalConfiguration;
024 import org.junit.Test;
025
026 /**
027 * Test class for UnionCombiner.
028 *
029 * @version $Id: TestUnionCombiner.java 1225911 2011-12-30 20:19:10Z oheger $
030 */
031 public class TestUnionCombiner extends AbstractCombinerTest
032 {
033 /**
034 * Creates the combiner.
035 *
036 * @return the combiner
037 */
038 @Override
039 protected NodeCombiner createCombiner()
040 {
041 return new UnionCombiner();
042 }
043
044 /**
045 * Tests combination of simple values (no lists).
046 */
047 @Test
048 public void testSimpleValues() throws ConfigurationException
049 {
050 HierarchicalConfiguration config = createCombinedConfiguration();
051 assertEquals("Too few bgcolors", 1, config.getMaxIndex("gui.bgcolor"));
052 assertEquals("Wrong first color", "green", config
053 .getString("gui.bgcolor(0)"));
054 assertEquals("Wrong second color", "black", config
055 .getString("gui.bgcolor(1)"));
056 assertEquals("Wrong number of selcolors", 0, config
057 .getMaxIndex("gui.selcolor"));
058 assertEquals("Wrong selcolor", "yellow", config
059 .getString("gui.selcolor"));
060 }
061
062 /**
063 * Tests combinations of elements with attributes.
064 */
065 @Test
066 public void testSimpleValuesWithAttributes() throws ConfigurationException
067 {
068 HierarchicalConfiguration config = createCombinedConfiguration();
069 assertEquals("Too few level elements", 1, config
070 .getMaxIndex("gui.level"));
071 assertEquals("Wrong value of first element", 1, config
072 .getInt("gui.level(0)"));
073 assertEquals("Wrong value of second element", 4, config
074 .getInt("gui.level(1)"));
075 assertEquals("Wrong value of first attribute", 2, config
076 .getInt("gui.level(0)[@default]"));
077 assertFalse("Found wrong attribute", config
078 .containsKey("gui.level(0)[@min]"));
079 assertEquals("Wrong value of second attribute", 1, config
080 .getInt("gui.level(1)[@min]"));
081 }
082
083 /**
084 * Tests combination of attributes.
085 */
086 @Test
087 public void testAttributes() throws ConfigurationException
088 {
089 HierarchicalConfiguration config = createCombinedConfiguration();
090 assertEquals("Too few attributes", 1, config
091 .getMaxIndex("database.tables.table(0)[@id]"));
092 assertEquals("Wrong value of first attribute", 1, config
093 .getInt("database.tables.table(0)[@id](0)"));
094 assertEquals("Wrong value of second attribute", 2, config
095 .getInt("database.tables.table(0)[@id](1)"));
096 }
097
098 /**
099 * 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 }