Skip to content

Commit 735d6df

Browse files
committed
Add tests for BaseBuilder and MapperBuilderAssistant
1 parent fc97b3b commit 735d6df

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

src/test/java/org/apache/ibatis/builder/AuthorMapper.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,9 @@
139139
where id=#{id}
140140
</update>
141141

142+
<select id="selectWithOptions" resultType="org.apache.ibatis.domain.blog.Author"
143+
fetchSize="200" timeout="10" statementType="PREPARED" resultSetType="SCROLL_SENSITIVE" flushCache="false" useCache="false">
144+
select * from author
145+
</select>
142146

143147
</mapper>

src/test/java/org/apache/ibatis/builder/XmlMapperBuilderTest.java

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,28 @@
1616
package org.apache.ibatis.builder;
1717

1818
import java.io.InputStream;
19+
import java.util.regex.Pattern;
1920

2021
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
2122
import org.apache.ibatis.io.Resources;
23+
import org.apache.ibatis.mapping.MappedStatement;
24+
import org.apache.ibatis.mapping.ResultSetType;
25+
import org.apache.ibatis.mapping.StatementType;
2226
import org.apache.ibatis.session.Configuration;
27+
import org.apache.ibatis.type.TypeHandler;
28+
import org.junit.Rule;
2329
import org.junit.Test;
30+
import org.junit.rules.ExpectedException;
31+
32+
import static org.junit.Assert.assertThat;
33+
import static org.hamcrest.core.Is.is;
34+
import static org.hamcrest.core.IsNull.nullValue;
2435

2536
public class XmlMapperBuilderTest {
2637

38+
@Rule
39+
public ExpectedException expectedException = ExpectedException.none();
40+
2741
@Test
2842
public void shouldSuccessfullyLoadXMLMapperFile() throws Exception {
2943
Configuration configuration = new Configuration();
@@ -33,6 +47,118 @@ public void shouldSuccessfullyLoadXMLMapperFile() throws Exception {
3347
builder.parse();
3448
}
3549

50+
@Test
51+
public void mappedStatementWithOptions() throws Exception {
52+
Configuration configuration = new Configuration();
53+
String resource = "org/apache/ibatis/builder/AuthorMapper.xml";
54+
InputStream inputStream = Resources.getResourceAsStream(resource);
55+
XMLMapperBuilder builder = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
56+
builder.parse();
57+
58+
MappedStatement mappedStatement = configuration.getMappedStatement("selectWithOptions");
59+
assertThat(mappedStatement.getFetchSize(), is(200));
60+
assertThat(mappedStatement.getTimeout(), is(10));
61+
assertThat(mappedStatement.getStatementType(), is(StatementType.PREPARED));
62+
assertThat(mappedStatement.getResultSetType(), is(ResultSetType.SCROLL_SENSITIVE));
63+
assertThat(mappedStatement.isFlushCacheRequired(), is(false));
64+
assertThat(mappedStatement.isUseCache(), is(false));
65+
66+
}
67+
68+
@Test
69+
public void parseExpression() {
70+
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
71+
{
72+
Pattern pattern = builder.parseExpression("[0-9]", "[a-z]");
73+
assertThat(pattern.matcher("0").find(), is(true));
74+
assertThat(pattern.matcher("a").find(), is(false));
75+
}
76+
{
77+
Pattern pattern = builder.parseExpression(null, "[a-z]");
78+
assertThat(pattern.matcher("0").find(), is(false));
79+
assertThat(pattern.matcher("a").find(), is(true));
80+
}
81+
}
82+
83+
@Test
84+
public void resolveJdbcTypeWithUndefinedValue() {
85+
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
86+
expectedException.expect(BuilderException.class);
87+
expectedException.expectMessage(is("Error resolving JdbcType. Cause: java.lang.IllegalArgumentException: No enum constant org.apache.ibatis.type.JdbcType.aaa"));
88+
builder.resolveJdbcType("aaa");
89+
}
90+
91+
@Test
92+
public void resolveResultSetTypeWithUndefinedValue() {
93+
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
94+
expectedException.expect(BuilderException.class);
95+
expectedException.expectMessage(is("Error resolving ResultSetType. Cause: java.lang.IllegalArgumentException: No enum constant org.apache.ibatis.mapping.ResultSetType.bbb"));
96+
builder.resolveResultSetType("bbb");
97+
}
98+
99+
@Test
100+
public void resolveParameterModeWithUndefinedValue() {
101+
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
102+
expectedException.expect(BuilderException.class);
103+
expectedException.expectMessage(is("Error resolving ParameterMode. Cause: java.lang.IllegalArgumentException: No enum constant org.apache.ibatis.mapping.ParameterMode.ccc"));
104+
builder.resolveParameterMode("ccc");
105+
}
106+
107+
@Test
108+
public void createInstanceWithAbstractClass() {
109+
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
110+
expectedException.expect(BuilderException.class);
111+
expectedException.expectMessage(is("Error creating instance. Cause: java.lang.InstantiationException: org.apache.ibatis.builder.BaseBuilder"));
112+
builder.createInstance("org.apache.ibatis.builder.BaseBuilder");
113+
}
114+
115+
@Test
116+
public void resolveClassWithNotFound() {
117+
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
118+
expectedException.expect(BuilderException.class);
119+
expectedException.expectMessage(is("Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'ddd'. Cause: java.lang.ClassNotFoundException: Cannot find class: ddd"));
120+
builder.resolveClass("ddd");
121+
}
122+
123+
@Test
124+
public void resolveTypeHandlerTypeHandlerAliasIsNull() {
125+
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
126+
TypeHandler<?> typeHandler = builder.resolveTypeHandler(String.class, (String)null);
127+
assertThat(typeHandler, nullValue());
128+
}
129+
130+
@Test
131+
public void resolveTypeHandlerNoAssignable() {
132+
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
133+
expectedException.expect(BuilderException.class);
134+
expectedException.expectMessage(is("Type java.lang.Integer is not a valid TypeHandler because it does not implement TypeHandler interface"));
135+
builder.resolveTypeHandler(String.class, "integer");
136+
}
137+
138+
@Test
139+
public void setCurrentNamespaceValueIsNull() {
140+
MapperBuilderAssistant builder = new MapperBuilderAssistant(new Configuration(), "resource");
141+
expectedException.expect(BuilderException.class);
142+
expectedException.expectMessage(is("The mapper element requires a namespace attribute to be specified."));
143+
builder.setCurrentNamespace(null);
144+
}
145+
146+
@Test
147+
public void useCacheRefNamespaceIsNull() {
148+
MapperBuilderAssistant builder = new MapperBuilderAssistant(new Configuration(), "resource");
149+
expectedException.expect(BuilderException.class);
150+
expectedException.expectMessage(is("cache-ref element requires a namespace attribute."));
151+
builder.useCacheRef(null);
152+
}
153+
154+
@Test
155+
public void useCacheRefNamespaceIsUndefined() {
156+
MapperBuilderAssistant builder = new MapperBuilderAssistant(new Configuration(), "resource");
157+
expectedException.expect(IncompleteElementException.class);
158+
expectedException.expectMessage(is("No cache for namespace 'eee' could be found."));
159+
builder.useCacheRef("eee");
160+
}
161+
36162
// @Test
37163
// public void shouldNotLoadTheSameNamespaceFromTwoResourcesWithDifferentNames() throws Exception {
38164
// Configuration configuration = new Configuration();

0 commit comments

Comments
 (0)