Skip to content

Commit d91d71e

Browse files
committed
Merge pull request #666 from kazuki43zoo/improve-tests
Improve tests
2 parents 2995f6e + baab2b7 commit d91d71e

File tree

5 files changed

+242
-5
lines changed

5 files changed

+242
-5
lines changed

src/test/java/org/apache/ibatis/binding/BindingTest.java

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,8 +24,10 @@
2424

2525
import java.lang.reflect.Method;
2626
import java.util.ArrayList;
27+
import java.util.Collection;
2728
import java.util.Collections;
2829
import java.util.HashMap;
30+
import java.util.Iterator;
2931
import java.util.List;
3032
import java.util.Map;
3133

@@ -36,6 +38,7 @@
3638
import net.sf.cglib.proxy.Factory;
3739

3840
import org.apache.ibatis.BaseDataTest;
41+
import org.apache.ibatis.cursor.Cursor;
3942
import org.apache.ibatis.domain.blog.Author;
4043
import org.apache.ibatis.domain.blog.Blog;
4144
import org.apache.ibatis.domain.blog.DraftPost;
@@ -734,5 +737,63 @@ public void shouldGetBlogsWithAuthorsAndPostsEagerly() {
734737
session.close();
735738
}
736739
}
737-
740+
741+
@Test
742+
public void executeWithResultHandlerAndRowBounds() {
743+
SqlSession session = sqlSessionFactory.openSession();
744+
try {
745+
BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
746+
final DefaultResultHandler handler = new DefaultResultHandler();
747+
mapper.collectRangeBlogs(handler, new RowBounds(1, 1));
748+
749+
assertEquals(1, handler.getResultList().size());
750+
Blog blog = (Blog) handler.getResultList().get(0);
751+
assertEquals(2, blog.getId());
752+
753+
} finally {
754+
session.close();
755+
}
756+
}
757+
758+
@Test
759+
public void executeWithMapKeyAndRowBounds() {
760+
SqlSession session = sqlSessionFactory.openSession();
761+
try {
762+
BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
763+
Map<Integer, Blog> blogs = mapper.selectRangeBlogsAsMapById(new RowBounds(1, 1));
764+
765+
assertEquals(1, blogs.size());
766+
Blog blog = blogs.get(2);
767+
assertEquals(2, blog.getId());
768+
769+
} finally {
770+
session.close();
771+
}
772+
}
773+
774+
@Test
775+
public void executeWithCursorAndRowBounds() {
776+
SqlSession session = sqlSessionFactory.openSession();
777+
try {
778+
BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
779+
Cursor<Blog> blogs = mapper.openRangeBlogs(new RowBounds(1, 1));
780+
781+
Iterator<Blog> blogIterator = blogs.iterator();
782+
Blog blog = blogIterator.next();
783+
assertEquals(2, blog.getId());
784+
assertFalse(blogIterator.hasNext());
785+
786+
} finally {
787+
session.close();
788+
}
789+
}
790+
791+
@Test
792+
public void registeredMappers() {
793+
Collection<Class<?>> mapperClasses = sqlSessionFactory.getConfiguration().getMapperRegistry().getMappers();
794+
assertEquals(2, mapperClasses.size());
795+
assertTrue(mapperClasses.contains(BoundBlogMapper.class));
796+
assertTrue(mapperClasses.contains(BoundAuthorMapper.class));
797+
}
798+
738799
}

src/test/java/org/apache/ibatis/binding/BoundBlogMapper.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,11 +16,13 @@
1616
package org.apache.ibatis.binding;
1717

1818
import org.apache.ibatis.annotations.*;
19+
import org.apache.ibatis.cursor.Cursor;
1920
import org.apache.ibatis.domain.blog.Author;
2021
import org.apache.ibatis.domain.blog.Blog;
2122
import org.apache.ibatis.domain.blog.DraftPost;
2223
import org.apache.ibatis.domain.blog.Post;
2324
import org.apache.ibatis.mapping.FetchType;
25+
import org.apache.ibatis.session.ResultHandler;
2426
import org.apache.ibatis.session.RowBounds;
2527

