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    *     https://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  
38      /** Constant for the first test configuration. */
39      private static final File CONF1 = ConfigurationAssert.getTestFile("testcombine1.xml");
40  
41      /** Constant for the second test configuration. */
42      private static final File CONF2 = ConfigurationAssert.getTestFile("testcombine2.xml");
43  
44      /** The combiner to be tested. */
45      protected NodeCombiner combiner;
46  
47      /**
48       * Constructs a union configuration based on the source configurations.
49       *
50       * @return the union configuration
51       * @throws ConfigurationException if an error occurs
52       */
53      protected BaseHierarchicalConfiguration createCombinedConfiguration() throws ConfigurationException {
54          final XMLConfiguration conf1 = new XMLConfiguration();
55          new FileHandler(conf1).load(CONF1);
56          final XMLConfiguration conf2 = new XMLConfiguration();
57          new FileHandler(conf2).load(CONF2);
58          final ImmutableNode cn = combiner.combine(conf1.getNodeModel().getNodeHandler().getRootNode(), conf2.getNodeModel().getNodeHandler().getRootNode());
59  
60          final BaseHierarchicalConfiguration result = new BaseHierarchicalConfiguration();
61          result.getNodeModel().setRootNode(cn);
62  
63          return result;
64      }
65  
66      /**
67       * Creates the combiner to be tested. This method is called by {@code setUp()}. It must be implemented in concrete sub
68       * classes.
69       *
70       * @return the combiner to be tested
71       */
72      protected abstract NodeCombiner createCombiner();
73  
74      @BeforeEach
75      public void setUp() throws Exception {
76          combiner = createCombiner();
77      }
78  
79      /**
80       * Tests a newly created combiner.
81       */
82      @Test
83      void testInit() {
84          assertTrue(combiner.getListNodes().isEmpty());
85          assertFalse(combiner.isListNode(NodeStructureHelper.createNode("test", null)));
86      }
87  }