diff options
author | Rajith Muditha Attapattu <rajith@apache.org> | 2011-05-27 15:44:23 +0000 |
---|---|---|
committer | Rajith Muditha Attapattu <rajith@apache.org> | 2011-05-27 15:44:23 +0000 |
commit | 66765100f4257159622cefe57bed50125a5ad017 (patch) | |
tree | a88ee23bb194eb91f0ebb2d9b23ff423e3ea8e37 /qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost | |
parent | 1aeaa7b16e5ce54f10c901d75c4d40f9f88b9db6 (diff) | |
parent | 88b98b2f4152ef59a671fad55a0d08338b6b78ca (diff) | |
download | qpid-python-66765100f4257159622cefe57bed50125a5ad017.tar.gz |
Creating a branch for experimenting with some ideas for JMS client.rajith_jms_client
git-svn-id: https://svn.apache.org/repos/asf/qpid/branches/rajith_jms_client@1128369 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost')
5 files changed, 1016 insertions, 0 deletions
diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/plugins/SlowConsumerDetectionConfigurationTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/plugins/SlowConsumerDetectionConfigurationTest.java new file mode 100644 index 0000000000..cc11d68e07 --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/plugins/SlowConsumerDetectionConfigurationTest.java @@ -0,0 +1,346 @@ +/* + * + * 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.server.virtualhost.plugins; + +import org.apache.commons.configuration.CompositeConfiguration; +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.XMLConfiguration; +import org.apache.qpid.server.configuration.plugins.SlowConsumerDetectionConfiguration; +import org.apache.qpid.server.util.InternalBrokerBaseCase; + +import java.util.concurrent.TimeUnit; + +/** + * Provide Unit Test coverage of the virtualhost SlowConsumer Configuration + * This is what controls how often the plugin will execute + */ +public class SlowConsumerDetectionConfigurationTest extends InternalBrokerBaseCase +{ + + /** + * Default Testing: + * + * Provide a fully complete and valid configuration specifying 'delay' and + * 'timeunit' and ensure that it is correctly processed. + * + * Ensure no exceptions are thrown and that we get the same values back that + * were put into the configuration. + */ + public void testConfigLoadingValidConfig() + { + SlowConsumerDetectionConfiguration config = new SlowConsumerDetectionConfiguration(); + + XMLConfiguration xmlconfig = new XMLConfiguration(); + + long DELAY=10; + String TIMEUNIT=TimeUnit.MICROSECONDS.toString(); + xmlconfig.addProperty("delay", String.valueOf(DELAY)); + xmlconfig.addProperty("timeunit", TIMEUNIT); + + // Create a CompositeConfiguration as this is what the broker uses + CompositeConfiguration composite = new CompositeConfiguration(); + composite.addConfiguration(xmlconfig); + + try + { + config.setConfiguration("", composite); + } + catch (ConfigurationException e) + { + e.printStackTrace(); + fail(e.getMessage()); + } + + assertEquals("Delay not correctly returned.", DELAY, config.getDelay()); + assertEquals("TimeUnit not correctly returned.", + TIMEUNIT, String.valueOf(config.getTimeUnit())); + } + + /** + * Default Testing: + * + * Test Missing TimeUnit value gets default. + * + * The TimeUnit value is optional and default to SECONDS. + * + * Test that if we do not specify a TimeUnit then we correctly get seconds. + * + * Also verify that relying on the default does not impact the setting of + * the 'delay' value. + * + */ + public void testConfigLoadingMissingTimeUnitDefaults() + { + SlowConsumerDetectionConfiguration config = new SlowConsumerDetectionConfiguration(); + + XMLConfiguration xmlconfig = new XMLConfiguration(); + + long DELAY=10; + xmlconfig.addProperty("delay", String.valueOf(DELAY)); + + // Create a CompositeConfiguration as this is what the broker uses + CompositeConfiguration composite = new CompositeConfiguration(); + composite.addConfiguration(xmlconfig); + try + { + config.setConfiguration("", composite); + } + catch (ConfigurationException e) + { + e.printStackTrace(); + fail(e.getMessage()); + } + + assertEquals("Delay not correctly returned.", DELAY, config.getDelay()); + assertEquals("Default TimeUnit incorrect", TimeUnit.SECONDS, config.getTimeUnit()); + } + + /** + * Input Testing: + * + * TimeUnit parsing requires the String value be in UpperCase. + * Ensure we can handle when the user doesn't know this. + * + * Same test as 'testConfigLoadingValidConfig' but checking that + * the timeunit field is not case sensitive. + * i.e. the toUpper is being correctly applied. + */ + public void testConfigLoadingValidConfigStrangeTimeUnit() + { + SlowConsumerDetectionConfiguration config = new SlowConsumerDetectionConfiguration(); + + XMLConfiguration xmlconfig = new XMLConfiguration(); + + long DELAY=10; + + xmlconfig.addProperty("delay", DELAY); + xmlconfig.addProperty("timeunit", "MiCrOsEcOnDs"); + + // Create a CompositeConfiguration as this is what the broker uses + CompositeConfiguration composite = new CompositeConfiguration(); + composite.addConfiguration(xmlconfig); + + try + { + config.setConfiguration("", composite); + } + catch (ConfigurationException e) + { + e.printStackTrace(); + fail(e.getMessage()); + } + + assertEquals("Delay not correctly returned.", DELAY, config.getDelay()); + assertEquals("TimeUnit not correctly returned.", + TimeUnit.MICROSECONDS.toString(), String.valueOf(config.getTimeUnit())); + + } + + /** + * Failure Testing: + * + * Test that delay must be long not a string value. + * Provide a delay as a written value not a long. 'ten'. + * + * This should throw a configuration exception which is being trapped and + * verified to be the right exception, a NumberFormatException. + * + */ + public void testConfigLoadingInValidDelayString() + { + SlowConsumerDetectionConfiguration config = new SlowConsumerDetectionConfiguration(); + + XMLConfiguration xmlconfig = new XMLConfiguration(); + + xmlconfig.addProperty("delay", "ten"); + xmlconfig.addProperty("timeunit", TimeUnit.MICROSECONDS.toString()); + + // Create a CompositeConfiguration as this is what the broker uses + CompositeConfiguration composite = new CompositeConfiguration(); + composite.addConfiguration(xmlconfig); + + try + { + config.setConfiguration("", composite); + fail("Configuration should fail to validate"); + } + catch (ConfigurationException e) + { + Throwable cause = e.getCause(); + + assertEquals("Cause not correct", NumberFormatException.class, cause.getClass()); + } + } + + /** + * Failure Testing: + * + * Test that negative delays are invalid. + * + * Delay must be a positive value as negative delay means doesn't make sense. + * + * Configuration exception with a useful message should be thrown here. + * + */ + public void testConfigLoadingInValidDelayNegative() + { + SlowConsumerDetectionConfiguration config = new SlowConsumerDetectionConfiguration(); + + XMLConfiguration xmlconfig = new XMLConfiguration(); + + xmlconfig.addProperty("delay", "-10"); + xmlconfig.addProperty("timeunit", TimeUnit.MICROSECONDS.toString()); + + // Create a CompositeConfiguration as this is what the broker uses + CompositeConfiguration composite = new CompositeConfiguration(); + composite.addConfiguration(xmlconfig); + + try + { + config.setConfiguration("", composite); + fail("Configuration should fail to validate"); + } + catch (ConfigurationException e) + { + Throwable cause = e.getCause(); + + assertNotNull("Configuration Exception must not be null.", cause); + assertEquals("Cause not correct", + ConfigurationException.class, cause.getClass()); + assertEquals("Incorrect message.", + "SlowConsumerDetectionConfiguration: 'delay' must be a Positive Long value.", + cause.getMessage()); + } + } + + /** + * Failure Testing: + * + * Test that delay cannot be 0. + * + * A zero delay means run constantly. This is not how VirtualHostTasks + * are designed to be run so we dis-allow the use of 0 delay. + * + * Same test as 'testConfigLoadingInValidDelayNegative' but with a 0 value. + * + */ + public void testConfigLoadingInValidDelayZero() + { + SlowConsumerDetectionConfiguration config = new SlowConsumerDetectionConfiguration(); + + XMLConfiguration xmlconfig = new XMLConfiguration(); + + xmlconfig.addProperty("delay", "0"); + xmlconfig.addProperty("timeunit", TimeUnit.MICROSECONDS.toString()); + + // Create a CompositeConfiguration as this is what the broker uses + CompositeConfiguration composite = new CompositeConfiguration(); + composite.addConfiguration(xmlconfig); + + try + { + config.setConfiguration("", composite); + fail("Configuration should fail to validate"); + } + catch (ConfigurationException e) + { + Throwable cause = e.getCause(); + + assertNotNull("Configuration Exception must not be null.", cause); + assertEquals("Cause not correct", + ConfigurationException.class, cause.getClass()); + assertEquals("Incorrect message.", + "SlowConsumerDetectionConfiguration: 'delay' must be a Positive Long value.", + cause.getMessage()); + } + } + + /** + * Failure Testing: + * + * Test that missing delay fails. + * If we have no delay then we do not pick a default. So a Configuration + * Exception is thrown. + * + * */ + public void testConfigLoadingInValidMissingDelay() + { + SlowConsumerDetectionConfiguration config = new SlowConsumerDetectionConfiguration(); + + XMLConfiguration xmlconfig = new XMLConfiguration(); + + xmlconfig.addProperty("timeunit", TimeUnit.SECONDS.toString()); + + // Create a CompositeConfiguration as this is what the broker uses + CompositeConfiguration composite = new CompositeConfiguration(); + composite.addConfiguration(xmlconfig); + try + { + config.setConfiguration("", composite); + fail("Configuration should fail to validate"); + } + catch (ConfigurationException e) + { + assertEquals("Incorrect message.", "SlowConsumerDetectionConfiguration: unable to configure invalid delay:null", e.getMessage()); + } + } + + /** + * Failure Testing: + * + * Test that erroneous TimeUnit fails. + * + * Valid TimeUnit values vary based on the JVM version i.e. 1.6 added HOURS/DAYS etc. + * + * We don't test the values for TimeUnit are accepted other than MILLISECONDS in the + * positive testing at the start. + * + * Here we ensure that an erroneous for TimeUnit correctly throws an exception. + * + * We test with 'foo', which will never be a TimeUnit + * + */ + public void testConfigLoadingInValidTimeUnit() + { + SlowConsumerDetectionConfiguration config = new SlowConsumerDetectionConfiguration(); + + String TIMEUNIT = "foo"; + XMLConfiguration xmlconfig = new XMLConfiguration(); + + xmlconfig.addProperty("delay", "10"); + xmlconfig.addProperty("timeunit", TIMEUNIT); + + // Create a CompositeConfiguration as this is what the broker uses + CompositeConfiguration composite = new CompositeConfiguration(); + composite.addConfiguration(xmlconfig); + try + { + config.setConfiguration("", composite); + fail("Configuration should fail to validate"); + } + catch (ConfigurationException e) + { + assertEquals("Incorrect message.", "Unable to configure Slow Consumer Detection invalid TimeUnit:" + TIMEUNIT, e.getMessage()); + } + } + + +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/plugins/SlowConsumerDetectionPolicyConfigurationTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/plugins/SlowConsumerDetectionPolicyConfigurationTest.java new file mode 100644 index 0000000000..efb898e365 --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/plugins/SlowConsumerDetectionPolicyConfigurationTest.java @@ -0,0 +1,104 @@ +/* + * + * 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.server.virtualhost.plugins; + +import org.apache.commons.configuration.CompositeConfiguration; +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.XMLConfiguration; +import org.apache.qpid.server.configuration.plugins.SlowConsumerDetectionPolicyConfiguration; +import org.apache.qpid.server.util.InternalBrokerBaseCase; + +/** + * Test class to ensure that the policy configuration can be processed. + */ +public class SlowConsumerDetectionPolicyConfigurationTest extends InternalBrokerBaseCase +{ + + /** + * Input Testing: + * + * Test that a given String can be set and retrieved through the configuration + * + * No validation is being performed to ensure that the policy exists. Only + * that a value can be set for the policy. + * + */ + public void testConfigLoadingValidConfig() + { + SlowConsumerDetectionPolicyConfiguration config = new SlowConsumerDetectionPolicyConfiguration(); + + XMLConfiguration xmlconfig = new XMLConfiguration(); + + String policyName = "TestPolicy"; + xmlconfig.addProperty("name", policyName); + + // Create a CompositeConfiguration as this is what the broker uses + CompositeConfiguration composite = new CompositeConfiguration(); + composite.addConfiguration(xmlconfig); + + try + { + config.setConfiguration("", composite); + } + catch (ConfigurationException e) + { + e.printStackTrace(); + fail(e.getMessage()); + } + + assertEquals("Policy name not retrieved as expected.", + policyName, config.getPolicyName()); + } + + /** + * Failure Testing: + * + * Test that providing a configuration section without the 'name' field + * causes an exception to be thrown. + * + * An empty configuration is provided and the thrown exception message + * is checked to confirm the right reason. + * + */ + public void testConfigLoadingInValidConfig() + { + SlowConsumerDetectionPolicyConfiguration config = new SlowConsumerDetectionPolicyConfiguration(); + + XMLConfiguration xmlconfig = new XMLConfiguration(); + + + // Create a CompositeConfiguration as this is what the broker uses + CompositeConfiguration composite = new CompositeConfiguration(); + composite.addConfiguration(xmlconfig); + + try + { + config.setConfiguration("", composite); + fail("Config is invalid so won't validate."); + } + catch (ConfigurationException e) + { + e.printStackTrace(); + assertEquals("Exception message not as expected.", "No Slow consumer policy defined.", e.getMessage()); + } + } + +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/plugins/SlowConsumerDetectionQueueConfigurationTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/plugins/SlowConsumerDetectionQueueConfigurationTest.java new file mode 100644 index 0000000000..be86037dd8 --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/plugins/SlowConsumerDetectionQueueConfigurationTest.java @@ -0,0 +1,185 @@ +/* + * 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.server.virtualhost.plugins; + +import org.apache.commons.configuration.CompositeConfiguration; +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.XMLConfiguration; +import org.apache.qpid.server.configuration.plugins.SlowConsumerDetectionQueueConfiguration; +import org.apache.qpid.server.util.InternalBrokerBaseCase; + +/** + * Unit test the QueueConfiguration processing. + * + * This is slightly awkward as the {@link SlowConsumerDetectionQueueConfiguration} + * requries that a policy be available. + * <p> + * So all the Valid test much catch the ensuing {@link ConfigurationException} and + * validate that the error is due to a lack of a valid policy. + */ +public class SlowConsumerDetectionQueueConfigurationTest extends InternalBrokerBaseCase +{ + /** + * Test a fully loaded configuration file. + * + * It is not an error to have all control values specified. + * <p> + * Here we need to catch the {@link ConfigurationException} that ensues due to lack + * of a policy plugin. + */ + public void testConfigLoadingValidConfig() + { + SlowConsumerDetectionQueueConfiguration config = new SlowConsumerDetectionQueueConfiguration(); + + XMLConfiguration xmlconfig = new XMLConfiguration(); + + xmlconfig.addProperty("messageAge", "60000"); + xmlconfig.addProperty("depth", "1024"); + xmlconfig.addProperty("messageCount", "10"); + + // Create a CompositeConfiguration as this is what the broker uses + CompositeConfiguration composite = new CompositeConfiguration(); + composite.addConfiguration(xmlconfig); + + try + { + config.setConfiguration("", composite); + fail("No Policies are avaialbe to load in a unit test"); + } + catch (ConfigurationException e) + { + assertTrue("Exception message incorrect, was: " + e.getMessage(), + e.getMessage().startsWith("No Slow Consumer Policy specified. Known Policies:[")); + } + } + + /** + * When we do not specify any control value then a {@link ConfigurationException} + * must be thrown to remind us. + */ + public void testConfigLoadingMissingConfig() + { + SlowConsumerDetectionQueueConfiguration config = new SlowConsumerDetectionQueueConfiguration(); + + XMLConfiguration xmlconfig = new XMLConfiguration(); + + // Create a CompositeConfiguration as this is what the broker uses + CompositeConfiguration composite = new CompositeConfiguration(); + composite.addConfiguration(xmlconfig); + + try + { + config.setConfiguration("", composite); + fail("No Policies are avaialbe to load in a unit test"); + } + catch (ConfigurationException e) + { + + assertEquals("At least one configuration property('messageAge','depth'" + + " or 'messageCount') must be specified.", e.getMessage()); + } + } + + /** + * Setting messageAge on its own is enough to have a valid configuration + * + * Here we need to catch the {@link ConfigurationException} that ensues due to lack + * of a policy plugin. + */ + public void testConfigLoadingMessageAgeOk() + { + SlowConsumerDetectionQueueConfiguration config = new SlowConsumerDetectionQueueConfiguration(); + + XMLConfiguration xmlconfig = new XMLConfiguration(); + xmlconfig.addProperty("messageAge", "60000"); + + // Create a CompositeConfiguration as this is what the broker uses + CompositeConfiguration composite = new CompositeConfiguration(); + composite.addConfiguration(xmlconfig); + + try + { + config.setConfiguration("", composite); + fail("No Policies are avaialbe to load in a unit test"); + } + catch (ConfigurationException e) + { + assertTrue("Exception message incorrect, was: " + e.getMessage(), + e.getMessage().startsWith("No Slow Consumer Policy specified. Known Policies:[")); + } + } + + /** + * Setting depth on its own is enough to have a valid configuration. + * + * Here we need to catch the {@link ConfigurationException} that ensues due to lack + * of a policy plugin. + */ + public void testConfigLoadingDepthOk() + { + SlowConsumerDetectionQueueConfiguration config = new SlowConsumerDetectionQueueConfiguration(); + + XMLConfiguration xmlconfig = new XMLConfiguration(); + xmlconfig.addProperty("depth", "1024"); + + // Create a CompositeConfiguration as this is what the broker uses + CompositeConfiguration composite = new CompositeConfiguration(); + composite.addConfiguration(xmlconfig); + + try + { + config.setConfiguration("", composite); + fail("No Policies are avaialbe to load in a unit test"); + } + catch (ConfigurationException e) + { + assertTrue("Exception message incorrect, was: " + e.getMessage(), + e.getMessage().startsWith("No Slow Consumer Policy specified. Known Policies:[")); + } + } + + /** + * Setting messageCount on its own is enough to have a valid configuration. + * + * Here we need to catch the {@link ConfigurationException} that ensues due to lack + * of a policy plugin. + */ + public void testConfigLoadingMessageCountOk() + { + SlowConsumerDetectionQueueConfiguration config = new SlowConsumerDetectionQueueConfiguration(); + + XMLConfiguration xmlconfig = new XMLConfiguration(); + xmlconfig.addProperty("messageCount", "10"); + + // Create a CompositeConfiguration as this is what the broker uses + CompositeConfiguration composite = new CompositeConfiguration(); + composite.addConfiguration(xmlconfig); + + try + { + config.setConfiguration("", composite); + fail("No Policies are avaialbe to load in a unit test"); + } + catch (ConfigurationException e) + { + assertTrue("Exception message incorrect, was: " + e.getMessage(), + e.getMessage().startsWith("No Slow Consumer Policy specified. Known Policies:[")); + } + } +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/plugins/policies/TopicDeletePolicyConfigurationTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/plugins/policies/TopicDeletePolicyConfigurationTest.java new file mode 100644 index 0000000000..3d3cc810df --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/plugins/policies/TopicDeletePolicyConfigurationTest.java @@ -0,0 +1,88 @@ +/* + * + * 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.server.virtualhost.plugins.policies; + +import org.apache.commons.configuration.CompositeConfiguration; +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.XMLConfiguration; +import org.apache.qpid.server.util.InternalBrokerBaseCase; + +/** + * Test to ensure TopicDelete Policy configuration can be loaded. + */ +public class TopicDeletePolicyConfigurationTest extends InternalBrokerBaseCase +{ + /** + * Test without any configuration being provided that the + * deletePersistent option is disabled. + */ + public void testNoConfigNoDeletePersistent() + { + TopicDeletePolicyConfiguration config = new TopicDeletePolicyConfiguration(); + + assertFalse("TopicDelete Configuration with no config should not delete persistent queues.", + config.deletePersistent()); + } + + /** + * Test that with the correct configuration the deletePersistent option can + * be enabled. + * + * Test creates a new Configuration object and passes in the xml snippet + * that the ConfigurationPlugin would receive during normal execution. + * This is the XML that would be matched for this plugin: + * <topicdelete> + * <delete-persistent> + * <topicdelete> + * + * So it would be subset and passed in as just: + * <delete-persistent> + * + * + * The property should therefore be enabled. + * + */ + public void testConfigDeletePersistent() + { + TopicDeletePolicyConfiguration config = new TopicDeletePolicyConfiguration(); + + XMLConfiguration xmlconfig = new XMLConfiguration(); + + xmlconfig.addProperty("delete-persistent",""); + + // Create a CompositeConfiguration as this is what the broker uses + CompositeConfiguration composite = new CompositeConfiguration(); + composite.addConfiguration(xmlconfig); + + try + { + config.setConfiguration("",composite); + } + catch (ConfigurationException e) + { + fail(e.getMessage()); + } + + assertTrue("A configured TopicDelete should delete persistent queues.", + config.deletePersistent()); + } + +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/plugins/policies/TopicDeletePolicyTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/plugins/policies/TopicDeletePolicyTest.java new file mode 100644 index 0000000000..a2e83add05 --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/plugins/policies/TopicDeletePolicyTest.java @@ -0,0 +1,293 @@ +/* + * + * 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.server.virtualhost.plugins.policies; + +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.XMLConfiguration; +import org.apache.qpid.AMQException; +import org.apache.qpid.server.AMQChannel; +import org.apache.qpid.server.binding.Binding; +import org.apache.qpid.server.exchange.DirectExchange; +import org.apache.qpid.server.exchange.TopicExchange; +import org.apache.qpid.server.protocol.AMQProtocolSession; +import org.apache.qpid.server.protocol.InternalTestProtocolSession; +import org.apache.qpid.server.queue.AMQQueue; +import org.apache.qpid.server.queue.MockAMQQueue; +import org.apache.qpid.server.registry.ApplicationRegistry; +import org.apache.qpid.server.util.InternalBrokerBaseCase; +import org.apache.qpid.server.virtualhost.VirtualHost; + +public class TopicDeletePolicyTest extends InternalBrokerBaseCase +{ + + TopicDeletePolicyConfiguration _config; + + VirtualHost _defaultVhost; + InternalTestProtocolSession _connection; + + public void setUp() throws Exception + { + super.setUp(); + + _defaultVhost = ApplicationRegistry.getInstance().getVirtualHostRegistry().getDefaultVirtualHost(); + + _connection = new InternalTestProtocolSession(_defaultVhost); + + _config = new TopicDeletePolicyConfiguration(); + + XMLConfiguration config = new XMLConfiguration(); + + _config.setConfiguration("", config); + } + + private MockAMQQueue createOwnedQueue() + { + MockAMQQueue queue = new MockAMQQueue("testQueue"); + + _defaultVhost.getQueueRegistry().registerQueue(queue); + + try + { + AMQChannel channel = new AMQChannel(_connection, 0, null); + _connection.addChannel(channel); + + queue.setExclusiveOwningSession(channel); + } + catch (AMQException e) + { + fail("Unable to create Channel:" + e.getMessage()); + } + + return queue; + } + + private void setQueueToAutoDelete(final AMQQueue queue) + { + ((MockAMQQueue) queue).setAutoDelete(true); + + queue.setDeleteOnNoConsumers(true); + final AMQProtocolSession.Task deleteQueueTask = + new AMQProtocolSession.Task() + { + public void doTask(AMQProtocolSession session) throws AMQException + { + queue.delete(); + } + }; + + ((AMQChannel) queue.getExclusiveOwningSession()).getProtocolSession().addSessionCloseTask(deleteQueueTask); + } + + /** Check that a null queue passed in does not upset the policy. */ + public void testNullQueueParameter() throws ConfigurationException + { + TopicDeletePolicy policy = new TopicDeletePolicy(); + policy.configure(_config); + + try + { + policy.performPolicy(null); + } + catch (Exception e) + { + fail("Exception should not be thrown:" + e.getMessage()); + } + + } + + /** + * Set a owning Session to null which means this is not an exclusive queue + * so the queue should not be deleted + */ + public void testNonExclusiveQueue() + { + TopicDeletePolicy policy = new TopicDeletePolicy(); + policy.configure(_config); + + MockAMQQueue queue = createOwnedQueue(); + + queue.setExclusiveOwningSession(null); + + policy.performPolicy(queue); + + assertFalse("Queue should not be deleted", queue.isDeleted()); + assertFalse("Connection should not be closed", _connection.isClosed()); + } + + /** + * Test that exclusive JMS Queues are not deleted. + * Bind the queue to the direct exchange (so it is a JMS Queue). + * + * JMS Queues are not to be processed so this should not delete the queue. + */ + public void testQueuesAreNotProcessed() + { + TopicDeletePolicy policy = new TopicDeletePolicy(); + policy.configure(_config); + + MockAMQQueue queue = createOwnedQueue(); + + queue.addBinding(new Binding(null, "bindingKey", queue, new DirectExchange(), null)); + + policy.performPolicy(queue); + + assertFalse("Queue should not be deleted", queue.isDeleted()); + assertFalse("Connection should not be closed", _connection.isClosed()); + } + + /** + * Give a non auto-delete queue is bound to the topic exchange the + * TopicDeletePolicy will close the connection and delete the queue, + */ + public void testNonAutoDeleteTopicIsNotClosed() + { + TopicDeletePolicy policy = new TopicDeletePolicy(); + policy.configure(_config); + + MockAMQQueue queue = createOwnedQueue(); + + queue.addBinding(new Binding(null, "bindingKey", queue, new TopicExchange(), null)); + + queue.setAutoDelete(false); + + policy.performPolicy(queue); + + assertFalse("Queue should not be deleted", queue.isDeleted()); + assertTrue("Connection should be closed", _connection.isClosed()); + } + + /** + * Give a auto-delete queue bound to the topic exchange the TopicDeletePolicy will + * close the connection and delete the queue + */ + public void testTopicIsClosed() + { + TopicDeletePolicy policy = new TopicDeletePolicy(); + policy.configure(_config); + + final MockAMQQueue queue = createOwnedQueue(); + + queue.addBinding(new Binding(null, "bindingKey", queue, new TopicExchange(), null)); + + setQueueToAutoDelete(queue); + + policy.performPolicy(queue); + + assertTrue("Queue should be deleted", queue.isDeleted()); + assertTrue("Connection should be closed", _connection.isClosed()); + } + + /** + * Give a queue bound to the topic exchange the TopicDeletePolicy will + * close the connection and NOT delete the queue + */ + public void testNonAutoDeleteTopicIsClosedNotDeleted() + { + TopicDeletePolicy policy = new TopicDeletePolicy(); + policy.configure(_config); + + MockAMQQueue queue = createOwnedQueue(); + + queue.addBinding(new Binding(null, "bindingKey", queue, new TopicExchange(), null)); + + policy.performPolicy(queue); + + assertFalse("Queue should not be deleted", queue.isDeleted()); + assertTrue("Connection should be closed", _connection.isClosed()); + } + + /** + * Give a queue bound to the topic exchange the TopicDeletePolicy suitably + * configured with the delete-persistent tag will close the connection + * and delete the queue + */ + public void testPersistentTopicIsClosedAndDeleted() + { + //Set the config to delete persistent queues + _config.getConfig().addProperty("delete-persistent", ""); + + TopicDeletePolicy policy = new TopicDeletePolicy(); + policy.configure(_config); + + assertTrue("Config was not updated to delete Persistent topics", + _config.deletePersistent()); + + MockAMQQueue queue = createOwnedQueue(); + + queue.addBinding(new Binding(null, "bindingKey", queue, new TopicExchange(), null)); + + policy.performPolicy(queue); + + assertTrue("Queue should be deleted", queue.isDeleted()); + assertTrue("Connection should be closed", _connection.isClosed()); + } + + /** + * Give a queue bound to the topic exchange the TopicDeletePolicy not + * configured to close a persistent queue + */ + public void testPersistentTopicIsClosedAndDeletedNullConfig() + { + TopicDeletePolicy policy = new TopicDeletePolicy(); + // Explicity say we are not configuring the policy. + policy.configure(null); + + MockAMQQueue queue = createOwnedQueue(); + + queue.addBinding(new Binding(null, "bindingKey", queue, new TopicExchange(), null)); + + policy.performPolicy(queue); + + assertFalse("Queue should not be deleted", queue.isDeleted()); + assertTrue("Connection should be closed", _connection.isClosed()); + } + + public void testNonExclusiveQueueNullConfig() + { + _config = null; + testNonExclusiveQueue(); + } + + public void testQueuesAreNotProcessedNullConfig() + { + _config = null; + testQueuesAreNotProcessed(); + } + + public void testNonAutoDeleteTopicIsNotClosedNullConfig() + { + _config = null; + testNonAutoDeleteTopicIsNotClosed(); + } + + public void testTopicIsClosedNullConfig() + { + _config = null; + testTopicIsClosed(); + } + + public void testNonAutoDeleteTopicIsClosedNotDeletedNullConfig() throws AMQException + { + _config = null; + testNonAutoDeleteTopicIsClosedNotDeleted(); + } + +} |