KickJava   Java API By Example, From Geeks To Geeks.

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


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.Queue JavaDoc;
26 import javax.jms.QueueConnection JavaDoc;
27 import javax.jms.QueueConnectionFactory JavaDoc;
28 import javax.jms.QueueReceiver JavaDoc;
29 import javax.jms.QueueSender JavaDoc;
30 import javax.jms.QueueSession JavaDoc;
31 import javax.jms.Session JavaDoc;
32 import javax.management.Attribute JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34 import javax.naming.Context JavaDoc;
35
36 import org.jboss.mq.DestinationFullException;
37 import org.jboss.mx.util.ObjectNameFactory;
38 import org.jboss.test.JBossTestCase;
39 /**
40  * Destination Full tests
41  *
42  * @author <a HREF="mailto:adrian@jboss.org>Adrian Brock</a>
43  * @version <tt>$Revision: 37406 $</tt>
44  */

45 public class DestinationFullUnitTestCase extends JBossTestCase
46 {
47    static String JavaDoc QUEUE_FACTORY = "ConnectionFactory";
48    static String JavaDoc TEST_QUEUE = "queue/testQueue";
49    static ObjectName JavaDoc QUEUE_OBJECT_NAME = ObjectNameFactory.create("jboss.mq.destination:service=Queue,name=testQueue");
50
51    QueueConnection JavaDoc queueConnection;
52    Queue JavaDoc queue;
53
54    public DestinationFullUnitTestCase(String JavaDoc name) throws Exception JavaDoc
55    {
56       super(name);
57    }
58
59    public void testQueueFull() throws Exception JavaDoc
60    {
61       connect();
62       try
63       {
64          drainQueue();
65          setMaxDepth(10);
66          
67          QueueSession JavaDoc session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
68          QueueSender JavaDoc sender = session.createSender(queue);
69          Message JavaDoc message = session.createMessage();
70          
71          for (int i = 0; i < 10; ++i)
72             sender.send(message);
73
74          try
75          {
76             sender.send(message);
77             fail("Expected a destination full exception.");
78          }
79          catch (DestinationFullException expected)
80          {
81          }
82          session.close();
83          drainQueue();
84       }
85       finally
86       {
87          setMaxDepth(0);
88          disconnect();
89       }
90    }
91
92    protected void setMaxDepth(int depth)
93       throws Exception JavaDoc
94    {
95       getServer().setAttribute(QUEUE_OBJECT_NAME, new Attribute JavaDoc("MaxDepth", new Integer JavaDoc(depth)));
96    }
97
98    protected void connect() throws Exception JavaDoc
99    {
100       Context JavaDoc context = getInitialContext();
101       QueueConnectionFactory JavaDoc queueFactory = (QueueConnectionFactory JavaDoc) context.lookup(QUEUE_FACTORY);
102       queue = (Queue JavaDoc) context.lookup(TEST_QUEUE);
103       queueConnection = queueFactory.createQueueConnection();
104       queueConnection.start();
105
106       getLog().debug("Connection established.");
107    }
108
109    protected void disconnect()
110    {
111       try
112       {
113          if (queueConnection != null)
114             queueConnection.close();
115       }
116       catch (Exception JavaDoc ignored)
117       {
118       }
119
120       getLog().debug("Connection closed.");
121    }
122
123    // Emptys out all the messages in a queue
124
protected void drainQueue() throws Exception JavaDoc
125    {
126       QueueSession JavaDoc session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
127
128       QueueReceiver JavaDoc receiver = session.createReceiver(queue);
129       Message JavaDoc message = receiver.receive(50);
130       int c = 0;
131       while (message != null)
132       {
133          c++;
134          message = receiver.receive(50);
135       }
136
137       if (c != 0)
138          getLog().debug("Drained " + c + " messages from the queue");
139
140       session.close();
141    }
142
143     public static junit.framework.Test suite() throws Exception JavaDoc
144     {
145         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
146         return getDeploySetup(DestinationFullUnitTestCase.class,
147                 loader.getResource("messaging/test-destinations-service.xml").toString());
148     }
149 }
150
Popular Tags