KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmsra > test > RaTest


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.MessageConsumer JavaDoc;
27 import javax.jms.Session JavaDoc;
28 import javax.naming.Context JavaDoc;
29
30 import javax.naming.InitialContext JavaDoc;
31 import junit.framework.Assert;
32
33 import junit.framework.TestCase;
34
35 import org.jboss.test.JBossTestCase;
36
37 import org.jboss.test.jmsra.bean.*;
38
39 /**
40  * Abstract test cases for JMS Resource Adapter. <p>
41  *
42  * Created: Mon Apr 23 21:35:25 2001
43  *
44  * @author <a HREF="mailto:peter.antman@tim.se">Peter Antman</a>
45  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
46  * @version $Revision: 37406 $
47  */

48 public abstract class RaTest
49        extends JBossTestCase
50 {
51    /**
52     * Description of the Field
53     */

54    public final static long DEFAULT_TIMEOUT = 500L;
55    /**
56     * Description of the Field
57     */

58    public final static long FLUSH_TIMEOUT = 500L;
59
60    /**
61     * Description of the Field
62     */

63    protected String JavaDoc beanJNDI;
64    /**
65     * Description of the Field
66     */

67    protected MessageConsumer JavaDoc consumer;
68    /**
69     * Description of the Field
70     */

71    protected Publisher publisher;
72    /**
73     * Description of the Field
74     */

75    protected Connection JavaDoc connection;
76    /**
77     * Description of the Field
78     */

79    protected Session JavaDoc session;
80
81    /**
82     * Constructor for the RaTest object
83     *
84     * @param name Description of Parameter
85     * @param beanJNDI Description of Parameter
86     * @exception Exception Description of Exception
87     */

88    protected RaTest(final String JavaDoc name, final String JavaDoc beanJNDI)
89           throws Exception JavaDoc
90    {
91       super(name);
92       this.beanJNDI = beanJNDI;
93    }
94
95    /**
96     * A unit test for JUnit
97     *
98     * @exception Exception Description of Exception
99     */

100    public void testSimple() throws Exception JavaDoc
101    {
102       printHeader();
103       getLog().debug("Verify simple send of message");
104       publisher.simple(1);
105
106       Assert.assertEquals(1, getJmsMessage());
107       printOK();
108    }
109
110    /**
111     * A unit test for JUnit
112     *
113     * @exception Exception Description of Exception
114     */

115    public void testSimpleFail() throws Exception JavaDoc
116    {
117       printHeader();
118       getLog().debug("Verify simple failed transaction");
119       publisher.simpleFail(2);
120
121       Assert.assertEquals(-1, getJmsMessage());
122       printOK();
123    }
124
125    /**
126     * A unit test for JUnit
127     *
128     * @exception Exception Description of Exception
129     */

130    public void testBeanOk() throws Exception JavaDoc
131    {
132       printHeader();
133       getLog().debug("Verify bean ok");
134       publisher.beanOk(3);
135
136       Assert.assertEquals(3, getJmsMessage());
137       printOK();
138    }
139
140    /**
141     * A unit test for JUnit
142     *
143     * @exception Exception Description of Exception
144     */

145    public void testBeanError() throws Exception JavaDoc
146    {
147       printHeader();
148       getLog().debug("Verify bean eroor failed transaction");
149
150       try
151       {
152          publisher.beanError(4);
153       }
154       catch (Exception JavaDoc ignore)
155       {
156       }
157
158       Assert.assertEquals(-1, getJmsMessage());
159       printOK();
160    }
161
162    /**
163     * The JUnit setup method
164     *
165     * @exception Exception Description of Exception
166     */

167    protected void setUp() throws Exception JavaDoc
168    {
169       // Create a publisher
170
Context JavaDoc context = getInitialContext();
171       try
172       {
173          PublisherHome home = (PublisherHome)context.lookup(beanJNDI);
174          publisher = home.create();
175
176          init(context);
177       }
178       finally
179       {
180          context.close();
181       }
182
183       // start up the session
184
connection.start();
185
186       // flush the destination
187
flush();
188    }
189
190    /**
191     * Check if we got a message.
192     *
193     * @return Publisher.JMS_MESSAGE_NR int property or -1 if no
194     * message was received.
195     * @exception Exception Description of Exception
196     */

197    protected int getJmsMessage() throws Exception JavaDoc
198    {
199       return getJmsMessage(DEFAULT_TIMEOUT);
200    }
201
202    /**
203     * Check if we got a message.
204     *
205     * @param timeout The time to wait for a message.
206     * @return Publisher.JMS_MESSAGE_NR int property or -1 if no
207     * message was received.
208     * @exception Exception Description of Exception
209     */

210    protected int getJmsMessage(long timeout) throws Exception JavaDoc
211    {
212       Message JavaDoc msg = consumer.receive(timeout);
213       if (msg != null)
214       {
215          getLog().debug("Recived message: " + msg);
216          int nr = msg.getIntProperty(Publisher.JMS_MESSAGE_NR);
217          getLog().debug("nr: " + nr);
218          return nr;
219       }
220       else
221       {
222          getLog().debug("NO message recived");
223          return -1;
224       }
225    }
226
227    /**
228     * #Description of the Method
229     *
230     * @param context Description of Parameter
231     * @exception Exception Description of Exception
232     */

233    protected abstract void init(final Context JavaDoc context) throws Exception JavaDoc;
234
235    /**
236     * The teardown method for JUnit
237     *
238     * @exception Exception Description of Exception
239     */

240    protected void tearDown() throws Exception JavaDoc
241    {
242       if (consumer != null)
243       {
244          consumer.close();
245       }
246       if (connection != null)
247       {
248          connection.close();
249       }
250    }
251
252    /**
253     * #Description of the Method
254     */

255    protected void printHeader()
256    {
257       getLog().debug("\n---- Testing method " + getName() +
258             " for bean " + beanJNDI);
259    }
260
261    /**
262     * #Description of the Method
263     */

264    protected void printOK()
265    {
266       getLog().debug("---- Test OK\n");
267    }
268
269    /**
270     * Flush the destiniation so we know that it contains no messages which might
271     * mess up the test.
272     *
273     * @exception Exception Description of Exception
274     */

275    protected void flush() throws Exception JavaDoc
276    {
277       // getLog().debug(" > Flushing Destination");
278

279       int nr = 0;
280       do
281       {
282          try
283          {
284             nr = getJmsMessage(FLUSH_TIMEOUT);
285          }
286          catch (Exception JavaDoc ignore)
287          {
288          }
289       } while (nr != -1);
290       // getLog().debug(" > Flushed");
291
}
292
293    public static junit.framework.Test suite() throws Exception JavaDoc
294    {
295        ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
296        return getDeploySetup(RaTest.class,
297                loader.getResource("messaging/test-destinations-service.xml").toString());
298    }
299
300
301 }
302
Popular Tags