Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/objectweb/jtests/jms/conform/queue/QueueBrowserTest.java


1   /*
2    * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3    * Copyright (C) 2002 INRIA
4    * Contact: joram-team@objectweb.org
5    * 
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or any later version.
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
19   * USA
20   * 
21   * Initial developer(s): Jeff Mesnil (jmesnil@inrialpes.fr)
22   * Contributor(s): ______________________________________.
23   */
24  
25  package org.objectweb.jtests.jms.conform.queue;
26  
27  import org.objectweb.jtests.jms.framework.*;
28  import junit.framework.*;
29  
30  import javax.jms.*;
31  import java.util.Enumeration;
32  
33  /**
34   * Test the <code>javax.jms.QueueBrowser</code> features.
35   *
36   * @author Jeff Mesnil (jmesnil@inrialpes.fr)
37   * @version $Id: QueueBrowserTest.java,v 1.1 2002/04/21 21:15:19 chirino Exp $
38   */
39  public class QueueBrowserTest extends PTPTestCase {
40  
41    /**
42     * The <code>QueueBrowser</code> of the receiver's session
43     */
44    protected QueueBrowser receiverBrowser;
45  
46    /**
47     * The <code>QueueBrowser</code> of the sender's session
48     */
49    protected QueueBrowser senderBrowser ;
50  
51    /**
52     * Test the <code>QueueBrowser</code> of the sender.
53     */
54    public void testSenderBrowser() {
55      try {
56        TextMessage message_1 = senderSession.createTextMessage();
57        message_1.setText("testBrowser:message_1");
58        TextMessage message_2 = senderSession.createTextMessage();
59        message_2.setText("testBrowser:message_2");
60  
61        // send two messages...
62        sender.send(message_1);
63        sender.send(message_2);
64        
65        // ask the browser to browse the sender's session
66        Enumeration enumeration = senderBrowser.getEnumeration();
67        int count = 0;
68        while (enumeration.hasMoreElements()) {
69    // one more message in the queue
70    count ++;
71    // check that the message in the queue is one of the two which where sent
72    Object obj = enumeration.nextElement();
73    assertTrue(obj instanceof TextMessage);
74    TextMessage msg = (TextMessage)obj;
75    assertTrue(msg.getText().startsWith("testBrowser:message_"));
76        }
77        // check that there is effectively 2 messages in the queue
78        assertEquals(2, count);
79  
80        // receive the first message...
81        Message m = receiver.receive();
82        // ... and check it is the first which was sent.
83        assertTrue (m instanceof TextMessage);
84        TextMessage msg = (TextMessage)m;
85        assertEquals("testBrowser:message_1", msg.getText());
86  
87        // receive the second message...
88        m = receiver.receive();
89        // ... and check it is the second which was sent.
90        assertTrue (m instanceof TextMessage);
91        msg = (TextMessage)m;
92        assertEquals("testBrowser:message_2", msg.getText());
93  
94        // ask the browser to browse the sender's session
95        enumeration = receiverBrowser.getEnumeration();
96        // check that there is no messages in the queue
97        // (the two messages have been acknowledged and so removed
98        // from the queue)
99        assertTrue(!enumeration.hasMoreElements());
100     } catch (JMSException e) {
101       fail(e);
102     }
103   }
104 
105     /**
106    * Test that a <code>QueueBrowser</cdeo> created with a message selector
107    * browses only the messages matching this selector.
108    */
109   public void testBrowserWithMessageSelector() {
110     try {
111       senderBrowser = senderSession.createBrowser(senderQueue, "pi = 3.14159");
112 
113       TextMessage message_1 = senderSession.createTextMessage();
114       message_1.setText("testBrowserWithMessageSelector:message_1");
115       TextMessage message_2 = senderSession.createTextMessage();
116       message_2.setDoubleProperty("pi", 3.14159);
117       message_2.setText("testBrowserWithMessageSelector:message_2");
118 
119       sender.send(message_1);
120       sender.send(message_2);
121 
122       Enumeration enumeration = senderBrowser.getEnumeration();
123       int count = 0;
124       while (enumeration.hasMoreElements()) {
125   count ++;
126   Object obj = enumeration.nextElement();
127   assertTrue(obj instanceof TextMessage);
128   TextMessage msg = (TextMessage)obj;
129   assertEquals("testBrowserWithMessageSelector:message_2", msg.getText());
130       }
131       assertEquals(1, count);
132     } catch (JMSException e) {
133       fail(e);
134     }
135   }
136 
137   public void setUp() {
138     try {
139       super.setUp();
140       receiverBrowser = receiverSession.createBrowser(receiverQueue);
141       senderBrowser = senderSession.createBrowser(senderQueue);
142     } catch (JMSException e) {
143       e.printStackTrace();
144     }
145   }
146 
147   public void tearDown() {
148     try {
149       receiverBrowser.close();
150       senderBrowser.close();
151       super.tearDown();
152     } catch (JMSException e) {
153       e.printStackTrace();
154     } finally {
155       receiverBrowser = null;
156       senderBrowser = null;
157     }
158   }  
159 
160   /** 
161    * Method to use this class in a Test suite
162    */
163   public static Test suite() {
164     return new TestSuite(QueueBrowserTest.class);
165   }
166   
167   public QueueBrowserTest(String name) {
168     super(name);
169   }
170 }