1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package com.gargoylesoftware.base.objectstore;
39
40 import com.gargoylesoftware.base.resource.ResourceFactory;
41 import com.gargoylesoftware.base.resource.ResourceManager;
42 import com.gargoylesoftware.base.resource.ProxyResourceFactory;
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.List;
46 import junit.framework.TestCase;
47
48 /***
49 * Tests for object store.
50 *
51 * @version $Revision: 1.8 $
52 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
53 */
54 public class ObjectStoreTest extends TestCase {
55
56 /***
57 * Create an instance
58 *
59 * @param name The name of the test
60 */
61 public ObjectStoreTest( final String name ) {
62 super( name );
63 }
64
65
66 /***
67 * Test that exceptions are being wrapped in an ObjectStoreException
68 */
69 public void testService_ExceptionThrown() {
70 final Exception exception = new NullPointerException();
71 final ExceptionObjectStore store = new ExceptionObjectStore( exception );
72 final List collectedExceptions = new ArrayList();
73 final List collectedErrors = new ArrayList();
74 final List expectedExceptions = new ArrayList();
75
76 expectedExceptions.add( exception );
77
78 store.collectExceptionsInto( collectedExceptions );
79 store.collectErrorsInto( collectedErrors );
80
81 try {
82 store.execute( new FakeObjectStoreCommand() );
83 fail( "Expected exception" );
84 }
85 catch( final ObjectStoreException e ) {
86 assertEquals( exception, e.getException() );
87 }
88
89 assertEquals( expectedExceptions, collectedExceptions );
90 assertEquals( new ArrayList(), collectedErrors );
91 }
92
93
94 /***
95 * Test that exceptions are being wrapped in an ObjectStoreException
96 */
97 public void testService_ErrorThrown() {
98 final Error error = new OutOfMemoryError();
99 final ExceptionObjectStore store = new ExceptionObjectStore( error );
100 final List collectedExceptions = new ArrayList();
101 final List collectedErrors = new ArrayList();
102 final List expectedErrors = new ArrayList();
103
104 expectedErrors.add( error );
105
106 store.collectExceptionsInto( collectedExceptions );
107 store.collectErrorsInto( collectedErrors );
108
109 try {
110 store.execute( new FakeObjectStoreCommand() );
111 fail( "Expected exception" );
112 }
113 catch( final OutOfMemoryError e ) {
114
115 }
116
117 assertEquals( new ArrayList(), collectedExceptions );
118 assertEquals( expectedErrors, collectedErrors );
119 }
120
121
122 /***
123 * Ensure that ObjectStoreExceptions are not being wrapped
124 */
125 public void testService_ObjectStoreExceptionThrown() {
126 final Exception exception = new ObjectStoreException( "foo" );
127
128 try {
129 new ExceptionObjectStore( exception ).execute( new FakeObjectStoreCommand() );
130 fail( "Expected exception" );
131 }
132 catch( final ObjectStoreException e ) {
133 assertEquals( exception, e );
134 }
135 }
136
137
138 /***
139 * Ensure that ObjectStoreCommandNotSupportedExceptions are not being
140 * wrapped
141 * @deprecated
142 */
143 public void testService_ObjectStoreCommandNotSupportedExceptionThrown() {
144 final Exception exception = new ObjectStoreCommandNotSupportedException( "foo" );
145
146 try {
147 new ExceptionObjectStore( exception ).execute( new FakeObjectStoreCommand() );
148 fail( "Expected exception" );
149 }
150 catch( final ObjectStoreCommandNotSupportedException e ) {
151 assertEquals( exception, e );
152 }
153 }
154
155
156
157 /***
158 * Ensure that an exception is being thrown when null is passed into
159 * setResourceMap()
160 */
161 public void testSetResourceMap_Null() {
162 try {
163 new EmptyObjectStore().setResourceFactoryMap( null );
164 fail( "Expected exception for null map" );
165 }
166 catch( final NullPointerException e ) {
167
168 }
169 }
170
171
172 /***
173 * Test setResourceMap()
174 */
175 public void testSetResourceMap() {
176 final ResourceFactory factory = new ProxyResourceFactory( new ArrayList() );
177 final ObjectStore store =
178 new ObjectStore() {
179 protected Object executeImpl( final ObjectStoreCommand command ) {
180 return getResource( "foo" );
181 }
182 };
183 store.setResourceFactoryMap( Collections.singletonMap( "foo", factory ) );
184 store.execute( new FakeObjectStoreCommand() );
185 }
186
187
188 /***
189 * The keys in the map must be strings. The values must be
190 * ResourceFactories.
191 */
192 public void testSetResourceMap_BadKey() {
193 final ObjectStore store = new EmptyObjectStore();
194 try {
195 store.setResourceFactoryMap( Collections.singletonMap( "jdbc", ProxyResourceFactory.class ) );
196 fail( "Expected exception for resource factory of wrong type" );
197 }
198 catch( final ClassCastException e ) {
199
200 }
201 }
202
203
204 /***
205 * The keys in the map must be strings. The values must be
206 * ResourceFactories.
207 */
208 public void testSetResourceMap_BadValue() {
209 final ObjectStore store = new EmptyObjectStore();
210 try {
211 store.setResourceFactoryMap( Collections.singletonMap( new Integer( 4 ), new ProxyResourceFactory() ) );
212 fail( "Expected exception for key of wrong type" );
213 }
214 catch( final ClassCastException e ) {
215
216 }
217 }
218
219
220 /***
221 * Test the accessors for ResourceManager
222 */
223 public void testSetResourceManager() {
224 final ResourceManager resourceManager = new ResourceManager( getName() );
225 final ObjectStore store = new EmptyObjectStore();
226 store.setResourceManager( resourceManager );
227 assertEquals( resourceManager, store.getResourceManager() );
228 }
229
230
231 private static class ExceptionObjectStore extends ObjectStore {
232 private final Throwable exception_;
233 private List collectedExceptions_;
234 private List collectedErrors_;
235
236
237 /***
238 * Create an instance of {1}
239 *
240 * @param e
241 */
242 public ExceptionObjectStore( final Throwable e ) {
243 exception_ = e;
244 }
245
246
247 protected Object executeImpl( final ObjectStoreCommand command )
248 throws Exception {
249 if( exception_ instanceof Exception ) {
250 throw ( Exception )exception_;
251 }
252
253 throw ( Error )exception_;
254 }
255
256
257 protected Object handleException( final Exception e ) {
258 if( collectedExceptions_ != null ) {
259 collectedExceptions_.add( e );
260 }
261 return super.handleException( e );
262 }
263
264
265 protected Object handleError( final Error e ) {
266 if( collectedErrors_ != null ) {
267 collectedErrors_.add( e );
268 }
269 return super.handleError( e );
270 }
271
272 /*** @param list The list that the exceptions will be placed within */
273 public void collectExceptionsInto( final List list ) {
274 collectedExceptions_ = list;
275 }
276
277 /*** @param list The list that the exceptions will be placed within */
278 public void collectErrorsInto( final List list ) {
279 collectedErrors_ = list;
280 }
281 }
282
283
284 private static class EmptyObjectStore extends ObjectStore {
285 protected Object executeImpl( final ObjectStoreCommand command ) {
286 return null;
287 }
288 }
289
290
291 private static class FakeObjectStoreCommand extends ObjectStoreCommand {
292
293 private static final long serialVersionUID = 1L;
294 }
295 }
296