KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > mdbtransactions > unit > MDBUnitTestCase


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.mdbtransactions.unit;
23
24 import javax.jms.Message JavaDoc;
25 import javax.jms.Queue JavaDoc;
26 import javax.jms.QueueConnection JavaDoc;
27 import javax.jms.QueueConnectionFactory JavaDoc;
28 import javax.jms.QueueReceiver JavaDoc;
29 import javax.jms.QueueSender JavaDoc;
30 import javax.jms.QueueSession JavaDoc;
31 import javax.jms.ObjectMessage JavaDoc;
32
33 import javax.naming.InitialContext JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35
36 import org.jboss.ejb3.InitialContextFactory;
37 import org.jboss.ejb3.test.mdbtransactions.Entity;
38 import org.jboss.ejb3.test.mdbtransactions.StatelessFacade;
39 import org.jboss.ejb3.test.mdbtransactions.TestStatus;
40 import org.jboss.logging.Logger;
41 import org.jboss.test.JBossTestCase;
42
43 import junit.framework.Test;
44
45 /**
46  * @author <a HREF="mailto:bdecoste@jboss.com">William DeCoste</a>
47  */

48 public class MDBUnitTestCase extends JBossTestCase
49 {
50    private static final Logger log = Logger.getLogger(MDBUnitTestCase.class);
51
52    static boolean deployed = false;
53
54    static int test = 0;
55
56    public MDBUnitTestCase(String JavaDoc name)
57    {
58
59       super(name);
60
61    }
62
63    public void testMdbTransactions() throws Exception JavaDoc
64    {
65       TestStatus status = (TestStatus)getInitialContext().lookup("TestStatusBean/remote");
66       status.clear();
67       
68       sendMessages("queue/mdbtest", 2);
69       
70       Thread.sleep(5000);
71       
72       assertEquals(1, status.messageCount());
73       assertTrue(status.caughtRollback());
74    }
75    
76    protected void sendMessages(String JavaDoc queueName, int numMessages) throws Exception JavaDoc
77    {
78       QueueConnection JavaDoc connection = null;
79       QueueSender JavaDoc sender = null;
80       QueueSession JavaDoc session = null;
81
82       Queue JavaDoc queue = (Queue JavaDoc) getInitialContext().lookup(
83             queueName);
84       QueueConnectionFactory JavaDoc factory = getQueueConnectionFactory();
85       connection = factory.createQueueConnection();
86       connection.start();
87       session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
88       
89       QueueReceiver JavaDoc receiver = session.createReceiver(queue);
90       Message JavaDoc message = receiver.receiveNoWait();
91       while (message != null)
92          message = receiver.receiveNoWait();
93
94       Entity entity = new Entity();
95       entity.setName("test-entity");
96       entity.setId(1234L);
97       
98       StatelessFacade stateless = (StatelessFacade)getInitialContext().lookup("StatelessFacade");
99       stateless.clear(entity);
100       
101       ObjectMessage JavaDoc msg = session.createObjectMessage(entity);
102
103       sender = session.createSender(queue);
104       
105       for (int i = 0 ; i < numMessages ; ++i)
106          sender.send(msg);
107  
108       session.close();
109       connection.close();
110    }
111    
112    public void testRollback() throws Exception JavaDoc
113    {
114       TestStatus status = (TestStatus)getInitialContext().lookup("TestStatusBean/remote");
115       status.clear();
116       
117       sendMessages("queue/rollbackmdbtest", 1);
118       
119       Thread.sleep(5000);
120       
121       Queue JavaDoc queue = (Queue JavaDoc) getInitialContext().lookup("queue/DLQ");
122       QueueConnectionFactory JavaDoc factory = getQueueConnectionFactory();
123       QueueConnection JavaDoc connection = factory.createQueueConnection();
124       connection.start();
125       QueueSession JavaDoc session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
126       QueueReceiver JavaDoc receiver = session.createReceiver(queue);
127       Message JavaDoc message = receiver.receiveNoWait();
128       assertNotNull(message);
129       
130       session.close();
131       connection.close();
132    }
133    
134    protected QueueConnectionFactory JavaDoc getQueueConnectionFactory()
135       throws Exception JavaDoc
136    {
137       try
138       {
139          return (QueueConnectionFactory JavaDoc) getInitialContext().lookup(
140                "ConnectionFactory");
141       } catch (NamingException JavaDoc e)
142       {
143          return (QueueConnectionFactory JavaDoc) getInitialContext().lookup(
144                "java:/ConnectionFactory");
145       }
146    }
147    
148    protected InitialContext JavaDoc getInitialContext() throws Exception JavaDoc
149    {
150       return InitialContextFactory.getInitialContext();
151    }
152
153    public static Test suite() throws Exception JavaDoc
154    {
155       return getDeploySetup(MDBUnitTestCase.class,
156             "mdbtransactionstest-service.xml, mdbtransactions-test.jar");
157    }
158
159 }
Popular Tags