KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmessaging > ra > RaSyncRecUnitTestCase


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.test.jbossmessaging.ra;
23
24 import javax.jms.Connection JavaDoc;
25 import javax.jms.Message JavaDoc;
26
27 import javax.jms.QueueConnection JavaDoc;
28 import javax.jms.QueueConnectionFactory JavaDoc;
29 import javax.jms.QueueSender JavaDoc;
30 import javax.jms.QueueSession JavaDoc;
31 import javax.jms.Session JavaDoc;
32 import javax.jms.TextMessage JavaDoc;
33 import javax.jms.Queue JavaDoc;
34 import javax.naming.Context JavaDoc;
35
36 import javax.management.ObjectName JavaDoc;
37
38 import javax.naming.InitialContext JavaDoc;
39 import junit.framework.Assert;
40
41 import junit.framework.Test;
42
43 import org.jboss.test.jbossmessaging.JMSTestCase;
44 import org.jboss.test.JBossTestSetup;
45
46 import org.jboss.test.jmsra.bean.*;
47
48 /**
49  *
50  * <p>Test sync receive.
51  *
52  * <p>Created: Sat Sep 22 13:31:54 2001.
53  *
54  * @author <a HREF="mailto:richard.achmatowicz@jboss.com">Richard Achmatowicz</a>
55  * @author <a HREF="mailto:peter.antman@tim.se">Peter Antman</a>
56  * @version $Revision: 37406 $
57  */

58
59 public class RaSyncRecUnitTestCase extends JMSTestCase {
60
61    private final static String JavaDoc BEAN_JNDI = "QueueRec";
62    private final static String JavaDoc QUEUE_FACTORY = "ConnectionFactory";
63    private final static String JavaDoc QUEUE = "queue/A";
64    private final static int MESSAGE_NR = 10;
65    
66    /**
67     * JMS connection
68     */

69    protected QueueConnection JavaDoc connection;
70    /**
71     * JMS session
72     */

73    protected QueueSession JavaDoc session;
74    /**
75     * JMS sender
76     */

77    protected QueueSender JavaDoc sender;
78
79    /**
80     * Receiving bean
81     */

82    protected QueueRec rec;
83
84    /**
85     *
86     * Constructor for the RaSyncRecUnitTestCase object
87     *
88     * @param name Description of Parameter
89     * @exception Exception Description of Exception
90     */

91    public RaSyncRecUnitTestCase(String JavaDoc name) {
92       super(name);
93    }
94    
95    /**
96     * The JUnit setup method
97     *
98     * @exception Exception Description of Exception
99     */

100    protected void setUp() throws Exception JavaDoc
101    {
102        // call setUp() in super class
103
super.setUp() ;
104
105       // Create a receiver
106
Context JavaDoc context = getInitialContext();
107       try
108       {
109          QueueRecHome home = (QueueRecHome)context.lookup(BEAN_JNDI);
110          rec = home.create();
111
112          init(context);
113       }
114       finally
115       {
116          context.close();
117       }
118
119       // start up the session
120
connection.start();
121
122    }
123
124    /**
125     * #Description of the Method
126     *
127     * @param context Description of Parameter
128     * @exception Exception Description of Exception
129     */

130    protected void init(final Context JavaDoc context) throws Exception JavaDoc
131    {
132       QueueConnectionFactory JavaDoc factory =
133      (QueueConnectionFactory JavaDoc)context.lookup(QUEUE_FACTORY);
134       
135       connection = factory.createQueueConnection();
136       
137       session = ((QueueConnection JavaDoc)connection).createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
138       
139       Queue JavaDoc queue = (Queue JavaDoc)context.lookup(QUEUE);
140       
141       sender = ((QueueSession JavaDoc)session).createSender(queue);
142    }
143
144    /**
145     * The teardown method for JUnit
146     *
147     * @exception Exception Description of Exception
148     */

149    protected void tearDown() throws Exception JavaDoc
150    {
151       if (sender != null)
152       {
153          sender.close();
154       }
155       if (session != null) {
156      session.close();
157       }
158       if (connection != null)
159       {
160          connection.close();
161       }
162
163       // call tearDown() in superclass
164
super.tearDown() ;
165    }
166
167    /**
168     * Test sync receive of message with jms ra.
169     */

170    public void testSyncRec() throws Exception JavaDoc {
171       // Send a message to queue
172
TextMessage JavaDoc message = session.createTextMessage();
173       message.setText(String.valueOf(MESSAGE_NR));
174       message.setIntProperty(Publisher.JMS_MESSAGE_NR, MESSAGE_NR);
175       sender.send(message);
176       getLog().debug("sent message with nr = " + MESSAGE_NR);
177
178       // Let bean fetch it sync
179
int res = rec.getMessage();
180       Assert.assertEquals(MESSAGE_NR, res);
181       getLog().debug("testSyncRec() OK");
182    }
183
184    public static Test suite() throws Exception JavaDoc
185    {
186       return new JBossTestSetup(getDeploySetup(RaSyncRecUnitTestCase.class, "jmsra.jar"))
187          {
188             protected void setUp() throws Exception JavaDoc
189             {
190                super.setUp();
191                ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
192            String JavaDoc resourceName = getJMSResourceRelativePathname("test-destinations-full-service.xml") ;
193                deploy (loader.getResource(resourceName).toString());
194             }
195
196              protected void tearDown() throws Exception JavaDoc
197              {
198                 super.tearDown();
199
200                 // Remove the messages
201
// richard.achmatowicz: why do we need to drain a Queue before we undeploy it?
202
//getServer().invoke
203
//(
204
// new ObjectName("jboss.mq.destination:service=Queue,name=testQueue"),
205
// "removeAllMessages",
206
// new Object[0],
207
// new String[0]
208
//);
209
ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
210         String JavaDoc resourceName = getJMSResourceRelativePathname("test-destinations-full-service.xml") ;
211                 undeploy (loader.getResource(resourceName).toString());
212              }
213           };
214    }
215 } // RaSyncRecUnitTestCase
216

217
218
219
220
221
Popular Tags