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