View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.configuration.tree;
18  
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.assertTrue;
21  
22  import java.io.File;
23  
24  import org.apache.commons.configuration.ConfigurationAssert;
25  import org.apache.commons.configuration.ConfigurationException;
26  import org.apache.commons.configuration.HierarchicalConfiguration;
27  import org.apache.commons.configuration.XMLConfiguration;
28  import org.junit.Before;
29  import org.junit.Test;
30  
31  /**
32   * A base class for testing combiner implementations. This base class provides
33   * some functionality for loading the test configurations, which are to be
34   * combined. Concrete sub classes only need to create the correct combiner
35   * object.
36   *
37   * @version $Id: AbstractCombinerTest.java 1225911 2011-12-30 20:19:10Z oheger $
38   */
39  public abstract class AbstractCombinerTest
40  {
41      /** Constant for the first test configuration. */
42      static File CONF1 = ConfigurationAssert.getTestFile("testcombine1.xml");
43  
44      /** Constant for the second test configuration. */
45      static File CONF2 = ConfigurationAssert.getTestFile("testcombine2.xml");
46  
47      /** The combiner to be tested. */
48      protected NodeCombiner combiner;
49  
50      @Before
51      public void setUp() throws Exception
52      {
53          combiner = createCombiner();
54      }
55  
56      /**
57       * Creates the combiner to be tested. This method is called by
58       * <code>setUp()</code>. It must be implemented in concrete sub classes.
59       *
60       * @return the combiner to be tested
61       */
62      protected abstract NodeCombiner createCombiner();
63  
64      /**
65       * Constructs a union configuration based on the source configurations.
66       *
67       * @return the union configuration
68       * @throws ConfigurationException if an error occurs
69       */
70      protected HierarchicalConfiguration createCombinedConfiguration()
71              throws ConfigurationException
72      {
73          XMLConfiguration conf1 = new XMLConfiguration(CONF1);
74          XMLConfiguration conf2 = new XMLConfiguration(CONF2);
75          ConfigurationNode cn = combiner.combine(conf1.getRootNode(), conf2
76                  .getRootNode());
77  
78          HierarchicalConfiguration result = new HierarchicalConfiguration();
79          result.setRootNode(cn);
80  
81          return result;
82      }
83  
84      /**
85       * Tests a newly created combiner.
86       */
87      @Test
88      public void testInit()
89      {
90          assertTrue("Combiner has list nodes", combiner.getListNodes().isEmpty());
91          assertFalse("Node is list node", combiner
92                  .isListNode(new DefaultConfigurationNode("test")));
93      }
94  
95  }