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.resource;
39  
40  import junit.framework.TestCase;
41  
42  /***
43   *  Tests for com.gargoylesoftware.base.objectstore.ResourceFactory
44   *
45   * @version  $Revision: 1.7 $
46   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
47   */
48  public class ResourceFactoryTest extends TestCase {
49  
50      /***
51       *  Create a test
52       *
53       * @param  name The name of the test
54       */
55      public ResourceFactoryTest( final String name ) {
56          super( name );
57      }
58  
59  
60  
61      /***
62       *  Try passing null into getResource()
63       */
64      public void testGetResource_null() {
65          try {
66              new ProxyResourceFactory().getResource( null );
67              fail( "Expected exception" );
68          }
69          catch( final NullPointerException e ) {
70              // Expected path
71          }
72      }
73  
74  
75      /***
76       *  Try passing a null store into releaseResource()
77       */
78      public void testReleaseResource_nullStore() {
79          final ResourceManager resourceManager = new ResourceManager( getName() );
80          final ProxyResourceFactory factory = new ProxyResourceFactory();
81          final ManagedResource resource = factory.getResource( resourceManager );
82          try {
83              factory.releaseResource( null, resource );
84              fail( "Expected exception" );
85          }
86          catch( final NullPointerException e ) {
87              // Expected path
88          }
89      }
90  
91  
92      /***
93       *  Try passing a null resource into releaseResource()
94       */
95      public void testReleaseResource_nullResource() {
96          final ResourceManager resourceManager = new ResourceManager( getName() );
97          final ProxyResourceFactory factory = new ProxyResourceFactory();
98          try {
99              factory.releaseResource( resourceManager, null );
100             fail( "Expected exception" );
101         }
102         catch( final NullPointerException e ) {
103             // Expected path
104         }
105     }
106 
107 
108     /***
109      *  Try calling releaseResource() with a resource that wasn't allocated from
110      *  this factory.
111      */
112     public void testReleaseResource_NotAllocatedHere() {
113         final ResourceManager resourceManager = new ResourceManager( getName() );
114         final ProxyResourceFactory factory = new ProxyResourceFactory();
115         factory.getResource( resourceManager );
116         try {
117             factory.releaseResource( resourceManager, new ProxyResourceFactory.FakeResource( "foobar" ) );
118             fail( "Expected exception" );
119         }
120         catch( final ResourceException e ) {
121             // Expected path
122         }
123     }
124 
125 
126     /***
127      *  Try releasing the same resource twice in a row
128      */
129     public void testReleaseResource_ReleaseTwice() {
130         final ResourceManager resourceManager = new ResourceManager( getName() );
131         final ProxyResourceFactory factory = new ProxyResourceFactory();
132         final ManagedResource resource = factory.getResource( resourceManager );
133         factory.releaseResource( resourceManager, resource );
134 
135         try {
136             factory.releaseResource( resourceManager, resource );
137             fail( "Expected exception" );
138         }
139         catch( final ResourceException e ) {
140             // Expected path
141         }
142     }
143 
144 
145     /***
146      *  Test the normal conditions using releaseResource()
147      */
148     public void testGetAndRelease() {
149         final ResourceManager resourceManager = new ResourceManager( getName() );
150         final ProxyResourceFactory factory = new ProxyResourceFactory();
151         final ManagedResource r1 = factory.getResource( resourceManager );
152         final ManagedResource r2 = factory.getResource( resourceManager );
153 
154         assertEquals( "r1", "1", r1.toString() );
155         assertEquals( "r2", "2", r2.toString() );
156 
157         assertEquals( "list.size", 2, factory.getAllocatedResourceCount() );
158 
159         factory.releaseResource( resourceManager, r1 );
160         factory.releaseResource( resourceManager, r2 );
161 
162         assertEquals( "list.size", 0, factory.getAllocatedResourceCount() );
163     }
164 
165 
166     /***
167      *  Test the normal conditions using releaseAllResources()
168      */
169     public void testGetAndReleaseAll() {
170         final ResourceManager resourceManager = new ResourceManager( getName() );
171         final ProxyResourceFactory factory = new ProxyResourceFactory();
172         final ManagedResource r1 = factory.getResource( resourceManager );
173         final ManagedResource r2 = factory.getResource( resourceManager );
174 
175         assertEquals( "r1", "1", r1.toString() );
176         assertEquals( "r2", "2", r2.toString() );
177 
178         assertEquals( "list.size", 2, factory.getAllocatedResourceCount() );
179 
180         factory.releaseAllResources( resourceManager );
181 
182         assertEquals( "list.size", 0, factory.getAllocatedResourceCount() );
183     }
184 
185 
186     /***
187      *  Test allocating and releasing from multiple stores
188      */
189     public void testGetFromMultipleManagersAndReleaseAll() {
190         final ResourceManager resourceManager1 = new ResourceManager( getName() + "-1" );
191         final ResourceManager resourceManager2 = new ResourceManager( getName() + "-2" );
192 
193         final ProxyResourceFactory factory = new ProxyResourceFactory();
194         factory.setAllowReinitialization( false );
195         final ManagedResource r1 = factory.getResource( resourceManager1 );
196         final ManagedResource r2 = factory.getResource( resourceManager2 );
197         final ManagedResource r3 = factory.getResource( resourceManager2 );
198 
199         assertEquals( "r1", "1", r1.toString() );
200         assertEquals( "r2", "2", r2.toString() );
201         assertEquals( "r3", "3", r3.toString() );
202 
203         assertEquals( "list.size", 3, factory.getAllocatedResourceCount() );
204 
205         factory.releaseAllResources( resourceManager1 );
206         assertEquals( "list.size", 2, factory.getAllocatedResourceCount() );
207 
208         factory.releaseAllResources( resourceManager2 );
209         assertEquals( "list.size", 0, factory.getAllocatedResourceCount() );
210     }
211 
212 
213     /***
214      *  Attempt to reinitialize the specified resource
215      *
216      * @param  resource the resource to reinitialize
217      * @return  true if the resource could be reinitialized. This one always
218      *      returns false.
219      */
220     public boolean reinitializeResourceIfPossible( final ManagedResource resource ) {
221         return false;
222     }
223 }
224