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/connection/TopicConnectionTest.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): Andreas Mueller <am@iit.de>
23   */
24  
25  package org.objectweb.jtests.jms.conform.connection;
26  
27  import org.objectweb.jtests.jms.framework.*;
28  import javax.jms.*;
29  import junit.framework.*;
30  
31  /**
32   * Test topic-specific connection features.
33   *
34   * Test setting of client ID which is relevant only for Durable Subscribtion
35   */
36  
37  public class TopicConnectionTest extends PubSubTestCase {
38      
39      /**
40       * Test that a call to <code>setClientID</code> will throw an 
41       * <code>IllegalStateException</code> if a client ID has already been set
42       * see JMS javadoc 
43       * http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/jms/Connection.html#setClientID(java.lang.String)
44       */
45      public void testSetClientID_1() {
46    try {
47        // we start from a clean state for the connection
48        subscriberConnection.close();
49        subscriberConnection = null;
50        
51        subscriberConnection = subscriberTCF.createTopicConnection();
52        // if the JMS provider does not set a client ID, we do.
53        if (subscriberConnection.getClientID() == null) {
54      subscriberConnection.setClientID("testSetClientID_1");
55      assertEquals("testSetClientID_1", subscriberConnection.getClientID());
56        }
57        // now the connection has a client ID (either "testSetClientID_1" or one set by the provider
58        assertTrue(subscriberConnection.getClientID() != null);
59        
60        // a attempt to set a client ID should now throw an IllegalStateException
61        subscriberConnection.setClientID("another client ID");
62        fail("Should raise a javax.jms.IllegalStateException");
63    } catch (javax.jms.IllegalStateException e) {
64    } catch (JMSException e) {
65        fail("Should raise a javax.jms.IllegalStateException, not a "+ e);
66    } catch (java.lang.IllegalStateException e) {
67        fail("Should raise a javax.jms.IllegalStateException, not a java.lang.IllegalStateException");
68    }
69      }
70  
71      /** 
72       * Test that a call to <code>setClientID</code> can occur only after connection creation 
73       * and before any other action on the connection.
74       * <em>This test is relevant only if the ID is set by the JMS client</em>
75       * see JMS javadoc 
76       * http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/jms/Connection.html#setClientID(java.lang.String)
77       */
78      public void testSetClientID_2() {
79    try {
80        // we start from a clean state for the first connection
81        subscriberConnection.close();
82        subscriberConnection = null;
83  
84        subscriberConnection = subscriberTCF.createTopicConnection();
85        // if the JMS provider has set a client ID, this test is not relevant
86        if (subscriberConnection.getClientID() != null) {
87      return;
88        }
89  
90        //we start the connection 
91        subscriberConnection.start();
92        
93        // an attempt to set the client ID now should throw a IllegalStateException
94        subscriberConnection.setClientID("testSetClientID_2");
95        fail("Should throw a javax.jms.IllegalStateException");
96    } catch (javax.jms.IllegalStateException e) {
97    } catch (JMSException e) {
98        fail("Should raise a javax.jms.IllegalStateException, not a "+ e);
99    } catch (java.lang.IllegalStateException e) {
100       fail("Should raise a javax.jms.IllegalStateException, not a java.lang.IllegalStateException");
101   }
102     }
103   
104     /**
105      * Test that if another connection with the same clientID is already running when 
106      * <code>setClientID</code> is called, the JMS provider should detect the duplicate
107      * ID and throw an <code>InvalidClientIDException</code>
108      * <em>This test is relevant only if the ID is set by the JMS client</em>
109      * see JMS javadoc 
110      * http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/jms/Connection.html#setClientID(java.lang.String)
111      */
112     public void testSetClientID_3() {
113   try {
114       // we start from a clean state for the first connection
115       subscriberConnection.close();
116       subscriberConnection = null;
117 
118       subscriberConnection = subscriberTCF.createTopicConnection();
119       // if the JMS provider has set a client ID, this test is not relevant
120       if (subscriberConnection.getClientID() != null) {
121     return;
122       }
123       // the JMS provider has not set a client ID, so we do
124       subscriberConnection.setClientID("testSetClientID_3");
125       assertEquals("testSetClientID_3", subscriberConnection.getClientID());
126 
127       // we create a new connection and try to set the same ID than for subscriberConnection
128       TopicConnection connection_2 = subscriberTCF.createTopicConnection();
129       assertTrue(connection_2.getClientID() == null);
130       connection_2.setClientID("testSetClientID_3");
131       fail("Should throw a javax.jms.InvalidClientIDException");
132   } catch (InvalidClientIDException e) {
133   } catch (JMSException e) {
134       fail(e);
135   }
136     }
137 
138     /** 
139      * Method to use this class in a Test suite
140      */
141     public static Test suite() {
142   return new TestSuite(TopicConnectionTest.class);
143     }
144   
145     public TopicConnectionTest(String name) {
146   super(name);
147     }
148 }
149 
150 
151