KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > strictpool > unit > StrictPoolUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.test.strictpool.unit;
23
24 import javax.jms.*;
25 import javax.naming.InitialContext JavaDoc;
26 import org.jboss.ejb3.test.strictpool.MDBInvoker;
27 import org.jboss.ejb3.test.strictpool.SessionInvoker;
28 import org.jboss.ejb3.test.strictpool.StrictlyPooledSession;
29 import org.jboss.ejb3.test.strictpool.Counter;
30 import org.jboss.test.JBossTestCase;
31 import EDU.oswego.cs.dl.util.concurrent.CountDown;
32 import junit.framework.Test;
33
34
35 /**
36  * Adapted from the EJB 2.1 tests (org.jboss.test.cts.test.StatefulSessionUnitTestCase and
37  * org.jboss.test.cts.test.MDBUnitTestCase)
38  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
39  * @version $Revision: 57344 $
40  */

41 public class StrictPoolUnitTestCase
42       extends JBossTestCase
43 {
44    static final int MAX_SIZE = 20;
45    static String JavaDoc QUEUE_FACTORY = "ConnectionFactory";
46
47
48    public StrictPoolUnitTestCase(String JavaDoc name)
49    {
50       super(name);
51    }
52    public void testSession() throws Exception JavaDoc
53    {
54       System.out.println("*** testSession");
55       CountDown done = new CountDown(MAX_SIZE);
56       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
57       StrictlyPooledSession session =(StrictlyPooledSession)ctx.lookup("StrictlyPooledSessionBean/remote");
58       SessionInvoker[] threads = new SessionInvoker[MAX_SIZE];
59       for(int n = 0; n < MAX_SIZE; n ++)
60       {
61          SessionInvoker t = new SessionInvoker(n, done, session);
62          threads[n] = t;
63          t.start();
64       }
65       boolean ok = done.attempt(1500 * MAX_SIZE);
66       super.assertTrue("Acquired done, remaining="+done.currentCount(), ok);
67
68       for(int n = 0; n < MAX_SIZE; n ++)
69       {
70          SessionInvoker t = threads[n];
71          if( t.runEx != null )
72          {
73             t.runEx.printStackTrace();
74             System.err.println("SessionInvoker.runEx != null");
75             t.runEx.printStackTrace();
76             fail("SessionInvoker.runEx != null");
77          }
78       }
79    }
80
81    public void testMessageDriven() throws Exception JavaDoc
82    {
83       System.out.println("*** testMessageDriven");
84       CountDown done = new CountDown(MAX_SIZE);
85       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
86       QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup(QUEUE_FACTORY);
87       QueueConnection queConn = factory.createQueueConnection();
88       QueueSession session = queConn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
89       Queue queueA = (Queue) ctx.lookup("queue/queueA");
90       Queue queueB = (Queue) ctx.lookup("queue/queueB");
91       queConn.start();
92       MDBInvoker[] threads = new MDBInvoker[MAX_SIZE];
93       for(int n = 0; n < MAX_SIZE; n ++)
94       {
95          MDBInvoker t = new MDBInvoker(session, queueA, queueB, n, done);
96          threads[n] = t;
97          t.start();
98       }
99       assertTrue("Acquired done", done.attempt(1500 * MAX_SIZE));
100       session.close();
101       queConn.close();
102
103       for(int n = 0; n < MAX_SIZE; n ++)
104       {
105          MDBInvoker t = threads[n];
106          if( t.runEx != null )
107          {
108             t.runEx.printStackTrace();
109             fail("Inovker.runEx != null, msg="+t.runEx.getMessage());
110          }
111       }
112    }
113    public void testPoolTimeout() throws Exception JavaDoc
114    {
115       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
116       ConnectionFactory factory = (ConnectionFactory)ctx.lookup("ConnectionFactory");
117       Connection conn = factory.createConnection();
118       Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
119       Queue queueC = (Queue)ctx.lookup("queue/queueC");
120       conn.start();
121       MessageProducer sender = session.createProducer(queueC);
122       TextMessage msg = session.createTextMessage("hello world");
123       msg.setIntProperty("JMS_JBOSS_REDELIVERY_LIMIT", 20);
124       sender.send(msg);
125       // the second message will timeout
126
TextMessage msg2 = session.createTextMessage("hello world 2");
127       msg2.setIntProperty("JMS_JBOSS_REDELIVERY_LIMIT", 20);
128       sender.send(msg2);
129
130       Thread.sleep(5000);
131       Counter counter = (Counter)ctx.lookup("CounterBean/remote");
132       assertEquals(1, counter.getCount());
133
134    }
135
136    public static Test suite() throws Exception JavaDoc
137    {
138       return getDeploySetup(StrictPoolUnitTestCase.class, "strictpool_mdbtest-service.xml, strictpool-test.jar");
139
140    }
141
142 }
143
Popular Tags