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 java.util.ArrayList;
41  import java.util.Arrays;
42  import java.util.List;
43  import junit.framework.TestCase;
44  
45  /***
46   *  Tests for ResourceManager
47   *
48   * @version  $Revision: 1.6 $
49   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
50   */
51  public class ResourceManagerTest extends TestCase {
52      /***
53       *  Instantiate the test
54       *
55       * @param  name Name of the test
56       */
57      public ResourceManagerTest( final String name ) {
58          super( name );
59      }
60  
61  
62      /***
63       *  Ensure that an exception is being thrown when we attempt to release a
64       *  resource that hadn't been allocated from this factory.
65       */
66      public void testReleaseResource_ObjectNotFromFactory() {
67          final ResourceManager resourceManager = new ResourceManager( getName() );
68          try {
69              resourceManager.releaseResource(
70                  new ManagedResource() {
71                      public void setResourceFactoryName( final String name ) {
72                      }
73  
74  
75                      public String getResourceFactoryName() {
76                          return "Flintstone";
77                      }
78                  } );
79              fail( "Expected exception for unregistered name" );
80          }
81          catch( final ResourceFactoryNotFoundException e ) {
82              // Expected path
83          }
84      }
85  
86  
87      /***
88       *  Ensure that an exception is being thrown when null is passed into
89       *  releaseResource()
90       */
91      public void testReleaseResource_null() {
92          final ResourceManager resourceManager = new ResourceManager( getName() );
93          try {
94              resourceManager.releaseResource( null );
95              fail( "Expected exception for null name" );
96          }
97          catch( final NullPointerException e ) {
98              // Expected path
99          }
100     }
101 
102 
103     /***
104      *  Test the resource allocation
105      */
106     public void testResourceAllocation() {
107         final List list = new ArrayList();
108         final ProxyResourceFactory factory = new ProxyResourceFactory( list );
109 
110         final ResourceManager resourceManager = new ResourceManager( getName() );
111         resourceManager.addFactory( "foo", factory );
112 
113         final ManagedResource resource = ( ManagedResource )resourceManager.getResource( "foo" );
114         resourceManager.getResource( "foo" );
115         resourceManager.releaseResource( resource );
116         resourceManager.releaseAllResources();
117 
118         final List expected = Arrays.asList( new String[]{
119             "getResourceImpl", "getResourceImpl", "releaseResourceImpl",
120             "releaseAllResources", "releaseResourceImpl"} );
121         assertEquals( expected, list );
122     }
123 
124 
125     /***
126      *  Ensure that an exception is being thrown when null is passed into
127      *  getResource
128      */
129     public void testGetResource_Null() {
130         final ResourceManager resourceManager = new ResourceManager( getName() );
131         resourceManager.addFactory( "jdbc", new ProxyResourceFactory() );
132         try {
133             resourceManager.getResource( null );
134             fail( "Expected NullPointerException" );
135         }
136         catch( final NullPointerException e ) {
137             // Expected path
138         }
139     }
140 
141 
142     /***
143      *  Ensure that an exception is being thrown when an unregistered name is
144      *  passed into getResource
145      */
146     public void testGetResource_BadName() {
147         final ResourceManager resourceManager = new ResourceManager( getName() );
148         resourceManager.addFactory( "jdbc", new ProxyResourceFactory() );
149         try {
150             resourceManager.getResource( "aResourceNameThatShouldNotMatchAnything" );
151             fail( "Expected ResourceFactoryNotFoundException" );
152         }
153         catch( final ResourceFactoryNotFoundException e ) {
154             // Expected path
155         }
156     }
157 
158     /*** */
159     public void testResourceAllocation_Many() {
160         final List list = new ArrayList();
161         final ProxyResourceFactory proxyResourceFactory = new ProxyResourceFactory( list );
162         proxyResourceFactory.setAllowReinitialization(true);
163 
164         final PooledResourceFactory factory = new PooledResourceFactory(proxyResourceFactory);
165         factory.setPreferredCacheSize(1);
166 
167         final ResourceManager resourceManager1 = new ResourceManager( getName() + "1");
168         resourceManager1.addFactory( "foo", factory );
169 
170         final ResourceManager resourceManager2 = new ResourceManager( getName() + "2");
171         resourceManager2.addFactory( "foo", factory );
172 
173         final ManagedResource resource = (ManagedResource)resourceManager1.getResource("foo");
174         for( int i=0; i<10; i++ ) {
175             resourceManager1.getResource( "foo" );
176             resourceManager2.getResource( "foo" );
177         }
178         resourceManager1.releaseResource(resource);
179         for( int i=0; i<10; i++ ) {
180             resourceManager1.getResource( "foo" );
181             resourceManager2.getResource( "foo" );
182         }
183         resourceManager1.releaseAllResources();
184         resourceManager2.releaseAllResources();
185     }
186 
187 }
188