KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > test > BackgroundValidationUnitTestCase


1 package org.jboss.test.jca.test;
2
3 import java.net.URL JavaDoc;
4 import java.sql.Connection JavaDoc;
5
6 import javax.management.ObjectName JavaDoc;
7 import javax.naming.InitialContext JavaDoc;
8 import javax.sql.DataSource JavaDoc;
9
10 import junit.framework.Test;
11
12 import org.jboss.logging.Logger;
13 import org.jboss.mx.util.ObjectNameFactory;
14 import org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory;
15 import org.jboss.resource.connectionmanager.ConnectionListener;
16 import org.jboss.resource.connectionmanager.InternalManagedConnectionPool;
17 import org.jboss.resource.connectionmanager.JBossManagedConnectionPool;
18 import org.jboss.resource.connectionmanager.ManagedConnectionPool;
19 import org.jboss.resource.connectionmanager.NoTxConnectionManager;
20 import org.jboss.resource.connectionmanager.PreFillPoolSupport;
21 import org.jboss.test.JBossTestCase;
22 import org.jboss.test.jca.jdbc.TestDriver;
23 import org.jboss.test.jca.support.PoolHelper;
24
25 public class BackgroundValidationUnitTestCase extends JBossTestCase
26 {
27    Logger log = Logger.getLogger(PreFillPoolingUnitTestCase.class);
28
29    private static ObjectName JavaDoc INVALID_BACKGROUND_POOL = ObjectNameFactory.create("jboss.jca:name=TestFailedBackgroundDS,service=ManagedConnectionPool");
30    private static ObjectName JavaDoc VALID_BACKGROUND_POOL = ObjectNameFactory.create("jboss.jca:name=TestSuccessBackgroundDS,service=ManagedConnectionPool");
31    private static ObjectName JavaDoc INVALID_MATCH_POOL = ObjectNameFactory.create("jboss.jca:name=TestNonMatchDS,service=ManagedConnectionPool");
32    private static ObjectName JavaDoc VALID_MATCH_POOL = ObjectNameFactory.create("jboss.jca:name=TestValidationMatchDS,service=ManagedConnectionPool");
33
34    private PoolHelper helper;
35    
36    public BackgroundValidationUnitTestCase(String JavaDoc name) throws Exception JavaDoc
37    {
38       super(name);
39       helper = PoolHelper.getInstance(getServer(), INVALID_BACKGROUND_POOL);
40       
41    }
42    
43    /**
44     * Test for connection background validation
45     *
46     * Pool: PoolByCri
47     * Deployed *-ds.xml: test-background-failed-validation-ds.xml
48     *
49     * Background validation is enabled, and a connection is acquired. The FailedValidation checker
50     * is being used. Background validation destroys the connections in the pool and the destroyed
51     * count is incremented.
52     *
53     * Next, we set background validation to false, flush the pool and acquire the connection.
54     * Since background validation is disabled, the pool should fill to the minimum allowance.
55     *
56     * @throws Exception
57     */

58    public void testDeployedBackgroundValidationFailure() throws Exception JavaDoc
59    {
60       
61       InitialContext JavaDoc ctx = super.getInitialContext();
62       DataSource JavaDoc ds = (DataSource JavaDoc) ctx.lookup("TestFailedBackgroundDS");
63       ds.getConnection("sa", "").close();
64       
65       Integer JavaDoc minCount = helper.getMinSize();
66       Long JavaDoc backMin = helper.getBackgroundValMinutes();
67       
68       PoolHelper.sleepForValidation(backMin.intValue() * 1000 * 60);
69     
70       Integer JavaDoc destroyedCount = helper.getDestroyed();
71       
72       assertTrue("Background validation ran. Destroyed count should exceed zero.", destroyedCount.intValue() > 0);
73       
74       Integer JavaDoc connCount = helper.getConnectionCount();
75       
76       assertTrue(connCount.intValue() == minCount.intValue());
77       
78       helper.setPoolAttributeAndFlush(PoolHelper.POOL_ATT_BACKGROUND_VAL, Boolean.FALSE);
79       
80       
81       //Reprime the pool
82
ds.getConnection().close();
83       
84       PoolHelper.sleepForValidation(backMin.intValue() * 1000 * 60);
85
86       destroyedCount = helper.getDestroyed();
87       connCount = helper.getConnectionCount();
88       
89       assertTrue("Background validation is disabled. Destroyed count should be zero", destroyedCount.intValue() == 0);
90       assertTrue("Background validation is disabled. Pool should be filled to min", connCount.intValue() == minCount.intValue());
91       
92    
93    }
94   
95    public void testDeployedBackgroundValidationSuccess() throws Exception JavaDoc
96    {
97       InitialContext JavaDoc ctx = super.getInitialContext();
98       DataSource JavaDoc ds = (DataSource JavaDoc) ctx.lookup("TestSuccessBackgroundDS");
99       ds.getConnection("sa", "").close();
100
101       Integer JavaDoc minCount = PoolHelper.getMinSize(getServer(), VALID_BACKGROUND_POOL);
102       Long JavaDoc backMin = PoolHelper.getBackgroundValMinutes(getServer(), VALID_BACKGROUND_POOL);
103             
104       PoolHelper.sleepForValidation(backMin.intValue() * 1000 * 60);
105     
106       Integer JavaDoc destroyedCount = PoolHelper.getDestroyed(getServer(), VALID_BACKGROUND_POOL);
107       
108
109       assertTrue("Background validation ran on valid pool. Destroyed count should not exceed zero.", destroyedCount.intValue() == 0);
110       
111       
112    }
113    
114    
115    /**
116     * Pool: PoolByCri
117     * Deployed *-ds.xml: test-non-validation-match-ds.xml
118     *
119     * @throws Exception
120     */

121    public void testDeployedNonValidateOnMatch() throws Exception JavaDoc
122    {
123
124       InitialContext JavaDoc ctx = super.getInitialContext();
125       DataSource JavaDoc ds = (DataSource JavaDoc) ctx.lookup("TestNonMatchDS");
126       ds.getConnection("sa", "").close();
127
128       Integer JavaDoc destroyed = PoolHelper.getDestroyed(getServer(), INVALID_MATCH_POOL);
129
130       assertTrue("Validation should not have occured at this point.", destroyed.intValue() == 0);
131
132       //No new-connection-sql provided, though connections are invalid, first one will
133
//succeed because a matchManagedConnections is not called...
134
ds.getConnection("sa", "").close();
135
136       destroyed = PoolHelper.getDestroyed(getServer(), INVALID_MATCH_POOL);
137
138       assertTrue(
139             "Validation on match is set to true for invalid connections. Destroyed count should be greater than zero.",
140             destroyed.intValue() > 0);
141        
142       
143    }
144    /**
145     * Pool: PoolByCri
146     * Deployed *-ds.xml: test-non-validation-match-ds.xml
147     *
148     * @throws Exception
149     */

150    public void testDeployedValidateOnMatch() throws Exception JavaDoc
151    {
152
153       InitialContext JavaDoc ctx = super.getInitialContext();
154       DataSource JavaDoc ds = (DataSource JavaDoc) ctx.lookup("TestValidationMatchDS");
155       ds.getConnection("sa", "").close();
156
157       Integer JavaDoc destroyed = PoolHelper.getDestroyed(getServer(), VALID_MATCH_POOL);
158
159       assertTrue("Validation should not have occured at this point.", destroyed.intValue() == 0);
160
161       ds.getConnection("sa", "").close();
162
163       destroyed = PoolHelper.getDestroyed(getServer(), VALID_MATCH_POOL);
164
165       assertTrue("Validation on match is set to true for connections. Destroyed count should be zero.", destroyed
166             .intValue() == 0);
167
168          
169          
170    
171    }
172    public void testValidateOnMatchSuccess() throws Exception JavaDoc
173    {
174       LocalManagedConnectionFactory mcf = new LocalManagedConnectionFactory();
175       
176       mcf.setDriverClass("org.jboss.test.jca.jdbc.TestDriver");
177       mcf.setConnectionURL("jdbc:jboss-test-adapter");
178       mcf.setValidConnectionCheckerClassName("org.jboss.test.jca.support.MockSuccessValidationConnectionChecker");
179       mcf.setValidateOnMatch(true);
180       
181       InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams();
182       pp.minSize = 1;
183       pp.maxSize = 2;
184       pp.blockingTimeout = 10000;
185       pp.idleTimeout = 0;
186       pp.prefill = true;
187       pp.backgroundValidation = false;
188       pp.backgroundInterval = 1 * 1000 * 30;
189       ManagedConnectionPool mcp = new JBossManagedConnectionPool.OnePool(mcf, pp, false, log);
190       NoTxConnectionManager noTxn = new NoTxConnectionManager(null, mcp);
191       mcp.setConnectionListenerFactory(noTxn);
192
193       ((PreFillPoolSupport)mcp).prefill();
194       
195       //Let prefiller run
196
Thread.sleep(5000);
197       
198       ConnectionListener cl = noTxn.getManagedConnection(null, null);
199       noTxn.returnManagedConnection(cl, false);
200       
201       assertTrue(mcp.getConnectionDestroyedCount() == 0);
202
203       
204    }
205    public void testBasicValidateOnMatchFailure() throws Exception JavaDoc
206    {
207       LocalManagedConnectionFactory mcf = new LocalManagedConnectionFactory();
208       
209       mcf.setDriverClass("org.jboss.test.jca.jdbc.TestDriver");
210       mcf.setConnectionURL("jdbc:jboss-test-adapter");
211       mcf.setValidConnectionCheckerClassName("org.jboss.test.jca.support.MockFailedValidationConnectionChecker");
212       mcf.setValidateOnMatch(true);
213
214       InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams();
215       pp.minSize = 1;
216       pp.maxSize = 2;
217       pp.blockingTimeout = 10000;
218       pp.idleTimeout = 0;
219       pp.prefill = true;
220       pp.backgroundValidation = false;
221       pp.backgroundInterval = 1 * 1000 * 30;
222       ManagedConnectionPool mcp = new JBossManagedConnectionPool.OnePool(mcf, pp, false, log);
223       NoTxConnectionManager noTxn = new NoTxConnectionManager(null, mcp);
224       mcp.setConnectionListenerFactory(noTxn);
225
226       ((PreFillPoolSupport)mcp).prefill();
227       
228       //Let prefiller run
229
Thread.sleep(5000);
230       
231       ConnectionListener cl = noTxn.getManagedConnection(null, null);
232       noTxn.returnManagedConnection(cl, false);
233       
234       assertTrue(mcp.getConnectionDestroyedCount() > 0);
235       
236    }
237    
238    
239    public void testBasicBackgroundValidationSuccess() throws Exception JavaDoc
240    {
241       
242       LocalManagedConnectionFactory mcf = new LocalManagedConnectionFactory();
243       
244       mcf.setDriverClass("org.jboss.test.jca.jdbc.TestDriver");
245       mcf.setConnectionURL("jdbc:jboss-test-adapter");
246       mcf.setValidConnectionCheckerClassName("org.jboss.test.jca.support.MockSuccessValidationConnectionChecker");
247       
248       InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams();
249       pp.minSize = 1;
250       pp.maxSize = 2;
251       pp.blockingTimeout = 10000;
252       pp.idleTimeout = 0;
253       pp.prefill = false;
254       pp.backgroundValidation = true;
255       pp.backgroundInterval = 1 * 1000 * 30;
256       ManagedConnectionPool mcp = new JBossManagedConnectionPool.OnePool(mcf, pp, false, log);
257       NoTxConnectionManager noTxn = new NoTxConnectionManager(null, mcp);
258       mcp.setConnectionListenerFactory(noTxn);
259       
260       ConnectionListener cl = noTxn.getManagedConnection(null, null);
261       noTxn.returnManagedConnection(cl, false);
262       
263       Thread.sleep(pp.backgroundInterval);
264       assertTrue(mcp.getConnectionDestroyedCount() == 0);
265       
266    }
267    public void testBasicBackgroundValidationDestroy() throws Exception JavaDoc
268    {
269       
270       LocalManagedConnectionFactory mcf = new LocalManagedConnectionFactory();
271       
272       mcf.setDriverClass("org.jboss.test.jca.jdbc.TestDriver");
273       mcf.setConnectionURL("jdbc:jboss-test-adapter");
274       mcf.setValidConnectionCheckerClassName("org.jboss.test.jca.support.MockFailedValidationConnectionChecker");
275       
276       InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams();
277       pp.minSize = 1;
278       pp.maxSize = 2;
279       pp.blockingTimeout = 10000;
280       pp.idleTimeout = 0;
281       pp.prefill = false;
282       pp.backgroundValidation = true;
283       pp.backgroundInterval = 1 * 1000 * 30;
284       ManagedConnectionPool mcp = new JBossManagedConnectionPool.OnePool(mcf, pp, false, log);
285       NoTxConnectionManager noTxn = new NoTxConnectionManager(null, mcp);
286       mcp.setConnectionListenerFactory(noTxn);
287       
288       ConnectionListener cl = noTxn.getManagedConnection(null, null);
289       noTxn.returnManagedConnection(cl, false);
290       
291       Thread.sleep(pp.backgroundInterval);
292       assertTrue(mcp.getConnectionDestroyedCount() > 0);
293       
294    }
295    
296    public static Test suite() throws Exception JavaDoc{
297       Test test1 = getDeploySetup(BackgroundValidationUnitTestCase.class, "jca-support.sar");
298       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
299       URL JavaDoc resURL = loader.getResource("jca/validation/test-background-failed-validation-ds.xml");
300       Test test2 = getDeploySetup(test1, resURL.toString());
301       resURL = loader.getResource("jca/validation/test-background-success-validation-ds.xml");
302       Test test3 = getDeploySetup(test2, resURL.toString());
303       resURL = loader.getResource("jca/validation/test-non-validation-match-ds.xml");
304       Test test4 = getDeploySetup(test3, resURL.toString());
305       resURL = loader.getResource("jca/validation/test-validation-match-ds.xml");
306       return getDeploySetup(test4, resURL.toString());
307       
308            
309    }
310    
311 }
312
Popular Tags