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.assertTrue; 022 023 import java.util.List; 024 025 import org.apache.commons.configuration.ConfigurationException; 026 import org.apache.commons.configuration.HierarchicalConfiguration; 027 import org.junit.Test; 028 029 /** 030 * Test class for OverrideCombiner. 031 * 032 * @version $Id: TestOverrideCombiner.java 1225911 2011-12-30 20:19:10Z oheger $ 033 */ 034 public class TestOverrideCombiner extends AbstractCombinerTest 035 { 036 /** 037 * Creates the combiner. 038 * 039 * @return the combiner 040 */ 041 @Override 042 protected NodeCombiner createCombiner() 043 { 044 return new OverrideCombiner(); 045 } 046 047 /** 048 * Tests combination of simple elements. 049 */ 050 @Test 051 public void testSimpleValues() throws ConfigurationException 052 { 053 HierarchicalConfiguration config = createCombinedConfiguration(); 054 assertEquals("Wrong number of bgcolors", 0, config 055 .getMaxIndex("gui.bgcolor")); 056 assertEquals("Wrong bgcolor", "green", config.getString("gui.bgcolor")); 057 assertEquals("Wrong selcolor", "yellow", config 058 .getString("gui.selcolor")); 059 assertEquals("Wrong fgcolor", "blue", config.getString("gui.fgcolor")); 060 assertEquals("Wrong level", 1, config.getInt("gui.level")); 061 } 062 063 /** 064 * Tests combination of attributes. 065 */ 066 @Test 067 public void testAttributes() throws ConfigurationException 068 { 069 HierarchicalConfiguration config = createCombinedConfiguration(); 070 assertEquals("Wrong value of min attribute", 1, config 071 .getInt("gui.level[@min]")); 072 assertEquals("Wrong value of default attribute", 2, config 073 .getInt("gui.level[@default]")); 074 assertEquals("Wrong number of id attributes", 0, config 075 .getMaxIndex("database.tables.table(0)[@id]")); 076 assertEquals("Wrong value of table id", 1, config 077 .getInt("database.tables.table(0)[@id]")); 078 } 079 080 /** 081 * Tests whether property values are correctly overridden. 082 */ 083 @Test 084 public void testOverrideValues() throws ConfigurationException 085 { 086 HierarchicalConfiguration config = createCombinedConfiguration(); 087 assertEquals("Wrong user", "Admin", config 088 .getString("base.services.security.login.user")); 089 assertEquals("Wrong user type", "default", config 090 .getString("base.services.security.login.user[@type]")); 091 assertEquals("Wrong password", "BeamMeUp", config 092 .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. Because the table node is 129 * not declared as a list node the structures will be combined. But this 130 * won't make any difference because the values in the first table override 131 * the values in the second table. Only the node for the table element will 132 * be a ViewNode. 133 */ 134 @Test 135 public void testCombinedTableNoList() throws ConfigurationException 136 { 137 ConfigurationNode tabNode = checkTable(createCombinedConfiguration()); 138 assertTrue("Node is not a view node", tabNode instanceof ViewNode); 139 } 140 141 /** 142 * Tests the combination of the table structure when the table node is 143 * declared as a list node. In this case the first table structure 144 * completely overrides the second and will be directly added to the 145 * resulting structure. 146 */ 147 @Test 148 public void testCombinedTableList() throws ConfigurationException 149 { 150 combiner.addListNode("table"); 151 ConfigurationNode tabNode = checkTable(createCombinedConfiguration()); 152 assertFalse("Node is a view node", tabNode instanceof ViewNode); 153 } 154 155 /** 156 * Helper method for checking the combined table structure. 157 * 158 * @param config the config 159 * @return the node for the table element 160 */ 161 private ConfigurationNode checkTable(HierarchicalConfiguration config) 162 { 163 assertEquals("Wrong number of tables", 0, config 164 .getMaxIndex("database.tables.table")); 165 HierarchicalConfiguration c = config 166 .configurationAt("database.tables.table"); 167 assertEquals("Wrong table name", "documents", c.getString("name")); 168 assertEquals("Wrong number of fields", 2, c 169 .getMaxIndex("fields.field.name")); 170 assertEquals("Wrong field", "docname", c 171 .getString("fields.field(1).name")); 172 173 List<ConfigurationNode> nds = config.getExpressionEngine().query(config.getRoot(), 174 "database.tables.table"); 175 assertFalse("No node found", nds.isEmpty()); 176 return nds.get(0); 177 } 178 }