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.configuration2.tree;
18  
19  import static org.junit.jupiter.api.Assertions.assertFalse;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import java.io.File;
23  
24  import org.apache.commons.configuration2.BaseHierarchicalConfiguration;
25  import org.apache.commons.configuration2.ConfigurationAssert;
26  import org.apache.commons.configuration2.XMLConfiguration;
27  import org.apache.commons.configuration2.ex.ConfigurationException;
28  import org.apache.commons.configuration2.io.FileHandler;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * A base class for testing combiner implementations. This base class provides some functionality for loading the test
34   * configurations, which are to be combined. Concrete sub classes only need to create the correct combiner object.
35   */
36  public abstract class AbstractCombinerTest {
37      /** Constant for the first test configuration. */
38      private static final File CONF1 = ConfigurationAssert.getTestFile("testcombine1.xml");
39  
40      /** Constant for the second test configuration. */
41      private static final File CONF2 = ConfigurationAssert.getTestFile("testcombine2.xml");
42  
43      /** The combiner to be tested. */
44      protected NodeCombiner combiner;
45  
46      /**
47       * Constructs a union configuration based on the source configurations.
48       *
49       * @return the union configuration
50       * @throws ConfigurationException if an error occurs
51       */
52      protected BaseHierarchicalConfiguration createCombinedConfiguration() throws ConfigurationException {
53          final XMLConfiguration conf1 = new XMLConfiguration();
54          new FileHandler(conf1).load(CONF1);
55          final XMLConfiguration conf2 = new XMLConfiguration();
56          new FileHandler(conf2).load(CONF2);
57          final ImmutableNode cn = combiner.combine(conf1.getNodeModel().getNodeHandler().getRootNode(), conf2.getNodeModel().getNodeHandler().getRootNode());
58  
59          final BaseHierarchicalConfiguration result = new BaseHierarchicalConfiguration();
60          result.getNodeModel().setRootNode(cn);
61  
62          return result;
63      }
64  
65      /**
66       * Creates the combiner to be tested. This method is called by {@code setUp()}. It must be implemented in concrete sub
67       * classes.
68       *
69       * @return the combiner to be tested
70       */
71      protected abstract NodeCombiner createCombiner();
72  
73      @BeforeEach
74      public void setUp() throws Exception {
75          combiner = createCombiner();
76      }
77  
78      /**
79       * Tests a newly created combiner.
80       */
81      @Test
82      public void testInit() {
83          assertTrue(combiner.getListNodes().isEmpty());
84          assertFalse(combiner.isListNode(NodeStructureHelper.createNode("test", null)));
85      }
86  }