1   /*
2    * Copyright (c) 1998, 2005 Gargoyle Software Inc. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    *
7    * 1. Redistributions of source code must retain the above copyright notice,
8    *    this list of conditions and the following disclaimer.
9    * 2. Redistributions in binary form must reproduce the above copyright notice,
10   *    this list of conditions and the following disclaimer in the documentation
11   *    and/or other materials provided with the distribution.
12   * 3. The end-user documentation included with the redistribution, if any, must
13   *    include the following acknowledgment:
14   *
15   *       "This product includes software developed by Gargoyle Software Inc.
16   *        (http://www.GargoyleSoftware.com/)."
17   *
18   *    Alternately, this acknowledgment may appear in the software itself, if
19   *    and wherever such third-party acknowledgments normally appear.
20   * 4. The name "Gargoyle Software" must not be used to endorse or promote
21   *    products derived from this software without prior written permission.
22   *    For written permission, please contact info@GargoyleSoftware.com.
23   * 5. Products derived from this software may not be called "GSBase", nor may
24   *    "GSBase" appear in their name, without prior written permission of
25   *    Gargoyle Software Inc.
26   *
27   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30   * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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             // Expected path
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             // Expected path
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             // Expected path
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             // Expected path
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