KickJava   Java API By Example, From Geeks To Geeks.

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

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

66    protected QueueConnection JavaDoc connection;
67    /**
68     * JMS session
69     */

70    protected QueueSession JavaDoc session;
71    /**
72     * JMS sender
73     */

74    protected QueueSender JavaDoc sender;
75
76    /**
77     * Receiving bean
78     */

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

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

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

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

143    protected void tearDown() throws Exception JavaDoc
144    {
145       if (sender != null)
146       {
147          sender.close();
148       }
149       if (session != null) {
150      session.close();
151       }
152       if (connection != null)
153       {
154          connection.close();
155       }
156    }
157
158    /**
159     * Test sync receive of message with jms ra.
160     */

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

205
206
207
208
209
Popular Tags