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