From b3bcd521a46ca9e351e295fc8685712291bd86dd Mon Sep 17 00:00:00 2001 From: Robert Gemmell Date: Mon, 28 May 2012 12:20:35 +0000 Subject: QPID-4023: restore connection URL setter, add check that URL details have been set before calling connect(), add unit+sys test to verify operation git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1343220 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/qpid/client/AMQConnectionFactory.java | 20 ++++-- .../qpid/client/AMQConnectionFactoryTest.java | 79 ++++++++++++++++++++++ .../qpid/test/unit/jndi/ConnectionFactoryTest.java | 63 ----------------- .../client/connection/ConnectionFactoryTest.java | 19 +++++- 4 files changed, 113 insertions(+), 68 deletions(-) create mode 100644 qpid/java/client/src/test/java/org/apache/qpid/client/AMQConnectionFactoryTest.java delete mode 100644 qpid/java/client/src/test/java/org/apache/qpid/test/unit/jndi/ConnectionFactoryTest.java (limited to 'qpid/java') diff --git a/qpid/java/client/src/main/java/org/apache/qpid/client/AMQConnectionFactory.java b/qpid/java/client/src/main/java/org/apache/qpid/client/AMQConnectionFactory.java index cc91746d98..8bc815d98e 100644 --- a/qpid/java/client/src/main/java/org/apache/qpid/client/AMQConnectionFactory.java +++ b/qpid/java/client/src/main/java/org/apache/qpid/client/AMQConnectionFactory.java @@ -55,12 +55,13 @@ public class AMQConnectionFactory implements ConnectionFactory, QueueConnectionF ObjectFactory, Referenceable, XATopicConnectionFactory, XAQueueConnectionFactory, XAConnectionFactory { - private final ConnectionURL _connectionDetails; + protected static final String NO_URL_CONFIGURED = "The connection factory wasn't created with a proper URL, the connection details are empty"; + + private ConnectionURL _connectionDetails; // The default constructor is necessary to allow AMQConnectionFactory to be deserialised from JNDI public AMQConnectionFactory() { - _connectionDetails = null; } public AMQConnectionFactory(final String url) throws URLSyntaxException @@ -106,6 +107,11 @@ public class AMQConnectionFactory implements ConnectionFactory, QueueConnectionF public Connection createConnection() throws JMSException { + if(_connectionDetails == null) + { + throw new JMSException(NO_URL_CONFIGURED); + } + try { if (_connectionDetails.getClientName() == null || _connectionDetails.getClientName().equals("")) @@ -158,7 +164,7 @@ public class AMQConnectionFactory implements ConnectionFactory, QueueConnectionF } else { - throw new JMSException("The connection factory wasn't created with a proper URL, the connection details are empty"); + throw new JMSException(NO_URL_CONFIGURED); } } @@ -193,6 +199,12 @@ public class AMQConnectionFactory implements ConnectionFactory, QueueConnectionF return _connectionDetails.toString(); } + //setter necessary to use instances created with the default constructor (which we can't remove) + public final void setConnectionURLString(String url) throws URLSyntaxException + { + _connectionDetails = new AMQConnectionURL(url); + } + /** * JNDI interface to create objects from References. * @@ -332,7 +344,7 @@ public class AMQConnectionFactory implements ConnectionFactory, QueueConnectionF } else { - throw new JMSException("The connection factory wasn't created with a proper URL, the connection details are empty"); + throw new JMSException(NO_URL_CONFIGURED); } } diff --git a/qpid/java/client/src/test/java/org/apache/qpid/client/AMQConnectionFactoryTest.java b/qpid/java/client/src/test/java/org/apache/qpid/client/AMQConnectionFactoryTest.java new file mode 100644 index 0000000000..bb92fa4ecd --- /dev/null +++ b/qpid/java/client/src/test/java/org/apache/qpid/client/AMQConnectionFactoryTest.java @@ -0,0 +1,79 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.client; + +import javax.jms.JMSException; + +import junit.framework.TestCase; + +import org.apache.qpid.client.AMQConnectionFactory; +import org.apache.qpid.jms.BrokerDetails; +import org.apache.qpid.jms.ConnectionURL; + +public class AMQConnectionFactoryTest extends TestCase +{ + + //URL will be returned with the password field swapped for '********' + // so ensure that these two strings are kept in sync. + public static final String URL = "amqp://guest:guest@clientID/test?brokerlist='tcp://localhost:5672'"; + public static final String URL_STAR_PWD = "amqp://guest:********@clientID/test?brokerlist='tcp://localhost:5672'"; + + public void testConnectionURLStringMasksPassword() throws Exception + { + AMQConnectionFactory factory = new AMQConnectionFactory(URL); + + //URL will be returned with the password field swapped for '********' + assertEquals("Connection URL not correctly set", URL_STAR_PWD, factory.getConnectionURLString()); + + // Further test that the processed ConnectionURL is as expected after + // the set call + ConnectionURL connectionurl = factory.getConnectionURL(); + + assertNull("Failover is set.", connectionurl.getFailoverMethod()); + assertEquals("guest", connectionurl.getUsername()); + assertEquals("guest", connectionurl.getPassword()); + assertEquals("clientID", connectionurl.getClientName()); + assertEquals("/test", connectionurl.getVirtualHost()); + + assertEquals(1, connectionurl.getBrokerCount()); + + BrokerDetails service = connectionurl.getBrokerDetails(0); + + assertEquals("tcp", service.getTransport()); + assertEquals("localhost", service.getHost()); + assertEquals(5672, service.getPort()); + } + + public void testInstanceCreatedWithDefaultConstructorThrowsExceptionOnCallingConnectWithoutSettingURL() throws Exception + { + AMQConnectionFactory factory = new AMQConnectionFactory(); + + try + { + factory.createConnection(); + fail("Expected exception not thrown"); + } + catch(JMSException e) + { + assertEquals("Unexpected exception", AMQConnectionFactory.NO_URL_CONFIGURED, e.getMessage()); + } + } +} diff --git a/qpid/java/client/src/test/java/org/apache/qpid/test/unit/jndi/ConnectionFactoryTest.java b/qpid/java/client/src/test/java/org/apache/qpid/test/unit/jndi/ConnectionFactoryTest.java deleted file mode 100644 index 20496026ce..0000000000 --- a/qpid/java/client/src/test/java/org/apache/qpid/test/unit/jndi/ConnectionFactoryTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.apache.qpid.test.unit.jndi; - -import junit.framework.TestCase; - -import org.apache.qpid.client.AMQConnectionFactory; -import org.apache.qpid.jms.BrokerDetails; -import org.apache.qpid.jms.ConnectionURL; - -public class ConnectionFactoryTest extends TestCase -{ - - //URL will be returned with the password field swapped for '********' - // so ensure that these two strings are kept in sync. - public static final String URL = "amqp://guest:guest@clientID/test?brokerlist='tcp://localhost:5672'"; - public static final String URL_STAR_PWD = "amqp://guest:********@clientID/test?brokerlist='tcp://localhost:5672'"; - - public void testConnectionURLStringMasksPassword() throws Exception - { - AMQConnectionFactory factory = new AMQConnectionFactory(URL); - - //URL will be returned with the password field swapped for '********' - assertEquals("Connection URL not correctly set", URL_STAR_PWD, factory.getConnectionURLString()); - - // Further test that the processed ConnectionURL is as expected after - // the set call - ConnectionURL connectionurl = factory.getConnectionURL(); - - assertNull("Failover is set.", connectionurl.getFailoverMethod()); - assertEquals("guest", connectionurl.getUsername()); - assertEquals("guest", connectionurl.getPassword()); - assertEquals("clientID", connectionurl.getClientName()); - assertEquals("/test", connectionurl.getVirtualHost()); - - assertEquals(1, connectionurl.getBrokerCount()); - - BrokerDetails service = connectionurl.getBrokerDetails(0); - - assertEquals("tcp", service.getTransport()); - assertEquals("localhost", service.getHost()); - assertEquals(5672, service.getPort()); - - } -} diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/test/unit/client/connection/ConnectionFactoryTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/test/unit/client/connection/ConnectionFactoryTest.java index a313475b11..bf1fbbf1a3 100644 --- a/qpid/java/systests/src/main/java/org/apache/qpid/test/unit/client/connection/ConnectionFactoryTest.java +++ b/qpid/java/systests/src/main/java/org/apache/qpid/test/unit/client/connection/ConnectionFactoryTest.java @@ -20,6 +20,8 @@ */ package org.apache.qpid.test.unit.client.connection; +import javax.jms.Connection; + import org.apache.qpid.client.AMQConnection; import org.apache.qpid.client.AMQConnectionFactory; import org.apache.qpid.test.utils.QpidBrokerTestCase; @@ -57,5 +59,20 @@ public class ConnectionFactoryTest extends QpidBrokerTestCase assertEquals("Usernames used is different from the one in URL","guest",con3.getConnectionURL().getUsername()); assertEquals("Password used is different from the one in URL","guest",con3.getConnectionURL().getPassword()); } - + + /** + * Verifies that a connection can be made using an instance of AMQConnectionFactory created with the + * default constructor and provided with the connection url via setter. + */ + public void testCreatingConnectionWithInstanceMadeUsingDefaultConstructor() throws Exception + { + String broker = getBroker().toString(); + String url = "amqp://guest:guest@clientID/test?brokerlist='" + broker + "'"; + + AMQConnectionFactory factory = new AMQConnectionFactory(); + factory.setConnectionURLString(url); + + Connection con = factory.createConnection(); + con.close(); + } } -- cgit v1.2.1