From d43d1912b376322e27fdcda551a73f9ff5487972 Mon Sep 17 00:00:00 2001 From: Kim van der Riet Date: Fri, 3 Aug 2012 12:13:32 +0000 Subject: QPID-3858: Updated branch - merged from trunk r.1368650 git-svn-id: https://svn.apache.org/repos/asf/qpid/branches/asyncstore@1368910 13f79535-47bb-0310-9956-ffa450edef68 --- .../qpid/client/AMQConnectionFactoryTest.java | 79 ++++++++++++++++++++++ .../client/message/AbstractJMSMessageTest.java | 2 +- .../PropertiesFileInitialContextFactoryTest.java | 78 ++++++++++++++++----- .../java/org/apache/qpid/jndi/hello.properties | 27 -------- .../qpid/test/unit/jndi/ConnectionFactoryTest.java | 63 ----------------- 5 files changed, 140 insertions(+), 109 deletions(-) create mode 100644 java/client/src/test/java/org/apache/qpid/client/AMQConnectionFactoryTest.java delete mode 100644 java/client/src/test/java/org/apache/qpid/jndi/hello.properties delete mode 100644 java/client/src/test/java/org/apache/qpid/test/unit/jndi/ConnectionFactoryTest.java (limited to 'java/client/src/test') diff --git a/java/client/src/test/java/org/apache/qpid/client/AMQConnectionFactoryTest.java b/java/client/src/test/java/org/apache/qpid/client/AMQConnectionFactoryTest.java new file mode 100644 index 0000000000..bb92fa4ecd --- /dev/null +++ b/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/java/client/src/test/java/org/apache/qpid/client/message/AbstractJMSMessageTest.java b/java/client/src/test/java/org/apache/qpid/client/message/AbstractJMSMessageTest.java index 1fbd7cf212..c535fdd705 100644 --- a/java/client/src/test/java/org/apache/qpid/client/message/AbstractJMSMessageTest.java +++ b/java/client/src/test/java/org/apache/qpid/client/message/AbstractJMSMessageTest.java @@ -1,4 +1,3 @@ -package org.apache.qpid.client.message; /* * * Licensed to the Apache Software Foundation (ASF) under one @@ -19,6 +18,7 @@ package org.apache.qpid.client.message; * under the License. * */ +package org.apache.qpid.client.message; import junit.framework.TestCase; diff --git a/java/client/src/test/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactoryTest.java b/java/client/src/test/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactoryTest.java index 2989970dcd..ce9e681eaf 100644 --- a/java/client/src/test/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactoryTest.java +++ b/java/client/src/test/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactoryTest.java @@ -21,51 +21,47 @@ package org.apache.qpid.jndi; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; import java.util.Properties; +import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.Queue; import javax.jms.Topic; import javax.naming.ConfigurationException; import javax.naming.Context; import javax.naming.InitialContext; +import javax.naming.NamingException; -import junit.framework.TestCase; - +import org.apache.qpid.client.AMQConnectionFactory; import org.apache.qpid.client.AMQDestination; +import org.apache.qpid.configuration.ClientProperties; import org.apache.qpid.framing.AMQShortString; +import org.apache.qpid.test.utils.QpidTestCase; -public class PropertiesFileInitialContextFactoryTest extends TestCase +public class PropertiesFileInitialContextFactoryTest extends QpidTestCase { - private static final String FILE_URL_PATH = System.getProperty("user.dir") + "/client/src/test/java/org/apache/qpid/jndi/"; - private static final String FILE_NAME = "hello.properties"; - - private Context ctx; - - protected void setUp() throws Exception - { - Properties properties = new Properties(); - properties.load(this.getClass().getResourceAsStream("JNDITest.properties")); - - //Create the initial context - ctx = new InitialContext(properties); - } - + private static final String CONNECTION_URL = "amqp://username:password@clientid/test?brokerlist='tcp://testContextFromProviderURL:5672'"; public void testQueueNamesWithTrailingSpaces() throws Exception { + Context ctx = prepareContext(); Queue queue = (Queue)ctx.lookup("QueueNameWithSpace"); assertEquals("QueueNameWithSpace",queue.getQueueName()); } public void testTopicNamesWithTrailingSpaces() throws Exception { + Context ctx = prepareContext(); Topic topic = (Topic)ctx.lookup("TopicNameWithSpace"); assertEquals("TopicNameWithSpace",topic.getTopicName()); } public void testMultipleTopicNamesWithTrailingSpaces() throws Exception { + Context ctx = prepareContext(); Topic topic = (Topic)ctx.lookup("MultipleTopicNamesWithSpace"); int i = 0; for (AMQShortString bindingKey: ((AMQDestination)topic).getBindingKeys()) @@ -83,13 +79,59 @@ public class PropertiesFileInitialContextFactoryTest extends TestCase try { - ctx = new InitialContext(properties); + new InitialContext(properties); fail("A configuration exception should be thrown with details about the address syntax error"); } catch(ConfigurationException e) { assertTrue("Incorrect exception", e.getMessage().contains("Failed to parse entry: amq.topic/test;create:always}")); } + } + + private InitialContext prepareContext() throws IOException, NamingException + { + Properties properties = new Properties(); + properties.load(this.getClass().getResourceAsStream("JNDITest.properties")); + return new InitialContext(properties); + } + + /** + * Test loading of a JNDI properties file through use of a file:// URL + * supplied via the InitialContext.PROVIDER_URL system property. + */ + public void testContextFromProviderURL() throws Exception + { + Properties properties = new Properties(); + properties.put("connectionfactory.qpidConnectionfactory", CONNECTION_URL); + properties.put("destination.topicExchange", "destName"); + + File f = File.createTempFile(getTestName(), ".properties"); + try + { + FileOutputStream fos = new FileOutputStream(f); + properties.store(fos, null); + fos.close(); + + setTestSystemProperty(ClientProperties.DEST_SYNTAX, "ADDR"); + setTestSystemProperty(InitialContext.INITIAL_CONTEXT_FACTORY, "org.apache.qpid.jndi.PropertiesFileInitialContextFactory"); + setTestSystemProperty(InitialContext.PROVIDER_URL, "file://" + f.getCanonicalPath()); + + InitialContext context = new InitialContext(); + Destination dest = (Destination) context.lookup("topicExchange"); + assertNotNull("Lookup from URI based context should not be null", dest); + assertTrue("Unexpected value from lookup", dest.toString().contains("destName")); + + ConnectionFactory factory = (ConnectionFactory) context.lookup("qpidConnectionfactory"); + assertTrue("ConnectionFactory was not an instance of AMQConnectionFactory", factory instanceof AMQConnectionFactory); + assertEquals("Unexpected ConnectionURL value", CONNECTION_URL.replaceAll("password", "********"), + ((AMQConnectionFactory)factory).getConnectionURLString()); + + context.close(); + } + finally + { + f.delete(); + } } } diff --git a/java/client/src/test/java/org/apache/qpid/jndi/hello.properties b/java/client/src/test/java/org/apache/qpid/jndi/hello.properties deleted file mode 100644 index d017d137fe..0000000000 --- a/java/client/src/test/java/org/apache/qpid/jndi/hello.properties +++ /dev/null @@ -1,27 +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. -# -java.naming.factory.initial = org.apache.qpid.jndi.PropertiesFileInitialContextFactory - -# register some connection factories -# connectionfactory.[jndiname] = [ConnectionURL] -connectionfactory.qpidConnectionfactory = amqp://guest:guest@clientid/test?brokerlist='tcp://10.0.1.46:5672' - -# Register an AMQP destination in JNDI -# destination.[jniName] = [Address Format] -destination.topicExchange = amq.topic diff --git a/java/client/src/test/java/org/apache/qpid/test/unit/jndi/ConnectionFactoryTest.java b/java/client/src/test/java/org/apache/qpid/test/unit/jndi/ConnectionFactoryTest.java deleted file mode 100644 index 20496026ce..0000000000 --- a/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()); - - } -} -- cgit v1.2.1