KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > standalone > embeddedjms > unit > EmbeddedJmsTestCase


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.standalone.embeddedjms.unit;
23
24 import java.util.Hashtable JavaDoc;
25 import javax.jms.Queue JavaDoc;
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.TextMessage JavaDoc;
31 import javax.jms.Topic JavaDoc;
32 import javax.jms.TopicConnection JavaDoc;
33 import javax.jms.TopicConnectionFactory JavaDoc;
34 import javax.jms.TopicPublisher JavaDoc;
35 import javax.jms.TopicSession JavaDoc;
36 import javax.naming.InitialContext JavaDoc;
37 import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap;
38 import org.jboss.ejb3.test.standalone.embeddedjms.TestStatus;
39 import junit.extensions.TestSetup;
40 import junit.framework.Test;
41 import junit.framework.TestCase;
42 import junit.framework.TestSuite;
43
44 /**
45  * POJO Environment tests
46  *
47  * @author <a HREF="bill@jboss.org">Bill Burke</a>
48  * @version $Revision: 45712 $
49  */

50 public class EmbeddedJmsTestCase extends TestCase
51 {
52    private static boolean booted = false;
53
54    public EmbeddedJmsTestCase(String JavaDoc name)
55    {
56       super(name);
57    }
58
59    public static Test suite() throws Exception JavaDoc
60    {
61       TestSuite suite = new TestSuite();
62       suite.addTestSuite(EmbeddedJmsTestCase.class);
63
64       // setup test so that embedded JBoss is started/stopped once for all tests here.
65
TestSetup wrapper = new TestSetup(suite)
66       {
67          protected void setUp()
68          {
69             startupEmbeddedJboss();
70          }
71
72          protected void tearDown()
73          {
74             shutdownEmbeddedJboss();
75          }
76       };
77
78       return wrapper;
79    }
80
81    public static void startupEmbeddedJboss()
82    {
83       EJB3StandaloneBootstrap.boot(null);
84       EJB3StandaloneBootstrap.deployXmlResource("jboss-jms-beans.xml");
85       EJB3StandaloneBootstrap.deployXmlResource("embeddedjms/testjms.xml");
86       EJB3StandaloneBootstrap.scanClasspath("mdb.jar");
87    }
88
89    public static void shutdownEmbeddedJboss()
90    {
91       EJB3StandaloneBootstrap.shutdown();
92    }
93
94    protected InitialContext JavaDoc getInitialContext() throws Exception JavaDoc
95    {
96       return new InitialContext JavaDoc(getInitialContextProperties());
97    }
98
99    protected Hashtable JavaDoc getInitialContextProperties()
100    {
101       return EJB3StandaloneBootstrap.getInitialContextProperties();
102    }
103
104
105    public void testArchivesByResource() throws Throwable JavaDoc
106    {
107       InitialContext JavaDoc ctx = getInitialContext();
108
109       executeQueue(ctx);
110       executeTopic(ctx);
111       executeTemporaryQueue(ctx);
112    }
113
114
115    private void executeQueue(InitialContext JavaDoc ctx)
116            throws Exception JavaDoc
117    {
118
119       TestStatus status = (TestStatus) getInitialContext().lookup("TestStatusBean/remote");
120       status.clear();
121       QueueConnection JavaDoc cnn = null;
122       QueueSender JavaDoc sender = null;
123       QueueSession JavaDoc session = null;
124
125       Queue JavaDoc queue = (Queue JavaDoc) getInitialContext().lookup("queue/mdbtest");
126       QueueConnectionFactory JavaDoc factory = (QueueConnectionFactory JavaDoc) getInitialContext().lookup("java:/ConnectionFactory");
127       cnn = factory.createQueueConnection();
128       session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
129
130       TextMessage JavaDoc msg = session.createTextMessage("Hello World");
131
132       sender = session.createSender(queue);
133       sender.send(msg);
134
135       Thread.sleep(1000);
136       assertTrue(status.queueFired());
137    }
138    
139    private void executeTemporaryQueue(InitialContext JavaDoc ctx)
140       throws Exception JavaDoc
141    {
142       TestStatus status = (TestStatus) getInitialContext().lookup("TestStatusBean/remote");
143       status.clear();
144       QueueConnection JavaDoc cnn = null;
145       QueueSender JavaDoc sender = null;
146       QueueSession JavaDoc session = null;
147       
148       Queue JavaDoc queue = (Queue JavaDoc) getInitialContext().lookup("queue/tempmdbtest");
149       QueueConnectionFactory JavaDoc factory = (QueueConnectionFactory JavaDoc) getInitialContext().lookup("java:/ConnectionFactory");
150       cnn = factory.createQueueConnection();
151       session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
152       
153       TextMessage JavaDoc msg = session.createTextMessage("Hello World");
154       
155       sender = session.createSender(queue);
156       sender.send(msg);
157       
158       Thread.sleep(1000);
159       assertTrue(status.temporaryQueueFired());
160    }
161
162    private void executeTopic(InitialContext JavaDoc ctx)
163            throws Exception JavaDoc
164    {
165
166       TestStatus status = (TestStatus) getInitialContext().lookup("TestStatusBean/remote");
167       status.clear();
168       TopicConnection JavaDoc cnn = null;
169       TopicPublisher JavaDoc sender = null;
170       TopicSession JavaDoc session = null;
171
172       Topic JavaDoc topic = (Topic JavaDoc) getInitialContext().lookup("topic/topictest");
173       TopicConnectionFactory JavaDoc factory = (TopicConnectionFactory JavaDoc) getInitialContext().lookup("java:/ConnectionFactory");
174       cnn = factory.createTopicConnection();
175       session = cnn.createTopicSession(false, QueueSession.AUTO_ACKNOWLEDGE);
176
177       TextMessage JavaDoc msg = session.createTextMessage("Hello World");
178
179       sender = session.createPublisher(topic);
180       sender.send(msg);
181
182       Thread.sleep(1000);
183       assertTrue(status.topicFired());
184    }
185
186    protected void configureLoggingAfterBootstrap()
187    {
188       //enableTrace("org.jboss.tm");
189
}
190 }
Popular Tags