Skip to content

Commit a8a8f1f

Browse files
authored
Merge pull request #842 from kazuki43zoo/add-attribute-CacheNamespaceRef
Add name attribute at '@CacheNamespaceRef'
2 parents 1cbf994 + 26b5d2e commit a8a8f1f

File tree

6 files changed

+122
-27
lines changed

6 files changed

+122
-27
lines changed

src/main/java/org/apache/ibatis/annotations/CacheNamespaceRef.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,24 @@
2222
import java.lang.annotation.Target;
2323

2424
/**
25+
* The annotation that reference a cache.
26+
* <p>
27+
* If you use this annotation, should be specified either {@link #value()} and {@link #name()} attribute.
28+
* </p>
2529
* @author Clinton Begin
30+
* @author Kazuki Shimizu
2631
*/
2732
@Documented
2833
@Retention(RetentionPolicy.RUNTIME)
2934
@Target(ElementType.TYPE)
3035
public @interface CacheNamespaceRef {
31-
Class<?> value();
36+
/**
37+
* A namespace type to reference a cache (the namespace name become a FQCN of specified type)
38+
*/
39+
Class<?> value() default void.class;
40+
/**
41+
* A namespace name to reference a cache
42+
* @since 3.4.2
43+
*/
44+
String name() default "";
3245
}

src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,16 @@ private Properties convertToProperties(Property[] properties) {
200200
private void parseCacheRef() {
201201
CacheNamespaceRef cacheDomainRef = type.getAnnotation(CacheNamespaceRef.class);
202202
if (cacheDomainRef != null) {
203-
assistant.useCacheRef(cacheDomainRef.value().getName());
203+
Class<?> refType = cacheDomainRef.value();
204+
String refName = cacheDomainRef.name();
205+
if (refType == void.class && refName.isEmpty()) {
206+
throw new BuilderException("Should be specified either value() or name() attribute in the @CacheNamespaceRef");
207+
}
208+
if (refType != void.class && !refName.isEmpty()) {
209+
throw new BuilderException("Cannot use both value() and name() attribute in the @CacheNamespaceRef");
210+
}
211+
String namespace = (refType != void.class) ? refType.getName() : refName;
212+
assistant.useCacheRef(namespace);
204213
}
205214
}
206215

src/test/java/org/apache/ibatis/submitted/cache/CacheTest.java

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.apache.ibatis.annotations.Property;
2424
import org.apache.ibatis.cache.Cache;
2525
import org.apache.ibatis.cache.CacheException;
26+
import org.apache.ibatis.annotations.CacheNamespaceRef;
27+
import org.apache.ibatis.builder.BuilderException;
2628
import org.apache.ibatis.io.Resources;
2729
import org.apache.ibatis.jdbc.ScriptRunner;
2830
import 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
}

src/test/java/org/apache/ibatis/submitted/cache/ImportantPersonMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import java.util.List;
2424

25-
@CacheNamespaceRef(PersonMapper.class)
25+
@CacheNamespaceRef(PersonMapper.class) // by type
2626
public interface ImportantPersonMapper {
2727

2828
@Select("select id, firstname, lastname from person")
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright 2009-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.cache;
17+
18+
import org.apache.ibatis.annotations.CacheNamespaceRef;
19+
import org.apache.ibatis.annotations.Options;
20+
import org.apache.ibatis.annotations.Options.FlushCachePolicy;
21+
import org.apache.ibatis.annotations.Select;
22+
23+
import java.util.List;
24+
25+
@CacheNamespaceRef(name = "org.apache.ibatis.submitted.cache.PersonMapper") // by name
26+
public interface SpecialPersonMapper {
27+
28+
@Select("select id, firstname, lastname from person")
29+
@Options(flushCache = FlushCachePolicy.TRUE)
30+
List<Person> findWithFlushCache();
31+
32+
}

src/test/java/org/apache/ibatis/submitted/cache/mybatis-config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@
4545
<mapper class="org.apache.ibatis.submitted.cache.PersonMapper"/>
4646
<mapper class="org.apache.ibatis.submitted.cache.ImportantPersonMapper"/>
4747
<mapper class="org.apache.ibatis.submitted.cache.CustomCacheMapper"/>
48+
<mapper class="org.apache.ibatis.submitted.cache.SpecialPersonMapper"/>
4849
</mappers>
4950
</configuration>

0 commit comments

Comments
 (0)