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 java.util.List;
020:
021: import org.apache.commons.configuration.ConfigurationException;
022: import org.apache.commons.configuration.HierarchicalConfiguration;
023:
024: /**
025: * Test class for OverrideCombiner.
026: *
027: * @version $Id: TestOverrideCombiner.java 439648 2006-09-02 20:42:10Z oheger $
028: */
029: public class TestOverrideCombiner extends AbstractCombinerTest {
030: /**
031: * Creates the combiner.
032: *
033: * @return the combiner
034: */
035: protected NodeCombiner createCombiner() {
036: return new OverrideCombiner();
037: }
038:
039: /**
040: * Tests combination of simple elements.
041: */
042: public void testSimpleValues() throws ConfigurationException {
043: HierarchicalConfiguration config = createCombinedConfiguration();
044: assertEquals("Wrong number of bgcolors", 0, config
045: .getMaxIndex("gui.bgcolor"));
046: assertEquals("Wrong bgcolor", "green", config
047: .getString("gui.bgcolor"));
048: assertEquals("Wrong selcolor", "yellow", config
049: .getString("gui.selcolor"));
050: assertEquals("Wrong fgcolor", "blue", config
051: .getString("gui.fgcolor"));
052: assertEquals("Wrong level", 1, config.getInt("gui.level"));
053: }
054:
055: /**
056: * Tests combination of attributes.
057: */
058: public void testAttributes() throws ConfigurationException {
059: HierarchicalConfiguration config = createCombinedConfiguration();
060: assertEquals("Wrong value of min attribute", 1, config
061: .getInt("gui.level[@min]"));
062: assertEquals("Wrong value of default attribute", 2, config
063: .getInt("gui.level[@default]"));
064: assertEquals("Wrong number of id attributes", 0, config
065: .getMaxIndex("database.tables.table(0)[@id]"));
066: assertEquals("Wrong value of table id", 1, config
067: .getInt("database.tables.table(0)[@id]"));
068: }
069:
070: /**
071: * Tests whether property values are correctly overridden.
072: */
073: public void testOverrideValues() throws ConfigurationException {
074: HierarchicalConfiguration config = createCombinedConfiguration();
075: assertEquals("Wrong user", "Admin", config
076: .getString("base.services.security.login.user"));
077: assertEquals("Wrong user type", "default", config
078: .getString("base.services.security.login.user[@type]"));
079: assertEquals("Wrong password", "BeamMeUp", config
080: .getString("base.services.security.login.passwd"));
081: assertEquals(
082: "Wrong password type",
083: "secret",
084: config
085: .getString("base.services.security.login.passwd[@type]"));
086: }
087:
088: /**
089: * Tests if a list from the first node structure overrides a list in the
090: * second structure.
091: */
092: public void testListFromFirstStructure()
093: throws ConfigurationException {
094: HierarchicalConfiguration config = createCombinedConfiguration();
095: assertEquals("Wrong number of services", 0, config
096: .getMaxIndex("net.service.url"));
097: assertEquals("Wrong service", "http://service1.org", config
098: .getString("net.service.url"));
099: assertFalse("Type attribute available", config
100: .containsKey("net.service.url[@type]"));
101: }
102:
103: /**
104: * Tests if a list from the second structure is added if it is not defined
105: * in the first structure.
106: */
107: public void testListFromSecondStructure()
108: throws ConfigurationException {
109: HierarchicalConfiguration config = createCombinedConfiguration();
110: assertEquals("Wrong number of servers", 3, config
111: .getMaxIndex("net.server.url"));
112: assertEquals("Wrong server", "http://testsvr.com", config
113: .getString("net.server.url(2)"));
114: }
115:
116: /**
117: * Tests the combination of the table structure. Because the table node is
118: * not declared as a list node the structures will be combined. But this
119: * won't make any difference because the values in the first table override
120: * the values in the second table. Only the node for the table element will
121: * be a ViewNode.
122: */
123: public void testCombinedTableNoList() throws ConfigurationException {
124: ConfigurationNode tabNode = checkTable(createCombinedConfiguration());
125: assertTrue("Node is not a view node",
126: tabNode instanceof ViewNode);
127: }
128:
129: /**
130: * Tests the combination of the table structure when the table node is
131: * declared as a list node. In this case the first table structure
132: * completely overrides the second and will be directly added to the
133: * resulting structure.
134: */
135: public void testCombinedTableList() throws ConfigurationException {
136: combiner.addListNode("table");
137: ConfigurationNode tabNode = checkTable(createCombinedConfiguration());
138: assertFalse("Node is a view node", tabNode instanceof ViewNode);
139: }
140:
141: /**
142: * Helper method for checking the combined table structure.
143: *
144: * @param config the config
145: * @return the node for the table element
146: */
147: private ConfigurationNode checkTable(
148: HierarchicalConfiguration config) {
149: assertEquals("Wrong number of tables", 0, config
150: .getMaxIndex("database.tables.table"));
151: HierarchicalConfiguration c = config
152: .configurationAt("database.tables.table");
153: assertEquals("Wrong table name", "documents", c
154: .getString("name"));
155: assertEquals("Wrong number of fields", 2, c
156: .getMaxIndex("fields.field.name"));
157: assertEquals("Wrong field", "docname", c
158: .getString("fields.field(1).name"));
159:
160: List nds = config.getExpressionEngine().query(config.getRoot(),
161: "database.tables.table");
162: assertFalse("No node found", nds.isEmpty());
163: return (ConfigurationNode) nds.get(0);
164: }
165: }
|