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 static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  
25  import org.apache.commons.configuration2.PropertiesConfiguration;
26  import org.apache.commons.configuration2.XMLPropertiesConfiguration;
27  import org.apache.commons.configuration2.builder.BasicConfigurationBuilder;
28  import org.apache.commons.configuration2.builder.BuilderParameters;
29  import org.apache.commons.configuration2.builder.FileBasedBuilderParametersImpl;
30  import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder;
31  import org.apache.commons.configuration2.ex.ConfigurationException;
32  import org.apache.commons.lang3.StringUtils;
33  import org.junit.jupiter.api.Test;
34  import org.mockito.Mockito;
35  
36  /**
37   * Test class for {@code FileExtensionConfigurationBuilderProvider}.
38   */
39  public class TestFileExtensionConfigurationBuilderProvider {
40  
41      /** Constant for the name of the default configuration class. */
42      private static final String DEF_CLASS = PropertiesConfiguration.class.getName();
43  
44      /** Constant for the name of the matching class. */
45      private static final String MATCH_CLASS = XMLPropertiesConfiguration.class.getName();
46  
47      /** The test file extension. */
48      private static final String EXT = "xml";
49  
50      /**
51       * Creates a test instance with default settings.
52       *
53       * @return the test object
54       */
55      private static FileExtensionConfigurationBuilderProvider setUpProvider() {
56          return new FileExtensionConfigurationBuilderProvider(BasicConfigurationBuilder.class.getName(), null, MATCH_CLASS, DEF_CLASS, EXT, null);
57      }
58  
59      /**
60       * Creates a mock for the configuration declaration.
61       *
62       * @return the mock object
63       */
64      private ConfigurationDeclaration setUpDecl() {
65          return Mockito.mock(ConfigurationDeclaration.class);
66      }
67  
68      /**
69       * Tests whether the correct configuration class is selected if the file extension matches.
70       */
71      @Test
72      void testDetermineConfigurationClassExtensionMatch() throws ConfigurationException {
73          final ConfigurationDeclaration decl = setUpDecl();
74          final BuilderParameters params = new FileBasedBuilderParametersImpl().setPath("C:\\Test\\someTestConfiguration." + EXT);
75          final FileExtensionConfigurationBuilderProvider provider = setUpProvider();
76          assertEquals(MATCH_CLASS, provider.determineConfigurationClass(decl, Collections.singleton(params)));
77      }
78  
79      /**
80       * Tests whether the correct configuration class is selected if the file extension does not match.
81       */
82      @Test
83      void testDetermineConfigurationClassExtensionNoMatch() throws ConfigurationException {
84          final ConfigurationDeclaration decl = setUpDecl();
85          final BuilderParameters params = new FileBasedBuilderParametersImpl().setPath("C:\\Test\\someTestConfiguration.properties");
86          final FileExtensionConfigurationBuilderProvider provider = setUpProvider();
87          assertEquals(DEF_CLASS, provider.determineConfigurationClass(decl, Collections.singleton(params)));
88      }
89  
90      /**
91       * Tests that matches of file extensions are case insensitive.
92       */
93      @Test
94      void testDetermineConfigurationClassMatchCase() throws ConfigurationException {
95          final ConfigurationDeclaration decl = setUpDecl();
96          final BuilderParameters params = new FileBasedBuilderParametersImpl().setPath("C:\\Test\\someTestConfiguration." + StringUtils.toRootUpperCase(EXT));
97          final FileExtensionConfigurationBuilderProvider provider = setUpProvider();
98          assertEquals(MATCH_CLASS, provider.determineConfigurationClass(decl, Collections.singleton(params)));
99      }
100 
101     /**
102      * Tests whether the correct configuration class is selected if the file name does not have an extension.
103      */
104     @Test
105     void testDetermineConfigurationClassNoExtension() throws ConfigurationException {
106         final ConfigurationDeclaration decl = setUpDecl();
107         final BuilderParameters params = new FileBasedBuilderParametersImpl().setPath("C:\\Test\\someTestConfiguration");
108         final FileExtensionConfigurationBuilderProvider provider = setUpProvider();
109         assertEquals(DEF_CLASS, provider.determineConfigurationClass(decl, Collections.singleton(params)));
110     }
111 
112     /**
113      * Tests whether the correct configuration class is selected if no file-based parameters are provided.
114      */
115     @Test
116     void testDetermineConfigurationClassNoParams() throws ConfigurationException {
117         final ConfigurationDeclaration decl = setUpDecl();
118         final FileExtensionConfigurationBuilderProvider provider = setUpProvider();
119         assertEquals(DEF_CLASS, provider.determineConfigurationClass(decl, new ArrayList<>()));
120     }
121 
122     /**
123      * Tests whether the correct configuration class is selected if no file name is set.
124      */
125     @Test
126     void testDeterminieConfigurationClassNoFileName() throws ConfigurationException {
127         final ConfigurationDeclaration decl = setUpDecl();
128         final BuilderParameters params = new FileBasedBuilderParametersImpl();
129         final FileExtensionConfigurationBuilderProvider provider = setUpProvider();
130         assertEquals(DEF_CLASS, provider.determineConfigurationClass(decl, Collections.singleton(params)));
131     }
132 
133     /**
134      * Tries to create an instance without the default configuration class.
135      */
136     @Test
137     void testInitNoDefaultConfigClass() {
138         final String builderClass = BasicConfigurationBuilder.class.getName();
139         assertThrows(IllegalArgumentException.class,
140                 () -> new FileExtensionConfigurationBuilderProvider(builderClass, null, MATCH_CLASS, null, EXT, null));
141     }
142 
143     /**
144      * Tries to create an instance without a file extension.
145      */
146     @Test
147     void testInitNoExt() {
148         final String builderClass = BasicConfigurationBuilder.class.getName();
149         assertThrows(IllegalArgumentException.class,
150                 () -> new FileExtensionConfigurationBuilderProvider(builderClass, null, MATCH_CLASS, DEF_CLASS, null, null));
151     }
152 
153     /**
154      * Tries to create an instance without the matching configuration class.
155      */
156     @Test
157     void testInitNoMatchingConfigClass() {
158         final String builderClass = BasicConfigurationBuilder.class.getName();
159         assertThrows(IllegalArgumentException.class,
160                 () -> new FileExtensionConfigurationBuilderProvider(builderClass, null, null, DEF_CLASS, EXT, null));
161     }
162 
163     /**
164      * Tests whether the super class is correctly initialized.
165      */
166     @Test
167     void testInitSuper() {
168         final FileExtensionConfigurationBuilderProvider provider = new FileExtensionConfigurationBuilderProvider(BasicConfigurationBuilder.class.getName(),
169             ReloadingFileBasedConfigurationBuilder.class.getName(), MATCH_CLASS, DEF_CLASS, EXT, null);
170         assertEquals(BasicConfigurationBuilder.class.getName(), provider.getBuilderClass());
171         assertEquals(ReloadingFileBasedConfigurationBuilder.class.getName(), provider.getReloadingBuilderClass());
172         assertEquals(DEF_CLASS, provider.getConfigurationClass());
173     }
174 }