2628
import java.util.List;
@@ -43,6 +45,10 @@ public interface BoundBlogMapper {
4345
@MapKey("id")
4446
Map<Integer,Blog> selectBlogsAsMapById();
4547

48+
@Select({ "SELECT * FROM blog ORDER BY id"})
49+
@MapKey("id")
50+
Map<Integer,Blog> selectRangeBlogsAsMapById(RowBounds rowBounds);
51+
4652
//======================================================
4753

4854
@Select({
@@ -51,6 +57,22 @@ public interface BoundBlogMapper {
5157
})
5258
List<Blog> selectBlogs();
5359

60+
@Select({
61+
"SELECT *",
62+
"FROM blog",
63+
"ORDER BY id"
64+
})
65+
@ResultType(Blog.class)
66+
void collectRangeBlogs(ResultHandler<Object> blog, RowBounds rowBounds);
67+
68+
69+
@Select({
70+
"SELECT *",
71+
"FROM blog",
72+
"ORDER BY id"
73+
})
74+
Cursor<Blog> openRangeBlogs(RowBounds rowBounds);
75+
5476
//======================================================
5577

5678
List<Blog> selectBlogsFromXML();

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/ParameterExpressionTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,10 +17,17 @@
1717

1818
import java.util.Map;
1919
import org.junit.Assert;
20+
import org.junit.Rule;
2021
import org.junit.Test;
22+
import org.junit.rules.ExpectedException;
23+
24+
import static org.hamcrest.core.Is.is;
2125

2226
public class ParameterExpressionTest {
2327

28+
@Rule
29+
public ExpectedException expectedException = ExpectedException.none();
30+
2431
@Test
2532
public void simpleProperty() {
2633
Map<String, String> result = new ParameterExpression("id");
@@ -124,4 +131,18 @@ public void shouldIgnoreLeadingAndTrailingSpaces() {
124131
Assert.assertEquals("val2", result.get("attr2"));
125132
}
126133

134+
@Test
135+
public void invalidOldJdbcTypeFormat() {
136+
expectedException.expect(BuilderException.class);
137+
expectedException.expectMessage(is("Parsing error in {id:} in position 3"));
138+
new ParameterExpression("id:");
139+
}
140+
141+
@Test
142+
public void invalidJdbcTypeOptUsingExpression() {
143+
expectedException.expect(BuilderException.class);
144+
expectedException.expectMessage(is("Parsing error in {(expression)+} in position 12"));
145+
new ParameterExpression("(expression)+");
146+
}
147+
127148
}

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

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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.hamcrest.CoreMatchers;
29+
import org.junit.Rule;
2330
import org.junit.Test;
31+
import org.junit.rules.ExpectedException;
32+
33+
import static org.junit.Assert.assertThat;
34+
import static org.hamcrest.CoreMatchers.*;
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,121 @@ 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(startsWith("Error resolving JdbcType. Cause: java.lang.IllegalArgumentException: No enum"));
88+
expectedException.expectMessage(endsWith("org.apache.ibatis.type.JdbcType.aaa"));
89+
builder.resolveJdbcType("aaa");
90+
}
91+
92+
@Test
93+
public void resolveResultSetTypeWithUndefinedValue() {
94+
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
95+
expectedException.expect(BuilderException.class);
96+
expectedException.expectMessage(startsWith("Error resolving ResultSetType. Cause: java.lang.IllegalArgumentException: No enum"));
97+
expectedException.expectMessage(endsWith("org.apache.ibatis.mapping.ResultSetType.bbb"));
98+
builder.resolveResultSetType("bbb");
99+
}
100+
101+
@Test
102+
public void resolveParameterModeWithUndefinedValue() {
103+
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
104+
expectedException.expect(BuilderException.class);
105+
expectedException.expectMessage(startsWith("Error resolving ParameterMode. Cause: java.lang.IllegalArgumentException: No enum"));
106+
expectedException.expectMessage(endsWith("org.apache.ibatis.mapping.ParameterMode.ccc"));
107+
builder.resolveParameterMode("ccc");
108+
}
109+
110+
@Test
111+
public void createInstanceWithAbstractClass() {
112+
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
113+
expectedException.expect(BuilderException.class);
114+
expectedException.expectMessage(is("Error creating instance. Cause: java.lang.InstantiationException: org.apache.ibatis.builder.BaseBuilder"));
115+
builder.createInstance("org.apache.ibatis.builder.BaseBuilder");
116+
}
117+
118+
@Test
119+
public void resolveClassWithNotFound() {
120+
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
121+
expectedException.expect(BuilderException.class);
122+
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"));
123+
builder.resolveClass("ddd");
124+
}
125+
126+
@Test
127+
public void resolveTypeHandlerTypeHandlerAliasIsNull() {
128+
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
129+
TypeHandler<?> typeHandler = builder.resolveTypeHandler(String.class, (String)null);
130+
assertThat(typeHandler, nullValue());
131+
}
132+
133+
@Test
134+
public void resolveTypeHandlerNoAssignable() {
135+
BaseBuilder builder = new BaseBuilder(new Configuration()){{}};
136+
expectedException.expect(BuilderException.class);
137+
expectedException.expectMessage(is("Type java.lang.Integer is not a valid TypeHandler because it does not implement TypeHandler interface"));
138+
builder.resolveTypeHandler(String.class, "integer");
139+
}
140+
141+
@Test
142+
public void setCurrentNamespaceValueIsNull() {
143+
MapperBuilderAssistant builder = new MapperBuilderAssistant(new Configuration(), "resource");
144+
expectedException.expect(BuilderException.class);
145+
expectedException.expectMessage(is("The mapper element requires a namespace attribute to be specified."));
146+
builder.setCurrentNamespace(null);
147+
}
148+
149+
@Test
150+
public void useCacheRefNamespaceIsNull() {
151+
MapperBuilderAssistant builder = new MapperBuilderAssistant(new Configuration(), "resource");
152+
expectedException.expect(BuilderException.class);
153+
expectedException.expectMessage(is("cache-ref element requires a namespace attribute."));
154+
builder.useCacheRef(null);
155+
}
156+
157+
@Test
158+
public void useCacheRefNamespaceIsUndefined() {
159+
MapperBuilderAssistant builder = new MapperBuilderAssistant(new Configuration(), "resource");
160+
expectedException.expect(IncompleteElementException.class);
161+
expectedException.expectMessage(is("No cache for namespace 'eee' could be found."));
162+
builder.useCacheRef("eee");
163+
}
164+
36165
// @Test
37166
// public void shouldNotLoadTheSameNamespaceFromTwoResourcesWithDifferentNames() throws Exception {
38167
// Configuration configuration = new Configuration();

0 commit comments

Comments
 (0)