KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > mdbtopic > unit > TopicMDBUnitTestCase


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, Red Hat Middleware LLC, 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.mdbtopic.unit;
23
24 import javax.jms.MessageProducer JavaDoc;
25 import javax.jms.QueueSession JavaDoc;
26 import javax.jms.TextMessage JavaDoc;
27 import javax.jms.Topic JavaDoc;
28 import javax.jms.TopicConnection JavaDoc;
29 import javax.jms.TopicConnectionFactory JavaDoc;
30 import javax.jms.TopicSession JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32
33 import junit.framework.Test;
34
35 import org.jboss.ejb3.test.mdbtopic.TestStatus;
36 //import org.jboss.ejb3.test.mdb.TestStatus; // makes StackOverflowErrors
37
import org.jboss.logging.Logger;
38 import org.jboss.test.JBossTestCase;
39
40 /**
41  * Sample client for the jboss container.
42  *
43  * @author <a HREF="mailto:bill@burkecentral.com">Bill Burke</a>
44  * @version $Id: MDBUnitTestCase.java 58403 2006-11-15 17:14:37 +0100 (Wed, 15 Nov 2006) wolfc $
45  */

46 public class TopicMDBUnitTestCase extends JBossTestCase
47 {
48    @SuppressWarnings JavaDoc("unused")
49    private static final Logger log = Logger.getLogger(TopicMDBUnitTestCase.class);
50
51    static boolean deployed = false;
52
53    static int test = 0;
54
55    public TopicMDBUnitTestCase(String JavaDoc name)
56    {
57       super(name);
58    }
59
60    public void testTopic() throws Exception JavaDoc
61    {
62       TestStatus status = (TestStatus) getInitialContext().lookup("TestStatusBean/remote");
63       clear(status);
64       TopicConnection JavaDoc cnn = null;
65       MessageProducer JavaDoc sender = null;
66       TopicSession JavaDoc session = null;
67
68       Topic JavaDoc topic = (Topic JavaDoc) getInitialContext().lookup("topic/mdbtest");
69       TopicConnectionFactory JavaDoc factory = getTopicConnectionFactory();
70       cnn = factory.createTopicConnection();
71       session = cnn.createTopicSession(false, QueueSession.AUTO_ACKNOWLEDGE);
72
73       TextMessage JavaDoc msg = session.createTextMessage("Hello World");
74
75       sender = session.createProducer(topic);
76       sender.send(msg);
77       session.close();
78       cnn.close();
79
80       Thread.sleep(2000);
81       assertEquals(1, status.topicFired());
82       assertTrue(status.interceptedTopic());
83       assertFalse(status.postConstruct());
84       assertFalse(status.preDestroy());
85    }
86
87    private TopicConnectionFactory JavaDoc getTopicConnectionFactory()
88          throws Exception JavaDoc
89    {
90       try
91       {
92          return (TopicConnectionFactory JavaDoc) getInitialContext().lookup("ConnectionFactory");
93       }
94       catch (NamingException JavaDoc e)
95       {
96          return (TopicConnectionFactory JavaDoc) getInitialContext().lookup("java:/ConnectionFactory");
97       }
98    }
99
100    private void clear(TestStatus status)
101    {
102       status.clear();
103       assertEquals(0, status.bmtQueueRan());
104       assertEquals(0, status.defaultedQueueFired());
105       assertEquals(0, status.messageCount());
106       assertEquals(0, status.nondurableQueueFired());
107       assertEquals(0, status.overrideDefaultedQueueFired());
108       assertEquals(0, status.overrideQueueFired());
109       assertEquals(0, status.queueFired());
110       assertEquals(0, status.topicFired());
111       assertFalse(status.interceptedQueue());
112       assertFalse(status.interceptedTopic());
113       assertFalse(status.postConstruct());
114       assertFalse(status.preDestroy());
115    }
116
117 // protected InitialContext getInitialContext() throws Exception
118
// {
119
// return InitialContextFactory.getInitialContext();
120
// }
121
//
122
public static Test suite() throws Exception JavaDoc
123    {
124       return getDeploySetup(TopicMDBUnitTestCase.class, "mdbtopic-test.jar");
125    }
126
127 }
Popular Tags