Skip to content

Commit 47d66a4

Browse files
committed
Throw exception on property file error rather than logging it
1 parent 6e526f6 commit 47d66a4

File tree

3 files changed

+12
-39
lines changed

3 files changed

+12
-39
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
*/
1616
package org.mybatis.dynamic.sql.configuration;
1717

18+
import org.mybatis.dynamic.sql.exception.DynamicSqlException;
19+
1820
import java.io.IOException;
1921
import java.io.InputStream;
2022
import java.util.Properties;
21-
import java.util.logging.Level;
22-
import java.util.logging.Logger;
2323

2424
public class GlobalConfiguration {
2525
public static final String CONFIGURATION_FILE_PROPERTY = "mybatis-dynamic-sql.configurationFile"; //$NON-NLS-1$
@@ -57,8 +57,7 @@ void loadProperties(InputStream inputStream, String propertyFile) {
5757
try {
5858
properties.load(inputStream);
5959
} catch (IOException e) {
60-
Logger logger = Logger.getLogger(GlobalConfiguration.class.getName());
61-
logger.log(Level.SEVERE, e, () -> "IOException reading property file \"" + propertyFile + "\"");
60+
throw new DynamicSqlException("IOException reading property file \"" + propertyFile + "\"", e);
6261
}
6362
}
6463

src/main/java/org/mybatis/dynamic/sql/exception/DynamicSqlException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ public class DynamicSqlException extends RuntimeException {
1919
public DynamicSqlException(String message) {
2020
super(message);
2121
}
22+
23+
public DynamicSqlException(String message, Throwable cause) {
24+
super(message, cause);
25+
}
2226
}

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

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,13 @@
1616
package org.mybatis.dynamic.sql.configuration;
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
1920

2021
import java.io.IOException;
2122
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;
2723

2824
import org.junit.jupiter.api.Test;
25+
import org.mybatis.dynamic.sql.exception.DynamicSqlException;
2926

3027
class GlobalConfigurationTest {
3128
@Test
@@ -62,35 +59,8 @@ void testBadPropertyFile() throws IOException {
6259
assert inputStream != null;
6360
inputStream.close();
6461

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() {}
62+
assertThatExceptionOfType(DynamicSqlException.class)
63+
.isThrownBy(() -> configuration.loadProperties(inputStream, "empty.properties"))
64+
.withMessage("IOException reading property file \"empty.properties\"");
9565
}
9666
}

0 commit comments

Comments
 (0)