Skip to content

Commit ff39b4a

Browse files
authored
Merge pull request #847 from kazuki43zoo/add-test-for-cache
Add tests for injecting property value to cache object
2 parents 97a1cf5 + 26e4b43 commit ff39b4a

File tree

3 files changed

+169
-4
lines changed

3 files changed

+169
-4
lines changed

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,29 @@
1919
import java.lang.reflect.Field;
2020
import java.sql.Connection;
2121

22+
import org.apache.ibatis.annotations.CacheNamespace;
23+
import org.apache.ibatis.annotations.Property;
2224
import org.apache.ibatis.cache.Cache;
25+
import org.apache.ibatis.cache.CacheException;
2326
import org.apache.ibatis.io.Resources;
2427
import org.apache.ibatis.jdbc.ScriptRunner;
2528
import org.apache.ibatis.session.SqlSession;
2629
import org.apache.ibatis.session.SqlSessionFactory;
2730
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
2831
import org.junit.Assert;
2932
import org.junit.Before;
33+
import org.junit.Rule;
3034
import org.junit.Test;
35+
import org.junit.rules.ExpectedException;
3136

3237
// issue #524
3338
public class CacheTest {
3439

3540
private static SqlSessionFactory sqlSessionFactory;
3641

42+
@Rule
43+
public ExpectedException expectedException = ExpectedException.none();
44+
3745
@Before
3846
public void setUp() throws Exception {
3947
// create a SqlSessionFactory
@@ -309,8 +317,28 @@ public void shouldApplyCacheNamespaceRef() {
309317
public void shouldApplyCustomCacheProperties() {
310318
CustomCache customCache = unwrap(sqlSessionFactory.getConfiguration().getCache(CustomCacheMapper.class.getName()));
311319
Assert.assertEquals("bar", customCache.getStringValue());
312-
Assert.assertEquals(99, customCache.getIntegerValue().intValue());
313-
Assert.assertEquals(9999, customCache.getLongValue());
320+
Assert.assertEquals(1, customCache.getIntegerValue().intValue());
321+
Assert.assertEquals(2, customCache.getIntValue());
322+
Assert.assertEquals(3, customCache.getLongWrapperValue().longValue());
323+
Assert.assertEquals(4, customCache.getLongValue());
324+
Assert.assertEquals(5, customCache.getShortWrapperValue().shortValue());
325+
Assert.assertEquals(6, customCache.getShortValue());
326+
Assert.assertEquals((float) 7.1, customCache.getFloatWrapperValue(), 0);
327+
Assert.assertEquals((float)8.1, customCache.getFloatValue(), 0);
328+
Assert.assertEquals(9.01, customCache.getDoubleWrapperValue(), 0);
329+
Assert.assertEquals(10.01, customCache.getDoubleValue(), 0);
330+
Assert.assertEquals((byte)11, customCache.getByteWrapperValue().byteValue());
331+
Assert.assertEquals((byte)12, customCache.getByteValue());
332+
Assert.assertEquals(true, customCache.getBooleanWrapperValue());
333+
Assert.assertEquals(true, customCache.isBooleanValue());
334+
}
335+
336+
@Test
337+
public void shouldErrorUnsupportedProperties() {
338+
expectedException.expect(CacheException.class);
339+
expectedException.expectMessage("Unsupported property type for cache: 'date' of type class java.util.Date");
340+
341+
sqlSessionFactory.getConfiguration().addMapper(CustomCacheUnsupportedPropertyMapper.class);
314342
}
315343

316344
private CustomCache unwrap(Cache cache){
@@ -330,4 +358,10 @@ private CustomCache unwrap(Cache cache){
330358
}
331359
}
332360

361+
@CacheNamespace(implementation = CustomCache.class, properties = {
362+
@Property(name = "date", value = "2016/11/21")
363+
})
364+
private interface CustomCacheUnsupportedPropertyMapper {
365+
}
366+
333367
}

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

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,26 @@
1717

1818
import org.apache.ibatis.cache.impl.PerpetualCache;
1919

20+
import java.util.Date;
21+
2022
public class CustomCache extends PerpetualCache {
2123

2224
private String stringValue;
2325
private Integer integerValue;
26+
private int intValue;
27+
private Long longWrapperValue;
2428
private long longValue;
29+
private Short shortWrapperValue;
30+
private short shortValue;
31+
private Float floatWrapperValue;
32+
private float floatValue;
33+
private Double doubleWrapperValue;
34+
private double doubleValue;
35+
private Byte byteWrapperValue;
36+
private byte byteValue;
37+
private Boolean booleanWrapperValue;
38+
private boolean booleanValue;
39+
private Date date;
2540

2641
public CustomCache(String id) {
2742
super(id);
@@ -51,4 +66,108 @@ public void setLongValue(long longValue) {
5166
this.longValue = longValue;
5267
}
5368

69+
public int getIntValue() {
70+
return intValue;
71+
}
72+
73+
public void setIntValue(int intValue) {
74+
this.intValue = intValue;
75+
}
76+
77+
public Long getLongWrapperValue() {
78+
return longWrapperValue;
79+
}
80+
81+
public void setLongWrapperValue(Long longWrapperValue) {
82+
this.longWrapperValue = longWrapperValue;
83+
}
84+
85+
public Short getShortWrapperValue() {
86+
return shortWrapperValue;
87+
}
88+
89+
public void setShortWrapperValue(Short shortWrapperValue) {
90+
this.shortWrapperValue = shortWrapperValue;
91+
}
92+
93+
public short getShortValue() {
94+
return shortValue;
95+
}
96+
97+
public void setShortValue(short shortValue) {
98+
this.shortValue = shortValue;
99+
}
100+
101+
public Float getFloatWrapperValue() {
102+
return floatWrapperValue;
103+
}
104+
105+
public void setFloatWrapperValue(Float floatWrapperValue) {
106+
this.floatWrapperValue = floatWrapperValue;
107+
}
108+
109+
public float getFloatValue() {
110+
return floatValue;
111+
}
112+
113+
public void setFloatValue(float floatValue) {
114+
this.floatValue = floatValue;
115+
}
116+
117+
public Double getDoubleWrapperValue() {
118+
return doubleWrapperValue;
119+
}
120+
121+
public void setDoubleWrapperValue(Double doubleWrapperValue) {
122+
this.doubleWrapperValue = doubleWrapperValue;
123+
}
124+
125+
public double getDoubleValue() {
126+
return doubleValue;
127+
}
128+
129+
public void setDoubleValue(double doubleValue) {
130+
this.doubleValue = doubleValue;
131+
}
132+
133+
public Byte getByteWrapperValue() {
134+
return byteWrapperValue;
135+
}
136+
137+
public void setByteWrapperValue(Byte byteWrapperValue) {
138+
this.byteWrapperValue = byteWrapperValue;
139+
}
140+
141+
public byte getByteValue() {
142+
return byteValue;
143+
}
144+
145+
public void setByteValue(byte byteValue) {
146+
this.byteValue = byteValue;
147+
}
148+
149+
public Boolean getBooleanWrapperValue() {
150+
return booleanWrapperValue;
151+
}
152+
153+
public void setBooleanWrapperValue(Boolean booleanWrapperValue) {
154+
this.booleanWrapperValue = booleanWrapperValue;
155+
}
156+
157+
public boolean isBooleanValue() {
158+
return booleanValue;
159+
}
160+
161+
public void setBooleanValue(boolean booleanValue) {
162+
this.booleanValue = booleanValue;
163+
}
164+
165+
public Date getDate() {
166+
return date;
167+
}
168+
169+
public void setDate(Date date) {
170+
this.date = date;
171+
}
172+
54173
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,20 @@
2020

2121
@CacheNamespace(implementation = CustomCache.class, properties = {
2222
@Property(name = "stringValue", value = "bar"),
23-
@Property(name = "integerValue", value = "99"),
24-
@Property(name = "longValue", value = "9999")
23+
@Property(name = "integerValue", value = "1"),
24+
@Property(name = "intValue", value = "2"),
25+
@Property(name = "longWrapperValue", value = "3"),
26+
@Property(name = "longValue", value = "4"),
27+
@Property(name = "shortWrapperValue", value = "5"),
28+
@Property(name = "shortValue", value = "6"),
29+
@Property(name = "floatWrapperValue", value = "7.1"),
30+
@Property(name = "floatValue", value = "8.1"),
31+
@Property(name = "doubleWrapperValue", value = "9.01"),
32+
@Property(name = "doubleValue", value = "10.01"),
33+
@Property(name = "byteWrapperValue", value = "11"),
34+
@Property(name = "byteValue", value = "12"),
35+
@Property(name = "booleanWrapperValue", value = "true"),
36+
@Property(name = "booleanValue", value = "true")
2537
})
2638
public interface CustomCacheMapper {
2739
}

0 commit comments

Comments
 (0)