diff options
author | Robert Gemmell <robbie@apache.org> | 2012-05-13 13:16:03 +0000 |
---|---|---|
committer | Robert Gemmell <robbie@apache.org> | 2012-05-13 13:16:03 +0000 |
commit | b336318f2650b7191512c5a45bba31c7c1216dc7 (patch) | |
tree | 9c0345768e03294164d4093ab109477b0808fdf8 | |
parent | bac0ec284c39cef7f55562a97e9f982f11e8a7fd (diff) | |
download | qpid-python-b336318f2650b7191512c5a45bba31c7c1216dc7.tar.gz |
QPID-3971: resolve CI failure (hopefully), make misc test improvements
- Use File.createTempFile to target the tmp dir, hopefully resolving the CI failure, ensure it is deleted even if the test subsequently fails.
- Use QpidTestCase system property handling, ensuring modified properties are unset/restored to prior values following the test.
- Move the properties used in the test internally as opposed to loading from another file.
- Verify the values returned from the loaded Context are as expected an not only non-null.
- Remove unused imports, fields.
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1337871 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | qpid/java/client/src/test/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactoryTest.java | 100 | ||||
-rw-r--r-- | qpid/java/client/src/test/java/org/apache/qpid/jndi/hello.properties | 27 |
2 files changed, 58 insertions, 69 deletions
diff --git a/qpid/java/client/src/test/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactoryTest.java b/qpid/java/client/src/test/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactoryTest.java index 8e9266ac61..ce9e681eaf 100644 --- a/qpid/java/client/src/test/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactoryTest.java +++ b/qpid/java/client/src/test/java/org/apache/qpid/jndi/PropertiesFileInitialContextFactoryTest.java @@ -22,76 +22,46 @@ package org.apache.qpid.jndi; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.FileWriter; +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")); - - ctx = new InitialContext(properties); - } + private static final String CONNECTION_URL = "amqp://username:password@clientid/test?brokerlist='tcp://testContextFromProviderURL:5672'"; - public void testContextFromProviderURL() throws Exception - { - Properties properties = new Properties(); - properties.load(this.getClass().getResourceAsStream("hello.properties")); - File f = new File(System.getProperty("java.io.tmpdir") + FILE_NAME); - FileOutputStream fos = new FileOutputStream(f); - properties.store(fos, null); - - System.setProperty("java.naming.factory.initial", "org.apache.qpid.jndi.PropertiesFileInitialContextFactory"); - System.setProperty("java.naming.provider.url", "file://" + f.getCanonicalPath()); - - InitialContext context = new InitialContext(); - assertNotNull("Lookup from URI based context should not be null", context.lookup("topicExchange")); - - context.close(); - - System.setProperty("java.naming.provider.url", f.getCanonicalPath()); - context = new InitialContext(); - assertNotNull("Lookup from fileName should not be null", context.lookup("qpidConnectionfactory")); - - context.close(); - f.delete(); - - } 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()) @@ -109,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/qpid/java/client/src/test/java/org/apache/qpid/jndi/hello.properties b/qpid/java/client/src/test/java/org/apache/qpid/jndi/hello.properties deleted file mode 100644 index d017d137fe..0000000000 --- a/qpid/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 |