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.List;
42  import junit.framework.TestCase;
43  
44  /***
45   *  tests for PooledResourceFactory
46   *
47   * @version  $Revision: 1.7 $
48   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
49   */
50  public class PooledResourceFactoryTest extends TestCase {
51  
52      /***
53       *  Create an instance
54       *
55       * @param  name the name of the test
56       */
57      public PooledResourceFactoryTest( final String name ) {
58          super( name );
59      }
60  
61  
62      /***
63       *  Test the constructor with a null resource factory
64       */
65      public void testConstructor_Null() {
66          try {
67              new PooledResourceFactory( null );
68              fail( "Expected NullPointerException" );
69          }
70          catch( final NullPointerException e ) {
71              // Expected path
72          }
73      }
74  
75  
76      /***
77       *  Test preferredCacheSize
78       */
79      public void testSetPreferredCacheSize() {
80          final List list = new ArrayList();
81          final PooledResourceFactory factory
82                   = new PooledResourceFactory( new ProxyResourceFactory( list ) );
83  
84          factory.setPreferredCacheSize( 0 );
85          assertEquals( 0, factory.getPreferredCacheSize() );
86  
87          factory.setPreferredCacheSize( 3 );
88          assertEquals( 3, factory.getPreferredCacheSize() );
89  
90          try {
91              factory.setPreferredCacheSize( -1 );
92              fail( "preferredCacheSize may not be negative" );
93          }
94          catch( final IllegalArgumentException e ) {
95              assertEquals( 3, factory.getPreferredCacheSize() );
96          }
97      }
98  
99  
100     /***
101      *  Test allocating and releasing resources to ensure that the cache is
102      *  being used correctly.
103      */
104     public void testGetAndRelease() {
105         final List loggedMethods = new ArrayList();
106         final PooledResourceFactory factory
107                  = new PooledResourceFactory( new ProxyResourceFactory( loggedMethods ) );
108         final ResourceManager resourceManager = new ResourceManager( getName() );
109 
110         factory.setPreferredCacheSize( 2 );
111 
112         ManagedResource a;
113         ManagedResource b;
114         ManagedResource c;
115 
116         a = factory.getResource( resourceManager );
117         assertListContainsAndClear( loggedMethods, "getResourceImpl" );
118         factory.releaseResource( resourceManager, a );
119         assertListContainsAndClear( loggedMethods, "reinitializeResourceIfPossible" );
120         a = factory.getResource( resourceManager );
121         assertListEmpty( loggedMethods );
122 
123         b = factory.getResource( resourceManager );
124         assertListContainsAndClear( loggedMethods, "getResourceImpl" );
125 
126         c = factory.getResource( resourceManager );
127         assertListContainsAndClear( loggedMethods, "getResourceImpl" );
128 
129         factory.releaseResource( resourceManager, a );
130         assertListContainsAndClear( loggedMethods, "reinitializeResourceIfPossible" );
131         factory.releaseResource( resourceManager, b );
132         assertListContainsAndClear( loggedMethods, "reinitializeResourceIfPossible" );
133         factory.releaseResource( resourceManager, c );
134         assertListContainsAndClear( loggedMethods, "releaseResourceImpl" );
135         loggedMethods.clear();
136     }
137 
138 
139     private void assertListEmpty( final List list ) {
140         assertNotNull( "List cannot be null", list );
141         if( list.isEmpty() == false ) {
142             fail( "Expected empty list but got " + list );
143         }
144     }
145 
146 
147     private void assertListContainsAndClear( final List list, final Object object ) {
148         if( list.size() != 1 ) {
149             fail( "Expected only one element: got " + list );
150         }
151         assertEquals( "object", object, list.get( 0 ) );
152         list.clear();
153     }
154 }
155