KickJava   Java API By Example, From Geeks To Geeks.

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


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.Attribute JavaDoc;
7 import javax.management.ObjectName JavaDoc;
8 import javax.naming.InitialContext JavaDoc;
9 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
10 import javax.sql.DataSource JavaDoc;
11 import javax.transaction.TransactionManager JavaDoc;
12
13 import junit.framework.Test;
14
15 import org.jboss.logging.Logger;
16 import org.jboss.mx.util.ObjectNameFactory;
17 import org.jboss.resource.connectionmanager.BaseConnectionManager2;
18 import org.jboss.resource.connectionmanager.CachedConnectionManager;
19 import org.jboss.resource.connectionmanager.InternalManagedConnectionPool;
20 import org.jboss.resource.connectionmanager.JBossManagedConnectionPool;
21 import org.jboss.resource.connectionmanager.ManagedConnectionPool;
22 import org.jboss.resource.connectionmanager.NoTxConnectionManager;
23 import org.jboss.resource.connectionmanager.PreFillPoolSupport;
24 import org.jboss.resource.connectionmanager.TransactionSynchronizer;
25 import org.jboss.test.JBossTestCase;
26 import org.jboss.test.jca.adapter.TestConnectionRequestInfo;
27 import org.jboss.test.jca.adapter.TestManagedConnectionFactory;
28 import org.jboss.tm.TransactionManagerLocator;
29
30 /**
31  * A PrefillDeploymentUnitTestCase.
32  *
33  * @author <a HREF="weston.price@jboss.com">Weston Price</a>
34  * @version $Revision: 45412 $
35  */

