/*
*
* 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.
*
*/
using log4net;
using Apache.Qpid.Integration.Tests.framework.localcircuit;//.LocalCircuitImpl;
//using Apache.Qpid.Integration.Tests.framework.localcircuit.LocalPublisherImpl;
//using Apache.Qpid.Integration.Tests.framework.localcircuit.LocalReceiverImpl;
//using Apache.Qpid.Integration.Tests.framework.sequencers.CircuitFactory;
//using org.apache.qpid.util.ConversationFactory;
//using uk.co.thebadgerset.junit.extensions.util.ParsedProperties;
//using javax.jms.*;
using System.Collections.Generic;//.IList;
//using java.util.Properties;
//using java.util.concurrent.atomic.AtomicLong;
namespace Apache.Qpid.Integration.Tests.framework
{
///
/// LocalCircuitFactory is a circuit factory that creates test circuits with publishing and receiving ends rooted
/// on the same JVM. The ends of the circuit are presented as and interfaces, which
/// in turn provide methods to apply assertions to the circuit. The creation of the circuit ends, and the presentation
/// of the ends as publisher/receiver interfaces, are designed to be overriden, so that circuits and assertions that
/// use messaging features not available in JMS can be written. This provides an extension point for writing tests
/// against proprietary features of JMS implementations.
///
///
CRC Card
///
Responsibilities
Collaborations
///
Provide a standard test procedure over a test circuit.
///
Construct test circuits appropriate to a tests context.
///
///
public class LocalCircuitFactory : CircuitFactory
{
/// Used for debugging.
private static ILog log = LogManager.GetLogger(typeof(LocalCircuitFactory));
/// Used to create unique destination names for each test.
protected static AtomicLong uniqueDestsId = new AtomicLong();
///
/// Holds a test coordinating conversation with the test clients. This should consist of assigning the test roles,
/// begining the test and gathering the test reports from the participants.
///
/// The test circuit.
/// The list of assertions to apply to the test circuit.
/// The test case definition.
public void sequenceTest(Circuit testCircuit, IList assertions, Properties testProperties)
{
FrameworkBaseCase.assertNoFailures(testCircuit.test(1, assertions));
}
///
/// Creates a test circuit for the test, configered by the test parameters specified.
///
/// The test parameters.
///
/// A test circuit.
public Circuit createCircuit(TestModel testProperties)
{
Circuit result;
// Create a standard publisher/receivers test client pair on a shared connection, individual sessions.
try
{
// Get a unique offset to append to destination names to make them unique to the connection.
long uniqueId = uniqueDestsId.incrementAndGet();
// Set up the connection.
Connection connection = TestUtils.createConnection(testProperties);
// Add the connection exception listener to assert on exception conditions with.
// ExceptionMonitor exceptionMonitor = new ExceptionMonitor();
// connection.setExceptionListener(exceptionMonitor);
// Set up the publisher.
CircuitEndBase publisherEnd = createPublisherCircuitEnd(connection, testProps, uniqueId);
// Set up the receiver.
CircuitEndBase receiverEnd = createReceiverCircuitEnd(connection, testProps, uniqueId);
// Start listening for incoming messages.
connection.start();
// Namespace everything up.
LocalPublisherImpl publisher = createPublisherFromCircuitEnd(publisherEnd);
LocalReceiverImpl receiver = createReceiverFromCircuitEnd(receiverEnd);
result = new LocalCircuitImpl(testProperties, publisher, receiver, connection, publisher.getExceptionMonitor());
}
catch (JMSException e)
{
throw new RuntimeException("Could not create publisher/receivers pair due to a JMSException.", e);
}
return result;
}
///
/// Creates a local from a . Sub-classes may override this to provide more
/// specialized receivers if necessary.
///
/// The receiving circuit end.
///
/// A .
protected LocalReceiverImpl createReceiverFromCircuitEnd(CircuitEndBase receiverEnd)
{
return new LocalReceiverImpl(receiverEnd);
}
///
/// Creates a local from a . Sub-classes may override this to provide more
/// specialized receivers if necessary.
///
/// The publishing circuit end.
///
/// A .
protected LocalPublisherImpl createPublisherFromCircuitEnd(CircuitEndBase publisherEnd)
{
return new LocalPublisherImpl(publisherEnd);
}
///
/// Builds a circuit end suitable for the publishing side of a test circuit, from standard test parameters.
///
/// The connection to build the circuit end on.
/// The test parameters to configure the circuit end construction.
/// A unique number to being numbering destinations from, to make this circuit unique.
///
/// A circuit end suitable for the publishing side of a test circuit.
///
/// Any underlying JMSExceptions are allowed to fall through and fail the creation.
public CircuitEndBase createPublisherCircuitEnd(Connection connection, TestModel testProps, long uniqueId)
throws JMSException
{
log.debug(
"public CircuitEndBase createPublisherCircuitEnd(Connection connection, TestModel testProps, long uniqueId = "
+ uniqueId + "): called");
// Check that the test properties do not contain AMQP/Qpid specific settings, and fail if they do.
if (testProps.getImmediate() || testProps.getMandatory())
{
throw new RuntimeException(
"Cannot create a pure JMS circuit as the test properties require AMQP specific options.");
}
Session session = connection.createSession(testProps.getPublisherTransacted(), testProps.getAckMode());
Destination destination =
testProps.getPubsub() ? session.createTopic(testProps.getSendDestinationNameRoot() + "_" + uniqueId)
: session.createQueue(testProps.getSendDestinationNameRoot() + "_" + uniqueId);
MessageProducer producer = testProps.getPublisherProducerBind() ? session.createProducer(destination) : null;
MessageConsumer consumer =
testProps.getPublisherConsumerBind()
? session.createConsumer(session.createQueue(testProps.getReceiveDestinationNameRoot() + "_" + uniqueId)) : null;
MessageMonitor messageMonitor = new MessageMonitor();
if (consumer != null)
{
consumer.setMessageListener(messageMonitor);
}
ExceptionMonitor exceptionMonitor = new ExceptionMonitor();
connection.setExceptionListener(exceptionMonitor);
if (!testProps.getPublisherConsumerActive() && (consumer != null))
{
consumer.close();
}
return new CircuitEndBase(producer, consumer, session, messageMonitor, exceptionMonitor);
}
///
/// Builds a circuit end suitable for the receiving side of a test circuit, from standard test parameters.
///
/// The connection to build the circuit end on.
/// The test parameters to configure the circuit end construction.
/// A unique number to being numbering destinations from, to make this circuit unique.
///
/// A circuit end suitable for the receiving side of a test circuit.
///
/// Any underlying JMSExceptions are allowed to fall through and fail the creation.
public CircuitEndBase createReceiverCircuitEnd(Connection connection, TestModel testProps, long uniqueId)
throws JMSException
{
log.debug(
"public CircuitEndBase createReceiverCircuitEnd(Connection connection, TestModel testProps, long uniqueId = "
+ uniqueId + "): called");
// Check that the test properties do not contain AMQP/Qpid specific settings, and fail if they do.
if (testProps.getImmediate() || testProps.getMandatory())
{
throw new RuntimeException(
"Cannot create a pure JMS circuit as the test properties require AMQP specific options.");
}
Session session = connection.createSession(testProps.getPublisherTransacted(), testProps.getAckMode());
MessageProducer producer =
testProps.getReceiverProducerBind()
? session.createProducer(session.createQueue(testProps.getReceiveDestinationNameRoot() + "_" + uniqueId)) : null;
Destination destination =
testProps.getPubsub() ? session.createTopic(testProps.getSendDestinationNameRoot() + "_" + uniqueId)
: session.createQueue(testProps.getSendDestinationNameRoot() + "_" + uniqueId);
MessageConsumer consumer =
testProps.getReceiverConsumerBind()
? ((testProps.getDurableSubscription() && testProps.getPubsub())
? session.createDurableSubscriber((Topic) destination, "testsub") : session.createConsumer(destination))
: null;
MessageMonitor messageMonitor = new MessageMonitor();
if (consumer != null)
{
consumer.setMessageListener(messageMonitor);
}
if (!testProps.getReceiverConsumerActive() && (consumer != null))
{
consumer.close();
}
return new CircuitEndBase(producer, consumer, session, messageMonitor, null);
}
/*
///
/// Sets the sender test client to coordinate the test with.
///
/// The contact details of the sending client in the test.
public void setSender(TestClientDetails sender)
{
throw new RuntimeException("Not implemented.");
}
///
/// Sets the receiving test client to coordinate the test with.
///
/// The contact details of the sending client in the test.
public void setReceiver(TestClientDetails receiver)
{
throw new RuntimeException("Not implemented.");
}
///
/// Supplies the sending test client.
///
/// The sending test client.
public TestClientDetails getSender()
{
throw new RuntimeException("Not implemented.");
}
///
/// Supplies the receiving test client.
///
/// The receiving test client.
public IList getReceivers()
{
throw new RuntimeException("Not implemented.");
}
*/
/*
///
/// Accepts the conversation factory over which to hold the test coordinating conversation.
///
/// The conversation factory to coordinate the test over.
public void setConversationFactory(ConversationFactory conversationFactory)
{
throw new RuntimeException("Not implemented.");
}
*/
}
}