You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2018/08/27 21:15:42 UTC
[05/15] activemq-nms-amqp git commit: AMQNET-575: NMS AMQP Client
Rework Add an NMS API implementation that wraps the AMQPnetLite .NET API.
http://git-wip-us.apache.org/repos/asf/activemq-nms-amqp/blob/432c9613/src/test/csharp/Test/Attribute/ConsumerSetup.cs
----------------------------------------------------------------------
diff --git a/src/test/csharp/Test/Attribute/ConsumerSetup.cs b/src/test/csharp/Test/Attribute/ConsumerSetup.cs
new file mode 100644
index 0000000..bcb6b12
--- /dev/null
+++ b/src/test/csharp/Test/Attribute/ConsumerSetup.cs
@@ -0,0 +1,81 @@
+/*
+ * 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.Text;
+using System.Collections.Specialized;
+using NUnit.Framework;
+using NUnit.Framework.Interfaces;
+using Apache.NMS;
+using Apache.NMS.AMQP.Test.Util;
+using System.Diagnostics;
+using System.Collections.Generic;
+using System.Collections;
+using System.Reflection;
+using Apache.NMS.AMQP.Test.TestCase;
+
+namespace Apache.NMS.AMQP.Test.Attribute
+{
+
+ #region Consumer Setup Attribute Class
+
+ internal class ConsumerSetupAttribute : SessionParentDestinationDependentSetupAttribute
+ {
+
+ public MessageListener OnMessage { get; set; } = null;
+
+ protected override string InstanceName { get { return typeof(IMessageConsumer).Name; } }
+
+ public ConsumerSetupAttribute(string sessionId, string destinationId, params string[] consumerIds) : base(sessionId, destinationId, consumerIds) { }
+
+ public ConsumerSetupAttribute(string sessionId, string destinationId, string consumerId) : this(sessionId, destinationId, new string[] { consumerId }) { }
+
+ public ConsumerSetupAttribute(string sessionId = null, string destinationId = null) : this(sessionId, destinationId, new string[] { null }) { }
+
+ public override void BeforeTest(ITest test)
+ {
+ base.BeforeTest(test);
+ InitializeNUnitTest<IMessageConsumer, ISession>(test);
+ }
+
+ public override void Setup(BaseTestCase nmsTest)
+ {
+ base.Setup(nmsTest);
+ InitializeTest<IMessageConsumer, ISession>(nmsTest);
+ }
+
+ protected void InitializeConsumerProperties(IMessageConsumer consumer)
+ {
+ consumer.Listener += OnMessage;
+ }
+
+ protected override T CreateNMSInstance<T, P>(BaseTestCase test, P parent)
+ {
+ IMessageConsumer consumer = test.CreateConsumer((ISession)parent, this.GetDestination(test));
+ InitializeConsumerProperties(consumer);
+ return (T)consumer;
+ }
+
+ protected override void AddInstance<T>(BaseTestCase test, T instance, string id)
+ {
+ test.AddConsumer((IMessageConsumer)instance, id);
+ }
+
+ }
+
+ #endregion
+
+}
http://git-wip-us.apache.org/repos/asf/activemq-nms-amqp/blob/432c9613/src/test/csharp/Test/Attribute/DestinationSetup.cs
----------------------------------------------------------------------
diff --git a/src/test/csharp/Test/Attribute/DestinationSetup.cs b/src/test/csharp/Test/Attribute/DestinationSetup.cs
new file mode 100644
index 0000000..7bad1b9
--- /dev/null
+++ b/src/test/csharp/Test/Attribute/DestinationSetup.cs
@@ -0,0 +1,171 @@
+/*
+ * 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.Text;
+using System.Collections.Specialized;
+using NUnit.Framework;
+using NUnit.Framework.Interfaces;
+using Apache.NMS;
+using Apache.NMS.AMQP.Test.Util;
+using System.Diagnostics;
+using System.Collections.Generic;
+using System.Collections;
+using System.Reflection;
+using Apache.NMS.AMQP.Test.TestCase;
+
+namespace Apache.NMS.AMQP.Test.Attribute
+{
+
+ #region Destination Setup Attribute Class
+
+ abstract class DestinationSetupAttribute : SessionParentSetupAttribute
+ {
+ public string Name { get; set; }
+
+ protected DestinationSetupAttribute(string pId, string[] destinationIds) : base(pId, destinationIds) { }
+
+ protected override void AddInstance<T>(BaseTestCase test, T instance, string id)
+ {
+ test.AddDestination((IDestination)instance, id);
+ }
+ }
+
+ #endregion // end Destination Attribute Class
+
+ #region Topic, Queue, Temp Topic, Temp Queue Atribute Classes
+
+ internal class TopicSetupAttribute : DestinationSetupAttribute
+ {
+ protected override string InstanceName { get { return typeof(ITopic).Name; } }
+
+ public TopicSetupAttribute(string pId, params string[] destinationIds) : base(pId, destinationIds) { }
+
+ public TopicSetupAttribute(string pId, string destinationId) : this(pId, new string[] { destinationId }) { }
+
+ public TopicSetupAttribute(string pId = null) : this(pId, new string[] { null }) { }
+
+ public override void BeforeTest(ITest test)
+ {
+ base.BeforeTest(test);
+ InitializeNUnitTest<ITopic, ISession>(test);
+ }
+
+ public override void Setup(BaseTestCase nmsTest)
+ {
+ base.Setup(nmsTest);
+ InitializeTest<ITopic, ISession>(nmsTest);
+ }
+
+ protected override T CreateNMSInstance<T, P>(BaseTestCase test, P parent)
+ {
+ ISession session = (ISession)parent;
+ return (T)test.CreateTopic(session, Name);
+ }
+ }
+
+ internal class TemporaryTopicSetupAttribute : DestinationSetupAttribute
+ {
+ protected override string InstanceName { get { return typeof(ITemporaryTopic).Name; } }
+
+ public TemporaryTopicSetupAttribute(string pId, string[] destinationIds) : base(pId, destinationIds) { }
+
+ public TemporaryTopicSetupAttribute(string pId, string destinationId) : this(pId, new string[] { destinationId }) { }
+
+ public TemporaryTopicSetupAttribute(string pId = null) : this(pId, new string[] { null }) { }
+
+ public override void BeforeTest(ITest test)
+ {
+ base.BeforeTest(test);
+ InitializeNUnitTest<ITemporaryTopic, ISession>(test);
+ }
+
+ public override void Setup(BaseTestCase nmsTest)
+ {
+ base.Setup(nmsTest);
+ InitializeTest<ITemporaryTopic, ISession>(nmsTest);
+ }
+
+ protected override T CreateNMSInstance<T, P>(BaseTestCase test, P parent)
+ {
+ ISession session = (ISession)parent;
+ return (T)test.CreateTemporaryTopic(session);
+ }
+ }
+
+ internal class QueueSetupAttribute : DestinationSetupAttribute
+ {
+ protected override string InstanceName { get { return typeof(IQueue).Name; } }
+
+ public QueueSetupAttribute(string pId, params string[] destinationIds) : base(pId, destinationIds) { }
+
+ public QueueSetupAttribute(string pId, string destinationId) : this(pId, new string[] { destinationId }) { }
+
+ public QueueSetupAttribute(string pId = null) : this(pId, new string[] { null }) { }
+
+ public override void BeforeTest(ITest test)
+ {
+ base.BeforeTest(test);
+ InitializeNUnitTest<IQueue, ISession>(test);
+ }
+
+ public override void Setup(BaseTestCase nmsTest)
+ {
+ base.Setup(nmsTest);
+ InitializeTest<IQueue, ISession>(nmsTest);
+ }
+
+ protected override T CreateNMSInstance<T, P>(BaseTestCase test, P parent)
+ {
+ ISession session = (ISession)parent;
+ return (T)test.CreateQueue(session, Name);
+ }
+ }
+
+ internal class TemporaryQueueSetupAttribute : DestinationSetupAttribute
+ {
+ protected override string InstanceName { get { return typeof(ITemporaryQueue).Name; } }
+
+ public TemporaryQueueSetupAttribute(string pId, string[] destinationIds) : base(pId, destinationIds) { }
+
+ public TemporaryQueueSetupAttribute(string pId, string destinationId) : this(pId, new string[] { destinationId }) { }
+
+ public TemporaryQueueSetupAttribute(string pId = null) : this(pId, new string[] { null }) { }
+
+ public override void BeforeTest(ITest test)
+ {
+ base.BeforeTest(test);
+ InitializeNUnitTest<ITemporaryQueue, ISession>(test);
+ }
+
+ public override void Setup(BaseTestCase nmsTest)
+ {
+ base.Setup(nmsTest);
+ InitializeTest<ITemporaryQueue, ISession>(nmsTest);
+ }
+
+ protected override T CreateNMSInstance<T, P>(BaseTestCase test, P parent)
+ {
+ ISession session = (ISession)parent;
+ return (T)test.CreateTemporaryQueue(session);
+ }
+ }
+
+ #endregion // End Topic, Queue, Temp Topic, Temp Queue Attribute Classes
+
+
+
+}
http://git-wip-us.apache.org/repos/asf/activemq-nms-amqp/blob/432c9613/src/test/csharp/Test/Attribute/ProducerSetup.cs
----------------------------------------------------------------------
diff --git a/src/test/csharp/Test/Attribute/ProducerSetup.cs b/src/test/csharp/Test/Attribute/ProducerSetup.cs
new file mode 100644
index 0000000..f7d13a0
--- /dev/null
+++ b/src/test/csharp/Test/Attribute/ProducerSetup.cs
@@ -0,0 +1,103 @@
+/*
+ * 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.Text;
+using System.Collections.Specialized;
+using NUnit.Framework;
+using NUnit.Framework.Interfaces;
+using Apache.NMS;
+using Apache.NMS.AMQP.Test.Util;
+using System.Diagnostics;
+using System.Collections.Generic;
+using System.Collections;
+using System.Reflection;
+using Apache.NMS.AMQP.Test.TestCase;
+
+namespace Apache.NMS.AMQP.Test.Attribute
+{
+
+ #region Producer Setup Attribute Class
+
+ internal class ProducerSetupAttribute : SessionParentDestinationDependentSetupAttribute
+ {
+ public static readonly long DEFAULT_TTL_LONG = Convert.ToInt64(NMSConstants.defaultTimeToLive.TotalMilliseconds);
+ public MsgDeliveryMode DeliveryMode { get; set; } = NMSConstants.defaultDeliveryMode;
+
+ public MsgPriority MsgPriority { get; set; } = NMSConstants.defaultPriority;
+
+ public long TimeToLive { get; set; } = DEFAULT_TTL_LONG;
+
+ public int RequestTimeout { get; set; } = System.Threading.Timeout.Infinite;
+
+ protected override string InstanceName { get { return typeof(IMessageProducer).Name; } }
+
+ public ProducerSetupAttribute(string parentId, string destinationId, string[] producerIds) : base(parentId, destinationId, producerIds) { }
+
+ public ProducerSetupAttribute(string parentId, string destinationId, string producerId) : this(parentId, destinationId, new string[] { producerId }) { }
+
+ public ProducerSetupAttribute(string parentId = null, string destinationId = null) : this(parentId, destinationId, new string[] { null }) { }
+
+ public override void BeforeTest(ITest test)
+ {
+ base.BeforeTest(test);
+ InitializeNUnitTest<IMessageProducer, ISession>(test);
+ }
+
+ public override void Setup(BaseTestCase nmsTest)
+ {
+ base.Setup(nmsTest);
+ InitializeTest<IMessageProducer, ISession>(nmsTest);
+ }
+
+ protected void InitializeProducerProperties(IMessageProducer producer)
+ {
+ if (MsgPriority != NMSConstants.defaultPriority)
+ {
+ producer.Priority = MsgPriority;
+ }
+ if (DeliveryMode != NMSConstants.defaultDeliveryMode)
+ {
+ producer.DeliveryMode = DeliveryMode;
+ }
+ if (RequestTimeout != System.Threading.Timeout.Infinite)
+ {
+ producer.RequestTimeout = TimeSpan.FromMilliseconds(RequestTimeout);
+ }
+ if(TimeToLive != DEFAULT_TTL_LONG)
+ {
+ producer.TimeToLive = TimeSpan.FromMilliseconds(TimeToLive);
+ }
+
+ }
+
+ protected override T CreateNMSInstance<T, P>(BaseTestCase test, P parent)
+ {
+ IMessageProducer producer = test.CreateProducer((ISession)parent, this.GetDestination(test));
+ InitializeProducerProperties(producer);
+ return (T)producer;
+ }
+
+ protected override void AddInstance<T>(BaseTestCase test, T instance, string id)
+ {
+ test.AddProducer((IMessageProducer)instance, id);
+ }
+
+ }
+
+ #endregion // End Producer Setup
+
+}
http://git-wip-us.apache.org/repos/asf/activemq-nms-amqp/blob/432c9613/src/test/csharp/Test/Attribute/SessionSetup.cs
----------------------------------------------------------------------
diff --git a/src/test/csharp/Test/Attribute/SessionSetup.cs b/src/test/csharp/Test/Attribute/SessionSetup.cs
new file mode 100644
index 0000000..3fe0cbe
--- /dev/null
+++ b/src/test/csharp/Test/Attribute/SessionSetup.cs
@@ -0,0 +1,106 @@
+/*
+ * 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.Text;
+using System.Collections.Specialized;
+using NUnit.Framework;
+using NUnit.Framework.Interfaces;
+using Apache.NMS;
+using Apache.NMS.AMQP.Test.Util;
+using System.Diagnostics;
+using System.Collections.Generic;
+using System.Collections;
+using System.Reflection;
+using Apache.NMS.AMQP.Test.TestCase;
+
+namespace Apache.NMS.AMQP.Test.Attribute
+{
+
+ #region Session Setup Attribute Class
+
+ internal class SessionSetupAttribute : TestSetupAttribute
+ {
+ public AcknowledgementMode AckMode { get; set; } = AcknowledgementMode.DupsOkAcknowledge;
+
+ protected override string InstanceName
+ {
+ get { return typeof(ISession).Name; }
+ }
+
+ protected override string ParentName
+ {
+ get { return typeof(IConnection).Name; }
+ }
+
+ protected override int ExecuteOrder { get { return 2; } }
+
+ public SessionSetupAttribute(string connectionId, params string[] sessionIds) : base(connectionId, sessionIds) { }
+
+ public SessionSetupAttribute(string connectionId, string sessionId) : this(connectionId, new string[] { sessionId }) { }
+ public SessionSetupAttribute(string connectionId = null) : this(connectionId, new string[] { null }) { }
+
+ public override void BeforeTest(ITest test)
+ {
+ base.BeforeTest(test);
+ InitializeNUnitTest<ISession, IConnection>(test);
+ }
+
+ public override void Setup(BaseTestCase nmsTest)
+ {
+ base.Setup(nmsTest);
+ InitializeTest<ISession, IConnection>(nmsTest);
+ }
+
+ protected override P GetParentNMSInstance<P>(BaseTestCase test)
+ {
+ IConnection connection = null;
+ if (!test.NMSInstanceExists<IConnection>(parentIndex))
+ {
+ if (NmsParentId == null)
+ {
+ TestSetupFailureParentNotFound(test);
+ }
+ else
+ {
+ connection = test.GetConnection(NmsParentId);
+ }
+ }
+ else
+ {
+ connection = test.GetConnection(parentIndex);
+ }
+ return (P)connection;
+ }
+
+ protected override T CreateNMSInstance<T, P>(BaseTestCase test, P parent)
+ {
+ IConnection Parent = (IConnection)parent;
+ Parent.AcknowledgementMode = AckMode;
+ return (T)test.CreateSession(Parent);
+ }
+
+ protected override void AddInstance<T>(BaseTestCase test, T instance, string id)
+ {
+ BaseTestCase.Logger.Info("Adding Session " + id + " to test " + TestName);
+ test.AddSession((ISession)instance, id);
+ }
+
+ }
+
+ #endregion // Session Setup
+
+}
http://git-wip-us.apache.org/repos/asf/activemq-nms-amqp/blob/432c9613/src/test/csharp/Test/Attribute/TestSetup.cs
----------------------------------------------------------------------
diff --git a/src/test/csharp/Test/Attribute/TestSetup.cs b/src/test/csharp/Test/Attribute/TestSetup.cs
new file mode 100644
index 0000000..6494e84
--- /dev/null
+++ b/src/test/csharp/Test/Attribute/TestSetup.cs
@@ -0,0 +1,385 @@
+/*
+ * 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.Text;
+using System.Collections.Specialized;
+using NUnit.Framework;
+using NUnit.Framework.Interfaces;
+using Apache.NMS;
+using Apache.NMS.AMQP.Test.Util;
+using System.Diagnostics;
+using System.Collections.Generic;
+using System.Collections;
+using System.Reflection;
+using Apache.NMS.AMQP.Test.TestCase;
+
+namespace Apache.NMS.AMQP.Test.Attribute
+{
+
+ #region Abstract Test Setup Attribute Classes
+
+ #region Base Test Setup Attribute Class
+
+ [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class |
+ AttributeTargets.Interface | AttributeTargets.Assembly,
+ AllowMultiple = true)]
+ internal abstract class TestSetupAttribute : System.Attribute, ITestAction, IComparable
+ {
+ private enum SetupType { Unknown = -1, TestAction = 0, TestSetup = 1 }
+ private static readonly Exception FailureException = new Exception("Test Setup Failure.");
+
+ public bool IsTestAction
+ {
+ get
+ {
+ return type == SetupType.TestAction;
+ }
+
+ set
+ {
+ if (value == false)
+ {
+ type = SetupType.TestSetup;
+ }
+ else
+ {
+ type = SetupType.TestAction;
+ }
+ }
+ }
+ public string NmsParentId;
+ protected int parentIndex = -1;
+ protected string[] nmsInstanceIds;
+ private SetupType type = SetupType.Unknown;
+
+ protected abstract string InstanceName { get; }
+ protected abstract string ParentName { get; }
+
+ protected string TestName { get; private set; }
+
+ protected TestSetupAttribute(string nmsParentId, string[] nmsIds)
+ {
+ if (nmsIds == null)
+ {
+ throw new ArgumentNullException("NMS " + InstanceName + " Ids can not be null.");
+ }
+ else if (nmsIds.Length == 0)
+ {
+ throw new ArgumentException("At Least one " + InstanceName + " must be specified.");
+ }
+ if (nmsParentId == null)
+ {
+ parentIndex = 0;
+ }
+ NmsParentId = nmsParentId;
+ this.nmsInstanceIds = nmsIds;
+ IsTestAction = false;
+ }
+
+ public ActionTargets Targets
+ {
+ get { return ActionTargets.Test; }
+ }
+
+ public virtual void AfterTest(ITest test)
+ {
+
+ }
+
+ public virtual void BeforeTest(ITest test)
+ {
+ TestName = test.MethodName;
+ if (type == SetupType.Unknown)
+ {
+ type = SetupType.TestAction;
+ }
+ }
+
+ public virtual void Setup(BaseTestCase nmsTest)
+ {
+ TestName = TestContext.CurrentContext.Test.MethodName;
+ if (type == SetupType.Unknown)
+ {
+ type = SetupType.TestSetup;
+ }
+ }
+
+ protected void InitializeTest<T, P>(BaseTestCase nmsTest, bool fromAction = false)
+ {
+ if (nmsTest != null && (!IsTestAction || fromAction))
+ {
+ BaseTestCase.Logger.Info("IsTestAction : " + IsTestAction + " fromAction " + fromAction + "");
+ if (BaseTestCase.Logger.IsDebugEnabled)
+ BaseTestCase.Logger.Debug("Initial test state: " + nmsTest.ToString());
+ P parent = GetParentNMSInstance<P>(nmsTest);
+ if (BaseTestCase.Logger.IsDebugEnabled)
+ BaseTestCase.Logger.Debug("Added parent test state: " + nmsTest.ToString());
+ for (int i = 0; i < nmsInstanceIds.Length; i++)
+ {
+ string id = nmsInstanceIds[i];
+ T instance = CreateNMSInstance<T, P>(nmsTest, parent);
+ if (BaseTestCase.Logger.IsDebugEnabled)
+ BaseTestCase.Logger.Debug("Adding Instance " + id + " test state: " + nmsTest.ToString());
+ AddInstance(nmsTest, instance, id);
+ if (BaseTestCase.Logger.IsDebugEnabled)
+ BaseTestCase.Logger.Debug("Added Instance " + id + " test state: " + nmsTest.ToString());
+ }
+ }
+ }
+
+
+ protected void InitializeNUnitTest<T, P>(ITest test)
+ {
+ BaseTestCase nmsTest = GetTest(test);
+ if (IsTestAction)
+ InitializeTest<T, P>(nmsTest, true);
+ }
+
+ protected BaseTestCase GetTest(ITest test)
+ {
+ object fixture = test.Fixture;
+ if (fixture is BaseTestCase)
+ {
+ return fixture as BaseTestCase;
+ }
+ return null;
+ }
+
+ protected abstract P GetParentNMSInstance<P>(BaseTestCase test);
+
+ protected abstract T CreateNMSInstance<T, P>(BaseTestCase test, P parent);
+ protected abstract void AddInstance<T>(BaseTestCase test, T instance, string id);
+
+
+ protected virtual void TestSetupFailure(BaseTestCase test, string message, Exception cause)
+ {
+ test.PrintTestFailureAndAssert(
+ TestName,
+ message,
+ cause
+ );
+ }
+
+ protected virtual void TestSetupFailure(BaseTestCase test, string message)
+ {
+ TestSetupFailure(test, message, FailureException);
+ }
+
+ protected virtual void TestSetupFailureParentNotFound(BaseTestCase test)
+ {
+ string id = (NmsParentId == null) ? parentIndex.ToString() : NmsParentId;
+
+ string message = string.Format(
+ "Failed to find Parent {0} {1} to create {2}.",
+ ParentName, id, InstanceName
+ );
+ TestSetupFailure(test, message);
+ }
+
+ public int CompareTo(object obj)
+ {
+ int result = -1;
+ if(obj != null && obj is TestSetupAttribute)
+ {
+ TestSetupAttribute other = obj as TestSetupAttribute;
+ result = this.InstanceName.CompareTo(other.InstanceName);
+
+ if (result == 0)
+ {
+ result = this.nmsInstanceIds.Length - other.nmsInstanceIds.Length;
+ }
+
+ if(result == 0 && this.nmsInstanceIds.Length != 0)
+ {
+ for(int i=0; (i < this.nmsInstanceIds.Length) && result == 0; i++)
+ {
+ string id = this.nmsInstanceIds[i];
+ string otherId = other.nmsInstanceIds[i];
+ result = id.CompareTo(otherId);
+ }
+ }
+
+ }
+ return result;
+ }
+
+ public int ComparableOrder
+ {
+ get
+ {
+ return int.MaxValue - ExecuteOrder;
+ }
+ }
+
+ protected abstract int ExecuteOrder { get; }
+
+ }
+
+ #endregion //End base Base Test Setup
+
+
+ #region TestRestrictionSetup Attribute Class
+
+ abstract class TestRestrictionSetupAttribute : TestSetupAttribute
+ {
+ protected interface IRestriction<T>
+ {
+ /// <summary>
+ /// Applies restriction to an NMSInstance.
+ /// </summary>
+ /// <param name="NMSInstance">The test object to apply the restriction to.</param>
+ /// <returns>
+ /// true, to indicate the NMSInstance can satisfy the restriction required of it.
+ /// false, to indicate the NMSInstance can not satisfy the restriction required of it this would indicate the test should be run.
+ /// </returns>
+ bool Apply(T NMSInstance);
+ }
+
+ public TestRestrictionSetupAttribute(string parentId) : base(parentId, new string[] { "r1" }) { }
+
+ #region Unused Test Setup Methods
+
+ protected override void AddInstance<T>(BaseTestCase test, T instance, string id)
+ {
+ throw new NotImplementedException();
+ }
+
+ protected override T CreateNMSInstance<T, P>(BaseTestCase test, P parent)
+ {
+ throw new NotImplementedException();
+ }
+
+ #endregion
+
+ protected void RestrictTestInstance<T>(BaseTestCase test)
+ {
+ T nmsInstance = this.GetParentNMSInstance<T>(test);
+ IList<IRestriction<T>> restrictions = GetRestrictions<T>();
+ foreach (IRestriction<T> r in restrictions)
+ {
+ if (!r.Apply(nmsInstance))
+ {
+ this.HandleUnsatisfiedRestriction(r, nmsInstance);
+ }
+ }
+ }
+
+ protected virtual IList<IRestriction<T>> GetRestrictions<T>()
+ {
+ return new List<IRestriction<T>>();
+ }
+
+ protected abstract void HandleUnsatisfiedRestriction<T>(IRestriction<T> restriction, T NMSInstance);
+ }
+
+ #endregion // end test restriction Attribute class.
+
+ #region Parent Session Setup Attribute Class
+
+ abstract class SessionParentSetupAttribute : TestSetupAttribute
+ {
+ protected override string ParentName { get { return typeof(ISession).Name; } }
+
+ protected override int ExecuteOrder { get { return 3; } }
+
+ protected SessionParentSetupAttribute(string sessionId, string[] nmsIds) : base(sessionId, nmsIds) { }
+
+ protected override P GetParentNMSInstance<P>(BaseTestCase test)
+ {
+ ISession session = null;
+ if (!test.NMSInstanceExists<ISession>(parentIndex))
+ {
+ if (NmsParentId == null)
+ {
+ TestSetupFailureParentNotFound(test);
+ }
+ else
+ {
+ session = test.GetSession(NmsParentId);
+ }
+ }
+ else
+ {
+ session = test.GetSession(parentIndex);
+ }
+ return (P)session;
+ }
+
+ }
+
+ #endregion // end Session Parent Attribute Class
+
+ #region Destination Dependent Attribute Class
+
+ abstract class SessionParentDestinationDependentSetupAttribute : SessionParentSetupAttribute
+ {
+ public string DestinationId { get; set; }
+
+ protected int destinationIndex = -1;
+
+ protected override int ExecuteOrder { get { return 4; } }
+
+ protected SessionParentDestinationDependentSetupAttribute(string sessionId, string destinationId, string[] nmsIds) : base(sessionId, nmsIds)
+ {
+ if (destinationId == null || destinationId.Length == 0)
+ {
+ destinationIndex = 0;
+ }
+ else
+ {
+ DestinationId = destinationId;
+ }
+ }
+
+ protected IDestination GetDestination(BaseTestCase test)
+ {
+ IDestination destination = null;
+ if (!test.NMSInstanceExists<IDestination>(destinationIndex))
+ {
+ if (DestinationId == null)
+ {
+ TestSetupFailureDestinationNotFound(test);
+ }
+ else
+ {
+ destination = test.GetDestination(DestinationId);
+ }
+ }
+ else
+ {
+ destination = test.GetDestination(destinationIndex);
+ }
+ return destination;
+ }
+
+ protected virtual void TestSetupFailureDestinationNotFound(BaseTestCase test)
+ {
+ string destinationId = DestinationId ?? destinationIndex.ToString();
+ string message = string.Format("Failed to find IDestination {0}, when creating {1}.",
+ destinationId,
+ InstanceName
+ );
+ TestSetupFailure(test, message);
+ }
+
+ }
+
+ #endregion
+
+ #endregion // end Attribute classes
+
+}
http://git-wip-us.apache.org/repos/asf/activemq-nms-amqp/blob/432c9613/src/test/csharp/Test/TestCase/BaseTestCase.cs
----------------------------------------------------------------------
diff --git a/src/test/csharp/Test/TestCase/BaseTestCase.cs b/src/test/csharp/Test/TestCase/BaseTestCase.cs
new file mode 100644
index 0000000..3395862
--- /dev/null
+++ b/src/test/csharp/Test/TestCase/BaseTestCase.cs
@@ -0,0 +1,1012 @@
+/*
+ * 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.Text;
+using System.Collections.Specialized;
+using NUnit.Framework;
+using NUnit.Framework.Interfaces;
+using Apache.NMS;
+using Apache.NMS.AMQP.Test.Util;
+using System.Diagnostics;
+using System.Collections.Generic;
+using System.Collections;
+using System.Reflection;
+using Apache.NMS.AMQP.Test.Attribute;
+
+namespace Apache.NMS.AMQP.Test.TestCase
+{
+
+ internal static class NMSTestConstants
+ {
+ public const string NMS_SOLACE_PLATFORM = "Solace PubSub+";
+ public const string NMS_ACTIVE_PRODUCT = "ActiveMQ";
+ }
+
+ internal static class NMSPropertyConstants
+ {
+ public const string NMS_CONNECTION_ENCODING = "NMS.Message.Serialization";
+ public const string NMS_CONNECTION_CLIENT_ID = "NMS.ClientId";
+ public const string NMS_CONNECTION_USERNAME = "NMS.Username";
+ public const string NMS_CONNECTION_PASSWORD = "NMS.Password";
+ public const string NMS_CONNECTION_REQUEST_TIMEOUT = "NMS.RequestTimeout";
+ public const string NMS_CONNECTION_MAX_FRAME_SIZE = "NMS.MaxFrameSize";
+ public const string NMS_CONNECTION_CLOSE_TIMEOUT = "NMS.CloseTimeout";
+
+ #region Transport Properties
+
+ public const string NMS_TRANSPORT_RECEIVE_BUFFER_SIZE = "transport.ReceiveBufferSize";
+
+ public const string NMS_TRANSPORT_RECEIVE_TIMEOUT = "transport.ReceiveTimeout";
+
+ public const string NMS_TRANSPORT_SEND_BUFFER_SIZE = "transport.SendBufferSize";
+
+ public const string NMS_TRANSPORT_SEND_TIMEOUT = "transport.SendTimeout";
+
+ public const string NMS_TRANSPORT_USE_LOGGING = "transport.UseLogging";
+
+ public const string NMS_SECURE_TRANSPORT_ACCEPT_INVALID_BROKER_CERT = "transport.AcceptInvalidBrokerCert";
+
+ public const string NMS_SECURE_TRANSPORT_CLIENT_CERT_FILE_NAME = "transport.ClientCertFileName";
+
+ public const string NMS_SECURE_TANSPORT_KEY_STORE_NAME = "transport.KeyStoreName";
+
+ public const string NMS_SECURE_TANSPORT_KEY_STORE_LOCATION = "transport.KeyStoreLocation";
+
+ public const string NMS_SECURE_TANSPORT_CLIENT_CERT_PASSWORD = "transport.ClientCertPassword";
+
+ public const string NMS_SECURE_TANSPORT_CLIENT_CERT_SUBJECT = "transport.ClientCertSubject";
+
+ public const string NMS_SECURE_TANSPORT_SERVER_NAME = "transport.ServerName";
+
+ public const string NMS_SECURE_TANSPORT_SSL_PROTOCOLS = "transport.SSLProtocol";
+
+ public const string NMS_SECURE_TRANSPORT_SSL_CHECK_CERTIFICATE_REVOCATION = "transport.CheckCertificateRevocation";
+
+ #endregion
+ }
+
+ #region NMSTestContainer Class
+
+ /// <summary>
+ /// NMSTestContainer Root class for all tests. Container NMS object Instances for testing and manages them.
+ /// </summary>
+ public abstract class NMSTestContainer
+ {
+ public static StringDictionary Clone(StringDictionary original)
+ {
+ if(original == null) return null;
+ StringDictionary clone = new StringDictionary();
+ foreach (string key in original.Keys)
+ {
+ clone.Add(key.Clone() as string, original[key].Clone() as string);
+ }
+ return clone;
+ }
+
+ public static string ToString(IDictionary dictionary, int indt = 0)
+ {
+ if (dictionary == null) return "[]";
+ StringBuilder sb = new StringBuilder();
+
+ int indent = Math.Max(0, Math.Min(indt, 16));
+ StringBuilder sbTabs = new StringBuilder();
+ for (int i = 0; i < indent; i++)
+ {
+ sbTabs.Append('\t');
+ }
+ string wspace = sbTabs.ToString();
+
+ sb.AppendFormat("[\n");
+ foreach (object key in dictionary.Keys)
+ {
+ if (key != null)
+ {
+ //Console.WriteLine("key: {0}, value: {1}", key, dictionary[key]);
+
+ sb.AppendFormat("{0}\t[Key:{1}, Value: {2}]\n", wspace, key.ToString(), dictionary[key]?.ToString());
+ }
+ }
+ sb.AppendFormat("{0}]", wspace);
+ return sb.ToString();
+ }
+
+ public static string ToString(StringDictionary dictionary, int indt = 0)
+ {
+ if (dictionary == null) return "[]";
+ StringBuilder sb = new StringBuilder();
+
+ int indent = Math.Max(0, Math.Min(indt, 16));
+ StringBuilder sbTabs = new StringBuilder();
+ for (int i = 0; i < indent; i++)
+ {
+ sbTabs.Append('\t');
+ }
+ string wspace = sbTabs.ToString();
+
+ sb.AppendFormat("{0}[\n", wspace);
+ foreach (string key in dictionary.Keys)
+ {
+ if (key != null)
+ {
+ sb.AppendFormat("{0}\t[Key:{1}, Value: {2}]\n", wspace, key, dictionary[key] ?? "null");
+ }
+ }
+ sb.AppendFormat("{0}]", wspace);
+ return sb.ToString();
+ }
+
+ protected StringDictionary properties = null;
+ protected StringDictionary DefaultProperties = null;
+ private Apache.NMS.NMSConnectionFactory providerFactory = null;
+
+ private IList<IConnectionFactory> connectionFactories = new List<IConnectionFactory>();
+ private IList<IConnection> connections = new List<IConnection>();
+ private IList<ISession> sessions = new List<ISession>();
+ private IList<IMessageProducer> producers = new List<IMessageProducer>();
+ private IList<IMessageConsumer> consumers = new List<IMessageConsumer>();
+ private IList<IDestination> destinations = new List<IDestination>();
+
+ private IDictionary<string, int> connectionFactoryIndexMap = new Dictionary<string, int>();
+ private IDictionary<string, int> connectionIndexMap = new Dictionary<string, int>();
+ private IDictionary<string, int> sessionIndexMap = new Dictionary<string, int>();
+ private IDictionary<string, int> producerIndexMap = new Dictionary<string, int>();
+ private IDictionary<string, int> consumerIndexMap = new Dictionary<string, int>();
+ private IDictionary<string, int> destinationIndexMap = new Dictionary<string, int>();
+ private IDictionary<Type, IList> NMSInstanceTypeMap = new Dictionary<Type, IList>();
+ private IDictionary<Type, IDictionary> NMSInstanceTypeIndexMap = new Dictionary<Type, IDictionary>();
+
+ protected NMSTestContainer()
+ {
+ NMSInstanceTypeMap[typeof(IConnectionFactory)] = connectionFactories as List<IConnectionFactory> as IList;
+ NMSInstanceTypeMap[typeof(IConnection)] = connections as List<IConnection> as IList;
+ NMSInstanceTypeMap[typeof(ISession)] = sessions as List<ISession> as IList;
+ NMSInstanceTypeMap[typeof(IMessageProducer)] = producers as List<IMessageProducer> as IList;
+ NMSInstanceTypeMap[typeof(IMessageConsumer)] = consumers as List<IMessageConsumer> as IList;
+ NMSInstanceTypeMap[typeof(IDestination)] = destinations as List<IDestination> as IList;
+
+ NMSInstanceTypeIndexMap[typeof(IConnectionFactory)] = connectionFactoryIndexMap as Dictionary<string, int>;
+ NMSInstanceTypeIndexMap[typeof(IConnection)] = connectionIndexMap as Dictionary<string, int>;
+ NMSInstanceTypeIndexMap[typeof(ISession)] = sessionIndexMap as Dictionary<string, int>;
+ NMSInstanceTypeIndexMap[typeof(IMessageProducer)] = producerIndexMap as Dictionary<string, int>;
+ NMSInstanceTypeIndexMap[typeof(IMessageConsumer)] = consumerIndexMap as Dictionary<string, int>;
+ NMSInstanceTypeIndexMap[typeof(IDestination)] = destinationIndexMap as Dictionary<string, int>;
+
+ //try
+ //{
+ // Console.WriteLine("TYPE MAP: {0}", ToString(NMSInstanceTypeMap as Dictionary<Type, IList>));
+ // Console.WriteLine("TYPE INDEX MAP: {0}", ToString(NMSInstanceTypeIndexMap as Dictionary<Type, IDictionary>));
+ //}
+ //catch (Exception e)
+ //{
+ // Console.Error.WriteLine("Error: {0}", e.Message);
+ // Console.WriteLine(e);
+ //}
+ }
+
+ public Uri BrokerURI
+ {
+ get { return TestConfig.Instance.BrokerUri; }
+ //protected set { if (connectionFactory != null) { ConnectionFactory.BrokerUri = value; } }
+ }
+ internal StringDictionary ConnectionFactoryProperties
+ {
+ get { return Clone(properties); }
+ }
+
+ private void UpdateConnectionFactoryProperty(string key, string value)
+ {
+ if(properties != null && key != null)
+ {
+ if (this.properties.ContainsKey(key))
+ {
+ this.properties[key] = value;
+ }
+ else
+ {
+ this.properties.Add(key, value);
+ }
+ }
+ }
+
+ internal void InitConnectedFactoryProperties(StringDictionary additionalProperties = null)
+ {
+ bool isDefault = this.properties == null;
+ // add properties from the TestConfig first and use as Default Properties.
+ properties = new StringDictionary();
+ if (TestConfig.Instance.BrokerUsername != null)
+ {
+ this.UpdateConnectionFactoryProperty(NMSPropertyConstants.NMS_CONNECTION_USERNAME, TestConfig.Instance.BrokerUsername);
+ }
+ if (TestConfig.Instance.BrokerPassword != null)
+ {
+ this.UpdateConnectionFactoryProperty(NMSPropertyConstants.NMS_CONNECTION_PASSWORD, TestConfig.Instance.BrokerPassword);
+ }
+ if (TestConfig.Instance.ClientId != null)
+ {
+ ConnectionFactoryProperties[NMSPropertyConstants.NMS_CONNECTION_CLIENT_ID] = TestConfig.Instance.ClientId;
+ }
+
+ // init secure properties if broker uri is secure
+ if (TestConfig.Instance.IsSecureBroker)
+ {
+ if (TestConfig.Instance.AcceptInvalidBrokerCert)
+ {
+ string value = TestConfig.Instance.AcceptInvalidBrokerCert ? bool.TrueString : bool.FalseString;
+ this.UpdateConnectionFactoryProperty(NMSPropertyConstants.NMS_SECURE_TRANSPORT_ACCEPT_INVALID_BROKER_CERT, value);
+ }
+ if (TestConfig.Instance.ClientCertFileName != null)
+ {
+ this.UpdateConnectionFactoryProperty(NMSPropertyConstants.NMS_SECURE_TRANSPORT_CLIENT_CERT_FILE_NAME, TestConfig.Instance.ClientCertFileName);
+ }
+ if (TestConfig.Instance.KeyStoreName != null)
+ {
+ this.UpdateConnectionFactoryProperty(NMSPropertyConstants.NMS_SECURE_TANSPORT_KEY_STORE_NAME, TestConfig.Instance.KeyStoreName);
+ }
+ if (TestConfig.Instance.KeyStoreLocation != null)
+ {
+ this.UpdateConnectionFactoryProperty(NMSPropertyConstants.NMS_SECURE_TANSPORT_KEY_STORE_NAME, TestConfig.Instance.KeyStoreLocation);
+ }
+ if (TestConfig.Instance.BrokerName != null)
+ {
+ this.UpdateConnectionFactoryProperty(NMSPropertyConstants.NMS_SECURE_TANSPORT_SERVER_NAME, TestConfig.Instance.BrokerName);
+ }
+ if (TestConfig.Instance.ClientCertSubject != null)
+ {
+ this.UpdateConnectionFactoryProperty(NMSPropertyConstants.NMS_SECURE_TANSPORT_CLIENT_CERT_SUBJECT, TestConfig.Instance.ClientCertSubject);
+ }
+ if (TestConfig.Instance.ClientCertPassword != null)
+ {
+ this.UpdateConnectionFactoryProperty(NMSPropertyConstants.NMS_SECURE_TANSPORT_CLIENT_CERT_PASSWORD, TestConfig.Instance.ClientCertPassword);
+ }
+ }
+
+ if (DefaultProperties == null || isDefault)
+ DefaultProperties = Clone(properties);
+
+ // add/overwrite Additional properies for unique purposes.
+ if (additionalProperties != null)
+ {
+ foreach (string key in additionalProperties.Keys)
+ {
+ if (properties.ContainsKey(key))
+ {
+ properties[key] = additionalProperties[key];
+ }
+ else
+ {
+ properties.Add(key, additionalProperties[key]);
+ }
+ }
+ }
+
+ }
+
+ #region NMS Instance Create Methods
+ internal IConnectionFactory CreateConnectionFactory()
+ {
+ if (providerFactory == null)
+ {
+ providerFactory = new NMSConnectionFactory(BrokerURI, properties);
+ }
+ return providerFactory.ConnectionFactory;
+ }
+
+ internal IConnection CreateConnection(string nmsConnectionFactoryId)
+ {
+ return CreateConnection(GetConnectionFactory(nmsConnectionFactoryId));
+ }
+
+ internal IConnection CreateConnection(int index = 0)
+ {
+ return CreateConnection(GetConnectionFactory(index));
+ }
+
+ internal IConnection CreateConnection(IConnectionFactory factory)
+ {
+ return factory.CreateConnection();
+ }
+
+ internal ISession CreateSession(string nmsConnectionId)
+ {
+ return CreateSession(GetConnection(nmsConnectionId));
+ }
+
+ internal ISession CreateSession(int index = 0)
+ {
+ return CreateSession(GetConnection(index));
+ }
+
+ internal ISession CreateSession(IConnection connection)
+ {
+ return connection.CreateSession();
+ }
+
+ internal IMessageProducer CreateProducer(string nmsSessionId, string nmsDestinationId)
+ {
+ return CreateProducer(GetSession(nmsSessionId), GetDestination(nmsDestinationId));
+ }
+
+
+ internal IMessageProducer CreateProducer(int sessionIndex = 0, int destinationIndex = 0)
+ {
+ return CreateProducer(GetSession(sessionIndex), GetDestination(destinationIndex));
+ }
+
+ internal IMessageProducer CreateProducer(ISession session, IDestination destination)
+ {
+ return session.CreateProducer(destination);
+ }
+
+ internal IMessageConsumer CreateConsumer(string nmsSessionId, string nmsDestinationId)
+ {
+ return CreateConsumer(GetSession(nmsSessionId), GetDestination(nmsDestinationId));
+ }
+
+
+ internal IMessageConsumer CreateConsumer(int sessionIndex = 0, int destinationIndex = 0)
+ {
+ return CreateConsumer(GetSession(sessionIndex), GetDestination(destinationIndex));
+ }
+
+ internal IMessageConsumer CreateConsumer(ISession session, IDestination destination)
+ {
+ return session.CreateConsumer(destination);
+ }
+
+ internal ITopic CreateTopic(string name, string nmsId)
+ {
+ return CreateTopic(GetSession(nmsId), name);
+ }
+
+ internal ITopic CreateTopic(string name, int index = 0)
+ {
+ return CreateTopic(GetSession(index), name);
+ }
+
+ internal ITopic CreateTopic(ISession session, string name)
+ {
+ return session.GetTopic(name);
+ }
+
+ internal ITopic CreateTemporaryTopic(string nmsId)
+ {
+ return CreateTemporaryTopic(GetSession(nmsId));
+ }
+
+ internal ITopic CreateTemporaryTopic(int index = 0)
+ {
+ return CreateTemporaryTopic(GetSession(index));
+ }
+
+ internal ITemporaryTopic CreateTemporaryTopic(ISession session)
+ {
+ return session.CreateTemporaryTopic();
+ }
+
+ internal IQueue CreateQueue(string name, string nmsId)
+ {
+ return CreateQueue(GetSession(nmsId), name);
+ }
+
+ internal IQueue CreateQueue(string name, int index = 0)
+ {
+ return CreateQueue(GetSession(index), name);
+ }
+
+ internal IQueue CreateQueue(ISession session, string name)
+ {
+ return session.GetQueue(name);
+ }
+
+ internal IQueue CreateTemporaryQueue(string nmsId)
+ {
+ return CreateTemporaryQueue(GetSession(nmsId));
+ }
+
+ internal IQueue CreateTemporaryQueue(int index = 0)
+ {
+ return CreateTemporaryQueue(GetSession(index));
+ }
+
+ internal ITemporaryQueue CreateTemporaryQueue(ISession session)
+ {
+ return session.CreateTemporaryQueue();
+ }
+
+
+ #endregion
+
+ #region NMS Instance Get Methods
+
+ internal IConnectionFactory GetConnectionFactory(int index = 0)
+ {
+ return LookupNMSInstance(connectionFactories, index);
+ }
+
+ internal IConnectionFactory GetConnectionFactory(string nmsId)
+ {
+ int index = connectionFactoryIndexMap[nmsId];
+ return LookupNMSInstance(connectionFactories, index);
+ }
+
+ internal IConnection GetConnection(int index = 0)
+ {
+ return LookupNMSInstance(connections, index);
+ }
+
+ internal IConnection GetConnection(string nmsId)
+ {
+ int index = connectionIndexMap[nmsId];
+ return LookupNMSInstance(connections, index);
+ }
+
+ internal ISession GetSession(int index = 0)
+ {
+ return LookupNMSInstance(sessions, index);
+ }
+
+ internal ISession GetSession(string nmsId)
+ {
+ //Console.WriteLine("Trying to find Session {0}, in Index Map {1}.", nmsId, ToString(sessionIndexMap as Dictionary<String, int>));
+ int index = sessionIndexMap[nmsId];
+ return LookupNMSInstance(sessions, index);
+ }
+
+ internal IMessageProducer GetProducer(int index = 0)
+ {
+ return LookupNMSInstance(producers, index);
+ }
+
+ internal IMessageProducer GetProducer(string nmsId)
+ {
+ int index = producerIndexMap[nmsId];
+ return LookupNMSInstance(producers, index);
+ }
+
+ internal IMessageConsumer GetConsumer(int index = 0)
+ {
+ return LookupNMSInstance(consumers, index);
+ }
+ internal IMessageConsumer GetConsumer(string nmsId)
+ {
+ int index = consumerIndexMap[nmsId];
+ return LookupNMSInstance(consumers, index);
+ }
+
+ internal IDestination GetDestination(int index = 0)
+ {
+ return LookupNMSInstance(destinations, index);
+ }
+
+ internal IDestination GetDestination(string nmsId)
+ {
+ int index = destinationIndexMap[nmsId];
+ return LookupNMSInstance(destinations, index);
+ }
+
+ internal IList<IConnection> GetConnections(params string[] nmsIds)
+ {
+ return GetNMSIntances<IConnection>(nmsIds);
+ }
+
+ internal IList<ISession> GetSessions(params string[] nmsIds)
+ {
+ return GetNMSIntances<ISession>(nmsIds);
+ }
+ internal IList<IDestination> GetDestinations(params string[] nmsIds)
+ {
+ return GetNMSIntances<IDestination>(nmsIds);
+ }
+ internal IList<IMessageConsumer> GetConsumers(params string[] nmsIds)
+ {
+ return GetNMSIntances<IMessageConsumer>(nmsIds);
+ }
+ internal IList<IMessageProducer> GetProducers(params string[] nmsIds)
+ {
+ return GetNMSIntances<IMessageProducer>(nmsIds);
+ }
+
+ private IList<I> GetNMSIntances<I>(params string[] nmsIds)
+ {
+ IDictionary NMSInstanceIndexMap = NMSInstanceTypeIndexMap[typeof(I)];
+ IList NMSInstances = NMSInstanceTypeMap[typeof(I)];
+ return GetNMSIntances<I>(NMSInstanceIndexMap as Dictionary<string,int>, NMSInstances as List<I>, nmsIds);
+ }
+
+ private IList<I> GetNMSIntances<I>(IDictionary<string, int> indexMap, IList<I> instances, params string[] nmsIds)
+ {
+ if (nmsIds == null)
+ {
+ return null;
+ }
+ else if (nmsIds.Length == 0)
+ {
+ return new List<I>();
+ }
+ List<I> list = new List<I>();
+ foreach (string id in nmsIds)
+ {
+ int index = -1;
+ if (indexMap.TryGetValue(id, out index))
+ {
+ try
+ {
+ list.Add(LookupNMSInstance(instances, index));
+ }
+ catch (Exception ex)
+ {
+ BaseTestCase.Logger.Error("Caught exception when looking up NMS Instance " + typeof(I).Name + " for Id \"" + id + "\" Message : " + ex.Message );
+ }
+ }
+ }
+
+ return list;
+ }
+
+ private I LookupNMSInstance<I>(IList<I> list, int index = 0)
+ {
+ if (index < 0 || index >= list.Count)
+ {
+ throw new ArgumentOutOfRangeException(string.Format("Invalid index {0}, for {1} Collection.", index, typeof(I).Name));
+ }
+
+ I nmsInstance = list[index];
+ if (nmsInstance == null)
+ {
+ throw new ArgumentException(string.Format("Invalid index {0}, for {1} Collection. Return value is null.", index, typeof(I).Name));
+ }
+ return nmsInstance;
+ }
+
+ #endregion
+
+ #region NMS Instance Exists Methods
+
+ internal bool NMSInstanceExists<I>(int index)
+ {
+ IList NMSInstances = NMSInstanceTypeMap[typeof(I)];
+ if (index < 0 || index >= NMSInstances.Count) return false;
+ return NMSInstances[index] != null;
+ }
+
+ internal bool NMSInstanceExists<I>(string nmsId)
+ {
+ IDictionary NMSInstances = NMSInstanceTypeIndexMap[typeof(I)];
+ if (NMSInstances.Contains(nmsId)) return false;
+ int index = (int)NMSInstances[nmsId];
+ return NMSInstanceExists<I>(index);
+ }
+
+
+ #endregion
+
+ #region NMS Instance Add Methods
+
+ internal void AddConnectionFactory(IConnectionFactory factory, string nmsId=null)
+ {
+ int index = AddNMSInstance(connectionFactories, factory);
+ AddNMSInstanceIndexLookup(connectionFactoryIndexMap, nmsId, index);
+ }
+
+ internal void AddConnection(IConnection connection, string nmsId = null)
+ {
+ int index = AddNMSInstance(connections, connection);
+ AddNMSInstanceIndexLookup(connectionIndexMap, nmsId, index);
+ }
+
+ internal void AddSession(ISession session, string nmsId = null)
+ {
+ int index = AddNMSInstance(sessions, session);
+ AddNMSInstanceIndexLookup(sessionIndexMap, nmsId, index);
+ }
+
+ internal void AddProducer(IMessageProducer producer, string nmsId = null)
+ {
+ int index = AddNMSInstance(producers, producer);
+ AddNMSInstanceIndexLookup(producerIndexMap, nmsId, index);
+ }
+
+ internal void AddConsumer(IMessageConsumer consumer, string nmsId = null)
+ {
+ int index = AddNMSInstance(consumers, consumer);
+ AddNMSInstanceIndexLookup(consumerIndexMap, nmsId, index);
+ }
+
+ internal void AddDestination(IDestination destination, string nmsId = null)
+ {
+ int index = AddNMSInstance(destinations, destination);
+ AddNMSInstanceIndexLookup(destinationIndexMap, nmsId, index);
+ }
+
+
+ private int AddNMSInstance<I>(IList<I> list, I instance)
+ {
+ if (instance == null)
+ {
+ return -1;
+ }
+ int previousIndex = list.IndexOf(instance);
+ if (list.Contains(instance)) return previousIndex;
+ list.Add(instance);
+ return list.IndexOf(instance);
+ }
+
+ private void AddNMSInstanceIndexLookup(IDictionary<string,int>lookupTable, string nmsId, int index)
+ {
+ if (index != -1 && nmsId != null)
+ {
+ lookupTable.Add(nmsId, index);
+ }
+ }
+
+ #endregion
+
+ #region Object Override Methods
+
+ public override string ToString()
+ {
+ string result = "" + this.GetType().Name + ": [\n";
+ result += "\tConnection Factories: " + this.connectionFactories.Count + "\n";
+ result += "\tConnections: " + this.connections.Count + "\n";
+ result += "\tSessions: " + this.sessions.Count + "\n";
+ result += "\tProducers: " + this.producers.Count + "\n";
+ result += "\tConsumers: " + this.consumers.Count + "\n";
+ result += "\tDestinations: " + this.destinations.Count + "\n";
+ result += "\tConnection Factory Index Table: " + ToString(this.connectionFactoryIndexMap as IDictionary, 1) + "\n";
+ result += "\tConnection Index Table: " + ToString(this.connectionIndexMap as IDictionary, 1) + "\n";
+ result += "\tSession Index Table: " + ToString(this.sessionIndexMap as IDictionary, 1) + "\n";
+ result += "\tConsumer Index Table: " + ToString(this.consumerIndexMap as IDictionary, 1) + "\n";
+ result += "\tProducer Index Table: " + ToString(this.producerIndexMap as IDictionary, 1) + "\n";
+ result += "\tDestination Index Table: " + ToString(this.destinationIndexMap as IDictionary, 1) + "\n";
+ result += "]";
+ return result;
+ }
+
+ #endregion
+
+ #region NMS Instance Management Cleanup
+
+ protected void CleanupInstances(bool dispose = false)
+ {
+ CloseInstances();
+ ClearIndexes();
+ ClearInstances(dispose);
+ GC.Collect();
+ }
+
+ private void CloseInstances()
+ {
+ if (this.producers != null)
+ {
+ foreach(IMessageProducer p in producers)
+ {
+ p?.Close();
+ }
+ }
+
+ if (this.consumers != null)
+ {
+ foreach(IMessageConsumer c in consumers)
+ {
+ c?.Close();
+ }
+ }
+
+ if (this.sessions != null)
+ {
+ foreach(ISession s in sessions)
+ {
+ s?.Close();
+ }
+ }
+
+ if (this.connections != null)
+ {
+ foreach(IConnection c in connections)
+ {
+ c?.Close();
+ }
+ }
+
+ }
+
+ private void ClearIndexes()
+ {
+ foreach(IDictionary indexTable in NMSInstanceTypeIndexMap.Values)
+ {
+ indexTable.Clear();
+ }
+ }
+
+ private void ClearInstances(bool dispose = false)
+ {
+
+ if (dispose && this.producers != null)
+ {
+ foreach (IMessageProducer p in producers)
+ {
+ p?.Dispose();
+ }
+ }
+ producers?.Clear();
+
+ if (dispose && this.consumers != null)
+ {
+ foreach (IMessageConsumer c in consumers)
+ {
+ c?.Dispose();
+ }
+ }
+ consumers?.Clear();
+
+ if (dispose && this.sessions != null)
+ {
+ foreach (ISession s in sessions)
+ {
+ s?.Close();
+ }
+ }
+ sessions?.Clear();
+
+ if (dispose && this.connections != null)
+ {
+ foreach (IConnection c in connections)
+ {
+ c?.Close();
+ }
+ }
+ connections?.Clear();
+
+ connectionFactories?.Clear();
+
+ providerFactory = null;
+ }
+
+ #endregion
+
+ }
+
+ #endregion
+
+ #region BaseTestCase Class
+
+ public abstract class BaseTestCase : NMSTestContainer
+ {
+
+ internal static ITrace Logger = new NMSLogger(NMSLogger.ToLogLevel(TestConfig.Instance.LogLevel));
+
+ internal const string DURABLE_TOPIC_NAME = "nms.durable.test";
+ internal const string DURABLE_SUBSRIPTION_NAME = "uniqueSub";
+
+ static BaseTestCase()
+ {
+ Tracer.Trace = Logger;
+ }
+
+ public virtual bool IsSecureBroker { get => TestConfig.Instance.IsSecureBroker; }
+
+ private static readonly TestSetupAttributeComparer TestSetupOrderComparer = new TestSetupAttributeComparer();
+ private class TestSetupAttributeComparer : IComparer<TestSetupAttribute>
+ {
+ public int Compare(TestSetupAttribute x, TestSetupAttribute y)
+ {
+ int result = y.ComparableOrder - x.ComparableOrder;
+ if(result == 0)
+ {
+ result = x.CompareTo(y);
+ }
+ return result;
+ }
+ }
+
+ private void ApplyTestSetupAttributes()
+ {
+ // Apply TestSetup Attribute in correct order
+ TestContext.TestAdapter testAdapter = TestContext.CurrentContext.Test;
+ MethodInfo methodInfo = GetType().GetMethod(testAdapter.MethodName);
+ object[] attributes = methodInfo.GetCustomAttributes(true);
+
+ // This set will order the TestSetup Attributes in the appropriate execution order for NMS Instance Initialization.
+ // ie, should a test setup a connection and a session dependent that connection it ensure the connection setup attribute
+ // execute its setup first.
+ ISet<TestSetupAttribute> testSetupAttributes = new SortedSet<TestSetupAttribute>(TestSetupOrderComparer);
+ foreach (System.Attribute attribute in attributes)
+ {
+ if (attribute is TestSetupAttribute)
+ {
+ //Console.WriteLine("Setup Attribute Identification: {0}.", attribute.GetType().Name);
+ testSetupAttributes.Add(attribute as TestSetupAttribute);
+ }
+ }
+ foreach (TestSetupAttribute tsa in testSetupAttributes)
+ {
+ //Console.WriteLine("Setup Attribute: {0}.", tsa.GetType().Name);
+ try
+ {
+ tsa.Setup(this);
+ }
+ catch (Exception ex)
+ {
+ this.PrintTestFailureAndAssert(testAdapter.MethodName, "Failed to setup test attribute.", ex);
+ }
+
+ }
+ }
+
+ [SetUp]
+ public virtual void Setup()
+ {
+ Logger.Info(string.Format("Setup TestCase {0} for test {1}.",
+ this.GetType().Name, TestContext.CurrentContext.Test.MethodName));
+ try
+ {
+ // Setup NMS Instances for test.
+ ApplyTestSetupAttributes();
+ }
+ catch(Exception ex)
+ {
+ this.PrintTestFailureAndAssert(GetTestMethodName(),
+ "Failure in setup attributes.", ex);
+ }
+ finally
+ {
+ // Always Setup common test varibles.
+ msgCount = 0;
+ asyncEx = null;
+ StopOnAsyncFailure = true;
+ }
+
+ }
+
+ [TearDown]
+ public virtual void TearDown()
+ {
+ Logger.Info(string.Format("Tear Down TestCase {0} for test {1}.", this.GetType().Name, TestContext.CurrentContext.Test.MethodName));
+ waiter?.Dispose();
+ // restore properties for next test
+ properties = Clone(DefaultProperties);
+ CleanupInstances();
+ Logger.Info(string.Format("Tear Down Finished for TestCase {0} for test {1}.", this.GetType().Name, TestContext.CurrentContext.Test.MethodName));
+ }
+
+ protected string GetMethodName()
+ {
+ StackTrace stackTrace = new StackTrace();
+ return stackTrace.GetFrame(1).GetMethod().Name;
+ }
+
+
+
+ protected string GetTestMethodName()
+ {
+ TestContext.TestAdapter testAdapter = TestContext.CurrentContext.Test;
+ return testAdapter.MethodName;
+ }
+
+ internal virtual void PrintTestFailureAndAssert(string methodDescription, string info, Exception ex)
+ {
+ if (ex is AssertionException || ex is IgnoreException || ex is SuccessException)
+ {
+ throw ex;
+ }
+ StringBuilder sb = new StringBuilder();
+ sb.AppendFormat("@@Test Failed in {0}", methodDescription);
+ if (info != null)
+ {
+ sb.AppendFormat(", where info = {0}", info);
+ }
+ if (ex != null)
+ {
+ sb.AppendFormat(", {0}\n", GetTestException(ex));
+ }
+
+ Logger.Error(sb.ToString());
+ Assert.Fail(sb.ToString());
+ }
+
+ protected virtual string GetTestException(Exception ex)
+ {
+ StringBuilder sb = new StringBuilder();
+ sb.AppendFormat("Encountered an exception:\n\tMessage = {0}\n", ex.Message);
+ sb.AppendFormat("\tType = {0}\n", ex.GetType());
+ sb.AppendFormat("\tStack = {0}\n", ex.StackTrace);
+ if(ex is NMSException)
+ {
+ string errcode = (ex as NMSException).ErrorCode;
+ if (errcode != null && errcode.Length > 0)
+ {
+ sb.AppendFormat("\tErrorCode = {0}\n", errcode);
+ }
+ }
+ if (null != ex.InnerException )
+ {
+ sb.AppendFormat("Inner Exception:\n\t{0}", GetTestException(ex.InnerException));
+ }
+ return sb.ToString();
+
+ }
+
+ internal void PrintTestException(Exception ex)
+ {
+
+ Logger.Error(GetTestException(ex));
+ }
+
+ #region Common Test properties and Components
+
+ protected int msgCount = 0;
+ protected Exception asyncEx = null;
+ protected System.Threading.ManualResetEvent waiter;
+ protected bool StopOnAsyncFailure = true;
+
+ protected virtual void DefaultExceptionListener(Exception ex)
+ {
+ this.PrintTestException(ex);
+ asyncEx = ex;
+ if(waiter!= null && StopOnAsyncFailure)
+ waiter.Set();
+ }
+
+ protected virtual MessageListener CreateListener(int expectedMsgs)
+ {
+ return (m) =>
+ {
+ DefaultMessageListener(m);
+ if (msgCount >= expectedMsgs)
+ {
+ if(waiter != null)
+ {
+ Logger.Debug(string.Format("Received all msgs ({0}) on callback.", msgCount));
+ waiter.Set();
+ }
+
+ }
+ };
+ }
+
+ protected long ExtractMsgId(string nmsMsgId)
+ {
+ long result = -1;
+ int index = nmsMsgId.LastIndexOf(':');
+ if (index >= 0)
+ {
+ try
+ {
+ result = Convert.ToInt64(nmsMsgId.Substring(index + 1));
+ }
+ catch (Exception e)
+ {
+ Logger.Warn("Failed to extract Msg Id from nmsMsgId " + nmsMsgId + " Cause: " + e.Message);
+ }
+ }
+ return result;
+ }
+
+ protected virtual void DefaultMessageListener(IMessage message)
+ {
+ msgCount++;
+ Logger.Debug(string.Format("Received msg {0} on Async Callback.(Count = {1})", message.NMSMessageId, msgCount));
+ }
+
+
+ #endregion
+
+ }
+
+ #endregion
+
+}
http://git-wip-us.apache.org/repos/asf/activemq-nms-amqp/blob/432c9613/src/test/csharp/Test/TestCase/ConnectionTest.cs
----------------------------------------------------------------------
diff --git a/src/test/csharp/Test/TestCase/ConnectionTest.cs b/src/test/csharp/Test/TestCase/ConnectionTest.cs
new file mode 100644
index 0000000..eed728f
--- /dev/null
+++ b/src/test/csharp/Test/TestCase/ConnectionTest.cs
@@ -0,0 +1,154 @@
+/*
+ * 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.Collections.Specialized;
+using NUnit.Framework;
+using NUnit.Framework.Interfaces;
+using Apache.NMS;
+using Apache.NMS.AMQP.Test.Util;
+using Apache.NMS.AMQP.Test.Attribute;
+
+namespace Apache.NMS.AMQP.Test.TestCase
+{
+ [TestFixture]
+ class ConnectionTest : BaseTestCase
+ {
+ protected IConnection Connection;
+ public override void Setup()
+ {
+ base.Setup();
+
+ }
+
+ public override void TearDown()
+ {
+ base.TearDown();
+
+ }
+
+ [Test]
+ [ConnectionSetup]
+ public void TestStart()
+ {
+ using (Connection = GetConnection())
+ {
+ Connection.Start();
+ }
+ }
+
+ [Test]
+ public void TestSetClientIdFromConnectionFactory()
+ {
+
+ StringDictionary props = new StringDictionary();
+ props[NMSPropertyConstants.NMS_CONNECTION_CLIENT_ID] = "foobarr";
+ this.InitConnectedFactoryProperties(props);
+ IConnectionFactory connectionFactory = CreateConnectionFactory();
+
+ IConnection connection = connectionFactory.CreateConnection();
+
+ try
+ {
+ Assert.AreEqual("foobarr", connection.ClientId, "ClientId was not set by Connection Factory.");
+ connection.ClientId = "barfoo";
+ Assert.Fail("Expect Invalid ClientId Exception");
+ }
+ catch (InvalidClientIDException e)
+ {
+ Assert.NotNull(e);
+
+ // success
+ }
+ finally
+ {
+ connection.Close();
+ }
+
+ }
+
+ [Test]
+ [ConnectionSetup]
+ public void TestSetClientIdFromConnection()
+ {
+ using (Connection = GetConnection())
+ {
+ try
+ {
+ Connection.ClientId = "barfoo";
+ Assert.AreEqual("barfoo", Connection.ClientId, "ClientId was not set.");
+ Connection.Start();
+ }
+ catch (NMSException e)
+ {
+ PrintTestFailureAndAssert(GetMethodName(), "Unexpected NMSException", e);
+ }
+
+ }
+ }
+
+ [Test]
+ [ConnectionSetup]
+ public void TestSetClientIdAfterStart()
+ {
+ using (Connection = GetConnection())
+ {
+ try
+ {
+ Connection.ClientId = "barfoo";
+ Connection.Start();
+ Connection.ClientId = "foobar";
+ Assert.Fail("Expected Invalid Operation Exception.");
+ }
+ catch (NMSException e)
+ {
+ Assert.IsTrue((e is InvalidClientIDException), "Expected InvalidClientIDException Got : {0}", e.GetType());
+ // success
+ }
+ }
+ }
+
+ [Test]
+ public void TestConnectWithUsernameAndPassword()
+ {
+ // TODO use test config to grab Client user for broker
+ if(TestConfig.Instance.BrokerUsername == null || TestConfig.Instance.BrokerPassword == null)
+ {
+ Assert.Ignore("Assign Client username and password in {0}", Configuration.CONFIG_FILENAME);
+ }
+
+ string username = TestConfig.Instance.BrokerUsername;
+ string password = TestConfig.Instance.BrokerPassword;
+ StringDictionary props = new StringDictionary();
+ try
+ {
+ this.InitConnectedFactoryProperties(props);
+ IConnectionFactory connectionFactory = CreateConnectionFactory();
+ using (IConnection connection = connectionFactory.CreateConnection(username, password))
+ {
+ connection.Start();
+ }
+
+ }
+ catch (Exception ex)
+ {
+ this.PrintTestFailureAndAssert(this.GetTestMethodName(), "Unexpected Exception", ex);
+ }
+ }
+
+
+ }
+}