36 public class PreFillPoolingUnitTestCase extends JBossTestCase
37 {
38    Logger log = Logger.getLogger(PreFillPoolingUnitTestCase.class);
39   
40    private static final ObjectName JavaDoc PREFILL_POOL = ObjectNameFactory.create("jboss.jca:name=PreFillDS,service=ManagedConnectionPool");
41    private static final ObjectName JavaDoc NO_PREFILL_POOL = ObjectNameFactory.create("jboss.jca:name=NoPreFillDS,service=ManagedConnectionPool");
42    private static final ObjectName JavaDoc NO_ELEMENT_PREFILL_POOL = ObjectNameFactory.create("jboss.jca:name=NoElementPreFillDS,service=ManagedConnectionPool");
43    private static final ObjectName JavaDoc CRI_PREFILL_POOL = ObjectNameFactory.create("jboss.jca:name=CriPreFillDS,service=ManagedConnectionPool");
44    private static final ObjectName JavaDoc INVALD_PREFILL_POOL = ObjectNameFactory.create("jboss.jca:name=InvalidPreFillDS,service=ManagedConnectionPool");
45
46    private static final String JavaDoc CONN_NAME = "ConnectionCount";
47    private static final String JavaDoc MIN_NAME = "MinSize";
48    private static final String JavaDoc FLUSH_METHOD_NAME = "flush";
49    private static final String JavaDoc PREFILL = "PreFill";
50    
51    //For non managed prefill testing.
52
static CachedConnectionManager ccm = new CachedConnectionManager();
53    static TransactionManager JavaDoc tm = TransactionManagerLocator.getInstance().locate();
54    static TestConnectionRequestInfo cri1 = new TestConnectionRequestInfo("info1");
55    static TestConnectionRequestInfo cri2 = new TestConnectionRequestInfo("info2");
56
57    public PreFillPoolingUnitTestCase(String JavaDoc name)
58    {
59       super(name);
60    }
61
62    /**
63     * Test basic prefill support on a OnePool.
64     *
65     * PoolType: OnePool
66     * Deployed *-ds.xml: prefill-ds.xml
67     * Conditions:
68     * Prefill should be set to true
69     * The pool should be prefilled and the connection count should equal the minimum count.
70     *
71     * @throws Exception
72     */

73    public void testDeployPreFillPool() throws Exception JavaDoc
74    {
75       Boolean JavaDoc prefill = (Boolean JavaDoc)getServer().getAttribute(PREFILL_POOL, PREFILL);
76       assertTrue(prefill.booleanValue());
77       
78       Integer JavaDoc count = (Integer JavaDoc)getServer().getAttribute(PREFILL_POOL, CONN_NAME);
79       Integer JavaDoc min = (Integer JavaDoc)getServer().getAttribute(PREFILL_POOL, MIN_NAME);
80       
81       assertTrue("Minimun count and connection count should be the same.", count.intValue() == min.intValue());
82       
83       
84    }
85    
86    /**
87     * Test prefill support after flushing a pool.
88     * PoolType: OnePool
89     * Deployed *-ds.xml: prefill-ds.xml
90     *
91     * Conditions:
92     * Prefill should be set to true
93     * After flush, the pool should be prefilled and the connection count should equal the minimum count.
94     *
95     * @throws Exception
96     */

97    public void testPreFillFlush() throws Exception JavaDoc
98    {
99
100       Boolean JavaDoc prefill = (Boolean JavaDoc)getServer().getAttribute(PREFILL_POOL, PREFILL);
101       assertTrue(prefill.booleanValue());
102
103       Integer JavaDoc count = (Integer JavaDoc)getServer().getAttribute(PREFILL_POOL, CONN_NAME);
104       Integer JavaDoc min = (Integer JavaDoc)getServer().getAttribute(PREFILL_POOL, MIN_NAME);
105       assertTrue("Prefill is set to true. Minimun count and connection count should be the same.", count.intValue() == min.intValue());
106
107       Attribute JavaDoc flush = new Attribute JavaDoc(PREFILL, Boolean.FALSE);
108       getServer().setAttribute(PREFILL_POOL, flush);
109       getServer().invoke(PREFILL_POOL, FLUSH_METHOD_NAME, new Object JavaDoc[0], new String JavaDoc[0]);
110       
111       count = (Integer JavaDoc)getServer().getAttribute(PREFILL_POOL, CONN_NAME);
112       min = (Integer JavaDoc)getServer().getAttribute(PREFILL_POOL, MIN_NAME);
113       assertTrue("Pool was flushed and prefill set to false. Minimum count and connection count should not be equal.", count.intValue() != min.intValue());
114
115       flush = new Attribute JavaDoc(PREFILL, Boolean.TRUE);
116       getServer().setAttribute(PREFILL_POOL, flush);
117       getServer().invoke(PREFILL_POOL, FLUSH_METHOD_NAME, new Object JavaDoc[0], new String JavaDoc[0]);
118       
119       //Let pool filler run
120
Thread.sleep(2000);
121       
122       count = (Integer JavaDoc)getServer().getAttribute(PREFILL_POOL, CONN_NAME);
123       min = (Integer JavaDoc)getServer().getAttribute(PREFILL_POOL, MIN_NAME);
124       assertTrue("Prefill is set to true. Minimun count and connection count should be the same.", count.intValue() == min.intValue());
125         
126       
127    }
128    
129    /**
130     *
131     * Test basic prefill support on a non prefilled pool.
132     *
133     * @throws Exception
134     */

135    public void testDeployNoPreFillPool() throws Exception JavaDoc
136    {
137       Boolean JavaDoc prefill = (Boolean JavaDoc)getServer().getAttribute(NO_PREFILL_POOL, PREFILL);
138       assertFalse("Prefill is set to false.", prefill.booleanValue());
139       
140       Integer JavaDoc count = (Integer JavaDoc)getServer().getAttribute(NO_PREFILL_POOL, CONN_NAME);
141       Integer JavaDoc min = (Integer JavaDoc)getServer().getAttribute(NO_PREFILL_POOL, MIN_NAME);
142       assertTrue("Prefill is set to false. Min count and connection count should not be equal", count.intValue() != min.intValue());
143       
144       
145    }
146    
147    /**
148     * Test prefill pool where <prefill> element is explicitly false.
149     *
150     * @throws Exception
151     */

152    public void testDeployElementNoPreFillPool() throws Exception JavaDoc
153    {
154       Boolean JavaDoc prefill = (Boolean JavaDoc)getServer().getAttribute(NO_ELEMENT_PREFILL_POOL, PREFILL);
155       assertFalse("Prefill was explicitly set ot false. Prefill should be false.", prefill.booleanValue());
156       
157       Integer JavaDoc count = (Integer JavaDoc)getServer().getAttribute(NO_ELEMENT_PREFILL_POOL, CONN_NAME);
158       Integer JavaDoc min = (Integer JavaDoc)getServer().getAttribute(NO_ELEMENT_PREFILL_POOL, MIN_NAME);
159       
160       assertTrue("Prefill is set to false. Min count and connection count should not be equal", count.intValue() != min.intValue());
161       
162    }
163    
164    /**
165     * FIXME Comment this
166     *
167     * @throws Exception
168     */

169    public void testInvalidPreFillPool() throws Exception JavaDoc
170    {
171       Boolean JavaDoc prefill = (Boolean JavaDoc)getServer().getAttribute(INVALD_PREFILL_POOL, PREFILL);
172       assertTrue(prefill.booleanValue());
173
174       Integer JavaDoc count = (Integer JavaDoc)getServer().getAttribute(INVALD_PREFILL_POOL, CONN_NAME);
175       Integer JavaDoc min = (Integer JavaDoc)getServer().getAttribute(INVALD_PREFILL_POOL, MIN_NAME);
176       assertTrue("Non supporting prefill pool is used. Min count and connection count should not be equal", count.intValue() != min.intValue());
177       
178    }
179    
180    /**
181     * FIXME Comment this
182     *
183     * @throws Exception
184     */

185    public void testPoolByCriPreFill() throws Exception JavaDoc
186    {
187       Boolean JavaDoc prefill = (Boolean JavaDoc)getServer().getAttribute(CRI_PREFILL_POOL, PREFILL);
188       assertTrue(prefill.booleanValue());
189
190       Integer JavaDoc count = (Integer JavaDoc)getServer().getAttribute(CRI_PREFILL_POOL, CONN_NAME);
191       Integer JavaDoc min = (Integer JavaDoc)getServer().getAttribute(CRI_PREFILL_POOL, MIN_NAME);
192       
193       assertTrue(count.intValue() != min.intValue());
194       
195       InitialContext JavaDoc ctx = super.getInitialContext();
196       DataSource JavaDoc ds = (DataSource JavaDoc)ctx.lookup("CriPreFillDS");
197       Connection JavaDoc conn = ds.getConnection("sa", "");
198       
199       count = (Integer JavaDoc)getServer().getAttribute(CRI_PREFILL_POOL, CONN_NAME);
200       min = (Integer JavaDoc)getServer().getAttribute(CRI_PREFILL_POOL, MIN_NAME);
201       
202       assertTrue(count.intValue() == min.intValue());
203       
204       conn.close();
205       
206       //Now we explictly set prefill, flush
207
Attribute JavaDoc flush = new Attribute JavaDoc(PREFILL, Boolean.TRUE);
208       getServer().setAttribute(PREFILL_POOL, flush);
209       getServer().invoke(CRI_PREFILL_POOL, FLUSH_METHOD_NAME, new Object JavaDoc[0], new String JavaDoc[0]);
210       
211       count = (Integer JavaDoc)getServer().getAttribute(CRI_PREFILL_POOL, CONN_NAME);
212       min = (Integer JavaDoc)getServer().getAttribute(CRI_PREFILL_POOL, MIN_NAME);
213       
214       assertTrue(count.intValue() != min.intValue());
215       
216       conn = ds.getConnection("sa", "");
217       
218       count = (Integer JavaDoc)getServer().getAttribute(CRI_PREFILL_POOL, CONN_NAME);
219       min = (Integer JavaDoc)getServer().getAttribute(CRI_PREFILL_POOL, MIN_NAME);
220       
221       assertTrue(count.intValue() == min.intValue());
222       
223    }
224    
225    
226    public void testNonPreFillSupportingPoolPreFill() throws Exception JavaDoc{
227       
228       int minSize = 3;
229       int maxSize = 5;
230       
231       ManagedConnectionPool mcp = getOnePoolPrefill(minSize, maxSize, true);
232       BaseConnectionManager2 cm = getNoTxCM(mcp);
233       PreFillPoolSupport support = (PreFillPoolSupport)mcp;
234       support.prefill(null, null, false);
235       
236       //Let pool fill
237
Thread.sleep(5000);
238       
239       int currentSize = mcp.getConnectionCount();
240       assertTrue(currentSize == minSize);
241       
242       
243    }
244
245    public void testNoPreFillSupportingPool() throws Exception JavaDoc{
246    
247       int minSize = 3;
248       int maxSize = 5;
249       
250       ManagedConnectionPool mcp = getOnePoolPrefill(minSize, maxSize, false);
251       BaseConnectionManager2 cm = getNoTxCM(mcp);
252       PreFillPoolSupport support = (PreFillPoolSupport)mcp;
253       support.prefill(null, null, false);
254       
255       //Let pool fill
256
Thread.sleep(5000);
257       
258       int currentSize = mcp.getConnectionCount();
259       assertTrue(currentSize == 0);
260       assertTrue(currentSize != minSize);
261       
262       
263    }
264
265    public void testPreFillSupportingPool() throws Exception JavaDoc{
266    
267       int minSize = 3;
268       int maxSize = 5;
269       
270       ManagedConnectionPool mcp = getOnePoolPrefill(minSize, maxSize, true);
271       BaseConnectionManager2 cm = getNoTxCM(mcp);
272       PreFillPoolSupport support = (PreFillPoolSupport)mcp;
273       support.prefill(null, null, false);
274       
275       //Let pool fill
276
Thread.sleep(5000);
277       
278       int currentSize = mcp.getConnectionCount();
279       assertTrue(currentSize == minSize);
280       
281    }
282
283    private ManagedConnectionPool getOnePoolPrefill(int minSize, int maxSize, boolean prefill){
284       
285       InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams();
286       pp.minSize = minSize;
287       pp.maxSize = maxSize;
288       pp.blockingTimeout = 10000;
289       pp.idleTimeout = 0;
290       pp.prefill = prefill;
291       ManagedConnectionFactory JavaDoc mcf = new TestManagedConnectionFactory();
292       ManagedConnectionPool poolingStrategy = new JBossManagedConnectionPool.OnePool(mcf, pp, false, log);
293       return poolingStrategy;
294       
295    }
296
297    private BaseConnectionManager2 getNoTxCM(ManagedConnectionPool poolingStrategy)
298    {
299       TransactionSynchronizer.setTransactionManager(tm);
300       BaseConnectionManager2 cm = new NoTxConnectionManager(ccm, poolingStrategy);
301       poolingStrategy.setConnectionListenerFactory(cm);
302       return cm;
303    }
304
305    public static Test suite() throws Exception JavaDoc
306    {
307       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
308       URL JavaDoc resURL = loader.getResource("jca/prefill/prefill-ds.xml");
309       Test t1 = getDeploySetup(PreFillPoolingUnitTestCase.class, resURL.toString());
310       
311       resURL = loader.getResource("jca/prefill/no-prefill-ds.xml");
312       
313       Test t2 = getDeploySetup(t1, resURL.toString());
314       resURL = loader.getResource("jca/prefill/no-element-prefill-ds.xml");
315       
316       Test t3 = getDeploySetup(t2, resURL.toString());
317       
318       resURL = loader.getResource("jca/prefill/cri-prefill-ds.xml");
319       
320       Test t4 = getDeploySetup(t3, resURL.toString());
321       resURL = loader.getResource("jca/prefill/invalid-prefill-ds.xml");
322       
323       return getDeploySetup(t4, resURL.toString());
324       
325       
326    }
327    
328    
329 }
330
Popular Tags