Skip to content

Commit 7c85dda

Browse files
committed
Make the configuration more testable
1 parent d2437d4 commit 7c85dda

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.io.IOException;
1919
import java.io.InputStream;
2020
import java.util.Properties;
21+
import java.util.logging.Level;
22+
import java.util.logging.Logger;
2123

2224
public class GlobalConfiguration {
2325
public static final String CONFIGURATION_FILE_PROPERTY = "mybatis-dynamic-sql.configurationFile"; //$NON-NLS-1$
@@ -35,9 +37,10 @@ private void initialize() {
3537
}
3638

3739
private void initializeProperties(){
38-
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(getConfigurationFileName());
40+
String configFileName = getConfigurationFileName();
41+
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(configFileName);
3942
if (inputStream != null) {
40-
loadProperties(inputStream);
43+
loadProperties(inputStream, configFileName);
4144
}
4245
}
4346

@@ -50,11 +53,12 @@ private String getConfigurationFileName() {
5053
}
5154
}
5255

53-
private void loadProperties(InputStream inputStream) {
56+
void loadProperties(InputStream inputStream, String propertyFile) {
5457
try {
5558
properties.load(inputStream);
5659
} catch (IOException e) {
57-
throw new RuntimeException(e);
60+
Logger logger = Logger.getLogger(GlobalConfiguration.class.getName());
61+
logger.log(Level.SEVERE, e, () -> "IOException reading property file \"" + propertyFile + "\"");
5862
}
5963
}
6064

src/test/java/org/mybatis/dynamic/sql/configuration/GlobalConfigurationTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
1919

20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
import java.util.logging.Handler;
25+
import java.util.logging.LogRecord;
26+
import java.util.logging.Logger;
27+
2028
import org.junit.jupiter.api.Test;
2129

2230
class GlobalConfigurationTest {
@@ -46,4 +54,43 @@ void testMissingPropertyFileUsesDefaults() {
4654

4755
assertThat(configuration.isIsNonRenderingWhereClauseAllowed()).isFalse();
4856
}
57+
58+
@Test
59+
void testBadPropertyFile() throws IOException {
60+
GlobalConfiguration configuration = new GlobalConfiguration();
61+
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("empty.properties");
62+
assert inputStream != null;
63+
inputStream.close();
64+
65+
TestLogHandler testLogHandler = new TestLogHandler();
66+
Logger logger = Logger.getLogger(GlobalConfiguration.class.getName());
67+
// we only want to use our handler for this test, so we don't pollute the test output
68+
logger.setUseParentHandlers(false);
69+
logger.addHandler(testLogHandler);
70+
71+
configuration.loadProperties(inputStream, "empty.properties");
72+
assertThat(testLogHandler.records).hasSize(1);
73+
assertThat(testLogHandler.getRecords().get(0).getMessage())
74+
.isEqualTo("IOException reading property file \"empty.properties\"");
75+
}
76+
77+
public static class TestLogHandler extends Handler {
78+
79+
private final List<LogRecord> records = new ArrayList<>();
80+
81+
public List<LogRecord> getRecords() {
82+
return records;
83+
}
84+
85+
@Override
86+
public void publish(LogRecord record) {
87+
records.add(record);
88+
}
89+
90+
@Override
91+
public void flush() {}
92+
93+
@Override
94+
public void close() {}
95+
}
4996
}

0 commit comments

Comments
 (0)