23
23
import org .apache .ibatis .annotations .Property ;
24
24
import org .apache .ibatis .cache .Cache ;
25
25
import org .apache .ibatis .cache .CacheException ;
26
+ import org .apache .ibatis .annotations .CacheNamespaceRef ;
27
+ import org .apache .ibatis .builder .BuilderException ;
26
28
import org .apache .ibatis .io .Resources ;
27
29
import org .apache .ibatis .jdbc .ScriptRunner ;
28
30
import org .apache .ibatis .session .SqlSession ;
@@ -59,7 +61,7 @@ public void setUp() throws Exception {
59
61
reader .close ();
60
62
session .close ();
61
63
}
62
-
64
+
63
65
/*
64
66
* Test Plan:
65
67
* 1) SqlSession 1 executes "select * from A".
@@ -76,23 +78,21 @@ public void testplan1() {
76
78
try {
77
79
PersonMapper pm = sqlSession1 .getMapper (PersonMapper .class );
78
80
Assert .assertEquals (2 , pm .findAll ().size ());
79
- }
80
- finally {
81
+ } finally {
81
82
sqlSession1 .close ();
82
83
}
83
-
84
+
84
85
SqlSession sqlSession2 = sqlSessionFactory .openSession (false );
85
86
try {
86
87
PersonMapper pm = sqlSession2 .getMapper (PersonMapper .class );
87
88
pm .delete (1 );
88
89
Assert .assertEquals (1 , pm .findAll ().size ());
89
- }
90
- finally {
90
+ } finally {
91
91
sqlSession2 .commit ();
92
92
sqlSession2 .close ();
93
93
}
94
94
}
95
-
95
+
96
96
/*
97
97
* Test Plan:
98
98
* 1) SqlSession 1 executes "select * from A".
@@ -111,31 +111,28 @@ public void testplan2() {
111
111
try {
112
112
PersonMapper pm = sqlSession1 .getMapper (PersonMapper .class );
113
113
Assert .assertEquals (2 , pm .findAll ().size ());
114
- }
115
- finally {
114
+ } finally {
116
115
sqlSession1 .close ();
117
116
}
118
-
117
+
119
118
SqlSession sqlSession2 = sqlSessionFactory .openSession (false );
120
119
try {
121
120
PersonMapper pm = sqlSession2 .getMapper (PersonMapper .class );
122
121
pm .delete (1 );
123
- }
124
- finally {
122
+ } finally {
125
123
sqlSession2 .rollback ();
126
124
sqlSession2 .close ();
127
125
}
128
-
126
+
129
127
SqlSession sqlSession3 = sqlSessionFactory .openSession (false );
130
128
try {
131
129
PersonMapper pm = sqlSession3 .getMapper (PersonMapper .class );
132
130
Assert .assertEquals (2 , pm .findAll ().size ());
133
- }
134
- finally {
131
+ } finally {
135
132
sqlSession3 .close ();
136
133
}
137
134
}
138
-
135
+
139
136
/*
140
137
* Test Plan with Autocommit on:
141
138
* 1) SqlSession 1 executes "select * from A".
@@ -154,26 +151,23 @@ public void testplan3() {
154
151
try {
155
152
PersonMapper pm = sqlSession1 .getMapper (PersonMapper .class );
156
153
Assert .assertEquals (2 , pm .findAll ().size ());
157
- }
158
- finally {
154
+ } finally {
159
155
sqlSession1 .close ();
160
156
}
161
-
157
+
162
158
SqlSession sqlSession2 = sqlSessionFactory .openSession (true );
163
159
try {
164
160
PersonMapper pm = sqlSession2 .getMapper (PersonMapper .class );
165
161
pm .delete (1 );
166
- }
167
- finally {
162
+ } finally {
168
163
sqlSession2 .close ();
169
164
}
170
-
165
+
171
166
SqlSession sqlSession3 = sqlSessionFactory .openSession (true );
172
167
try {
173
168
PersonMapper pm = sqlSession3 .getMapper (PersonMapper .class );
174
169
Assert .assertEquals (1 , pm .findAll ().size ());
175
- }
176
- finally {
170
+ } finally {
177
171
sqlSession3 .close ();
178
172
}
179
173
}
@@ -307,6 +301,26 @@ public void shouldApplyCacheNamespaceRef() {
307
301
try {
308
302
PersonMapper pm = sqlSession .getMapper (PersonMapper .class );
309
303
Assert .assertEquals (3 , pm .findAll ().size ());
304
+ Person p = new Person (4 , "foo" , "bar" );
305
+ pm .createWithoutFlushCache (p );
306
+ } finally {
307
+ sqlSession .close ();
308
+ }
309
+ }
310
+ {
311
+ SqlSession sqlSession = sqlSessionFactory .openSession (true );
312
+ try {
313
+ SpecialPersonMapper pm = sqlSession .getMapper (SpecialPersonMapper .class );
314
+ Assert .assertEquals (4 , pm .findWithFlushCache ().size ());
315
+ } finally {
316
+ sqlSession .close ();
317
+ }
318
+ }
319
+ {
320
+ SqlSession sqlSession = sqlSessionFactory .openSession (true );
321
+ try {
322
+ PersonMapper pm = sqlSession .getMapper (PersonMapper .class );
323
+ Assert .assertEquals (4 , pm .findAll ().size ());
310
324
} finally {
311
325
sqlSession .close ();
312
326
}
@@ -341,6 +355,24 @@ public void shouldErrorUnsupportedProperties() {
341
355
sqlSessionFactory .getConfiguration ().addMapper (CustomCacheUnsupportedPropertyMapper .class );
342
356
}
343
357
358
+ @ Test
359
+ public void shouldErrorInvalidCacheNamespaceRefAttributesSpecifyBoth () {
360
+ expectedException .expect (BuilderException .class );
361
+ expectedException .expectMessage ("Cannot use both value() and name() attribute in the @CacheNamespaceRef" );
362
+
363
+ sqlSessionFactory .getConfiguration ().getMapperRegistry ()
364
+ .addMapper (InvalidCacheNamespaceRefBothMapper .class );
365
+ }
366
+
367
+ @ Test
368
+ public void shouldErrorInvalidCacheNamespaceRefAttributesIsEmpty () {
369
+ expectedException .expect (BuilderException .class );
370
+ expectedException .expectMessage ("Should be specified either value() or name() attribute in the @CacheNamespaceRef" );
371
+
372
+ sqlSessionFactory .getConfiguration ().getMapperRegistry ()
373
+ .addMapper (InvalidCacheNamespaceRefEmptyMapper .class );
374
+ }
375
+
344
376
private CustomCache unwrap (Cache cache ){
345
377
Field field ;
346
378
try {
@@ -364,4 +396,12 @@ private CustomCache unwrap(Cache cache){
364
396
private interface CustomCacheUnsupportedPropertyMapper {
365
397
}
366
398
399
+ @ CacheNamespaceRef (value = PersonMapper .class , name = "org.apache.ibatis.submitted.cache.PersonMapper" )
400
+ private interface InvalidCacheNamespaceRefBothMapper {
401
+ }
402
+
403
+ @ CacheNamespaceRef
404
+ private interface InvalidCacheNamespaceRefEmptyMapper {
405
+ }
406
+
367
407
}
0 commit comments