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.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
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