16
16
package org .apache .ibatis .builder ;
17
17
18
18
import java .io .InputStream ;
19
+ import java .util .regex .Pattern ;
19
20
20
21
import org .apache .ibatis .builder .xml .XMLMapperBuilder ;
21
22
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 ;
22
26
import org .apache .ibatis .session .Configuration ;
27
+ import org .apache .ibatis .type .TypeHandler ;
28
+ import org .junit .Rule ;
23
29
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 ;
24
35
25
36
public class XmlMapperBuilderTest {
26
37
38
+ @ Rule
39
+ public ExpectedException expectedException = ExpectedException .none ();
40
+
27
41
@ Test
28
42
public void shouldSuccessfullyLoadXMLMapperFile () throws Exception {
29
43
Configuration configuration = new Configuration ();
@@ -33,6 +47,118 @@ public void shouldSuccessfullyLoadXMLMapperFile() throws Exception {
33
47
builder .parse ();
34
48
}
35
49
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
+
36
162
// @Test
37
163
// public void shouldNotLoadTheSameNamespaceFromTwoResourcesWithDifferentNames() throws Exception {
38
164
// Configuration configuration = new Configuration();
0 commit comments