KickJava   Java API By Example, From Geeks To Geeks.

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

50 public abstract class RaTest extends JMSTestCase
51 {
52    /**
53     * Description of the Field
54     */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

237    protected abstract void init(final Context JavaDoc context) throws Exception JavaDoc;
238
239    /**
240     * The teardown method for JUnit
241     *
242     * @exception Exception Description of Exception
243     */

244    protected void tearDown() throws Exception JavaDoc
245    {
246       if (consumer != null)
247       {
248          consumer.close();
249       }
250       if (connection != null)
251       {
252          connection.close();
253       }
254
255       //call tearDown() in superclass
256
super.tearDown() ;
257    }
258
259    /**
260     * #Description of the Method
261     */

262    protected void printHeader()
263    {
264       getLog().debug("\n---- Testing method " + getName() +
265             " for bean " + beanJNDI);
266    }
267
268    /**
269     * #Description of the Method
270     */

271    protected void printOK()
272    {
273       getLog().debug("---- Test OK\n");
274    }
275
276    /**
277     * Flush the destiniation so we know that it contains no messages which might
278     * mess up the test.
279     *
280     * @exception Exception Description of Exception
281     */

282    protected void flush() throws Exception JavaDoc
283    {
284       // getLog().debug(" > Flushing Destination");
285

286       int nr = 0;
287       do
288       {
289          try
290          {
291             nr = getJmsMessage(FLUSH_TIMEOUT);
292          }
293          catch (Exception JavaDoc ignore)
294          {
295          }
296       } while (nr != -1);
297       // getLog().debug(" > Flushed");
298
}
299
300    public static junit.framework.Test suite() throws Exception JavaDoc
301    {
302        ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
303        String JavaDoc resourceName = getJMSResourceRelativePathname("test-destinations-service.xml") ;
304        return getDeploySetup(RaTest.class, loader.getResource(resourceName).toString());
305    }
306
307
308 }
309
Popular Tags