summaryrefslogtreecommitdiff
path: root/qpid/java/systests/src/main/java/org/apache/qpid/server
diff options
context:
space:
mode:
Diffstat (limited to 'qpid/java/systests/src/main/java/org/apache/qpid/server')
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/BrokerStartupTest.java149
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/configuration/ServerConfigurationFileTest.java89
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/exchange/MessagingTestConfigProperties.java308
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/exchange/ReturnUnroutableMandatoryMessageTest.java309
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/failover/FailoverMethodTest.java254
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/failover/MessageDisappearWithIOExceptionTest.java338
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/failure/HeapExhaustion.java236
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/logging/AbstractTestLogging.java448
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/logging/AccessControlLoggingTest.java174
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/logging/AlertingTest.java202
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/logging/BindingLoggingTest.java271
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/logging/BrokerLoggingTest.java1003
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/logging/ChannelLoggingTest.java313
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/logging/ConnectionLoggingTest.java192
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/logging/DerbyMessageStoreLoggingTest.java574
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/logging/DurableQueueLoggingTest.java307
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/logging/ExchangeLoggingTest.java217
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/logging/ManagementLoggingTest.java305
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/logging/MemoryMessageStoreLoggingTest.java186
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/logging/QueueLoggingTest.java183
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/logging/SubscriptionLoggingTest.java458
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/logging/TransientQueueLoggingTest.java30
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/logging/VirtualHostLoggingTest.java134
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/persistent/NoLocalAfterRecoveryTest.java246
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/queue/ConflationQueueTest.java435
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/queue/DeepQueueConsumeWithSelector.java159
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/queue/ModelTest.java343
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/queue/MultipleTransactedBatchProducerTest.java246
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/queue/PersistentTestManual.java276
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/queue/PriorityTest.java212
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/queue/ProducerFlowControlTest.java496
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/queue/QueueDepthWithSelectorTest.java176
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/queue/SubscriptionTestHelper.java294
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/queue/TimeToLiveTest.java370
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/AbstractACLTestCase.java285
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/ExhaustiveACLTest.java195
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/ExternalACLFileTest.java184
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/ExternalACLJMXTest.java244
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/ExternalACLTest.java37
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/ExternalAdminACLTest.java186
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/SimpleACLTest.java644
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/security/firewall/FirewallConfigTest.java298
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/store/PersistentStoreTest.java193
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/store/SlowMessageStore.java321
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/util/AveragedRun.java66
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/util/RunStats.java57
-rw-r--r--qpid/java/systests/src/main/java/org/apache/qpid/server/util/TimedRun.java52
47 files changed, 12695 insertions, 0 deletions
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/BrokerStartupTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/BrokerStartupTest.java
new file mode 100644
index 0000000000..f9227c53ba
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/BrokerStartupTest.java
@@ -0,0 +1,149 @@
+/*
+ * 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;
+
+import org.apache.qpid.server.logging.AbstractTestLogging;
+import org.apache.qpid.util.LogMonitor;
+import org.apache.log4j.Logger;
+import org.apache.log4j.Level;
+
+import java.util.List;
+
+import junit.framework.AssertionFailedError;
+
+import javax.jms.Connection;
+import javax.jms.Session;
+import javax.jms.Queue;
+
+/**
+ * Series of tests to validate the external Java broker starts up as expected.
+ */
+public class BrokerStartupTest extends AbstractTestLogging
+{
+ public void setUp() throws Exception
+ {
+ // We either do this here or have a null check in tearDown.
+ // As when this test is run against profiles other than java it will NPE
+ _monitor = new LogMonitor(_outputFile);
+ //We explicitly do not call super.setUp as starting up the broker is
+ //part of the test case.
+ }
+
+
+ /**
+ * Description:
+ * Test that providing an invalid broker logging configuration file does not
+ * cause the broker to enable DEBUG logging that will seriously impair
+ * performance
+ * Input:
+ * -l value that does not exist
+ * <p/>
+ * Output:
+ * <p/>
+ * No DEBUG output
+ * <p/>
+ * Validation Steps:
+ * <p/>
+ * 1. Start the broker and verify no DEBUG output exists
+ *
+ * @throws Exception caused by broker startup
+ */
+ public void testInvalidLog4jConfigurationFile() throws Exception
+ {
+ // This logging startup code only occurs when you run a Java broker,
+ // that broker must be started via Main so not an InVM broker.
+ if (isJavaBroker() && isExternalBroker())
+ {
+ //Remove test Log4j config from the commandline
+ _broker = _broker.substring(0, _broker.indexOf("-l"));
+
+ // Add an invalid value
+ _broker += " -l invalid";
+
+ // The broker has a built in default log4j configuration set up
+ // so if the the broker cannot load the -l value it will use default
+ // use this default. Test that this is correctly loaded, by
+ // including -Dlog4j.debug so we can validate.
+ setBrokerEnvironment("QPID_OPTS", "-Dlog4j.debug");
+
+ // Disable all client logging so we can test for broker DEBUG only.
+ setLoggerLevel(Logger.getRootLogger(), Level.WARN);
+ setLoggerLevel(Logger.getLogger("qpid.protocol"), Level.WARN);
+ setLoggerLevel(Logger.getLogger("org.apache.qpid"), Level.WARN);
+
+ // Set the broker to use info level logging, which is the qpid-server
+ // default. Rather than debug which is the test default.
+ setBrokerOnlySystemProperty("amqj.server.logging.level", "info");
+ // Set the logging defaults to info for this test.
+ setBrokerOnlySystemProperty("amqj.logging.level", "info");
+ setBrokerOnlySystemProperty("root.logging.level", "info");
+
+ startBroker();
+
+ assertEquals("Log4j could not load desired configruation.",
+ 0, findMatches("log4j:ERROR Could not read configuration file from URL").size());
+
+ assertEquals("Logging did not error as expected",
+ 1, waitAndFindMatches("Logging configuration error: unable to read file ").size());
+
+
+ // Perfom some action on the broker to ensure that we hit the DEBUG
+ // messages that we know are there. Though the current xml parsing
+ // will generate a LOT of DEBUG on startup.
+ Connection connection = getConnection();
+
+ Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Queue queue = session.createQueue(getTestQueueName());
+ session.createConsumer(queue).close();
+
+ int COUNT = 10;
+ sendMessage(session, queue, COUNT);
+
+ assertEquals(COUNT,drainQueue(queue));
+
+ List<String> results = waitAndFindMatches("DEBUG");
+ try
+ {
+ // Validation
+
+ assertEquals("DEBUG messages should not be logged", 0, results.size());
+ }
+ catch (AssertionFailedError afe)
+ {
+ System.err.println("Log Dump:");
+ for (String log : results)
+ {
+ System.err.println(log);
+ }
+
+ if (results.size() == 0)
+ {
+ System.err.println("Monitored file contents:");
+ System.err.println(_monitor.readFile());
+ }
+
+ throw afe;
+ }
+ }
+ }
+
+} \ No newline at end of file
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/configuration/ServerConfigurationFileTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/configuration/ServerConfigurationFileTest.java
new file mode 100644
index 0000000000..d4c550bc08
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/configuration/ServerConfigurationFileTest.java
@@ -0,0 +1,89 @@
+/*
+ *
+ * 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.configuration;
+
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.qpid.test.utils.QpidBrokerTestCase;
+
+/**
+ * This system test ensures that when loading our default system-test
+ * configuration file the configuration is correctly loaded.
+ *
+ * All configuration values should be set in the systest config file so that
+ * the ability to load them can be validated.
+ */
+public class ServerConfigurationFileTest extends QpidBrokerTestCase
+{
+ ServerConfiguration _serverConfig;
+
+ public void setUp() throws ConfigurationException
+ {
+ if (!_configFile.exists())
+ {
+ fail("Unable to test without config file:" + _configFile);
+ }
+
+ saveTestConfiguration();
+ saveTestVirtualhosts();
+
+ _serverConfig = new ServerConfiguration(_configFile);
+ }
+
+ /**
+ * This helper method ensures that when we attempt to read a value that is
+ * set in the configuration file we do actualy read a value and not
+ * simply get a defaulted value from the ServerConfiguration.get*() methods.
+ *
+ * @param property the propert to test
+ */
+ private void validatePropertyDefinedInFile(String property)
+ {
+ //Verify that we are not just picking up the the default value from the getBoolean
+ assertNotNull("The value set in the configuration file is not being read for property:" + property,
+ _serverConfig.getConfig().getProperty(property));
+ }
+
+ public void testProtectIOEnabled() throws ConfigurationException
+ {
+ validatePropertyDefinedInFile(ServerConfiguration.CONNECTOR_PROTECTIO_ENABLED);
+ }
+
+ public void testProtectIOReadBufferLimitSize() throws ConfigurationException
+ {
+ validatePropertyDefinedInFile(ServerConfiguration.CONNECTOR_PROTECTIO_READ_BUFFER_LIMIT_SIZE);
+ }
+
+ public void testProtectIOWriteBufferLimitSize() throws ConfigurationException
+ {
+ validatePropertyDefinedInFile(ServerConfiguration.CONNECTOR_PROTECTIO_WRITE_BUFFER_LIMIT_SIZE);
+ }
+
+ public void testStatusUpdates() throws ConfigurationException
+ {
+ validatePropertyDefinedInFile(ServerConfiguration.STATUS_UPDATES);
+ }
+
+ public void testLocale() throws ConfigurationException
+ {
+ validatePropertyDefinedInFile(ServerConfiguration.ADVANCED_LOCALE);
+ }
+
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/exchange/MessagingTestConfigProperties.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/exchange/MessagingTestConfigProperties.java
new file mode 100644
index 0000000000..2d89d319d7
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/exchange/MessagingTestConfigProperties.java
@@ -0,0 +1,308 @@
+/*
+ *
+ * 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.exchange;
+
+import org.apache.qpid.jms.Session;
+import org.apache.qpid.junit.extensions.util.ParsedProperties;
+
+/**
+ * MessagingTestConfigProperties defines a set of property names and default values for specifying a messaging topology,
+ * and test parameters for running a messaging test over that topology. A Properties object holding some of these
+ * properties, superimposed onto the defaults, is used to establish test topologies and control test behaviour.
+ *
+ * <p/>A complete list of the parameters, default values and comments on their usage is provided here:
+ *
+ * <p/><table><caption>Parameters</caption>
+ * <tr><th> Parameter <th> Default <th> Comments
+ * <tr><td> messageSize <td> 0 <td> Message size in bytes. Not including any headers.
+ * <tr><td> destinationName <td> ping <td> The root name to use to generate destination names to ping.
+ * <tr><td> persistent <td> false <td> Determines whether peristent delivery is used.
+ * <tr><td> transacted <td> false <td> Determines whether messages are sent/received in transactions.
+ * <tr><td> broker <td> tcp://localhost:5672 <td> Determines the broker to connect to.
+ * <tr><td> virtualHost <td> test <td> Determines the virtual host to send all ping over.
+ * <tr><td> rate <td> 0 <td> The maximum rate (in hertz) to send messages at. 0 means no limit.
+ * <tr><td> verbose <td> false <td> The verbose flag for debugging. Prints to console on every message.
+ * <tr><td> pubsub <td> false <td> Whether to ping topics or queues. Uses p2p by default.
+ * <tr><td> username <td> guest <td> The username to access the broker with.
+ * <tr><td> password <td> guest <td> The password to access the broker with.
+ * <tr><td> selector <td> null <td> Not used. Defines a message selector to filter pings with.
+ * <tr><td> destinationCount <td> 1 <td> The number of receivers listening to the pings.
+ * <tr><td> timeout <td> 30000 <td> In milliseconds. The timeout to stop waiting for replies.
+ * <tr><td> commitBatchSize <td> 1 <td> The number of messages per transaction in transactional mode.
+ * <tr><td> uniqueDests <td> true <td> Whether each receiver only listens to one ping destination or all.
+ * <tr><td> durableDests <td> false <td> Whether or not durable destinations are used.
+ * <tr><td> ackMode <td> AUTO_ACK <td> The message acknowledgement mode. Possible values are:
+ * 0 - SESSION_TRANSACTED
+ * 1 - AUTO_ACKNOWLEDGE
+ * 2 - CLIENT_ACKNOWLEDGE
+ * 3 - DUPS_OK_ACKNOWLEDGE
+ * 257 - NO_ACKNOWLEDGE
+ * 258 - PRE_ACKNOWLEDGE
+ * <tr><td> maxPending <td> 0 <td> The maximum size in bytes, of messages sent but not yet received.
+ * Limits the volume of messages currently buffered on the client
+ * or broker. Can help scale test clients by limiting amount of buffered
+ * data to avoid out of memory errors.
+ * </table>
+ *
+ * <p><table id="crc"><caption>CRC Card</caption>
+ * <tr><th> Responsibilities <th> Collaborations
+ * <tr><td> Provide the names and defaults of all test parameters.
+ * </table>
+ */
+public class MessagingTestConfigProperties
+{
+ // ====================== Connection Properties ==================================
+
+ /** Holds the name of the default connection configuration. */
+ public static final String CONNECTION_NAME = "broker";
+
+ /** Holds the name of the property to get the initial context factory name from. */
+ public static final String INITIAL_CONTEXT_FACTORY_PROPNAME = "java.naming.factory.initial";
+
+ /** Defines the class to use as the initial context factory by default. */
+ public static final String INITIAL_CONTEXT_FACTORY_DEFAULT = "org.apache.qpid.jndi.PropertiesFileInitialContextFactory";
+
+ /** Holds the name of the default connection factory configuration property. */
+ public static final String CONNECTION_PROPNAME = "connectionfactory.broker";
+
+ /** Defeins the default connection configuration. */
+ public static final String CONNECTION_DEFAULT = "amqp://guest:guest@clientid/?brokerlist='vm://:1'";
+
+ /** Holds the name of the property to get the test broker url from. */
+ public static final String BROKER_PROPNAME = "qpid.test.broker";
+
+ /** Holds the default broker url for the test. */
+ public static final String BROKER_DEFAULT = "vm://:1";
+
+ /** Holds the name of the property to get the test broker virtual path. */
+ public static final String VIRTUAL_HOST_PROPNAME = "virtualHost";
+
+ /** Holds the default virtual path for the test. */
+ public static final String VIRTUAL_HOST_DEFAULT = "";
+
+ /** Holds the name of the property to get the broker access username from. */
+ public static final String USERNAME_PROPNAME = "username";
+
+ /** Holds the default broker log on username. */
+ public static final String USERNAME_DEFAULT = "guest";
+
+ /** Holds the name of the property to get the broker access password from. */
+ public static final String PASSWORD_PROPNAME = "password";
+
+ /** Holds the default broker log on password. */
+ public static final String PASSWORD_DEFAULT = "guest";
+
+ // ====================== Messaging Topology Properties ==========================
+
+ /** Holds the name of the property to get the bind publisher procuder flag from. */
+ public static final String PUBLISHER_PRODUCER_BIND_PROPNAME = "publisherProducerBind";
+
+ /** Holds the default value of the publisher producer flag. */
+ public static final boolean PUBLISHER_PRODUCER_BIND_DEFAULT = true;
+
+ /** Holds the name of the property to get the bind publisher procuder flag from. */
+ public static final String PUBLISHER_CONSUMER_BIND_PROPNAME = "publisherConsumerBind";
+
+ /** Holds the default value of the publisher consumer flag. */
+ public static final boolean PUBLISHER_CONSUMER_BIND_DEFAULT = false;
+
+ /** Holds the name of the property to get the bind receiver procuder flag from. */
+ public static final String RECEIVER_PRODUCER_BIND_PROPNAME = "receiverProducerBind";
+
+ /** Holds the default value of the receiver producer flag. */
+ public static final boolean RECEIVER_PRODUCER_BIND_DEFAULT = false;
+
+ /** Holds the name of the property to get the bind receiver procuder flag from. */
+ public static final String RECEIVER_CONSUMER_BIND_PROPNAME = "receiverConsumerBind";
+
+ /** Holds the default value of the receiver consumer flag. */
+ public static final boolean RECEIVER_CONSUMER_BIND_DEFAULT = true;
+
+ /** Holds the name of the property to get the destination name root from. */
+ public static final String SEND_DESTINATION_NAME_ROOT_PROPNAME = "sendDestinationRoot";
+
+ /** Holds the root of the name of the default destination to send to. */
+ public static final String SEND_DESTINATION_NAME_ROOT_DEFAULT = "sendTo";
+
+ /** Holds the name of the property to get the destination name root from. */
+ public static final String RECEIVE_DESTINATION_NAME_ROOT_PROPNAME = "receiveDestinationRoot";
+
+ /** Holds the root of the name of the default destination to send to. */
+ public static final String RECEIVE_DESTINATION_NAME_ROOT_DEFAULT = "receiveFrom";
+
+ /** Holds the name of the proeprty to get the destination count from. */
+ public static final String DESTINATION_COUNT_PROPNAME = "destinationCount";
+
+ /** Defines the default number of destinations to ping. */
+ public static final int DESTINATION_COUNT_DEFAULT = 1;
+
+ /** Holds the name of the property to get the p2p or pub/sub messaging mode from. */
+ public static final String PUBSUB_PROPNAME = "pubsub";
+
+ /** Holds the pub/sub mode default, true means ping a topic, false means ping a queue. */
+ public static final boolean PUBSUB_DEFAULT = false;
+
+ // ====================== JMS Options and Flags =================================
+
+ /** Holds the name of the property to get the test delivery mode from. */
+ public static final String PERSISTENT_MODE_PROPNAME = "persistent";
+
+ /** Holds the message delivery mode to use for the test. */
+ public static final boolean PERSISTENT_MODE_DEFAULT = false;
+
+ /** Holds the name of the property to get the test transactional mode from. */
+ public static final String TRANSACTED_PROPNAME = "transacted";
+
+ /** Holds the transactional mode to use for the test. */
+ public static final boolean TRANSACTED_DEFAULT = false;
+
+ /** Holds the name of the property to set the no local flag from. */
+ public static final String NO_LOCAL_PROPNAME = "noLocal";
+
+ /** Defines the default value of the no local flag to use when consuming messages. */
+ public static final boolean NO_LOCAL_DEFAULT = false;
+
+ /** Holds the name of the property to get the message acknowledgement mode from. */
+ public static final String ACK_MODE_PROPNAME = "ackMode";
+
+ /** Defines the default message acknowledgement mode. */
+ public static final int ACK_MODE_DEFAULT = Session.AUTO_ACKNOWLEDGE;
+
+ /** Holds the name of the property to get the durable subscriptions flag from, when doing pub/sub messaging. */
+ public static final String DURABLE_SUBSCRIPTION_PROPNAME = "durableSubscription";
+
+ /** Defines the default value of the durable subscriptions flag. */
+ public static final boolean DURABLE_SUBSCRIPTION_DEFAULT = false;
+
+ // ====================== Qpid Options and Flags ================================
+
+ /** Holds the name of the property to set the exclusive flag from. */
+ public static final String EXCLUSIVE_PROPNAME = "exclusive";
+
+ /** Defines the default value of the exclusive flag to use when consuming messages. */
+ public static final boolean EXCLUSIVE_DEFAULT = false;
+
+ /** Holds the name of the property to set the immediate flag from. */
+ public static final String IMMEDIATE_PROPNAME = "immediate";
+
+ /** Defines the default value of the immediate flag to use when sending messages. */
+ public static final boolean IMMEDIATE_DEFAULT = false;
+
+ /** Holds the name of the property to set the mandatory flag from. */
+ public static final String MANDATORY_PROPNAME = "mandatory";
+
+ /** Defines the default value of the mandatory flag to use when sending messages. */
+ public static final boolean MANDATORY_DEFAULT = false;
+
+ /** Holds the name of the property to get the durable destinations flag from. */
+ public static final String DURABLE_DESTS_PROPNAME = "durableDests";
+
+ /** Default value for the durable destinations flag. */
+ public static final boolean DURABLE_DESTS_DEFAULT = false;
+
+ /** Holds the name of the proeprty to set the prefetch size from. */
+ public static final String PREFECTH_PROPNAME = "prefetch";
+
+ /** Defines the default prefetch size to use when consuming messages. */
+ public static final int PREFETCH_DEFAULT = 100;
+
+ // ====================== Common Test Parameters ================================
+
+ /** Holds the name of the property to get the test message size from. */
+ public static final String MESSAGE_SIZE_PROPNAME = "messageSize";
+
+ /** Used to set up a default message size. */
+ public static final int MESSAGE_SIZE_DEAFULT = 0;
+
+ /** Holds the name of the property to get the message rate from. */
+ public static final String RATE_PROPNAME = "rate";
+
+ /** Defines the default rate (in pings per second) to send pings at. 0 means as fast as possible, no restriction. */
+ public static final int RATE_DEFAULT = 0;
+
+ /** Holds the name of the proeprty to get the. */
+ public static final String SELECTOR_PROPNAME = "selector";
+
+ /** Holds the default message selector. */
+ public static final String SELECTOR_DEFAULT = "";
+
+ /** Holds the name of the property to get the waiting timeout for response messages. */
+ public static final String TIMEOUT_PROPNAME = "timeout";
+
+ /** Default time to wait before assuming that a ping has timed out. */
+ public static final long TIMEOUT_DEFAULT = 30000;
+
+ /** Holds the name of the property to get the commit batch size from. */
+ public static final String TX_BATCH_SIZE_PROPNAME = "commitBatchSize";
+
+ /** Defines the default number of pings to send in each transaction when running transactionally. */
+ public static final int TX_BATCH_SIZE_DEFAULT = 1;
+
+ /** Holds the name of the property to set the maximum amount of pending message data for a producer to hold. */
+ public static final String MAX_PENDING_PROPNAME = "maxPending";
+
+ /** Defines the default maximum quantity of pending message data to allow producers to hold. */
+ public static final int MAX_PENDING_DEFAULT = 0;
+
+ /** Holds the name of the property to get the verbose mode proeprty from. */
+ public static final String VERBOSE_PROPNAME = "verbose";
+
+ /** Holds the default verbose mode. */
+ public static final boolean VERBOSE_DEFAULT = false;
+
+ /** Holds the default configuration properties. */
+ public static ParsedProperties defaults = new ParsedProperties();
+
+ static
+ {
+ defaults.setPropertyIfNull(INITIAL_CONTEXT_FACTORY_PROPNAME, INITIAL_CONTEXT_FACTORY_DEFAULT);
+ defaults.setPropertyIfNull(CONNECTION_PROPNAME, CONNECTION_DEFAULT);
+ defaults.setPropertyIfNull(MESSAGE_SIZE_PROPNAME, MESSAGE_SIZE_DEAFULT);
+ defaults.setPropertyIfNull(PUBLISHER_PRODUCER_BIND_PROPNAME, PUBLISHER_PRODUCER_BIND_DEFAULT);
+ defaults.setPropertyIfNull(PUBLISHER_CONSUMER_BIND_PROPNAME, PUBLISHER_CONSUMER_BIND_DEFAULT);
+ defaults.setPropertyIfNull(RECEIVER_PRODUCER_BIND_PROPNAME, RECEIVER_PRODUCER_BIND_DEFAULT);
+ defaults.setPropertyIfNull(RECEIVER_CONSUMER_BIND_PROPNAME, RECEIVER_CONSUMER_BIND_DEFAULT);
+ defaults.setPropertyIfNull(SEND_DESTINATION_NAME_ROOT_PROPNAME, SEND_DESTINATION_NAME_ROOT_DEFAULT);
+ defaults.setPropertyIfNull(RECEIVE_DESTINATION_NAME_ROOT_PROPNAME, RECEIVE_DESTINATION_NAME_ROOT_DEFAULT);
+ defaults.setPropertyIfNull(PERSISTENT_MODE_PROPNAME, PERSISTENT_MODE_DEFAULT);
+ defaults.setPropertyIfNull(TRANSACTED_PROPNAME, TRANSACTED_DEFAULT);
+ defaults.setPropertyIfNull(BROKER_PROPNAME, BROKER_DEFAULT);
+ defaults.setPropertyIfNull(VIRTUAL_HOST_PROPNAME, VIRTUAL_HOST_DEFAULT);
+ defaults.setPropertyIfNull(RATE_PROPNAME, RATE_DEFAULT);
+ defaults.setPropertyIfNull(VERBOSE_PROPNAME, VERBOSE_DEFAULT);
+ defaults.setPropertyIfNull(PUBSUB_PROPNAME, PUBSUB_DEFAULT);
+ defaults.setPropertyIfNull(USERNAME_PROPNAME, USERNAME_DEFAULT);
+ defaults.setPropertyIfNull(PASSWORD_PROPNAME, PASSWORD_DEFAULT);
+ defaults.setPropertyIfNull(SELECTOR_PROPNAME, SELECTOR_DEFAULT);
+ defaults.setPropertyIfNull(DESTINATION_COUNT_PROPNAME, DESTINATION_COUNT_DEFAULT);
+ defaults.setPropertyIfNull(TIMEOUT_PROPNAME, TIMEOUT_DEFAULT);
+ defaults.setPropertyIfNull(TX_BATCH_SIZE_PROPNAME, TX_BATCH_SIZE_DEFAULT);
+ defaults.setPropertyIfNull(DURABLE_DESTS_PROPNAME, DURABLE_DESTS_DEFAULT);
+ defaults.setPropertyIfNull(ACK_MODE_PROPNAME, ACK_MODE_DEFAULT);
+ defaults.setPropertyIfNull(DURABLE_SUBSCRIPTION_PROPNAME, DURABLE_SUBSCRIPTION_DEFAULT);
+ defaults.setPropertyIfNull(MAX_PENDING_PROPNAME, MAX_PENDING_DEFAULT);
+ defaults.setPropertyIfNull(PREFECTH_PROPNAME, PREFETCH_DEFAULT);
+ defaults.setPropertyIfNull(NO_LOCAL_PROPNAME, NO_LOCAL_DEFAULT);
+ defaults.setPropertyIfNull(EXCLUSIVE_PROPNAME, EXCLUSIVE_DEFAULT);
+ defaults.setPropertyIfNull(IMMEDIATE_PROPNAME, IMMEDIATE_DEFAULT);
+ defaults.setPropertyIfNull(MANDATORY_PROPNAME, MANDATORY_DEFAULT);
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/exchange/ReturnUnroutableMandatoryMessageTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/exchange/ReturnUnroutableMandatoryMessageTest.java
new file mode 100644
index 0000000000..4b4fbd711b
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/exchange/ReturnUnroutableMandatoryMessageTest.java
@@ -0,0 +1,309 @@
+/*
+ *
+ * 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.exchange;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import javax.jms.Connection;
+import javax.jms.ExceptionListener;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+
+import org.apache.log4j.Logger;
+import org.apache.qpid.client.AMQHeadersExchange;
+import org.apache.qpid.client.AMQNoRouteException;
+import org.apache.qpid.client.AMQQueue;
+import org.apache.qpid.client.AMQSession;
+import org.apache.qpid.client.AMQTopic;
+import org.apache.qpid.configuration.ClientProperties;
+import org.apache.qpid.exchange.ExchangeDefaults;
+import org.apache.qpid.framing.FieldTable;
+import org.apache.qpid.test.utils.QpidBrokerTestCase;
+import org.apache.qpid.url.AMQBindingURL;
+import org.apache.qpid.url.BindingURL;
+
+public class ReturnUnroutableMandatoryMessageTest extends QpidBrokerTestCase implements ExceptionListener
+{
+ private static final Logger _logger = Logger.getLogger(ReturnUnroutableMandatoryMessageTest.class);
+
+ private final List<Message> _bouncedMessageList = Collections.synchronizedList(new ArrayList<Message>());
+
+ static
+ {
+ String workdir = System.getProperty("QPID_WORK");
+ if (workdir == null || workdir.equals(""))
+ {
+ String tempdir = System.getProperty("java.io.tmpdir");
+ System.out.println("QPID_WORK not set using tmp directory: " + tempdir);
+ System.setProperty("QPID_WORK", tempdir);
+ }
+ }
+
+ /**
+ * Tests that mandatory message which are not routable are returned to the producer
+ *
+ * @throws Exception
+ */
+ public void testReturnUnroutableMandatoryMessage_HEADERS() throws Exception
+ {
+ _bouncedMessageList.clear();
+ MessageConsumer consumer = null;
+ AMQSession producerSession = null;
+ AMQHeadersExchange queue = null;
+ Connection con=null, con2 = null;
+ try
+ {
+ con = getConnection();
+
+ AMQSession consumerSession = (AMQSession) con.createSession(false, Session.CLIENT_ACKNOWLEDGE);
+
+ queue = new AMQHeadersExchange(new AMQBindingURL(ExchangeDefaults.HEADERS_EXCHANGE_CLASS + "://" + ExchangeDefaults.HEADERS_EXCHANGE_NAME + "/test/queue1?" + BindingURL.OPTION_ROUTING_KEY + "='F0000=1'"));
+ FieldTable ft = new FieldTable();
+ ft.setString("F1000", "1");
+ consumer = consumerSession.createConsumer(queue, Integer.parseInt(ClientProperties.MAX_PREFETCH_DEFAULT), Integer.parseInt(ClientProperties.MAX_PREFETCH_DEFAULT) /2 , false, false, (String) null, ft);
+
+ //force synch to ensure the consumer has resulted in a bound queue
+ //((AMQSession) consumerSession).declareExchangeSynch(ExchangeDefaults.HEADERS_EXCHANGE_NAME, ExchangeDefaults.HEADERS_EXCHANGE_CLASS);
+ // This is the default now
+
+ con2 = getConnection();
+
+ con2.setExceptionListener(this);
+ producerSession = (AMQSession) con2.createSession(false, Session.CLIENT_ACKNOWLEDGE);
+
+ // Need to start the "producer" connection in order to receive bounced messages
+ _logger.info("Starting producer connection");
+ con2.start();
+ }
+ catch (JMSException jmse)
+ {
+ fail(jmse.getMessage());
+ }
+
+ try
+ {
+ MessageProducer nonMandatoryProducer = producerSession.createProducer(queue, false, false);
+ MessageProducer mandatoryProducer = producerSession.createProducer(queue);
+
+ // First test - should neither be bounced nor routed
+ _logger.info("Sending non-routable non-mandatory message");
+ TextMessage msg1 = producerSession.createTextMessage("msg1");
+ nonMandatoryProducer.send(msg1);
+
+ // Second test - should be bounced
+ _logger.info("Sending non-routable mandatory message");
+ TextMessage msg2 = producerSession.createTextMessage("msg2");
+ mandatoryProducer.send(msg2);
+
+ // Third test - should be routed
+ _logger.info("Sending routable message");
+ TextMessage msg3 = producerSession.createTextMessage("msg3");
+ msg3.setStringProperty("F1000", "1");
+ mandatoryProducer.send(msg3);
+
+ _logger.info("Starting consumer connection");
+ con.start();
+ TextMessage tm = (TextMessage) consumer.receive(1000L);
+
+ assertTrue("No message routed to receiver", tm != null);
+ assertTrue("Wrong message routed to receiver: " + tm.getText(), "msg3".equals(tm.getText()));
+
+ try
+ {
+ Thread.sleep(1000L);
+ }
+ catch (InterruptedException e)
+ {
+ ;
+ }
+
+ assertTrue("Wrong number of messages bounced (expect 1): " + _bouncedMessageList.size(), _bouncedMessageList.size() == 1);
+ Message m = _bouncedMessageList.get(0);
+ assertTrue("Wrong message bounced: " + m.toString(), m.toString().contains("msg2"));
+ }
+ catch (JMSException jmse)
+ {
+
+ }
+ con.close();
+ con2.close();
+
+ }
+
+ public void testReturnUnroutableMandatoryMessage_QUEUE() throws Exception
+ {
+ _bouncedMessageList.clear();
+ Connection con = getConnection();
+
+ AMQSession consumerSession = (AMQSession) con.createSession(false, Session.CLIENT_ACKNOWLEDGE);
+
+ AMQQueue valid_queue = new AMQQueue(ExchangeDefaults.DIRECT_EXCHANGE_CLASS, "testReturnUnroutableMandatoryMessage_QUEUE");
+ AMQQueue invalid_queue = new AMQQueue(ExchangeDefaults.DIRECT_EXCHANGE_CLASS, "testReturnUnroutableMandatoryMessage_QUEUE_INVALID");
+ MessageConsumer consumer = consumerSession.createConsumer(valid_queue);
+
+ //force synch to ensure the consumer has resulted in a bound queue
+ //((AMQSession) consumerSession).declareExchangeSynch(ExchangeDefaults.HEADERS_EXCHANGE_NAME, ExchangeDefaults.HEADERS_EXCHANGE_CLASS);
+ // This is the default now
+
+ Connection con2 = getConnection();
+
+ con2.setExceptionListener(this);
+ AMQSession producerSession = (AMQSession) con2.createSession(false, Session.CLIENT_ACKNOWLEDGE);
+
+ // Need to start the "producer" connection in order to receive bounced messages
+ _logger.info("Starting producer connection");
+ con2.start();
+
+ MessageProducer nonMandatoryProducer = producerSession.createProducer(valid_queue, false, false);
+ MessageProducer mandatoryProducer = producerSession.createProducer(invalid_queue);
+
+ // First test - should be routed
+ _logger.info("Sending non-mandatory message");
+ TextMessage msg1 = producerSession.createTextMessage("msg1");
+ nonMandatoryProducer.send(msg1);
+
+ // Second test - should be bounced
+ _logger.info("Sending non-routable mandatory message");
+ TextMessage msg2 = producerSession.createTextMessage("msg2");
+ mandatoryProducer.send(msg2);
+
+ _logger.info("Starting consumer connection");
+ con.start();
+ TextMessage tm = (TextMessage) consumer.receive(1000L);
+
+ assertTrue("No message routed to receiver", tm != null);
+ assertTrue("Wrong message routed to receiver: " + tm.getText(), "msg1".equals(tm.getText()));
+
+ try
+ {
+ Thread.sleep(1000L);
+ }
+ catch (InterruptedException e)
+ {
+ ;
+ }
+
+ assertTrue("Wrong number of messages bounced (expect 1): " + _bouncedMessageList.size(), _bouncedMessageList.size() == 1);
+ Message m = _bouncedMessageList.get(0);
+ assertTrue("Wrong message bounced: " + m.toString(), m.toString().contains("msg2"));
+
+ con.close();
+ con2.close();
+ }
+
+ public void testReturnUnroutableMandatoryMessage_TOPIC() throws Exception
+ {
+ _bouncedMessageList.clear();
+ Connection con = getConnection();
+
+ AMQSession consumerSession = (AMQSession) con.createSession(false, Session.CLIENT_ACKNOWLEDGE);
+
+ AMQTopic valid_topic = new AMQTopic(ExchangeDefaults.TOPIC_EXCHANGE_CLASS, "test.Return.Unroutable.Mandatory.Message.TOPIC");
+ AMQTopic invalid_topic = new AMQTopic(ExchangeDefaults.TOPIC_EXCHANGE_CLASS, "test.Return.Unroutable.Mandatory.Message.TOPIC.invalid");
+ MessageConsumer consumer = consumerSession.createConsumer(valid_topic);
+
+ //force synch to ensure the consumer has resulted in a bound queue
+ //((AMQSession) consumerSession).declareExchangeSynch(ExchangeDefaults.HEADERS_EXCHANGE_NAME, ExchangeDefaults.HEADERS_EXCHANGE_CLASS);
+ // This is the default now
+
+ Connection con2 = getConnection();
+
+ con2.setExceptionListener(this);
+ AMQSession producerSession = (AMQSession) con2.createSession(false, Session.CLIENT_ACKNOWLEDGE);
+
+ // Need to start the "producer" connection in order to receive bounced messages
+ _logger.info("Starting producer connection");
+ con2.start();
+
+ MessageProducer nonMandatoryProducer = producerSession.createProducer(valid_topic, false, false);
+ MessageProducer mandatoryProducer = producerSession.createProducer(invalid_topic);
+
+ // First test - should be routed
+ _logger.info("Sending non-mandatory message");
+ TextMessage msg1 = producerSession.createTextMessage("msg1");
+ nonMandatoryProducer.send(msg1);
+
+ // Second test - should be bounced
+ _logger.info("Sending non-routable mandatory message");
+ TextMessage msg2 = producerSession.createTextMessage("msg2");
+ mandatoryProducer.send(msg2);
+
+ _logger.info("Starting consumer connection");
+ con.start();
+ TextMessage tm = (TextMessage) consumer.receive(1000L);
+
+ assertTrue("No message routed to receiver", tm != null);
+ assertTrue("Wrong message routed to receiver: " + tm.getText(), "msg1".equals(tm.getText()));
+
+ try
+ {
+ Thread.sleep(1000L);
+ }
+ catch (InterruptedException e)
+ {
+ ;
+ }
+
+ assertEquals("Wrong number of messages bounced: ", 1, _bouncedMessageList.size());
+ Message m = _bouncedMessageList.get(0);
+ assertTrue("Wrong message bounced: " + m.toString(), m.toString().contains("msg2"));
+
+ con.close();
+ con2.close();
+ }
+
+ public static junit.framework.Test suite()
+ {
+ return new junit.framework.TestSuite(ReturnUnroutableMandatoryMessageTest.class);
+ }
+
+ public void onException(JMSException jmsException)
+ {
+
+ Exception linkedException = null;
+ try
+ {
+ linkedException = jmsException.getLinkedException();
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+ if (linkedException instanceof AMQNoRouteException)
+ {
+ AMQNoRouteException noRoute = (AMQNoRouteException) linkedException;
+ Message bounced = (Message) noRoute.getUndeliveredMessage();
+ _bouncedMessageList.add(bounced);
+ _logger.info("Caught expected NoRouteException");
+ }
+ else
+ {
+ _logger.warn("Caught exception on producer: ", jmsException);
+ }
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/failover/FailoverMethodTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/failover/FailoverMethodTest.java
new file mode 100644
index 0000000000..ec222ff03d
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/failover/FailoverMethodTest.java
@@ -0,0 +1,254 @@
+/*
+ *
+ * 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.failover;
+
+import junit.framework.TestCase;
+
+import org.apache.qpid.AMQDisconnectedException;
+import org.apache.qpid.AMQException;
+import org.apache.qpid.client.AMQConnection;
+import org.apache.qpid.client.AMQConnectionURL;
+import org.apache.qpid.client.transport.TransportConnection;
+import org.apache.qpid.client.vmbroker.AMQVMBrokerCreationException;
+import org.apache.qpid.server.registry.ApplicationRegistry;
+import org.apache.qpid.server.util.InternalBrokerBaseCase;
+import org.apache.qpid.url.URLSyntaxException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.jms.ExceptionListener;
+import javax.jms.JMSException;
+import java.util.concurrent.CountDownLatch;
+
+public class FailoverMethodTest extends InternalBrokerBaseCase implements ExceptionListener
+{
+ private CountDownLatch _failoverComplete = new CountDownLatch(1);
+ protected static final Logger _logger = LoggerFactory.getLogger(FailoverMethodTest.class);
+
+ @Override
+ public void createBroker() throws Exception
+ {
+ super.createBroker();
+ TransportConnection.createVMBroker(ApplicationRegistry.DEFAULT_INSTANCE);
+ }
+
+ @Override
+ public void stopBroker()
+ {
+ TransportConnection.killVMBroker(ApplicationRegistry.DEFAULT_INSTANCE);
+ super.stopBroker();
+ }
+
+ /**
+ * Test that the round robin method has the correct delays.
+ * The first connection to vm://:1 will work but the localhost connection should fail but the duration it takes
+ * to report the failure is what is being tested.
+ *
+ * @throws URLSyntaxException
+ * @throws InterruptedException
+ * @throws JMSException
+ */
+ public void testFailoverRoundRobinDelay() throws URLSyntaxException, InterruptedException, JMSException
+ {
+ //note: The VM broker has no connect delay and the default 1 retry
+ // while the tcp:localhost broker has 3 retries with a 2s connect delay
+ String connectionString = "amqp://guest:guest@/test?brokerlist=" +
+ "'vm://:" + ApplicationRegistry.DEFAULT_INSTANCE +
+ ";tcp://localhost:5670?connectdelay='2000',retries='3''";
+
+ AMQConnectionURL url = new AMQConnectionURL(connectionString);
+
+ try
+ {
+ long start = System.currentTimeMillis();
+ AMQConnection connection = new AMQConnection(url, null);
+
+ connection.setExceptionListener(this);
+
+ stopBroker();
+
+ _failoverComplete.await();
+
+ long end = System.currentTimeMillis();
+
+ long duration = (end - start);
+
+ //Failover should take more that 6 seconds.
+ // 3 Retires
+ // so VM Broker NoDelay 0 (Connect) NoDelay 0
+ // then TCP NoDelay 0 Delay 1 Delay 2 Delay 3
+ // so 3 delays of 2s in total for connection
+ // as this is a tcp connection it will take 1second per connection to fail
+ // so max time is 6seconds of delay plus 4 seconds of TCP Delay + 1 second of runtime. == 11 seconds
+
+ // Ensure we actually had the delay
+ assertTrue("Failover took less than 6 seconds", duration > 6000);
+
+ // Ensure we don't have delays before initial connection and reconnection.
+ // We allow 1 second for initial connection and failover logic on top of 6s of sleep.
+ assertTrue("Failover took more than 11 seconds:(" + duration + ")", duration < 11000);
+ }
+ catch (AMQException e)
+ {
+ fail(e.getMessage());
+ }
+ }
+
+ public void testFailoverSingleDelay() throws URLSyntaxException, AMQVMBrokerCreationException,
+ InterruptedException, JMSException
+ {
+ String connectionString = "amqp://guest:guest@/test?brokerlist='vm://:1?connectdelay='2000',retries='3''";
+
+ AMQConnectionURL url = new AMQConnectionURL(connectionString);
+
+ try
+ {
+ long start = System.currentTimeMillis();
+ AMQConnection connection = new AMQConnection(url, null);
+
+ connection.setExceptionListener(this);
+
+ stopBroker();
+
+ _failoverComplete.await();
+
+ long end = System.currentTimeMillis();
+
+ long duration = (end - start);
+
+ //Failover should take more that 6 seconds.
+ // 3 Retires
+ // so NoDelay 0 (Connect) NoDelay 0 Delay 1 Delay 2 Delay 3
+ // so 3 delays of 2s in total for connection
+ // so max time is 6 seconds of delay + 1 second of runtime. == 7 seconds
+
+ // Ensure we actually had the delay
+ assertTrue("Failover took less than 6 seconds", duration > 6000);
+
+ // Ensure we don't have delays before initial connection and reconnection.
+ // We allow 1 second for initial connection and failover logic on top of 6s of sleep.
+ assertTrue("Failover took more than 7 seconds:(" + duration + ")", duration < 7000);
+ }
+ catch (AMQException e)
+ {
+ fail(e.getMessage());
+ }
+ }
+
+ public void onException(JMSException e)
+ {
+ if (e.getLinkedException() instanceof AMQDisconnectedException)
+ {
+ _logger.debug("Received AMQDisconnectedException");
+ _failoverComplete.countDown();
+ }
+ }
+
+ /**
+ * Test that setting 'nofailover' as the failover policy does not result in
+ * delays or connection attempts when the initial connection is lost.
+ *
+ * Test validates that there is a connection delay as required on initial
+ * connection.
+ *
+ * @throws URLSyntaxException
+ * @throws AMQVMBrokerCreationException
+ * @throws InterruptedException
+ * @throws JMSException
+ */
+ public void testNoFailover() throws URLSyntaxException, AMQVMBrokerCreationException,
+ InterruptedException, JMSException
+ {
+ int CONNECT_DELAY = 2000;
+ String connectionString = "amqp://guest:guest@/test?brokerlist='vm://:1?connectdelay='" + CONNECT_DELAY + "'," +
+ "retries='3'',failover='nofailover'";
+
+ AMQConnectionURL url = new AMQConnectionURL(connectionString);
+
+ try
+ {
+ //Kill initial broker
+ stopBroker();
+
+ //Create a thread to start the broker asynchronously
+ Thread brokerStart = new Thread(new Runnable()
+ {
+ public void run()
+ {
+ try
+ {
+ //Wait before starting broker
+ // The wait should allow atleast 1 retries to fail before broker is ready
+ Thread.sleep(750);
+ createBroker();
+ }
+ catch (Exception e)
+ {
+ System.err.println(e.getMessage());
+ e.printStackTrace();
+ }
+ }
+ });
+
+
+ brokerStart.start();
+ long start = System.currentTimeMillis();
+
+
+ //Start the connection so it will use the retries
+ AMQConnection connection = new AMQConnection(url, null);
+
+ long end = System.currentTimeMillis();
+
+ long duration = (end - start);
+
+ // Check that we actually had a delay had a delay in connection
+ assertTrue("Initial connection should be longer than 1 delay : " + CONNECT_DELAY + " <:(" + duration + ")", duration > CONNECT_DELAY);
+
+
+ connection.setExceptionListener(this);
+
+ //Ensure we collect the brokerStart thread
+ brokerStart.join();
+
+ start = System.currentTimeMillis();
+
+ //Kill connection
+ stopBroker();
+
+ _failoverComplete.await();
+
+ end = System.currentTimeMillis();
+
+ duration = (end - start);
+
+ // Notification of the connection failure should be very quick as we are denying the ability to failover.
+ // It may not be as quick for Java profile tests so lets just make sure it is less than the connectiondelay
+ // Occasionally it takes 1s so we have to set CONNECT_DELAY to be higher to take that in to account.
+ assertTrue("Notification of the connection failure took was : " + CONNECT_DELAY + " >:(" + duration + ")", duration < CONNECT_DELAY);
+ }
+ catch (AMQException e)
+ {
+ fail(e.getMessage());
+ }
+ }
+
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/failover/MessageDisappearWithIOExceptionTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/failover/MessageDisappearWithIOExceptionTest.java
new file mode 100644
index 0000000000..4c2758241e
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/failover/MessageDisappearWithIOExceptionTest.java
@@ -0,0 +1,338 @@
+/*
+ *
+ * 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.failover;
+
+import org.apache.mina.common.WriteTimeoutException;
+import org.apache.qpid.client.AMQConnection;
+import org.apache.qpid.client.protocol.AMQProtocolSession;
+import org.apache.qpid.jms.ConnectionListener;
+import org.apache.qpid.test.utils.QpidBrokerTestCase;
+import org.apache.qpid.test.utils.FailoverBaseCase;
+import org.apache.qpid.AMQConnectionClosedException;
+
+import javax.jms.Destination;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Test case based on user reported error.
+ *
+ * Summary:
+ * A user has reported message loss from their application. On bouncing of
+ * the broker the 'lost' messages are delivered to the broker.
+ *
+ * Note:
+ * The client was using Spring so that may influence the situation.
+ *
+ * Issue:
+ * The log files show 7 instances of the following which result in 7
+ * missing messages.
+ *
+ * The client log files show:
+ *
+ * The broker log file show:
+ *
+ *
+ * 7 missing messages have delivery tags 5-11. Which says that they are
+ * sequentially the next message from the broker.
+ *
+ * The only way for the 'without a handler' log to occur is if the consumer
+ * has been removed from the look up table of the dispatcher.
+ * And the only way for the 'null message' log to occur on the broker is is
+ * if the message does not exist in the unacked-map
+ *
+ * The consumer is only removed from the list during session
+ * closure and failover.
+ *
+ * If the session was closed then the broker would requeue the unacked
+ * messages so the potential exists to have an empty map but the broker
+ * will not send a message out after the unacked map has been cleared.
+ *
+ * When failover occurs the _consumer map is cleared and the consumers are
+ * resubscribed. This is down without first stopping any existing
+ * dispatcher so there exists the potential to receive a message after
+ * the _consumer map has been cleared which is how the 'without a handler'
+ * log statement occurs.
+ *
+ * Scenario:
+ *
+ * Looking over logs the sequence that best fits the events is as follows:
+ * - Something causes Mina to be delayed causing the WriteTimoutException.
+ * - This exception is recevied by AMQProtocolHandler#exceptionCaught
+ * - As the WriteTimeoutException is an IOException this will cause
+ * sessionClosed to be called to start failover.
+ * + This is potentially the issues here. All IOExceptions are treated
+ * as connection failure events.
+ * - Failover Runs
+ * + Failover assumes that the previous connection has been closed.
+ * + Failover binds the existing objects (AMQConnection/Session) to the
+ * new connection objects.
+ * - Everything is reported as being successfully failed over.
+ * However, what is neglected is that the original connection has not
+ * been closed.
+ * + So what occurs is that the broker sends a message to the consumer on
+ * the original connection, as it was not notified of the client
+ * failing over.
+ * As the client failover reuses the original AMQSession and Dispatcher
+ * the new messages the broker sends to the old consumer arrives at the
+ * client and is processed by the same AMQSession and Dispatcher.
+ * However, as the failover process cleared the _consumer map and
+ * resubscribe the consumers the Dispatcher does not recognise the
+ * delivery tag and so logs the 'without a handler' message.
+ * - The Dispatcher then attempts to reject the message, however,
+ * + The AMQSession/Dispatcher pair have been swapped to using a new Mina
+ * ProtocolSession as part of the failover process so the reject is
+ * sent down the second connection. The broker receives the Reject
+ * request but as the Message was sent on a different connection the
+ * unacknowledgemap is empty and a 'message is null' log message
+ * produced.
+ *
+ * Test Strategy:
+ *
+ * It should be easy to demonstrate if we can send an IOException to
+ * AMQProtocolHandler#exceptionCaught and then try sending a message.
+ *
+ * The current unknowns here are the type of consumers that are in use.
+ * If it was an exclusive queue(Durable Subscription) then why did the
+ * resubscribe not fail.
+ *
+ * If it was not exclusive then why did the messages not round robin?
+ */
+public class MessageDisappearWithIOExceptionTest extends FailoverBaseCase implements ConnectionListener
+{
+ private CountDownLatch _failoverOccured = new CountDownLatch(1);
+ AMQConnection _connection;
+ Session _session;
+ Queue _queue;
+ MessageConsumer _consumer;
+
+ public void setUp() throws Exception
+ {
+ super.setUp();
+ stopBroker(getFailingPort());
+
+ }
+
+ /**
+ * Test Summary:
+ *
+ * Create a queue consumer and send 10 messages to the broker.
+ *
+ * Consume the first message.
+ * This will pull the rest into the prefetch
+ *
+ * Send an IOException to the MinaProtocolHandler.
+ *
+ * This will force failover to occur.
+ *
+ * 9 messages would normally be expected but it is expected that none will
+ * arrive. As they are still in the prefetch of the first session.
+ *
+ * To free the messages we need to close all connections.
+ * - Simply doing connection.close() and retesting will not be enough as
+ * the original connection's IO layer will still exist and is nolonger
+ * connected to the connection object as a result of failover.
+ *
+ * - Test will need to retain a reference to the original connection IO so
+ * that it can be closed releasing the messages to validate that the
+ * messages have indeed been 'lost' on that sesssion.
+ */
+ public void test() throws Exception
+ {
+ initialiseConnection();
+
+ // Create Producer
+ // Send 10 messages
+ List<Message> messages = sendNumberedBytesMessage(_session, _queue, 10);
+
+ // Consume first messasge
+ Message received = _consumer.receive(2000);
+
+ // Verify received messages
+ assertNotNull("First message not received.", received);
+ assertEquals("Incorrect message Received",
+ messages.remove(0).getIntProperty("count"),
+ received.getIntProperty("count"));
+
+ // When the Exception is received by the underlying IO layer it will
+ // initiate failover. The first step of which is to ensure that the
+ // existing conection is closed. So in this situation the connection
+ // will be flushed casuing the above ACK to be sent to the broker.
+ //
+ // That said:
+ // when the socket close is detected on the server it will rise up the
+ // Mina filter chain and interrupt processing.
+ // this has been raised as QPID-2138
+ _session.createConsumer(_session.createTemporaryQueue()).close();
+
+ //Retain IO Layer
+ AMQProtocolSession protocolSession = _connection.getProtocolHandler().getProtocolSession();
+
+ // Send IO Exception - causing failover
+ _connection.getProtocolHandler().
+ exception(new WriteTimeoutException("WriteTimeoutException to cause failover."));
+
+ // Verify Failover occured through ConnectionListener
+ assertTrue("Failover did not occur",
+ _failoverOccured.await(4000, TimeUnit.MILLISECONDS));
+
+ /***********************************/
+ // This verifies that the bug has been resolved
+
+ // Attempt to consume again. Expect 9 messages
+ for (int count = 1; count < 10; count++)
+ {
+ received = _consumer.receive(2000);
+ assertNotNull("Expected message not received:" + count, received);
+ assertEquals(messages.remove(0).getIntProperty("count"),
+ received.getIntProperty("count"));
+ }
+
+ //Verify there are no more messages
+ received = _consumer.receive(1000);
+ assertNull("Message receieved when there should be none:" + received,
+ received);
+
+// /***********************************/
+// // This verifies that the bug exists
+//
+// // Attempt to consume remaining 9 messages.. Expecting NONE.
+// // receiving just one message should fail so no need to fail 9 times
+// received = _consumer.receive(1000);
+// assertNull("Message receieved when it should be null:" + received, received);
+//
+//// //Close the Connection which you would assume would free the messages
+//// _connection.close();
+////
+//// // Reconnect
+//// initialiseConnection();
+////
+//// // We should still be unable to receive messages
+//// received = _consumer.receive(1000);
+//// assertNull("Message receieved when it should be null:" + received, received);
+////
+//// _connection.close();
+//
+// // Close original IO layer. Expecting messages to be released
+// protocolSession.closeProtocolSession();
+//
+// // Reconnect and all should be good.
+//// initialiseConnection();
+//
+// // Attempt to consume again. Expect 9 messages
+// for (int count = 1; count < 10; count++)
+// {
+// received = _consumer.receive(2000);
+// assertNotNull("Expected message not received:" + count, received);
+// assertEquals(messages.remove(0).getIntProperty("count"),
+// received.getIntProperty("count"));
+// }
+//
+// //Verify there are no more messages
+// received = _consumer.receive(1000);
+// assertNull("Message receieved when there should be none:" + received,
+// received);
+ }
+
+ private void initialiseConnection()
+ throws Exception
+ {
+ //Create Connection using the default connection URL. i.e. not the Failover URL that would be used by default
+ _connection = (AMQConnection) getConnectionFactory("default").createConnection("guest", "guest");
+ // The default connection does not have any retries configured so
+ // Allow this connection to retry so that we can block on the failover.
+ // The alternative would be to use the getConnection() default. However,
+ // this would add additional complexity in the logging as a second
+ // broker is defined in that url. We do not need it for this test.
+ _connection.getFailoverPolicy().getCurrentMethod().setRetries(1);
+ _connection.setConnectionListener(this);
+
+ _session = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ _queue = _session.createQueue(getTestQueueName());
+
+ // Create Consumer
+ _consumer = _session.createConsumer(_queue);
+
+ //Start connection
+ _connection.start();
+ }
+
+ /** QpidTestCase back port to this release */
+
+ // modified from QTC as sendMessage is not testable.
+ // - should be renamed sendBlankBytesMessage
+ // - should be renamed sendNumberedBytesMessage
+ public List<Message> sendNumberedBytesMessage(Session session, Destination destination,
+ int count) throws Exception
+ {
+ List<Message> messages = new ArrayList<Message>(count);
+
+ MessageProducer producer = session.createProducer(destination);
+
+ for (int i = 0; i < count; i++)
+ {
+ Message next = session.createMessage();
+
+ next.setIntProperty("count", i);
+
+ producer.send(next);
+
+ messages.add(next);
+ }
+
+ producer.close();
+ return messages;
+ }
+
+ public void bytesSent(long count)
+ {
+ //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public void bytesReceived(long count)
+ {
+ }
+
+ public boolean preFailover(boolean redirect)
+ {
+ //Allow failover to occur
+ return true;
+ }
+
+ public boolean preResubscribe()
+ {
+ //Allow failover to occur
+ return true;
+ }
+
+ public void failoverComplete()
+ {
+ _failoverOccured.countDown();
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/failure/HeapExhaustion.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/failure/HeapExhaustion.java
new file mode 100644
index 0000000000..22a1b119fa
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/failure/HeapExhaustion.java
@@ -0,0 +1,236 @@
+/*
+ *
+ * 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.failure;
+
+import junit.framework.TestCase;
+import org.apache.qpid.test.utils.QpidClientConnectionHelper;
+import org.apache.qpid.client.failover.FailoverException;
+import org.apache.qpid.AMQException;
+import org.apache.qpid.protocol.AMQConstant;
+import org.apache.log4j.Logger;
+
+import javax.jms.JMSException;
+import javax.jms.DeliveryMode;
+import java.io.IOException;
+
+
+/** Test Case provided by client Non-functional Test NF101: heap exhaustion behaviour */
+public class HeapExhaustion extends TestCase
+{
+ private static final Logger _logger = Logger.getLogger(HeapExhaustion.class);
+
+ protected QpidClientConnectionHelper conn;
+ protected final String BROKER = "localhost";
+ protected final String vhost = "/test";
+ protected final String queue = "direct://amq.direct//queue";
+
+ protected String hundredK;
+ protected String megabyte;
+
+ protected String generatePayloadOfSize(Integer numBytes)
+ {
+ return new String(new byte[numBytes]);
+ }
+
+ protected void setUp() throws Exception
+ {
+ conn = new QpidClientConnectionHelper(BROKER);
+ conn.setVirtualHost(vhost);
+
+ try
+ {
+ conn.connect();
+ } catch (JMSException e)
+ {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+ // clear queue
+ _logger.debug("setup: clearing test queue");
+ conn.consume(queue, 2000);
+
+ hundredK = generatePayloadOfSize(1024 * 100);
+ megabyte = generatePayloadOfSize(1024 * 1024);
+ }
+
+ protected void tearDown() throws Exception
+ {
+ conn.disconnect();
+ }
+
+
+ /**
+ * PUT at maximum rate (although we commit after each PUT) until failure
+ *
+ * @throws Exception on error
+ */
+ public void testUntilFailureTransient() throws Exception
+ {
+ int copies = 0;
+ int total = 0;
+ String payload = hundredK;
+ int size = payload.getBytes().length;
+ while (true)
+ {
+ conn.put(queue, payload, 1, DeliveryMode.NON_PERSISTENT);
+ copies++;
+ total += size;
+ System.out.println("put copy " + copies + " OK for total bytes: " + total);
+ }
+ }
+
+ /**
+ * PUT at lower rate (5 per second) until failure
+ *
+ * @throws Exception on error
+ */
+ public void testUntilFailureWithDelaysTransient() throws Exception
+ {
+ int copies = 0;
+ int total = 0;
+ String payload = hundredK;
+ int size = payload.getBytes().length;
+ while (true)
+ {
+ conn.put(queue, payload, 1, DeliveryMode.NON_PERSISTENT);
+ copies++;
+ total += size;
+ System.out.println("put copy " + copies + " OK for total bytes: " + total);
+ Thread.sleep(200);
+ }
+ }
+
+ public static void noDelay()
+ {
+ HeapExhaustion he = new HeapExhaustion();
+
+ try
+ {
+ he.setUp();
+ }
+ catch (Exception e)
+ {
+ _logger.info("Unable to connect");
+ System.exit(0);
+ }
+
+ try
+ {
+ _logger.info("Running testUntilFailure");
+ try
+ {
+ he.testUntilFailureTransient();
+ }
+ catch (FailoverException fe)
+ {
+ _logger.error("Caught failover:" + fe);
+ }
+ _logger.info("Finishing Connection ");
+
+ try
+ {
+ he.tearDown();
+ }
+ catch (JMSException jmse)
+ {
+ if (((AMQException) jmse.getLinkedException()).getErrorCode() == AMQConstant.REQUEST_TIMEOUT)
+ {
+ _logger.info("Successful test of testUntilFailure");
+ }
+ else
+ {
+ _logger.error("Test Failed due to:" + jmse);
+ }
+ }
+ }
+ catch (Exception e)
+ {
+ _logger.error("Test Failed due to:" + e);
+ }
+ }
+
+ public static void withDelay()
+ {
+ HeapExhaustion he = new HeapExhaustion();
+
+ try
+ {
+ he.setUp();
+ }
+ catch (Exception e)
+ {
+ _logger.info("Unable to connect");
+ System.exit(0);
+ }
+
+ try
+ {
+ _logger.info("Running testUntilFailure");
+ try
+ {
+ he.testUntilFailureWithDelaysTransient();
+ }
+ catch (FailoverException fe)
+ {
+ _logger.error("Caught failover:" + fe);
+ }
+ _logger.info("Finishing Connection ");
+
+ try
+ {
+ he.tearDown();
+ }
+ catch (JMSException jmse)
+ {
+ if (((AMQException) jmse.getLinkedException()).getErrorCode() == AMQConstant.REQUEST_TIMEOUT)
+ {
+ _logger.info("Successful test of testUntilFailure");
+ }
+ else
+ {
+ _logger.error("Test Failed due to:" + jmse);
+ }
+ }
+ }
+ catch (Exception e)
+ {
+ _logger.error("Test Failed due to:" + e);
+ }
+ }
+
+ public static void main(String args[])
+ {
+ noDelay();
+
+
+ try
+ {
+ System.out.println("Restart failed broker now to retest broker with delays in send.");
+ System.in.read();
+ }
+ catch (IOException e)
+ {
+ _logger.info("Continuing");
+ }
+
+ withDelay();
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/AbstractTestLogging.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/AbstractTestLogging.java
new file mode 100644
index 0000000000..f56f428f0b
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/AbstractTestLogging.java
@@ -0,0 +1,448 @@
+/*
+ *
+ * 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.logging;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.qpid.server.configuration.ServerConfiguration;
+import org.apache.qpid.server.logging.subjects.AbstractTestLogSubject;
+import org.apache.qpid.server.registry.ApplicationRegistry;
+import org.apache.qpid.server.util.InternalBrokerBaseCase;
+import org.apache.qpid.test.utils.QpidBrokerTestCase;
+import org.apache.qpid.util.LogMonitor;
+
+/**
+ * Abstract superclass for logging test set up and utility methods.
+ *
+ * So named to prevent it being selected itself as a test to run by the test suite.
+ */
+public class AbstractTestLogging extends QpidBrokerTestCase
+{
+ public static final long DEFAULT_LOG_WAIT = 2000;
+ public static final String TEST_LOG_PREFIX = "MESSAGE";
+ protected LogMonitor _monitor;
+
+ InternalBrokerBaseCase _configLoader;
+
+ @Override
+ public void setUp() throws Exception
+ {
+ setLogMessagePrefix();
+
+ super.setUp();
+ _monitor = new LogMonitor(_outputFile);
+ }
+
+ protected ServerConfiguration getServerConfig() throws ConfigurationException
+ {
+ ServerConfiguration _serverConfiguration;
+ if (isExternalBroker())
+ {
+ _serverConfiguration = new ServerConfiguration(_configFile)
+ {
+ @Override
+ public void initialise() throws ConfigurationException
+ {
+ //Overriding initialise to only setup the vhosts and not
+ //perform the ConfigurationPlugin setup, removing need for
+ //an ApplicationRegistry to be loaded.
+ setupVirtualHosts(getConfig());
+ }
+ };
+ _serverConfiguration.initialise();
+ }
+ else
+ {
+ _serverConfiguration = ApplicationRegistry.getInstance().getConfiguration();
+ }
+
+ return _serverConfiguration;
+ }
+
+ protected void setLogMessagePrefix()
+ {
+ //set the message prefix to facilitate scraping from the munged test output.
+ setSystemProperty("qpid.logging.prefix", TEST_LOG_PREFIX);
+ }
+
+ @Override
+ public void tearDown() throws Exception
+ {
+ _monitor.close();
+ if (isExternalBroker() && _configLoader != null)
+ {
+ _configLoader.tearDown();
+ }
+ super.tearDown();
+ }
+
+ /**
+ * assert that the requested log message has not occured
+ *
+ * @param log
+ *
+ * @throws IOException
+ */
+ public void assertLoggingNotYetOccured(String log) throws IOException
+ {
+ // Ensure the alert has not occured yet
+ assertEquals("Message has already occured:" + log, 0,
+ findMatches(log).size());
+ }
+
+ protected void validateMessageID(String id, String log)
+ {
+ assertEquals("Incorrect message", id, getMessageID(log));
+ }
+
+ protected String getMessageID(String log)
+ {
+ String message = fromMessage(log);
+
+ return message.substring(0, message.indexOf(" "));
+ }
+
+ /**
+ * Return the first channel id from the log string
+ * ' ch;X' if there is no channel id return -1.
+ *
+ * @param log the log string to search.
+ *
+ * @return channel id or -1 if no channel id exists.
+ */
+ protected int getChannelID(String log)
+ {
+ int start = log.indexOf("ch:") + 3;
+
+ // If we do a check for ] as the boundary we will get cases where log
+ // is presented with the bounding. If we don't match a ] then we can use
+ // the end of the string as the boundary.
+ int end = log.indexOf("]", start);
+ if (end == -1)
+ {
+ end = log.length();
+ }
+
+ try
+ {
+ return Integer.parseInt(log.substring(start, end));
+ }
+ catch (Exception e)
+ {
+ return -1;
+ }
+ }
+
+ protected String fromMessage(String log)
+ {
+ int startSubject = log.indexOf("]") + 1;
+ int start = log.indexOf("]", startSubject) + 1;
+
+ // If we don't have a subject then the second indexOf will return 0
+ // in which case we can use the end of the actor as the index.
+ if (start == 0)
+ {
+ start = startSubject;
+ }
+
+ return log.substring(start).trim();
+ }
+
+ /**
+ * Extract the Subject from the Log Message.
+ *
+ * The subject is the second block inclosed in brackets '[ ]'.
+ *
+ * If there is no Subject or the second block of brackets '[ ]' cannot be
+ * identified then an empty String ("") is returned.
+ *
+ * The brackets '[ ]' are not included in the returned String.
+ *
+ * @param log The log message to process
+ *
+ * @return the Subject string or the empty string ("") if the subject can't be identified.
+ */
+ protected String fromSubject(String log)
+ {
+ int start = log.indexOf("[") + 1;
+ // Take the second index
+ start = log.indexOf("[", start) + 1;
+
+ // There may not be a subject so in that case return nothing.
+ if (start == 0)
+ {
+ return "";
+ }
+
+ int end = log.indexOf("]", start);
+ try
+ {
+ return log.substring(start, end);
+ }
+ catch (IndexOutOfBoundsException iobe)
+ {
+ return "";
+ }
+ }
+
+ /**
+ * Extract the actor segment from the log message.
+ * The Actor segment is the first section enclosed in '[ ]'.
+ *
+ * No analysis is performed to ensure that the first '[ ]' section of the
+ * given log is really an Actor segment.
+ *
+ * The brackets '[ ]' are not included in the returned String.
+ *
+ * @param log the Log Message
+ *
+ * @return the Actor segment or "" if unable to locate '[ ]' section
+ */
+ protected String fromActor(String log)
+ {
+ int start = log.indexOf("[") + 1;
+ int end = log.indexOf("]", start);
+ try
+ {
+ return log.substring(start, end).trim();
+ }
+ catch (IndexOutOfBoundsException iobe)
+ {
+ return "";
+ }
+ }
+
+ /**
+ * Return the message String from the given message section
+ *
+ * @param log the Message Section
+ *
+ * @return the Message String.
+ */
+ protected String getMessageString(String log)
+ {
+ // Remove the Log ID from the returned String
+ int start = log.indexOf(":") + 1;
+
+ return log.substring(start).trim();
+ }
+
+ /**
+ * Given our log message extract the connection ID:
+ *
+ * The log string will contain the connectionID identified by 'con:'
+ *
+ * So extract the value shown here by X:
+ *
+ * 'con:X('
+ *
+ * Extract the value between the ':' and '(' and process it as an Integer
+ *
+ * If we are unable to find the right index or process the substring as an
+ * Integer then return -1.
+ *
+ * @param log the log String to process
+ *
+ * @return the connection ID or -1.
+ */
+ protected int getConnectionID(String log)
+ {
+ int conIDStart = log.indexOf("con:") + 4;
+ int conIDEnd = log.indexOf("(", conIDStart);
+ try
+ {
+ return Integer.parseInt(log.substring(conIDStart, conIDEnd));
+ }
+ catch (Exception e)
+ {
+ return -1;
+ }
+ }
+
+ /**
+ * Extract the log entry from the raw log line which will contain other
+ * log4j formatting.
+ *
+ * This formatting may impead our testing process so extract the log message
+ * as we know it to be formatted.
+ *
+ * This starts with the string MESSAGE
+ *
+ * @param rawLog the raw log
+ *
+ * @return the log we are expecting to be printed without the log4j prefixes
+ */
+ protected String getLog(String rawLog)
+ {
+ int start = rawLog.indexOf(TEST_LOG_PREFIX);
+ return rawLog.substring(start);
+ }
+
+ /**
+ * Extract the log entry from the result set. Positions are 0-based.
+ *
+ * @param results list of log message results to extract from
+ * @param position position in the list of the message to extract
+ * @return the message string
+ */
+ protected String getLogMessage(List<String> results, int position)
+ {
+ return getLog(results.get(position));
+ }
+
+ /**
+ * Extract the nth-from-last log entry from the result set.
+ *
+ * @param results list of log message results to extract from
+ * @param positionFromEnd position from end of the message list to extract (eg 0 for last)
+ * @return the message string
+ */
+ protected String getLogMessageFromEnd(List<String> results, int positionFromEnd)
+ {
+ int resultSize = results.size();
+ return getLogMessage(results, resultSize - 1 - positionFromEnd);
+ }
+
+ protected List<String> findMatches(String toFind) throws IOException
+ {
+ return _monitor.findMatches(toFind);
+ }
+
+ protected List<String> waitAndFindMatches(String toFind) throws IOException
+ {
+ return waitAndFindMatches(toFind, DEFAULT_LOG_WAIT);
+ }
+
+ protected List<String> waitAndFindMatches(String toFind, long wait) throws IOException
+ {
+ return _monitor.waitAndFindMatches(toFind, wait);
+ }
+
+ public boolean waitForMessage(String message) throws FileNotFoundException, IOException
+ {
+ return waitForMessage(message, DEFAULT_LOG_WAIT);
+ }
+
+ public boolean waitForMessage(String message, long wait) throws FileNotFoundException, IOException
+ {
+ return _monitor.waitForMessage(message, wait, true);
+ }
+
+ /**
+ * Given a list of messages that have been pulled out of a log file
+ * Process the results splitting the log statements in to lists based on the
+ * actor's connection ID.
+ *
+ * So for each log entry extract the Connecition ID from the Actor of the log
+ *
+ * Then use that as a key to a HashMap storing the list of log messages for
+ * that connection.
+ *
+ * @param logMessages The list of mixed connection log messages
+ *
+ * @return Map indexed by connection id to a list of log messages just for that connection.
+ */
+ protected HashMap<Integer, List<String>> splitResultsOnConnectionID(List<String> logMessages)
+ {
+ HashMap<Integer, List<String>> connectionSplitList = new HashMap<Integer, List<String>>();
+
+ for (String log : logMessages)
+ {
+ // Get the connectionID from the Actor in the Message Log.
+ int cID = getConnectionID(fromActor(getLog(log)));
+
+ List<String> connectionData = connectionSplitList.get(cID);
+
+ // Create the initial List if we don't have one already
+ if (connectionData == null)
+ {
+ connectionData = new LinkedList<String>();
+ connectionSplitList.put(cID, connectionData);
+ }
+
+ // Store the log
+ connectionData.add(log);
+ }
+
+ return connectionSplitList;
+ }
+
+ /**
+ * Filter the give result set by the specficifed virtualhost.
+ * This is done using the getSlice to identify the virtualhost (vh) in the
+ * log message
+ *
+ * @param results full list of logs
+ * @param virtualHostName the virtualhostName to filter on
+ *
+ * @return the list of messages only for that virtualhost
+ */
+ protected List<String> filterResultsByVirtualHost(List<String> results, String virtualHostName)
+ {
+ List<String> filteredResults = new LinkedList<String>();
+ Iterator<String> iterator = results.iterator();
+
+ while (iterator.hasNext())
+ {
+ String log = iterator.next();
+
+ if (AbstractTestLogSubject.getSlice("vh", log).equals(virtualHostName))
+ {
+ filteredResults.add(log);
+ }
+ }
+
+ return filteredResults;
+ }
+
+ /**
+ * Dump the log results.
+ */
+ protected void dumpLogs(List<String> results) throws IOException
+ {
+ dumpLogs(results, null);
+ }
+
+ /**
+ * Dump the log results or if there are none, the contents of the
+ * monitored log file if the monitor is non-null.
+ */
+ protected void dumpLogs(List<String> results, LogMonitor monitor) throws IOException
+ {
+ System.err.println("Log Dump:");
+ for (String log : results)
+ {
+ System.err.println(log);
+ }
+
+ if (results.isEmpty() && monitor != null)
+ {
+ System.err.println("Monitored file contents:");
+ System.err.println(monitor.readFile());
+ }
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/AccessControlLoggingTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/AccessControlLoggingTest.java
new file mode 100644
index 0000000000..2629e82831
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/AccessControlLoggingTest.java
@@ -0,0 +1,174 @@
+/*
+ * 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.logging;
+
+import java.io.File;
+import java.util.List;
+
+import javax.jms.Connection;
+import javax.jms.JMSException;
+import javax.jms.Session;
+
+import org.apache.qpid.AMQException;
+import org.apache.qpid.client.AMQSession;
+import org.apache.qpid.framing.AMQShortString;
+import org.apache.qpid.protocol.AMQConstant;
+
+/**
+ * ACL version 2/3 file testing to verify that ACL actor logging works correctly.
+ *
+ * This suite of tests validate that the AccessControl messages occur correctly
+ * and according to the following format:
+ *
+ * <pre>
+ * ACL-1001 : Allowed Operation Object {PROPERTIES}
+ * ACL-1002 : Denied Operation Object {PROPERTIES}
+ * </pre>
+ */
+public class AccessControlLoggingTest extends AbstractTestLogging
+{
+ private static final String ACL_LOG_PREFIX = "ACL-";
+ private static final String USER = "client";
+ private static final String PASS = "guest";
+
+ public void setUp() throws Exception
+ {
+ setConfigurationProperty("virtualhosts.virtualhost.test.security.aclv2",
+ QpidHome + File.separator + "etc" + File.separator + "test-logging.txt");
+
+ super.setUp();
+ }
+
+ /** FIXME This comes from SimpleACLTest and makes me suspicious. */
+ @Override
+ public void tearDown() throws Exception
+ {
+ try
+ {
+ super.tearDown();
+ }
+ catch (JMSException e)
+ {
+ //we're throwing this away as it can happen in this test as the state manager remembers exceptions
+ //that we provoked with authentication failures, where the test passes - we can ignore on con close
+ }
+ }
+
+ /**
+ * Test that {@code allow} ACL entries do not log anything.
+ */
+ public void testAllow() throws Exception
+ {
+ Connection conn = getConnection(USER, PASS);
+ Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ conn.start();
+ ((AMQSession<?, ?>) sess).createQueue(new AMQShortString("allow"), false, false, false);
+
+ List<String> matches = findMatches(ACL_LOG_PREFIX);
+
+ assertTrue("Should be no ACL log messages", matches.isEmpty());
+ }
+
+ /**
+ * Test that {@code allow-log} ACL entries log correctly.
+ */
+ public void testAllowLog() throws Exception
+ {
+ Connection conn = getConnection(USER, PASS);
+ Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ conn.start();
+ ((AMQSession<?, ?>) sess).createQueue(new AMQShortString("allow-log"), false, false, false);
+
+ List<String> matches = findMatches(ACL_LOG_PREFIX);
+
+ assertEquals("Should only be one ACL log message", 1, matches.size());
+
+ String log = getLogMessage(matches, 0);
+ String actor = fromActor(log);
+ String subject = fromSubject(log);
+ String message = getMessageString(fromMessage(log));
+
+ validateMessageID(ACL_LOG_PREFIX + 1001, log);
+
+ assertTrue("Actor should contain the user identity", actor.contains(USER));
+ assertTrue("Subject should be empty", subject.length() == 0);
+ assertTrue("Message should start with 'Allowed'", message.startsWith("Allowed"));
+ assertTrue("Message should contain 'Create Queue'", message.contains("Create Queue"));
+ assertTrue("Message should have contained the queue name", message.contains("allow-log"));
+ }
+
+ /**
+ * Test that {@code deny-log} ACL entries log correctly.
+ */
+ public void testDenyLog() throws Exception
+ {
+ Connection conn = getConnection(USER, PASS);
+ Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ conn.start();
+ try {
+ ((AMQSession<?, ?>) sess).createQueue(new AMQShortString("deny-log"), false, false, false);
+ fail("Should have denied queue creation");
+ }
+ catch (AMQException amqe)
+ {
+ // Denied, so exception thrown
+ assertEquals("Expected ACCESS_REFUSED error code", AMQConstant.ACCESS_REFUSED, amqe.getErrorCode());
+ }
+
+ List<String> matches = findMatches(ACL_LOG_PREFIX);
+
+ assertEquals("Should only be one ACL log message", 1, matches.size());
+
+ String log = getLogMessage(matches, 0);
+ String actor = fromActor(log);
+ String subject = fromSubject(log);
+ String message = getMessageString(fromMessage(log));
+
+ validateMessageID(ACL_LOG_PREFIX + 1002, log);
+
+ assertTrue("Actor should contain the user identity", actor.contains(USER));
+ assertTrue("Subject should be empty", subject.length() == 0);
+ assertTrue("Message should start with 'Denied'", message.startsWith("Denied"));
+ assertTrue("Message should contain 'Create Queue'", message.contains("Create Queue"));
+ assertTrue("Message should have contained the queue name", message.contains("deny-log"));
+ }
+
+ /**
+ * Test that {@code deny} ACL entries do not log anything.
+ */
+ public void testDeny() throws Exception
+ {
+ Connection conn = getConnection(USER, PASS);
+ Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ conn.start();
+ try {
+ ((AMQSession<?, ?>) sess).createQueue(new AMQShortString("deny"), false, false, false);
+ fail("Should have denied queue creation");
+ }
+ catch (AMQException amqe)
+ {
+ // Denied, so exception thrown
+ assertEquals("Expected ACCESS_REFUSED error code", AMQConstant.ACCESS_REFUSED, amqe.getErrorCode());
+ }
+
+ List<String> matches = findMatches(ACL_LOG_PREFIX);
+
+ assertTrue("Should be no ACL log messages", matches.isEmpty());
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/AlertingTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/AlertingTest.java
new file mode 100644
index 0000000000..05aaf16af1
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/AlertingTest.java
@@ -0,0 +1,202 @@
+/*
+*
+* 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.logging;
+
+import javax.jms.Connection;
+import javax.jms.Queue;
+import javax.jms.Session;
+
+import org.apache.qpid.client.AMQDestination;
+import org.apache.qpid.client.AMQSession;
+import org.apache.qpid.framing.AMQShortString;
+import org.apache.qpid.server.configuration.ServerConfiguration;
+import org.apache.qpid.server.registry.ApplicationRegistry;
+import org.apache.qpid.util.FileUtils;
+
+public class AlertingTest extends AbstractTestLogging
+{
+ private String VIRTUALHOST = "test";
+ private Session _session;
+ private Connection _connection;
+ private Queue _destination;
+ private int _numMessages;
+
+ private static final int ALERT_LOG_WAIT_PERIOD = 5000;
+ private static final String MESSAGE_COUNT_ALERT = "MESSAGE_COUNT_ALERT";
+
+ public void setUp() throws Exception
+ {
+ // Update the configuration to make our virtualhost Persistent.
+ makeVirtualHostPersistent(VIRTUALHOST);
+ setConfigurationProperty("virtualhosts.virtualhost." + VIRTUALHOST + ".housekeeping.expiredMessageCheckPeriod", "5000");
+
+ _numMessages = 50;
+
+ // Then we do the normal setup stuff like starting the broker, getting a connection etc.
+ super.setUp();
+
+ setupConnection();
+ }
+
+ @Override
+ public void tearDown() throws Exception
+ {
+ // Ensure queue is clean for next run.
+ drainQueue(_destination);
+ super.tearDown();
+ }
+
+
+ /**
+ * Create a new connection and ensure taht our destination queue is created
+ * and bound.
+ *
+ * Note that the tests here that restart the broker rely on persistence.
+ * However, the queue creation here is transient. So the queue will not be
+ * rebound on restart. Hence the consumer creation here rather than just the
+ * once.
+ *
+ * The persistent messages will recreate the queue but not bind it (as it
+ * was not a durable queue) However, the consumer creation here will ensure
+ * that the queue is correctly bound and can receive new messages.
+ *
+ * @throws Exception
+ */
+ private void setupConnection()
+ throws Exception
+ {
+ _connection = getConnection();
+ _session = _connection.createSession(true, Session.SESSION_TRANSACTED);
+ _destination = _session.createQueue(getTestQueueName());
+
+ // Consumer is only used to actually create the destination
+ _session.createConsumer(_destination).close();
+ }
+
+ /**
+ * Checks the log file for MESSAGE_COUNT_ALERT, fails() the test if it's not found and
+ * places the entire contents in the message to help debug cruise control failures.
+ *
+ * @throws Exception
+ */
+ private void wasAlertFired() throws Exception
+ {
+ if (!waitForMessage(MESSAGE_COUNT_ALERT, ALERT_LOG_WAIT_PERIOD))
+ {
+ StringBuffer message = new StringBuffer("Could not find 'MESSAGE_COUNT_ALERT' in log file: " + _monitor.getMonitoredFile().getAbsolutePath());
+ message.append("\n");
+
+ // Add the current contents of the log file to test output
+ message.append(_monitor.readFile());
+
+ // Write the test config file to test output
+ message.append("Server configuration overrides in use:\n");
+ message.append(FileUtils.readFileAsString(getTestConfigFile()));
+
+ message.append("\nVirtualhost maxMessageCount:\n");
+ ServerConfiguration config = new ServerConfiguration(_configFile);
+ config.initialise();
+ message.append(config.getVirtualHostConfig(VIRTUALHOST).getMaximumMessageCount());
+
+ fail(message.toString());
+ }
+ }
+
+ public void testAlertingReallyWorks() throws Exception
+ {
+ // Send 5 messages, make sure that the alert was fired properly.
+ sendMessage(_session, _destination, _numMessages + 1);
+ _session.commit();
+ wasAlertFired();
+ }
+
+ public void testAlertingReallyWorksWithRestart() throws Exception
+ {
+ sendMessage(_session, _destination, _numMessages + 1);
+ _session.commit();
+ _connection.close();
+ stopBroker();
+
+ // Rest the monitoring clearing the current output file.
+ _monitor.reset();
+ startBroker();
+ wasAlertFired();
+ }
+
+ /**
+ * Test that if the alert value is change from the previous value we can
+ * still get alerts.
+ *
+ * Test sends two messages to the broker then restarts the broker with new
+ * configuration.
+ *
+ * If the test is running inVM the test validates that the new configuration
+ * has been applied.
+ *
+ * Validates that we only have two messages on the queue and then sends
+ * enough messages to trigger the alert.
+ *
+ * The alert is then validate.
+ *
+ *
+ * @throws Exception
+ */
+ public void testAlertingReallyWorksWithChanges() throws Exception
+ {
+ // send some messages and nuke the logs
+ sendMessage(_session, _destination, 2);
+ _session.commit();
+ // To prevent any failover/retry/connection dropped errors
+ _connection.close();
+
+ stopBroker();
+
+ _monitor.reset();
+
+ // Change max message count to 5, start broker and make sure that that's triggered at the right time
+ setConfigurationProperty("virtualhosts.virtualhost." + VIRTUALHOST + ".queues.maximumMessageCount", "5");
+
+ startBroker();
+
+ if (!isExternalBroker())
+ {
+ assertEquals("Alert Max Msg Count is not correct", 5, ApplicationRegistry.getInstance().getVirtualHostRegistry().
+ getVirtualHost(VIRTUALHOST).getQueueRegistry().getQueue(new AMQShortString(_destination.getQueueName())).
+ getMaximumMessageCount());
+ }
+
+ setupConnection();
+
+ // Validate the queue depth is as expected
+ long messageCount = ((AMQSession<?, ?>) _session).getQueueDepth((AMQDestination) _destination);
+ assertEquals("Broker has invalid message count for test", 2, messageCount);
+
+ // Ensure the alert has not occured yet
+ assertLoggingNotYetOccured(MESSAGE_COUNT_ALERT);
+
+ // Trigger the new value
+ sendMessage(_session, _destination, 3);
+ _session.commit();
+
+ // Validate that the alert occured.
+ wasAlertFired();
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/BindingLoggingTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/BindingLoggingTest.java
new file mode 100644
index 0000000000..97914f84a5
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/BindingLoggingTest.java
@@ -0,0 +1,271 @@
+/*
+ *
+ * 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.logging;
+
+import org.apache.qpid.server.logging.subjects.AbstractTestLogSubject;
+
+import javax.jms.Connection;
+import javax.jms.JMSException;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.jms.Topic;
+import java.io.IOException;
+import java.util.List;
+
+/**
+ * Binding
+ *
+ * The Binding test suite validates that the follow log messages as specified in the Functional Specification.
+ *
+ * This suite of tests validate that the Binding messages occur correctly and according to the following format:
+ *
+ * BND-1001 : Create [: Arguments : <key=value>]
+ * BND-1002 : Deleted
+ */
+public class BindingLoggingTest extends AbstractTestLogging
+{
+
+ static final String BND_PREFIX = "BND-";
+
+ Connection _connection;
+ Session _session;
+ Queue _queue;
+ Topic _topic;
+
+ @Override
+ public void setUp() throws Exception
+ {
+ super.setUp();
+ //Ignore broker startup messages
+ _monitor.reset();
+
+ _connection = getConnection();
+
+ _session = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ _queue = _session.createQueue(getName());
+ _topic = (Topic) getInitialContext().lookup(TOPIC);
+ }
+
+ private void validateLogMessage(String log, String messageID, String message, String exchange, String rkey, String queueName)
+ {
+ validateMessageID(messageID, log);
+
+ String subject = fromSubject(log);
+
+ assertEquals("Queue not correct.", queueName,
+ AbstractTestLogSubject.getSlice("qu", subject));
+ assertEquals("Routing Key not correct.", rkey,
+ AbstractTestLogSubject.getSlice("rk", subject));
+ assertEquals("Virtualhost not correct.", "/test",
+ AbstractTestLogSubject.getSlice("vh", subject));
+ assertEquals("Exchange not correct.", exchange,
+ AbstractTestLogSubject.getSlice("ex", subject));
+
+ assertEquals("Log Message not as expected", message, getMessageString(fromMessage(log)));
+ }
+
+ /**
+ * testBindingCreate
+ *
+ * Description:
+ * The binding of a Queue and an Exchange is done via a Binding. When this Binding is created a BND-1001 Create message will be logged.
+ * Input:
+ *
+ * 1. Running Broker
+ * 2. New Client requests that a Queue is bound to a new exchange.
+ * Output:
+ *
+ * <date> BND-1001 : Create : Arguments : {x-filter-jms-selector=}
+ *
+ * Validation Steps:
+ * 3. The BND ID is correct
+ * 4. This will be the first message for the given binding
+ */
+ public void testBindingCreate() throws JMSException, IOException
+ {
+ _session.createConsumer(_queue).close();
+
+ List<String> results = waitAndFindMatches(BND_PREFIX);
+
+ // We will have two binds as we bind all queues to the default exchange
+ assertEquals("Result set larger than expected.", 2, results.size());
+
+ String exchange = "direct/<<default>>";
+ String messageID = "BND-1001";
+ String message = "Create";
+ String queueName = _queue.getQueueName();
+
+ validateLogMessage(getLogMessage(results, 0), messageID, message, exchange, queueName, queueName);
+
+ exchange = "direct/amq.direct";
+ message = "Create : Arguments : {x-filter-jms-selector=}";
+ validateLogMessage(getLogMessage(results, 1), messageID, message, exchange, queueName, queueName);
+ }
+
+ /**
+ * Description:
+ * A Binding can be made with a set of arguments. When this occurs we logged the key,value pairs as part of the Binding log message. When the subscriber with a JMS Selector consumes from an exclusive queue such as a topic. The binding is made with the JMS Selector as an argument.
+ * Input:
+ *
+ * 1. Running Broker
+ * 2. Java Client consumes from a topic with a JMS selector.
+ * Output:
+ *
+ * <date> BND-1001 : Create : Arguments : {x-filter-jms-selector=<value>}
+ *
+ * Validation Steps:
+ * 3. The BND ID is correct
+ * 4. The JMS Selector argument is present in the message
+ * 5. This will be the first message for the given binding
+ */
+ public void testBindingCreateWithArguments() throws JMSException, IOException
+ {
+ final String SELECTOR = "Selector='True'";
+
+ _session.createDurableSubscriber(_topic, getName(), SELECTOR, false).close();
+
+ List<String> results = waitAndFindMatches(BND_PREFIX);
+
+ // We will have two binds as we bind all queues to the default exchange
+ assertEquals("Result set larger than expected.", 2, results.size());
+
+ //Verify the first entry is the default binding
+ String messageID = "BND-1001";
+ String message = "Create";
+
+ validateLogMessage(getLogMessage(results, 0), messageID, message,
+ "direct/<<default>>", "clientid:" + getName(), "clientid:" + getName());
+
+ //Default binding will be without the selector
+ assertTrue("JMSSelector identified in binding:"+message, !message.contains("jms-selector"));
+
+ // Perform full testing on the second non default binding
+ message = getMessageString(fromMessage(getLogMessage(results, 1)));
+
+ validateLogMessage(getLogMessage(results, 1), messageID, message,
+ "topic/amq.topic", "topic", "clientid:" + getName());
+
+ assertTrue("JMSSelector not identified in binding:"+message, message.contains("jms-selector"));
+ assertTrue("Selector not part of binding.:"+message, message.contains(SELECTOR));
+
+ }
+
+ /**
+ * Description:
+ * Bindings can be deleted so that a queue can be rebound with a different set of values.
+ * Input:
+ *
+ * 1. Running Broker
+ * 2. AMQP UnBind Request is made
+ * Output:
+ *
+ * <date> BND-1002 : Deleted
+ *
+ * Validation Steps:
+ * 3. The BND ID is correct
+ * 4. There must have been a BND-1001 Create message first.
+ * 5. This will be the last message for the given binding
+ */
+ public void testBindingDelete() throws JMSException, IOException
+ {
+ //Closing a consumer on a temporary queue will cause it to autodelete
+ // and so unbind.
+ _session.createConsumer(_session.createTemporaryQueue()).close();
+
+ if(isBroker010())
+ {
+ //auto-delete is at session close for 0-10
+ _session.close();
+ }
+
+ //wait for the deletion messages to be logged
+ waitForMessage("BND-1002");
+
+ //gather all the BND messages
+ List<String> results = waitAndFindMatches(BND_PREFIX);
+
+ // We will have two binds as we bind all queues to the default exchange
+ assertEquals("Result not as expected." + results, 4, results.size());
+
+
+ String messageID = "BND-1001";
+ String message = "Create";
+
+ String log = getLogMessage(results, 0);
+ validateMessageID(messageID, log);
+ assertEquals("Log Message not as expected", message, getMessageString(fromMessage(log)));
+
+ log = getLogMessage(results, 1);
+ validateMessageID(messageID, log);
+ assertEquals("Log Message not as expected", message, getMessageString(fromMessage(log)));
+
+
+ String DEFAULT = "direct/<<default>>";
+ String DIRECT = "direct/amq.direct";
+
+ messageID = "BND-1002";
+ message = "Deleted";
+
+ log = getLogMessage(results, 2);
+ validateMessageID(messageID, log);
+
+ String subject = fromSubject(log);
+
+ validateBindingDeleteArguments(subject, "/test");
+
+ boolean defaultFirst = DEFAULT.equals(AbstractTestLogSubject.getSlice("ex", subject));
+ boolean directFirst = DIRECT.equals(AbstractTestLogSubject.getSlice("ex", subject));
+
+ assertEquals("Log Message not as expected", message, getMessageString(fromMessage(log)));
+
+ log = getLogMessage(results, 3);
+
+ validateMessageID(messageID, log);
+
+ subject = fromSubject(log);
+
+ validateBindingDeleteArguments(subject, "/test");
+
+ if (!defaultFirst)
+ {
+ assertEquals(DEFAULT, AbstractTestLogSubject.getSlice("ex", subject));
+ assertTrue("First Exchange Log was not a direct exchange delete",directFirst);
+ }
+ else
+ {
+ assertEquals(DIRECT, AbstractTestLogSubject.getSlice("ex", subject));
+ assertTrue("First Exchange Log was not a default exchange delete",defaultFirst);
+ }
+
+ assertEquals("Log Message not as expected", message, getMessageString(fromMessage(log)));
+ }
+
+ private void validateBindingDeleteArguments(String subject, String vhostName)
+ {
+ String routingKey = AbstractTestLogSubject.getSlice("rk", subject);
+
+ assertTrue("Routing Key does not start with TempQueue:"+routingKey,
+ routingKey.startsWith("TempQueue"));
+ assertEquals("Virtualhost not correct.", vhostName,
+ AbstractTestLogSubject.getSlice("vh", subject));
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/BrokerLoggingTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/BrokerLoggingTest.java
new file mode 100644
index 0000000000..8fd2c085c3
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/BrokerLoggingTest.java
@@ -0,0 +1,1003 @@
+/*
+*
+* 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.logging;
+
+import junit.framework.AssertionFailedError;
+import org.apache.qpid.server.Main;
+import org.apache.qpid.transport.ConnectionException;
+import org.apache.qpid.util.LogMonitor;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.Socket;
+import java.util.List;
+
+/**
+ * Broker Test Suite
+ *
+ * The Broker test suite validates that the follow log messages as specified in the Functional Specification.
+ *
+ * BRK-1001 : Startup : Version: <Version> Build: <Build>
+ * BRK-1002 : Starting : Listening on <Transport> port <Port>
+ * BRK-1003 : Shuting down : <Transport> port <Port>
+ * BRK-1004 : Ready
+ * BRK-1005 : Stopped
+ * BRK-1006 : Using configuration : <path>
+ * BRK-1007 : Using logging configuration : <path>
+ *
+ * These messages should only occur during startup. The tests need to verify the order of messages. In the case of the BRK-1002 and BRK-1003 the respective ports should only be available between the two log messages.
+ */
+public class BrokerLoggingTest extends AbstractTestLogging
+{
+ private static final String BRK_LOG_PREFIX = "BRK-";
+
+ public void setUp() throws Exception
+ {
+ setLogMessagePrefix();
+
+ // We either do this here or have a null check in tearDown.
+ // As when this test is run against profiles other than java it will NPE
+ _monitor = new LogMonitor(_outputFile);
+ //We explicitly do not call super.setUp as starting up the broker is
+ //part of the test case.
+ }
+
+ /**
+ * Description:
+ * On startup the broker must report the active configuration file. The
+ * logging system must output this so that we can know what configuration
+ * is being used for this broker instance.
+ *
+ * Input:
+ * The value of -c specified on the command line.
+ * Output:
+ * <date> MESSAGE BRK-1006 : Using configuration : <config file>
+ * Constraints:
+ * This MUST BE the first BRK log message.
+ *
+ * Validation Steps:
+ * 1. This is first BRK log message.
+ * 2. The BRK ID is correct
+ * 3. The config file is the full path to the file specified on
+ * the commandline.
+ *
+ * @throws Exception caused by broker startup
+ */
+ public void testBrokerStartupConfiguration() throws Exception
+ {
+ String TESTID="BRK-1006";
+
+ // This logging startup code only occurs when you run a Java broker,
+ // that broker must be started via Main so not an InVM broker.
+ if (isJavaBroker() && isExternalBroker())
+ {
+ startBroker();
+
+ // Now we can create the monitor as _outputFile will now be defined
+ _monitor = new LogMonitor(_outputFile);
+
+
+ String configFilePath = _configFile.toString();
+
+ // Ensure we wait for TESTID to be logged
+ waitAndFindMatches(TESTID);
+
+ List<String> results = waitAndFindMatches(BRK_LOG_PREFIX);
+ try
+ {
+ // Validation
+
+ assertTrue("BRKer message not logged", results.size() > 0);
+
+ String log = getLogMessage(results, 0);
+
+ //1
+ validateMessageID(TESTID, log);
+
+ //2
+ results = findMatches(TESTID);
+ assertEquals("More than one configuration message found.",
+ 1, results.size());
+
+ //3
+ assertTrue("Config file details not correctly logged",
+ log.endsWith(configFilePath));
+ }
+ catch (AssertionFailedError afe)
+ {
+ dumpLogs(results, _monitor);
+
+ throw afe;
+ }
+ }
+ }
+
+ /**
+ * Description:
+ * On startup the broker must report correctly report the log4j file in use. This is important as it can help diagnose why logging messages are not being reported.
+ * Input:
+ * No custom -l value should be provided on the command line so that the default value is correctly reported.
+ * Output:
+ *
+ * <date> MESSAGE BRK-1007 : Using logging configuration : <$QPID_HOME>/etc/log4j.xml
+ *
+ * Validation Steps:
+ *
+ * 1. The BRK ID is correct
+ * 2. This occurs before the BRK-1001 startup message.
+ * 3. The log4j file is the full path to the file specified on the commandline.
+ *
+ * @throws Exception caused by broker startup
+ */
+ public void testBrokerStartupDefaultLog4j() throws Exception
+ {
+ // This logging startup code only occurs when you run a Java broker,
+ // that broker must be started via Main so not an InVM broker.
+ if (isJavaBroker() && isExternalBroker())
+ {
+ String TESTID = "BRK-1007";
+
+ //Remove test Log4j config from the commandline
+ _broker = _broker.substring(0, _broker.indexOf("-l"));
+
+ startBroker();
+
+ // Now we can create the monitor as _outputFile will now be defined
+ _monitor = new LogMonitor(_outputFile);
+
+ // Ensure broker has fully started up.
+ getConnection();
+
+ // Ensure we wait for TESTID to be logged
+ waitAndFindMatches(TESTID);
+
+ List<String> results = waitAndFindMatches(BRK_LOG_PREFIX);
+ try
+ {
+ // Validation
+
+ assertTrue("BRKer message not logged", results.size() > 0);
+
+ boolean validation = false;
+ for (String rawLog : results)
+ {
+ // We don't care about messages after we have our log config
+ if (validation)
+ {
+ break;
+ }
+
+ String log = getLog(rawLog);
+
+ // Ensure we do not have a BRK-1001 message before
+ if (!getMessageID(log).equals(TESTID))
+ {
+ assertFalse(getMessageID(log).equals("BRK-1001"));
+ continue;
+ }
+
+ //1
+ validateMessageID(TESTID, log);
+
+ //2
+ //There will be 1 copy of this startup message (via SystemOut)
+ assertEquals("Unexpected log4j configuration message count.",
+ 1, findMatches(TESTID).size());
+
+ //3
+ String defaultLog4j = _configFile.getParent() + "/" + Main.DEFAULT_LOG_CONFIG_FILENAME;
+ assertTrue("Log4j file(" + defaultLog4j + ") details not correctly logged:" + getMessageString(log),
+ getMessageString(log).endsWith(defaultLog4j));
+
+ validation = true;
+ }
+
+ assertTrue("Validation not performed: " + TESTID + " not logged", validation);
+ }
+ catch (AssertionFailedError afe)
+ {
+ dumpLogs(results, _monitor);
+
+ throw afe;
+ }
+ }
+ }
+
+ /**
+ * Description:
+ * On startup the broker must report correctly report the log4j file in use. This is important as it can help diagnose why logging messages are not being reported. The broker must also be capable of correctly recognising the command line property to specify the custom logging configuration.
+ * Input:
+ * The value of -l specified on the command line.
+ * Output:
+ *
+ * <date> MESSAGE BRK-1007 : Using logging configuration : <log4j file>
+ *
+ * Validation Steps:
+ *
+ * 1. The BRK ID is correct
+ * 2. This should occur before the BRK-1001 : Startup message
+ * 3. The log4j file is the full path to the file specified on the commandline.
+ *
+ * @throws Exception caused by broker startup
+ */
+ public void testBrokerStartupCustomLog4j() throws Exception
+ {
+ // This logging startup code only occurs when you run a Java broker,
+ // that broker must be started via Main so not an InVM broker.
+ if (isJavaBroker() && isExternalBroker())
+ {
+ // Get custom -l value used during testing for the broker startup
+ String customLog4j = _broker.substring(_broker.indexOf("-l") + 2);
+
+ String TESTID = "BRK-1007";
+
+ startBroker();
+
+ // Now we can create the monitor as _outputFile will now be defined
+ _monitor = new LogMonitor(_outputFile);
+
+
+ // Ensure broker has fully started up.
+ getConnection();
+
+ // Ensure we wait for TESTID to be logged
+ waitAndFindMatches(TESTID);
+
+ List<String> results = waitAndFindMatches(BRK_LOG_PREFIX);
+ try
+ {
+ // Validation
+
+ assertTrue("BRKer message not logged", results.size() > 0);
+
+ boolean validation = false;
+ for (String rawLog : results)
+ {
+ // We don't care about messages after we have our log config
+ if (validation)
+ {
+ break;
+ }
+ String log = getLog(rawLog);
+
+ // Ensure we do not have a BRK-1001 message before
+ if (!getMessageID(log).equals(TESTID))
+ {
+ assertFalse(getMessageID(log).equals("BRK-1001"));
+ continue;
+ }
+
+ //1
+ validateMessageID(TESTID, log);
+
+ //2
+ //There will be 1 copy of this startup message (via SystemOut)
+ assertEquals("Unexpected log4j configuration message count.",
+ 1, findMatches(TESTID).size());
+
+ //3
+ assertTrue("Log4j file details not correctly logged:" + getMessageString(log),
+ getMessageString(log).endsWith(customLog4j));
+
+ validation = true;
+ }
+
+ assertTrue("Validation not performed: " + TESTID + " not logged", validation);
+ }
+ catch (AssertionFailedError afe)
+ {
+ dumpLogs(results, _monitor);
+
+ throw afe;
+ }
+ }
+ }
+
+ /**
+ * Description: On startup the broker reports the broker version number and svn build revision. This information is retrieved from the resource 'qpidversion.properties' which is located via the classloader.
+ * Input: The 'qpidversion.properties' file located on the classpath.
+ * Output:
+ *
+ * <date> MESSAGE BRK-1001 : Startup : qpid Version: 0.6 Build: 767150
+ *
+ * Validation Steps:
+ *
+ * 1. The BRK ID is correct
+ * 2. This occurs before any BRK-1002 listening messages are reported.
+ *
+ * @throws Exception caused by broker startup
+ */
+ public void testBrokerStartupStartup() throws Exception
+ {
+ // This logging startup code only occurs when you run a Java broker,
+ // that broker must be started via Main so not an InVM broker.
+ if (isJavaBroker() && isExternalBroker())
+ {
+ String TESTID = "BRK-1001";
+
+ startBroker();
+
+ // Now we can create the monitor as _outputFile will now be defined
+ _monitor = new LogMonitor(_outputFile);
+
+ // Ensure we wait for TESTID to be logged
+ waitAndFindMatches(TESTID);
+
+ // Retrieve all BRK- log messages so we can check for an erroneous
+ // BRK-1002 message.
+ List<String> results = findMatches(BRK_LOG_PREFIX);
+
+ try
+ {
+ // Validation
+
+ assertTrue("BRKer message not logged", results.size() > 0);
+
+ boolean validation = false;
+ for (String rawLog : results)
+ {
+ if (validation)
+ {
+ //Stop checking once we have got to our startup test
+ break;
+ }
+ String log = getLog(rawLog);
+
+ // Ensure we do not have a BRK-1002 message
+ if (!getMessageID(log).equals(TESTID))
+ {
+ assertFalse(getMessageID(log).equals("BRK-1002"));
+ continue;
+ }
+
+ //1
+ validateMessageID(TESTID, log);
+
+ //2
+ //There will be 2 copies of the startup message (one via SystemOut, and one via Log4J)
+ assertEquals("Unexpected startup message count",
+ 2, findMatches(TESTID).size());
+
+ validation = true;
+ }
+
+ assertTrue("Validation not performed: " + TESTID + " not logged", validation);
+ }
+ catch (AssertionFailedError afe)
+ {
+ dumpLogs(results, _monitor);
+
+ throw afe;
+ }
+ }
+ }
+
+ /**
+ * Description:
+ * On startup the broker may listen on a number of ports and protocols. Each of these must be reported as they are made available.
+ * Input:
+ * The default configuration with no SSL
+ * Output:
+ *
+ * <date> MESSAGE BRK-1002 : Starting : Listening on TCP port 5672
+ *
+ * Constraints:
+ * Additional broker configuration will occur between the Startup(BRK-1001) and Starting(BRK-1002) messages depending on what VirtualHosts are configured.
+ * Validation Steps:
+ *
+ * 1. The BRK ID is correct
+ * 2. This occurs after the BRK-1001 startup message
+ * 3. Using the default configuration a single BRK-1002 will be printed showing values TCP / 5672
+ *
+ * @throws Exception caused by broker startup
+ */
+ public void testBrokerStartupListeningTCPDefault() throws Exception
+ {
+ // This logging startup code only occurs when you run a Java broker,
+ // that broker must be started via Main so not an InVM broker.
+ if (isJavaBroker() && isExternalBroker())
+ {
+ String TESTID = "BRK-1002";
+
+ startBroker();
+
+ // Now we can create the monitor as _outputFile will now be defined
+ _monitor = new LogMonitor(_outputFile);
+
+ // Ensure broker has fully started up.
+ getConnection();
+
+ // Ensure we wait for TESTID to be logged
+ waitAndFindMatches(TESTID);
+
+ // Retrieve all BRK- log messages so we can check for an erroneous
+ // BRK-1002 message.
+ List<String> results = findMatches(BRK_LOG_PREFIX);
+ try
+ {
+ // Validation
+
+ assertTrue("BRKer message not logged", results.size() > 0);
+
+ boolean validation = false;
+ boolean foundBRK1001 = false;
+ for (String rawLog : results)
+ {
+ String log = getLog(rawLog);
+
+ // Ensure we do not have a BRK-1002 message
+ if (!getMessageID(log).equals(TESTID))
+ {
+ if (getMessageID(log).equals("BRK-1001"))
+ {
+ foundBRK1001 = true;
+ }
+ continue;
+ }
+
+ assertTrue("BRK-1001 not logged before this message", foundBRK1001);
+
+ //1
+ validateMessageID(TESTID, log);
+
+ //2
+ //There will be 2 copies of the startup message (one via SystemOut, and one via Log4J)
+ assertEquals("Unexpected listen message count",
+ 2, findMatches(TESTID).size());
+
+ //3
+ String message = getMessageString(log);
+ assertTrue("Expected Listen log not correct" + message,
+ message.endsWith("Listening on TCP port " + getPort()));
+
+ validation = true;
+ }
+
+ assertTrue("Validation not performed: " + TESTID + " not logged", validation);
+ }
+ catch (AssertionFailedError afe)
+ {
+ dumpLogs(results, _monitor);
+
+ throw afe;
+ }
+ }
+ }
+
+ /**
+ * Description:
+ * On startup the broker may listen on a number of ports and protocols. Each of these must be reported as they are made available.
+ * Input:
+ * The default configuration with SSL enabled
+ * Output:
+ *
+ * <date> MESSAGE BRK-1002 : Starting : Listening on TCP port 5672
+ * <date> MESSAGE BRK-1002 : Starting : Listening on TCP/SSL port 8672
+ *
+ * Constraints:
+ * Additional broker configuration will occur between the Startup(BRK-1001) and Starting(BRK-1002) messages depending on what VirtualHosts are configured.
+ * Validation Steps:
+ *
+ * 1. The BRK ID is correct
+ * 2. This occurs after the BRK-1001 startup message
+ * 3. With SSL enabled in the configuration two BRK-1002 will be printed (order is not specified)
+ * 1. One showing values TCP / 5672
+ * 2. One showing values TCP/SSL / 5672
+ *
+ * @throws Exception caused by broker startup
+ */
+ public void testBrokerStartupListeningTCPSSL() throws Exception
+ {
+ // This logging startup code only occurs when you run a Java broker,
+ // that broker must be started via Main so not an InVM broker.
+ if (isJavaBroker() && isExternalBroker())
+ {
+ String TESTID = "BRK-1002";
+
+ // Enable SSL on the connection
+ setConfigurationProperty("connector.ssl.enabled", "true");
+ setConfigurationProperty("connector.ssl.sslOnly", "false");
+ setConfigurationProperty("connector.ssl.keyStorePath", getConfigurationStringProperty("management.ssl.keyStorePath"));
+ setConfigurationProperty("connector.ssl.keyStorePassword", getConfigurationStringProperty("management.ssl.keyStorePassword"));
+
+ Integer sslPort = Integer.parseInt(getConfigurationStringProperty("connector.sslport"));
+
+ startBroker();
+
+ // Now we can create the monitor as _outputFile will now be defined
+ _monitor = new LogMonitor(_outputFile);
+
+ // Ensure broker has fully started up.
+ getConnection();
+
+ // Ensure we wait for TESTID to be logged
+ waitAndFindMatches(TESTID);
+
+ // Retrieve all BRK- log messages so we can check for an erroneous
+ // BRK-1002 message.
+ List<String> results = findMatches(BRK_LOG_PREFIX);
+ try
+ {
+ // Validation
+
+ assertTrue("BRKer message not logged", results.size() > 0);
+
+ boolean validation = false;
+ boolean foundBRK1001 = false;
+ for (String rawLog : results)
+ {
+ String log = getLog(rawLog);
+
+ // Ensure we do not have a BRK-1002 message
+ if (!getMessageID(log).equals(TESTID))
+ {
+ if (getMessageID(log).equals("BRK-1001"))
+ {
+ foundBRK1001 = true;
+ }
+ continue;
+ }
+
+ assertTrue("BRK-1001 not logged before this message", foundBRK1001);
+
+ //1
+ validateMessageID(TESTID, log);
+
+ //2
+ //There will be 4 copies of the startup message (two via SystemOut, and two via Log4J)
+ List<String> listenMessages = findMatches(TESTID);
+ assertEquals("Four listen messages should be found.",
+ 4, listenMessages .size());
+
+ //3
+ //Check the first
+ String message = getMessageString(getLog(listenMessages .get(0)));
+ assertTrue("Expected Listen log not correct" + message,
+ message.endsWith("Listening on TCP port " + getPort()));
+
+ // Check the third, ssl listen.
+ message = getMessageString(getLog(listenMessages .get(2)));
+ assertTrue("Expected Listen log not correct" + message,
+ message.endsWith("Listening on TCP/SSL port " + sslPort));
+
+ //4 Test ports open
+ testSocketOpen(getPort());
+ testSocketOpen(sslPort);
+
+ validation = true;
+ }
+
+ assertTrue("Validation not performed: " + TESTID + " not logged", validation);
+ }
+ catch (AssertionFailedError afe)
+ {
+ dumpLogs(results, _monitor);
+
+ throw afe;
+ }
+ }
+ }
+
+ /**
+ * Description:
+ * The final message the broker will print when it has performed all initialisation and listener startups will be to log the BRK-1004 Ready message
+ * Input:
+ * No input, all successful broker startups will show BRK-1004 messages.
+ * Output:
+ *
+ * 2009-07-09 15:50:20 +0100 MESSAGE BRK-1004 : Qpid Broker Ready
+ *
+ * Validation Steps:
+ *
+ * 1. The BRK ID is correct
+ * 2. This occurs after the BRK-1001 startup message
+ * 3. This must be the last message the broker prints after startup. Currently, if there is no further interaction with the broker then there should be no more logging.
+ *
+ * @throws Exception caused by broker startup
+ */
+ public void testBrokerStartupReady() throws Exception
+ {
+ // This logging startup code only occurs when you run a Java broker,
+ // that broker must be started via Main so not an InVM broker.
+ if (isJavaBroker() && isExternalBroker())
+ {
+ String TESTID = "BRK-1004";
+
+ startBroker();
+
+ //Ensure the broker has fully started up.
+ getConnection();
+ // Ensure we wait for TESTID to be logged
+ waitAndFindMatches(TESTID);
+
+ // Retrieve all BRK- log messages so we can check for an erroneous
+ // BRK-1001 message.
+ List<String> results = findMatches(BRK_LOG_PREFIX);
+ try
+ {
+ // Validation
+
+ assertTrue("BRKer message not logged", results.size() > 0);
+
+ boolean validationComplete = false;
+ boolean foundBRK1001 = false;
+
+ for (int i=0; i < results.size(); i++)
+ {
+ String rawLog = results.get(i);
+ String log = getLog(rawLog);
+
+ // Ensure we do not have a BRK-1001 message
+ if (!getMessageID(log).equals(TESTID))
+ {
+ if (getMessageID(log).equals("BRK-1001"))
+ {
+ foundBRK1001 = true;
+ }
+ continue;
+ }
+
+ assertTrue("BRK-1001 not logged before this message", foundBRK1001);
+
+ //1
+ validateMessageID(TESTID, log);
+
+ //2
+ assertEquals("Ready message not present", "Qpid Broker Ready", getMessageString(log));
+
+ //There will be 2 copies of the startup message (one via SystemOut, and one via Log4J)
+ assertEquals("Unexpected ready message count",
+ 2, findMatches(TESTID).size());
+ assertEquals("The ready messages should have been the last 2 messages", results.size() - 2, i);
+
+ validationComplete = true;
+ break;
+ }
+
+ assertTrue("Validation not performed: " + TESTID + " not logged", validationComplete);
+ }
+ catch (AssertionFailedError afe)
+ {
+ dumpLogs(results, _monitor);
+
+ throw afe;
+ }
+ }
+ }
+
+ /**
+ * Description:
+ * On startup the broker may listen on a number of ports and protocols. Each of these must then report a shutting down message as they stop listening.
+ * Input:
+ * The default configuration with no SSL
+ * Output:
+ *
+ * <date> MESSAGE BRK-1003 : Shutting down : TCP port 5672
+ *
+ * Validation Steps:
+ *
+ * 1. The BRK ID is correct
+ * 2. Only TCP is reported with the default configuration with no SSL.
+ * 3. The default port is correct
+ * 4. The port is not accessible after this message
+ *
+ * @throws Exception caused by broker startup
+ */
+ public void testBrokerShutdownListeningTCPDefault() throws Exception
+ {
+ // This logging startup code only occurs when you run a Java broker,
+ // that broker must be started via Main so not an InVM broker.
+ if (isJavaBroker() && isExternalBroker())
+ {
+ String TESTID = "BRK-1003";
+
+ startBroker();
+
+ // Now we can create the monitor as _outputFile will now be defined
+ _monitor = new LogMonitor(_outputFile);
+
+ stopBroker();
+
+ //Give broker time to shutdown and flush log
+ checkSocketClosed(getPort());
+
+ List<String> results = waitAndFindMatches(BRK_LOG_PREFIX);
+ try
+ {
+ // Validation
+
+ assertTrue("BRKer message not logged", results.size() > 0);
+
+ boolean validation = false;
+ boolean foundBRK1001 = false;
+ for (String rawLog : results)
+ {
+ String log = getLog(rawLog);
+
+ // Ensure we do not have a BRK-1002 message
+ if (!getMessageID(log).equals(TESTID))
+ {
+ if (getMessageID(log).equals("BRK-1001"))
+ {
+ foundBRK1001 = true;
+ }
+ continue;
+ }
+
+ assertTrue("BRK-1001 not logged before this message", foundBRK1001);
+
+ //1
+ validateMessageID(TESTID, log);
+
+ //2
+ assertEquals("More than one listen message found.",
+ 1, findMatches(TESTID).size());
+
+ //3
+ String message = getMessageString(log);
+ assertTrue("Expected shutdown log not correct" + message,
+ message.endsWith("TCP port " + getPort()));
+
+ //4
+ checkSocketClosed(getPort());
+
+ validation = true;
+ }
+
+ assertTrue("Validation not performed: " + TESTID + " not logged", validation);
+ }
+ catch (AssertionFailedError afe)
+ {
+ dumpLogs(results, _monitor);
+
+ throw afe;
+ }
+ }
+ }
+
+ /**
+ * Description:
+ * On startup the broker may listen on a number of ports and protocols. Each of these must be reported as they are made available.
+ * Input:
+ * The default configuration with SSL enabled
+ * Output:
+ *
+ * <date> MESSAGE BRK-1002 : Starting : Listening on TCP port 5672
+ * <date> MESSAGE BRK-1002 : Starting : Listening on TCP/SSL port 8672
+ *
+ * Constraints:
+ * Additional broker configuration will occur between the Startup(BRK-1001) and Starting(BRK-1002) messages depending on what VirtualHosts are configured.
+ * Validation Steps:
+ *
+ * 1. The BRK ID is correct
+ * 2. This occurs after the BRK-1001 startup message
+ * 3. With SSL enabled in the configuration two BRK-1002 will be printed (order is not specified)
+ * 1. One showing values TCP / 5672
+ * 2. One showing values TCP/SSL / 5672
+ *
+ * @throws Exception caused by broker startup
+ */
+ public void testBrokerShutdownListeningTCPSSL() throws Exception
+ {
+ // This logging startup code only occurs when you run a Java broker,
+ // that broker must be started via Main so not an InVM broker.
+ if (isJavaBroker() && isExternalBroker())
+ {
+ String TESTID = "BRK-1003";
+
+ // Enable SSL on the connection
+ setConfigurationProperty("connector.ssl.enabled", "true");
+ setConfigurationProperty("connector.ssl.keyStorePath", getConfigurationStringProperty("management.ssl.keyStorePath"));
+ setConfigurationProperty("connector.ssl.keyStorePassword", getConfigurationStringProperty("management.ssl.keyStorePassword"));
+
+ Integer sslPort = Integer.parseInt(getConfigurationStringProperty("connector.sslport"));
+
+ startBroker();
+
+ // Now we can create the monitor as _outputFile will now be defined
+ _monitor = new LogMonitor(_outputFile);
+
+
+// //Clear any startup messages as we don't need them for validation
+// _monitor.reset();
+ //Stop the broker to get the log messages for testing
+ stopBroker();
+
+ //Give broker time to shutdown and flush log
+ checkSocketClosed(getPort());
+
+ List<String> results = waitAndFindMatches(TESTID);
+ try
+ {
+ // Validation
+
+ assertTrue(TESTID + " messages not logged", results.size() > 0);
+
+ String log = getLog(results.get(0));
+
+ //1
+ validateMessageID(TESTID, log);
+
+ //2
+ List<String> listenMessages = findMatches(TESTID);
+ assertEquals("Two shutdown messages should be found.",
+ 2, listenMessages.size());
+
+ //3
+ String message = getMessageString(getLog(listenMessages.get(0)));
+ assertTrue("Expected shutdown log not correct" + message,
+ message.endsWith("TCP port " + getPort()));
+
+ // Check second, ssl, listen.
+ message = getMessageString(getLog(listenMessages.get(1)));
+ assertTrue("Expected shutdown log not correct" + message,
+ message.endsWith("TCP/SSL port " + sslPort));
+
+ //4
+ //Test Port closed
+ checkSocketClosed(getPort());
+ //Test SSL Port closed
+ checkSocketClosed(sslPort);
+ }
+ catch (AssertionFailedError afe)
+ {
+ dumpLogs(results, _monitor);
+
+ throw afe;
+ }
+ }
+ }
+
+ /**
+ * Description:
+ * Input:
+ * No input, all clean broker shutdowns will show BRK-1005 messages.
+ * Output:
+ *
+ * <date> MESSAGE BRK-1005 : Stopped
+ *
+ * Constraints:
+ * This is the LAST message the broker will log.
+ * Validation Steps:
+ *
+ * 1. The BRK ID is correct
+ * 2. This is the last message the broker will log.
+ *
+ * @throws Exception caused by broker startup
+ */
+ public void testBrokerShutdownStopped() throws Exception
+ {
+ // This logging startup code only occurs when you run a Java broker,
+ // that broker must be started via Main so not an InVM broker.
+ if (isJavaBroker() && isExternalBroker())
+ {
+ String TESTID = "BRK-1005";
+
+ startBroker();
+
+ // Now we can create the monitor as _outputFile will now be defined
+ _monitor = new LogMonitor(_outputFile);
+
+ getConnection().close();
+
+ stopBroker();
+
+ // Ensure the broker has shutdown before retreving results
+ checkSocketClosed(getPort());
+
+ waitAndFindMatches(TESTID);
+
+ List<String> results = waitAndFindMatches(BRK_LOG_PREFIX);
+ try
+ {
+ // Validation
+
+ assertTrue("BRKer message not logged", results.size() > 0);
+
+ boolean validation = false;
+ for (String rawLog : results)
+ {
+ assertFalse("More broker log statements present after ready message", validation);
+ String log = getLog(rawLog);
+
+ // Ignore all logs until we get to the test id.
+ if (!getMessageID(log).equals(TESTID))
+ {
+ continue;
+ }
+
+ //1
+ validateMessageID(TESTID, log);
+
+ //2
+ assertEquals("More than one ready message found.",
+ 1, findMatches(TESTID).size());
+
+ //3
+ assertEquals("Stopped message not present", "Stopped", getMessageString(log));
+
+ validation = true;
+ }
+
+ assertTrue("Validation not performed: " + TESTID + " not logged", validation);
+ }
+ catch (AssertionFailedError afe)
+ {
+ dumpLogs(results, _monitor);
+
+ throw afe;
+ }
+ }
+ }
+
+ /**
+ * Test that a socket on the given port is closed.
+ *
+ * Does this by attempting to connect to the port and expecting a
+ * ConnectionRefused IOException or a ConnectionException
+ *
+ * @param port the port number
+ */
+ private void checkSocketClosed(int port)
+ {
+ try
+ {
+ Socket socket = new Socket((String) null, port);
+ fail("Socket not closed on port:" + port);
+ }
+ catch (ConnectionException e)
+ {
+ //normal path
+ }
+ catch (IOException e)
+ {
+ if (!e.getMessage().equals("Connection refused"))
+ {
+ fail("Socket not closed on port:" + port + ":" + e.getMessage());
+ // Keep stack trace for diagnosis.
+ e.printStackTrace(System.err);
+ }
+ }
+ }
+
+ /**
+ * Test that a socket on the given port is open.
+ *
+ * Does this by attempting to connect to the port and expecting a
+ * The connection to succeed.
+ * It then closes the socket and expects that to work cleanly.
+ *
+ * @param port the port number
+ */
+ private void testSocketOpen(int port)
+ {
+ try
+ {
+ Socket socket = new Socket((String) null, port);
+ socket.close();
+ }
+ catch (IOException e)
+ {
+ fail("Unable to open and close socket to port:" + port
+ + ". Due to:" + e.getMessage());
+ }
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/ChannelLoggingTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/ChannelLoggingTest.java
new file mode 100644
index 0000000000..02d0d6f334
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/ChannelLoggingTest.java
@@ -0,0 +1,313 @@
+/*
+ *
+ * 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.logging;
+
+import org.apache.qpid.client.AMQConnection;
+
+import javax.jms.Connection;
+import javax.jms.MessageConsumer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import java.util.List;
+
+public class ChannelLoggingTest extends AbstractTestLogging
+{
+ private static final String CHANNEL_PREFIX = "CHN-";
+
+ // No explicit startup configuration is required for this test
+ // so no setUp() method
+
+ /**
+ * Description:
+ * When a new Channel (JMS Session) is created this will be logged as a CHN-1001 Create message. The messages will contain the prefetch details about this new Channel.
+ * Input:
+ *
+ * 1. Running Broker
+ * 2. New JMS Session/Channel creation
+ *
+ * Output:
+ * <date> CHN-1001 : Create
+ * <date> CHN-1004 : Prefetch Size (bytes) {0,number} : Count {1,number}
+ *
+ * Validation Steps:
+ * 1. The CHN ID is correct
+ * 2. The prefetch value matches that defined by the requesting client.
+ *
+ * @throws Exception - if an error occurs
+ */
+ public void testChannelCreate() throws Exception
+ {
+ assertLoggingNotYetOccured(CHANNEL_PREFIX);
+
+ Connection connection = getConnection();
+
+ int PREFETCH = 12;
+
+ // Test that calling session.close gives us the expected output
+ ((AMQConnection)connection).createSession(false, Session.AUTO_ACKNOWLEDGE,PREFETCH);
+
+ // Wait to ensure that the CHN-1001 message is logged
+ waitForMessage("CHN-1001");
+
+ List<String> results = findMatches("CHN-1001");
+
+ // Validation
+ assertEquals("CHN-1001 messages not logged", 1, results.size());
+
+ String log = getLogMessage(results, 0);
+ // MESSAGE [con:0(guest@anonymous(3273383)/test)/ch:1] CHN-1001 : Create
+ validateMessageID("CHN-1001", log);
+ assertEquals("Incorrect Channel in actor:"+fromActor(log), isBroker010()? 0 : 1, getChannelID(fromActor(log)));
+
+ if (isBroker08())
+ {
+ // Wait to ensure that the CHN-1004 message is logged
+ waitForMessage("CHN-1004");
+
+ results = findMatches("CHN-1004");
+
+ // Validation
+ assertEquals("CHN-1004 messages not logged", 1, results.size());
+ log = getLogMessage(results, 0);
+ // MESSAGE [con:0(guest@anonymous(3273383)/test)/ch:1] CHN-1004 : Prefetch Size (bytes) {0,number} : Count {1,number}
+ validateMessageID("CHN-1004", log);
+ assertEquals("Incorrect Channel in actor:"+fromActor(log), isBroker010()? 0 : 1, getChannelID(fromActor(log)));
+ assertTrue("Prefetch Count not correct",getMessageString(fromMessage(log)).endsWith("Count "+PREFETCH));
+ }
+
+ connection.close();
+ }
+
+ /**
+ * Description:
+ * The Java Broker implements consumer flow control for all ack modes except
+ * No-Ack. When a client connects the session's flow is initially set to
+ * Stopped. Verify this message appears
+ *
+ * Input:
+ * 1. Running broker
+ * 2. Create consumer
+ * Output:
+ *
+ * <date> CHN-1002 : Flow Stopped
+ *
+ * Validation Steps:
+ * 4. The CHN ID is correct
+ *
+ * @throws Exception - if an error occurs
+ */
+
+ public void testChannelStartsFlowStopped() throws Exception
+ {
+ assertLoggingNotYetOccured(CHANNEL_PREFIX);
+
+ Connection connection = getConnection();
+
+ // Create a session to fill up
+ Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Queue queue = (Queue) getInitialContext().lookup(QUEUE);
+ MessageConsumer consumer = session.createConsumer(queue);
+
+ connection.start();
+
+ // Wait to ensure that the CHN-1002 message is logged
+ waitForMessage("CHN-1002");
+
+ List<String> results = findMatches(CHANNEL_PREFIX);
+
+ assertTrue("No CHN messages logged", results.size() > 0);
+
+ // The last channel message should be:
+ //
+ // INFO - MESSAGE [con:0(guest@anonymous(4205299)/test)/ch:1] [con:0(guest@anonymous(4205299)/test)/ch:1] CHN-1002 : Flow Stopped
+
+ // Verify the last channel message is stopped
+ validateChannelStart(results, false);
+ }
+
+ private void validateChannelStart(List<String> results, boolean flowStarted)
+ {
+ String log = getLogMessageFromEnd(results, 0);
+
+ String flow = flowStarted ? "Started" : "Stopped";
+ validateMessageID("CHN-1002", log);
+ assertEquals("Message should be Flow " + flow, "Flow " + flow, getMessageString(fromMessage(log)));
+ }
+
+ /**
+ * Description:
+ * The Java Broker implements consumer flow control for all ack modes except
+ * No-Ack. When the client first attempts to receive a message then the Flow
+ * status of the Session is set to Started.
+ *
+ * Input:
+ * 1. Running broker
+ * 2. Create a consumer
+ * 3. Attempt to receive a message
+ * Output:
+ *
+ * <date> CHN-1002 : Flow Started
+ *
+ * Validation Steps:
+ * 4. The CHN ID is correct
+ *
+ * @throws Exception - if an error occurs
+ */
+
+ public void testChannelStartConsumerFlowStarted() throws Exception
+ {
+ assertLoggingNotYetOccured(CHANNEL_PREFIX);
+
+ Connection connection = getConnection();
+
+ // Create a session to fill up
+ Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Queue queue = (Queue) getInitialContext().lookup(QUEUE);
+ MessageConsumer consumer = session.createConsumer(queue);
+
+ connection.start();
+
+ //Call receive to send the Flow On message
+ consumer.receiveNoWait();
+
+ //Wait for up to 2 seconds for message to appear
+ // ignore response as we will use the findMatches afterwards just
+ // incase it did take more than 2 seconds to log.
+ _monitor.waitForMessage(CHANNEL_PREFIX, 2000);
+
+ // Wait to ensure that the CHN-1002 message is logged
+ waitForMessage("CHN-1002");
+
+ List<String> results = findMatches(CHANNEL_PREFIX);
+
+ assertTrue("No CHN messages logged", results.size() > 0);
+
+ // The last two channel messages(before the close) should be:
+ //
+ // INFO [qpid.message] MESSAGE [con:1(guest@/127.0.0.1:49869/test)/ch:1] [con:1(guest@/127.0.0.1:49869/test)/ch:1] CHN-1002 : Flow Stopped
+ // INFO [qpid.message] MESSAGE [con:1(guest@/127.0.0.1:49869/test)/ch:1] [con:1(guest@/127.0.0.1:49869/test)/ch:1] CHN-1002 : Flow Started
+
+ // Verify the last channel msg is Started.
+ validateChannelStart(results, true);
+ }
+
+ /**
+ * Description:
+ * When the client gracefully closes the Connection then a CHN-1003 Close
+ * message will be issued. This must be the last message logged for this
+ * Channel.
+ * Input:
+ * 1. Running Broker
+ * 2. Connected Client
+ * 3. Client then requests that the Connection is closed
+ * Output:
+ *
+ * <date> CHN-1003 : Close
+ *
+ * Validation Steps:
+ * 4. The MST ID is correct
+ * 5. This must be the last message logged for this Channel.
+ *
+ * @throws Exception - if an error occurs
+ */
+ public void testChannelCloseViaConnectionClose() throws Exception
+ {
+ assertLoggingNotYetOccured(CHANNEL_PREFIX);
+
+ Connection connection = getConnection();
+
+ // Create a session
+ connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ // Close the connection to verify the created session closing is logged.
+ connection.close();
+
+ // Wait to ensure that the CHN-1003 message is logged
+ waitForMessage("CHN-1003");
+
+ List<String> results = findMatches(CHANNEL_PREFIX);
+
+ assertTrue("No CHN messages logged", results.size() > 0);
+
+ // The last two channel messages should be:
+ //
+ // INFO - MESSAGE [con:0(guest@anonymous(4205299)/test)/ch:1] [con:0(guest@anonymous(4205299)/test)/ch:1] CHN-1002 : Flow On
+
+ // Verify
+ validateChannelClose(results);
+ }
+
+ /**
+ * Description:
+ * When the client gracefully closes the Connection then a CHN-1003 Close
+ * message will be issued. This must be the last message logged for this
+ * Channel.
+ * Input:
+ * 1. Running Broker
+ * 2. Connected Client
+ * 3. Client then requests that the Channel is closed
+ * Output:
+ *
+ * <date> CHN-1003 : Close
+ *
+ * Validation Steps:
+ * 4. The MST ID is correct
+ * 5. This must be the last message logged for this Channel.
+ *
+ * @throws Exception - if an error occurs
+ */
+ public void testChannelCloseViaChannelClose() throws Exception
+ {
+ assertLoggingNotYetOccured(CHANNEL_PREFIX);
+
+ Connection connection = getConnection();
+
+ // Create a session and then close it
+ Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ waitForMessage("CHN-1001");
+
+ // Wait to ensure that the CHN-1003 message is logged
+ session.close();
+ waitForMessage("CHN-1003");
+
+ List<String> results = findMatches(CHANNEL_PREFIX);
+
+ assertTrue("No CHN messages logged", results.size() > 0);
+
+ // Verify
+ validateChannelClose(results);
+ }
+
+ private void validateChannelClose(List<String> results)
+ {
+ String open = getLogMessage(results, 0);
+ String close = getLogMessageFromEnd(results, 0);
+
+ validateMessageID("CHN-1001", open);
+ validateMessageID("CHN-1003", close);
+ assertEquals("Message should be Close", "Close", getMessageString(fromMessage(close)));
+ assertEquals("Incorrect Channel ID closed", isBroker010()? 0 : 1, getChannelID(fromSubject(close)));
+ assertEquals("Channel IDs should be the same", getChannelID(fromActor(open)), getChannelID(fromSubject(close)));
+ assertEquals("Connection IDs should be the same", getConnectionID(fromActor(open)), getConnectionID(fromSubject(close)));
+ }
+} \ No newline at end of file
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/ConnectionLoggingTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/ConnectionLoggingTest.java
new file mode 100644
index 0000000000..d28429aa39
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/ConnectionLoggingTest.java
@@ -0,0 +1,192 @@
+/*
+*
+* 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.logging;
+
+import javax.jms.Connection;
+import java.util.List;
+import java.util.HashMap;
+import java.util.TreeSet;
+
+public class ConnectionLoggingTest extends AbstractTestLogging
+{
+ private static final String CONNECTION_PREFIX = "CON-";
+
+ // No explicit startup configuration is required for this test
+ // so no setUp() method
+
+ /**
+ * Description:
+ * When a new connection is made to the broker this must be logged.
+ *
+ * Input:
+ * 1. Running Broker
+ * 2. Connecting client
+ * Output:
+ * <date> CON-1001 : Open : Client ID {0}[ : Protocol Version : {1}] <version>
+ *
+ * Validation Steps:
+ * 1. The CON ID is correct
+ * 2. This is the first CON message for that Connection
+ *
+ * @throws Exception - if an error occurs
+ */
+ public void testConnectionOpen() throws Exception
+ {
+ assertLoggingNotYetOccured(CONNECTION_PREFIX);
+
+ Connection connection = getConnection();
+ String clientid = connection.getClientID();
+
+ // Wait until opened
+ waitForMessage("CON-1001");
+
+ // Close the conneciton
+ connection.close();
+
+ // Wait to ensure that the desired message is logged
+ waitForMessage("CON-1002");
+
+ List<String> results = waitAndFindMatches("CON-1001");
+
+ // Validation
+ // We should have at least three messages when running InVM but when running External
+ // we will get 0-10 negotiation on con:0 whcih may close at some random point
+ // MESSAGE [con:0(/127.0.0.1:46926)] CON-1001 : Open
+ // MESSAGE [con:0(/127.0.0.1:46926)] CON-1001 : Open : Protocol Version : 0-10
+ // MESSAGE [con:1(/127.0.0.1:46927)] CON-1001 : Open
+ // MESSAGE [con:1(/127.0.0.1:46927)] CON-1001 : Open : Protocol Version : 0-9
+ // MESSAGE [con:0(/127.0.0.1:46926)] CON-1002 : Close
+ // MESSAGE [con:1(/127.0.0.1:46927)] CON-1001 : Open : Client ID : clientid : Protocol Version : 0-9
+
+ //So check how many connections we have in the result set and extract the last one.
+ // When running InVM we will have con:0 and externally con:1
+
+ HashMap<Integer, List<String>> connectionData = splitResultsOnConnectionID(results);
+
+ // Get the last Integer from keySet of the ConnectionData
+ int connectionID = new TreeSet<Integer>(connectionData.keySet()).last();
+
+ //Use just the data from the last connection for the test
+ results = connectionData.get(connectionID);
+
+ // If we are running inVM or with 0-10 we will get three open messagse
+ // if running externally with 0-8/0-9 we will also have open and close messages from the failed 0-10 negotiation
+ assertTrue("CON messages not logged:" + results.size(), results.size() >= 3);
+
+ String log = getLogMessage(results, 0);
+ // MESSAGE [con:1(/127.0.0.1:52540)] CON-1001 : Open
+ //1 & 2
+ validateMessageID("CON-1001",log);
+
+ // validate the last three CON- messages.
+ // This is because when running externally we may also have logged the failed
+ // 0-10 negotiation messages if using 0-8/0-9 on the broker.
+
+ // 3 - Assert the options are correct
+ // MESSAGE [con:1(/127.0.0.1:52540)] CON-1001 : Open : Client ID : clientid : Protocol Version : 0-9
+ validateConnectionOpen(results, 0, true, true, clientid);
+
+ // MESSAGE [con:1(/127.0.0.1:52540)] CON-1001 : Open : Protocol Version : 0-9
+ validateConnectionOpen(results, 1, true, false, null);
+
+ validateConnectionOpen(results, 2, false, false, null);
+ }
+
+ private void validateConnectionOpen(List<String> results, int positionFromEnd,
+ boolean protocolVersionPresent, boolean clientIdOptionPresent, String clientIdValue)
+ {
+ String log = getLogMessageFromEnd(results, positionFromEnd);
+
+ validateMessageID("CON-1001",log);
+
+ assertEquals("unexpected Client ID option state", clientIdOptionPresent, fromMessage(log).contains("Client ID :"));
+
+ if(clientIdOptionPresent && clientIdValue != null)
+ {
+ assertTrue("Client ID value is not present: " + clientIdValue, fromMessage(log).contains(clientIdValue));
+ }
+
+ assertEquals("unexpected Protocol Version option state",
+ protocolVersionPresent, fromMessage(log).contains("Protocol Version :"));
+ //fixme there is no way currently to find out the negotiated protocol version
+ // The delegate is the versioned class ((AMQConnection)connection)._delegate
+ }
+
+ /**
+ * Description:
+ * When a connected client closes the connection this will be logged as a CON-1002 message.
+ * Input:
+ *
+ * 1. Running Broker
+ * 2. Connected Client
+ * Output:
+ *
+ * <date> CON-1002 : Close
+ *
+ * Validation Steps:
+ * 3. The CON ID is correct
+ * 4. This must be the last CON message for the Connection
+ * 5. It must be preceded by a CON-1001 for this Connection
+ */
+ public void testConnectionClose() throws Exception
+ {
+ assertLoggingNotYetOccured(CONNECTION_PREFIX);
+
+ Connection connection = getConnection();
+
+ // Wait until opened
+ waitForMessage("CON-1001");
+
+ // Close the conneciton
+ connection.close();
+
+ // Wait to ensure that the desired message is logged
+ waitForMessage("CON-1002");
+
+ List<String> results = findMatches(CONNECTION_PREFIX);
+
+ // Validation
+
+ // We should have at least four messages
+ assertTrue("CON messages not logged:" + results.size(), results.size() >= 4);
+
+ // Validate Close message occurs
+ String log = getLogMessageFromEnd(results, 0);
+ validateMessageID("CON-1002",log);
+ assertTrue("Message does not end with close:" + log, log.endsWith("Close"));
+
+ // Extract connection ID to validate there is a CON-1001 messasge for it
+ int closeConnectionID = getConnectionID(fromSubject(log));
+ assertTrue("Could not find connection id in CLOSE", closeConnectionID != -1);
+
+ //Previous log message should be the open
+ log = getLogMessageFromEnd(results, 1);
+ // MESSAGE [con:1(/127.0.0.1:52540)] CON-1001 : Open : Client ID : clientid : Protocol Version : 0-9
+ validateMessageID("CON-1001",log);
+
+ // Extract connection ID to validate it matches the CON-1002 messasge
+ int openConnectionID = getConnectionID(fromActor(log));
+ assertTrue("Could not find connection id in OPEN", openConnectionID != -1);
+
+ // Check connection ids match
+ assertEquals("Connection IDs do not match", closeConnectionID, openConnectionID);
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/DerbyMessageStoreLoggingTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/DerbyMessageStoreLoggingTest.java
new file mode 100644
index 0000000000..16c529316a
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/DerbyMessageStoreLoggingTest.java
@@ -0,0 +1,574 @@
+/*
+ *
+ * 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.logging;
+
+import org.apache.commons.configuration.Configuration;
+import org.apache.qpid.server.configuration.ServerConfiguration;
+import org.apache.qpid.server.logging.subjects.AbstractTestLogSubject;
+
+import javax.jms.Connection;
+import javax.jms.Queue;
+import javax.jms.Session;
+import java.util.List;
+import java.io.File;
+
+/**
+ * The MessageStore test suite validates that the follow log messages as
+ * specified in the Functional Specification.
+ *
+ * This suite of tests validate that the MessageStore messages occur correctly
+ * and according to the following format:
+ *
+ * MST-1001 : Created : <name>
+ * MST-1003 : Closed
+ *
+ * NOTE: Only for Persistent Stores
+ * MST-1002 : Store location : <path>
+ * MST-1004 : Recovery Start [: <queue.name>]
+ * MST-1005 : Recovered <count> messages for queue <queue.name>
+ * MST-1006 : Recovery Complete [: <queue.name>]
+ */
+public class DerbyMessageStoreLoggingTest extends MemoryMessageStoreLoggingTest
+{
+
+ @Override
+ public void setUp() throws Exception
+ {
+ super.setUp();
+ // MemoryMessageStoreLoggingTest setUp itself does not call super.setUp
+ //We call super.setUp but this will not start the broker as that is
+ //part of the test case.
+
+ // Load the default configuration file to get the list of defined vhosts
+ ServerConfiguration configuration = new ServerConfiguration(new File(_configFile.getParent() + "/config.xml"));
+ configuration.initialise();
+ List<String> vhosts = configuration.getConfig().getList("virtualhosts.virtualhost.name");
+
+ // Make them all persistent i.e. Use DerbyMessageStore and
+ // test that it logs correctly.
+ for (String vhost : vhosts)
+ {
+ makeVirtualHostPersistent(vhost);
+ }
+ }
+
+ /**
+ * Description:
+ * Persistent MessageStores will require space on disk to persist the data.
+ * This value will be logged on startup after the MessageStore has been
+ * created.
+ * Input:
+ * Default configuration
+ * Output:
+ *
+ * <date> MST-1002 : Store location : <path>
+ *
+ * Validation Steps:
+ *
+ * 1. The MST ID is correct
+ * 2. This must occur after MST-1001
+ */
+ public void testMessageStoreStoreLocation() throws Exception
+ {
+ assertLoggingNotYetOccured(MESSAGES_STORE_PREFIX);
+
+ startBroker();
+
+ List<String> results = waitAndFindMatches(MESSAGES_STORE_PREFIX);
+
+ // Validation
+
+ assertTrue("MST messages not logged", results.size() > 0);
+
+ // Load VirtualHost list from file.
+ ServerConfiguration configuration = new ServerConfiguration(_configFile);
+ configuration.initialise();
+ List<String> vhosts = configuration.getConfig().getList("virtualhosts.virtualhost.name");
+
+ //Validate each vhost logs a creation
+ results = waitAndFindMatches("MST-1002");
+
+ assertEquals("Each vhost did not close its store.", vhosts.size(), results.size());
+
+ for (int index = 0; index < results.size(); index++)
+ {
+ String result = getLogMessage(results, index);;
+
+ // getSlize will return extract the vhost from vh(/test) -> '/test'
+ // so remove the '/' to get the name
+ String vhostName = AbstractTestLogSubject.getSlice("vh", result).substring(1);
+
+ // To get the store class used in the configuration we need to know
+ // the virtualhost name, found above. AND
+ // the index that the virtualhost is within the configuration.
+ // we can retrive that from the vhosts list previously extracted.
+ String fullStoreName = configuration.getConfig().getString("virtualhosts.virtualhost(" + vhosts.indexOf(vhostName) + ")." + vhostName + ".store.class");
+
+ // Get the Simple class name from the expected class name of o.a.q.s.s.MMS
+ String storeName = fullStoreName.substring(fullStoreName.lastIndexOf(".") + 1);
+
+ assertTrue("MST-1002 does not contain a store path" + getMessageString(result),
+ getMessageString(result).length() > 0);
+
+ assertEquals("The store name does not match expected value",
+ storeName, AbstractTestLogSubject.getSlice("ms", fromSubject(result)));
+ }
+ }
+
+ /**
+ * Description:
+ * Persistent message stores may have state on disk that they must recover
+ * during startup. As the MessageStore starts up it will report that it is
+ * about to start the recovery process by logging MST-1004. This message
+ * will always be logged for persistent MessageStores. If there is no data
+ * to recover then there will be no subsequent recovery messages.
+ * Input:
+ * Default persistent configuration
+ * Output:
+ * <date> MST-1004 : Recovery Start
+ *
+ * Validation Steps:
+ *
+ * 1. The MST ID is correct
+ * 2. The MessageStore must have first logged a creation event.
+ */
+ public void testMessageStoreRecoveryStart() throws Exception
+ {
+ assertLoggingNotYetOccured(MESSAGES_STORE_PREFIX);
+
+ startBroker();
+
+ List<String> results = waitAndFindMatches(MESSAGES_STORE_PREFIX);
+
+ // Validation
+
+ assertTrue("MST messages not logged", results.size() > 0);
+
+ // Load VirtualHost list from file.
+ ServerConfiguration configuration = new ServerConfiguration(_configFile);
+ configuration.initialise();
+ List<String> vhosts = configuration.getConfig().getList("virtualhosts.virtualhost.name");
+
+ //Validate each vhost logs a creation
+ results = waitAndFindMatches("MST-1004");
+
+ assertTrue("Each vhost did not close its store.", vhosts.size() <= results.size());
+
+ for (int index = 0; index < results.size(); index++)
+ {
+ String result = getLogMessage(results, index);;
+
+ if (getMessageString(result).contains("Recovery Start :"))
+ {
+ //Don't test queue start recoveries
+ continue;
+ }
+
+ // getSlize will return extract the vhost from vh(/test) -> '/test'
+ // so remove the '/' to get the name
+ String vhostName = AbstractTestLogSubject.getSlice("vh", result).substring(1);
+
+ // To get the store class used in the configuration we need to know
+ // the virtualhost name, found above. AND
+ // the index that the virtualhost is within the configuration.
+ // we can retrive that from the vhosts list previously extracted.
+ String fullStoreName = configuration.getConfig().getString("virtualhosts.virtualhost(" + vhosts.indexOf(vhostName) + ")." + vhostName + ".store.class");
+
+ // Get the Simple class name from the expected class name of o.a.q.s.s.MMS
+ String storeName = fullStoreName.substring(fullStoreName.lastIndexOf(".") + 1);
+
+ assertEquals("MST-1004 does have expected message", "Recovery Start",
+ getMessageString(result));
+
+ assertEquals("The store name does not match expected value",
+ storeName, AbstractTestLogSubject.getSlice("ms", fromSubject(result)));
+ }
+ }
+
+ /**
+ * Description:
+ * Once all persistent queues have been recovered and the MessageStore has completed all recovery it must logged that the recovery process has completed.
+ * Input:
+ * Default persistent configuration
+ * Output:
+ *
+ * <date> MST-1006 : Recovery Complete
+ *
+ * Validation Steps:
+ *
+ * 1. The MST ID is correct
+ * 2. This is the last message from the MessageStore during startup.
+ * 3. This must be proceeded by a MST-1006 Recovery Start.
+ */
+ public void testMessageStoreRecoveryComplete() throws Exception
+ {
+ assertLoggingNotYetOccured(MESSAGES_STORE_PREFIX);
+
+ startBroker();
+
+ List<String> results = waitAndFindMatches(MESSAGES_STORE_PREFIX);
+
+ // Validation
+
+ assertTrue("MST messages not logged", results.size() > 0);
+
+ // Load VirtualHost list from file.
+ ServerConfiguration configuration = new ServerConfiguration(_configFile);
+ configuration.initialise();
+ List<String> vhosts = configuration.getConfig().getList("virtualhosts.virtualhost.name");
+
+ //Validate each vhost logs a creation
+ results = waitAndFindMatches("MST-1006");
+
+ assertTrue("Each vhost did not close its store.", vhosts.size() <= results.size());
+
+ for (int index = 0; index < results.size(); index++)
+ {
+ String result = getLogMessage(results, index);
+
+ if (getMessageString(result).contains("Recovery Complete :"))
+ {
+ //Don't test queue start recoveries
+ continue;
+ }
+
+ // getSlize will return extract the vhost from vh(/test) -> '/test'
+ // so remove the '/' to get the name
+ String vhostName = AbstractTestLogSubject.getSlice("vh", result).substring(1);
+
+ // To get the store class used in the configuration we need to know
+ // the virtualhost name, found above. AND
+ // the index that the virtualhost is within the configuration.
+ // we can retrive that from the vhosts list previously extracted.
+ String fullStoreName = configuration.getConfig().getString("virtualhosts.virtualhost(" + vhosts.indexOf(vhostName) + ")." + vhostName + ".store.class");
+
+ // Get the Simple class name from the expected class name of o.a.q.s.s.MMS
+ String storeName = fullStoreName.substring(fullStoreName.lastIndexOf(".") + 1);
+
+ assertEquals("MST-1006 does have expected message", "Recovery Complete",
+ getMessageString(result));
+
+ assertEquals("The store name does not match expected value",
+ storeName, AbstractTestLogSubject.getSlice("ms", fromSubject(result)));
+ }
+ }
+
+ /**
+ * Description:
+ * A persistent MessageStore may have data to recover from disk. The message store will use MST-1004 to report the start of recovery for a specific queue that it has previously persisted.
+ * Input:
+ * Default persistent configuration
+ * Output:
+ *
+ * <date> MST-1004 : Recovery Start : <queue.name>
+ *
+ * Validation Steps:
+ *
+ * 1. The MST ID is correct
+ * 2. This must occur after the recovery start MST-1004 has been logged.
+ */
+ public void testMessageStoreQueueRecoveryStart() throws Exception
+ {
+ assertLoggingNotYetOccured(MESSAGES_STORE_PREFIX);
+
+ startBroker();
+
+ List<String> results = waitAndFindMatches(MESSAGES_STORE_PREFIX);
+
+ // Validation
+
+ assertTrue("MST messages not logged", results.size() > 0);
+
+ // Load VirtualHost list from file.
+ ServerConfiguration configuration = new ServerConfiguration(_configFile);
+ configuration.initialise();
+ List<String> vhosts = configuration.getConfig().getList("virtualhosts.virtualhost.name");
+
+ //Validate each vhost logs a creation
+ results = waitAndFindMatches("MST-1004 : Recovery Start :");
+
+ // We are only looking for the default queue defined in local host being
+ // recovered. If other tests have made queues in test then we want to
+ // exclude them here.
+ results = filterResultsByVirtualHost(results, "/localhost");
+
+ assertEquals("Recovered test queue not found.", 1, results.size());
+
+ String result = getLogMessage(results, 0);
+
+ // getSlize will return extract the vhost from vh(/test) -> '/test'
+ // so remove the '/' to get the name
+ String vhostName = AbstractTestLogSubject.getSlice("vh", result).substring(1);
+
+ // To get the store class used in the configuration we need to know
+ // the virtualhost name, found above. AND
+ // the index that the virtualhost is within the configuration.
+ // we can retrive that from the vhosts list previously extracted.
+ String fullStoreName = configuration.getConfig().getString("virtualhosts.virtualhost(" + vhosts.indexOf(vhostName) + ")." + vhostName + ".store.class");
+
+ // Get the Simple class name from the expected class name of o.a.q.s.s.MMS
+ String storeName = fullStoreName.substring(fullStoreName.lastIndexOf(".") + 1);
+
+ assertTrue("MST-1006 does end with queue 'test-queue':" + getMessageString(result),
+ getMessageString(result).endsWith("test-queue"));
+
+ assertEquals("The store name does not match expected value",
+ storeName, AbstractTestLogSubject.getSlice("ms", fromSubject(result)));
+
+ }
+
+ /**
+ * Description:
+ * After the queue has been recovered the store will log that recovery has been completed. The MessageStore must not report further status about the recovery of this queue after this message. In addition every MST-1004 queue recovery start message must be matched with a MST-1006 recovery complete.
+ * Input:
+ * Default persistent configuration
+ * Output:
+ *
+ * <date> MST-1006 : Recovery Complete : <queue.name>
+ *
+ * Validation Steps:
+ *
+ * 1. The MST ID is correct
+ * 2. This must occur after the queue recovery start MST-1004 has been logged.
+ * 3. The queue.name is non-empty
+ * 4. The queue.name correlates with a previous recovery start
+ */
+ public void testMessageStoreQueueRecoveryComplete() throws Exception
+ {
+ assertLoggingNotYetOccured(MESSAGES_STORE_PREFIX);
+
+ startBroker();
+
+ List<String> results = waitAndFindMatches(MESSAGES_STORE_PREFIX);
+
+ // Validation
+
+ assertTrue("MST messages not logged", results.size() > 0);
+
+ // Load VirtualHost list from file.
+ ServerConfiguration configuration = new ServerConfiguration(_configFile);
+ configuration.initialise();
+ List<String> vhosts = configuration.getConfig().getList("virtualhosts.virtualhost.name");
+
+ //Validate each vhost logs a creation
+ results = waitAndFindMatches("MST-1006 : Recovery Complete :");
+
+ // We are only looking for the default queue defined in local host being
+ // recovered. If other tests have made queues in test then we want to
+ // exclude them here.
+ results = filterResultsByVirtualHost(results, "/localhost");
+
+ assertEquals("Recovered test queue not found.", 1, results.size());
+
+ String result = getLogMessage(results, 0);
+
+ // getSlize will return extract the vhost from vh(/test) -> '/test'
+ // so remove the '/' to get the name
+ String vhostName = AbstractTestLogSubject.getSlice("vh", result).substring(1);
+
+ // To get the store class used in the configuration we need to know
+ // the virtualhost name, found above. AND
+ // the index that the virtualhost is within the configuration.
+ // we can retrive that from the vhosts list previously extracted.
+ String fullStoreName = configuration.getConfig().getString("virtualhosts.virtualhost(" + vhosts.indexOf(vhostName) + ")." + vhostName + ".store.class");
+
+ // Get the Simple class name from the expected class name of o.a.q.s.s.MMS
+ String storeName = fullStoreName.substring(fullStoreName.lastIndexOf(".") + 1);
+
+ assertTrue("MST-1006 does end with queue 'test-queue':" + getMessageString(result),
+ getMessageString(result).endsWith("test-queue"));
+
+ assertEquals("The store name does not match expected value",
+ storeName, AbstractTestLogSubject.getSlice("ms", fromSubject(result)));
+
+ results = findMatches("MST-1004 : Recovery Start : test-queue");
+
+ assertEquals("MST-1004 for test-queue not found", 1, results.size());
+ }
+
+ /**
+ * Description:
+ * A persistent queue must be persisted so that on recovery it can be restored independently of any messages that may be stored on it. This test verifies that the MessageStore will log that it has recovered 0 messages for persistent queues that do not have any messages.
+ * Input:
+ *
+ * 1. Default persistent configuration
+ * 2. Persistent queue with no messages enqueued
+ * Output:
+ *
+ * <date> MST-1005 : Recovered 0 messages for queue <queue.name>
+ *
+ * Validation Steps:
+ * 3. The MST ID is correct
+ * 4. This must occur after the queue recovery start MST-1004 has been logged.
+ * 5. The count is 0
+ * 6. 'messages' is correctly printed
+ * 7. The queue.name is non-empty
+ */
+ public void testMessageStoreQueueRecoveryCountEmpty() throws Exception
+ {
+ assertLoggingNotYetOccured(MESSAGES_STORE_PREFIX);
+
+ String queueName = getTestQueueName();
+
+ startBroker();
+ Connection connetion = getConnection();
+ Session session = connetion.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ Queue queue = session.createQueue("direct://amq.direct/" + queueName + "/" + queueName + "?durable='true'");
+
+ session.createConsumer(queue).close();
+
+ // Stop the broker so that we can test recovery
+ stopBroker();
+
+ int COUNT = 0;
+ testDurableRecoveryCount(COUNT, queueName);
+ }
+
+ /**
+ * Description:
+ * On recovery all the persistent messages that are stored on disk must be returned to the queue. MST-1005 will report the number of messages that have been recovered from disk.
+ * Input:
+ *
+ * 1. Default persistent configuration
+ * 2. Persistent queue with multiple messages enqueued
+ * Output:
+ *
+ * <date> MST-1005 : Recovered <count> messages for queue <queue.name>
+ *
+ * Validation Steps:
+ * 3. The MST ID is correct
+ * 4. This must occur after the queue recovery start MST-1004 has been logged.
+ * 5. The count is > 1
+ * 6. 'messages' is correctly printed
+ * 7. The queue.name is non-empty
+ */
+ public void testMessageStoreQueueRecoveryCountPlural() throws Exception
+ {
+ assertLoggingNotYetOccured(MESSAGES_STORE_PREFIX);
+
+ String queueName = getTestQueueName();
+
+ int COUNT = 10;
+
+ testDurableRecoveryCount(COUNT, queueName);
+ }
+
+ /**
+ * Send a set number of messages to a new durable queue, as specified. Then
+ * restart the broker and validate that they are restored.
+ *
+ * @param COUNT - the count to send
+ * @param queueName - the new queue name
+ * @throws Exception - if a problem occured.
+ */
+ private void testDurableRecoveryCount(int COUNT, String queueName) throws Exception
+ {
+ startBroker();
+ Connection connetion = getConnection();
+ Session session = connetion.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ Queue queue = session.createQueue("direct://amq.direct/" + queueName + "/" + queueName + "?durable='true'");
+
+ session.createConsumer(queue).close();
+
+ sendMessage(session, queue, COUNT);
+ try
+ {
+ connetion.close();
+
+ stopBroker();
+
+ // Clear our monitor
+ _monitor.reset();
+
+ startBroker();
+
+ List<String> results = waitAndFindMatches(MESSAGES_STORE_PREFIX);
+
+ // Validation
+
+ assertTrue("MST messages not logged", results.size() > 0);
+
+ // Load VirtualHost list from file.
+ ServerConfiguration configuration = new ServerConfiguration(_configFile);
+ configuration.initialise();
+ List<String> vhosts = configuration.getConfig().getList("virtualhosts.virtualhost.name");
+
+ //Validate each vhost logs a creation
+ results = waitAndFindMatches("MST-1004 : Recovery Start : " + queueName);
+
+ assertEquals("Recovered test queue not found.", 1, results.size());
+
+ String result = getLogMessage(results, 0);
+
+ validateMessageID("MST-1004", result);
+
+ assertTrue("MST-1004 does end with queue '" + queueName + "':" + getMessageString(result),
+ getMessageString(result).endsWith(queueName));
+
+ results = waitAndFindMatches("MST-1005");
+
+ assertTrue("Insufficient MST-1005 logged.", results.size()>0);
+
+ result = null;
+
+ // If the first message is not our queue the second one will be
+ for(String resultEntry : results)
+ {
+ // Look for first match and set that to result
+ if (resultEntry.contains(queueName))
+ {
+ result = getLog(resultEntry);
+ break;
+ }
+ }
+
+ assertNotNull("MST-1005 entry for queue:" + queueName + ". Not found", result);
+
+ // getSlize will return extract the vhost from vh(/test) -> '/test'
+ // so remove the '/' to get the name
+ String vhostName = AbstractTestLogSubject.getSlice("vh", result).substring(1);
+
+ // To get the store class used in the configuration we need to know
+ // the virtualhost name, found above. AND
+ // the index that the virtualhost is within the configuration.
+ // we can retrive that from the vhosts list previously extracted.
+ String fullStoreName = configuration.getConfig().getString("virtualhosts.virtualhost(" + vhosts.indexOf(vhostName) + ")." + vhostName + ".store.class");
+
+ // Get the Simple class name from the expected class name of o.a.q.s.s.MMS
+ String storeName = fullStoreName.substring(fullStoreName.lastIndexOf(".") + 1);
+
+ assertTrue("MST-1005 does end with queue 'test-queue':" + getMessageString(result),
+ getMessageString(result).endsWith(queueName));
+
+ assertTrue("MST-1005 does end show correct count:" + getMessageString(result),
+ getMessageString(result).contains("Recovered " + COUNT + " messages"));
+
+ assertEquals("The store name does not match expected value",
+ storeName, AbstractTestLogSubject.getSlice("ms", fromSubject(result)));
+ }
+ finally
+ {
+ //Ensure we attempt to drain the queue.
+ assertEquals("Unable to drain queue", COUNT, drainQueue(queue));
+ }
+ }
+
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/DurableQueueLoggingTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/DurableQueueLoggingTest.java
new file mode 100644
index 0000000000..32adc49521
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/DurableQueueLoggingTest.java
@@ -0,0 +1,307 @@
+/*
+ *
+ * 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.logging;
+
+import org.apache.qpid.AMQException;
+import org.apache.qpid.client.AMQSession;
+import org.apache.qpid.framing.AMQShortString;
+
+import javax.jms.Connection;
+import javax.jms.JMSException;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.naming.NamingException;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * The Queue test suite validates that the follow log messages as specified in
+ * the Functional Specification.
+ *
+ * This suite of tests validate that the Queue messages occur correctly and
+ * according to the following format:
+ *
+ * QUE-1001 : Create : [AutoDelete] [Durable|Transient] [Priority:<levels>] [Owner:<name>]
+ */
+public class DurableQueueLoggingTest extends AbstractTestLogging
+{
+ protected String DURABLE = "Durable";
+ protected String TRANSIENT = "Transient";
+ protected boolean _durable;
+
+ protected Connection _connection;
+ protected Session _session;
+ private static final String QUEUE_PREFIX = "QUE-";
+ private static int PRIORITIES = 6;
+
+ public void setUp() throws Exception
+ {
+ super.setUp();
+ //Ensure we only have logs from our test
+ _monitor.reset();
+
+ _connection = getConnection();
+ _session = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ _durable = true;
+ }
+
+ /**
+ * Description:
+ * When a simple transient queue is created then a QUE-1001 create message
+ * is expected to be logged.
+ * Input:
+ * 1. Running broker
+ * 2. Persistent Queue is created from a client
+ * Output:
+ *
+ * <date> QUE-1001 : Create : Owner: '<name>' Durable
+ *
+ * Validation Steps:
+ * 3. The QUE ID is correct
+ * 4. The Durable tag is present in the message
+ * 5. The Owner is as expected
+ *
+ * @throws javax.jms.JMSException
+ * @throws javax.naming.NamingException
+ * @throws java.io.IOException
+ */
+ public void testQueueCreateDurableExclusive() throws NamingException, JMSException, IOException
+ {
+ String queueName= getTestQueueName();
+ // To force a queue Creation Event we need to create a consumer.
+ Queue queue = (Queue) _session.createQueue("direct://amq.direct/" + queueName + "/" + queueName + "?durable='" + _durable + "'&exclusive='true'");
+
+ _session.createConsumer(queue);
+
+ List<String> results = waitForMesssage();
+
+ String clientID = _connection.getClientID();
+ assertNotNull("clientID should not be null", clientID);
+
+ validateQueueProperties(results, false, false, clientID);
+ }
+
+ /**
+ * Description:
+ * When a simple transient queue is created then a QUE-1001 create message
+ * is expected to be logged.
+ * Input:
+ * 1. Running broker
+ * 2. Persistent Queue is created from a client
+ * Output:
+ *
+ * <date> QUE-1001 : Create : Owner: '<name>' Durable
+ *
+ * Validation Steps:
+ * 3. The QUE ID is correct
+ * 4. The Durable tag is present in the message
+ * 5. The Owner is as expected
+ *
+ * @throws javax.jms.JMSException
+ * @throws javax.naming.NamingException
+ * @throws java.io.IOException
+ */
+ public void testQueueCreateDurable() throws NamingException, JMSException, IOException
+ {
+ String queueName = getTestQueueName();
+
+ // To force a queue Creation Event we need to create a consumer.
+ Queue queue = (Queue) _session.createQueue("direct://amq.direct/" + queueName + "/" + queueName + "?durable='" + _durable + "'");
+
+ _session.createConsumer(queue);
+
+ List<String> results = waitForMesssage();
+
+ validateQueueProperties(results, false, false, null);
+ }
+
+ /**
+ * Description:
+ * When a simple transient queue is created then a QUE-1001 create message
+ * is expected to be logged.
+ * Input:
+ * 1. Running broker
+ * 2. AutoDelete Persistent Queue is created from a client
+ * Output:
+ *
+ * <date> QUE-1001 : Create : Owner: '<name>' AutoDelete Durable
+ *
+ * Validation Steps:
+ * 3. The QUE ID is correct
+ * 4. The Durable tag is present in the message
+ * 5. The Owner is as expected
+ * 6. The AutoDelete tag is present in the message
+ *
+ * @throws javax.jms.JMSException
+ * @throws javax.naming.NamingException
+ * @throws java.io.IOException
+ */
+ public void testQueueCreatePersistentAutoDelete() throws NamingException, JMSException, IOException
+ {
+ String queueName = getTestQueueName();
+ // To force a queue Creation Event we need to create a consumer.
+ Queue queue = (Queue) _session.createQueue("direct://amq.direct/"+queueName+"/"+queueName+"?durable='"+_durable+"'&autodelete='true'");
+
+ _session.createConsumer(queue);
+
+ List<String> results = waitForMesssage();
+
+ validateQueueProperties(results, false, true, null);
+ }
+
+ /**
+ * Description:
+ * When a simple transient queue is created then a QUE-1001 create message
+ * is expected to be logged.
+ * Input:
+ * 1. Running broker
+ * 2. Persistent Queue is created from a client
+ * Output:
+ *
+ * <date> QUE-1001 : Create : Owner: '<name>' Durable Priority:<levels>
+ *
+ * Validation Steps:
+ * 3. The QUE ID is correct
+ * 4. The Durable tag is present in the message
+ * 5. The Owner is as expected
+ * 6. The Priority level is correctly set
+ *
+ * @throws javax.jms.JMSException
+ * @throws javax.naming.NamingException
+ * @throws java.io.IOException
+ */
+ public void testCreateQueuePersistentPriority() throws NamingException, JMSException, IOException, AMQException
+ {
+ // To Create a Priority queue we need to use AMQSession specific code
+ final Map<String, Object> arguments = new HashMap<String, Object>();
+ arguments.put("x-qpid-priorities", PRIORITIES);
+ // Need to create a queue that does not exist so use test name
+ final String queueName = getTestQueueName();
+ ((AMQSession) _session).createQueue(new AMQShortString(queueName), false, _durable, false, arguments);
+
+ Queue queue = (Queue) _session.createQueue("direct://amq.direct/"+queueName+"/"+queueName+"?durable='"+_durable+"'&autodelete='false'");
+
+
+ //Need to create a Consumer to ensure that the log has had time to write
+ // as the above Create is Asynchronous
+ _session.createConsumer(queue);
+
+ List<String> results = waitForMesssage();
+
+ // Only 1 Queue message should hav been logged
+ assertEquals("Result set size not as expected", 1, results.size());
+
+ validateQueueProperties(results, true, false, null);
+ }
+
+ /**
+ * Description:
+ * When a simple transient queue is created then a QUE-1001 create message
+ * is expected to be logged.
+ * Input:
+ * 1. Running broker
+ * 2. AutoDelete Persistent Queue is created from a client
+ * Output:
+ *
+ * <date> QUE-1001 : Create : Owner: '<name>' Durable Priority:<levels>
+ *
+ * Validation Steps:
+ * 3. The QUE ID is correct
+ * 4. The Durable tag is present in the message
+ * 5. The Owner is as expected
+ * 6. The AutoDelete tag is present in the message
+ * 7. The Priority level is correctly set
+ *
+ * @throws javax.jms.JMSException
+ * @throws javax.naming.NamingException
+ * @throws java.io.IOException
+ */
+ public void testCreateQueuePersistentAutoDeletePriority() throws NamingException, JMSException, IOException, AMQException
+ {
+ // To Create a Priority queue we need to use AMQSession specific code
+ final Map<String, Object> arguments = new HashMap<String, Object>();
+ arguments.put("x-qpid-priorities", PRIORITIES);
+ // Need to create a queue that does not exist so use test name
+ final String queueName = getTestQueueName() + "-autoDeletePriority";
+ ((AMQSession) _session).createQueue(new AMQShortString(queueName), true, _durable, false, arguments);
+
+ Queue queue = (Queue) _session.createQueue("direct://amq.direct/"+queueName+"/"+queueName+"?durable='"+_durable+"'&autodelete='true'");
+
+
+ //Need to create a Consumer to ensure that the log has had time to write
+ // as the above Create is Asynchronous
+ _session.createConsumer(queue);
+
+ List<String> results = waitForMesssage();
+
+ validateQueueProperties(results, true, true, null);
+ }
+
+ private List<String> waitForMesssage() throws IOException
+ {
+ // Validation
+ // Ensure we have received the QUE log msg.
+ waitForMessage("QUE-1001");
+
+ List<String> results = findMatches(QUEUE_PREFIX);
+
+ // Only 1 Queue message should hav been logged
+ assertEquals("Result set size not as expected", 1, results.size());
+
+ return results;
+ }
+
+ public void validateQueueProperties(List<String> results, boolean hasPriority, boolean hasAutodelete, String clientID)
+ {
+ String log = getLogMessage(results, 0);
+
+ // Message Should be a QUE-1001
+ validateMessageID("QUE-1001", log);
+
+ // Queue is Durable
+ assertEquals(DURABLE + " keyword not correct in log entry",
+ _durable, fromMessage(log).contains(DURABLE));
+
+ assertEquals(TRANSIENT + " keyword not correct in log entry.",
+ !_durable, fromMessage(log).contains(TRANSIENT));
+
+ // Queue is Priority
+ assertEquals("Unexpected priority status:" + fromMessage(log), hasPriority,
+ fromMessage(log).contains("Priority: " + PRIORITIES));
+
+ // Queue is AutoDelete
+ assertEquals("Unexpected AutoDelete status:" + fromMessage(log), hasAutodelete,
+ fromMessage(log).contains("AutoDelete"));
+
+ if(clientID != null)
+ {
+ assertTrue("Queue does not have correct owner value:" + fromMessage(log),
+ fromMessage(log).contains("Owner: " + clientID));
+ }
+ else
+ {
+ assertFalse("Queue should not contain Owner tag:" + fromMessage(log),
+ fromMessage(log).contains("Owner"));
+ }
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/ExchangeLoggingTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/ExchangeLoggingTest.java
new file mode 100644
index 0000000000..1e48f34f99
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/ExchangeLoggingTest.java
@@ -0,0 +1,217 @@
+/*
+ *
+ * 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.logging;
+
+import org.apache.qpid.client.AMQConnection;
+import org.apache.qpid.client.AMQSession_0_10;
+import org.apache.qpid.framing.AMQFrame;
+import org.apache.qpid.framing.AMQShortString;
+import org.apache.qpid.framing.ExchangeDeleteBody;
+import org.apache.qpid.framing.ExchangeDeleteOkBody;
+import org.apache.qpid.framing.amqp_8_0.MethodRegistry_8_0;
+
+import javax.jms.Connection;
+import javax.jms.JMSException;
+import javax.jms.Queue;
+import javax.jms.Session;
+import java.io.IOException;
+import java.util.List;
+
+/**
+ * Exchange
+ *
+ * The Exchange test suite validates that the follow log messages as specified in the Functional Specification.
+ *
+ * This suite of tests validate that the Exchange messages occur correctly and according to the following format:
+ *
+ * EXH-1001 : Create : [Durable] Type:<value> Name:<value>
+ * EXH-1002 : Deleted
+ */
+public class ExchangeLoggingTest extends AbstractTestLogging
+{
+
+ static final String EXH_PREFIX = "EXH-";
+
+ Connection _connection;
+ Session _session;
+ Queue _queue;
+ String _name;
+ String _type;
+
+ @Override
+ public void setUp() throws Exception
+ {
+ super.setUp();
+
+ _connection = getConnection();
+
+ _session = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ _type = "direct";
+ _name = getTestQueueName()+ "-exchange";
+
+ _queue = _session.createQueue(_type + "://" + _name + "/queue/queue");
+
+ }
+
+ /**
+ * Description:
+ * When a durable exchange is created an EXH-1001 message is logged with the Durable tag. This will be the first message from this exchange.
+ * Input:
+ *
+ * 1. Running broker
+ * 2. Client requests a durable exchange be created.
+ * Output:
+ *
+ * <date> EXH-1001 : Create : Durable Type:<value> Name:<value>
+ *
+ * Validation Steps:
+ * 3. The EXH ID is correct
+ * 4. The Durable tag is present in the message
+ */
+
+ public void testExchangeCreateDurable() throws JMSException, IOException
+ {
+ // The client cannot create durable exchanges lets just look at the
+ // ones the broker creates at startup.
+
+ // They should all be durable
+
+ // Ensure we have received the EXH log msg.
+ waitForMessage("EXH-1001");
+
+ List<String> results = findMatches(EXH_PREFIX);
+
+ assertTrue("No Results found for Exchange.", results.size()>0);
+
+ validateExchangeCreate(results, true, false);
+ }
+
+ /**
+ * Description:
+ * When an exchange is created an EXH-1001 message is logged. This will be the first message from this exchange.
+ * Input:
+ *
+ * 1. Running broker
+ * 2. Client requests an exchange be created.
+ * Output:
+ *
+ * <date> EXH-1001 : Create : Type:<value> Name:<value>
+ *
+ * Validation Steps:
+ * 3. The EXH ID is correct
+ */
+ public void testExchangeCreate() throws JMSException, IOException
+ {
+ //Ignore broker startup messages
+ _monitor.reset();
+
+ _session.createConsumer(_queue);
+ // Ensure we have received the EXH log msg.
+ waitForMessage("EXH-1001");
+
+ List<String> results = findMatches(EXH_PREFIX);
+
+ assertEquals("Result set larger than expected.", 1, results.size());
+
+ validateExchangeCreate(results, false, true);
+ }
+
+ private void validateExchangeCreate(List<String> results, boolean durable, boolean checkNameAndType)
+ {
+ String log = getLogMessage(results, 0);
+ String message = getMessageString(fromMessage(log));
+
+ validateMessageID("EXH-1001", log);
+
+ assertTrue("Log Message does not start with create:" + message,
+ message.startsWith("Create"));
+
+ assertEquals("Unexpected Durable state:" + message, durable,
+ message.contains("Durable"));
+
+ if(checkNameAndType)
+ {
+ assertTrue("Log Message does not contain Type:" + message,
+ message.contains("Type: " + _type));
+ assertTrue("Log Message does not contain Name:" + message,
+ message.contains("Name: " + _name));
+ }
+ }
+
+ /**
+ * Description:
+ * An Exchange can be deleted through an AMQP ExchangeDelete method. When this is successful an EXH-1002 Delete message will be logged. This will be the last message from this exchange.
+ * Input:
+ *
+ * 1. Running broker
+ * 2. A new Exchange has been created
+ * 3. Client requests that the new exchange be deleted.
+ * Output:
+ *
+ * <date> EXH-1002 : Deleted
+ *
+ * Validation Steps:
+ * 4. The EXH ID is correct
+ * 5. There is a corresponding EXH-1001 Create message logged.
+ */
+ public void testExchangeDelete() throws Exception, IOException
+ {
+ //Ignore broker startup messages
+ _monitor.reset();
+
+ //create the exchange by creating a consumer
+ _session.createConsumer(_queue);
+
+ //now delete the exchange
+ if(isBroker010())
+ {
+ ((AMQSession_0_10) _session).sendExchangeDelete(_name, false);
+ }
+ else
+ {
+ MethodRegistry_8_0 registry = new MethodRegistry_8_0();
+
+ ExchangeDeleteBody body = registry.createExchangeDeleteBody(0, new AMQShortString(_name), false, true);
+
+ AMQFrame exchangeDeclare = body.generateFrame(0);
+
+ ((AMQConnection) _connection).getProtocolHandler().syncWrite(exchangeDeclare, ExchangeDeleteOkBody.class);
+ }
+
+ //Wait and ensure we get our last EXH-1002 msg
+ waitForMessage("EXH-1002");
+
+ List<String> results = findMatches(EXH_PREFIX);
+
+ assertEquals("Result set larger than expected.", 2, results.size());
+
+ validateExchangeCreate(results, false, false);
+
+ String log = getLogMessage(results, 1);
+ validateMessageID("EXH-1002", log);
+
+ String message = getMessageString(fromMessage(log));
+ assertEquals("Log Message not as expected", "Deleted", message);
+
+ }
+
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/ManagementLoggingTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/ManagementLoggingTest.java
new file mode 100644
index 0000000000..595c0d5f35
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/ManagementLoggingTest.java
@@ -0,0 +1,305 @@
+/*
+ *
+ * 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.logging;
+
+import junit.framework.AssertionFailedError;
+
+import org.apache.qpid.util.LogMonitor;
+
+import java.util.List;
+import java.io.File;
+
+/**
+ * Management Console Test Suite
+ *
+ * The Management Console test suite validates that the follow log messages as specified in the Functional Specification.
+ *
+ * This suite of tests validate that the management console messages occur correctly and according to the following format:
+ *
+ * MNG-1001 : Startup
+ * MNG-1002 : Starting : <service> : Listening on port <Port>
+ * MNG-1003 : Shutting down : <service> : port <Port>
+ * MNG-1004 : Ready
+ * MNG-1005 : Stopped
+ * MNG-1006 : Using SSL Keystore : <path>
+ */
+public class ManagementLoggingTest extends AbstractTestLogging
+{
+ private static final String MNG_PREFIX = "MNG-";
+
+ public void setUp() throws Exception
+ {
+ setLogMessagePrefix();
+
+ // We either do this here or have a null check in tearDown.
+ // As when this test is run against profiles other than java it will NPE
+ _monitor = new LogMonitor(_outputFile);
+ //We explicitly do not call super.setUp as starting up the broker is
+ //part of the test case.
+
+ }
+
+ /**
+ * Description:
+ * Using the startup configuration validate that the management startup
+ * message is logged correctly.
+ * Input:
+ * Standard configuration with management enabled
+ * Output:
+ *
+ * <date> MNG-1001 : Startup
+ *
+ * Constraints:
+ * This is the FIRST message logged by MNG
+ * Validation Steps:
+ *
+ * 1. The BRK ID is correct
+ * 2. This is the FIRST message logged by MNG
+ */
+ public void testManagementStartupEnabled() throws Exception
+ {
+ // This test only works on external java brokers due to the fact that
+ // Management is disabled on InVM brokers.
+ if (isJavaBroker() && isExternalBroker())
+ {
+ startBrokerAndCreateMonitor(true, false);
+
+ // Ensure we have received the MNG log msg.
+ waitForMessage("MNG-1001");
+
+ List<String> results = findMatches(MNG_PREFIX);
+
+ try
+ {
+ // Validation
+
+ assertTrue("MNGer message not logged", results.size() > 0);
+
+ String log = getLogMessage(results, 0);
+
+ //1
+ validateMessageID("MNG-1001", log);
+
+ //2
+ //There will be 2 copies of the startup message (one via SystemOut, and one via Log4J)
+ results = findMatches("MNG-1001");
+ assertEquals("Unexpected startup message count.",
+ 2, results.size());
+
+ //3
+ assertEquals("Startup log message is not 'Startup'.", "Startup",
+ getMessageString(log));
+ }
+ catch (AssertionFailedError afe)
+ {
+ dumpLogs(results, _monitor);
+ throw afe;
+ }
+ }
+ }
+
+ /**
+ * Description:
+ * Verify that when management is disabled in the configuration file the
+ * startup message is not logged.
+ * Input:
+ * Standard configuration with management disabled
+ * Output:
+ * NO MNG messages
+ * Validation Steps:
+ *
+ * 1. Validate that no MNG messages are produced.
+ */
+ public void testManagementStartupDisabled() throws Exception
+ {
+ // This test only works on external java brokers due to the fact that
+ // Management is disabled on InVM brokers.
+ if (isJavaBroker() && isExternalBroker())
+ {
+ startBrokerAndCreateMonitor(false, false);
+
+ List<String> results = findMatches(MNG_PREFIX);
+ try
+ {
+ // Validation
+
+ assertEquals("MNGer messages logged", 0, results.size());
+ }
+ catch (AssertionFailedError afe)
+ {
+ dumpLogs(results, _monitor);
+ throw afe;
+ }
+ }
+ }
+
+ /**
+ * The two MNG-1002 messages are logged at the same time so lets test them
+ * at the same time.
+ *
+ * Description:
+ * Using the default configuration validate that the RMI Registry socket is
+ * correctly reported as being opened
+ *
+ * Input:
+ * The default configuration file
+ * Output:
+ *
+ * <date> MESSAGE MNG-1002 : Starting : RMI Registry : Listening on port 8999
+ *
+ * Constraints:
+ * The RMI ConnectorServer and Registry log messages do not have a prescribed order
+ * Validation Steps:
+ *
+ * 1. The MNG ID is correct
+ * 2. The specified port is the correct '8999'
+ *
+ * Description:
+ * Using the default configuration validate that the RMI ConnectorServer
+ * socket is correctly reported as being opened
+ *
+ * Input:
+ * The default configuration file
+ * Output:
+ *
+ * <date> MESSAGE MNG-1002 : Starting : RMI ConnectorServer : Listening on port 9099
+ *
+ * Constraints:
+ * The RMI ConnectorServer and Registry log messages do not have a prescribed order
+ * Validation Steps:
+ *
+ * 1. The MNG ID is correct
+ * 2. The specified port is the correct '9099'
+ */
+ public void testManagementStartupRMIEntries() throws Exception
+ {
+ // This test only works on external java brokers due to the fact that
+ // Management is disabled on InVM brokers.
+ if (isJavaBroker() && isExternalBroker())
+ {
+ startBrokerAndCreateMonitor(true, false);
+
+ List<String> results = waitAndFindMatches("MNG-1002");
+ try
+ {
+ // Validation
+
+ //There will be 4 startup messages (two via SystemOut, and two via Log4J)
+ assertEquals("Unexpected MNG-1002 message count", 4, results.size());
+
+ String log = getLogMessage(results, 0);
+
+ //1
+ validateMessageID("MNG-1002", log);
+
+ //Check the RMI Registry port is as expected
+ int mPort = getPort() + (DEFAULT_MANAGEMENT_PORT - DEFAULT_PORT);
+ assertTrue("RMI Registry port not as expected(" + mPort + ").:" + getMessageString(log),
+ getMessageString(log).endsWith(String.valueOf(mPort)));
+
+ log = getLogMessage(results, 2);
+
+ //1
+ validateMessageID("MNG-1002", log);
+
+ // We expect the RMI Registry port (the defined 'management port') to be
+ // 100 lower than the JMX RMIConnector Server Port (the actual JMX server)
+ int jmxPort = mPort + 100;
+ assertTrue("JMX RMIConnectorServer port not as expected(" + jmxPort + ").:" + getMessageString(log),
+ getMessageString(log).endsWith(String.valueOf(jmxPort)));
+ }
+ catch (AssertionFailedError afe)
+ {
+ dumpLogs(results, _monitor);
+ throw afe;
+ }
+ }
+ }
+
+ /**
+ * Description:
+ * Using the default configuration with SSL enabled for the management port the SSL Keystore path should be reported via MNG-1006
+ * Input:
+ * Management SSL enabled default configuration.
+ * Output:
+ *
+ * <date> MESSAGE MNG-1006 : Using SSL Keystore : test_resources/ssl/keystore.jks
+ *
+ * Validation Steps:
+ *
+ * 1. The MNG ID is correct
+ * 2. The keystore path is as specified in the configuration
+ */
+ public void testManagementStartupSSLKeystore() throws Exception
+ {
+ // This test only works on external java brokers due to the fact that
+ // Management is disabled on InVM brokers.
+ if (isJavaBroker() && isExternalBroker())
+ {
+ startBrokerAndCreateMonitor(true, true);
+
+ List<String> results = waitAndFindMatches("MNG-1006");
+ try
+ {
+ // Validation
+
+ assertTrue("MNGer message not logged", results.size() > 0);
+
+ String log = getLogMessage(results, 0);
+
+ //1
+ validateMessageID("MNG-1006", log);
+
+ // Validate we only have two MNG-1002 (one via stdout, one via log4j)
+ results = findMatches("MNG-1006");
+ assertEquals("Upexpected SSL Keystore message count",
+ 2, results.size());
+
+ // Validate the keystore path is as expected
+ assertTrue("SSL Keystore entry expected.:" + getMessageString(log),
+ getMessageString(log).endsWith(new File(getConfigurationStringProperty("management.ssl.keyStorePath")).getName()));
+ }
+ catch (AssertionFailedError afe)
+ {
+ dumpLogs(results, _monitor);
+ throw afe;
+ }
+ }
+
+ }
+
+ private void startBrokerAndCreateMonitor(boolean managementEnabled, boolean useManagementSSL) throws Exception
+ {
+ //Ensure management is on
+ setConfigurationProperty("management.enabled", String.valueOf(managementEnabled));
+
+ if(useManagementSSL)
+ {
+ // This test requires we have an ssl connection
+ setConfigurationProperty("management.ssl.enabled", "true");
+ }
+
+ startBroker();
+
+ // Now we can create the monitor as _outputFile will now be defined
+ _monitor = new LogMonitor(_outputFile);
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/MemoryMessageStoreLoggingTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/MemoryMessageStoreLoggingTest.java
new file mode 100644
index 0000000000..34d9e1f057
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/MemoryMessageStoreLoggingTest.java
@@ -0,0 +1,186 @@
+/*
+*
+* 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.logging;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.qpid.server.configuration.ServerConfiguration;
+import org.apache.qpid.server.logging.subjects.AbstractTestLogSubject;
+import org.apache.qpid.util.LogMonitor;
+
+/**
+ * The MessageStore test suite validates that the follow log messages as
+ * specified in the Functional Specification.
+ *
+ * This suite of tests validate that the MessageStore messages occur correctly
+ * and according to the following format:
+ *
+ * MST-1001 : Created : <name>
+ * MST-1003 : Closed
+ *
+ * NOTE: Only for Persistent Stores
+ * MST-1002 : Store location : <path>
+ * MST-1004 : Recovery Start [: <queue.name>]
+ * MST-1005 : Recovered <count> messages for queue <queue.name>
+ * MST-1006 : Recovery Complete [: <queue.name>]
+ */
+public class MemoryMessageStoreLoggingTest extends AbstractTestLogging
+{
+ protected static final String MESSAGES_STORE_PREFIX = "MST-";
+
+ public void setUp() throws Exception
+ {
+ //We explicitly do not call super.setUp as starting up the broker is
+ //part of the test case.
+ // So we have to make the new Log Monitor here
+
+ _monitor = new LogMonitor(_outputFile);
+ }
+
+ /**
+ * Description:
+ * During Virtualhost startup a MessageStore will be created. The first MST
+ * message that must be logged is the MST-1001 MessageStore creation.
+ * Input:
+ * Default configuration
+ * Output:
+ * <date> MST-1001 : Created : <name>
+ *
+ * Validation Steps:
+ *
+ * 1. The MST ID is correct
+ * 2. The <name> is the correct MessageStore type as specified in the Default configuration
+ *
+ * @throws Exception caused by broker startup
+ */
+ public void testMessageStoreCreation() throws Exception
+ {
+ assertLoggingNotYetOccured(MESSAGES_STORE_PREFIX);
+
+ super.setUp();
+
+ List<String> results = waitAndFindMatches(MESSAGES_STORE_PREFIX);
+
+ // Validation
+
+ assertTrue("MST messages not logged", results.size() > 0);
+
+ String log = getLogMessage(results, 0);
+ //1
+ assertEquals("MST-1001 is not the first MST message", "MST-1001", getMessageID(fromMessage(log)));
+
+ //Validate each vhost logs a creation
+ results = waitAndFindMatches("MST-1001");
+
+ // Load VirtualHost list from file.
+ List<String> vhosts = Arrays.asList(getServerConfig().getVirtualHosts());
+
+ assertEquals("Each vhost did not create a store.", vhosts.size(), results.size());
+
+ for (int index = 0; index < results.size(); index++)
+ {
+ String result = getLogMessage(results, index);
+
+ // getSlice will return extract the vhost from vh(/test) -> '/test'
+ // so remove the '/' to get the name
+ String vhostName = AbstractTestLogSubject.getSlice("vh", result).substring(1);
+
+ // Get the store class used in the configuration for the virtualhost.
+ String fullStoreName = getServerConfig().getVirtualHostConfig(vhostName).getMessageStoreClass();
+
+ // Get the Simple class name from the expected class name of o.a.q.s.s.MMS
+ String storeName = fullStoreName.substring(fullStoreName.lastIndexOf(".") + 1);
+
+ assertTrue("MST-1001 does not contains correct store name:"
+ + storeName + ":" + result, getMessageString(result).endsWith(storeName));
+
+ assertEquals("The store name does not match expected value",
+ storeName, AbstractTestLogSubject.getSlice("ms", fromSubject(result)));
+ }
+ }
+
+ /**
+ * Description:
+ * During shutdown the MessageStore will also cleanly close. When this has
+ * completed a MST-1003 closed message will be logged. No further messages
+ * from this MessageStore will be logged after this message.
+ *
+ * Input:
+ * Default configuration
+ * Output:
+ * <date> MST-1003 : Closed
+ *
+ * Validation Steps:
+ *
+ * 1. The MST ID is correct
+ * 2. This is teh last log message from this MessageStore
+ *
+ * @throws Exception caused by broker startup
+ */
+ public void testMessageStoreClose() throws Exception
+ {
+ assertLoggingNotYetOccured(MESSAGES_STORE_PREFIX);
+
+ super.setUp();
+
+ //Stop the broker so we get the close messages.
+ stopBroker();
+
+ List<String> results = waitAndFindMatches(MESSAGES_STORE_PREFIX);
+
+ // Validation
+
+ assertTrue("MST messages not logged", results.size() > 0);
+
+ // Load VirtualHost list from file.
+ ServerConfiguration configuration = new ServerConfiguration(_configFile);
+ configuration.initialise();
+ List<String> vhosts = Arrays.asList(configuration.getVirtualHosts());
+
+ //Validate each vhost logs a creation
+ results = waitAndFindMatches("MST-1003");
+
+ assertEquals("Each vhost did not close its store.", vhosts.size(), results.size());
+
+ for (int index = 0; index < results.size(); index++)
+ {
+ String result = getLogMessage(results, index);
+
+ // getSlice will return extract the vhost from vh(/test) -> '/test'
+ // so remove the '/' to get the name
+ String vhostName = AbstractTestLogSubject.getSlice("vh", result).substring(1);
+
+ // Get the store class used in the configuration for the virtualhost.
+ String fullStoreName = configuration.getVirtualHostConfig(vhostName).getMessageStoreClass();
+
+ // Get the Simple class name from the expected class name of o.a.q.s.s.MMS
+ String storeName = fullStoreName.substring(fullStoreName.lastIndexOf(".") + 1);
+
+ assertEquals("MST-1003 does not close:",
+ "Closed", getMessageString(result));
+
+ assertEquals("The store name does not match expected value",
+ storeName, AbstractTestLogSubject.getSlice("ms", fromSubject(result)));
+ }
+ }
+
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/QueueLoggingTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/QueueLoggingTest.java
new file mode 100644
index 0000000000..b8a42c0ab3
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/QueueLoggingTest.java
@@ -0,0 +1,183 @@
+/*
+ *
+ * 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.logging;
+
+import org.apache.qpid.client.AMQSession;
+import org.apache.qpid.client.failover.FailoverException;
+import org.apache.qpid.server.logging.subjects.AbstractTestLogSubject;
+import org.apache.qpid.framing.AMQShortString;
+import org.apache.qpid.AMQException;
+
+import javax.jms.Connection;
+import javax.jms.Session;
+import javax.jms.Queue;
+import javax.jms.JMSException;
+import javax.naming.NamingException;
+import java.util.List;
+import java.io.IOException;
+
+/**
+ * The Queue test suite validates that the follow log messages as specified in
+ * the Functional Specification.
+ *
+ * This suite of tests validate that the Queue messages occur correctly and
+ * according to the following format:
+ *
+ * QUE-1002 : Deleted
+ */
+public class QueueLoggingTest extends AbstractTestLogging
+{
+ protected Connection _connection;
+ protected Session _session;
+ private static final String QUEUE_PREFIX = "QUE-";
+
+ public void setUp() throws Exception
+ {
+ super.setUp();
+ //Remove broker startup logging messages
+ _monitor.reset();
+
+ _connection = getConnection();
+ _session = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ }
+
+ /**
+ * Description:
+ * An explict QueueDelete request must result in a QUE-1002 Deleted message
+ * being logged. This can be done via an explict AMQP QueueDelete method.
+ * Input:
+ *
+ * 1. Running Broker
+ * 2. Queue created on the broker with no subscribers
+ * 3. Client requests the queue be deleted via a QueueDelete
+ * Output:
+ *
+ * <date> QUE-1002 : Deleted
+ *
+ * Validation Steps:
+ *
+ * 4. The QUE ID is correct
+ *
+ * @throws java.io.IOException
+ * @throws javax.jms.JMSException
+ * @throws javax.naming.NamingException
+ */
+ public void testQueueDelete() throws NamingException, JMSException, IOException, FailoverException, AMQException
+ {
+ // To force a queue Creation Event we need to create a consumer.
+ Queue queue = _session.createQueue(getTestQueueName());
+
+ _session.createConsumer(queue);
+
+ // Delete Queue
+ ((AMQSession)_session).sendQueueDelete(new AMQShortString(queue.getQueueName()));
+
+ //Perform a synchronous action to ensure that the above log will be on disk
+ _session.close();
+
+ // Validation
+ //Ensure that we wait for the QUE log message
+ waitAndFindMatches("QUE-1002");
+
+ List<String> results = findMatches(QUEUE_PREFIX);
+
+ // Only 1 Queue message should hav been logged
+ assertEquals("Result set size not as expected", 2, results.size());
+
+ String log = getLogMessage(results, 0);
+
+ // Message Should be a QUE-1001
+ validateMessageID("QUE-1001", log);
+
+ String createdQueueName = AbstractTestLogSubject.getSlice("qu", fromSubject(log));
+
+ log = getLogMessage(results, 1);
+ // Message Should be a QUE-1002
+ validateMessageID("QUE-1002", log);
+
+ assertEquals("Log Message is incorrect ", "Deleted", getMessageString(fromMessage(log)));
+
+ assertEquals("Queue Delete not for created queue:", createdQueueName,
+ AbstractTestLogSubject.getSlice("qu", fromSubject(log)));
+ }
+
+
+ /**
+ * Description:
+ * An explict QueueDelete request must result in a QUE-1002 Deleted message
+ * being logged. This can be done via an explict AMQP QueueDelete method.
+ * Input:
+ *
+ * 1. Running Broker
+ * 2. Queue created on the broker with no subscribers
+ * 3. Client creates a temporary queue then disconnects
+ * Output:
+ *
+ * <date> QUE-1002 : Deleted
+ *
+ * Validation Steps:
+ *
+ * 4. The QUE ID is correct
+ *
+ * @throws java.io.IOException
+ * @throws javax.jms.JMSException
+ * @throws javax.naming.NamingException
+ */
+ public void testQueueAutoDelete() throws NamingException, JMSException, IOException
+ {
+ // Create a temporary queue so that when we consume from it and
+ // then close the consumer it will be autoDeleted.
+ _session.createConsumer(_session.createTemporaryQueue()).close();
+
+ if(isBroker010())
+ {
+ //auto-delete is at session close for 0-10
+ _session.close();
+ }
+
+ // Validation
+ //Ensure that we wait for the QUE log message
+ waitAndFindMatches("QUE-1002");
+
+ List<String> results = findMatches(QUEUE_PREFIX);
+
+ // Only 1 Queue message should hav been logged
+ assertEquals("Result set size not as expected", 2, results.size());
+
+ String log = getLogMessage(results, 0);
+
+ // Message Should be a QUE-1001
+ validateMessageID("QUE-1001", log);
+
+ String createdQueueName = AbstractTestLogSubject.getSlice("qu", fromSubject(log));
+
+ log = getLogMessage(results, 1);
+ // Message Should be a QUE-1002
+ validateMessageID("QUE-1002", log);
+
+ assertEquals("Log Message is incorrect ", "Deleted", getMessageString(fromMessage(log)));
+
+ assertEquals("Queue Delete not for created queue:", createdQueueName,
+ AbstractTestLogSubject.getSlice("qu", fromSubject(log)));
+
+ }
+
+} \ No newline at end of file
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/SubscriptionLoggingTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/SubscriptionLoggingTest.java
new file mode 100644
index 0000000000..6e156f091e
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/SubscriptionLoggingTest.java
@@ -0,0 +1,458 @@
+/*
+ *
+ * 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.logging;
+
+import junit.framework.AssertionFailedError;
+import org.apache.qpid.client.AMQConnection;
+
+import javax.jms.Connection;
+import javax.jms.JMSException;
+import javax.jms.MessageConsumer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.jms.Topic;
+import javax.jms.Message;
+import java.io.IOException;
+import java.util.List;
+
+/**
+ * Subscription
+ *
+ * The Subscription test suite validates that the follow log messages as specified in the Functional Specification.
+ *
+ * This suite of tests validate that the Subscription messages occur correctly and according to the following format:
+ *
+ * SUB-1001 : Create : [Durable] [Arguments : <key=value>]
+ * SUB-1002 : Close
+ * SUB-1003 : State : <state>
+ */
+public class SubscriptionLoggingTest extends AbstractTestLogging
+{
+ static final String SUB_PREFIX = "SUB-";
+
+ Connection _connection;
+ Session _session;
+ Queue _queue;
+ Topic _topic;
+
+ @Override
+ public void setUp() throws Exception
+ {
+ super.setUp();
+ //Remove broker startup logging messages
+ _monitor.reset();
+
+ _connection = getConnection();
+
+ _session = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ _queue = (Queue) getInitialContext().lookup(QUEUE);
+ _topic = (Topic) getInitialContext().lookup(TOPIC);
+ }
+
+ /**
+ * Description:
+ * When a Subscription is created it will be logged. This test validates that Subscribing to a transient queue is correctly logged.
+ * Input:
+ *
+ * 1. Running Broker
+ * 2. Create a new Subscription to a transient queue/topic.
+ * Output: 6
+ *
+ * <date> SUB-1001 : Create
+ *
+ * Validation Steps:
+ * 3. The SUB ID is correct
+ *
+ * @throws java.io.IOException - if there is a problem getting the matches
+ * @throws javax.jms.JMSException - if there is a problem creating the consumer
+ */
+ public void testSubscriptionCreate() throws JMSException, IOException
+ {
+ _session.createConsumer(_queue);
+
+ //Validate
+
+ //Ensure that we wait for the SUB log message
+ waitAndFindMatches("SUB-1001");
+
+ List<String> results = findMatches(SUB_PREFIX);
+
+ assertEquals("Result set larger than expected.", 1, results.size());
+
+ String log = getLogMessage(results, 0);
+
+ validateMessageID("SUB-1001", log);
+
+ assertEquals("Log Message not as expected", "Create", getMessageString(fromMessage(log)));
+ }
+
+ /**
+ * Description:
+ * The creation of a Durable Subscription, such as a JMS DurableTopicSubscriber will result in an extra Durable tag being included in the Create log message
+ * Input:
+ *
+ * 1. Running Broker
+ * 2. Creation of a JMS DurableTopicSubiber
+ * Output:
+ *
+ * <date> SUB-1001 : Create : Durable
+ *
+ * Validation Steps:
+ * 3. The SUB ID is correct
+ * 4. The Durable tag is present in the message
+ * NOTE: A Subscription is not Durable, the queue it consumes from is.
+ *
+ * @throws java.io.IOException - if there is a problem getting the matches
+ * @throws javax.jms.JMSException - if there is a problem creating the consumer
+ */
+ public void testSubscriptionCreateDurable() throws JMSException, IOException
+ {
+ _session.createDurableSubscriber(_topic, getName());
+
+ //Validate
+ //Ensure that we wait for the SUB log message
+ waitAndFindMatches("SUB-1001");
+
+ List<String> results = findMatches(SUB_PREFIX);
+
+ assertEquals("Result set not as expected.", 1, results.size());
+
+ String log = getLogMessage(results, 0);
+
+ validateMessageID("SUB-1001", log);
+
+ String message = getMessageString(fromMessage(log));
+ assertTrue("Durable not on log message:" + message, message.contains("Durable"));
+ }
+
+ /**
+ * Description:
+ * The creation of a QueueBrowser will provides a number arguments and so should form part of the SUB-1001 Create message.
+ * Input:
+ *
+ * 1. Running Broker
+ * 2. Java Client creates a QueueBroweser
+ * Output:
+ *
+ * <date> SUB-1001 : Create : Arguments : <key=value>
+ *
+ * Validation Steps:
+ * 3. The SUB ID is correct
+ * 4. The Arguments are present in the message
+ * 5. Arguments keys include AutoClose and Browser.
+ *
+ * @throws java.io.IOException - if there is a problem getting the matches
+ * @throws javax.jms.JMSException - if there is a problem creating the consumer
+ */
+ public void testSubscriptionCreateQueueBrowser() throws JMSException, IOException
+ {
+ _session.createBrowser(_queue);
+
+ //Validate
+ //Ensure that we wait for the SUB log message
+ waitAndFindMatches("SUB-1001");
+
+ List<String> results = findMatches(SUB_PREFIX);
+
+ assertEquals("Result set larger than expected.", 2, results.size());
+
+ String log = getLogMessage(results, 0);
+
+ validateMessageID("SUB-1001", log);
+
+ String message = getMessageString(fromMessage(log));
+ assertTrue("Browser not on log message:" + message, message.contains("Browser"));
+ if(!isBroker010())
+ {
+ assertTrue("AutoClose not on log message:" + message, message.contains("AutoClose"));
+ }
+
+ // Beacause it is an auto close and we have no messages on the queue we
+ // will get a close message
+ log = getLogMessage(results, 1);
+ validateMessageID("SUB-1002", log);
+
+ }
+
+ /**
+ * Description:
+ * The creation of a Subscriber with a JMS Selector will result in the Argument field being populated. These argument key/value pairs are then shown in the log message.
+ * Input:
+ *
+ * 1. Running Broker
+ * 2. Subscriber created with a JMS Selector.
+ * Output:
+ *
+ * <date> SUB-1001 : Create : Arguments : <key=value>
+ *
+ * Validation Steps:
+ * 3. The SUB ID is correct
+ * 4. Argument tag is present in the message
+ *
+ * @throws java.io.IOException - if there is a problem getting the matches
+ * @throws javax.jms.JMSException - if there is a problem creating the consumer
+ */
+ public void testSubscriptionCreateWithArguments() throws JMSException, IOException
+ {
+ final String SELECTOR = "Selector='True'";
+ _session.createConsumer(_queue, SELECTOR);
+
+ //Validate
+
+ //Ensure that we wait for the SUB log message
+ waitAndFindMatches("SUB-1001");
+
+ List<String> results = findMatches(SUB_PREFIX);
+
+ assertEquals("Result set larger than expected.", 1, results.size());
+
+ String log = getLogMessage(results, 0);
+
+ validateMessageID("SUB-1001", log);
+
+ String message = getMessageString(fromMessage(log));
+ assertTrue("Selector not on log message:" + message, message.contains(SELECTOR));
+ }
+
+ /**
+ * Description:
+ * The final combination of SUB-1001 Create messages involves the creation of a Durable Subscription that also contains a set of Arguments, such as those provided via a JMS Selector.
+ * Input:
+ *
+ * 1. Running Broker
+ * 2. Java Client creates a Durable Subscription with Selector
+ * Output:
+ *
+ * <date> SUB-1001 : Create : Durable Arguments : <key=value>
+ *
+ * Validation Steps:
+ * 3. The SUB ID is correct
+ * 4. The tag Durable is present in the message
+ * 5. The Arguments are present in the message
+ *
+ * @throws java.io.IOException - if there is a problem getting the matches
+ * @throws javax.jms.JMSException - if there is a problem creating the consumer
+ */
+ public void testSubscriptionCreateDurableWithArguments() throws JMSException, IOException
+ {
+ final String SELECTOR = "Selector='True'";
+ _session.createDurableSubscriber(_topic, getName(), SELECTOR, false);
+
+ //Validate
+
+ //Ensure that we wait for the SUB log message
+ waitAndFindMatches("SUB-1001");
+
+ List<String> results = findMatches(SUB_PREFIX);
+
+ assertEquals("Result set larger than expected.", 1, results.size());
+
+ String log = getLogMessage(results, 0);
+
+ validateMessageID("SUB-1001", log);
+
+ String message = getMessageString(fromMessage(log));
+ assertTrue("Durable not on log message:" + message, message.contains("Durable"));
+ assertTrue("Selector not on log message:" + message, message.contains(SELECTOR));
+ }
+
+ /**
+ * Description:
+ * When a Subscription is closed it will log this so that it can be correlated with the Create.
+ * Input:
+ *
+ * 1. Running Broker
+ * 2. Client with a subscription.
+ * 3. The subscription is then closed.
+ * Output:
+ *
+ * <date> SUB-1002 : Close
+ *
+ * Validation Steps:
+ * 1. The SUB ID is correct
+ * 2. There must be a SUB-1001 Create message preceding this message
+ * 3. This must be the last message from the given Subscription
+ *
+ * @throws java.io.IOException - if there is a problem getting the matches
+ * @throws javax.jms.JMSException - if there is a problem creating the consumer
+ */
+ public void testSubscriptionClose() throws JMSException, IOException
+ {
+ _session.createConsumer(_queue).close();
+
+ //Validate
+ //Ensure that we wait for the SUB log message
+ waitAndFindMatches("SUB-1002");
+
+ List<String> results = findMatches(SUB_PREFIX);
+
+ //3
+ assertEquals("Result set larger than expected.", 2, results.size());
+
+ // 2
+ String log = getLogMessage(results, 0);
+ validateMessageID("SUB-1001", log);
+ // 1
+ log = getLogMessage(results, 1);
+ validateMessageID("SUB-1002", log);
+
+ String message = getMessageString(fromMessage(log));
+ assertEquals("Log message is not close", "Close", message);
+
+ }
+
+ /**
+ * Description:
+ * When a Subscription fills its prefetch it will become suspended. This
+ * will be logged as a SUB-1003 message.
+ * Input:
+ *
+ * 1. Running broker
+ * 2. Message Producer to put more data on the queue than the client's prefetch
+ * 3. Client that ensures that its prefetch becomes full
+ * Output:
+ *
+ * <date> SUB-1003 : State : <state>
+ *
+ * Validation Steps:
+ * 1. The SUB ID is correct
+ * 2. The state is correct
+ *
+ * @throws java.io.IOException - if there is a problem getting the matches
+ * @throws javax.jms.JMSException - if there is a problem creating the consumer
+ */
+ public void testSubscriptionSuspend() throws Exception, IOException
+ {
+ //Close session with large prefetch
+ _connection.createSession(false, Session.AUTO_ACKNOWLEDGE).close();
+
+ int PREFETCH = 15;
+
+ //Create new session with small prefetch
+ _session = ((AMQConnection) _connection).createSession(true, Session.SESSION_TRANSACTED, PREFETCH);
+
+ MessageConsumer consumer = _session.createConsumer(_queue);
+
+ _connection.start();
+
+ //Start the dispatcher & Unflow the channel.
+ consumer.receiveNoWait();
+
+ //Fill the prefetch and two extra so that our receive bellow allows the
+ // subscription to become active
+ // Previously we set this to 17 so that it would return to a suspended
+ // state. However, testing has shown that the state change can occur
+ // sufficiently quickly that logging does not occur consistently enough
+ // for testing.
+ int SEND_COUNT = 16;
+ sendMessage(_session, _queue, SEND_COUNT);
+ _session.commit();
+ // Retreive the first message, and start the flow of messages
+ Message msg = consumer.receive(1000);
+ assertNotNull("First message not retreived", msg);
+ _session.commit();
+
+ // Drain the queue to ensure there is time for the ACTIVE log message
+ // Check that we can received all the messages
+ int receivedCount = 0;
+ while (msg != null)
+ {
+ receivedCount++;
+ msg = consumer.receive(1000);
+ _session.commit();
+ }
+
+ //Validate we received all the messages
+ assertEquals("Not all sent messages received.", SEND_COUNT, receivedCount);
+
+ // Fill the queue again to suspend the consumer
+ sendMessage(_session, _queue, SEND_COUNT);
+ _session.commit();
+
+ //Validate
+ List<String> results = waitAndFindMatches("SUB-1003");
+
+ try
+ {
+ // Validation expects three messages.
+ // The Actor can be any one of the following depending on the exactly what is going on on the broker.
+ // Ideally we would test that we can get all of them but setting up
+ // the timing to do this in a consistent way is not benefitial.
+ // Ensuring the State is as expected is sufficient.
+// INFO - MESSAGE [vh(/test)/qu(example.queue)] [sub:6(qu(example.queue))] SUB-1003 : State :
+// INFO - MESSAGE [con:6(guest@anonymous(26562441)/test)/ch:3] [sub:6(qu(example.queue))] SUB-1003 : State :
+// INFO - MESSAGE [sub:6(vh(test)/qu(example.queue))] [sub:6(qu(example.queue))] SUB-1003 : State :
+
+ assertEquals("Result set not expected size:", 3, results.size());
+
+ // Validate Initial Suspension
+ String expectedState = "SUSPENDED";
+ String log = getLogMessage(results, 0);
+ validateSubscriptionState(log, expectedState);
+
+ // After being suspended the subscription should become active.
+ expectedState = "ACTIVE";
+ log = getLogMessage(results, 1);
+ validateSubscriptionState(log, expectedState);
+
+ // Validate that it was re-suspended
+ expectedState = "SUSPENDED";
+ log = getLogMessage(results, 2);
+ validateSubscriptionState(log, expectedState);
+ // We only need validate the state.
+ }
+ catch (AssertionFailedError afe)
+ {
+ System.err.println("Log Dump:");
+ for (String log : results)
+ {
+ System.err.println(log);
+ }
+ throw afe;
+ }
+ _connection.close();
+
+ //Ensure the queue is drained before the test ends
+ drainQueue(_queue);
+
+ }
+
+ /**
+ * Validate that the given log statement is a well formatted SUB-1003
+ * message. That means the ID and expected state are correct.
+ *
+ * @param log the log to test
+ * @param expectedState the state that should be logged.
+ */
+ private void validateSubscriptionState(String log, String expectedState)
+ {
+ validateMessageID("SUB-1003", log);
+ String logMessage = getMessageString(fromMessage(log));
+ assertTrue("Log Message does not start with 'State'" + logMessage,
+ logMessage.startsWith("State"));
+
+ assertTrue("Log Message does not have expected State of '"
+ + expectedState + "'" + logMessage,
+ logMessage.endsWith(expectedState));
+ }
+
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/TransientQueueLoggingTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/TransientQueueLoggingTest.java
new file mode 100644
index 0000000000..29f74c5818
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/TransientQueueLoggingTest.java
@@ -0,0 +1,30 @@
+/*
+ *
+ * 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.logging;
+
+public class TransientQueueLoggingTest extends DurableQueueLoggingTest
+{
+ public void setUp() throws Exception
+ {
+ super.setUp();
+ _durable = false;
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/VirtualHostLoggingTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/VirtualHostLoggingTest.java
new file mode 100644
index 0000000000..a23e40ecce
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/logging/VirtualHostLoggingTest.java
@@ -0,0 +1,134 @@
+/*
+ * 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.logging;
+
+import java.util.Arrays;
+import java.util.List;
+
+import junit.framework.AssertionFailedError;
+
+import org.apache.qpid.server.configuration.ServerConfiguration;
+
+/**
+ * Virtualhost Test Cases
+ * The virtualhost test suite validates that the follow log messages as specified in the Functional Specification.
+ * <p/>
+ * This suite of tests validate that the management console messages occur correctly and according to the following format:
+ * <p/>
+ * VHT-1001 : Created : <name>
+ * VHT-1002 : Work directory : <path>
+ * VHT-1003 : Closed
+ */
+public class VirtualHostLoggingTest extends AbstractTestLogging
+{
+ private static final String VHT_PREFIX = "VHT-";
+
+ /**
+ * Description:
+ * Testing can be performed using the default configuration. The goal is to validate that for each virtualhost defined in the configuration file a VHT-1001 Created message is provided.
+ * Input:
+ * The default configuration file
+ * Output:
+ * <p/>
+ * <date> VHT-1001 : Created : <name>
+ * Validation Steps:
+ * <p/>
+ * The VHT ID is correct
+ * A VHT-1001 is printed for each virtualhost defined in the configuration file.
+ * This must be the first message for the specified virtualhost.
+ *
+ * @throws Exception caused by broker startup
+ */
+ public void testVirtualhostCreation() throws Exception
+ {
+ //Wait for the correct VHT message to arrive.
+ waitForMessage(VHT_PREFIX + "1001");
+
+ //Validate each vhost logs a creation
+ List<String> results = findMatches(VHT_PREFIX + "1001");
+
+ try
+ {
+ List<String> vhosts = Arrays.asList(getServerConfig().getVirtualHosts());
+
+ assertEquals("Each vhost did not create a store.", vhosts.size(), results.size());
+
+ for (int index = 0; index < results.size(); index++)
+ {
+ // Retrieve the vhostname from the log entry message 'Created : <vhostname>'
+ String result = getLogMessage(results, index);
+ String vhostName = getMessageString(fromMessage(result)).split(" ")[2];
+
+ assertTrue("Virtualhost named in log not found in config file:" + vhostName + ":" + vhosts, vhosts.contains(vhostName));
+ }
+ }
+ catch (AssertionFailedError afe)
+ {
+ dumpLogs(results, _monitor);
+
+ throw afe;
+ }
+ }
+
+ /**
+ * Description:
+ * Testing can be performed using the default configuration. During broker shutdown a VHT-1002 Closed message will be printed for each of the configured virtualhosts. For every virtualhost that was started a close must be logged. After the close message has been printed no further logging will be performed by this virtualhost.
+ * Input:
+ * The default configuration file
+ * Output:
+ * <p/>
+ * <date> VHT-1002 : Closed
+ * Validation Steps:
+ * <p/>
+ * The VHT ID is correct
+ * This is the last VHT message for the given virtualhost.
+ *
+ * @throws Exception caused by broker startup
+ */
+ public void testVirtualhostClosure() throws Exception
+ {
+ stopBroker();
+
+ // Wait for the correct VHT message to arrive.
+ waitForMessage(VHT_PREFIX + "1002");
+
+ // Validate each vhost logs a closure
+ List<String> results = findMatches(VHT_PREFIX + "1002");
+
+ try
+ {
+ // Load VirtualHost list from file.
+ ServerConfiguration configuration = new ServerConfiguration(_configFile);
+ configuration.initialise();
+ List<String> vhosts = Arrays.asList(configuration.getVirtualHosts());
+
+ assertEquals("Each vhost did not close their store.", vhosts.size(), results.size());
+ }
+ catch (AssertionFailedError afe)
+ {
+ dumpLogs(results, _monitor);
+
+ throw afe;
+ }
+ }
+
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/persistent/NoLocalAfterRecoveryTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/persistent/NoLocalAfterRecoveryTest.java
new file mode 100644
index 0000000000..a5aec3edce
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/persistent/NoLocalAfterRecoveryTest.java
@@ -0,0 +1,246 @@
+/*
+ *
+ * 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.persistent;
+
+import org.apache.qpid.test.utils.QpidBrokerTestCase;
+import org.apache.qpid.client.AMQConnection;
+import org.apache.qpid.client.AMQSession;
+import org.apache.qpid.jms.ConnectionListener;
+import org.apache.qpid.jms.BrokerDetails;
+import org.apache.qpid.jms.ConnectionURL;
+import org.apache.qpid.server.registry.ApplicationRegistry;
+import org.apache.qpid.server.registry.ConfigurationFileApplicationRegistry;
+import org.apache.qpid.server.store.DerbyMessageStore;
+import org.apache.commons.configuration.XMLConfiguration;
+
+import javax.jms.Connection;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.Session;
+import javax.jms.Topic;
+import javax.jms.TopicSubscriber;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import java.util.concurrent.CountDownLatch;
+import java.io.File;
+
+/**
+ * QPID-1813 : We do not store the client id with a message so on store restart
+ * that information is lost and we are unable to perform no local checks.
+ *
+ * QPID-1813 highlights the lack of testing here as the broker will NPE as it
+ * assumes that the client id of the publisher will always exist
+ */
+public class NoLocalAfterRecoveryTest extends QpidBrokerTestCase implements ConnectionListener
+{
+ protected final String MY_TOPIC_SUBSCRIPTION_NAME = this.getName();
+ protected static final int SEND_COUNT = 10;
+ private CountDownLatch _failoverComplete = new CountDownLatch(1);
+
+ protected ConnectionURL _connectionURL;
+
+ @Override
+ protected void setUp() throws Exception
+ {
+
+ XMLConfiguration configuration = new XMLConfiguration(_configFile);
+ configuration.setProperty("virtualhosts.virtualhost.test.store.class", "org.apache.qpid.server.store.DerbyMessageStore");
+ configuration.setProperty("virtualhosts.virtualhost.test.store."+ DerbyMessageStore.ENVIRONMENT_PATH_PROPERTY,
+ System.getProperty("QPID_WORK", System.getProperty("java.io.tmpdir")) + File.separator + "derbyDB-NoLocalAfterRecoveryTest");
+
+ File tmpFile = File.createTempFile("configFile", "test");
+ tmpFile.deleteOnExit();
+ configuration.save(tmpFile);
+
+ _configFile = tmpFile;
+ _connectionURL = getConnectionURL();
+
+ BrokerDetails details = _connectionURL.getBrokerDetails(0);
+
+ // Due to the problem with SingleServer delaying on all connection
+ // attempts. So using a high retry value.
+ if (_broker.equals(VM))
+ {
+ // Local testing suggests InVM restart takes under a second
+ details.setProperty(BrokerDetails.OPTIONS_RETRY, "5");
+ details.setProperty(BrokerDetails.OPTIONS_CONNECT_DELAY, "200");
+ }
+ else
+ {
+ // This will attempt to failover for 3 seconds.
+ // Local testing suggests failover takes 2 seconds
+ details.setProperty(BrokerDetails.OPTIONS_RETRY, "10");
+ details.setProperty(BrokerDetails.OPTIONS_CONNECT_DELAY, "500");
+ }
+
+ super.setUp();
+ }
+
+ public void test() throws Exception
+ {
+
+ Connection connection = getConnection(_connectionURL);
+ Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
+
+ Topic topic = (Topic) getInitialContext().lookup("topic");
+
+ TopicSubscriber noLocalSubscriber = session.
+ createDurableSubscriber(topic, MY_TOPIC_SUBSCRIPTION_NAME + "-NoLocal",
+ null, true);
+
+ TopicSubscriber normalSubscriber = session.
+ createDurableSubscriber(topic, MY_TOPIC_SUBSCRIPTION_NAME + "-Normal",
+ null, false);
+
+ List<Message> sent = sendMessage(session, topic, SEND_COUNT);
+
+ session.commit();
+
+ assertEquals("Incorrect number of messages sent",
+ SEND_COUNT, sent.size());
+
+
+ // Check messages can be received as expected.
+ connection.start();
+
+ assertTrue("No Local Subscriber is not a no-local subscriber",
+ noLocalSubscriber.getNoLocal());
+
+ assertFalse("Normal Subscriber is a no-local subscriber",
+ normalSubscriber.getNoLocal());
+
+
+ List<Message> received = receiveMessage(noLocalSubscriber, SEND_COUNT);
+ assertEquals("No Local Subscriber Received messages", 0, received.size());
+
+ received = receiveMessage(normalSubscriber, SEND_COUNT);
+ assertEquals("Normal Subscriber Received no messages",
+ SEND_COUNT, received.size());
+
+
+ ((AMQConnection)connection).setConnectionListener(this);
+
+ restartBroker();
+
+
+ //Await
+ if (!_failoverComplete.await(4000L, TimeUnit.MILLISECONDS))
+ {
+ fail("Failover Failed to compelete");
+ }
+
+ session.rollback();
+
+ //Failover will restablish our clients
+ assertTrue("No Local Subscriber is not a no-local subscriber",
+ noLocalSubscriber.getNoLocal());
+
+ assertFalse("Normal Subscriber is a no-local subscriber",
+ normalSubscriber.getNoLocal());
+
+
+ // NOTE : here that the NO-local subscriber actually now gets ALL the
+ // messages as the connection has failed and they are consuming on a
+ // different connnection to the one that was published on.
+ received = receiveMessage(noLocalSubscriber, SEND_COUNT);
+ assertEquals("No Local Subscriber Received messages", SEND_COUNT, received.size());
+
+ received = receiveMessage(normalSubscriber, SEND_COUNT);
+ assertEquals("Normal Subscriber Received no messages",
+ SEND_COUNT, received.size());
+
+ //leave the store in a clean state.
+ session.commit();
+ }
+
+ protected List<Message> assertReceiveMessage(MessageConsumer messageConsumer,
+ int count) throws JMSException
+ {
+
+ List<Message> receivedMessages = new ArrayList<Message>(count);
+ for (int i = 0; i < count; i++)
+ {
+ Message received = messageConsumer.receive(1000);
+
+ if (received != null)
+ {
+ receivedMessages.add(received);
+ }
+ else
+ {
+ fail("Only "
+ + receivedMessages.size() + "/" + count + " received.");
+ }
+ }
+
+ return receivedMessages;
+ }
+
+ protected List<Message> receiveMessage(MessageConsumer messageConsumer,
+ int count) throws JMSException
+ {
+
+ List<Message> receivedMessages = new ArrayList<Message>(count);
+ for (int i = 0; i < count; i++)
+ {
+ Message received = messageConsumer.receive(1000);
+
+ if (received != null)
+ {
+ receivedMessages.add(received);
+ }
+ else
+ {
+ break;
+ }
+ }
+
+ return receivedMessages;
+ }
+
+ public void bytesSent(long count)
+ {
+
+ }
+
+ public void bytesReceived(long count)
+ {
+
+ }
+
+ public boolean preFailover(boolean redirect)
+ {
+ return true;
+ }
+
+ public boolean preResubscribe()
+ {
+ return true;
+ }
+
+ public void failoverComplete()
+ {
+ _failoverComplete.countDown();
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/ConflationQueueTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/ConflationQueueTest.java
new file mode 100644
index 0000000000..ae7be6f7f4
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/ConflationQueueTest.java
@@ -0,0 +1,435 @@
+/*
+ *
+ * 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.queue;
+
+import org.apache.log4j.Logger;
+
+import org.apache.qpid.client.AMQDestination;
+import org.apache.qpid.client.AMQQueue;
+import org.apache.qpid.client.AMQSession;
+import org.apache.qpid.framing.AMQShortString;
+import org.apache.qpid.test.utils.QpidBrokerTestCase;
+import org.apache.qpid.url.AMQBindingURL;
+
+import javax.jms.Connection;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class ConflationQueueTest extends QpidBrokerTestCase
+{
+ private static final int TIMEOUT = 1500;
+
+
+ private static final Logger _logger = Logger.getLogger(ConflationQueueTest.class);
+
+
+
+ protected final String VHOST = "/test";
+ protected final String QUEUE = "ConflationQueue";
+
+ private static final int MSG_COUNT = 400;
+
+ private Connection producerConnection;
+ private MessageProducer producer;
+ private Session producerSession;
+ private Queue queue;
+ private Connection consumerConnection;
+ private Session consumerSession;
+
+
+ private MessageConsumer consumer;
+
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+
+ producerConnection = getConnection();
+ producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ producerConnection.start();
+
+
+ }
+
+ protected void tearDown() throws Exception
+ {
+ producerConnection.close();
+ consumerConnection.close();
+ super.tearDown();
+ }
+
+ public void testConflation() throws Exception
+ {
+ consumerConnection = getConnection();
+ consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+
+ final Map<String,Object> arguments = new HashMap<String, Object>();
+ arguments.put("qpid.last_value_queue_key","key");
+ ((AMQSession) producerSession).createQueue(new AMQShortString(QUEUE), false, true, false, arguments);
+ queue = new org.apache.qpid.client.AMQQueue("amq.direct",QUEUE);
+ ((AMQSession) producerSession).declareAndBind((AMQDestination)queue);
+ producer = producerSession.createProducer(queue);
+
+ for (int msg = 0; msg < MSG_COUNT; msg++)
+ {
+ producer.send(nextMessage(msg, producerSession));
+ }
+
+ producer.close();
+ producerSession.close();
+ producerConnection.close();
+
+ consumer = consumerSession.createConsumer(queue);
+ consumerConnection.start();
+ Message received;
+
+ List<Message> messages = new ArrayList<Message>();
+ while((received = consumer.receive(1000))!=null)
+ {
+ messages.add(received);
+ }
+
+ assertEquals("Unexpected number of messages received",10,messages.size());
+
+ for(int i = 0 ; i < 10; i++)
+ {
+ Message msg = messages.get(i);
+ assertEquals("Unexpected message number received", MSG_COUNT - 10 + i, msg.getIntProperty("msg"));
+ }
+
+
+ }
+
+
+ public void testConflationWithRelease() throws Exception
+ {
+ consumerConnection = getConnection();
+ consumerSession = consumerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
+
+
+ final Map<String,Object> arguments = new HashMap<String, Object>();
+ arguments.put("qpid.last_value_queue_key","key");
+ ((AMQSession) producerSession).createQueue(new AMQShortString(QUEUE), false, true, false, arguments);
+ queue = new org.apache.qpid.client.AMQQueue("amq.direct",QUEUE);
+ ((AMQSession) producerSession).declareAndBind((AMQDestination)queue);
+ producer = producerSession.createProducer(queue);
+
+ for (int msg = 0; msg < MSG_COUNT/2; msg++)
+ {
+ producer.send(nextMessage(msg, producerSession));
+
+ }
+
+ // HACK to do something synchronous
+ ((AMQSession)producerSession).sync();
+
+ consumer = consumerSession.createConsumer(queue);
+ consumerConnection.start();
+ Message received;
+ List<Message> messages = new ArrayList<Message>();
+ while((received = consumer.receive(1000))!=null)
+ {
+ messages.add(received);
+ }
+
+ assertEquals("Unexpected number of messages received",10,messages.size());
+
+ for(int i = 0 ; i < 10; i++)
+ {
+ Message msg = messages.get(i);
+ assertEquals("Unexpected message number received", MSG_COUNT/2 - 10 + i, msg.getIntProperty("msg"));
+ }
+
+ consumerSession.close();
+ consumerConnection.close();
+
+
+ consumerConnection = getConnection();
+ consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+
+ for (int msg = MSG_COUNT/2; msg < MSG_COUNT; msg++)
+ {
+ producer.send(nextMessage(msg, producerSession));
+ }
+
+
+ // HACK to do something synchronous
+ ((AMQSession)producerSession).sync();
+
+ consumer = consumerSession.createConsumer(queue);
+ consumerConnection.start();
+
+ messages = new ArrayList<Message>();
+ while((received = consumer.receive(1000))!=null)
+ {
+ messages.add(received);
+ }
+
+ assertEquals("Unexpected number of messages received",10,messages.size());
+
+ for(int i = 0 ; i < 10; i++)
+ {
+ Message msg = messages.get(i);
+ assertEquals("Unexpected message number received", MSG_COUNT - 10 + i, msg.getIntProperty("msg"));
+ }
+
+ }
+
+
+
+ public void testConflationWithReleaseAfterNewPublish() throws Exception
+ {
+ consumerConnection = getConnection();
+ consumerSession = consumerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
+
+
+ final Map<String,Object> arguments = new HashMap<String, Object>();
+ arguments.put("qpid.last_value_queue_key","key");
+ ((AMQSession) producerSession).createQueue(new AMQShortString(QUEUE), false, true, false, arguments);
+ queue = new org.apache.qpid.client.AMQQueue("amq.direct",QUEUE);
+ ((AMQSession) producerSession).declareAndBind((AMQDestination)queue);
+ producer = producerSession.createProducer(queue);
+
+ for (int msg = 0; msg < MSG_COUNT/2; msg++)
+ {
+ producer.send(nextMessage(msg, producerSession));
+ }
+
+ // HACK to do something synchronous
+ ((AMQSession)producerSession).sync();
+
+ consumer = consumerSession.createConsumer(queue);
+ consumerConnection.start();
+ Message received;
+ List<Message> messages = new ArrayList<Message>();
+ while((received = consumer.receive(1000))!=null)
+ {
+ messages.add(received);
+ }
+
+ assertEquals("Unexpected number of messages received",10,messages.size());
+
+ for(int i = 0 ; i < 10; i++)
+ {
+ Message msg = messages.get(i);
+ assertEquals("Unexpected message number received", MSG_COUNT/2 - 10 + i, msg.getIntProperty("msg"));
+ }
+
+ consumer.close();
+
+ for (int msg = MSG_COUNT/2; msg < MSG_COUNT; msg++)
+ {
+ producer.send(nextMessage(msg, producerSession));
+ }
+
+ // HACK to do something synchronous
+ ((AMQSession)producerSession).sync();
+
+
+ // this causes the "old" messages to be released
+ consumerSession.close();
+ consumerConnection.close();
+
+
+ consumerConnection = getConnection();
+ consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+
+
+ consumer = consumerSession.createConsumer(queue);
+ consumerConnection.start();
+
+ messages = new ArrayList<Message>();
+ while((received = consumer.receive(1000))!=null)
+ {
+ messages.add(received);
+ }
+
+ assertEquals("Unexpected number of messages received",10,messages.size());
+
+ for(int i = 0 ; i < 10; i++)
+ {
+ Message msg = messages.get(i);
+ assertEquals("Unexpected message number received", MSG_COUNT - 10 + i, msg.getIntProperty("msg"));
+ }
+
+ }
+
+ public void testConflationBrowser() throws Exception
+ {
+ consumerConnection = getConnection();
+ consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+
+ final Map<String,Object> arguments = new HashMap<String, Object>();
+ arguments.put("qpid.last_value_queue_key","key");
+ ((AMQSession) producerSession).createQueue(new AMQShortString(QUEUE), false, true, false, arguments);
+ queue = new org.apache.qpid.client.AMQQueue("amq.direct",QUEUE);
+ ((AMQSession) producerSession).declareAndBind((AMQDestination)queue);
+ producer = producerSession.createProducer(queue);
+
+ for (int msg = 0; msg < MSG_COUNT; msg++)
+ {
+ producer.send(nextMessage(msg, producerSession));
+
+ }
+
+ ((AMQSession)producerSession).sync();
+
+ AMQBindingURL url = new AMQBindingURL("direct://amq.direct//"+QUEUE+"?browse='true'&durable='true'");
+ AMQQueue browseQueue = new AMQQueue(url);
+
+ consumer = consumerSession.createConsumer(browseQueue);
+ consumerConnection.start();
+ Message received;
+ List<Message> messages = new ArrayList<Message>();
+ while((received = consumer.receive(1000))!=null)
+ {
+ messages.add(received);
+ }
+
+ assertEquals("Unexpected number of messages received",10,messages.size());
+
+ for(int i = 0 ; i < 10; i++)
+ {
+ Message msg = messages.get(i);
+ assertEquals("Unexpected message number received", MSG_COUNT - 10 + i, msg.getIntProperty("msg"));
+ }
+
+ messages.clear();
+
+ producer.send(nextMessage(MSG_COUNT, producerSession));
+
+ ((AMQSession)producerSession).sync();
+
+ while((received = consumer.receive(1000))!=null)
+ {
+ messages.add(received);
+ }
+ assertEquals("Unexpected number of messages received",1,messages.size());
+ assertEquals("Unexpected message number received", MSG_COUNT, messages.get(0).getIntProperty("msg"));
+
+
+ producer.close();
+ producerSession.close();
+ producerConnection.close();
+
+
+
+ }
+
+
+ public void testConflation2Browsers() throws Exception
+ {
+ consumerConnection = getConnection();
+ consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+
+ final Map<String,Object> arguments = new HashMap<String, Object>();
+ arguments.put("qpid.last_value_queue_key","key");
+ ((AMQSession) producerSession).createQueue(new AMQShortString(QUEUE), false, true, false, arguments);
+ queue = new org.apache.qpid.client.AMQQueue("amq.direct",QUEUE);
+ ((AMQSession) producerSession).declareAndBind((AMQDestination)queue);
+ producer = producerSession.createProducer(queue);
+
+ for (int msg = 0; msg < MSG_COUNT; msg++)
+ {
+ producer.send(nextMessage(msg, producerSession));
+
+ }
+
+ ((AMQSession)producerSession).sync();
+
+ AMQBindingURL url = new AMQBindingURL("direct://amq.direct//"+QUEUE+"?browse='true'&durable='true'");
+ AMQQueue browseQueue = new AMQQueue(url);
+
+ consumer = consumerSession.createConsumer(browseQueue);
+ MessageConsumer consumer2 = consumerSession.createConsumer(browseQueue);
+ consumerConnection.start();
+ List<Message> messages = new ArrayList<Message>();
+ List<Message> messages2 = new ArrayList<Message>();
+ Message received = consumer.receive(1000);
+ Message received2 = consumer2.receive(1000);
+
+ while(received!=null || received2!=null)
+ {
+ if(received != null)
+ {
+ messages.add(received);
+ }
+ if(received2 != null)
+ {
+ messages2.add(received2);
+ }
+
+
+ received = consumer.receive(1000);
+ received2 = consumer2.receive(1000);
+
+ }
+
+ assertEquals("Unexpected number of messages received on first browser",10,messages.size());
+ assertEquals("Unexpected number of messages received on second browser",10,messages2.size());
+
+ for(int i = 0 ; i < 10; i++)
+ {
+ Message msg = messages.get(i);
+ assertEquals("Unexpected message number received on first browser", MSG_COUNT - 10 + i, msg.getIntProperty("msg"));
+ msg = messages2.get(i);
+ assertEquals("Unexpected message number received on second browser", MSG_COUNT - 10 + i, msg.getIntProperty("msg"));
+ }
+
+
+ producer.close();
+ producerSession.close();
+ producerConnection.close();
+
+
+
+ }
+
+
+
+ private Message nextMessage(int msg, Session producerSession) throws JMSException
+ {
+ Message send = producerSession.createTextMessage("Message: " + msg);
+
+ send.setStringProperty("key", String.valueOf(msg % 10));
+ send.setIntProperty("msg", msg);
+
+ return send;
+ }
+
+
+}
+
+
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/DeepQueueConsumeWithSelector.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/DeepQueueConsumeWithSelector.java
new file mode 100644
index 0000000000..4ac0d2c4d6
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/DeepQueueConsumeWithSelector.java
@@ -0,0 +1,159 @@
+/*
+ *
+ * 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.queue;
+
+import org.apache.qpid.test.utils.QpidBrokerTestCase;
+import org.apache.qpid.client.AMQConnection;
+
+import javax.jms.Connection;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageListener;
+import javax.jms.Queue;
+import javax.jms.Session;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Test DeapQueueConsumerWithSelector
+ * Summary:
+ * Prior to M4 the broker had a different queue model which pre-processed the
+ * messages on the queue for any connecting subscription that had a selector.
+ *
+ * If the queue had a lot of data then this may take a long time to process
+ * to such an extent that the subscription creation may time out. During this
+ * pre-process phase the virtualhost would be come unresposive.
+ *
+ * Our solution was to allow the timeout to be adjusted QPID-1119, which allowed
+ * the subscription to connect but did not address the unresponsiveness.
+ *
+ * The new queue model introduced in M4 resolved this.
+ *
+ * This test is to validate that the new queueing model does indeed remove the
+ * long pre-processing phase and allow immediate subscription so that there is
+ * no unresponsive period.
+ *
+ * Test Strategy:
+ *
+ * Add 100k messages to the queue with a numberic header property that will
+ * allow later subscribers to use as in a selector.
+ *
+ * Connect the subscriber and time how long it takes to connect.
+ *
+ * Finally consume all the messages from the queue to clean up.
+ */
+public class DeepQueueConsumeWithSelector extends QpidBrokerTestCase implements MessageListener
+{
+
+ private static final int MESSAGE_COUNT = 10000;
+ private static final int BATCH_SIZE = MESSAGE_COUNT / 10;
+
+ private CountDownLatch _receviedLatch = new CountDownLatch(MESSAGE_COUNT);
+
+ protected long SYNC_WRITE_TIMEOUT = 120000L;
+
+
+ public void setUp() throws Exception
+ {
+ //Set the syncWrite timeout to be just larger than the delay on the commitTran.
+ setSystemProperty("amqj.default_syncwrite_timeout", String.valueOf(SYNC_WRITE_TIMEOUT));
+
+ super.setUp();
+ }
+
+ public void test() throws Exception
+ {
+ // Create Connection
+ Connection connection = getConnection();
+ Session session = ((AMQConnection)connection).createSession(true, Session.SESSION_TRANSACTED, 100000);
+
+ Queue queue = (Queue) getInitialContext().lookup("queue");
+
+ // Validate that the destination exists
+ session.createConsumer(queue).close();
+
+ // Send Messages
+ sendMessage(session, queue, MESSAGE_COUNT, BATCH_SIZE);
+
+ session.close();
+
+ session = ((AMQConnection) connection).createSession(false, Session.AUTO_ACKNOWLEDGE);//, 100000);
+
+
+ // Setup Selector to perform a few calculations which will slow it down
+ String selector = "((\"" + INDEX + "\" % 1) = 0) AND ('" + INDEX + "' IS NOT NULL) AND ('" + INDEX + "' <> -1)";
+
+ // Setup timing
+ long start = System.nanoTime();
+
+ System.err.println("Create Consumer");
+ // Connect Consumer
+ MessageConsumer consumer = session.createConsumer(queue, selector);
+ consumer.setMessageListener(this);
+
+ // Validate timing details
+ long end = System.nanoTime();
+
+ System.err.println("Subscription time took:" + (end - start));
+
+ // Consume Messages
+ connection.start();
+
+
+
+ assertTrue("Messages took to long to be received :"+_receviedLatch.getCount(),
+ _receviedLatch.await(SYNC_WRITE_TIMEOUT, TimeUnit.MILLISECONDS ));
+
+ }
+
+ @Override
+ public Message createNextMessage(Session session, int msgCount) throws JMSException
+ {
+ Message message = super.createNextMessage(session,msgCount);
+
+ if ((msgCount % BATCH_SIZE) == 0 )
+ {
+ System.err.println("Sent:"+msgCount);
+ }
+
+ return message;
+ }
+
+ public void onMessage(Message message)
+ {
+ _receviedLatch.countDown();
+ int msgCount = 0;
+ try
+ {
+ msgCount = message.getIntProperty(INDEX);
+ }
+ catch (JMSException e)
+ {
+ //ignore
+ }
+ if ((msgCount % BATCH_SIZE) == 0 )
+ {
+ System.err.println("Received:"+msgCount);
+ }
+
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/ModelTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/ModelTest.java
new file mode 100644
index 0000000000..e3fd042560
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/ModelTest.java
@@ -0,0 +1,343 @@
+/*
+ *
+ * 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.queue;
+
+import org.apache.qpid.AMQException;
+import org.apache.qpid.client.AMQConnection;
+import org.apache.qpid.client.AMQSession;
+import org.apache.qpid.framing.AMQShortString;
+import org.apache.qpid.management.common.mbeans.ManagedBroker;
+import org.apache.qpid.management.common.mbeans.ManagedQueue;
+import org.apache.qpid.test.utils.JMXTestUtils;
+import org.apache.qpid.test.utils.QpidBrokerTestCase;
+
+import javax.jms.Connection;
+import javax.jms.JMSException;
+import javax.jms.Session;
+import javax.management.JMException;
+import javax.management.MBeanException;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.UndeclaredThrowableException;
+
+/**
+ * This Test validates the Queue Model on the broker.
+ * Currently it has some basic queue creation / deletion tests.
+ * However, it should be expanded to include other tests that relate to the
+ * model. i.e.
+ *
+ * The Create and Delete tests should ensure that the requisite logging is
+ * performed.
+ *
+ * Additions to this suite would be to complete testing of creations, validating
+ * fields such as owner/exclusive, autodelete and priority are correctly set.
+ *
+ * Currently this test uses the JMX interface to validate that the queue has
+ * been declared as expected so these tests cannot run against a CPP broker.
+ *
+ *
+ * Tests should ensure that they clean up after themselves.
+ * e,g. Durable queue creation test should perform a queue delete.
+ */
+public class ModelTest extends QpidBrokerTestCase
+{
+
+ private static final String USER = "admin";
+ private JMXTestUtils _jmxUtils;
+ private static final String VIRTUALHOST_NAME = "test";
+
+ @Override
+ public void setUp() throws Exception
+ {
+ // Create a JMX Helper
+ _jmxUtils = new JMXTestUtils(this, USER, USER);
+ _jmxUtils.setUp();
+ super.setUp();
+
+ // Open the JMX Connection
+ _jmxUtils.open();
+ }
+
+ @Override
+ public void tearDown() throws Exception
+ {
+ // Close the JMX Connection
+ _jmxUtils.close();
+ super.tearDown();
+ }
+
+ /**
+ * Test that an exclusive transient queue can be created via AMQP.
+ *
+ * @throws Exception On unexpected error
+ */
+ public void testExclusiveQueueCreationTransientViaAMQP() throws Exception
+ {
+ Connection connection = getConnection();
+
+ String queueName = getTestQueueName();
+ boolean durable = false;
+ boolean autoDelete = false;
+ boolean exclusive = true;
+
+ createViaAMQPandValidateViaJMX(connection, queueName, durable,
+ autoDelete, exclusive);
+ }
+
+
+
+ /**
+ * Test that a transient queue can be created via AMQP.
+ *
+ * @throws Exception On unexpected error
+ */
+ public void testQueueCreationTransientViaAMQP() throws Exception
+ {
+ Connection connection = getConnection();
+
+ String queueName = getTestQueueName();
+ boolean durable = false;
+ boolean autoDelete = false;
+ boolean exclusive = true;
+
+ createViaAMQPandValidateViaJMX(connection, queueName, durable,
+ autoDelete, exclusive);
+ }
+
+ /**
+ * Test that a durable exclusive queue can be created via AMQP.
+ *
+ * @throws Exception On unexpected error
+ */
+
+ public void testExclusiveQueueCreationDurableViaAMQP() throws Exception
+ {
+ Connection connection = getConnection();
+
+ String queueName = getTestQueueName();
+ boolean durable = true;
+ boolean autoDelete = false;
+ boolean exclusive = true;
+
+ createViaAMQPandValidateViaJMX(connection, queueName, durable,
+ autoDelete, exclusive);
+
+ // Clean up
+ ManagedBroker managedBroker =
+ _jmxUtils.getManagedBroker(VIRTUALHOST_NAME);
+ managedBroker.deleteQueue(queueName);
+ }
+
+ /**
+ * Test that a durable queue can be created via AMQP.
+ *
+ * @throws Exception On unexpected error
+ */
+
+ public void testQueueCreationDurableViaAMQP() throws Exception
+ {
+ Connection connection = getConnection();
+
+ String queueName = getTestQueueName();
+ boolean durable = true;
+ boolean autoDelete = false;
+ boolean exclusive = false;
+
+ createViaAMQPandValidateViaJMX(connection, queueName, durable,
+ autoDelete, exclusive);
+
+ // Clean up
+ ManagedBroker managedBroker =
+ _jmxUtils.getManagedBroker(VIRTUALHOST_NAME);
+ managedBroker.deleteQueue(queueName);
+ }
+
+
+ /**
+ * Test that a transient queue can be created via JMX.
+ *
+ * @throws IOException if there is a problem via the JMX connection
+ * @throws javax.management.JMException if there is a problem with the JMX command
+ */
+ public void testCreationTransientViaJMX() throws IOException, JMException
+ {
+ String name = getName();
+ String owner = null;
+ boolean durable = false;
+
+ createViaJMXandValidateViaJMX(name, owner, durable);
+ }
+
+ /**
+ * Test that a durable queue can be created via JMX.
+ *
+ * @throws IOException if there is a problem via the JMX connection
+ * @throws javax.management.JMException if there is a problem with the JMX command
+ */
+ public void testCreationDurableViaJMX() throws IOException, JMException
+ {
+ String name = getName();
+ String owner = null;
+ boolean durable = true;
+
+ createViaJMXandValidateViaJMX(name, owner, durable);
+
+ // Clean up
+ ManagedBroker managedBroker =
+ _jmxUtils.getManagedBroker(VIRTUALHOST_NAME);
+ managedBroker.deleteQueue(name);
+ }
+
+ /**
+ * Test that a transient queue can be deleted via JMX.
+ *
+ * @throws IOException if there is a problem via the JMX connection
+ * @throws javax.management.JMException if there is a problem with the JMX command
+ */
+ public void testDeletionTransientViaJMX() throws IOException, JMException
+ {
+ String name = getName();
+
+ _jmxUtils.createQueue(VIRTUALHOST_NAME, name, null, false);
+
+ ManagedBroker managedBroker = _jmxUtils.
+ getManagedBroker(VIRTUALHOST_NAME);
+
+ try
+ {
+ managedBroker.deleteQueue(name);
+ }
+ catch (UndeclaredThrowableException e)
+ {
+ fail(((MBeanException) ((InvocationTargetException)
+ e.getUndeclaredThrowable()).getTargetException()).getTargetException().getMessage());
+ }
+ }
+
+ /**
+ * Test that a durable queue can be created via JMX.
+ *
+ * @throws IOException if there is a problem via the JMX connection
+ * @throws javax.management.JMException if there is a problem with the JMX command
+ */
+ public void testDeletionDurableViaJMX() throws IOException, JMException
+ {
+ String name = getName();
+
+ _jmxUtils.createQueue(VIRTUALHOST_NAME, name, null, true);
+
+ ManagedBroker managedBroker = _jmxUtils.
+ getManagedBroker(VIRTUALHOST_NAME);
+
+ try
+ {
+ managedBroker.deleteQueue(name);
+ }
+ catch (UndeclaredThrowableException e)
+ {
+ fail(((MBeanException) ((InvocationTargetException)
+ e.getUndeclaredThrowable()).getTargetException()).getTargetException().getMessage());
+ }
+ }
+
+ /*
+ * Helper Methods
+ */
+
+ /**
+ * Using the provided JMS Connection create a queue using the AMQP extension
+ * with the given properties and then validate it was created correctly via
+ * the JMX Connection
+ *
+ * @param connection Qpid JMS Connection
+ * @param queueName String the desired QueueName
+ * @param durable boolean if the queue should be durable
+ * @param autoDelete boolean if the queue is an autoDelete queue
+ * @param exclusive boolean if the queue is exclusive
+ *
+ * @throws AMQException if there is a problem with the createQueue call
+ * @throws JMException if there is a problem with the JMX validatation
+ * @throws IOException if there is a problem with the JMX connection
+ * @throws JMSException if there is a problem creating the JMS Session
+ */
+ private void createViaAMQPandValidateViaJMX(Connection connection,
+ String queueName,
+ boolean durable,
+ boolean autoDelete,
+ boolean exclusive)
+ throws AMQException, JMException, IOException, JMSException
+ {
+ AMQSession session = (AMQSession) connection.createSession(false,
+ Session.AUTO_ACKNOWLEDGE);
+
+ session.createQueue(new AMQShortString(queueName),
+ autoDelete, durable, exclusive);
+
+ validateQueueViaJMX(queueName, exclusive ? connection.getClientID() : null, durable, autoDelete);
+ }
+
+ /**
+ * Use the JMX Helper to create a queue with the given properties and then
+ * validate it was created correctly via the JMX Connection
+ *
+ * @param queueName String the desired QueueName
+ * @param owner String the owner value that should be set
+ * @param durable boolean if the queue should be durable
+ * @param autoDelete boolean if the queue is an autoDelete queue
+ *
+ * @throws JMException if there is a problem with the JMX validatation
+ * @throws IOException if there is a problem with the JMX connection
+ */
+ private void createViaJMXandValidateViaJMX(String queueName, String owner,
+ boolean durable)
+ throws JMException, IOException
+ {
+ _jmxUtils.createQueue(VIRTUALHOST_NAME, queueName, owner, durable);
+
+ validateQueueViaJMX(queueName, owner, durable, false);
+ }
+
+ /**
+ * Validate that a queue with the given properties exists on the broker
+ *
+ * @param queueName String the desired QueueName
+ * @param owner String the owner value that should be set
+ * @param durable boolean if the queue should be durable
+ * @param autoDelete boolean if the queue is an autoDelete queue
+ *
+ * @throws JMException if there is a problem with the JMX validatation
+ * @throws IOException if there is a problem with the JMX connection
+ */
+ private void validateQueueViaJMX(String queueName, String owner, boolean durable, boolean autoDelete)
+ throws JMException, IOException
+ {
+ ManagedQueue managedQueue = _jmxUtils.
+ getManagedObject(ManagedQueue.class,
+ _jmxUtils.getQueueObjectName(VIRTUALHOST_NAME,
+ queueName));
+
+ assertEquals(queueName, managedQueue.getName());
+ assertEquals(String.valueOf(owner), managedQueue.getOwner());
+ assertEquals(durable, managedQueue.isDurable());
+ assertEquals(autoDelete, managedQueue.isAutoDelete());
+ }
+
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/MultipleTransactedBatchProducerTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/MultipleTransactedBatchProducerTest.java
new file mode 100644
index 0000000000..460270e188
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/MultipleTransactedBatchProducerTest.java
@@ -0,0 +1,246 @@
+/*
+ *
+ * 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.queue;
+
+import java.util.Random;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import javax.jms.Connection;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageListener;
+import javax.jms.Session;
+
+import org.apache.log4j.Logger;
+import org.apache.qpid.test.utils.QpidBrokerTestCase;
+
+public class MultipleTransactedBatchProducerTest extends QpidBrokerTestCase
+{
+ private static final Logger _logger = Logger.getLogger(MultipleTransactedBatchProducerTest.class);
+
+ private static final int MESSAGE_COUNT = 1000;
+ private static final int BATCH_SIZE = 50;
+ private static final int NUM_PRODUCERS = 2;
+ private static final int NUM_CONSUMERS = 3;
+ private static final Random RANDOM = new Random();
+
+ private CountDownLatch _receivedLatch;
+ private String _queueName;
+
+ private volatile String _failMsg;
+
+ public void setUp() throws Exception
+ {
+ //debug level logging often makes this test pass artificially, turn the level down to info.
+ setSystemProperty("amqj.server.logging.level", "INFO");
+ _receivedLatch = new CountDownLatch(MESSAGE_COUNT * NUM_PRODUCERS);
+ setConfigurationProperty("management.enabled", "true");
+ super.setUp();
+ _queueName = getTestQueueName();
+ _failMsg = null;
+ }
+
+ /**
+ * When there are multiple producers submitting batches of messages to a given
+ * queue using transacted sessions, it is highly probable that concurrent
+ * enqueue() activity will occur and attempt delivery of their message to the
+ * same subscription. In this scenario it is likely that one of the attempts
+ * will succeed and the other will result in use of the deliverAsync() method
+ * to start a queue Runner and ensure delivery of the message.
+ *
+ * A defect within the processQueue() method used by the Runner would mean that
+ * delivery of these messages may not occur, should the Runner stop before all
+ * messages have been processed. Such a defect was discovered and found to be
+ * most visible when Selectors are used such that one and only one subscription
+ * can/will accept any given message, but multiple subscriptions are present,
+ * and one of the earlier subscriptions receives more messages than the others.
+ *
+ * This test is to validate that the processQueue() method is able to correctly
+ * deliver all of the messages present for asynchronous delivery to subscriptions,
+ * by utilising multiple batch transacted producers to create the scenario and
+ * ensure all messages are received by a consumer.
+ */
+ public void testMultipleBatchedProducersWithMultipleConsumersUsingSelectors() throws Exception
+ {
+ String selector1 = ("(\"" + _queueName +"\" % " + NUM_CONSUMERS + ") = 0");
+ String selector2 = ("(\"" + _queueName +"\" % " + NUM_CONSUMERS + ") = 1");
+ String selector3 = ("(\"" + _queueName +"\" % " + NUM_CONSUMERS + ") = 2");
+
+ //create consumers
+ Connection conn1 = getConnection();
+ conn1.setExceptionListener(new ExceptionHandler("conn1"));
+ Session sess1 = conn1.createSession(true, Session.SESSION_TRANSACTED);
+ MessageConsumer cons1 = sess1.createConsumer(sess1.createQueue(_queueName), selector1);
+ cons1.setMessageListener(new Cons(sess1,"consumer1"));
+
+ Connection conn2 = getConnection();
+ conn2.setExceptionListener(new ExceptionHandler("conn2"));
+ Session sess2 = conn2.createSession(true, Session.SESSION_TRANSACTED);
+ MessageConsumer cons2 = sess2.createConsumer(sess2.createQueue(_queueName), selector2);
+ cons2.setMessageListener(new Cons(sess2,"consumer2"));
+
+ Connection conn3 = getConnection();
+ conn3.setExceptionListener(new ExceptionHandler("conn3"));
+ Session sess3 = conn3.createSession(true, Session.SESSION_TRANSACTED);
+ MessageConsumer cons3 = sess3.createConsumer(sess3.createQueue(_queueName), selector3);
+ cons3.setMessageListener(new Cons(sess3,"consumer3"));
+
+ conn1.start();
+ conn2.start();
+ conn3.start();
+
+ //create producers
+ Connection connA = getConnection();
+ connA.setExceptionListener(new ExceptionHandler("connA"));
+ Connection connB = getConnection();
+ connB.setExceptionListener(new ExceptionHandler("connB"));
+ Thread producer1 = new Thread(new ProducerThread(connA, _queueName, "producer1"));
+ Thread producer2 = new Thread(new ProducerThread(connB, _queueName, "producer2"));
+
+ producer1.start();
+ Thread.sleep(10);
+ producer2.start();
+
+ //await delivery of the messages
+ boolean result = _receivedLatch.await(75, TimeUnit.SECONDS);
+
+ assertNull("Test failed because: " + String.valueOf(_failMsg), _failMsg);
+ assertTrue("Some of the messages were not all recieved in the given timeframe, remaining count was: "+_receivedLatch.getCount(),
+ result);
+
+ }
+
+ @Override
+ public Message createNextMessage(Session session, int msgCount) throws JMSException
+ {
+ Message message = super.createNextMessage(session,msgCount);
+
+ //bias at least 50% of the messages to the first consumers selector because
+ //the issue presents itself primarily when an earlier subscription completes
+ //delivery after the later subscriptions
+ int val;
+ if (msgCount % 2 == 0)
+ {
+ val = 0;
+ }
+ else
+ {
+ val = RANDOM.nextInt(Integer.MAX_VALUE);
+ }
+
+ message.setIntProperty(_queueName, val);
+
+ return message;
+ }
+
+ private class Cons implements MessageListener
+ {
+ private Session _sess;
+ private String _desc;
+
+ public Cons(Session sess, String desc)
+ {
+ _sess = sess;
+ _desc = desc;
+ }
+
+ public void onMessage(Message message)
+ {
+ _receivedLatch.countDown();
+ int msgCount = 0;
+ int msgID = 0;
+ try
+ {
+ msgCount = message.getIntProperty(INDEX);
+ msgID = message.getIntProperty(_queueName);
+ }
+ catch (JMSException e)
+ {
+ _logger.error(_desc + " received exception: " + e.getMessage(), e);
+ failAsyncTest(e.getMessage());
+ }
+
+ _logger.info("Consumer received message:"+ msgCount + " with ID: " + msgID);
+
+ try
+ {
+ _sess.commit();
+ }
+ catch (JMSException e)
+ {
+ _logger.error(_desc + " received exception: " + e.getMessage(), e);
+ failAsyncTest(e.getMessage());
+ }
+ }
+ }
+
+ private class ProducerThread implements Runnable
+ {
+ private Connection _conn;
+ private String _dest;
+ private String _desc;
+
+ public ProducerThread(Connection conn, String dest, String desc)
+ {
+ _conn = conn;
+ _dest = dest;
+ _desc = desc;
+ }
+
+ public void run()
+ {
+ try
+ {
+ Session session = _conn.createSession(true, Session.SESSION_TRANSACTED);
+ sendMessage(session, session.createQueue(_dest), MESSAGE_COUNT, BATCH_SIZE);
+ }
+ catch (Exception e)
+ {
+ _logger.error(_desc + " received exception: " + e.getMessage(), e);
+ failAsyncTest(e.getMessage());
+ }
+ }
+ }
+
+ private class ExceptionHandler implements javax.jms.ExceptionListener
+ {
+ private String _desc;
+
+ public ExceptionHandler(String description)
+ {
+ _desc = description;
+ }
+
+ public void onException(JMSException e)
+ {
+ _logger.error(_desc + " received exception: " + e.getMessage(), e);
+ failAsyncTest(e.getMessage());
+ }
+ }
+
+ private void failAsyncTest(String msg)
+ {
+ _logger.error("Failing test because: " + msg);
+ _failMsg = msg;
+ }
+} \ No newline at end of file
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/PersistentTestManual.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/PersistentTestManual.java
new file mode 100644
index 0000000000..c4e744573f
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/PersistentTestManual.java
@@ -0,0 +1,276 @@
+/*
+ * 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.queue;
+
+import org.apache.log4j.Logger;
+import org.apache.qpid.AMQChannelClosedException;
+import org.apache.qpid.AMQConnectionClosedException;
+import org.apache.qpid.AMQException;
+import org.apache.qpid.client.AMQConnection;
+import org.apache.qpid.client.AMQSession;
+import org.apache.qpid.url.URLSyntaxException;
+import org.apache.qpid.util.CommandLineParser;
+
+import javax.jms.JMSException;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+import java.io.IOException;
+import java.util.Properties;
+
+public class PersistentTestManual
+{
+ private static final Logger _logger = Logger.getLogger(PersistentTestManual.class);
+
+
+ private static final String QUEUE = "direct://amq.direct//PersistentTest-Queue2?durable='true',exclusive='true'";
+
+ protected AMQConnection _connection;
+
+ protected Session _session;
+
+ protected Queue _queue;
+ private Properties properties;
+
+ private String _brokerDetails;
+ private String _username;
+ private String _password;
+ private String _virtualpath;
+
+ public PersistentTestManual(Properties overrides)
+ {
+ properties = new Properties(defaults);
+ properties.putAll(overrides);
+
+ _brokerDetails = properties.getProperty(BROKER_PROPNAME);
+ _username = properties.getProperty(USERNAME_PROPNAME);
+ _password = properties.getProperty(PASSWORD_PROPNAME);
+ _virtualpath = properties.getProperty(VIRTUAL_HOST_PROPNAME);
+
+ createConnection();
+ }
+
+ protected void createConnection()
+ {
+ try
+ {
+ _connection = new AMQConnection(_brokerDetails, _username, _password, "PersistentTest", _virtualpath);
+
+ _session = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ _connection.start();
+ }
+ catch (Exception e)
+ {
+ _logger.error("Unable to create test class due to:" + e.getMessage(), e);
+ System.exit(0);
+ }
+ }
+
+ public void test() throws AMQException, URLSyntaxException
+ {
+
+ //Create the Durable Queue
+ try
+ {
+ _session.createConsumer(_session.createQueue(QUEUE)).close();
+ }
+ catch (JMSException e)
+ {
+ _logger.error("Unable to create Queue due to:" + e.getMessage(), e);
+ System.exit(0);
+ }
+
+ try
+ {
+ if (testQueue())
+ {
+ // close connection
+ _connection.close();
+ // wait
+ System.out.println("Restart Broker Now");
+ try
+ {
+ System.in.read();
+ }
+ catch (IOException e)
+ {
+ //
+ }
+ finally
+ {
+ System.out.println("Continuing....");
+ }
+
+ //Test queue is still there.
+ AMQConnection connection = new AMQConnection(_brokerDetails, _username, _password, "DifferentClientID", _virtualpath);
+
+ AMQSession session = (AMQSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ try
+ {
+ session.createConsumer(session.createQueue(QUEUE));
+ _logger.error("Create consumer succeeded." +
+ " This shouldn't be allowed as this means the queue didn't exist when it should");
+
+ connection.close();
+
+ exit();
+ }
+ catch (JMSException e)
+ {
+ try
+ {
+ connection.close();
+ }
+ catch (JMSException cce)
+ {
+ if (cce.getLinkedException() instanceof AMQConnectionClosedException)
+ {
+ _logger.error("Channel Close Bug still present QPID-432, should see an 'Error closing session'");
+ }
+ else
+ {
+ exit(cce);
+ }
+ }
+
+ if (e.getLinkedException() instanceof AMQChannelClosedException)
+ {
+ _logger.info("AMQChannelClosedException received as expected");
+ }
+ else
+ {
+ exit(e);
+ }
+ }
+ }
+ }
+ catch (JMSException e)
+ {
+ _logger.error("Unable to test Queue due to:" + e.getMessage(), e);
+ System.exit(0);
+ }
+ }
+
+ private void exit(JMSException e)
+ {
+ _logger.error("JMSException received:" + e.getMessage());
+ e.printStackTrace();
+ exit();
+ }
+
+ private void exit()
+ {
+ try
+ {
+ _connection.close();
+ }
+ catch (JMSException e)
+ {
+ //
+ }
+ System.exit(0);
+ }
+
+ private boolean testQueue() throws JMSException
+ {
+ String TEST_TEXT = "init";
+
+ //Create a new session to send producer
+ Session session = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Queue q = session.createQueue(QUEUE);
+ MessageProducer producer = session.createProducer(q);
+
+ producer.send(session.createTextMessage(TEST_TEXT));
+
+ //create a new consumer on the original session
+ TextMessage m = (TextMessage) _session.createConsumer(q).receive();
+
+
+ if ((m != null) && m.getText().equals(TEST_TEXT))
+ {
+ return true;
+ }
+ else
+ {
+ _logger.error("Incorrect values returned from Queue Test:" + m);
+ System.exit(0);
+ return false;
+ }
+ }
+
+ /** Holds the name of the property to get the test broker url from. */
+ public static final String BROKER_PROPNAME = "broker";
+
+ /** Holds the default broker url for the test. */
+ public static final String BROKER_DEFAULT = "tcp://localhost:5672";
+
+ /** Holds the name of the property to get the test broker virtual path. */
+ public static final String VIRTUAL_HOST_PROPNAME = "virtualHost";
+
+ /** Holds the default virtual path for the test. */
+ public static final String VIRTUAL_HOST_DEFAULT = "";
+
+ /** Holds the name of the property to get the broker access username from. */
+ public static final String USERNAME_PROPNAME = "username";
+
+ /** Holds the default broker log on username. */
+ public static final String USERNAME_DEFAULT = "guest";
+
+ /** Holds the name of the property to get the broker access password from. */
+ public static final String PASSWORD_PROPNAME = "password";
+
+ /** Holds the default broker log on password. */
+ public static final String PASSWORD_DEFAULT = "guest";
+
+ /** Holds the default configuration properties. */
+ public static Properties defaults = new Properties();
+
+ static
+ {
+ defaults.setProperty(BROKER_PROPNAME, BROKER_DEFAULT);
+ defaults.setProperty(USERNAME_PROPNAME, USERNAME_DEFAULT);
+ defaults.setProperty(PASSWORD_PROPNAME, PASSWORD_DEFAULT);
+ defaults.setProperty(VIRTUAL_HOST_PROPNAME, VIRTUAL_HOST_DEFAULT);
+ }
+
+ public static void main(String[] args)
+ {
+ PersistentTestManual test;
+
+ Properties options =
+ CommandLineParser.processCommandLine(args, new CommandLineParser(new String[][]{}), System.getProperties());
+
+ test = new PersistentTestManual(options);
+ try
+ {
+ test.test();
+ System.out.println("Test was successfull.");
+ }
+ catch (Exception e)
+ {
+ _logger.error("Unable to test due to:" + e.getMessage(), e);
+ }
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/PriorityTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/PriorityTest.java
new file mode 100644
index 0000000000..6203e8a194
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/PriorityTest.java
@@ -0,0 +1,212 @@
+/*
+*
+* 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.queue;
+
+import junit.framework.TestCase;
+import junit.framework.Assert;
+import org.apache.log4j.Logger;
+import org.apache.qpid.client.transport.TransportConnection;
+import org.apache.qpid.client.AMQConnection;
+import org.apache.qpid.client.AMQSession;
+import org.apache.qpid.client.AMQQueue;
+import org.apache.qpid.client.AMQDestination;
+import org.apache.qpid.jndi.PropertiesFileInitialContextFactory;
+import org.apache.qpid.test.utils.QpidBrokerTestCase;
+import org.apache.qpid.url.URLSyntaxException;
+import org.apache.qpid.AMQException;
+import org.apache.qpid.framing.AMQShortString;
+import org.apache.qpid.framing.FieldTable;
+
+import javax.jms.*;
+import javax.naming.NamingException;
+import javax.naming.Context;
+import javax.naming.spi.InitialContextFactory;
+import java.util.Hashtable;
+import java.util.HashMap;
+import java.util.Map;
+
+public class PriorityTest extends QpidBrokerTestCase
+{
+ private static final int TIMEOUT = 1500;
+
+
+ private static final Logger _logger = Logger.getLogger(PriorityTest.class);
+
+ protected final String QUEUE = "PriorityQueue";
+
+ private static final int MSG_COUNT = 50;
+
+ private Connection producerConnection;
+ private MessageProducer producer;
+ private Session producerSession;
+ private Queue queue;
+ private Connection consumerConnection;
+ private Session consumerSession;
+
+
+ private MessageConsumer consumer;
+
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+
+ producerConnection = getConnection();
+ producerSession = producerConnection.createSession(true, Session.AUTO_ACKNOWLEDGE);
+
+ producerConnection.start();
+
+ consumerConnection = getConnection();
+ consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ }
+
+ protected void tearDown() throws Exception
+ {
+ producerConnection.close();
+ consumerConnection.close();
+ super.tearDown();
+ }
+
+ public void testPriority() throws JMSException, NamingException, AMQException
+ {
+ final Map<String,Object> arguments = new HashMap<String, Object>();
+ arguments.put("x-qpid-priorities",10);
+ ((AMQSession) producerSession).createQueue(new AMQShortString(QUEUE), true, false, false, arguments);
+ queue = (Queue) producerSession.createQueue("direct://amq.direct/"+QUEUE+"/"+QUEUE+"?durable='false'&autodelete='true'");
+
+ ((AMQSession) producerSession).declareAndBind((AMQDestination)queue);
+ producer = producerSession.createProducer(queue);
+
+ for (int msg = 0; msg < MSG_COUNT; msg++)
+ {
+ producer.setPriority(msg % 10);
+ producer.send(nextMessage(msg, false, producerSession, producer));
+ }
+ producerSession.commit();
+ producer.close();
+ producerSession.close();
+ producerConnection.close();
+
+ consumer = consumerSession.createConsumer(queue);
+ consumerConnection.start();
+ Message received;
+ int receivedCount = 0;
+ Message previous = null;
+ int messageCount = 0;
+ while((received = consumer.receive(1000))!=null)
+ {
+ messageCount++;
+ if(previous != null)
+ {
+ assertTrue("Messages arrived in unexpected order " + messageCount + " " + previous.getIntProperty("msg") + " " + received.getIntProperty("msg") + " " + previous.getJMSPriority() + " " + received.getJMSPriority(), (previous.getJMSPriority() > received.getJMSPriority()) || ((previous.getJMSPriority() == received.getJMSPriority()) && previous.getIntProperty("msg") < received.getIntProperty("msg")) );
+ }
+
+ previous = received;
+ receivedCount++;
+ }
+
+ assertEquals("Incorrect number of message received", 50, receivedCount);
+ }
+
+ public void testOddOrdering() throws AMQException, JMSException
+ {
+ final Map<String,Object> arguments = new HashMap<String, Object>();
+ arguments.put("x-qpid-priorities",3);
+ ((AMQSession) producerSession).createQueue(new AMQShortString(QUEUE), true, false, false, arguments);
+ queue = producerSession.createQueue("direct://amq.direct/"+QUEUE+"/"+QUEUE+"?durable='false'&autodelete='true'");
+
+ ((AMQSession) producerSession).declareAndBind((AMQDestination)queue);
+ producer = producerSession.createProducer(queue);
+
+ // In order ABC
+ producer.setPriority(9);
+ producer.send(nextMessage(1, false, producerSession, producer));
+ producer.setPriority(4);
+ producer.send(nextMessage(2, false, producerSession, producer));
+ producer.setPriority(1);
+ producer.send(nextMessage(3, false, producerSession, producer));
+
+ // Out of order BAC
+ producer.setPriority(4);
+ producer.send(nextMessage(4, false, producerSession, producer));
+ producer.setPriority(9);
+ producer.send(nextMessage(5, false, producerSession, producer));
+ producer.setPriority(1);
+ producer.send(nextMessage(6, false, producerSession, producer));
+
+ // Out of order BCA
+ producer.setPriority(4);
+ producer.send(nextMessage(7, false, producerSession, producer));
+ producer.setPriority(1);
+ producer.send(nextMessage(8, false, producerSession, producer));
+ producer.setPriority(9);
+ producer.send(nextMessage(9, false, producerSession, producer));
+
+ // Reverse order CBA
+ producer.setPriority(1);
+ producer.send(nextMessage(10, false, producerSession, producer));
+ producer.setPriority(4);
+ producer.send(nextMessage(11, false, producerSession, producer));
+ producer.setPriority(9);
+ producer.send(nextMessage(12, false, producerSession, producer));
+ producerSession.commit();
+
+ consumer = consumerSession.createConsumer(queue);
+ consumerConnection.start();
+
+ Message msg = consumer.receive(TIMEOUT);
+ assertEquals(1, msg.getIntProperty("msg"));
+ msg = consumer.receive(TIMEOUT);
+ assertEquals(5, msg.getIntProperty("msg"));
+ msg = consumer.receive(TIMEOUT);
+ assertEquals(9, msg.getIntProperty("msg"));
+ msg = consumer.receive(TIMEOUT);
+ assertEquals(12, msg.getIntProperty("msg"));
+
+ msg = consumer.receive(TIMEOUT);
+ assertEquals(2, msg.getIntProperty("msg"));
+ msg = consumer.receive(TIMEOUT);
+ assertEquals(4, msg.getIntProperty("msg"));
+ msg = consumer.receive(TIMEOUT);
+ assertEquals(7, msg.getIntProperty("msg"));
+ msg = consumer.receive(TIMEOUT);
+ assertEquals(11, msg.getIntProperty("msg"));
+
+ msg = consumer.receive(TIMEOUT);
+ assertEquals(3, msg.getIntProperty("msg"));
+ msg = consumer.receive(TIMEOUT);
+ assertEquals(6, msg.getIntProperty("msg"));
+ msg = consumer.receive(TIMEOUT);
+ assertEquals(8, msg.getIntProperty("msg"));
+ msg = consumer.receive(TIMEOUT);
+ assertEquals(10, msg.getIntProperty("msg"));
+ }
+
+ private Message nextMessage(int msg, boolean first, Session producerSession, MessageProducer producer) throws JMSException
+ {
+ Message send = producerSession.createTextMessage("Message: " + msg);
+ send.setIntProperty("msg", msg);
+
+ return send;
+ }
+
+
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/ProducerFlowControlTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/ProducerFlowControlTest.java
new file mode 100644
index 0000000000..f78b327209
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/ProducerFlowControlTest.java
@@ -0,0 +1,496 @@
+/*
+*
+* 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.queue;
+
+import org.apache.log4j.Logger;
+import org.apache.qpid.client.AMQSession;
+import org.apache.qpid.client.AMQDestination;
+import org.apache.qpid.AMQException;
+import org.apache.qpid.management.common.mbeans.ManagedQueue;
+import org.apache.qpid.server.logging.AbstractTestLogging;
+import org.apache.qpid.test.utils.JMXTestUtils;
+import org.apache.qpid.framing.AMQShortString;
+
+import javax.jms.*;
+import javax.naming.NamingException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.io.IOException;
+
+public class ProducerFlowControlTest extends AbstractTestLogging
+{
+ private static final int TIMEOUT = 10000;
+
+ private static final Logger _logger = Logger.getLogger(ProducerFlowControlTest.class);
+
+ private Connection producerConnection;
+ private MessageProducer producer;
+ private Session producerSession;
+ private Queue queue;
+ private Connection consumerConnection;
+ private Session consumerSession;
+
+ private MessageConsumer consumer;
+ private final AtomicInteger _sentMessages = new AtomicInteger();
+
+ private JMXTestUtils _jmxUtils;
+ private boolean _jmxUtilConnected;
+ private static final String USER = "admin";
+
+ public void setUp() throws Exception
+ {
+ _jmxUtils = new JMXTestUtils(this, USER , USER);
+ _jmxUtils.setUp();
+ _jmxUtilConnected=false;
+ super.setUp();
+
+ _monitor.reset();
+
+ producerConnection = getConnection();
+ producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ producerConnection.start();
+
+ consumerConnection = getConnection();
+ consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ }
+
+ public void tearDown() throws Exception
+ {
+ if(_jmxUtilConnected)
+ {
+ try
+ {
+ _jmxUtils.close();
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+ }
+ producerConnection.close();
+ consumerConnection.close();
+ super.tearDown();
+ }
+
+ public void testCapacityExceededCausesBlock()
+ throws JMSException, NamingException, AMQException, InterruptedException
+ {
+ String queueName = getTestQueueName();
+
+ final Map<String,Object> arguments = new HashMap<String, Object>();
+ arguments.put("x-qpid-capacity",1000);
+ arguments.put("x-qpid-flow-resume-capacity",800);
+ ((AMQSession) producerSession).createQueue(new AMQShortString(queueName), true, false, false, arguments);
+ queue = producerSession.createQueue("direct://amq.direct/"+queueName+"/"+queueName+"?durable='false'&autodelete='true'");
+ ((AMQSession) producerSession).declareAndBind((AMQDestination)queue);
+ producer = producerSession.createProducer(queue);
+
+ _sentMessages.set(0);
+
+
+ // try to send 5 messages (should block after 4)
+ sendMessagesAsync(producer, producerSession, 5, 50L);
+
+ Thread.sleep(5000);
+
+ assertEquals("Incorrect number of message sent before blocking", 4, _sentMessages.get());
+
+ consumer = consumerSession.createConsumer(queue);
+ consumerConnection.start();
+
+
+ consumer.receive();
+
+ Thread.sleep(1000);
+
+ assertEquals("Message incorrectly sent after one message received", 4, _sentMessages.get());
+
+
+ consumer.receive();
+
+ Thread.sleep(1000);
+
+ assertEquals("Message not sent after two messages received", 5, _sentMessages.get());
+
+ }
+
+ public void testBrokerLogMessages()
+ throws JMSException, NamingException, AMQException, InterruptedException, IOException
+ {
+ String queueName = getTestQueueName();
+
+ final Map<String,Object> arguments = new HashMap<String, Object>();
+ arguments.put("x-qpid-capacity",1000);
+ arguments.put("x-qpid-flow-resume-capacity",800);
+ ((AMQSession) producerSession).createQueue(new AMQShortString(queueName), true, false, false, arguments);
+ queue = producerSession.createQueue("direct://amq.direct/"+queueName+"/"+queueName+"?durable='false'&autodelete='true'");
+ ((AMQSession) producerSession).declareAndBind((AMQDestination)queue);
+ producer = producerSession.createProducer(queue);
+
+ _sentMessages.set(0);
+
+
+ // try to send 5 messages (should block after 4)
+ sendMessagesAsync(producer, producerSession, 5, 50L);
+
+ Thread.sleep(5000);
+ List<String> results = waitAndFindMatches("QUE-1003");
+
+ assertEquals("Did not find correct number of QUE-1003 queue overfull messages", 1, results.size());
+
+ consumer = consumerSession.createConsumer(queue);
+ consumerConnection.start();
+
+
+ while(consumer.receive(1000) != null);
+
+ results = waitAndFindMatches("QUE-1004");
+
+ assertEquals("Did not find correct number of UNDERFULL queue underfull messages", 1, results.size());
+
+
+
+ }
+
+
+ public void testClientLogMessages()
+ throws JMSException, NamingException, AMQException, InterruptedException, IOException
+ {
+ String queueName = getTestQueueName();
+
+ setTestClientSystemProperty("qpid.flow_control_wait_failure","3000");
+ setTestClientSystemProperty("qpid.flow_control_wait_notify_period","1000");
+
+ Session session = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+
+ final Map<String,Object> arguments = new HashMap<String, Object>();
+ arguments.put("x-qpid-capacity",1000);
+ arguments.put("x-qpid-flow-resume-capacity",800);
+ ((AMQSession) session).createQueue(new AMQShortString(queueName), true, false, false, arguments);
+ queue = producerSession.createQueue("direct://amq.direct/"+queueName+"/"+queueName+"?durable='false'&autodelete='true'");
+ ((AMQSession) session).declareAndBind((AMQDestination)queue);
+ producer = session.createProducer(queue);
+
+ _sentMessages.set(0);
+
+
+ // try to send 5 messages (should block after 4)
+ MessageSender sender = sendMessagesAsync(producer, producerSession, 5, 50L);
+
+ Thread.sleep(TIMEOUT);
+ List<String> results = waitAndFindMatches("Message send delayed by", TIMEOUT);
+ assertTrue("No delay messages logged by client",results.size()!=0);
+ results = findMatches("Message send failed due to timeout waiting on broker enforced flow control");
+ assertEquals("Incorrect number of send failure messages logged by client",1,results.size());
+
+
+
+ }
+
+
+ public void testFlowControlOnCapacityResumeEqual()
+ throws JMSException, NamingException, AMQException, InterruptedException
+ {
+ String queueName = getTestQueueName();
+
+ final Map<String,Object> arguments = new HashMap<String, Object>();
+ arguments.put("x-qpid-capacity",1000);
+ arguments.put("x-qpid-flow-resume-capacity",1000);
+ ((AMQSession) producerSession).createQueue(new AMQShortString(queueName), true, false, false, arguments);
+ queue = producerSession.createQueue("direct://amq.direct/"+queueName+"/"+queueName+"?durable='false'&autodelete='true'");
+ ((AMQSession) producerSession).declareAndBind((AMQDestination)queue);
+ producer = producerSession.createProducer(queue);
+
+ _sentMessages.set(0);
+
+ // try to send 5 messages (should block after 4)
+ sendMessagesAsync(producer, producerSession, 5, 50L);
+
+ Thread.sleep(5000);
+
+ assertEquals("Incorrect number of message sent before blocking", 4, _sentMessages.get());
+
+ consumer = consumerSession.createConsumer(queue);
+ consumerConnection.start();
+
+
+ consumer.receive();
+
+ Thread.sleep(1000);
+
+ assertEquals("Message incorrectly sent after one message received", 5, _sentMessages.get());
+
+
+ }
+
+
+ public void testFlowControlSoak()
+ throws Exception, NamingException, AMQException, InterruptedException
+ {
+ String queueName = getTestQueueName();
+
+ _sentMessages.set(0);
+ final int numProducers = 10;
+ final int numMessages = 100;
+
+ final Map<String,Object> arguments = new HashMap<String, Object>();
+ arguments.put("x-qpid-capacity",6000);
+ arguments.put("x-qpid-flow-resume-capacity",3000);
+
+ ((AMQSession) consumerSession).createQueue(new AMQShortString(queueName), false, false, false, arguments);
+
+ queue = producerSession.createQueue("direct://amq.direct/"+queueName+"/"+queueName+"?durable='false'&autodelete='false'");
+ ((AMQSession) consumerSession).declareAndBind((AMQDestination)queue);
+ consumerConnection.start();
+
+ Connection[] producers = new Connection[numProducers];
+ for(int i = 0 ; i < numProducers; i ++)
+ {
+
+ producers[i] = getConnection();
+ producers[i].start();
+ Session session = producers[i].createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ MessageProducer myproducer = session.createProducer(queue);
+ MessageSender sender = sendMessagesAsync(myproducer, session, numMessages, 50L);
+ }
+
+ consumer = consumerSession.createConsumer(queue);
+ consumerConnection.start();
+
+ for(int j = 0; j < numProducers * numMessages; j++)
+ {
+
+ Message msg = consumer.receive(5000);
+ Thread.sleep(50L);
+ assertNotNull("Message not received("+j+"), sent: "+_sentMessages.get(), msg);
+
+ }
+
+
+
+ Message msg = consumer.receive(500);
+ assertNull("extra message received", msg);
+
+
+ for(int i = 0; i < numProducers; i++)
+ {
+ producers[i].close();
+ }
+
+ }
+
+
+
+ public void testSendTimeout()
+ throws JMSException, NamingException, AMQException, InterruptedException
+ {
+ String queueName = getTestQueueName();
+
+ setTestClientSystemProperty("qpid.flow_control_wait_failure","3000");
+ Session session = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+
+ final Map<String,Object> arguments = new HashMap<String, Object>();
+ arguments.put("x-qpid-capacity",1000);
+ arguments.put("x-qpid-flow-resume-capacity",800);
+ ((AMQSession) session).createQueue(new AMQShortString(queueName), true, false, false, arguments);
+ queue = producerSession.createQueue("direct://amq.direct/"+queueName+"/"+queueName+"?durable='false'&autodelete='true'");
+ ((AMQSession) session).declareAndBind((AMQDestination)queue);
+ producer = session.createProducer(queue);
+
+ _sentMessages.set(0);
+
+
+ // try to send 5 messages (should block after 4)
+ MessageSender sender = sendMessagesAsync(producer, producerSession, 5, 50L);
+
+ Thread.sleep(10000);
+
+ Exception e = sender.getException();
+
+ assertNotNull("No timeout exception on sending", e);
+
+ }
+
+
+ public void testFlowControlAttributeModificationViaJMX()
+ throws JMSException, NamingException, AMQException, InterruptedException, Exception
+ {
+ _jmxUtils.open();
+ _jmxUtilConnected = true;
+
+ String queueName = getTestQueueName();
+
+ //create queue
+ final Map<String,Object> arguments = new HashMap<String, Object>();
+ arguments.put("x-qpid-capacity",0);
+ arguments.put("x-qpid-flow-resume-capacity",0);
+ ((AMQSession) producerSession).createQueue(new AMQShortString(queueName), true, false, false, arguments);
+
+ queue = producerSession.createQueue("direct://amq.direct/"+queueName+"/"+queueName+"?durable='false'&autodelete='true'");
+
+ ((AMQSession) producerSession).declareAndBind((AMQDestination)queue);
+ producer = producerSession.createProducer(queue);
+
+ Thread.sleep(1000);
+
+ //Create a JMX MBean proxy for the queue
+ ManagedQueue queueMBean = _jmxUtils.getManagedObject(ManagedQueue.class, _jmxUtils.getQueueObjectName("test", queueName));
+ assertNotNull(queueMBean);
+
+ //check current attribute values are 0 as expected
+ assertTrue("Capacity was not the expected value", queueMBean.getCapacity() == 0L);
+ assertTrue("FlowResumeCapacity was not the expected value", queueMBean.getFlowResumeCapacity() == 0L);
+
+ //set new values that will cause flow control to be active, and the queue to become overfull after 1 message is sent
+ queueMBean.setCapacity(250L);
+ queueMBean.setFlowResumeCapacity(250L);
+ assertTrue("Capacity was not the expected value", queueMBean.getCapacity() == 250L);
+ assertTrue("FlowResumeCapacity was not the expected value", queueMBean.getFlowResumeCapacity() == 250L);
+ assertFalse("Queue should not be overfull", queueMBean.isFlowOverfull());
+
+ // try to send 2 messages (should block after 1)
+ _sentMessages.set(0);
+ sendMessagesAsync(producer, producerSession, 2, 50L);
+
+ Thread.sleep(2000);
+
+ //check only 1 message was sent, and queue is overfull
+ assertEquals("Incorrect number of message sent before blocking", 1, _sentMessages.get());
+ assertTrue("Queue should be overfull", queueMBean.isFlowOverfull());
+
+ //raise the attribute values, causing the queue to become underfull and allow the second message to be sent.
+ queueMBean.setCapacity(300L);
+ queueMBean.setFlowResumeCapacity(300L);
+
+ Thread.sleep(2000);
+
+ //check second message was sent, and caused the queue to become overfull again
+ assertEquals("Second message was not sent after lifting FlowResumeCapacity", 2, _sentMessages.get());
+ assertTrue("Queue should be overfull", queueMBean.isFlowOverfull());
+
+ //raise capacity above queue depth, check queue remains overfull as FlowResumeCapacity still exceeded
+ queueMBean.setCapacity(700L);
+ assertTrue("Queue should be overfull", queueMBean.isFlowOverfull());
+
+ //receive a message, check queue becomes underfull
+
+ consumer = consumerSession.createConsumer(queue);
+ consumerConnection.start();
+
+ consumer.receive();
+
+ //perform a synchronous op on the connection
+ ((AMQSession) consumerSession).sync();
+
+ assertFalse("Queue should not be overfull", queueMBean.isFlowOverfull());
+
+ consumer.receive();
+ }
+
+ private MessageSender sendMessagesAsync(final MessageProducer producer,
+ final Session producerSession,
+ final int numMessages,
+ long sleepPeriod)
+ {
+ MessageSender sender = new MessageSender(producer, producerSession, numMessages,sleepPeriod);
+ new Thread(sender).start();
+ return sender;
+ }
+
+ private void sendMessages(MessageProducer producer, Session producerSession, int numMessages, long sleepPeriod)
+ throws JMSException
+ {
+
+ for (int msg = 0; msg < numMessages; msg++)
+ {
+ producer.send(nextMessage(msg, producerSession));
+ _sentMessages.incrementAndGet();
+
+
+ try
+ {
+ ((AMQSession)producerSession).sync();
+ }
+ catch (AMQException e)
+ {
+ e.printStackTrace();
+ throw new RuntimeException(e);
+ }
+ }
+ }
+
+ private static final byte[] BYTE_300 = new byte[300];
+
+
+ private Message nextMessage(int msg, Session producerSession) throws JMSException
+ {
+ BytesMessage send = producerSession.createBytesMessage();
+ send.writeBytes(BYTE_300);
+ send.setIntProperty("msg", msg);
+
+ return send;
+ }
+
+
+ private class MessageSender implements Runnable
+ {
+ private final MessageProducer _producer;
+ private final Session _producerSession;
+ private final int _numMessages;
+
+
+
+ private JMSException _exception;
+ private long _sleepPeriod;
+
+ public MessageSender(MessageProducer producer, Session producerSession, int numMessages, long sleepPeriod)
+ {
+ _producer = producer;
+ _producerSession = producerSession;
+ _numMessages = numMessages;
+ _sleepPeriod = sleepPeriod;
+ }
+
+ public void run()
+ {
+ try
+ {
+ sendMessages(_producer, _producerSession, _numMessages, _sleepPeriod);
+ }
+ catch (JMSException e)
+ {
+ _exception = e;
+ }
+ }
+
+ public JMSException getException()
+ {
+ return _exception;
+ }
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/QueueDepthWithSelectorTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/QueueDepthWithSelectorTest.java
new file mode 100644
index 0000000000..74f50e8659
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/QueueDepthWithSelectorTest.java
@@ -0,0 +1,176 @@
+/*
+ *
+ * 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.queue;
+
+import javax.jms.Connection;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.Session;
+
+import org.apache.qpid.AMQException;
+import org.apache.qpid.client.AMQDestination;
+import org.apache.qpid.client.AMQSession;
+import org.apache.qpid.test.utils.QpidBrokerTestCase;
+
+/**
+ * Test Case to ensure that messages are correctly returned.
+ * This includes checking:
+ * - The message is returned.
+ * - The broker doesn't leak memory.
+ * - The broker's state is correct after test.
+ */
+public class QueueDepthWithSelectorTest extends QpidBrokerTestCase
+{
+ protected final String VHOST = "test";
+ protected final String QUEUE = this.getClass().getName();
+
+ protected Connection _clientConnection;
+ protected Connection _producerConnection;
+ private Session _clientSession;
+ protected Session _producerSession;
+ protected MessageProducer _producer;
+ private MessageConsumer _consumer;
+
+ protected static int MSG_COUNT = 50;
+
+ protected Message[] _messages = new Message[MSG_COUNT];
+
+ protected Queue _queue;
+
+ @Override
+ public void setUp() throws Exception
+ {
+ super.setUp();
+
+ _messages = new Message[MSG_COUNT];
+ _queue = getTestQueue();
+
+ //Create Producer
+ _producerConnection = getConnection();
+ _producerConnection.start();
+ _producerSession = _producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ _producer = _producerSession.createProducer(_queue);
+
+ // Create consumer
+ _clientConnection = getConnection();
+ _clientConnection.start();
+ _clientSession = _clientConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ _consumer = _clientSession.createConsumer(_queue, "key = 23");
+ }
+
+ public void test() throws Exception
+ {
+ //Send messages
+ _logger.info("Starting to send messages");
+ for (int msg = 0; msg < MSG_COUNT; msg++)
+ {
+ _producer.send(nextMessage(msg));
+ }
+ _logger.info("Closing connection");
+ //Close the connection.. .giving the broker time to clean up its state.
+ _producerConnection.close();
+
+ //Verify we get all the messages.
+ _logger.info("Verifying messages");
+ verifyAllMessagesRecevied(50);
+ verifyBrokerState(0);
+
+ //Close the connection.. .giving the broker time to clean up its state.
+ _clientConnection.close();
+
+ //Verify Broker state
+ _logger.info("Verifying broker state");
+ verifyBrokerState(0);
+ }
+
+ protected void verifyBrokerState(int expectedDepth)
+ {
+ try
+ {
+ Connection connection = getConnection();
+ Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Thread.sleep(2000);
+ long queueDepth = ((AMQSession) session).getQueueDepth((AMQDestination) _queue);
+ assertEquals("Session reports Queue depth not as expected", expectedDepth, queueDepth);
+
+ connection.close();
+ }
+ catch (InterruptedException e)
+ {
+ fail(e.getMessage());
+ }
+ catch (AMQException e)
+ {
+ fail(e.getMessage());
+ }
+ catch (Exception e)
+ {
+ fail(e.getMessage());
+ }
+ }
+
+ protected void verifyAllMessagesRecevied(int expectedDepth) throws Exception
+ {
+ boolean[] msgIdRecevied = new boolean[MSG_COUNT];
+
+ for (int i = 0; i < expectedDepth; i++)
+ {
+ _messages[i] = _consumer.receive(1000);
+ assertNotNull("should have received a message but didn't", _messages[i]);
+ }
+
+ //Check received messages
+ int msgId = 0;
+ for (Message msg : _messages)
+ {
+ assertNotNull("Message should not be null", msg);
+ assertEquals("msgId was wrong", msgId, msg.getIntProperty("ID"));
+ assertFalse("Already received msg id " + msgId, msgIdRecevied[msgId]);
+ msgIdRecevied[msgId] = true;
+ msgId++;
+ }
+
+ //Check all received
+ for (msgId = 0; msgId < expectedDepth; msgId++)
+ {
+ assertTrue("Message " + msgId + " not received.", msgIdRecevied[msgId]);
+ }
+ }
+
+ /**
+ * Get the next message putting the given count into the intProperties as ID.
+ *
+ * @param msgNo the message count to store as ID.
+ * @throws JMSException
+ */
+ protected Message nextMessage(int msgNo) throws JMSException
+ {
+ Message send = _producerSession.createTextMessage("MessageReturnTest");
+ send.setIntProperty("ID", msgNo);
+ send.setIntProperty("key", 23);
+ return send;
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/SubscriptionTestHelper.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/SubscriptionTestHelper.java
new file mode 100644
index 0000000000..1152797dbf
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/SubscriptionTestHelper.java
@@ -0,0 +1,294 @@
+/*
+ *
+ * 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.queue;
+
+import org.apache.qpid.server.AMQChannel;
+import org.apache.qpid.server.logging.LogActor;
+import org.apache.qpid.server.subscription.Subscription;
+import org.apache.qpid.framing.AMQShortString;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Queue;
+
+public class SubscriptionTestHelper implements Subscription
+{
+ private final List<QueueEntry> messages;
+ private final Object key;
+ private boolean isSuspended;
+ private AMQQueue.Context _queueContext;
+
+ public SubscriptionTestHelper(Object key)
+ {
+ this(key, new ArrayList<QueueEntry>());
+ }
+
+ public SubscriptionTestHelper(final Object key, final boolean isSuspended)
+ {
+ this(key);
+ setSuspended(isSuspended);
+ }
+
+ SubscriptionTestHelper(Object key, List<QueueEntry> messages)
+ {
+ this.key = key;
+ this.messages = messages;
+ }
+
+ List<QueueEntry> getMessages()
+ {
+ return messages;
+ }
+
+ public void setQueue(AMQQueue queue, boolean exclusive)
+ {
+
+ }
+
+ public void setNoLocal(boolean noLocal)
+ {
+
+ }
+
+ public void send(QueueEntry msg)
+ {
+ messages.add(msg);
+ }
+
+ public void setSuspended(boolean suspended)
+ {
+ isSuspended = suspended;
+ }
+
+ public boolean isSuspended()
+ {
+ return isSuspended;
+ }
+
+ public boolean wouldSuspend(QueueEntry msg)
+ {
+ return isSuspended;
+ }
+
+ public void addToResendQueue(QueueEntry msg)
+ {
+ //no-op
+ }
+
+ public void getSendLock()
+ {
+ return;
+ }
+
+ public void releaseSendLock()
+ {
+ //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public void resend(final QueueEntry entry)
+ {
+ //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public void onDequeue(final QueueEntry queueEntry)
+ {
+
+ }
+
+ public void restoreCredit(QueueEntry queueEntry)
+ {
+ //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public void setStateListener(final StateListener listener)
+ {
+ //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public State getState()
+ {
+ return null; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public AMQQueue.Context getQueueContext()
+ {
+ return _queueContext;
+ }
+
+ public void setQueueContext(AMQQueue.Context queueContext)
+ {
+ _queueContext = queueContext;
+ }
+
+ public boolean setLastSeenEntry(QueueEntry expected, QueueEntry newValue)
+ {
+ return false; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public AMQChannel getChannel()
+ {
+ return null;
+ }
+
+ public void start()
+ {
+ //no-op
+ }
+
+ public AMQShortString getConsumerTag()
+ {
+ return null; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public long getSubscriptionID()
+ {
+ return 0; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public boolean isActive()
+ {
+ return false; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public void confirmAutoClose()
+ {
+ //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public void set(String key, Object value)
+ {
+ //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public Object get(String key)
+ {
+ return null; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public LogActor getLogActor()
+ {
+ return null; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public boolean isTransient()
+ {
+ return false;
+ }
+
+ public AMQQueue getQueue()
+ {
+ return null;
+ }
+
+ public QueueEntry.SubscriptionAcquiredState getOwningState()
+ {
+ return null; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public QueueEntry.SubscriptionAssignedState getAssignedState()
+ {
+ return null; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public void queueDeleted(AMQQueue queue)
+ {
+ }
+
+ public boolean filtersMessages()
+ {
+ return false;
+ }
+
+ public boolean hasInterest(QueueEntry msg)
+ {
+ return true;
+ }
+
+ public boolean isAutoClose()
+ {
+ return false;
+ }
+
+ public Queue<QueueEntry> getPreDeliveryQueue()
+ {
+ return null;
+ }
+
+ public Queue<QueueEntry> getResendQueue()
+ {
+ return null;
+ }
+
+ public Queue<QueueEntry> getNextQueue(Queue<QueueEntry> messages)
+ {
+ return messages;
+ }
+
+ public void enqueueForPreDelivery(QueueEntry msg, boolean deliverFirst)
+ {
+ //no-op
+ }
+
+ public void close()
+ {
+ //no-op
+ }
+
+ public boolean isClosed()
+ {
+ return false;
+ }
+
+ public boolean acquires()
+ {
+ return true;
+ }
+
+ public boolean seesRequeues()
+ {
+ return true;
+ }
+
+ public boolean isBrowser()
+ {
+ return false;
+ }
+
+ public int hashCode()
+ {
+ return key.hashCode();
+ }
+
+ public boolean equals(Object o)
+ {
+ return o instanceof SubscriptionTestHelper && ((SubscriptionTestHelper) o).key.equals(key);
+ }
+
+ public String toString()
+ {
+ return key.toString();
+ }
+
+ public boolean isSessionTransactional()
+ {
+ return false;
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/TimeToLiveTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/TimeToLiveTest.java
new file mode 100644
index 0000000000..abb0781536
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/queue/TimeToLiveTest.java
@@ -0,0 +1,370 @@
+/*
+ *
+ * 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.queue;
+
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.ReentrantLock;
+
+import javax.jms.Connection;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.jms.TopicSubscriber;
+
+import junit.framework.Assert;
+
+import org.apache.log4j.Logger;
+import org.apache.qpid.client.AMQConnection;
+import org.apache.qpid.client.AMQDestination;
+import org.apache.qpid.client.AMQQueue;
+import org.apache.qpid.client.AMQSession;
+import org.apache.qpid.client.AMQTopic;
+import org.apache.qpid.test.utils.QpidBrokerTestCase;
+
+public class TimeToLiveTest extends QpidBrokerTestCase
+{
+ private static final Logger _logger = Logger.getLogger(TimeToLiveTest.class);
+
+ protected final String QUEUE = "TimeToLiveQueue";
+
+ private final long TIME_TO_LIVE = 100L;
+
+ private static final int MSG_COUNT = 50;
+ private static final long SERVER_TTL_TIMEOUT = 60000L;
+
+ public void testPassiveTTL() throws Exception
+ {
+ //Create Client 1
+ Connection clientConnection = getConnection();
+
+ Session clientSession = clientConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ Queue queue = clientSession.createQueue(QUEUE);
+
+ // Create then close the consumer so the queue is actually created
+ // Closing it then reopening it ensures that the consumer shouldn't get messages
+ // which should have expired and allows a shorter sleep period. See QPID-1418
+
+ MessageConsumer consumer = clientSession.createConsumer(queue);
+ consumer.close();
+
+ //Create Producer
+ Connection producerConnection = getConnection();
+
+ producerConnection.start();
+
+ // Move to a Transacted session to ensure that all messages have been delivered to broker before
+ // we start waiting for TTL
+ Session producerSession = producerConnection.createSession(true, Session.SESSION_TRANSACTED);
+
+ MessageProducer producer = producerSession.createProducer(queue);
+
+ //Set TTL
+ int msg = 0;
+ producer.send(nextMessage(String.valueOf(msg), true, producerSession, producer));
+
+ producer.setTimeToLive(TIME_TO_LIVE);
+
+ for (; msg < MSG_COUNT - 2; msg++)
+ {
+ producer.send(nextMessage(String.valueOf(msg), false, producerSession, producer));
+ }
+
+ //Reset TTL
+ producer.setTimeToLive(0L);
+ producer.send(nextMessage(String.valueOf(msg), false, producerSession, producer));
+
+ producerSession.commit();
+
+ consumer = clientSession.createConsumer(queue);
+
+ // Ensure we sleep the required amount of time.
+ ReentrantLock waitLock = new ReentrantLock();
+ Condition wait = waitLock.newCondition();
+ final long MILLIS = 1000000L;
+
+ long waitTime = TIME_TO_LIVE * MILLIS;
+ while (waitTime > 0)
+ {
+ try
+ {
+ waitLock.lock();
+
+ waitTime = wait.awaitNanos(waitTime);
+ }
+ catch (InterruptedException e)
+ {
+ //Stop if we are interrupted
+ fail(e.getMessage());
+ }
+ finally
+ {
+ waitLock.unlock();
+ }
+
+ }
+
+ clientConnection.start();
+
+ //Receive Message 0
+ // Set 5s receive time for messages we expect to receive.
+ Message receivedFirst = consumer.receive(5000);
+ Message receivedSecond = consumer.receive(5000);
+ Message receivedThird = consumer.receive(1000);
+
+ // Log the messages to help diagnosis incase of failure
+ _logger.info("First:"+receivedFirst);
+ _logger.info("Second:"+receivedSecond);
+ _logger.info("Third:"+receivedThird);
+
+ // Only first and last messages sent should survive expiry
+ Assert.assertNull("More messages received", receivedThird);
+
+ Assert.assertNotNull("First message not received", receivedFirst);
+ Assert.assertTrue("First message doesn't have first set.", receivedFirst.getBooleanProperty("first"));
+ Assert.assertEquals("First message has incorrect TTL.", 0L, receivedFirst.getLongProperty("TTL"));
+
+ Assert.assertNotNull("Final message not received", receivedSecond);
+ Assert.assertFalse("Final message has first set.", receivedSecond.getBooleanProperty("first"));
+ Assert.assertEquals("Final message has incorrect TTL.", 0L, receivedSecond.getLongProperty("TTL"));
+
+ clientConnection.close();
+
+ producerConnection.close();
+ }
+
+ private Message nextMessage(String msg, boolean first, Session producerSession, MessageProducer producer) throws JMSException
+ {
+ Message send = producerSession.createTextMessage("Message " + msg);
+ send.setBooleanProperty("first", first);
+ send.setStringProperty("testprop", "TimeToLiveTest");
+ send.setLongProperty("TTL", producer.getTimeToLive());
+ return send;
+ }
+
+
+ /**
+ * Tests the expired messages get actively deleted even on queues which have no consumers
+ * @throws Exception
+ */
+ public void testActiveTTL() throws Exception
+ {
+ Connection producerConnection = getConnection();
+ AMQSession producerSession = (AMQSession) producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ Queue queue = producerSession.createTemporaryQueue();
+ producerSession.declareAndBind((AMQDestination) queue);
+ MessageProducer producer = producerSession.createProducer(queue);
+ producer.setTimeToLive(1000L);
+
+ // send Messages
+ for(int i = 0; i < MSG_COUNT; i++)
+ {
+ producer.send(producerSession.createTextMessage("Message: "+i));
+ }
+ long failureTime = System.currentTimeMillis() + 2 * SERVER_TTL_TIMEOUT;
+
+ // check Queue depth for up to TIMEOUT seconds after the Queue Depth hasn't changed for 100ms.
+ long messageCount = MSG_COUNT;
+ long lastPass;
+
+ do
+ {
+ lastPass = messageCount;
+ Thread.sleep(100);
+ messageCount = producerSession.getQueueDepth((AMQDestination) queue);
+
+ // If we have received messages in the last loop then extend the timeout time.
+ // if we get messages stuck that are not expiring then the failureTime will occur
+ // failing the test. This will help with the scenario when the broker does not
+ // have enough CPU cycles to process the TTLs.
+ if (lastPass != messageCount)
+ {
+ failureTime = System.currentTimeMillis() + 2 * SERVER_TTL_TIMEOUT;
+ }
+ }
+ while(messageCount > 0L && System.currentTimeMillis() < failureTime);
+
+ assertEquals("Messages not automatically expired: ", 0L, messageCount);
+
+ producer.close();
+ producerSession.close();
+ producerConnection.close();
+ }
+
+ public void testPassiveTTLwithDurableSubscription() throws Exception
+ {
+ //Create Client 1
+ Connection clientConnection = getConnection();
+
+ Session clientSession = clientConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ // Create and close the durable subscriber
+ AMQTopic topic = new AMQTopic((AMQConnection) clientConnection, getTestQueueName());
+ TopicSubscriber durableSubscriber = clientSession.createDurableSubscriber(topic, getTestQueueName(),"testprop='TimeToLiveTest'", false);
+ durableSubscriber.close();
+
+ //Create Producer
+ Connection producerConnection = getConnection();
+
+ producerConnection.start();
+
+ // Move to a Transacted session to ensure that all messages have been delivered to broker before
+ // we start waiting for TTL
+ Session producerSession = producerConnection.createSession(true, Session.SESSION_TRANSACTED);
+
+ MessageProducer producer = producerSession.createProducer(topic);
+
+ //Set TTL
+ int msg = 0;
+ producer.send(nextMessage(String.valueOf(msg), true, producerSession, producer));
+
+ producer.setTimeToLive(TIME_TO_LIVE);
+
+ for (; msg < MSG_COUNT - 2; msg++)
+ {
+ producer.send(nextMessage(String.valueOf(msg), false, producerSession, producer));
+ }
+
+ //Reset TTL
+ producer.setTimeToLive(0L);
+ producer.send(nextMessage(String.valueOf(msg), false, producerSession, producer));
+
+ producerSession.commit();
+
+ //resubscribe
+ durableSubscriber = clientSession.createDurableSubscriber(topic, getTestQueueName(),"testprop='TimeToLiveTest'", false);
+
+ // Ensure we sleep the required amount of time.
+ ReentrantLock waitLock = new ReentrantLock();
+ Condition wait = waitLock.newCondition();
+ final long MILLIS = 1000000L;
+
+ long waitTime = TIME_TO_LIVE * MILLIS;
+ while (waitTime > 0)
+ {
+ try
+ {
+ waitLock.lock();
+
+ waitTime = wait.awaitNanos(waitTime);
+ }
+ catch (InterruptedException e)
+ {
+ //Stop if we are interrupted
+ fail(e.getMessage());
+ }
+ finally
+ {
+ waitLock.unlock();
+ }
+
+ }
+
+ clientConnection.start();
+
+ //Receive Message 0
+ // Set 5s receive time for messages we expect to receive.
+ Message receivedFirst = durableSubscriber.receive(5000);
+ Message receivedSecond = durableSubscriber.receive(5000);
+ Message receivedThird = durableSubscriber.receive(1000);
+
+ // Log the messages to help diagnosis incase of failure
+ _logger.info("First:"+receivedFirst);
+ _logger.info("Second:"+receivedSecond);
+ _logger.info("Third:"+receivedThird);
+
+ // Only first and last messages sent should survive expiry
+ Assert.assertNull("More messages received", receivedThird);
+
+ Assert.assertNotNull("First message not received", receivedFirst);
+ Assert.assertTrue("First message doesn't have first set.", receivedFirst.getBooleanProperty("first"));
+ Assert.assertEquals("First message has incorrect TTL.", 0L, receivedFirst.getLongProperty("TTL"));
+
+ Assert.assertNotNull("Final message not received", receivedSecond);
+ Assert.assertFalse("Final message has first set.", receivedSecond.getBooleanProperty("first"));
+ Assert.assertEquals("Final message has incorrect TTL.", 0L, receivedSecond.getLongProperty("TTL"));
+
+ clientSession.unsubscribe(getTestQueueName());
+ clientConnection.close();
+
+ producerConnection.close();
+ }
+
+ public void testActiveTTLwithDurableSubscription() throws Exception
+ {
+ //Create Client 1
+ Connection clientConnection = getConnection();
+ Session clientSession = clientConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ // Create and close the durable subscriber
+ AMQTopic topic = new AMQTopic((AMQConnection) clientConnection, getTestQueueName());
+ TopicSubscriber durableSubscriber = clientSession.createDurableSubscriber(topic, "MyDurableTTLSubscription","testprop='TimeToLiveTest'", false);
+ durableSubscriber.close();
+
+ //Create Producer
+ Connection producerConnection = getConnection();
+ AMQSession producerSession = (AMQSession) producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ MessageProducer producer = producerSession.createProducer(topic);
+ producer.setTimeToLive(1000L);
+
+ // send Messages
+ for(int i = 0; i < MSG_COUNT; i++)
+ {
+ producer.send(producerSession.createTextMessage("Message: "+i));
+ }
+ long failureTime = System.currentTimeMillis() + 2 * SERVER_TTL_TIMEOUT;
+
+ // check Queue depth for up to TIMEOUT seconds after the Queue Depth hasn't changed for 100ms.
+ long messageCount = MSG_COUNT;
+ long lastPass;
+ AMQQueue subcriptionQueue = new AMQQueue("amq.topic","clientid" + ":" + "MyDurableTTLSubscription");
+ do
+ {
+ lastPass = messageCount;
+ Thread.sleep(100);
+ messageCount = producerSession.getQueueDepth((AMQDestination) subcriptionQueue);
+
+ // If we have received messages in the last loop then extend the timeout time.
+ // if we get messages stuck that are not expiring then the failureTime will occur
+ // failing the test. This will help with the scenario when the broker does not
+ // have enough CPU cycles to process the TTLs.
+ if (lastPass != messageCount)
+ {
+ failureTime = System.currentTimeMillis() + 2 * SERVER_TTL_TIMEOUT;
+ }
+ }
+ while(messageCount > 0L && System.currentTimeMillis() < failureTime);
+
+ assertEquals("Messages not automatically expired: ", 0L, messageCount);
+
+ producer.close();
+ producerSession.close();
+ producerConnection.close();
+
+ clientSession.unsubscribe("MyDurableTTLSubscription");
+ clientSession.close();
+ clientConnection.close();
+ }
+
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/AbstractACLTestCase.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/AbstractACLTestCase.java
new file mode 100644
index 0000000000..f845ff1214
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/AbstractACLTestCase.java
@@ -0,0 +1,285 @@
+/*
+ * 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.security.acl;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import javax.jms.Connection;
+import javax.jms.ExceptionListener;
+import javax.jms.JMSException;
+import javax.naming.NamingException;
+
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.lang.StringUtils;
+import org.apache.qpid.AMQException;
+import org.apache.qpid.client.AMQConnection;
+import org.apache.qpid.client.AMQConnectionURL;
+import org.apache.qpid.jms.ConnectionListener;
+import org.apache.qpid.protocol.AMQConstant;
+import org.apache.qpid.test.utils.QpidBrokerTestCase;
+import org.apache.qpid.url.URLSyntaxException;
+
+/**
+ * Abstract test case for ACLs.
+ *
+ * This base class contains convenience methods to mange ACL files and implements a mechanism that allows each
+ * test method to run its own setup code before the broker starts.
+ *
+ * TODO move the pre broker-startup setup method invocation code to {@link QpidBrokerTestCase}
+ *
+ * @see SimpleACLTest
+ * @see ExternalACLTest
+ * @see ExternalACLFileTest
+ * @see ExternalACLJMXTest
+ * @see ExternalAdminACLTest
+ * @see ExhaustiveACLTest
+ */
+public abstract class AbstractACLTestCase extends QpidBrokerTestCase implements ConnectionListener
+{
+ /** Used to synchronise {@link #tearDown()} when exceptions are thrown */
+ protected CountDownLatch _exceptionReceived;
+
+ /** Override this to return the name of the configuration XML file. */
+ public String getConfig()
+ {
+ return "config-systests-acl.xml";
+ }
+
+ /** Override this to setup external ACL files for virtual hosts. */
+ public List<String> getHostList()
+ {
+ return Collections.emptyList();
+ }
+
+ /**
+ * This setup method checks {@link #getConfig()} and {@link #getHostList()} to initialise the broker with specific
+ * ACL configurations and then runs an optional per-test setup method, which is simply a method with the same name
+ * as the test, but starting with {@code setUp} rather than {@code test}.
+ *
+ * @see #setUpACLFile(String)
+ * @see org.apache.qpid.test.utils.QpidBrokerTestCase#setUp()
+ */
+ @Override
+ public void setUp() throws Exception
+ {
+ if (QpidHome == null)
+ {
+ fail("QPID_HOME not set");
+ }
+
+ // Initialise ACLs.
+ _configFile = new File(QpidHome, "etc" + File.separator + getConfig());
+
+ // Initialise ACL files
+ for (String virtualHost : getHostList())
+ {
+ setUpACLFile(virtualHost);
+ }
+
+ // run test specific setup
+ String testSetup = StringUtils.replace(getName(), "test", "setUp");
+ try
+ {
+ Method setup = getClass().getDeclaredMethod(testSetup);
+ setup.invoke(this);
+ }
+ catch (NoSuchMethodException e)
+ {
+ // Ignore
+ }
+ catch (InvocationTargetException e)
+ {
+ throw (Exception) e.getTargetException();
+ }
+
+ super.setUp();
+ }
+
+ @Override
+ public void tearDown() throws Exception
+ {
+ try
+ {
+ super.tearDown();
+ }
+ catch (JMSException e)
+ {
+ //we're throwing this away as it can happen in this test as the state manager remembers exceptions
+ //that we provoked with authentication failures, where the test passes - we can ignore on con close
+ }
+ }
+
+ /**
+ * Configures specific ACL files for a virtual host.
+ *
+ * This method checks for ACL files that exist on the filesystem. If dynamically generatyed ACL files are required in a test,
+ * then it is easier to use the {@code setUp} prefix on a method to generate the ACL file. In order, this method looks
+ * for three files:
+ * <ol>
+ * <li><em>virtualhost</em>-<em>class</em>-<em>test</em>.txt
+ * <li><em>virtualhost</em>-<em>class</em>.txt
+ * <li><em>virtualhost</em>-default.txt
+ * </ol>
+ * The <em>class</em> and <em>test</em> parts are the test class and method names respectively, with the word {@code test}
+ * removed and the rest of the text converted to lowercase. For example, the test class and method named
+ * {@code org.apache.qpid.test.AccessExampleTest#testExampleMethod} on the {@code testhost} virtualhost would use
+ * one of the following files:
+ * <ol>
+ * <li>testhost-accessexample-examplemethod.txt
+ * <li>testhost-accessexample.txt
+ * <li>testhost-default.txt
+ * </ol>
+ * These files should be copied to the <em>${QPID_HOME}/etc</em> directory when the test is run.
+ *
+ * @see #writeACLFile(String, String...)
+ */
+ public void setUpACLFile(String virtualHost) throws IOException, ConfigurationException
+ {
+ String path = QpidHome + File.separator + "etc";
+ String className = StringUtils.substringBeforeLast(getClass().getSimpleName().toLowerCase(), "test");
+ String testName = StringUtils.substringAfter(getName(), "test").toLowerCase();
+
+ File aclFile = new File(path, virtualHost + "-" + className + "-" + testName + ".txt");
+ if (!aclFile.exists())
+ {
+ aclFile = new File(path, virtualHost + "-" + className + ".txt");
+ if (!aclFile.exists())
+ {
+ aclFile = new File(path, virtualHost + "-" + "default.txt");
+ }
+ }
+
+ // Set the ACL file configuration property
+ if (virtualHost.equals("global"))
+ {
+ setConfigurationProperty("security.aclv2", aclFile.getAbsolutePath());
+ }
+ else
+ {
+ setConfigurationProperty("virtualhosts.virtualhost." + virtualHost + ".security.aclv2", aclFile.getAbsolutePath());
+ }
+ }
+
+ public void writeACLFile(String vhost, String...rules) throws ConfigurationException, IOException
+ {
+ File aclFile = File.createTempFile(getClass().getSimpleName(), getName());
+ aclFile.deleteOnExit();
+
+ if ("global".equals(vhost))
+ {
+ setConfigurationProperty("security.aclv2", aclFile.getAbsolutePath());
+ }
+ else
+ {
+ setConfigurationProperty("virtualhosts.virtualhost." + vhost + ".security.aclv2", aclFile.getAbsolutePath());
+ }
+
+ PrintWriter out = new PrintWriter(new FileWriter(aclFile));
+ out.println(String.format("# %s", _testName));
+ for (String line : rules)
+ {
+ out.println(line);
+ }
+ out.close();
+ }
+
+ /**
+ * Creates a connection to the broker, and sets a connection listener to prevent failover and an exception listener
+ * with a {@link CountDownLatch} to synchronise in the {@link #check403Exception(Throwable)} method and allow the
+ * {@link #tearDown()} method to complete properly.
+ */
+ public Connection getConnection(String vhost, String username, String password) throws NamingException, JMSException, URLSyntaxException
+ {
+ AMQConnection connection = (AMQConnection) getConnection(createConnectionURL(vhost, username, password));
+
+ //Prevent Failover
+ connection.setConnectionListener(this);
+
+ //QPID-2081: use a latch to sync on exception causing connection close, to work
+ //around the connection close race during tearDown() causing sporadic failures
+ _exceptionReceived = new CountDownLatch(1);
+
+ connection.setExceptionListener(new ExceptionListener()
+ {
+ public void onException(JMSException e)
+ {
+ _exceptionReceived.countDown();
+ }
+ });
+
+ return (Connection) connection;
+ }
+
+ // Connection Listener Interface - Used here to block failover
+
+ public void bytesSent(long count)
+ {
+ }
+
+ public void bytesReceived(long count)
+ {
+ }
+
+ public boolean preFailover(boolean redirect)
+ {
+ //Prevent failover.
+ return false;
+ }
+
+ public boolean preResubscribe()
+ {
+ return false;
+ }
+
+ public void failoverComplete()
+ {
+ }
+
+ /**
+ * Convenience method to build an {@link AMQConnectionURL} with the right parameters.
+ */
+ public AMQConnectionURL createConnectionURL(String vhost, String username, String password) throws URLSyntaxException
+ {
+ String url = "amqp://" + username + ":" + password + "@clientid/" + vhost + "?brokerlist='" + getBroker() + "?retries='0''";
+ return new AMQConnectionURL(url);
+ }
+
+ /**
+ * Convenience method to validate a JMS exception with a linked {@link AMQConstant#ACCESS_REFUSED} 403 error code exception.
+ */
+ public void check403Exception(Throwable t) throws Exception
+ {
+ assertNotNull("There was no linked exception", t);
+ assertTrue("Wrong linked exception type", t instanceof AMQException);
+ assertEquals("Incorrect error code received", 403, ((AMQException) t).getErrorCode().getCode());
+
+ //use the latch to ensure the control thread waits long enough for the exception thread
+ //to have done enough to mark the connection closed before teardown commences
+ assertTrue("Timed out waiting for conneciton to report close", _exceptionReceived.await(2, TimeUnit.SECONDS));
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/ExhaustiveACLTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/ExhaustiveACLTest.java
new file mode 100644
index 0000000000..1b2c98d30a
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/ExhaustiveACLTest.java
@@ -0,0 +1,195 @@
+/*
+ * 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.security.acl;
+
+import java.util.Arrays;
+import java.util.List;
+
+import javax.jms.Connection;
+import javax.jms.Session;
+
+import org.apache.qpid.AMQException;
+import org.apache.qpid.client.AMQSession;
+import org.apache.qpid.framing.AMQShortString;
+import org.apache.qpid.protocol.AMQConstant;
+
+/**
+ * ACL version 2/3 file testing to verify that ACL entries control queue creation with specific properties.
+ *
+ * Tests have their own ACL files that setup specific permissions, and then try to create queues with every possible combination
+ * of properties to show that rule matching works correctly. For example, a rule that specified {@code autodelete="true"} for
+ * queues with {@link name="temp.true.*"} as well should not affect queues that have names that do not match, or queues that
+ * are not autodelete, or both. Also checks that ACL entries only affect the specified users and virtual hosts.
+ */
+public class ExhaustiveACLTest extends AbstractACLTestCase
+{
+ @Override
+ public String getConfig()
+ {
+ return "config-systests-aclv2.xml";
+ }
+
+ @Override
+ public List<String> getHostList()
+ {
+ return Arrays.asList("test", "test2");
+ }
+
+ /**
+ * Creates a queue.
+ *
+ * Connects to the broker as a particular user and create the named queue on a virtual host, with the provided
+ * parameters. Uses a new {@link Connection} and {@link Session} and closes them afterwards.
+ */
+ private void createQueue(String vhost, String user, String name, boolean autoDelete, boolean durable) throws Exception
+ {
+ Connection conn = getConnection(vhost, user, "guest");
+ Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
+ conn.start();
+ ((AMQSession<?, ?>) sess).createQueue(new AMQShortString(name), autoDelete, durable, false);
+ sess.commit();
+ conn.close();
+ }
+
+ /**
+ * Calls {@link #createQueue(String, String, String, boolean, boolean)} with the provided parameters and checks that
+ * no exceptions were thrown.
+ */
+ private void createQueueSuccess(String vhost, String user, String name, boolean autoDelete, boolean durable) throws Exception
+ {
+ try
+ {
+ createQueue(vhost, user, name, autoDelete, durable);
+ }
+ catch (AMQException e)
+ {
+ fail(String.format("Create queue should have worked for \"%s\" for user %s@%s, autoDelete=%s, durable=%s",
+ name, user, vhost, Boolean.toString(autoDelete), Boolean.toString(durable)));
+ }
+ }
+
+ /**
+ * Calls {@link #createQueue(String, String, String, boolean, boolean)} with the provided parameters and checks that
+ * the exception thrown was an {@link AMQConstant#ACCESS_REFUSED} or 403 error code.
+ */
+ private void createQueueFailure(String vhost, String user, String name, boolean autoDelete, boolean durable) throws Exception
+ {
+ try
+ {
+ createQueue(vhost, user, name, autoDelete, durable);
+ fail(String.format("Create queue should have failed for \"%s\" for user %s@%s, autoDelete=%s, durable=%s",
+ name, user, vhost, Boolean.toString(autoDelete), Boolean.toString(durable)));
+ }
+ catch (AMQException e)
+ {
+ assertEquals("Should be an ACCESS_REFUSED error", 403, e.getErrorCode().getCode());
+ }
+ }
+
+ public void setUpAuthoriseCreateQueueAutodelete() throws Exception
+ {
+ writeACLFile("test",
+ "acl allow client access virtualhost",
+ "acl allow server access virtualhost",
+ "acl allow client create queue name=\"temp.true.*\" autodelete=true",
+ "acl allow client create queue name=\"temp.false.*\" autodelete=false",
+ "acl deny client create queue",
+ "acl allow client delete queue",
+ "acl deny all create queue"
+ );
+ }
+
+ /**
+ * Test creation of temporary queues, with the autodelete property set to true.
+ */
+ public void testAuthoriseCreateQueueAutodelete() throws Exception
+ {
+ createQueueSuccess("test", "client", "temp.true.00", true, false);
+ createQueueSuccess("test", "client", "temp.true.01", true, false);
+ createQueueSuccess("test", "client", "temp.true.02", true, true);
+ createQueueSuccess("test", "client", "temp.false.03", false, false);
+ createQueueSuccess("test", "client", "temp.false.04", false, false);
+ createQueueSuccess("test", "client", "temp.false.05", false, true);
+ createQueueFailure("test", "client", "temp.true.06", false, false);
+ createQueueFailure("test", "client", "temp.false.07", true, false);
+ createQueueFailure("test", "server", "temp.true.08", true, false);
+ createQueueFailure("test", "client", "temp.other.09", false, false);
+ createQueueSuccess("test2", "guest", "temp.true.01", false, false);
+ createQueueSuccess("test2", "guest", "temp.false.02", true, false);
+ createQueueSuccess("test2", "guest", "temp.true.03", true, false);
+ createQueueSuccess("test2", "guest", "temp.false.04", false, false);
+ createQueueSuccess("test2", "guest", "temp.other.05", false, false);
+ }
+
+ public void setUpAuthoriseCreateQueue() throws Exception
+ {
+ writeACLFile("test",
+ "acl allow client access virtualhost",
+ "acl allow server access virtualhost",
+ "acl allow client create queue name=\"create.*\""
+ );
+ }
+
+ /**
+ * Tests creation of named queues.
+ *
+ * If a named queue is specified
+ */
+ public void testAuthoriseCreateQueue() throws Exception
+ {
+ createQueueSuccess("test", "client", "create.00", true, true);
+ createQueueSuccess("test", "client", "create.01", true, false);
+ createQueueSuccess("test", "client", "create.02", false, true);
+ createQueueSuccess("test", "client", "create.03", true, false);
+ createQueueFailure("test", "server", "create.04", true, true);
+ createQueueFailure("test", "server", "create.05", true, false);
+ createQueueFailure("test", "server", "create.06", false, true);
+ createQueueFailure("test", "server", "create.07", true, false);
+ createQueueSuccess("test2", "guest", "create.00", true, true);
+ createQueueSuccess("test2", "guest", "create.01", true, false);
+ createQueueSuccess("test2", "guest", "create.02", false, true);
+ createQueueSuccess("test2", "guest", "create.03", true, false);
+ }
+
+ public void setUpAuthoriseCreateQueueBoth() throws Exception
+ {
+ writeACLFile("test",
+ "acl allow all access virtualhost",
+ "acl allow client create queue name=\"create.*\"",
+ "acl allow all create queue temporary=true"
+ );
+ }
+
+ /**
+ * Tests creation of named queues.
+ *
+ * If a named queue is specified
+ */
+ public void testAuthoriseCreateQueueBoth() throws Exception
+ {
+ createQueueSuccess("test", "client", "create.00", true, false);
+ createQueueSuccess("test", "client", "create.01", false, false);
+ createQueueFailure("test", "server", "create.02", false, false);
+ createQueueFailure("test", "guest", "create.03", false, false);
+ createQueueSuccess("test", "client", "tmp.00", true, false);
+ createQueueSuccess("test", "server", "tmp.01", true, false);
+ createQueueSuccess("test", "guest", "tmp.02", true, false);
+ createQueueSuccess("test2", "guest", "create.02", false, false);
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/ExternalACLFileTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/ExternalACLFileTest.java
new file mode 100644
index 0000000000..1d08015669
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/ExternalACLFileTest.java
@@ -0,0 +1,184 @@
+/*
+ * 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.security.acl;
+
+import java.util.Arrays;
+import java.util.List;
+
+import javax.jms.Connection;
+import javax.jms.Session;
+
+import org.apache.qpid.client.AMQSession;
+import org.apache.qpid.framing.AMQShortString;
+
+/**
+ * Tests that ACL version 2/3 files following the specification work correctly.
+ *
+ * ACL lines that are identical in meaning apart from differences allowed by the specification, such as whitespace or case
+ * of tokens are set up for numbered queues and the queues are then created to show that the meaning is correctly parsed by
+ * the plugin.
+ *
+ * TODO move this to the access-control plugin unit tests instead
+ */
+public class ExternalACLFileTest extends AbstractACLTestCase
+{
+ @Override
+ public String getConfig()
+ {
+ return "config-systests-aclv2.xml";
+ }
+
+ @Override
+ public List<String> getHostList()
+ {
+ return Arrays.asList("test");
+ }
+
+ private void createQueuePrefixList(String prefix, int count)
+ {
+ try
+ {
+ Connection conn = getConnection("test", "client", "guest");
+ Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ conn.start();
+
+ //Create n queues
+ for (int n = 0; n < count; n++)
+ {
+ AMQShortString queueName = new AMQShortString(String.format("%s.%03d", prefix, n));
+ ((AMQSession<?, ?>) sess).createQueue(queueName, false, false, false);
+ }
+
+ conn.close();
+ }
+ catch (Exception e)
+ {
+ fail("Test failed due to:" + e.getMessage());
+ }
+ }
+
+ private void createQueueNameList(String...queueNames)
+ {
+ try
+ {
+ Connection conn = getConnection("test", "client", "guest");
+ Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ conn.start();
+
+ //Create all queues
+ for (String queueName : queueNames)
+ {
+ ((AMQSession<?, ?>) sess).createQueue(new AMQShortString(queueName), false, false, false);
+ }
+
+ conn.close();
+ }
+ catch (Exception e)
+ {
+ fail("Test failed due to:" + e.getMessage());
+ }
+ }
+
+ public void setUpCreateQueueMixedCase() throws Exception
+ {
+ writeACLFile(
+ "test",
+ "acl allow client create queue name=mixed.000",
+ "ACL ALLOW client CREATE QUEUE NAME=mixed.001",
+ "Acl Allow client Create Queue Name=mixed.002",
+ "aCL aLLOW client cREATE qUEUE nAME=mixed.003",
+ "aCl AlLoW client cReAtE qUeUe NaMe=mixed.004"
+ );
+ }
+
+ public void testCreateQueueMixedCase()
+ {
+ createQueuePrefixList("mixed", 5);
+ }
+
+ public void setUpCreateQueueContinuation() throws Exception
+ {
+ writeACLFile(
+ "test",
+ "acl allow client create queue name=continuation.000",
+ "acl allow client create queue \\",
+ " name=continuation.001",
+ "acl allow client \\",
+ " create queue \\",
+ " name=continuation.002",
+ "acl allow \\",
+ " client \\",
+ " create queue \\",
+ " name=continuation.003",
+ "acl \\",
+ " allow \\",
+ " client \\",
+ " create queue \\",
+ " name=continuation.004"
+ );
+ }
+
+ public void testCreateQueueContinuation()
+ {
+ createQueuePrefixList("continuation", 5);
+ }
+
+ public void setUpCreateQueueWhitespace() throws Exception
+ {
+ writeACLFile(
+ "test",
+ "acl allow client create queue name=whitespace.000",
+ "acl\tallow\tclient\tcreate\tqueue\tname=whitespace.001",
+ "acl allow client create queue name = whitespace.002",
+ "acl\tallow\tclient\tcreate\tqueue\tname\t=\twhitespace.003",
+ "acl allow\t\tclient\t \tcreate\t\t queue\t \t name \t =\t \twhitespace.004"
+ );
+ }
+
+ public void testCreateQueueWhitespace()
+ {
+ createQueuePrefixList("whitespace", 5);
+ }
+
+ public void setUpCreateQueueQuoting() throws Exception
+ {
+ writeACLFile(
+ "test",
+ "acl allow client create queue name='quoting.ABC.000'",
+ "acl allow client create queue name='quoting.*.000'",
+ "acl allow client create queue name='quoting.#.000'",
+ "acl allow client create queue name='quoting. .000'",
+ "acl allow client create queue name='quoting.!@$%.000'"
+ );
+ }
+
+ public void testCreateQueueQuoting()
+ {
+ createQueueNameList(
+ "quoting.ABC.000",
+ "quoting.*.000",
+ "quoting.#.000",
+ "quoting. .000",
+ "quoting.!@$%.000"
+ );
+ }
+}
+
+
+
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/ExternalACLJMXTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/ExternalACLJMXTest.java
new file mode 100644
index 0000000000..b823690002
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/ExternalACLJMXTest.java
@@ -0,0 +1,244 @@
+/*
+ * 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.security.acl;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.qpid.AMQConnectionClosedException;
+import org.apache.qpid.AMQException;
+import org.apache.qpid.AMQSecurityException;
+import org.apache.qpid.protocol.AMQConstant;
+import org.apache.qpid.test.utils.JMXTestUtils;
+
+/**
+ * Tests that ACL entries that apply to AMQP objects also apply when those objects are accessed via JMX.
+ */
+public class ExternalACLJMXTest extends AbstractACLTestCase
+{
+ private JMXTestUtils _jmx;
+
+ private static final String QUEUE_NAME = "kipper";
+ private static final String EXCHANGE_NAME = "amq.kipper";
+
+ @Override
+ public String getConfig()
+ {
+ return "config-systests-aclv2.xml";
+ }
+
+ @Override
+ public List<String> getHostList()
+ {
+ return Arrays.asList("test");
+ }
+
+ @Override
+ public void setUp() throws Exception
+ {
+ _jmx = new JMXTestUtils(this, "admin", "admin");
+ _jmx.setUp();
+ super.setUp();
+ _jmx.open();
+ }
+
+ @Override
+ public void tearDown() throws Exception
+ {
+ _jmx.close();
+ super.tearDown();
+ }
+
+ // test-externalacljmx.txt
+ // create queue owner=client # success
+ public void testCreateClientQueueSuccess() throws Exception
+ {
+ //Queue Parameters
+ String queueOwner = "client";
+
+ _jmx.createQueue("test", QUEUE_NAME, queueOwner, true);
+ }
+
+ // test-externalacljmx.txt
+ // create queue owner=client # failure
+ public void testCreateServerQueueFailure() throws Exception
+ {
+ //Queue Parameters
+ String queueOwner = "server";
+
+ try
+ {
+ _jmx.createQueue("test", QUEUE_NAME, queueOwner, true);
+
+ fail("Queue create should fail");
+ }
+ catch (Exception e)
+ {
+ assertNotNull("Cause is not set", e.getCause());
+ assertEquals("Cause message incorrect",
+ "org.apache.qpid.AMQSecurityException: Permission denied: queue-name 'kipper' [error code 403: access refused]", e.getCause().getMessage());
+ }
+ }
+
+ // no create queue acl in file # failure
+ public void testCreateQueueFailure() throws Exception
+ {
+ //Queue Parameters
+ String queueOwner = "guest";
+
+ try
+ {
+ _jmx.createQueue("test", QUEUE_NAME, queueOwner, true);
+
+ fail("Queue create should fail");
+ }
+ catch (Exception e)
+ {
+ assertNotNull("Cause is not set", e.getCause());
+ assertEquals("Cause message incorrect",
+ "org.apache.qpid.AMQSecurityException: Permission denied: queue-name 'kipper' [error code 403: access refused]", e.getCause().getMessage());
+ }
+ }
+
+ // test-externalacljmx.txt
+ // allow create exchange name=amq.kipper.success
+ public void testCreateExchangeSuccess() throws Exception
+ {
+ _jmx.createExchange("test", EXCHANGE_NAME + ".success", "direct", true);
+ }
+
+ // test-externalacljmx.txt
+ // deny create exchange name=amq.kipper.failure
+ public void testCreateExchangeFailure() throws Exception
+ {
+ try
+ {
+ _jmx.createExchange("test", EXCHANGE_NAME + ".failure", "direct", true);
+
+ fail("Exchange create should fail");
+ }
+ catch (Exception e)
+ {
+ assertNotNull("Cause is not set", e.getCause());
+ assertEquals("Cause message incorrect",
+ "org.apache.qpid.AMQSecurityException: Permission denied: exchange-name 'amq.kipper.failure' [error code 403: access refused]", e.getCause().getMessage());
+ }
+ }
+
+ // test-externalacljmx.txt
+ // allow create exchange name=amq.kipper.success
+ // allow delete exchange name=amq.kipper.success
+ public void testDeleteExchangeSuccess() throws Exception
+ {
+ _jmx.createExchange("test", EXCHANGE_NAME + ".success", "direct", true);
+ _jmx.unregisterExchange("test", EXCHANGE_NAME + ".success");
+ }
+
+ // test-externalacljmx-deleteexchangefailure.txt
+ // allow create exchange name=amq.kipper.delete
+ // deny delete exchange name=amq.kipper.delete
+ public void testDeleteExchangeFailure() throws Exception
+ {
+ _jmx.createExchange("test", EXCHANGE_NAME + ".delete", "direct", true);
+ try
+ {
+ _jmx.unregisterExchange("test", EXCHANGE_NAME + ".delete");
+
+ fail("Exchange delete should fail");
+ }
+ catch (Exception e)
+ {
+ assertNotNull("Cause is not set", e.getCause());
+ assertEquals("Cause message incorrect",
+ "org.apache.qpid.AMQSecurityException: Permission denied [error code 403: access refused]", e.getCause().getMessage());
+ }
+ }
+
+ /**
+ * admin user has JMX right but not AMQP
+ */
+ public void setUpCreateQueueJMXRights() throws Exception
+ {
+ writeACLFile("test",
+ "ACL ALLOW admin EXECUTE METHOD component=\"VirtualHost.VirtualHostManager\" name=\"createNewQueue\"",
+ "ACL DENY admin CREATE QUEUE");
+ }
+
+ public void testCreateQueueJMXRights() throws Exception
+ {
+ try
+ {
+ _jmx.createQueue("test", QUEUE_NAME, "admin", true);
+
+ fail("Queue create should fail");
+ }
+ catch (Exception e)
+ {
+ assertNotNull("Cause is not set", e.getCause());
+ assertEquals("Cause message incorrect",
+ "org.apache.qpid.AMQSecurityException: Permission denied: queue-name 'kipper' [error code 403: access refused]", e.getCause().getMessage());
+ }
+ }
+
+ /**
+ * admin user has AMQP right but not JMX
+ */
+ public void setUpCreateQueueAMQPRights() throws Exception
+ {
+ writeACLFile("test",
+ "ACL DENY admin EXECUTE METHOD component=\"VirtualHost.VirtualHostManager\" name=\"createNewQueue\"",
+ "ACL ALLOW admin CREATE QUEUE");
+ }
+
+ public void testCreateQueueAMQPRights() throws Exception
+ {
+ try
+ {
+ _jmx.createQueue("test", QUEUE_NAME, "admin", true);
+
+ fail("Queue create should fail");
+ }
+ catch (Exception e)
+ {
+ assertEquals("Cause message incorrect", "Permission denied: Execute createNewQueue", e.getMessage());
+ }
+ }
+
+ /**
+ * admin has both JMX and AMQP rights
+ */
+ public void setUpCreateQueueJMXAMQPRights() throws Exception
+ {
+ writeACLFile("test",
+ "ACL ALLOW admin EXECUTE METHOD component=\"VirtualHost.VirtualHostManager\" name=\"createNewQueue\"",
+ "ACL ALLOW admin CREATE QUEUE");
+ }
+
+ public void testCreateQueueJMXAMQPRights() throws Exception
+ {
+ try
+ {
+ _jmx.createQueue("test", QUEUE_NAME, "admin", true);
+ }
+ catch (Exception e)
+ {
+ fail("Queue create should succeed: " + e.getCause().getMessage());
+ }
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/ExternalACLTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/ExternalACLTest.java
new file mode 100644
index 0000000000..4603cc1862
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/ExternalACLTest.java
@@ -0,0 +1,37 @@
+/*
+ * 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.security.acl;
+
+import java.util.Arrays;
+import java.util.List;
+
+public class ExternalACLTest extends SimpleACLTest
+{
+ @Override
+ public String getConfig()
+ {
+ return "config-systests-aclv2.xml";
+ }
+
+ @Override
+ public List<String> getHostList()
+ {
+ return Arrays.asList("test", "test2");
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/ExternalAdminACLTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/ExternalAdminACLTest.java
new file mode 100644
index 0000000000..290cbfdc14
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/ExternalAdminACLTest.java
@@ -0,0 +1,186 @@
+/*
+ * 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.security.acl;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.qpid.server.logging.management.LoggingManagementMBean;
+import org.apache.qpid.test.utils.JMXTestUtils;
+
+/**
+ * Tests that ACLs can be applied to mangement operations that do not correspond to a specific AMQP object.
+ *
+ * Theses tests use the logging component, exposed as the {@link LoggingManagementMBean}, to get and set properties.
+ */
+public class ExternalAdminACLTest extends AbstractACLTestCase
+{
+ private static final String CATEGORY_PRIORITY = "LogManMBeanTest.category.priority";
+ private static final String CATEGORY_LEVEL = "LogManMBeanTest.category.level";
+ private static final String LOGGER_LEVEL = "LogManMBeanTest.logger.level";
+
+ private static final String NEWLINE = System.getProperty("line.separator");
+
+ private JMXTestUtils _jmx;
+ private File _testConfigFile;
+
+ @Override
+ public String getConfig()
+ {
+ return "config-systests-aclv2.xml";
+ }
+
+ @Override
+ public List<String> getHostList()
+ {
+ return Arrays.asList("global");
+ }
+
+ @Override
+ public void setUp() throws Exception
+ {
+ _testConfigFile = createTempTestLog4JConfig();
+
+ _jmx = new JMXTestUtils(this, "admin", "admin");
+ _jmx.setUp();
+ super.setUp();
+ _jmx.open();
+ }
+
+ @Override
+ public void tearDown() throws Exception
+ {
+ _jmx.close();
+ super.tearDown();
+ }
+
+ private File createTempTestLog4JConfig()
+ {
+ File tmpFile = null;
+ try
+ {
+ tmpFile = File.createTempFile("LogManMBeanTestLog4jConfig", ".tmp");
+ tmpFile.deleteOnExit();
+
+ FileWriter fstream = new FileWriter(tmpFile);
+ BufferedWriter writer = new BufferedWriter(fstream);
+
+ writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+NEWLINE);
+ writer.write("<!DOCTYPE log4j:configuration SYSTEM \"log4j.dtd\">"+NEWLINE);
+
+ writer.write("<log4j:configuration xmlns:log4j=\"http://jakarta.apache.org/log4j/\" debug=\"null\" " +
+ "threshold=\"null\">"+NEWLINE);
+
+ writer.write(" <appender class=\"org.apache.log4j.ConsoleAppender\" name=\"STDOUT\">"+NEWLINE);
+ writer.write(" <layout class=\"org.apache.log4j.PatternLayout\">"+NEWLINE);
+ writer.write(" <param name=\"ConversionPattern\" value=\"%d %-5p [%t] %C{2} (%F:%L) - %m%n\"/>"+NEWLINE);
+ writer.write(" </layout>"+NEWLINE);
+ writer.write(" </appender>"+NEWLINE);
+
+ //Example of a 'category' with a 'priority'
+ writer.write(" <category additivity=\"true\" name=\"" + CATEGORY_PRIORITY +"\">"+NEWLINE);
+ writer.write(" <priority value=\"info\"/>"+NEWLINE);
+ writer.write(" <appender-ref ref=\"STDOUT\"/>"+NEWLINE);
+ writer.write(" </category>"+NEWLINE);
+
+ //Example of a 'category' with a 'level'
+ writer.write(" <category additivity=\"true\" name=\"" + CATEGORY_LEVEL +"\">"+NEWLINE);
+ writer.write(" <level value=\"warn\"/>"+NEWLINE);
+ writer.write(" <appender-ref ref=\"STDOUT\"/>"+NEWLINE);
+ writer.write(" </category>"+NEWLINE);
+
+ //Example of a 'logger' with a 'level'
+ writer.write(" <logger additivity=\"true\" name=\"" + LOGGER_LEVEL + "\">"+NEWLINE);
+ writer.write(" <level value=\"error\"/>"+NEWLINE);
+ writer.write(" <appender-ref ref=\"STDOUT\"/>"+NEWLINE);
+ writer.write(" </logger>"+NEWLINE);
+
+ //'root' logger
+ writer.write(" <root>"+NEWLINE);
+ writer.write(" <priority value=\"info\"/>"+NEWLINE);
+ writer.write(" <appender-ref ref=\"STDOUT\"/>"+NEWLINE);
+ writer.write(" </root>"+NEWLINE);
+
+ writer.write("</log4j:configuration>"+NEWLINE);
+
+ writer.flush();
+ writer.close();
+ }
+ catch (IOException e)
+ {
+ fail("Unable to create temporary test log4j configuration");
+ }
+
+ return tmpFile;
+ }
+
+ public void testGetAllLoggerLevels() throws Exception
+ {
+ String[] levels = _jmx.getAvailableLoggerLevels();
+ for (int i = 0; i < levels.length; i++)
+ {
+ System.out.println(levels[i]);
+ }
+ assertEquals("Got incorrect number of log levels", 9, levels.length);
+ }
+
+ public void testGetAllLoggerLevelsDenied() throws Exception
+ {
+ try
+ {
+ _jmx.getAvailableLoggerLevels();
+ fail("Got list of log levels");
+ }
+ catch (Exception e)
+ {
+ // Exception throws
+ e.printStackTrace();
+ assertEquals("Permission denied: Access getAvailableLoggerLevels", e.getMessage());
+ }
+ }
+
+ public void testChangeLoggerLevel() throws Exception
+ {
+ String oldLevel = _jmx.getRuntimeRootLoggerLevel();
+ System.out.println("old level = " + oldLevel);
+ _jmx.setRuntimeRootLoggerLevel("DEBUG");
+ String newLevel = _jmx.getRuntimeRootLoggerLevel();
+ System.out.println("new level = " + newLevel);
+ assertEquals("Logging level was not changed", "DEBUG", newLevel);
+ }
+
+ public void testChangeLoggerLevelDenied() throws Exception
+ {
+ try
+ {
+ _jmx.setRuntimeRootLoggerLevel("DEBUG");
+ fail("Logging level was changed");
+ }
+ catch (Exception e)
+ {
+ assertEquals("Permission denied: Update setRuntimeRootLoggerLevel", e.getMessage());
+ }
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/SimpleACLTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/SimpleACLTest.java
new file mode 100644
index 0000000000..a50817e659
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/security/acl/SimpleACLTest.java
@@ -0,0 +1,644 @@
+/*
+ * 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.security.acl;
+
+import java.io.IOException;
+
+import javax.jms.Connection;
+import javax.jms.DeliveryMode;
+import javax.jms.IllegalStateException;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+import javax.jms.Topic;
+import javax.jms.TopicSubscriber;
+import javax.naming.NamingException;
+
+import org.apache.qpid.AMQException;
+import org.apache.qpid.client.AMQConnection;
+import org.apache.qpid.client.AMQSession;
+import org.apache.qpid.framing.AMQShortString;
+import org.apache.qpid.protocol.AMQConstant;
+import org.apache.qpid.url.URLSyntaxException;
+
+/**
+ * Basic access control list tests.
+ *
+ * These tests require an access control security plugin to be configured in the broker, and carry out various broker
+ * operations that will succeed or fail depending on the user and virtual host. See the {@code config-systests-acl-setup.xml}
+ * configuration file for the SimpleXML version of the ACLs used by the Java broker only, or the various {@code .txt}
+ * files in the system tests directory for the external version 3 ACL files used by both the Java and C++ brokers.
+ * <p>
+ * This class can be extended and the {@link #getConfig()} method overridden to run the same tests with a different type
+ * of access control mechanism. Extension classes should differ only in the configuration file used, but extra tests can be
+ * added that are specific to a particular configuration.
+ * <p>
+ * The tests perform basic AMQP operations like creating queues or excahnges and publishing and consuming messages, using
+ * JMS to contact the broker.
+ *
+ * @see ExternalACLTest
+ */
+public class SimpleACLTest extends AbstractACLTestCase
+{
+ public void testAccessAuthorizedSuccess() throws AMQException, URLSyntaxException, Exception
+ {
+ try
+ {
+ Connection conn = getConnection("test", "client", "guest");
+ Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
+ conn.start();
+
+ //Do something to show connection is active.
+ sess.rollback();
+
+ conn.close();
+ }
+ catch (Exception e)
+ {
+ fail("Connection was not created due to:" + e);
+ }
+ }
+
+ public void testAccessVhostAuthorisedGuestSuccess() throws IOException, Exception
+ {
+ //The 'guest' user has no access to the 'test' vhost, as tested below in testAccessNoRights(), and so
+ //is unable to perform actions such as connecting (and by extension, creating a queue, and consuming
+ //from a queue etc). In order to test the vhost-wide 'access' ACL right, the 'guest' user has been given
+ //this right in the 'test2' vhost.
+
+ try
+ {
+ //get a connection to the 'test2' vhost using the guest user and perform various actions.
+ Connection conn = getConnection("test2", "guest", "guest");
+ Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ conn.start();
+
+ //create Queues and consumers for each
+ Queue namedQueue = sess.createQueue("vhostAccessCreatedQueue" + getTestQueueName());
+ Queue tempQueue = sess.createTemporaryQueue();
+ MessageConsumer consumer = sess.createConsumer(namedQueue);
+ MessageConsumer tempConsumer = sess.createConsumer(tempQueue);
+
+ //send a message to each queue (also causing an exchange declare)
+ MessageProducer sender = ((AMQSession<?, ?>) sess).createProducer(null);
+ ((org.apache.qpid.jms.MessageProducer) sender).send(namedQueue, sess.createTextMessage("test"),
+ DeliveryMode.NON_PERSISTENT, 0, 0L, false, false, true);
+ ((org.apache.qpid.jms.MessageProducer) sender).send(tempQueue, sess.createTextMessage("test"),
+ DeliveryMode.NON_PERSISTENT, 0, 0L, false, false, true);
+
+ //consume the messages from the queues
+ consumer.receive(2000);
+ tempConsumer.receive(2000);
+
+ conn.close();
+ }
+ catch (Exception e)
+ {
+ fail("Test failed due to:" + e.getMessage());
+ }
+ }
+
+ public void testAccessNoRightsFailure() throws Exception
+ {
+ try
+ {
+ Connection conn = getConnection("test", "guest", "guest");
+ Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
+ conn.start();
+ sess.rollback();
+
+ fail("Connection was created.");
+ }
+ catch (JMSException e)
+ {
+ // JMSException -> linkedException -> cause = AMQException (403 or 320)
+ Exception linkedException = e.getLinkedException();
+ assertNotNull("There was no linked exception", linkedException);
+ Throwable cause = linkedException.getCause();
+ assertNotNull("Cause was null", cause);
+ assertTrue("Wrong linked exception type", cause instanceof AMQException);
+ AMQConstant errorCode = isBroker010() ? AMQConstant.CONTEXT_IN_USE : AMQConstant.ACCESS_REFUSED;
+ assertEquals("Incorrect error code received", errorCode, ((AMQException) cause).getErrorCode());
+ }
+ }
+
+ public void testClientDeleteQueueSuccess() throws Exception
+ {
+ try
+ {
+ Connection conn = getConnection("test", "client", "guest");
+ Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
+ conn.start();
+
+ // create kipper
+ Topic kipper = sess.createTopic("kipper");
+ TopicSubscriber subscriber = sess.createDurableSubscriber(kipper, "kipper");
+
+ subscriber.close();
+ sess.unsubscribe("kipper");
+
+ //Do something to show connection is active.
+ sess.rollback();
+ conn.close();
+ }
+ catch (Exception e)
+ {
+ fail("Test failed due to:" + e.getMessage());
+ }
+ }
+
+ public void testServerDeleteQueueFailure() throws Exception
+ {
+ try
+ {
+ Connection conn = getConnection("test", "server", "guest");
+ Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
+ conn.start();
+
+ // create kipper
+ Topic kipper = sess.createTopic("kipper");
+ TopicSubscriber subscriber = sess.createDurableSubscriber(kipper, "kipper");
+
+ subscriber.close();
+ sess.unsubscribe("kipper");
+
+ //Do something to show connection is active.
+ sess.rollback();
+ conn.close();
+ }
+ catch (JMSException e)
+ {
+ // JMSException -> linedException = AMQException.403
+ check403Exception(e.getLinkedException());
+ }
+ }
+
+ public void testClientConsumeFromTempQueueSuccess() throws AMQException, URLSyntaxException, Exception
+ {
+ try
+ {
+ Connection conn = getConnection("test", "client", "guest");
+
+ Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ conn.start();
+
+ sess.createConsumer(sess.createTemporaryQueue());
+
+ conn.close();
+ }
+ catch (Exception e)
+ {
+ fail("Test failed due to:" + e.getMessage());
+ }
+ }
+
+ public void testClientConsumeFromNamedQueueFailure() throws NamingException, Exception
+ {
+ try
+ {
+ Connection conn = getConnection("test", "client", "guest");
+
+ Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ conn.start();
+
+ sess.createConsumer(sess.createQueue("IllegalQueue"));
+
+ fail("Test failed as consumer was created.");
+ }
+ catch (JMSException e)
+ {
+ check403Exception(e.getLinkedException());
+ }
+ }
+
+ public void testClientCreateTemporaryQueueSuccess() throws JMSException, URLSyntaxException, Exception
+ {
+ try
+ {
+ Connection conn = getConnection("test", "client", "guest");
+
+ Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ conn.start();
+
+ //Create Temporary Queue - can't use the createTempQueue as QueueName is null.
+ ((AMQSession<?, ?>) sess).createQueue(new AMQShortString("doesnt_matter_as_autodelete_means_tmp"),
+ true, false, false);
+
+ conn.close();
+ }
+ catch (Exception e)
+ {
+ fail("Test failed due to:" + e.getMessage());
+ }
+ }
+
+ public void testClientCreateNamedQueueFailure() throws NamingException, JMSException, AMQException, Exception
+ {
+ try
+ {
+ Connection conn = getConnection("test", "client", "guest");
+
+ Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ conn.start();
+
+ //Create a Named Queue
+ ((AMQSession<?, ?>) sess).createQueue(new AMQShortString("IllegalQueue"), false, false, false);
+
+ fail("Test failed as Queue creation succeded.");
+ //conn will be automatically closed
+ }
+ catch (AMQException e)
+ {
+ check403Exception(e);
+ }
+ }
+
+ public void testClientPublishUsingTransactionSuccess() throws AMQException, URLSyntaxException, Exception
+ {
+ try
+ {
+ Connection conn = getConnection("test", "client", "guest");
+
+ Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
+
+ conn.start();
+
+ MessageProducer sender = sess.createProducer(sess.createQueue("example.RequestQueue"));
+
+ sender.send(sess.createTextMessage("test"));
+
+ //Send the message using a transaction as this will allow us to retrieve any errors that occur on the broker.
+ sess.commit();
+
+ conn.close();
+ }
+ catch (Exception e)
+ {
+ fail("Test publish failed:" + e);
+ }
+ }
+
+ public void testClientPublishValidQueueSuccess() throws AMQException, URLSyntaxException, Exception
+ {
+ try
+ {
+ Connection conn = getConnection("test", "client", "guest");
+
+ Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ conn.start();
+
+ MessageProducer sender = ((AMQSession<?, ?>) sess).createProducer(null);
+
+ Queue queue = sess.createQueue("example.RequestQueue");
+
+ // Send a message that we will wait to be sent, this should give the broker time to process the msg
+ // before we finish this test. Message is set !immed !mand as the queue is invalid so want to test ACLs not
+ // queue existence.
+ ((org.apache.qpid.jms.MessageProducer) sender).send(queue, sess.createTextMessage("test"),
+ DeliveryMode.NON_PERSISTENT, 0, 0L, false, false, true);
+
+ conn.close();
+ }
+ catch (Exception e)
+ {
+ fail("Test publish failed:" + e);
+ }
+ }
+
+ public void testClientPublishInvalidQueueSuccess() throws AMQException, URLSyntaxException, JMSException, NamingException, Exception
+ {
+ try
+ {
+ Connection conn = getConnection("test", "client", "guest");
+
+ Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ conn.start();
+
+ MessageProducer sender = ((AMQSession<?, ?>) session).createProducer(null);
+
+ Queue queue = session.createQueue("Invalid");
+
+ // Send a message that we will wait to be sent, this should give the broker time to close the connection
+ // before we finish this test. Message is set !immed !mand as the queue is invalid so want to test ACLs not
+ // queue existence.
+ ((org.apache.qpid.jms.MessageProducer) sender).send(queue, session.createTextMessage("test"),
+ DeliveryMode.NON_PERSISTENT, 0, 0L, false, false, true);
+
+ // Test the connection with a valid consumer
+ // This may fail as the session may be closed before the queue or the consumer created.
+ Queue temp = session.createTemporaryQueue();
+
+ session.createConsumer(temp).close();
+
+ //Connection should now be closed and will throw the exception caused by the above send
+ conn.close();
+
+ fail("Close is not expected to succeed.");
+ }
+ catch (IllegalStateException e)
+ {
+ _logger.info("QPID-2345: Session became closed and we got that error rather than the authentication error.");
+ }
+ catch (JMSException e)
+ {
+ check403Exception(e.getLinkedException());
+ }
+ }
+
+ public void testServerConsumeFromNamedQueueValid() throws AMQException, URLSyntaxException, Exception
+ {
+ try
+ {
+ Connection conn = getConnection("test", "server", "guest");
+
+ Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ conn.start();
+
+ sess.createConsumer(sess.createQueue("example.RequestQueue"));
+
+ conn.close();
+ }
+ catch (Exception e)
+ {
+ fail("Test failed due to:" + e.getMessage());
+ }
+ }
+
+ public void testServerConsumeFromNamedQueueInvalid() throws AMQException, URLSyntaxException, NamingException, Exception
+ {
+ try
+ {
+ Connection conn = getConnection("test", "client", "guest");
+
+ Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ conn.start();
+
+ sess.createConsumer(sess.createQueue("Invalid"));
+
+ fail("Test failed as consumer was created.");
+ }
+ catch (JMSException e)
+ {
+ check403Exception(e.getLinkedException());
+ }
+ }
+
+ public void testServerConsumeFromTemporaryQueue() throws AMQException, URLSyntaxException, NamingException, Exception
+ {
+ try
+ {
+ Connection conn = getConnection("test", "server", "guest");
+
+ Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ conn.start();
+
+ sess.createConsumer(sess.createTemporaryQueue());
+
+ fail("Test failed as consumer was created.");
+ }
+ catch (JMSException e)
+ {
+ check403Exception(e.getLinkedException());
+ }
+ }
+
+ public void testServerCreateNamedQueueValid() throws JMSException, URLSyntaxException, Exception
+ {
+ try
+ {
+ Connection conn = getConnection("test", "server", "guest");
+
+ Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ conn.start();
+
+ //Create Temporary Queue
+ ((AMQSession<?, ?>) sess).createQueue(new AMQShortString("example.RequestQueue"), false, false, false);
+
+ conn.close();
+ }
+ catch (Exception e)
+ {
+ fail("Test failed due to:" + e.getMessage());
+ }
+ }
+
+ public void testServerCreateNamedQueueInvalid() throws JMSException, URLSyntaxException, AMQException, NamingException, Exception
+ {
+ try
+ {
+ Connection conn = getConnection("test", "server", "guest");
+
+ Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ conn.start();
+
+ //Create a Named Queue
+ ((AMQSession<?, ?>) sess).createQueue(new AMQShortString("IllegalQueue"), false, false, false);
+
+ fail("Test failed as creation succeded.");
+ }
+ catch (Exception e)
+ {
+ check403Exception(e);
+ }
+ }
+
+ public void testServerCreateTemporaryQueueInvalid() throws NamingException, Exception
+ {
+ try
+ {
+ Connection conn = getConnection("test", "server", "guest");
+ Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ conn.start();
+
+ session.createTemporaryQueue();
+
+ fail("Test failed as creation succeded.");
+ }
+ catch (JMSException e)
+ {
+ check403Exception(e.getLinkedException());
+ }
+ }
+
+ public void testServerCreateAutoDeleteQueueInvalid() throws NamingException, JMSException, AMQException, Exception
+ {
+ try
+ {
+ Connection connection = getConnection("test", "server", "guest");
+
+ Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ connection.start();
+
+ ((AMQSession<?, ?>) session).createQueue(new AMQShortString("again_ensure_auto_delete_queue_for_temporary"),
+ true, false, false);
+
+ fail("Test failed as creation succeded.");
+ }
+ catch (Exception e)
+ {
+ check403Exception(e);
+ }
+ }
+
+ /**
+ * This test uses both the cilent and sender to validate that the Server is able to publish to a temporary queue.
+ * The reason the client must be involved is that the Server is unable to create its own Temporary Queues.
+ *
+ * @throws AMQException
+ * @throws URLSyntaxException
+ * @throws JMSException
+ */
+ public void testServerPublishUsingTransactionSuccess() throws AMQException, URLSyntaxException, JMSException, NamingException, Exception
+ {
+ //Set up the Server
+ Connection serverConnection = getConnection("test", "server", "guest");
+
+ Session serverSession = serverConnection.createSession(true, Session.SESSION_TRANSACTED);
+
+ Queue requestQueue = serverSession.createQueue("example.RequestQueue");
+
+ MessageConsumer server = serverSession.createConsumer(requestQueue);
+
+ serverConnection.start();
+
+ //Set up the consumer
+ Connection clientConnection = getConnection("test", "client", "guest");
+
+ //Send a test mesage
+ Session clientSession = clientConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ Queue responseQueue = clientSession.createTemporaryQueue();
+
+ MessageConsumer clientResponse = clientSession.createConsumer(responseQueue);
+
+ clientConnection.start();
+
+ Message request = clientSession.createTextMessage("Request");
+
+ assertNotNull("Response Queue is null", responseQueue);
+
+ request.setJMSReplyTo(responseQueue);
+
+ clientSession.createProducer(requestQueue).send(request);
+
+ try
+ {
+ Message msg = null;
+
+ msg = server.receive(2000);
+
+ while (msg != null && !((TextMessage) msg).getText().equals("Request"))
+ {
+ msg = server.receive(2000);
+ }
+
+ assertNotNull("Message not received", msg);
+
+ assertNotNull("Reply-To is Null", msg.getJMSReplyTo());
+
+ MessageProducer sender = serverSession.createProducer(msg.getJMSReplyTo());
+
+ sender.send(serverSession.createTextMessage("Response"));
+
+ //Send the message using a transaction as this will allow us to retrieve any errors that occur on the broker.
+ serverSession.commit();
+
+ //Ensure Response is received.
+ Message clientResponseMsg = clientResponse.receive(2000);
+ assertNotNull("Client did not receive response message,", clientResponseMsg);
+ assertEquals("Incorrect message received", "Response", ((TextMessage) clientResponseMsg).getText());
+
+ }
+ catch (Exception e)
+ {
+ fail("Test publish failed:" + e);
+ }
+ finally
+ {
+ try
+ {
+ serverConnection.close();
+ }
+ finally
+ {
+ clientConnection.close();
+ }
+ }
+ }
+
+ public void testServerPublishInvalidQueueSuccess() throws AMQException, URLSyntaxException, JMSException, NamingException, Exception
+ {
+ try
+ {
+ Connection conn = getConnection("test", "server", "guest");
+
+ ((AMQConnection) conn).setConnectionListener(this);
+
+ Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ conn.start();
+
+ MessageProducer sender = ((AMQSession<?, ?>) session).createProducer(null);
+
+ Queue queue = session.createQueue("Invalid");
+
+ // Send a message that we will wait to be sent, this should give the broker time to close the connection
+ // before we finish this test. Message is set !immed !mand as the queue is invalid so want to test ACLs not
+ // queue existence.
+ ((org.apache.qpid.jms.MessageProducer) sender).send(queue, session.createTextMessage("test"),
+ DeliveryMode.NON_PERSISTENT, 0, 0L, false, false, true);
+
+ // Test the connection with a valid consumer
+ // This may not work as the session may be closed before the queue or consumer creation can occur.
+ // The correct JMSexception with linked error will only occur when the close method is recevied whilst in
+ // the failover safe block
+ session.createConsumer(session.createQueue("example.RequestQueue")).close();
+
+ //Connection should now be closed and will throw the exception caused by the above send
+ conn.close();
+
+ fail("Close is not expected to succeed.");
+ }
+ catch (IllegalStateException e)
+ {
+ _logger.info("QPID-2345: Session became closed and we got that error rather than the authentication error.");
+ }
+ catch (JMSException e)
+ {
+ check403Exception(e.getLinkedException());
+ }
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/security/firewall/FirewallConfigTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/security/firewall/FirewallConfigTest.java
new file mode 100644
index 0000000000..f40e95885d
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/security/firewall/FirewallConfigTest.java
@@ -0,0 +1,298 @@
+/*
+ * 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.security.firewall;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+
+import javax.jms.Connection;
+import javax.jms.JMSException;
+
+import org.apache.qpid.client.AMQConnectionURL;
+import org.apache.qpid.test.utils.QpidBrokerTestCase;
+
+public class FirewallConfigTest extends QpidBrokerTestCase
+{
+ private File _tmpConfig, _tmpVirtualhosts;
+
+ @Override
+ protected void setUp() throws Exception
+ {
+ // do setup
+ final String QPID_HOME = System.getProperty("QPID_HOME");
+
+ if (QPID_HOME == null)
+ {
+ fail("QPID_HOME not set");
+ }
+
+ // Setup initial config file.
+ _configFile = new File(QPID_HOME, "etc/config-systests-firewall.xml");
+
+ // Setup temporary config file
+ _tmpConfig = File.createTempFile("config-systests-firewall", ".xml");
+ setSystemProperty("QPID_FIREWALL_CONFIG_SETTINGS", _tmpConfig.getAbsolutePath());
+ _tmpConfig.deleteOnExit();
+
+ // Setup temporary virtualhosts file
+ _tmpVirtualhosts = File.createTempFile("virtualhosts-systests-firewall", ".xml");
+ setSystemProperty("QPID_FIREWALL_VIRTUALHOSTS_SETTINGS", _tmpVirtualhosts.getAbsolutePath());
+ _tmpVirtualhosts.deleteOnExit();
+ }
+
+ private void writeFirewallFile(boolean allow, boolean inVhost) throws IOException
+ {
+ FileWriter out = new FileWriter(inVhost ? _tmpVirtualhosts : _tmpConfig);
+ String ipAddr = "127.0.0.1"; // FIXME: get this from InetAddress.getLocalHost().getAddress() ?
+ if (inVhost)
+ {
+ out.write("<virtualhosts><virtualhost><test>");
+ }
+ else
+ {
+ out.write("<broker>");
+ }
+ out.write("<security><firewall>");
+ out.write("<rule access=\""+((allow) ? "allow" : "deny")+"\" network=\""+ipAddr +"\"/>");
+ out.write("</firewall></security>");
+ if (inVhost)
+ {
+ out.write("</test></virtualhost></virtualhosts>");
+ }
+ else
+ {
+ out.write("</broker>");
+ }
+ out.close();
+ }
+
+ public void testVhostAllowBrokerDeny() throws Exception
+ {
+ if (_broker.equals(VM))
+ {
+ //No point running this test with an InVM broker as the
+ //firewall plugin only functions for TCP connections.
+ return;
+ }
+
+ _configFile = new File(System.getProperty("QPID_HOME"), "etc/config-systests-firewall-2.xml");
+
+ super.setUp();
+
+ Connection conn = null;
+ try
+ {
+ //Try to get a connection to the 'test2' vhost
+ //This is expected to succeed as it is allowed at the vhost level
+ conn = getConnection(new AMQConnectionURL("amqp://guest:guest@clientid/test2?brokerlist='" + getBroker() + "'"));
+ }
+ catch (JMSException e)
+ {
+ e.getLinkedException().printStackTrace();
+ fail("The connection was expected to succeed: " + e.getMessage());
+ }
+
+ conn = null;
+ try
+ {
+ //Try to get a connection to the 'test' vhost
+ //This is expected to fail as it is denied at the broker level
+ conn = getConnection();
+ fail("We expected the connection to fail");
+ }
+ catch (JMSException e)
+ {
+ //ignore
+ }
+ }
+
+ public void testVhostDenyBrokerAllow() throws Exception
+ {
+ if (_broker.equals(VM))
+ {
+ //No point running this test with an InVM broker as the
+ //firewall plugin only functions for TCP connections.
+ return;
+ }
+
+ _configFile = new File(System.getProperty("QPID_HOME"), "etc/config-systests-firewall-3.xml");
+
+ super.setUp();
+
+ Connection conn = null;
+ try
+ {
+ //Try to get a connection to the 'test2' vhost
+ //This is expected to fail as it is denied at the vhost level
+ conn = getConnection(new AMQConnectionURL("amqp://guest:guest@clientid/test2?brokerlist='" + getBroker() + "'"));
+ fail("The connection was expected to fail");
+ }
+ catch (JMSException e)
+ {
+ //ignore
+ }
+
+ conn = null;
+ try
+ {
+ //Try to get a connection to the 'test' vhost
+ //This is expected to succeed as it is allowed at the broker level
+ conn = getConnection();
+ }
+ catch (JMSException e)
+ {
+ e.getLinkedException().printStackTrace();
+ fail("The connection was expected to succeed: " + e.getMessage());
+ }
+ }
+
+ public void testDenyOnRestart() throws Exception
+ {
+ testDeny(false, new Runnable() {
+
+ public void run()
+ {
+ try
+ {
+ restartBroker();
+ } catch (Exception e)
+ {
+ fail(e.getMessage());
+ }
+ }
+ });
+ }
+
+ public void testDenyOnRestartInVhost() throws Exception
+ {
+ testDeny(true, new Runnable() {
+
+ public void run()
+ {
+ try
+ {
+ restartBroker();
+ } catch (Exception e)
+ {
+ fail(e.getMessage());
+ }
+ }
+ });
+ }
+
+ public void testAllowOnReloadInVhost() throws Exception
+ {
+ testFirewall(false, true, new Runnable() {
+
+ public void run()
+ {
+ try
+ {
+ reloadBrokerSecurityConfig();
+ } catch (Exception e)
+ {
+ fail(e.getMessage());
+ }
+ }
+ });
+ }
+
+ public void testDenyOnReload() throws Exception
+ {
+ testDeny(false, new Runnable() {
+
+ public void run()
+ {
+ try
+ {
+ reloadBrokerSecurityConfig();
+ } catch (Exception e)
+ {
+ fail(e.getMessage());
+ }
+ }
+ }
+ );
+ }
+
+ public void testDenyOnReloadInVhost() throws Exception
+ {
+ testDeny(true, new Runnable() {
+
+ public void run()
+ {
+ try
+ {
+ reloadBrokerSecurityConfig();
+ } catch (Exception e)
+ {
+ fail(e.getMessage());
+ }
+ }
+ }
+ );
+
+ }
+
+ private void testDeny(boolean inVhost, Runnable restartOrReload) throws Exception
+ {
+ testFirewall(true, inVhost, restartOrReload);
+ }
+
+ /*
+ * Check we can get a connection
+ */
+ private boolean checkConnection() throws Exception
+ {
+ Exception exception = null;
+ Connection conn = null;
+ try
+ {
+ conn = getConnection();
+ }
+ catch (JMSException e)
+ {
+ exception = e;
+ }
+
+ return conn != null;
+ }
+
+ private void testFirewall(boolean initial, boolean inVhost, Runnable restartOrReload) throws Exception
+ {
+ if (_broker.equals(VM))
+ {
+ // No point running this test in a vm broker
+ return;
+ }
+
+ writeFirewallFile(initial, inVhost);
+ setConfigurationProperty("management.enabled", String.valueOf(true));
+ super.setUp();
+
+ assertEquals("Initial connection check failed", initial, checkConnection());
+
+ // Reload changed firewall file after restart or reload
+ writeFirewallFile(!initial, inVhost);
+ restartOrReload.run();
+
+ assertEquals("Second connection check failed", !initial, checkConnection());
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/store/PersistentStoreTest.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/store/PersistentStoreTest.java
new file mode 100644
index 0000000000..bf9d0e0f7b
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/store/PersistentStoreTest.java
@@ -0,0 +1,193 @@
+/*
+ *
+ * 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.store;
+
+import org.apache.qpid.client.AMQSession;
+import org.apache.qpid.test.utils.QpidBrokerTestCase;
+
+import javax.jms.Connection;
+import javax.jms.Destination;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Queue;
+import javax.jms.Session;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class PersistentStoreTest extends QpidBrokerTestCase
+{
+
+ private static final int NUM_MESSAGES = 100;
+ private Connection _con;
+ private Session _session;
+ private Queue _destination;
+ private MessageConsumer _consumer;
+
+ public void setUp() throws Exception, JMSException
+ {
+ super.setUp();
+ _con = getConnection();
+ _con.start();
+ _session = _con.createSession(true, Session.SESSION_TRANSACTED);
+ _destination = _session.createQueue(getTestQueueName());
+ _consumer = _session.createConsumer(_destination);
+ _consumer.close();
+
+ sendMessage(_session, _destination, NUM_MESSAGES);
+ _session.commit();
+ }
+
+ /** Checks that a new consumer on a new connection can get NUM_MESSAGES from _destination */
+ private void checkMessages() throws Exception, JMSException
+ {
+ _con = getConnection();
+ _session = _con.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ _con.start();
+ _consumer = _session.createConsumer(_destination);
+ for (int i = 1; i <= NUM_MESSAGES; i++)
+ {
+ Message msg = _consumer.receive(RECEIVE_TIMEOUT);
+ assertNotNull("Message " + i + " not received", msg);
+ assertEquals("Did not receive the expected message", i, msg.getIntProperty(INDEX));
+ }
+
+ Message msg = _consumer.receive(100);
+ if(msg != null)
+ {
+ fail("No more messages should be received, but received additional message with index: " + msg.getIntProperty(INDEX));
+ }
+ }
+
+// /**
+// * starts the server, sends 100 messages, restarts the server and gets 100 messages back
+// * the test formerly referred to as BDB-Qpid-1
+// * @throws Exception
+// */
+// public void testStartStop() throws Exception
+// {
+// restartBroker(); -- Not Currently a gracefull restart so not BDB-Qpid-1
+// checkMessages();
+// }
+
+ /**
+ * starts the server, sends 100 messages, nukes then starts the server and gets 100 messages back
+ * the test formerly referred to as BDB-Qpid-2
+ *
+ * @throws Exception
+ */
+ public void testForcibleStartStop() throws Exception
+ {
+ restartBroker();
+ checkMessages();
+ }
+
+// /**
+// * starts the server, sends 100 committed messages, 5 uncommited ones,
+// * restarts the server and gets 100 messages back
+// * the test formerly referred to as BDB-Qpid-5
+// * @throws Exception
+// */
+// public void testStartStopMidTransaction() throws Exception
+// {
+// sendMessage(_session, _destination, 5);
+// restartBroker(); -- Not Currently a gracefull restart so not BDB-Qpid-1
+// checkMessages();
+// }
+
+ /**
+ * starts the server, sends 100 committed messages, 5 uncommited ones,
+ * nukes and starts the server and gets 100 messages back
+ * the test formerly referred to as BDB-Qpid-6
+ *
+ * @throws Exception
+ */
+ public void testForcibleStartStopMidTransaction() throws Exception
+ {
+ sendMessage(_session, _destination, 5);
+ //sync to ensure that the above messages have reached the broker
+ ((AMQSession) _session).sync();
+ restartBroker();
+ checkMessages();
+ }
+
+ /**
+ * starts the server, sends 100 committed messages, 5 uncommited ones,
+ * restarts the client and gets 100 messages back.
+ * the test formerly referred to as BDB-Qpid-7
+ *
+ * FIXME: is this a PersistentStoreTest? Seems more like a transaction test to me.. aidan
+ *
+ * @throws Exception
+ */
+ public void testClientDeathMidTransaction() throws Exception
+ {
+ sendMessage(_session, _destination, 5);
+ _con.close();
+ checkMessages();
+ }
+
+// /**
+// * starts the server, sends 50 committed messages, copies $QPID_WORK to a new location,
+// * sends 10 messages, stops the server, nukes the store, restores the copy, starts the server
+// * checks that we get the first 50 back.
+// */
+// public void testHotBackup()
+// {
+// -- removing as this will leave 100msgs on a queue
+// }
+
+ /**
+ * This test requires that we can send messages without commiting.
+ * QTC always commits the messages sent via sendMessages.
+ *
+ * @param session the session to use for sending
+ * @param destination where to send them to
+ * @param count no. of messages to send
+ *
+ * @return the sent messges
+ *
+ * @throws Exception
+ */
+ @Override
+ public List<Message> sendMessage(Session session, Destination destination,
+ int count) throws Exception
+ {
+ List<Message> messages = new ArrayList<Message>(count);
+
+ MessageProducer producer = session.createProducer(destination);
+
+ for (int i = 1;i <= (count); i++)
+ {
+ Message next = createNextMessage(session, i);
+
+ producer.send(next);
+
+ messages.add(next);
+ }
+
+ return messages;
+ }
+
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/store/SlowMessageStore.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/store/SlowMessageStore.java
new file mode 100644
index 0000000000..a5c38e7e33
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/store/SlowMessageStore.java
@@ -0,0 +1,321 @@
+/*
+ *
+ * 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.store;
+
+import org.apache.commons.configuration.Configuration;
+import org.apache.log4j.Logger;
+import org.apache.qpid.AMQException;
+import org.apache.qpid.AMQStoreException;
+import org.apache.qpid.framing.AMQShortString;
+import org.apache.qpid.framing.FieldTable;
+import org.apache.qpid.server.exchange.Exchange;
+import org.apache.qpid.server.queue.AMQQueue;
+import org.apache.qpid.server.message.ServerMessage;
+import org.apache.qpid.server.logging.LogSubject;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.nio.ByteBuffer;
+
+public class SlowMessageStore implements MessageStore
+{
+ private static final Logger _logger = Logger.getLogger(SlowMessageStore.class);
+ private static final String DELAYS = "delays";
+ private HashMap<String, Long> _preDelays = new HashMap<String, Long>();
+ private HashMap<String, Long> _postDelays = new HashMap<String, Long>();
+ private long _defaultDelay = 0L;
+ private MessageStore _realStore = new MemoryMessageStore();
+ private static final String PRE = "pre";
+ private static final String POST = "post";
+ private String DEFAULT_DELAY = "default";
+
+ // ***** MessageStore Interface.
+
+ public void configureConfigStore(String name,
+ ConfigurationRecoveryHandler recoveryHandler,
+ Configuration config,
+ LogSubject logSubject) throws Exception
+ {
+ //To change body of implemented methods use File | Settings | File Templates.
+
+ _logger.info("Starting SlowMessageStore on Virtualhost:" + name);
+ Configuration delays = config.subset(DELAYS);
+
+ configureDelays(delays);
+
+ String messageStoreClass = config.getString("realStore");
+
+ if (delays.containsKey(DEFAULT_DELAY))
+ {
+ _defaultDelay = delays.getLong(DEFAULT_DELAY);
+ }
+
+ if (messageStoreClass != null)
+ {
+ Class clazz = Class.forName(messageStoreClass);
+
+ Object o = clazz.newInstance();
+
+ if (!(o instanceof MessageStore))
+ {
+ throw new ClassCastException("Message store class must implement " + MessageStore.class + ". Class " + clazz +
+ " does not.");
+ }
+ _realStore = (MessageStore) o;
+ _realStore.configureConfigStore(name, recoveryHandler, config, logSubject);
+ }
+ else
+ {
+ _realStore.configureConfigStore(name, recoveryHandler, config, logSubject);
+ }
+ }
+
+ private void configureDelays(Configuration config)
+ {
+ Iterator delays = config.getKeys();
+
+ while (delays.hasNext())
+ {
+ String key = (String) delays.next();
+ if (key.endsWith(PRE))
+ {
+ _preDelays.put(key.substring(0, key.length() - PRE.length() - 1), config.getLong(key));
+ }
+ else if (key.endsWith(POST))
+ {
+ _postDelays.put(key.substring(0, key.length() - POST.length() - 1), config.getLong(key));
+ }
+ }
+ }
+
+ private void doPostDelay(String method)
+ {
+ long delay = lookupDelay(_postDelays, method);
+ doDelay(delay);
+ }
+
+ private void doPreDelay(String method)
+ {
+ long delay = lookupDelay(_preDelays, method);
+ doDelay(delay);
+ }
+
+ private long lookupDelay(HashMap<String, Long> delays, String method)
+ {
+ Long delay = delays.get(method);
+ return (delay == null) ? _defaultDelay : delay;
+ }
+
+ private void doDelay(long delay)
+ {
+ if (delay > 0)
+ {
+ long start = System.nanoTime();
+ try
+ {
+
+ Thread.sleep(delay);
+ }
+ catch (InterruptedException e)
+ {
+ _logger.warn("Interrupted : " + e);
+ }
+
+ long slept = (System.nanoTime() - start) / 1000000;
+
+ if (slept >= delay)
+ {
+ _logger.info("Done sleep for:" + slept+":"+delay);
+ }
+ else
+ {
+ _logger.info("Only sleep for:" + slept + " re-sleeping");
+ doDelay(delay - slept);
+ }
+ }
+ }
+
+
+ public void configureMessageStore(String name,
+ MessageStoreRecoveryHandler recoveryHandler,
+ Configuration config,
+ LogSubject logSubject) throws Exception
+ {
+ _realStore.configureMessageStore(name, recoveryHandler, config, logSubject);
+ }
+
+ public void close() throws Exception
+ {
+ doPreDelay("close");
+ _realStore.close();
+ doPostDelay("close");
+ }
+
+ public <M extends StorableMessageMetaData> StoredMessage<M> addMessage(M metaData)
+ {
+ return _realStore.addMessage(metaData);
+ }
+
+
+ public void createExchange(Exchange exchange) throws AMQStoreException
+ {
+ doPreDelay("createExchange");
+ _realStore.createExchange(exchange);
+ doPostDelay("createExchange");
+ }
+
+ public void removeExchange(Exchange exchange) throws AMQStoreException
+ {
+ doPreDelay("removeExchange");
+ _realStore.removeExchange(exchange);
+ doPostDelay("removeExchange");
+ }
+
+ public void bindQueue(Exchange exchange, AMQShortString routingKey, AMQQueue queue, FieldTable args) throws AMQStoreException
+ {
+ doPreDelay("bindQueue");
+ _realStore.bindQueue(exchange, routingKey, queue, args);
+ doPostDelay("bindQueue");
+ }
+
+ public void unbindQueue(Exchange exchange, AMQShortString routingKey, AMQQueue queue, FieldTable args) throws AMQStoreException
+ {
+ doPreDelay("unbindQueue");
+ _realStore.unbindQueue(exchange, routingKey, queue, args);
+ doPostDelay("unbindQueue");
+ }
+
+ public void createQueue(AMQQueue queue) throws AMQStoreException
+ {
+ createQueue(queue, null);
+ }
+
+ public void createQueue(AMQQueue queue, FieldTable arguments) throws AMQStoreException
+ {
+ doPreDelay("createQueue");
+ _realStore.createQueue(queue, arguments);
+ doPostDelay("createQueue");
+ }
+
+ public void removeQueue(AMQQueue queue) throws AMQStoreException
+ {
+ doPreDelay("removeQueue");
+ _realStore.removeQueue(queue);
+ doPostDelay("removeQueue");
+ }
+
+ public void configureTransactionLog(String name,
+ TransactionLogRecoveryHandler recoveryHandler,
+ Configuration storeConfiguration, LogSubject logSubject)
+ throws Exception
+ {
+ _realStore.configureTransactionLog(name, recoveryHandler, storeConfiguration, logSubject);
+ }
+
+ public Transaction newTransaction()
+ {
+ doPreDelay("beginTran");
+ Transaction txn = new SlowTransaction(_realStore.newTransaction());
+ doPostDelay("beginTran");
+ return txn;
+ }
+
+
+ public boolean isPersistent()
+ {
+ return _realStore.isPersistent();
+ }
+
+ public void storeMessageHeader(Long messageNumber, ServerMessage message)
+ {
+ //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public void storeContent(Long messageNumber, long offset, ByteBuffer body)
+ {
+ //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public ServerMessage getMessage(Long messageNumber)
+ {
+ return null; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ private class SlowTransaction implements Transaction
+ {
+ private final Transaction _underlying;
+
+ private SlowTransaction(Transaction underlying)
+ {
+ _underlying = underlying;
+ }
+
+ public void enqueueMessage(TransactionLogResource queue, Long messageId)
+ throws AMQStoreException
+ {
+ doPreDelay("enqueueMessage");
+ _underlying.enqueueMessage(queue, messageId);
+ doPostDelay("enqueueMessage");
+ }
+
+ public void dequeueMessage(TransactionLogResource queue, Long messageId)
+ throws AMQStoreException
+ {
+ doPreDelay("dequeueMessage");
+ _underlying.dequeueMessage(queue, messageId);
+ doPostDelay("dequeueMessage");
+ }
+
+ public void commitTran()
+ throws AMQStoreException
+ {
+ doPreDelay("commitTran");
+ _underlying.commitTran();
+ doPostDelay("commitTran");
+ }
+
+ public StoreFuture commitTranAsync()
+ throws AMQStoreException
+ {
+ doPreDelay("commitTran");
+ StoreFuture future = _underlying.commitTranAsync();
+ doPostDelay("commitTran");
+ return future;
+ }
+
+ public void abortTran()
+ throws AMQStoreException
+ {
+ doPreDelay("abortTran");
+ _underlying.abortTran();
+ doPostDelay("abortTran");
+ }
+ }
+
+ public void updateQueue(AMQQueue queue) throws AMQStoreException
+ {
+ doPreDelay("updateQueue");
+ _realStore.updateQueue(queue);
+ doPostDelay("updateQueue");
+ }
+
+
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/util/AveragedRun.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/util/AveragedRun.java
new file mode 100644
index 0000000000..1d17985ab5
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/util/AveragedRun.java
@@ -0,0 +1,66 @@
+/*
+ *
+ * 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.util;
+
+import org.apache.qpid.server.util.TimedRun;
+
+import java.util.concurrent.Callable;
+import java.util.Collection;
+
+public class AveragedRun implements Callable<RunStats>
+{
+ private final RunStats stats = new RunStats();
+ private final TimedRun test;
+ private final int iterations;
+
+ public AveragedRun(TimedRun test, int iterations)
+ {
+ this.test = test;
+ this.iterations = iterations;
+ }
+
+ public RunStats call() throws Exception
+ {
+ for (int i = 0; i < iterations; i++)
+ {
+ stats.record(test.call());
+ }
+ return stats;
+ }
+
+ public void run() throws Exception
+ {
+ System.out.println(test + ": " + call());
+ }
+
+ public String toString()
+ {
+ return test.toString();
+ }
+
+ static void run(Collection<AveragedRun> tests) throws Exception
+ {
+ for(AveragedRun test : tests)
+ {
+ test.run();
+ }
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/util/RunStats.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/util/RunStats.java
new file mode 100644
index 0000000000..ec67fc68b3
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/util/RunStats.java
@@ -0,0 +1,57 @@
+/*
+ *
+ * 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.util;
+
+public class RunStats
+{
+ private long min = Long.MAX_VALUE;
+ private long max;
+ private long total;
+ private int count;
+
+ public void record(long time)
+ {
+ max = Math.max(time, max);
+ min = Math.min(time, min);
+ total += time;
+ count++;
+ }
+
+ public long getMin()
+ {
+ return min;
+ }
+
+ public long getMax()
+ {
+ return max;
+ }
+
+ public long getAverage()
+ {
+ return total / count;
+ }
+
+ public String toString()
+ {
+ return "avg=" + getAverage() + ", min=" + min + ", max=" + max;
+ }
+}
diff --git a/qpid/java/systests/src/main/java/org/apache/qpid/server/util/TimedRun.java b/qpid/java/systests/src/main/java/org/apache/qpid/server/util/TimedRun.java
new file mode 100644
index 0000000000..1291380311
--- /dev/null
+++ b/qpid/java/systests/src/main/java/org/apache/qpid/server/util/TimedRun.java
@@ -0,0 +1,52 @@
+/*
+ *
+ * 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.util;
+
+import java.util.concurrent.Callable;
+
+public abstract class TimedRun implements Callable<Long>
+{
+ private final String description;
+
+ public TimedRun(String description)
+ {
+ this.description = description;
+ }
+
+ public Long call() throws Exception
+ {
+ setup();
+ long start = System.currentTimeMillis();
+ run();
+ long stop = System.currentTimeMillis();
+ teardown();
+ return stop - start;
+ }
+
+ public String toString()
+ {
+ return description;
+ }
+
+ protected void setup() throws Exception{}
+ protected void teardown() throws Exception{}
+ protected abstract void run() throws Exception;
+}