2323import org .apache .ibatis .annotations .Property ;
2424import org .apache .ibatis .cache .Cache ;
2525import org .apache .ibatis .cache .CacheException ;
26+ import org .apache .ibatis .annotations .CacheNamespaceRef ;
27+ import org .apache .ibatis .builder .BuilderException ;
2628import org .apache .ibatis .io .Resources ;
2729import org .apache .ibatis .jdbc .ScriptRunner ;
2830import org .apache .ibatis .session .SqlSession ;
@@ -59,7 +61,7 @@ public void setUp() throws Exception {
5961 reader .close ();
6062 session .close ();
6163 }
62-
64+
6365 /*
6466 * Test Plan:
6567 * 1) SqlSession 1 executes "select * from A".
@@ -76,23 +78,21 @@ public void testplan1() {
7678 try {
7779 PersonMapper pm = sqlSession1 .getMapper (PersonMapper .class );
7880 Assert .assertEquals (2 , pm .findAll ().size ());
79- }
80- finally {
81+ } finally {
8182 sqlSession1 .close ();
8283 }
83-
84+
8485 SqlSession sqlSession2 = sqlSessionFactory .openSession (false );
8586 try {
8687 PersonMapper pm = sqlSession2 .getMapper (PersonMapper .class );
8788 pm .delete (1 );
8889 Assert .assertEquals (1 , pm .findAll ().size ());
89- }
90- finally {
90+ } finally {
9191 sqlSession2 .commit ();
9292 sqlSession2 .close ();
9393 }
9494 }
95-
95+
9696 /*
9797 * Test Plan:
9898 * 1) SqlSession 1 executes "select * from A".
@@ -111,31 +111,28 @@ public void testplan2() {
111111 try {
112112 PersonMapper pm = sqlSession1 .getMapper (PersonMapper .class );
113113 Assert .assertEquals (2 , pm .findAll ().size ());
114- }
115- finally {
114+ } finally {
116115 sqlSession1 .close ();
117116 }
118-
117+
119118 SqlSession sqlSession2 = sqlSessionFactory .openSession (false );
120119 try {
121120 PersonMapper pm = sqlSession2 .getMapper (PersonMapper .class );
122121 pm .delete (1 );
123- }
124- finally {
122+ } finally {
125123 sqlSession2 .rollback ();
126124 sqlSession2 .close ();
127125 }
128-
126+
129127 SqlSession sqlSession3 = sqlSessionFactory .openSession (false );
130128 try {
131129 PersonMapper pm = sqlSession3 .getMapper (PersonMapper .class );
132130 Assert .assertEquals (2 , pm .findAll ().size ());
133- }
134- finally {
131+ } finally {
135132 sqlSession3 .close ();
136133 }
137134 }
138-
135+
139136 /*
140137 * Test Plan with Autocommit on:
141138 * 1) SqlSession 1 executes "select * from A".
@@ -154,26 +151,23 @@ public void testplan3() {
154151 try {
155152 PersonMapper pm = sqlSession1 .getMapper (PersonMapper .class );
156153 Assert .assertEquals (2 , pm .findAll ().size ());
157- }
158- finally {
154+ } finally {
159155 sqlSession1 .close ();
160156 }
161-
157+
162158 SqlSession sqlSession2 = sqlSessionFactory .openSession (true );
163159 try {
164160 PersonMapper pm = sqlSession2 .getMapper (PersonMapper .class );
165161 pm .delete (1 );
166- }
167- finally {
162+ } finally {
168163 sqlSession2 .close ();
169164 }
170-
165+
171166 SqlSession sqlSession3 = sqlSessionFactory .openSession (true );
172167 try {
173168 PersonMapper pm = sqlSession3 .getMapper (PersonMapper .class );
174169 Assert .assertEquals (1 , pm .findAll ().size ());
175- }
176- finally {
170+ } finally {
177171 sqlSession3 .close ();
178172 }
179173 }
@@ -307,6 +301,26 @@ public void shouldApplyCacheNamespaceRef() {
307301 try {
308302 PersonMapper pm = sqlSession .getMapper (PersonMapper .class );
309303 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 ());
310324 } finally {
311325 sqlSession .close ();
312326 }
@@ -341,6 +355,24 @@ public void shouldErrorUnsupportedProperties() {
341355 sqlSessionFactory .getConfiguration ().addMapper (CustomCacheUnsupportedPropertyMapper .class );
342356 }
343357
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+
344376 private CustomCache unwrap (Cache cache ){
345377 Field field ;
346378 try {
@@ -364,4 +396,12 @@ private CustomCache unwrap(Cache cache){
364396 private interface CustomCacheUnsupportedPropertyMapper {
365397 }
366398
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+
367407}
0 commit comments