41
41
import org .springframework .context .annotation .Bean ;
42
42
import org .springframework .context .annotation .Configuration ;
43
43
44
+ import static org .junit .Assert .*;
44
45
import static org .mockito .BDDMockito .*;
45
46
46
47
/**
@@ -53,55 +54,94 @@ public class JCacheErrorHandlerTests {
53
54
54
55
private Cache cache ;
55
56
57
+ private Cache errorCache ;
58
+
56
59
private CacheErrorHandler errorHandler ;
57
60
58
61
private SimpleService simpleService ;
59
62
60
63
@ Before
61
64
public void setup () {
62
- AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (Config .class );
65
+ AnnotationConfigApplicationContext context =
66
+ new AnnotationConfigApplicationContext (Config .class );
63
67
this .cache = context .getBean ("mockCache" , Cache .class );
68
+ this .errorCache = context .getBean ("mockErrorCache" , Cache .class );
64
69
this .errorHandler = context .getBean (CacheErrorHandler .class );
65
70
this .simpleService = context .getBean (SimpleService .class );
66
71
}
67
72
68
73
@ Test
69
74
public void getFail () {
70
- UnsupportedOperationException exception = new UnsupportedOperationException ("Test exception on get" );
75
+ UnsupportedOperationException exception =
76
+ new UnsupportedOperationException ("Test exception on get" );
77
+ Object key = SimpleKeyGenerator .generateKey (0L );
78
+ willThrow (exception ).given (this .cache ).get (key );
79
+
80
+ this .simpleService .get (0L );
81
+ verify (this .errorHandler ).handleCacheGetError (exception , this .cache , key );
82
+ }
83
+
84
+ @ Test
85
+ public void getPutNewElementFail () {
86
+ UnsupportedOperationException exception =
87
+ new UnsupportedOperationException ("Test exception on put" );
71
88
Object key = SimpleKeyGenerator .generateKey (0L );
72
- willThrow (exception ).given (cache ).get (key );
89
+ given (this .cache .get (key )).willReturn (null );
90
+ willThrow (exception ).given (this .cache ).put (key , 0L );
73
91
74
92
this .simpleService .get (0L );
75
- verify (errorHandler ).handleCacheGetError (exception , cache , key );
93
+ verify (this .errorHandler ).handleCachePutError (exception , this .cache , key , 0L );
94
+ }
95
+
96
+ @ Test
97
+ public void getFailPutExceptionFail () {
98
+ UnsupportedOperationException exceptionOnPut =
99
+ new UnsupportedOperationException ("Test exception on put" );
100
+ Object key = SimpleKeyGenerator .generateKey (0L );
101
+ given (this .cache .get (key )).willReturn (null );
102
+ willThrow (exceptionOnPut ).given (this .errorCache ).put (key ,
103
+ SimpleService .TEST_EXCEPTION );
104
+
105
+ try {
106
+ this .simpleService .getFail (0L );
107
+ }
108
+ catch (IllegalStateException ex ) {
109
+ assertEquals ("Test exception" , ex .getMessage ());
110
+ }
111
+ verify (this .errorHandler ).handleCachePutError (exceptionOnPut ,
112
+ this .errorCache , key , SimpleService .TEST_EXCEPTION );
76
113
}
77
114
78
115
@ Test
79
116
public void putFail () {
80
- UnsupportedOperationException exception = new UnsupportedOperationException ("Test exception on put" );
117
+ UnsupportedOperationException exception =
118
+ new UnsupportedOperationException ("Test exception on put" );
81
119
Object key = SimpleKeyGenerator .generateKey (0L );
82
- willThrow (exception ).given (cache ).put (key , 234L );
120
+ willThrow (exception ).given (this . cache ).put (key , 234L );
83
121
84
122
this .simpleService .put (0L , 234L );
85
- verify (errorHandler ).handleCachePutError (exception , cache , key , 234L );
123
+ verify (this . errorHandler ).handleCachePutError (exception , this . cache , key , 234L );
86
124
}
87
125
88
126
@ Test
89
127
public void evictFail () {
90
- UnsupportedOperationException exception = new UnsupportedOperationException ("Test exception on evict" );
128
+ UnsupportedOperationException exception =
129
+ new UnsupportedOperationException ("Test exception on evict" );
91
130
Object key = SimpleKeyGenerator .generateKey (0L );
92
- willThrow (exception ).given (cache ).evict (key );
131
+ willThrow (exception ).given (this . cache ).evict (key );
93
132
94
133
this .simpleService .evict (0L );
95
- verify (errorHandler ).handleCacheEvictError (exception , cache , key );
134
+ verify (this . errorHandler ).handleCacheEvictError (exception , this . cache , key );
96
135
}
97
136
98
137
@ Test
99
138
public void clearFail () {
100
- UnsupportedOperationException exception = new UnsupportedOperationException ("Test exception on evict" );
101
- willThrow (exception ).given (cache ).clear ();
139
+ UnsupportedOperationException exception =
140
+ new UnsupportedOperationException ("Test exception on evict" );
141
+ willThrow (exception ).given (this .cache ).clear ();
102
142
103
143
this .simpleService .clear ();
104
- verify (errorHandler ).handleCacheClearError (exception , cache );
144
+ verify (this . errorHandler ).handleCacheClearError (exception , this . cache );
105
145
}
106
146
107
147
@@ -113,7 +153,7 @@ static class Config extends JCacheConfigurerSupport {
113
153
@ Override
114
154
public CacheManager cacheManager () {
115
155
SimpleCacheManager cacheManager = new SimpleCacheManager ();
116
- cacheManager .setCaches (Arrays .asList (mockCache ()));
156
+ cacheManager .setCaches (Arrays .asList (mockCache (), mockErrorCache () ));
117
157
return cacheManager ;
118
158
}
119
159
@@ -135,15 +175,31 @@ public Cache mockCache() {
135
175
return cache ;
136
176
}
137
177
178
+ @ Bean
179
+ public Cache mockErrorCache () {
180
+ Cache cache = mock (Cache .class );
181
+ given (cache .getName ()).willReturn ("error" );
182
+ return cache ;
183
+ }
184
+
138
185
}
139
186
140
187
@ CacheDefaults (cacheName = "test" )
141
188
public static class SimpleService {
189
+
190
+ private static final IllegalStateException TEST_EXCEPTION =
191
+ new IllegalStateException ("Test exception" );
192
+
142
193
private AtomicLong counter = new AtomicLong ();
143
194
144
195
@ CacheResult
145
196
public Object get (long id ) {
146
- return counter .getAndIncrement ();
197
+ return this .counter .getAndIncrement ();
198
+ }
199
+
200
+ @ CacheResult (exceptionCacheName = "error" )
201
+ public Object getFail (long id ) {
202
+ throw TEST_EXCEPTION ;
147
203
}
148
204
149
205
@ CachePut
0 commit comments