/* * 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 System; using System.Xml; using Apache.NMS.Util; using NUnit.Framework; namespace Apache.NMS.Test { /// /// Base class for test cases /// public abstract class NMSTest { protected TimeSpan receiveTimeout = TimeSpan.FromMilliseconds(15000); public static string ToHex(long value) { return String.Format("{0:x}", value); } #region Constructors and test support private NMSTestSupport testSupport; static NMSTest() { Apache.NMS.Tracer.Trace = new NMSTracer(); } protected NMSTest(NMSTestSupport testSupport) { this.testSupport = testSupport; this.testSupport.TestClassType = this.GetType(); } #endregion #region Set up and tear down [SetUp] public virtual void SetUp() { this.testSupport.SetUp(); } [TearDown] public virtual void TearDown() { this.testSupport.TearDown(); } #endregion #region Configuration file /// /// The configuration document. /// public XmlDocument ConfigurationDocument { get { return this.testSupport.ConfigurationDocument; } } /// /// Loads the configuration file. /// /// XmlDocument of the configuration file protected virtual XmlDocument LoadConfigFile() { return this.testSupport.LoadConfigFile(); } /// /// Loads the configuration file. /// /// Configuration file path /// XmlDocument of the configuration file protected virtual XmlDocument LoadConfigFile(string configFilePath) { return this.testSupport.LoadConfigFile(configFilePath); } /// /// Gets the path of the configuration filename. /// /// Path of the configuration filename protected virtual string GetConfigFilePath() { return this.testSupport.GetConfigFilePath(); } /// /// Gets the environment variable name for the configuration file path. /// /// Environment variable name protected virtual string GetConfigEnvVarName() { return this.testSupport.GetConfigEnvVarName(); } /// /// Gets the default name for the configuration filename. /// /// Default name of the configuration filename protected virtual string GetDefaultConfigFileName() { return this.testSupport.GetDefaultConfigFileName(); } /// /// Gets the value of the "value" attribute of the specified node. /// /// Parent node /// Node name /// Default value /// protected virtual string GetNodeValueAttribute(XmlElement parentNode, string nodeName, string defaultVaue) { return this.testSupport.GetNodeValueAttribute(parentNode, nodeName, defaultVaue); } #endregion #region URI node /// /// Gets the URI node for the default configuration. /// /// URI node for the default configuration name public virtual XmlElement GetURINode() { return this.testSupport.GetURINode(); } /// /// Gets the URI node for the default configuration. /// /// Name of the default configuration node /// /// URI node for the default configuration name public virtual XmlElement GetURINode(string nameTestURI) { return this.testSupport.GetURINode(nameTestURI); } /// /// Gets the name of the default connection configuration to be loaded. /// /// Default configuration name protected virtual string GetNameTestURI() { return this.testSupport.GetNameTestURI(); } #endregion #region Factory private NMSConnectionFactory nmsFactory; /// /// The connection factory interface property. /// public IConnectionFactory Factory { get { return this.testSupport.Factory; } } /// /// Create the NMS Factory that can create NMS Connections. /// /// Connection factory protected NMSConnectionFactory CreateNMSFactory() { return this.testSupport.CreateNMSFactory(); } /// /// Create the NMS Factory that can create NMS Connections. This /// function loads the connection settings from the configuration file. /// /// The named connection configuration. /// /// Connection factory protected NMSConnectionFactory CreateNMSFactory(string nameTestURI) { return this.testSupport.CreateNMSFactory(nameTestURI); } /// /// Get the parameters for the ConnectionFactory from the configuration /// file. /// /// Parent node of the factoryParams node. /// Object array of parameter objects to be passsed to provider /// factory object. Null if no parameters are specified in /// configuration file. protected object[] GetFactoryParams(XmlElement uriNode) { return this.testSupport.GetFactoryParams(uriNode); } #endregion #region Client id and connection /// /// Client id. /// public string ClientId { get { return this.testSupport.ClientId; } } /// /// Gets a new client id. /// /// Client id public virtual string GetTestClientId() { return this.testSupport.GetTestClientId(); } /// /// Create a new connection to the broker. /// /// New connection public virtual IConnection CreateConnection() { return this.testSupport.CreateConnection(); } /// /// Create a new connection to the broker. /// /// Client ID of the new connection. /// New connection public virtual IConnection CreateConnection(string newClientId) { return this.testSupport.CreateConnection(newClientId); } /// /// Create a new connection to the broker, and start it. /// /// Started connection public virtual IConnection CreateConnectionAndStart() { return this.testSupport.CreateConnectionAndStart(); } /// /// Create a new connection to the broker, and start it. /// /// Client ID of the new connection. /// Started connection public virtual IConnection CreateConnectionAndStart(string newClientId) { return this.testSupport.CreateConnectionAndStart(newClientId); } #endregion #region Destination /// /// Gets a clear destination. /// /// Session /// Destination type /// Configuration node name for the /// destination URI /// Destination public virtual IDestination GetClearDestination(ISession session, DestinationType type, string destinationRef) { return this.testSupport.GetClearDestination(session, type, destinationRef); } /// /// Gets a clear destination. This will try to delete an existing /// destination and re-create it. /// /// Session /// Destination URI /// Clear destination public virtual IDestination GetClearDestination(ISession session, string destinationURI) { return this.testSupport.GetClearDestination(session, destinationURI); } /// /// Gets an existing destination. Don't clear its contents. /// /// Session /// Destination type /// Configuration node name for the /// destination URI /// Destination public virtual IDestination GetDestination(ISession session, DestinationType type, string destinationRef) { return this.testSupport.GetDestination(session, type, destinationRef); } /// /// Gets a destination URI. /// /// Destination type /// Configuration node name for the /// destination URI /// Destination URI public virtual string GetDestinationURI(DestinationType type, string destinationRef) { return this.testSupport.GetDestinationURI(type, destinationRef); } #endregion #region Durable consumer /// /// Register a durable consumer /// /// Connection ID of the consumer. /// Destination name to register. Supports /// embedded prefix names. /// Name of the durable consumer. /// Selector parameters for consumer. /// protected void RegisterDurableConsumer(string connectionID, string destination, string consumerID, string selector, bool noLocal) { using(IConnection connection = CreateConnection(connectionID)) { connection.Start(); using(ISession session = connection.CreateSession( AcknowledgementMode.DupsOkAcknowledge)) { ITopic destinationTopic = (ITopic)SessionUtil.GetDestination(session, destination); Assert.IsNotNull(destinationTopic, "Could not get destination topic."); using(IMessageConsumer consumer = session.CreateDurableConsumer(destinationTopic, consumerID, selector, noLocal)) { Assert.IsNotNull(consumer, "Could not create durable consumer."); } } } } /// /// Unregister a durable consumer for the given connection ID. /// /// Connection ID of the consumer. /// Name of the durable consumer. protected void UnregisterDurableConsumer(string connectionID, string consumerID) { using(IConnection connection = CreateConnection(connectionID)) { connection.Start(); using(ISession session = connection.CreateSession(AcknowledgementMode.DupsOkAcknowledge)) { session.DeleteDurableConsumer(consumerID); } } } #endregion #region Send messages /// /// Sends a specified number of text messages to the designated /// destination. /// /// Destination. /// Delivery mode. /// Number of messages to be sent. public void SendMessages(IDestination destination, MsgDeliveryMode deliveryMode, int count) { IConnection connection = CreateConnection(); connection.Start(); SendMessages(connection, destination, deliveryMode, count); connection.Close(); } /// /// Sends a specified number of text messages to the designated /// destination. /// /// Connection. /// Destination. /// Delivery mode. /// Number of messages to be sent. public void SendMessages(IConnection connection, IDestination destination, MsgDeliveryMode deliveryMode, int count) { ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge); SendMessages(session, destination, deliveryMode, count); session.Close(); } /// /// Sends a specified number of text messages to the designated /// destination. /// /// Session. /// Destination. /// Delivery mode. /// Number of messages to be sent. public void SendMessages(ISession session, IDestination destination, MsgDeliveryMode deliveryMode, int count) { IMessageProducer producer = session.CreateProducer(destination); producer.DeliveryMode = deliveryMode; for(int i = 0; i < count; i++) { producer.Send(session.CreateTextMessage("" + i)); } producer.Close(); } #endregion #region Check messages protected void AssertTextMessagesEqual(IMessage[] firstSet, IMessage[] secondSet) { AssertTextMessagesEqual(firstSet, secondSet, ""); } protected void AssertTextMessagesEqual(IMessage[] firstSet, IMessage[] secondSet, string messsage) { Assert.AreEqual(firstSet.Length, secondSet.Length, "Message count does not match: " + messsage); for(int i = 0; i < secondSet.Length; i++) { ITextMessage m1 = firstSet[i] as ITextMessage; ITextMessage m2 = secondSet[i] as ITextMessage; AssertTextMessageEqual(m1, m2, "Message " + (i + 1) + " did not match : "); } } protected void AssertEquals(ITextMessage m1, ITextMessage m2) { AssertEquals(m1, m2, ""); } protected void AssertTextMessageEqual(ITextMessage m1, ITextMessage m2, string message) { Assert.IsFalse(m1 == null ^ m2 == null, message + ": expected {" + m1 + "}, but was {" + m2 + "}"); if(m1 == null) { return; } Assert.AreEqual(m1.Text, m2.Text, message); } protected void AssertEquals(IMessage m1, IMessage m2) { AssertEquals(m1, m2, ""); } protected void AssertEquals(IMessage m1, IMessage m2, string message) { Assert.IsFalse(m1 == null ^ m2 == null, message + ": expected {" + m1 + "}, but was {" + m2 + "}"); if(m1 == null) { return; } Assert.IsTrue(m1.GetType() == m2.GetType(), message + ": expected {" + m1 + "}, but was {" + m2 + "}"); if(m1 is ITextMessage) { AssertTextMessageEqual((ITextMessage) m1, (ITextMessage) m2, message); } else { Assert.AreEqual(m1, m2, message); } } #endregion } }