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.builder.combined;
18  
19  import org.apache.commons.configuration2.builder.BasicBuilderParameters;
20  import org.apache.commons.configuration2.builder.BuilderParameters;
21  import org.apache.commons.configuration2.interpol.ConfigurationInterpolator;
22  import org.apache.commons.configuration2.interpol.DefaultLookups;
23  import org.junit.jupiter.api.AfterEach;
24  
25  /**
26   * A base class for test classes for {@code MultiFileConfigurationBuilder} and derived classes. This class provides some
27   * common functionality related to file name pattern which can be used by concrete tests.
28   */
29  public class AbstractMultiFileConfigurationBuilderTest {
30      /** The system property which selects a sub configuration. */
31      private static final String PROP = "Id";
32  
33      /** The part of the pattern containing the variable. */
34      protected static String PATTERN_VAR = "${sys:Id}";
35  
36      /** The pattern for file names. */
37      protected static String PATTERN = "target/test-classes/testMultiConfiguration_" + PATTERN_VAR + ".xml";
38  
39      /**
40       * Creates a {@code ConfigurationInterpolator} to be used by tests. This object contains a lookup for system properties.
41       *
42       * @return the new {@code ConfigurationInterpolator}
43       */
44      protected static ConfigurationInterpolator createInterpolator() {
45          final ConfigurationInterpolator ci = new ConfigurationInterpolator();
46          ci.registerLookup(DefaultLookups.SYSTEM_PROPERTIES.getPrefix(), DefaultLookups.SYSTEM_PROPERTIES.getLookup());
47          return ci;
48      }
49  
50      /**
51       * Creates a parameters object with default settings for a test builder instance.
52       *
53       * @param managedParams the parameters for managed configurations
54       * @return the test parameters
55       */
56      protected static BasicBuilderParameters createTestBuilderParameters(final BuilderParameters managedParams) {
57          return new MultiFileBuilderParametersImpl().setFilePattern(PATTERN).setManagedBuilderParameters(managedParams).setInterpolator(createInterpolator());
58      }
59  
60      /**
61       * Selects a specific configuration to be obtained from the builder.
62       *
63       * @param index the index of the configuration to be accessed (valid indices are 1-3)
64       */
65      protected static void switchToConfig(final int index) {
66          switchToConfig("100" + index);
67      }
68  
69      /**
70       * Sets a system property for accessing a specific configuration file from the test builder. The passed in id can be
71       * null, then the system property is removed.
72       *
73       * @param id the ID of the managed configuration to load
74       */
75      protected static void switchToConfig(final String id) {
76          if (id != null) {
77              System.setProperty(PROP, id);
78          } else {
79              System.getProperties().remove(PROP);
80          }
81      }
82  
83      @AfterEach
84      public void tearDown() throws Exception {
85          switchToConfig(null);
86      }
87  }