1
1
/**
2
- * Copyright 2009-2015 the original author or authors.
2
+ * Copyright 2009-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
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 .hamcrest .CoreMatchers ;
29
+ import org .junit .Rule ;
23
30
import org .junit .Test ;
31
+ import org .junit .rules .ExpectedException ;
32
+
33
+ import static org .junit .Assert .assertThat ;
34
+ import static org .hamcrest .CoreMatchers .*;
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,121 @@ 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 (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
+
36
165
// @Test
37
166
// public void shouldNotLoadTheSameNamespaceFromTwoResourcesWithDifferentNames() throws Exception {
38
167
// Configuration configuration = new Configuration();
0 commit comments