Skip to content

Commit d2437d4

Browse files
committed
Make the configuration more testable
1 parent 81153f4 commit d2437d4

File tree

7 files changed

+171
-19
lines changed

7 files changed

+171
-19
lines changed

src/main/java/org/mybatis/dynamic/sql/configuration/GlobalConfiguration.java

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,51 @@
1919
import java.io.InputStream;
2020
import java.util.Properties;
2121

22-
// TODO - read initial value from a properties file!
2322
public class GlobalConfiguration {
24-
private static boolean isNonRenderingWhereClauseAllowed = false;
25-
26-
private GlobalConfiguration() {}
27-
28-
static {
29-
String propertyFile = "mybatis-dynamic-sql.properties";
30-
try (InputStream is = GlobalConfiguration.class.getResourceAsStream(propertyFile)) {
31-
if (is != null) {
32-
Properties p = new Properties();
33-
p.load(is);
34-
String value = p.getProperty("nonRenderingWhereClauseAllowed");
35-
if (value != null) {
36-
isNonRenderingWhereClauseAllowed = Boolean.parseBoolean(value);
37-
}
38-
}
23+
public static final String CONFIGURATION_FILE_PROPERTY = "mybatis-dynamic-sql.configurationFile"; //$NON-NLS-1$
24+
private static final String DEFAULT_PROPERTY_FILE = "mybatis-dynamic-sql.properties"; //$NON-NLS-1$
25+
private boolean isNonRenderingWhereClauseAllowed = false;
26+
private final Properties properties = new Properties();
27+
28+
public GlobalConfiguration() {
29+
initialize();
30+
}
31+
32+
private void initialize() {
33+
initializeProperties();
34+
initializeNonRenderingWhereClauseAllowed();
35+
}
36+
37+
private void initializeProperties(){
38+
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(getConfigurationFileName());
39+
if (inputStream != null) {
40+
loadProperties(inputStream);
41+
}
42+
}
43+
44+
private String getConfigurationFileName() {
45+
String property = System.getProperty(CONFIGURATION_FILE_PROPERTY);
46+
if (property == null) {
47+
return DEFAULT_PROPERTY_FILE;
48+
} else {
49+
return property;
50+
}
51+
}
52+
53+
private void loadProperties(InputStream inputStream) {
54+
try {
55+
properties.load(inputStream);
3956
} catch (IOException e) {
40-
// ignore
57+
throw new RuntimeException(e);
4158
}
4259
}
4360

44-
public static boolean isIsNonRenderingWhereClauseAllowed() {
61+
private void initializeNonRenderingWhereClauseAllowed() {
62+
String value = properties.getProperty("nonRenderingWhereClauseAllowed", "false"); //$NON-NLS-1$ //$NON-NLS-2$
63+
isNonRenderingWhereClauseAllowed = Boolean.parseBoolean(value);
64+
}
65+
66+
public boolean isIsNonRenderingWhereClauseAllowed() {
4567
return isNonRenderingWhereClauseAllowed;
4668
}
4769
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2016-2022 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+
* http://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+
public class GlobalContext {
19+
20+
private static final GlobalContext instance = new GlobalContext();
21+
22+
private final GlobalConfiguration globalConfiguration = new GlobalConfiguration();
23+
24+
private GlobalContext() {}
25+
26+
public static GlobalConfiguration getConfiguration() {
27+
return instance.globalConfiguration;
28+
}
29+
}

src/main/java/org/mybatis/dynamic/sql/configuration/StatementConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
* @author Jeff Butler
4040
*/
4141
public class StatementConfiguration {
42-
private boolean isNonRenderingWhereClauseAllowed = GlobalConfiguration.isIsNonRenderingWhereClauseAllowed();
42+
private boolean isNonRenderingWhereClauseAllowed =
43+
GlobalContext.getConfiguration().isIsNonRenderingWhereClauseAllowed();
4344

4445
public boolean isNonRenderingWhereClauseAllowed() {
4546
return isNonRenderingWhereClauseAllowed;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2016-2022 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+
* http://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+
20+
import org.junit.jupiter.api.Test;
21+
22+
class GlobalConfigurationTest {
23+
@Test
24+
void testAlternateConfigurationFileChangesDefaults() {
25+
System.setProperty(GlobalConfiguration.CONFIGURATION_FILE_PROPERTY, "defaultTrue.properties");
26+
GlobalConfiguration configuration = new GlobalConfiguration();
27+
System.clearProperty(GlobalConfiguration.CONFIGURATION_FILE_PROPERTY);
28+
29+
assertThat(configuration.isIsNonRenderingWhereClauseAllowed()).isTrue();
30+
}
31+
32+
@Test
33+
void testMissingPropertyUsesDefaults() {
34+
System.setProperty(GlobalConfiguration.CONFIGURATION_FILE_PROPERTY, "empty.properties");
35+
GlobalConfiguration configuration = new GlobalConfiguration();
36+
System.clearProperty(GlobalConfiguration.CONFIGURATION_FILE_PROPERTY);
37+
38+
assertThat(configuration.isIsNonRenderingWhereClauseAllowed()).isFalse();
39+
}
40+
41+
@Test
42+
void testMissingPropertyFileUsesDefaults() {
43+
System.setProperty(GlobalConfiguration.CONFIGURATION_FILE_PROPERTY, "apfbsglf.properties");
44+
GlobalConfiguration configuration = new GlobalConfiguration();
45+
System.clearProperty(GlobalConfiguration.CONFIGURATION_FILE_PROPERTY);
46+
47+
assertThat(configuration.isIsNonRenderingWhereClauseAllowed()).isFalse();
48+
}
49+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#
2+
# Copyright 2016-2022 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+
# http://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+
17+
nonRenderingWhereClauseAllowed=true

src/test/resources/empty.properties

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#
2+
# Copyright 2016-2022 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+
# http://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+
17+
# intentionally empty for testing!
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#
2+
# Copyright 2016-2022 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+
# http://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+
17+
nonRenderingWhereClauseAllowed=false

0 commit comments

Comments
 (0)