Skip to content

Commit

Permalink
Merge pull request #2727 from epochcoder/feature/2724-pass-subclassed…
Browse files Browse the repository at this point in the history
…-configuration-xmlconfigbuilder

Enable ability to provide custom configuration to XMLConfigBuilder
  • Loading branch information
harawata authored Oct 31, 2022
2 parents 043aaaa + d7826d7 commit 350ac91
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
24 changes: 20 additions & 4 deletions src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ public XMLConfigBuilder(Reader reader, String environment) {
}

public XMLConfigBuilder(Reader reader, String environment, Properties props) {
this(new XPathParser(reader, true, props, new XMLMapperEntityResolver()), environment, props);
this(Configuration.class, reader, environment, props);
}

public XMLConfigBuilder(Class<? extends Configuration> configClass, Reader reader, String environment, Properties props) {
this(configClass, new XPathParser(reader, true, props, new XMLMapperEntityResolver()), environment, props);
}

public XMLConfigBuilder(InputStream inputStream) {
Expand All @@ -79,11 +83,15 @@ public XMLConfigBuilder(InputStream inputStream, String environment) {
}

public XMLConfigBuilder(InputStream inputStream, String environment, Properties props) {
this(new XPathParser(inputStream, true, props, new XMLMapperEntityResolver()), environment, props);
this(Configuration.class, inputStream, environment, props);
}

public XMLConfigBuilder(Class<? extends Configuration> configClass, InputStream inputStream, String environment, Properties props) {
this(configClass, new XPathParser(inputStream, true, props, new XMLMapperEntityResolver()), environment, props);
}

private XMLConfigBuilder(XPathParser parser, String environment, Properties props) {
super(new Configuration());
private XMLConfigBuilder(Class<? extends Configuration> configClass, XPathParser parser, String environment, Properties props) {
super(newConfig(configClass));
ErrorContext.instance().resource("SQL Mapper Configuration");
this.configuration.setVariables(props);
this.parsed = false;
Expand Down Expand Up @@ -406,4 +414,12 @@ private boolean isSpecifiedEnvironment(String id) {
return environment.equals(id);
}

private static Configuration newConfig(Class<? extends Configuration> configClass) {
try {
return configClass.getDeclaredConstructor().newInstance();
} catch (Exception ex) {
throw new BuilderException("Failed to create a new Configuration instance.", ex);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.then;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.math.RoundingMode;
Expand Down Expand Up @@ -64,6 +66,7 @@
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -114,7 +117,7 @@ enum MyEnum {

public static class EnumOrderTypeHandler<E extends Enum<E>> extends BaseTypeHandler<E> {

private E[] constants;
private final E[] constants;

public EnumOrderTypeHandler(Class<E> javaType) {
constants = javaType.getEnumConstants();
Expand Down Expand Up @@ -319,4 +322,37 @@ public static String provideSql() {
}
}

@Test
void shouldAllowSubclassedConfiguration() throws IOException {
String resource = "org/apache/ibatis/builder/MinimalMapperConfig.xml";
try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
XMLConfigBuilder builder = new XMLConfigBuilder(
MyConfiguration.class, inputStream, null, null);
Configuration config = builder.parse();

assertThat(config).isInstanceOf(MyConfiguration.class);
}
}

@Test
void noDefaultConstructorForSubclassedConfiguration() throws IOException {
String resource = "org/apache/ibatis/builder/MinimalMapperConfig.xml";
try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
Exception exception = Assertions.assertThrows(Exception.class, () -> new XMLConfigBuilder(
BadConfiguration.class, inputStream, null, null));
assertEquals("Failed to create a new Configuration instance.", exception.getMessage());
}
}

public static class MyConfiguration extends Configuration {
// only using to check configuration was used
}

public static class BadConfiguration extends Configuration {

public BadConfiguration(String parameter) {
// should have a default constructor
}
}

}

0 comments on commit 350ac91

Please sign in to comment.