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 import static org.junit.Assert.assertNull; 022 023 import java.util.List; 024 025 import org.apache.commons.configuration.ConfigurationException; 026 import org.apache.commons.configuration.HierarchicalConfiguration; 027 import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine; 028 import org.junit.Test; 029 030 /** 031 * Test class for MergeCombiner. 032 * 033 * @version $Id: TestMergeCombiner.java 1225911 2011-12-30 20:19:10Z oheger $ 034 */ 035 public class TestMergeCombiner extends AbstractCombinerTest 036 { 037 /** 038 * Creates the combiner. 039 * 040 * @return the combiner 041 */ 042 @Override 043 protected NodeCombiner createCombiner() 044 { 045 return new MergeCombiner(); 046 } 047 048 /** 049 * Tests combination of simple elements. 050 */ 051 @Test 052 public void testSimpleValues() throws ConfigurationException 053 { 054 HierarchicalConfiguration config = createCombinedConfiguration(); 055 assertEquals("Wrong number of bgcolors", 0, config 056 .getMaxIndex("gui.bgcolor")); 057 assertEquals("Wrong bgcolor", "green", config.getString("gui.bgcolor")); 058 assertEquals("Wrong selcolor", "yellow", config 059 .getString("gui.selcolor")); 060 assertEquals("Wrong fgcolor", "blue", config.getString("gui.fgcolor")); 061 assertEquals("Wrong level", 1, config.getInt("gui.level")); 062 } 063 064 /** 065 * Tests combination of attributes. 066 */ 067 @Test 068 public void testAttributes() throws ConfigurationException 069 { 070 HierarchicalConfiguration config = createCombinedConfiguration(); 071 assertEquals("Wrong value of min attribute", 1, config 072 .getInt("gui.level[@min]")); 073 assertEquals("Wrong value of default attribute", 2, config 074 .getInt("gui.level[@default]")); 075 assertEquals("Wrong number of id attributes", 0, config 076 .getMaxIndex("database.tables.table(0)[@id]")); 077 assertEquals("Wrong value of table id", 1, config 078 .getInt("database.tables.table(0)[@id]")); 079 } 080 081 /** 082 * Tests whether property values are correctly overridden. 083 */ 084 @Test 085 public void testOverrideValues() throws ConfigurationException 086 { 087 HierarchicalConfiguration config = createCombinedConfiguration(); 088 assertEquals("Wrong user", "Admin", config 089 .getString("base.services.security.login.user")); 090 assertEquals("Wrong user type", "default", config 091 .getString("base.services.security.login.user[@type]")); 092 assertNull("Wrong password", config.getString("base.services.security.login.passwd")); 093 assertEquals("Wrong password type", "secret", config 094 .getString("base.services.security.login.passwd[@type]")); 095 } 096 097 /** 098 * Tests if a list from the first node structure overrides a list in the 099 * second structure. 100 */ 101 @Test 102 public void testListFromFirstStructure() throws ConfigurationException 103 { 104 HierarchicalConfiguration config = createCombinedConfiguration(); 105 assertEquals("Wrong number of services", 0, config 106 .getMaxIndex("net.service.url")); 107 assertEquals("Wrong service", "http://service1.org", config 108 .getString("net.service.url")); 109 assertFalse("Type attribute available", config 110 .containsKey("net.service.url[@type]")); 111 } 112 113 /** 114 * Tests if a list from the second structure is added if it is not defined 115 * in the first structure. 116 */ 117 @Test 118 public void testListFromSecondStructure() throws ConfigurationException 119 { 120 HierarchicalConfiguration config = createCombinedConfiguration(); 121 assertEquals("Wrong number of servers", 3, config 122 .getMaxIndex("net.server.url")); 123 assertEquals("Wrong server", "http://testsvr.com", config 124 .getString("net.server.url(2)")); 125 } 126 127 /** 128 * Tests the combination of the table structure. With the merge combiner 129 * both table 1 and table 2 should be present. 130 */ 131 @Test 132 public void testCombinedTable() throws ConfigurationException 133 { 134 checkTable(createCombinedConfiguration()); 135 } 136 137 @Test 138 public void testMerge() throws ConfigurationException 139 { 140 //combiner.setDebugStream(System.out); 141 HierarchicalConfiguration config = createCombinedConfiguration(); 142 config.setExpressionEngine(new XPathExpressionEngine()); 143 assertEquals("Wrong number of Channels", 3, config.getMaxIndex("Channels/Channel")); 144 assertEquals("Bad Channel 1 Name", "My Channel", 145 config.getString("Channels/Channel[@id='1']/Name")); 146 assertEquals("Bad Channel Type", "half", 147 config.getString("Channels/Channel[@id='1']/@type")); 148 assertEquals("Bad Channel 2 Name", "Channel 2", 149 config.getString("Channels/Channel[@id='2']/Name")); 150 assertEquals("Bad Channel Type", "full", 151 config.getString("Channels/Channel[@id='2']/@type")); 152 assertEquals("Bad Channel Data", "test 1 data", 153 config.getString("Channels/Channel[@id='1']/ChannelData")); 154 assertEquals("Bad Channel Data", "test 2 data", 155 config.getString("Channels/Channel[@id='2']/ChannelData")); 156 assertEquals("Bad Channel Data", "more test 2 data", 157 config.getString("Channels/Channel[@id='2']/MoreChannelData")); 158 159 } 160 161 /** 162 * Helper method for checking the combined table structure. 163 * 164 * @param config the config 165 * @return the node for the table element 166 */ 167 private ConfigurationNode checkTable(HierarchicalConfiguration config) 168 { 169 assertEquals("Wrong number of tables", 1, config 170 .getMaxIndex("database.tables.table")); 171 HierarchicalConfiguration c = config 172 .configurationAt("database.tables.table(0)"); 173 assertEquals("Wrong table name", "documents", c.getString("name")); 174 assertEquals("Wrong number of fields", 2, c 175 .getMaxIndex("fields.field.name")); 176 assertEquals("Wrong field", "docname", c 177 .getString("fields.field(1).name")); 178 179 List<ConfigurationNode> nds = config.getExpressionEngine().query(config.getRoot(), 180 "database.tables.table"); 181 assertFalse("No node found", nds.isEmpty()); 182 return nds.get(0); 183 } 184 }