View Javadoc
1   /*
2    *    Copyright 2016-2025 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.dynamic.sql.configuration;
17  
18  import static org.assertj.core.api.Assertions.assertThat;
19  import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  import org.junit.jupiter.api.Test;
25  import org.mybatis.dynamic.sql.exception.DynamicSqlException;
26  import org.mybatis.dynamic.sql.util.Messages;
27  
28  class GlobalConfigurationTest {
29      @Test
30      void testAlternateConfigurationFileChangesDefaults() {
31          System.setProperty(GlobalConfiguration.CONFIGURATION_FILE_PROPERTY, "defaultTrue.properties");
32          GlobalConfiguration configuration = new GlobalConfiguration();
33          System.clearProperty(GlobalConfiguration.CONFIGURATION_FILE_PROPERTY);
34  
35          assertThat(configuration.isIsNonRenderingWhereClauseAllowed()).isTrue();
36      }
37  
38      @Test
39      void testMissingPropertyUsesDefaults() {
40          System.setProperty(GlobalConfiguration.CONFIGURATION_FILE_PROPERTY, "empty.properties");
41          GlobalConfiguration configuration = new GlobalConfiguration();
42          System.clearProperty(GlobalConfiguration.CONFIGURATION_FILE_PROPERTY);
43  
44          assertThat(configuration.isIsNonRenderingWhereClauseAllowed()).isFalse();
45      }
46  
47      @Test
48      void testMissingPropertyFileUsesDefaults() {
49          System.setProperty(GlobalConfiguration.CONFIGURATION_FILE_PROPERTY, "apfbsglf.properties");
50          GlobalConfiguration configuration = new GlobalConfiguration();
51          System.clearProperty(GlobalConfiguration.CONFIGURATION_FILE_PROPERTY);
52  
53          assertThat(configuration.isIsNonRenderingWhereClauseAllowed()).isFalse();
54      }
55  
56      @Test
57      void testBadPropertyFile() throws IOException {
58          GlobalConfiguration configuration = new GlobalConfiguration();
59          InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("empty.properties");
60          assert inputStream != null;
61          inputStream.close();
62  
63          assertThatExceptionOfType(DynamicSqlException.class)
64                  .isThrownBy(() -> configuration.loadProperties(inputStream, "empty.properties"))
65                  .withMessage(Messages.getString("ERROR.3", "empty.properties"))
66                  .withCauseInstanceOf(IOException.class);
67      }
68  }