diff options
Diffstat (limited to 'java/client/src/test/java/org/apache/qpid/client')
6 files changed, 514 insertions, 13 deletions
diff --git a/java/client/src/test/java/org/apache/qpid/client/AMQConnectionUnitTest.java b/java/client/src/test/java/org/apache/qpid/client/AMQConnectionUnitTest.java index d186a440da..d309251b44 100644 --- a/java/client/src/test/java/org/apache/qpid/client/AMQConnectionUnitTest.java +++ b/java/client/src/test/java/org/apache/qpid/client/AMQConnectionUnitTest.java @@ -20,25 +20,60 @@ */ package org.apache.qpid.client; -import junit.framework.TestCase; - -import org.apache.qpid.AMQInvalidArgumentException; +import java.util.concurrent.atomic.AtomicReference; import javax.jms.ExceptionListener; import javax.jms.JMSException; -import java.util.concurrent.atomic.AtomicReference; -public class AMQConnectionUnitTest extends TestCase +import org.apache.qpid.AMQInvalidArgumentException; +import org.apache.qpid.configuration.ClientProperties; +import org.apache.qpid.jms.ConnectionURL; +import org.apache.qpid.test.utils.QpidTestCase; + +public class AMQConnectionUnitTest extends QpidTestCase { + String _url = "amqp://guest:guest@/test?brokerlist='tcp://localhost:5672'"; + + public void testVerifyQueueOnSendDefault() throws Exception + { + MockAMQConnection connection = new MockAMQConnection(_url); + assertFalse(connection.validateQueueOnSend()); + } + + public void testVerifyQueueOnSendViaSystemProperty() throws Exception + { + setTestSystemProperty(ClientProperties.VERIFY_QUEUE_ON_SEND, "true"); + MockAMQConnection connection = new MockAMQConnection(_url); + assertTrue(connection.validateQueueOnSend()); + + setTestSystemProperty(ClientProperties.VERIFY_QUEUE_ON_SEND, "false"); + connection = new MockAMQConnection(_url); + assertFalse(connection.validateQueueOnSend()); + } + + public void testVerifyQueueOnSendViaURL() throws Exception + { + MockAMQConnection connection = new MockAMQConnection(_url + "&" + ConnectionURL.OPTIONS_VERIFY_QUEUE_ON_SEND + "='true'"); + assertTrue(connection.validateQueueOnSend()); + + connection = new MockAMQConnection(_url + "&" + ConnectionURL.OPTIONS_VERIFY_QUEUE_ON_SEND + "='false'"); + assertFalse(connection.validateQueueOnSend()); + } + + public void testVerifyQueueOnSendViaURLoverridesSystemProperty() throws Exception + { + setTestSystemProperty(ClientProperties.VERIFY_QUEUE_ON_SEND, "false"); + MockAMQConnection connection = new MockAMQConnection(_url + "&" + ConnectionURL.OPTIONS_VERIFY_QUEUE_ON_SEND + "='true'"); + assertTrue(connection.validateQueueOnSend()); + } public void testExceptionReceived() { - String url = "amqp://guest:guest@/test?brokerlist='tcp://localhost:5672'"; AMQInvalidArgumentException expectedException = new AMQInvalidArgumentException("Test", null); final AtomicReference<JMSException> receivedException = new AtomicReference<JMSException>(); try { - MockAMQConnection connection = new MockAMQConnection(url); + MockAMQConnection connection = new MockAMQConnection(_url); connection.setExceptionListener(new ExceptionListener() { @@ -62,4 +97,22 @@ public class AMQConnectionUnitTest extends TestCase assertEquals("JMSException linked exception is incorrect", expectedException, exception.getLinkedException()); } + /** + * This should expand to test all the defaults. + */ + public void testDefaultStreamMessageEncoding() throws Exception + { + MockAMQConnection connection = new MockAMQConnection(_url); + assertTrue("Legacy Stream message encoding should be the default",connection.isUseLegacyStreamMessageFormat()); + } + + /** + * This should expand to test all the connection properties. + */ + public void testStreamMessageEncodingProperty() throws Exception + { + MockAMQConnection connection = new MockAMQConnection(_url + "&use_legacy_stream_msg_format='false'"); + assertFalse("Stream message encoding should be amqp/list",connection.isUseLegacyStreamMessageFormat()); + } + } diff --git a/java/client/src/test/java/org/apache/qpid/client/AMQSession_0_10Test.java b/java/client/src/test/java/org/apache/qpid/client/AMQSession_0_10Test.java index 028e2d5cc3..40ed9319f1 100644 --- a/java/client/src/test/java/org/apache/qpid/client/AMQSession_0_10Test.java +++ b/java/client/src/test/java/org/apache/qpid/client/AMQSession_0_10Test.java @@ -18,6 +18,7 @@ */ package org.apache.qpid.client; +import org.apache.qpid.client.message.AMQPEncodedListMessage; import org.apache.qpid.framing.AMQShortString; import org.apache.qpid.test.utils.QpidTestCase; import org.apache.qpid.transport.*; @@ -28,6 +29,8 @@ import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.MessageProducer; +import javax.jms.StreamMessage; + import java.util.ArrayList; import java.util.List; @@ -276,7 +279,7 @@ public class AMQSession_0_10Test extends QpidTestCase { BasicMessageConsumer_0_10 consumer = session.createMessageConsumer(createDestination(), 1, 1, true, false, null, null, false, true); - session.sendConsume(consumer, new AMQShortString("test"), null, true, 1); + session.sendConsume(consumer, new AMQShortString("test"), true, 1); } catch (Exception e) { @@ -459,6 +462,13 @@ public class AMQSession_0_10Test extends QpidTestCase assertNotNull("ExchangeDeclare event was not sent", event); } + public void testCreateStreamMessage() throws Exception + { + AMQSession_0_10 session = createAMQSession_0_10(); + StreamMessage m = session.createStreamMessage(); + assertTrue("Legacy Stream message encoding should be the default" + m.getClass(),!(m instanceof AMQPEncodedListMessage)); + } + public void testGetQueueDepthWithSync() { // slow down a flush thread @@ -587,7 +597,7 @@ public class AMQSession_0_10Test extends QpidTestCase connection.setSessionFactory(new SessionFactory() { - public Session newSession(Connection conn, Binary name, long expiry) + public Session newSession(Connection conn, Binary name, long expiry, boolean isNoReplay) { return new MockSession(conn, new SessionDelegate(), name, expiry, throwException); } @@ -660,7 +670,6 @@ public class AMQSession_0_10Test extends QpidTestCase if (m instanceof ExchangeBound) { ExchangeBoundResult struc = new ExchangeBoundResult(); - struc.setQueueNotFound(true); result.setValue(struc); } else if (m instanceof ExchangeQuery) diff --git a/java/client/src/test/java/org/apache/qpid/client/BasicMessageConsumer_0_8_Test.java b/java/client/src/test/java/org/apache/qpid/client/BasicMessageConsumer_0_8_Test.java index 722cbd0752..066ece7ed1 100644 --- a/java/client/src/test/java/org/apache/qpid/client/BasicMessageConsumer_0_8_Test.java +++ b/java/client/src/test/java/org/apache/qpid/client/BasicMessageConsumer_0_8_Test.java @@ -48,7 +48,7 @@ public class BasicMessageConsumer_0_8_Test extends TestCase TestAMQSession testSession = new TestAMQSession(conn); BasicMessageConsumer_0_8 consumer = - new BasicMessageConsumer_0_8(0, conn, queue, "", false, null, testSession, null, null, 10, 5, false, Session.SESSION_TRANSACTED, false, false); + new BasicMessageConsumer_0_8(0, conn, queue, "", false, null, testSession, null, 10, 5, false, Session.SESSION_TRANSACTED, false, false); assertEquals("Reject behaviour was was not as expected", RejectBehaviour.SERVER, consumer.getRejectBehaviour()); } @@ -68,7 +68,7 @@ public class BasicMessageConsumer_0_8_Test extends TestCase final TestAMQSession testSession = new TestAMQSession(conn); final BasicMessageConsumer_0_8 consumer = - new BasicMessageConsumer_0_8(0, conn, queue, "", false, null, testSession, null, null, 10, 5, false, Session.SESSION_TRANSACTED, false, false); + new BasicMessageConsumer_0_8(0, conn, queue, "", false, null, testSession, null, 10, 5, false, Session.SESSION_TRANSACTED, false, false); assertEquals("Reject behaviour was was not as expected", RejectBehaviour.NORMAL, consumer.getRejectBehaviour()); } @@ -94,7 +94,7 @@ public class BasicMessageConsumer_0_8_Test extends TestCase TestAMQSession testSession = new TestAMQSession(conn); BasicMessageConsumer_0_8 consumer = - new BasicMessageConsumer_0_8(0, conn, queue, "", false, null, testSession, null, null, 10, 5, false, Session.SESSION_TRANSACTED, false, false); + new BasicMessageConsumer_0_8(0, conn, queue, "", false, null, testSession, null, 10, 5, false, Session.SESSION_TRANSACTED, false, false); assertEquals("Reject behaviour was was not as expected", RejectBehaviour.NORMAL, consumer.getRejectBehaviour()); } diff --git a/java/client/src/test/java/org/apache/qpid/client/message/AMQPEncodedListMessageUnitTest.java b/java/client/src/test/java/org/apache/qpid/client/message/AMQPEncodedListMessageUnitTest.java new file mode 100644 index 0000000000..e131ab3dd2 --- /dev/null +++ b/java/client/src/test/java/org/apache/qpid/client/message/AMQPEncodedListMessageUnitTest.java @@ -0,0 +1,153 @@ +/* + * + * 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.message; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import javax.jms.MessageFormatException; + +import org.apache.qpid.test.utils.QpidTestCase; +import org.apache.qpid.transport.codec.BBEncoder; + +public class AMQPEncodedListMessageUnitTest extends QpidTestCase +{ + + Map<String,String> _map = new HashMap<String,String>(); + List<Object> _list = new ArrayList<Object>(); + UUID _uuid = UUID.randomUUID(); + + @Override + public void setUp() throws Exception + { + super.setUp(); + _map.put("Key1","String1"); + _map.put("Key2","String2"); + _map.put("Key3","String3"); + + _list.add(1); + _list.add(2); + _list.add(3); + } + + /** + * Test whether we accept the correct types while rejecting invalid types. + */ + public void testAddObject() throws Exception + { + AMQPEncodedListMessage m = new AMQPEncodedListMessage(AMQMessageDelegateFactory.FACTORY_0_10); + m.add(true); + m.add((byte)256); + m.add(Short.MAX_VALUE); + m.add(Integer.MAX_VALUE); + m.add(Long.MAX_VALUE); + m.add(10.22); + m.add("Msg"); + m.add("Msg".getBytes()); + m.add(_list); + m.add(_map); + m.add(_uuid); + + try + { + m.add(new Object()); + fail("Validation for element type failed"); + } + catch (MessageFormatException e) + { + } + } + + public void testListBehaviorForIncommingMsg() throws Exception + { + BBEncoder encoder = new BBEncoder(1024); + encoder.writeList(_list); + AMQPEncodedListMessage m = new AMQPEncodedListMessage(new AMQMessageDelegate_0_10(),encoder.segment()); + + assertTrue("contains(Object) method did not return true as expected",m.contains(1)); + assertFalse("contains(Object) method did not return false as expected",m.contains(5)); + assertEquals("get(index) method returned incorrect value",((Integer)m.get(1)).intValue(),2); + assertEquals("indexOf(Object) method returned incorrect index",m.indexOf(2),1); + try + { + m.get(10); + } + catch (MessageFormatException e) + { + assertTrue("Incorrect exception type. Expected IndexOutOfBoundsException", e.getCause() instanceof IndexOutOfBoundsException); + } + } + + public void testStreamMessageInterfaceForIncommingMsg() throws Exception + { + BBEncoder encoder = new BBEncoder(1024); + encoder.writeList(getList()); + AMQPEncodedListMessage m = new AMQPEncodedListMessage(new AMQMessageDelegate_0_10(),encoder.segment()); + + assertEquals(true,m.readBoolean()); + assertEquals((byte)256,m.readByte()); + assertEquals(Short.MAX_VALUE,m.readShort()); + assertEquals(Integer.MAX_VALUE,m.readInt()); + assertEquals(Long.MAX_VALUE,m.readLong()); + assertEquals(10.22,m.readDouble()); + assertEquals("Msg",m.readString()); + assertEquals(_list,(List)m.readObject()); + assertEquals(_map,(Map)m.readObject()); + assertEquals(_uuid,(UUID)m.readObject()); + } + + public void testMapMessageInterfaceForIncommingMsg() throws Exception + { + BBEncoder encoder = new BBEncoder(1024); + encoder.writeList(getList()); + AMQPEncodedListMessage m = new AMQPEncodedListMessage(new AMQMessageDelegate_0_10(),encoder.segment()); + + assertEquals(true,m.getBoolean("0")); + assertEquals((byte)256,m.getByte("1")); + assertEquals(Short.MAX_VALUE,m.getShort("2")); + assertEquals(Integer.MAX_VALUE,m.getInt("3")); + assertEquals(Long.MAX_VALUE,m.getLong("4")); + assertEquals(10.22,m.getDouble("5")); + assertEquals("Msg",m.getString("6")); + assertEquals(_list,(List)m.getObject("7")); + assertEquals(_map,(Map)m.getObject("8")); + assertEquals(_uuid,(UUID)m.getObject("9")); + } + + public List<Object> getList() + { + List<Object> myList = new ArrayList<Object>(); + myList.add(true); + myList.add((byte)256); + myList.add(Short.MAX_VALUE); + myList.add(Integer.MAX_VALUE); + myList.add(Long.MAX_VALUE); + myList.add(10.22); + myList.add("Msg"); + myList.add(_list); + myList.add(_map); + myList.add(_uuid); + return myList; + } +} diff --git a/java/client/src/test/java/org/apache/qpid/client/messaging/address/AddressHelperTest.java b/java/client/src/test/java/org/apache/qpid/client/messaging/address/AddressHelperTest.java new file mode 100644 index 0000000000..7401168978 --- /dev/null +++ b/java/client/src/test/java/org/apache/qpid/client/messaging/address/AddressHelperTest.java @@ -0,0 +1,146 @@ +/* + * + * 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.messaging.address; + +import org.apache.qpid.client.AMQDestination; +import org.apache.qpid.client.AMQDestination.AddressOption; +import org.apache.qpid.client.AMQDestination.Binding; +import org.apache.qpid.client.messaging.address.Link.Reliability; +import org.apache.qpid.messaging.Address; +import org.apache.qpid.test.utils.QpidTestCase; + +public class AddressHelperTest extends QpidTestCase +{ + public void testAddressOptions() throws Exception + { + Address addr = Address.parse("queue/test;{create:sender, assert:always, delete:receiver, mode:browse}"); + AddressHelper helper = new AddressHelper(addr); + assertEquals(AddressOption.SENDER,AddressOption.getOption(helper.getCreate())); + assertEquals(AddressOption.ALWAYS,AddressOption.getOption(helper.getAssert())); + assertEquals(AddressOption.RECEIVER,AddressOption.getOption(helper.getDelete())); + assertTrue("'mode' option wasn't read properly",helper.isBrowseOnly()); + } + + public void testNodeProperties() throws Exception + { + Address addr = Address.parse("my-queue;{" + + "node: " + + "{" + + "type: queue ," + + "durable: true ," + + "x-declare: " + + "{" + + "exclusive: true," + + "auto-delete: true," + + "alternate-exchange: 'amq.fanout'," + + "arguments: {" + + "'qpid.max_size': 1000," + + "'qpid.max_count': 100" + + "}" + + "}, " + + "x-bindings: [{exchange : 'amq.direct', queue:my-queue, key : test}, " + + "{exchange : 'amq.fanout', queue:my-queue}," + + "{exchange: 'amq.match', queue:my-queue, arguments: {x-match: any, dep: sales, loc: CA}}," + + "{exchange : 'amq.topic',queue:my-queue, key : 'a.#'}" + + "]" + + + "}" + + "}"); + AddressHelper helper = new AddressHelper(addr); + Node node = helper.getNode(); + assertEquals("'type' property wasn't read properly",AMQDestination.QUEUE_TYPE,helper.getNodeType()); + assertTrue("'durable' property wasn't read properly",node.isDurable()); + assertTrue("'auto-delete' property wasn't read properly",node.isAutoDelete()); + assertTrue("'exclusive' property wasn't read properly",node.isExclusive()); + assertEquals("'alternate-exchange' property wasn't read properly","amq.fanout",node.getAlternateExchange()); + assertEquals("'arguments' in 'x-declare' property wasn't read properly",2,node.getDeclareArgs().size()); + assertEquals("'bindings' property wasn't read properly",4,node.getBindings().size()); + for (Binding binding: node.getBindings()) + { + assertTrue("property 'exchange' in bindings wasn't read properly",binding.getExchange().startsWith("amq.")); + assertEquals("property 'queue' in bindings wasn't read properly","my-queue",binding.getQueue()); + if (binding.getExchange().equals("amq.direct")) + { + assertEquals("'key' property in bindings wasn't read properly","test",binding.getBindingKey()); + } + if (binding.getExchange().equals("amq.match")) + { + assertEquals("'arguments' property in bindings wasn't read properly",3,binding.getArgs().size()); + } + } + } + + public void testLinkProperties() throws Exception + { + Address addr = Address.parse("my-queue;{" + + "link: " + + "{" + + "name: my-queue ," + + "durable: true ," + + "reliability: at-least-once," + + "capacity: {source:10, target:15}," + + "x-declare: " + + "{" + + "exclusive: true," + + "auto-delete: true," + + "alternate-exchange: 'amq.fanout'," + + "arguments: {" + + "'qpid.max_size': 1000," + + "'qpid.max_count': 100" + + "}" + + "}, " + + "x-bindings: [{exchange : 'amq.direct', queue:my-queue, key : test}, " + + "{exchange : 'amq.fanout', queue:my-queue}," + + "{exchange: 'amq.match', queue:my-queue, arguments: {x-match: any, dep: sales, loc: CA}}," + + "{exchange : 'amq.topic',queue:my-queue, key : 'a.#'}" + + "]," + + "x-subscribes:{exclusive: true, arguments: {a:b,x:y}}" + + "}" + + "}"); + + AddressHelper helper = new AddressHelper(addr); + Link link = helper.getLink(); + assertEquals("'name' property wasn't read properly","my-queue",link.getName()); + assertTrue("'durable' property wasn't read properly",link.isDurable()); + assertEquals("'reliability' property wasn't read properly",Reliability.AT_LEAST_ONCE,link.getReliability()); + assertTrue("'auto-delete' property in 'x-declare' wasn't read properly",link.getSubscriptionQueue().isAutoDelete()); + assertTrue("'exclusive' property in 'x-declare' wasn't read properly",link.getSubscriptionQueue().isExclusive()); + assertEquals("'alternate-exchange' property in 'x-declare' wasn't read properly","amq.fanout",link.getSubscriptionQueue().getAlternateExchange()); + assertEquals("'arguments' in 'x-declare' property wasn't read properly",2,link.getSubscriptionQueue().getDeclareArgs().size()); + assertEquals("'bindings' property wasn't read properly",4,link.getBindings().size()); + for (Binding binding: link.getBindings()) + { + assertTrue("property 'exchange' in bindings wasn't read properly",binding.getExchange().startsWith("amq.")); + assertEquals("property 'queue' in bindings wasn't read properly","my-queue",binding.getQueue()); + if (binding.getExchange().equals("amq.direct")) + { + assertEquals("'key' property in bindings wasn't read properly","test",binding.getBindingKey()); + } + if (binding.getExchange().equals("amq.match")) + { + assertEquals("'arguments' property in bindings wasn't read properly",3,binding.getArgs().size()); + } + } + assertTrue("'exclusive' property in 'x-subscribe' wasn't read properly",link.getSubscription().isExclusive()); + assertEquals("'arguments' in 'x-subscribe' property wasn't read properly",2,link.getSubscription().getArgs().size()); + } + +} diff --git a/java/client/src/test/java/org/apache/qpid/client/security/DynamicSaslRegistrarTest.java b/java/client/src/test/java/org/apache/qpid/client/security/DynamicSaslRegistrarTest.java new file mode 100644 index 0000000000..4281984212 --- /dev/null +++ b/java/client/src/test/java/org/apache/qpid/client/security/DynamicSaslRegistrarTest.java @@ -0,0 +1,140 @@ +/* + * + * 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.security; + +import java.io.File; +import java.security.Provider; +import java.security.Security; + +import org.apache.qpid.client.security.DynamicSaslRegistrar.ProviderRegistrationResult; +import org.apache.qpid.test.utils.QpidTestCase; +import org.apache.qpid.test.utils.TestFileUtils; + +public class DynamicSaslRegistrarTest extends QpidTestCase +{ + private Provider _registeredProvider; + + public void setUp() throws Exception + { + super.setUp(); + + //If the client provider is already registered, remove it for the duration of the test + _registeredProvider = DynamicSaslRegistrar.findProvider(JCAProvider.QPID_CLIENT_SASL_PROVIDER_NAME); + if (_registeredProvider != null) + { + Security.removeProvider(JCAProvider.QPID_CLIENT_SASL_PROVIDER_NAME); + } + } + + public void tearDown() throws Exception + { + //Remove any provider left behind by the test. + Security.removeProvider(JCAProvider.QPID_CLIENT_SASL_PROVIDER_NAME); + try + { + //If the client provider was already registered before the test, restore it. + if (_registeredProvider != null) + { + Security.insertProviderAt(_registeredProvider, 1); + } + } + finally + { + super.tearDown(); + } + } + + public void testRegisterDefaultProvider() + { + assertNull("Provider should not yet be registered", DynamicSaslRegistrar.findProvider(JCAProvider.QPID_CLIENT_SASL_PROVIDER_NAME)); + + ProviderRegistrationResult firstRegistrationResult = DynamicSaslRegistrar.registerSaslProviders(); + assertEquals("Unexpected registration result", ProviderRegistrationResult.SUCCEEDED, firstRegistrationResult); + assertNotNull("Providers should now be registered", DynamicSaslRegistrar.findProvider(JCAProvider.QPID_CLIENT_SASL_PROVIDER_NAME)); + } + + public void testRegisterDefaultProviderTwice() + { + assertNull("Provider should not yet be registered", DynamicSaslRegistrar.findProvider(JCAProvider.QPID_CLIENT_SASL_PROVIDER_NAME)); + + DynamicSaslRegistrar.registerSaslProviders(); + assertNotNull("Providers should now be registered", DynamicSaslRegistrar.findProvider(JCAProvider.QPID_CLIENT_SASL_PROVIDER_NAME)); + + ProviderRegistrationResult result = DynamicSaslRegistrar.registerSaslProviders(); + assertEquals("Unexpected registration result when trying to re-register", ProviderRegistrationResult.EQUAL_ALREADY_REGISTERED, result); + assertNotNull("Providers should still be registered", DynamicSaslRegistrar.findProvider(JCAProvider.QPID_CLIENT_SASL_PROVIDER_NAME)); + } + + @SuppressWarnings("serial") + public void testRegisterDefaultProviderWhenAnotherIsAlreadyPresentWithDifferentFactories() + { + assertNull("Provider should not be registered", DynamicSaslRegistrar.findProvider(JCAProvider.QPID_CLIENT_SASL_PROVIDER_NAME)); + + //Add a test provider with the same name, version, info as the default client provider, but with different factory properties (none). + Provider testProvider = new Provider(JCAProvider.QPID_CLIENT_SASL_PROVIDER_NAME, + JCAProvider.QPID_CLIENT_SASL_PROVIDER_VERSION, + JCAProvider.QPID_CLIENT_SASL_PROVIDER_INFO){}; + Security.addProvider(testProvider); + assertSame("Test provider should be registered", testProvider, DynamicSaslRegistrar.findProvider(JCAProvider.QPID_CLIENT_SASL_PROVIDER_NAME)); + + //Try to register the default provider now that another with the same name etc (but different factories) + //is already registered, expect it not to be registered as a result. + ProviderRegistrationResult result = DynamicSaslRegistrar.registerSaslProviders(); + assertEquals("Unexpected registration result", ProviderRegistrationResult.DIFFERENT_ALREADY_REGISTERED, result); + + //Verify the test provider is still registered + assertSame("Test provider should still be registered", testProvider, DynamicSaslRegistrar.findProvider(JCAProvider.QPID_CLIENT_SASL_PROVIDER_NAME)); + } + + public void testRegisterWithNoFactories() + { + File emptyTempFile = TestFileUtils.createTempFile(this); + + assertNull("Provider should not be registered", DynamicSaslRegistrar.findProvider(JCAProvider.QPID_CLIENT_SASL_PROVIDER_NAME)); + + //Adjust the location of the properties file to point at an empty file, so no factories are found to register. + setTestSystemProperty("amq.dynamicsaslregistrar.properties", emptyTempFile.getPath()); + + //Try to register the default provider, expect it it not to be registered because there were no factories. + ProviderRegistrationResult result = DynamicSaslRegistrar.registerSaslProviders(); + assertEquals("Unexpected registration result", ProviderRegistrationResult.NO_SASL_FACTORIES, result); + + assertNull("Provider should not be registered", DynamicSaslRegistrar.findProvider(JCAProvider.QPID_CLIENT_SASL_PROVIDER_NAME)); + } + + public void testRegisterWithMissingFileGetsDefault() + { + //Create a temp file and then delete it, such that we get a path which doesn't exist + File tempFile = TestFileUtils.createTempFile(this); + assertTrue("Failed to delete file", tempFile.delete()); + + assertNull("Provider should not be registered", DynamicSaslRegistrar.findProvider(JCAProvider.QPID_CLIENT_SASL_PROVIDER_NAME)); + + //Adjust the location of the properties file to point at non-existent file. + setTestSystemProperty("amq.dynamicsaslregistrar.properties", tempFile.getPath()); + + //Try to register the default provider, expect it to fall back to the default in the jar and succeed. + ProviderRegistrationResult result = DynamicSaslRegistrar.registerSaslProviders(); + assertEquals("Unexpected registration result", ProviderRegistrationResult.SUCCEEDED, result); + + assertNotNull("Provider should be registered", DynamicSaslRegistrar.findProvider(JCAProvider.QPID_CLIENT_SASL_PROVIDER_NAME)); + } +} |