KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmq > test > CleanTopicRemovalUnitTestCase


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.jbossmq.test;
23
24 import javax.jms.Message JavaDoc;
25 import javax.jms.Session JavaDoc;
26 import javax.jms.Topic JavaDoc;
27 import javax.jms.TopicConnection JavaDoc;
28 import javax.jms.TopicConnectionFactory JavaDoc;
29 import javax.jms.TopicPublisher JavaDoc;
30 import javax.jms.TopicSession JavaDoc;
31 import javax.management.MBeanServerConnection JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33 import javax.naming.Context JavaDoc;
34
35 import org.jboss.mx.util.ObjectNameFactory;
36 import org.jboss.test.JBossTestCase;
37
38 /**
39  * A test to make sure topic subscriptions are tidied up correctly
40  *
41  * @author <a HREF="mailto:adrian@jboss.org>Adrian Brock</a>
42  * @version <tt>$Revision: 37406 $</tt>
43  */

44 public class CleanTopicRemovalUnitTestCase extends JBossTestCase
45 {
46    static String JavaDoc TOPIC_FACTORY = "ConnectionFactory";
47    static ObjectName JavaDoc destinationManager = ObjectNameFactory.create("jboss.mq:service=DestinationManager");
48    static ObjectName JavaDoc messageCache = ObjectNameFactory.create("jboss.mq:service=MessageCache");
49    
50    TopicConnection JavaDoc topicConnection;
51    Topic JavaDoc topic;
52    
53    public CleanTopicRemovalUnitTestCase(String JavaDoc name) throws Exception JavaDoc
54    {
55       super(name);
56    }
57
58    public void testCleanTopicRemoval() throws Throwable JavaDoc
59    {
60       createTopic();
61       try
62       {
63          connect();
64          try
65          {
66             TopicSession JavaDoc session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
67             TopicPublisher JavaDoc publisher = session.createPublisher(topic);
68             session.createSubscriber(topic);
69             topicConnection.start();
70
71             getLog().debug("Publish message");
72             Message JavaDoc message = session.createMessage();
73             publisher.publish(message);
74             
75             int beforeCacheCount = getCacheCount();
76             getLog().debug("beforeCacheCount=" + beforeCacheCount);
77             
78             removeTopic();
79             
80             int afterCacheCount = getCacheCount();
81             getLog().debug("afterCacheCount=" + afterCacheCount);
82            
83             assertEquals("Message should be removed ", afterCacheCount, beforeCacheCount - 1);
84          }
85          finally
86          {
87             disconnect();
88          }
89       }
90       catch (Throwable JavaDoc t)
91       {
92          try
93          {
94             getLog().error("Error ", t);
95             removeTopic();
96          }
97          catch (Throwable JavaDoc ignored)
98          {
99          }
100          throw t;
101       }
102    }
103
104    protected void connect() throws Exception JavaDoc
105    {
106       Context JavaDoc context = getInitialContext();
107       TopicConnectionFactory JavaDoc topicFactory = (TopicConnectionFactory JavaDoc) context.lookup(TOPIC_FACTORY);
108       topicConnection = topicFactory.createTopicConnection();
109
110       getLog().debug("Connection established.");
111    }
112
113    protected void disconnect()
114    {
115       try
116       {
117          if (topicConnection != null)
118             topicConnection.close();
119       }
120       catch (Throwable JavaDoc ignored)
121       {
122          getLog().warn("Ignored", ignored);
123       }
124
125       getLog().debug("Connection closed.");
126    }
127
128    protected void createTopic() throws Exception JavaDoc
129    {
130       getLog().debug("Create topic");
131       MBeanServerConnection JavaDoc server = getServer();
132       server.invoke(destinationManager, "createTopic",
133          new Object JavaDoc[]
134          {
135             "cleanTopicRemovalTest",
136             "topic/cleanTopicRemovalTest"
137          },
138          new String JavaDoc[]
139          {
140             String JavaDoc.class.getName(),
141             String JavaDoc.class.getName()
142          }
143       );
144       Context JavaDoc context = getInitialContext();
145       topic = (Topic JavaDoc) context.lookup("topic/cleanTopicRemovalTest");
146       
147       log.debug("Got topic " + topic);
148    }
149
150    protected void removeTopic() throws Exception JavaDoc
151    {
152       getLog().debug("Remove topic");
153       MBeanServerConnection JavaDoc server = getServer();
154       server.invoke(destinationManager, "destroyTopic",
155          new Object JavaDoc[]
156          {
157             "cleanTopicRemovalTest",
158          },
159          new String JavaDoc[]
160          {
161             String JavaDoc.class.getName()
162          }
163       );
164    }
165
166    protected int getCacheCount() throws Exception JavaDoc
167    {
168       MBeanServerConnection JavaDoc server = getServer();
169       Integer JavaDoc cacheCount = (Integer JavaDoc) server.getAttribute(messageCache, "TotalCacheSize");
170       return cacheCount.intValue();
171    }
172 }
173
174
Popular Tags