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.assertFalse;
020    import static org.junit.Assert.assertTrue;
021    
022    import java.io.File;
023    
024    import org.apache.commons.configuration.ConfigurationAssert;
025    import org.apache.commons.configuration.ConfigurationException;
026    import org.apache.commons.configuration.HierarchicalConfiguration;
027    import org.apache.commons.configuration.XMLConfiguration;
028    import org.junit.Before;
029    import org.junit.Test;
030    
031    /**
032     * A base class for testing combiner implementations. This base class provides
033     * some functionality for loading the test configurations, which are to be
034     * combined. Concrete sub classes only need to create the correct combiner
035     * object.
036     *
037     * @version $Id: AbstractCombinerTest.java 1225911 2011-12-30 20:19:10Z oheger $
038     */
039    public abstract class AbstractCombinerTest
040    {
041        /** Constant for the first test configuration. */
042        static File CONF1 = ConfigurationAssert.getTestFile("testcombine1.xml");
043    
044        /** Constant for the second test configuration. */
045        static File CONF2 = ConfigurationAssert.getTestFile("testcombine2.xml");
046    
047        /** The combiner to be tested. */
048        protected NodeCombiner combiner;
049    
050        @Before
051        public void setUp() throws Exception
052        {
053            combiner = createCombiner();
054        }
055    
056        /**
057         * Creates the combiner to be tested. This method is called by
058         * <code>setUp()</code>. It must be implemented in concrete sub classes.
059         *
060         * @return the combiner to be tested
061         */
062        protected abstract NodeCombiner createCombiner();
063    
064        /**
065         * Constructs a union configuration based on the source configurations.
066         *
067         * @return the union configuration
068         * @throws ConfigurationException if an error occurs
069         */
070        protected HierarchicalConfiguration createCombinedConfiguration()
071                throws ConfigurationException
072        {
073            XMLConfiguration conf1 = new XMLConfiguration(CONF1);
074            XMLConfiguration conf2 = new XMLConfiguration(CONF2);
075            ConfigurationNode cn = combiner.combine(conf1.getRootNode(), conf2
076                    .getRootNode());
077    
078            HierarchicalConfiguration result = new HierarchicalConfiguration();
079            result.setRootNode(cn);
080    
081            return result;
082        }
083    
084        /**
085         * Tests a newly created combiner.
086         */
087        @Test
088        public void testInit()
089        {
090            assertTrue("Combiner has list nodes", combiner.getListNodes().isEmpty());
091            assertFalse("Node is list node", combiner
092                    .isListNode(new DefaultConfigurationNode("test")));
093        }
094    
095    }