View Javadoc
1 /*** 2 * Redistribution and use of this software and associated documentation 3 * ("Software"), with or without modification, are permitted provided 4 * that the following conditions are met: 5 * 6 * 1. Redistributions of source code must retain copyright 7 * statements and notices. Redistributions must also contain a 8 * copy of this document. 9 * 10 * 2. Redistributions in binary form must reproduce the 11 * above copyright notice, this list of conditions and the 12 * following disclaimer in the documentation and/or other 13 * materials provided with the distribution. 14 * 15 * 3. The name "Exolab" must not be used to endorse or promote 16 * products derived from this Software without prior written 17 * permission of Exoffice Technologies. For written permission, 18 * please contact tma@netspace.net.au. 19 * 20 * 4. Products derived from this Software may not be called "Exolab" 21 * nor may "Exolab" appear in their names without prior written 22 * permission of Exoffice Technologies. Exolab is a registered 23 * trademark of Exoffice Technologies. 24 * 25 * 5. Due credit should be given to the Exolab Project 26 * (http://www.exolab.org/). 27 * 28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 39 * OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved. 42 * 43 * $Id: ClientIdentifierTest.java,v 1.8 2004/02/03 07:32:07 tanderson Exp $ 44 */ 45 package org.exolab.jmscts.test.connection; 46 47 import javax.jms.Connection; 48 import javax.jms.ExceptionListener; 49 import javax.jms.IllegalStateException; 50 import javax.jms.InvalidClientIDException; 51 import javax.jms.JMSException; 52 import javax.jms.Session; 53 54 import org.apache.log4j.Category; 55 56 import junit.framework.Test; 57 58 import org.exolab.jmscts.core.AbstractConnectionTestCase; 59 import org.exolab.jmscts.core.AckTypes; 60 import org.exolab.jmscts.core.ConnectionHelper; 61 import org.exolab.jmscts.core.ConnectionFactoryHelper; 62 import org.exolab.jmscts.core.TestContext; 63 import org.exolab.jmscts.core.TestCreator; 64 65 66 /*** 67 * This class verifies that the client identifier can be set, but only 68 * on creation of a connection. 69 * 70 * @todo: need to cover connection consumers 71 * 72 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a> 73 * @version $Revision: 1.8 $ 74 * @see AbstractConnectionTestCase 75 * @see org.exolab.jmscts.core.ConnectionTestRunner 76 */ 77 public class ClientIdentifierTest extends AbstractConnectionTestCase { 78 79 /*** 80 * The client identifier 81 */ 82 private static final String CLIENT_ID = "myClientID"; 83 84 /*** 85 * The logger 86 */ 87 private static final Category log = 88 Category.getInstance(ClientIdentifierTest.class); 89 90 91 /*** 92 * Create a new <code>ClientIdentifierTest</code> 93 * 94 * @param name the name of test case 95 */ 96 public ClientIdentifierTest(String name) { 97 super(name); 98 } 99 100 /*** 101 * Sets up the test suite 102 * 103 * @return an instance of this class that may be run by 104 * {@link org.exolab.jmscts.core.JMSTestRunner} 105 */ 106 public static Test suite() { 107 return TestCreator.createConnectionTest(ClientIdentifierTest.class); 108 } 109 110 /*** 111 * Verifies that the client identifier can be set on a connection, just 112 * after it is created, but not subsequently. 113 * 114 * @jmscts.requirement connection.method.setClientID 115 * @jmscts.requirement connection.clientID.administered 116 * @throws Exception for any error 117 */ 118 public void testSetOnCreation() throws Exception { 119 TestContext context = getContext(); 120 Connection connection = context.getConnection(); 121 try { 122 connection.setClientID(CLIENT_ID); 123 } catch (IllegalStateException exception) { 124 context.getCoverage().setUnsupported( 125 "connection.clientID.administered"); 126 } catch (Exception exception) { 127 String msg = "Expected IllegalStateException to be thrown if an " 128 + "administered client identifier exists for a connection, " 129 + "but got: " + exception.getClass().getName(); 130 log.debug(msg, exception); 131 fail(msg); 132 } 133 expectFailure(); 134 } 135 136 /*** 137 * Verifies that two connections may not have the same client identifier 138 * @todo - check variations of queue and topic connections 139 * 140 * @jmscts.requirement connection.clientID.duplicate 141 * @throws Exception for any error 142 */ 143 public void testDuplicateClientID() throws Exception { 144 TestContext context = getContext(); 145 Connection first = context.getConnection(); 146 Connection second = null; 147 boolean test = true; 148 149 try { 150 second = ConnectionFactoryHelper.createConnection(context, 151 CLIENT_ID); 152 } catch (IllegalStateException unsupported) { 153 // can't continue to test - the provider uses administered 154 // client identifiers 155 test = false; 156 } 157 if (test) { 158 try { 159 first.setClientID(CLIENT_ID); 160 fail("Managed to set the same client identifier on two " 161 + "different connections"); 162 } catch (InvalidClientIDException expected) { 163 // the expected behaviour 164 } catch (Exception exception) { 165 fail("Expected setClientID to throw , InvalidClientIDException" 166 + " but got: " + exception); 167 } finally { 168 second.close(); 169 } 170 171 // verify that the client identifier may now be set 172 first.setClientID(CLIENT_ID); 173 } 174 } 175 176 /*** 177 * Verifies that the client identifier cannot be set on a closed connection 178 * 179 * @jmscts.requirement connection.method.setClientID 180 * @throws Exception for any error 181 */ 182 public void testSetAfterClose() throws Exception { 183 Connection connection = getContext().getConnection(); 184 connection.close(); 185 expectFailure(); 186 } 187 188 /*** 189 * Verifies that the client identifier cannot be set after an exception 190 * listener has been registered 191 * 192 * @jmscts.requirement connection.method.setClientID 193 * @throws Exception for any error 194 */ 195 public void testSetAfterListenerRegistration() throws Exception { 196 Connection connection = getContext().getConnection(); 197 connection.setExceptionListener(new ExceptionListener() { 198 public void onException(JMSException exception) { 199 } 200 }); 201 expectFailure(); 202 } 203 204 /*** 205 * Verifies that the client identifier cannot be set after the connection 206 * has been started 207 * 208 * @jmscts.requirement connection.method.setClientID 209 * @throws Exception for any error 210 */ 211 public void testSetAfterStart() throws Exception { 212 Connection connection = getContext().getConnection(); 213 connection.start(); 214 expectFailure(); 215 } 216 217 /*** 218 * Verifies that the client identifier cannot be set after the connection 219 * has stopped 220 * 221 * @jmscts.requirement connection.method.setClientID 222 * @throws Exception for any error 223 */ 224 public void testSetAfterStop() throws Exception { 225 Connection connection = getContext().getConnection(); 226 connection.stop(); 227 expectFailure(); 228 } 229 230 /*** 231 * Verifies that the client identifier cannot be set after a session has 232 * been created 233 * 234 * @jmscts.requirement connection.method.setClientID 235 * @throws Exception for any error 236 */ 237 public void testSetAfterCreateSession() throws Exception { 238 TestContext context = getContext(); 239 Session session = ConnectionHelper.createSession( 240 context, AckTypes.AUTO_ACKNOWLEDGE); 241 expectFailure(); 242 } 243 244 /*** 245 * Returns true if the connection should be started prior to running the 246 * test. This implementation returns false, as it would affect the 247 * behaviour of {@link #testSetOnCreation} 248 * 249 * @return false 250 */ 251 public boolean startConnection() { 252 return false; 253 } 254 255 /*** 256 * Expect the setClientID operation to fail 257 * 258 * @throws Exception if setClientID is successful or the wrong exception is 259 * thrown 260 */ 261 private void expectFailure() throws Exception { 262 try { 263 getContext().getConnection().setClientID(CLIENT_ID); 264 fail("setClientID should throw IllegalStateException"); 265 } catch (IllegalStateException expected) { 266 // the expected behaviour 267 } catch (Exception exception) { 268 fail("Expected setClientID to throw IllegalStateException, " 269 + "but got: " + exception); 270 } 271 } 272 273 }

This page was automatically generated by Maven