From 6826b52d857641507fb616b42f317a83dc0bbb16 Mon Sep 17 00:00:00 2001 From: xinchen Date: Fri, 27 May 2016 13:42:51 -0700 Subject: [PATCH] Unit tests for .Net Core (Xunit) --- .gitattributes | 10 +++ Microsoft.Azure.Amqp.Core/project.json | 1 + .../Amqp/Transport/TlsTransport.cs | 2 +- .../Microsoft.Azure.Amqp.csproj | 1 + .../Properties/AssemblyInfo.cs | 2 - .../Properties/InternalVisible.cs | 4 + global.json | 3 + test/Test.Microsoft.Amqp/Common/AmqpUtils.cs | 15 ++-- test/Test.Microsoft.Amqp/Common/Person.cs | 8 +- .../TestCases/AmqpCodecTests.cs | 74 ++++++++++++++----- test/Test.Microsoft.Azure.Amqp.Core/Assert.cs | 30 ++++++++ .../project.json | 32 ++++++++ 12 files changed, 153 insertions(+), 29 deletions(-) create mode 100644 .gitattributes create mode 100644 Microsoft.Azure.Amqp/Properties/InternalVisible.cs create mode 100644 global.json create mode 100644 test/Test.Microsoft.Azure.Amqp.Core/Assert.cs create mode 100644 test/Test.Microsoft.Azure.Amqp.Core/project.json diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..9551485 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,10 @@ +# Set the default behavior, in case people don't have core.autocrlf set. +* text=auto + +# Denote all files that are truly binary and should not be modified. +*.exe binary +*.dll binary +*.pdb binary +*.ico binary +*.png binary +*.jpg binary diff --git a/Microsoft.Azure.Amqp.Core/project.json b/Microsoft.Azure.Amqp.Core/project.json index c85fdc6..2069668 100644 --- a/Microsoft.Azure.Amqp.Core/project.json +++ b/Microsoft.Azure.Amqp.Core/project.json @@ -31,6 +31,7 @@ "../Microsoft.Azure.Amqp/Exceptions/*.cs", "../Microsoft.Azure.Amqp/Extensions/*.cs", "../Microsoft.Azure.Amqp/Interop/*.cs", + "../Microsoft.Azure.Amqp/Properties/InternalVisible.cs", "../Microsoft.Azure.Amqp/Tracing/*.cs" ] }, diff --git a/Microsoft.Azure.Amqp/Amqp/Transport/TlsTransport.cs b/Microsoft.Azure.Amqp/Amqp/Transport/TlsTransport.cs index 56bc586..a1437d8 100644 --- a/Microsoft.Azure.Amqp/Amqp/Transport/TlsTransport.cs +++ b/Microsoft.Azure.Amqp/Amqp/Transport/TlsTransport.cs @@ -14,7 +14,7 @@ namespace Microsoft.Azure.Amqp.Transport static readonly AsyncCallback onOpenComplete = OnOpenComplete; static readonly AsyncCallback onWriteComplete = OnWriteComplete; static readonly AsyncCallback onReadComplete = OnReadComplete; - const SslProtocols DefaultSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls | SslProtocols.Ssl3; + const SslProtocols DefaultSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls; readonly TransportBase innerTransport; readonly CustomSslStream sslStream; TlsTransportSettings tlsSettings; diff --git a/Microsoft.Azure.Amqp/Microsoft.Azure.Amqp.csproj b/Microsoft.Azure.Amqp/Microsoft.Azure.Amqp.csproj index 0b9ffd1..a0fcbb3 100644 --- a/Microsoft.Azure.Amqp/Microsoft.Azure.Amqp.csproj +++ b/Microsoft.Azure.Amqp/Microsoft.Azure.Amqp.csproj @@ -208,6 +208,7 @@ + ResourcesGeneric.resx True diff --git a/Microsoft.Azure.Amqp/Properties/AssemblyInfo.cs b/Microsoft.Azure.Amqp/Properties/AssemblyInfo.cs index a0dc447..bf9d6f2 100644 --- a/Microsoft.Azure.Amqp/Properties/AssemblyInfo.cs +++ b/Microsoft.Azure.Amqp/Properties/AssemblyInfo.cs @@ -23,5 +23,3 @@ using System.Runtime.InteropServices; // Version information for an assembly follows semantic versioning 1.0.0 (because // NuGet didn't support semver 2.0.0 before VS 2015). See semver.org for details. [assembly: AssemblyInformationalVersion("1.1.2")] - -[assembly: InternalsVisibleTo("Test.Microsoft.Azure.Amqp")] diff --git a/Microsoft.Azure.Amqp/Properties/InternalVisible.cs b/Microsoft.Azure.Amqp/Properties/InternalVisible.cs new file mode 100644 index 0000000..7053e98 --- /dev/null +++ b/Microsoft.Azure.Amqp/Properties/InternalVisible.cs @@ -0,0 +1,4 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("Test.Microsoft.Azure.Amqp")] +[assembly: InternalsVisibleTo("Test.Microsoft.Azure.Amqp.Core")] diff --git a/global.json b/global.json new file mode 100644 index 0000000..cfe251d --- /dev/null +++ b/global.json @@ -0,0 +1,3 @@ +{ + "projects": [ ".", "./test" ] +} \ No newline at end of file diff --git a/test/Test.Microsoft.Amqp/Common/AmqpUtils.cs b/test/Test.Microsoft.Amqp/Common/AmqpUtils.cs index bd72b57..576047c 100644 --- a/test/Test.Microsoft.Amqp/Common/AmqpUtils.cs +++ b/test/Test.Microsoft.Amqp/Common/AmqpUtils.cs @@ -173,7 +173,7 @@ initiator.ConnectAsync(TimeSpan.FromSeconds(120), args); complete.WaitOne(); - complete.Close(); + complete.Dispose(); if (args.Exception != null) { @@ -207,7 +207,7 @@ sb.AppendFormat("{0:X2}", item.Array[i + item.Offset]); } - System.Diagnostics.Trace.WriteLine(sb.ToString()); + System.Diagnostics.Debug.WriteLine(sb.ToString()); } } @@ -220,8 +220,8 @@ } catch (Exception exception) { - System.Diagnostics.Trace.WriteLine(string.Format("Exception occurred at offset {0}", buffer.Offset)); - System.Diagnostics.Trace.WriteLine(exception.ToString()); + System.Diagnostics.Debug.WriteLine(string.Format("Exception occurred at offset {0}", buffer.Offset)); + System.Diagnostics.Debug.WriteLine(exception.ToString()); } } @@ -246,8 +246,11 @@ false); } +#if DOTNET + store.Dispose(); +#else store.Close(); - +#endif if (collection.Count > 0) { return collection[0]; @@ -420,7 +423,7 @@ sb.Append(value); - System.Diagnostics.Trace.WriteLine(sb.ToString()); + System.Diagnostics.Debug.WriteLine(sb.ToString()); } public static async Task ReceiveMessageAsync(this ReceivingAmqpLink link, TimeSpan timeout) diff --git a/test/Test.Microsoft.Amqp/Common/Person.cs b/test/Test.Microsoft.Amqp/Common/Person.cs index cdc6400..d8f0d53 100644 --- a/test/Test.Microsoft.Amqp/Common/Person.cs +++ b/test/Test.Microsoft.Amqp/Common/Person.cs @@ -12,6 +12,8 @@ [KnownType(typeof(Teacher))] class Person { + public Person() { } + public Person(string name) { this.Name = name; @@ -60,7 +62,7 @@ [AmqpContract(Name = "student", Code = 1)] class Student : Person { - Student() : base(null) { } + public Student() : base(null) { } public Student(string name) : base(name) @@ -77,6 +79,8 @@ [AmqpContract(Name = "teacher", Code = 2)] class Teacher : Person { + public Teacher() { } + public Teacher(string name) : base(name) { @@ -123,6 +127,8 @@ this.uuid = uuid; } + public EmployeeId() { } + public int EncodeSize { get { return FixedWidth.UuidEncoded; } diff --git a/test/Test.Microsoft.Amqp/TestCases/AmqpCodecTests.cs b/test/Test.Microsoft.Amqp/TestCases/AmqpCodecTests.cs index dfdfeeb..909ecd3 100644 --- a/test/Test.Microsoft.Amqp/TestCases/AmqpCodecTests.cs +++ b/test/Test.Microsoft.Amqp/TestCases/AmqpCodecTests.cs @@ -12,9 +12,13 @@ using global::Microsoft.Azure.Amqp.Sasl; using global::Microsoft.Azure.Amqp.Serialization; using global::Microsoft.Azure.Amqp.Transaction; +#if !DOTNET using global::Microsoft.VisualStudio.TestTools.UnitTesting; +#endif +#if !DOTNET [TestClass] +#endif public class AmqpCodecTests { bool boolTrue = true; @@ -124,13 +128,11 @@ bin32ValueBin[4] = 0x00; } - - [ClassInitialize] - public static void ClassInitialize(TestContext context) - { - } - - [TestMethod()] +#if DOTNET + [Xunit.Fact] +#else + [TestMethod] +#endif public void AmqpCodecSingleValueTest() { byte[] workBuffer = new byte[2048]; @@ -305,7 +307,11 @@ Assert.IsTrue(str32Utf8 == strValue, "UTF8 string32 string value is not equal."); } - [TestMethod()] +#if DOTNET + [Xunit.Fact] +#else + [TestMethod] +#endif public void AmqpCodecListTest() { byte[] workBuffer = new byte[4096]; @@ -406,7 +412,11 @@ EnsureEqual((IList)described.Value, (IList)described4.Value); } - [TestMethod()] +#if DOTNET + [Xunit.Fact] +#else + [TestMethod] +#endif public void AmqpCodecList0Test() { byte[] list0Bin = new byte[] { 0x45 }; @@ -421,7 +431,11 @@ Assert.IsTrue(list0v.Count == 0, "The list should contain 0 items."); } - [TestMethod()] +#if DOTNET + [Xunit.Fact] +#else + [TestMethod] +#endif public void AmqpCodecMapTest() { byte[] workBuffer = new byte[4096]; @@ -508,7 +522,11 @@ Assert.IsTrue(described.Value.Equals(described1.Value), "Described value 1 value is different"); } - [TestMethod()] +#if DOTNET + [Xunit.Fact] +#else + [TestMethod] +#endif public void AmqpCodecMultipleTest() { byte[] workBuffer = new byte[2048]; @@ -535,7 +553,11 @@ Assert.IsTrue(Multiple.Intersect(threeValues, threeDecoded).Count == 3, "multiple of three symbol values failed"); } - [TestMethod()] +#if DOTNET + [Xunit.Fact] +#else + [TestMethod] +#endif public void AmqpCodecFramingTypeTest() { byte[] workBuffer = new byte[1024 * 16]; @@ -615,7 +637,11 @@ Assert.IsTrue(buffer.Length == 0, "All bytes in the buffer should be consumed"); } - [TestMethod()] +#if DOTNET + [Xunit.Fact] +#else + [TestMethod] +#endif public void AmqpCodecDescribedArrayTest() { int size = AmqpCodec.GetObjectEncodeSize(described5); @@ -625,14 +651,18 @@ Assert.IsTrue(decoded.Descriptor.Equals(described5.Descriptor), "Descriptor value not equal"); string[] original = (string[])described5.Value; string[] array = (string[])decoded.Value; - Assert.IsTrue(original.Length == array.Length, "length not equal {0} != {1}", original.Length, array.Length); + Assert.IsTrue(original.Length == array.Length, string.Format("length not equal {0} != {1}", original.Length, array.Length)); for (int i = 0; i < original.Length; ++i) { - Assert.IsTrue(original[i] == array[i], "index {0}: {1} != {2}", i, original[i], array[i]); + Assert.IsTrue(original[i] == array[i], string.Format("index {0}: {1} != {2}", i, original[i], array[i])); } } - [TestMethod()] +#if DOTNET + [Xunit.Fact] +#else + [TestMethod] +#endif public void AmqpCodecArrayTest() { ArrayTest( @@ -729,7 +759,11 @@ (n1, n2) => { }); } - [TestMethod()] +#if DOTNET + [Xunit.Fact] +#else + [TestMethod] +#endif public void AmqpSerializerListEncodingTest() { Action personValidator = (p1, p2) => @@ -785,7 +819,7 @@ // Inter-op: it should be an AMQP described list as other clients see it stream.Seek(0, SeekOrigin.Begin); - DescribedType dl1 = DescribedEncoding.Decode(new ByteBuffer(stream.GetBuffer(), 0, (int)stream.Length)); + DescribedType dl1 = DescribedEncoding.Decode(new ByteBuffer(stream.ToArray(), 0, (int)stream.Length)); Assert.AreEqual(dl1.Descriptor, 1ul); List lv = dl1.Value as List; Assert.IsNotNull(lv); @@ -841,6 +875,7 @@ Assert.AreEqual(teacher.Classes[205], ((Teacher)p6).Classes[205]); } +#if !DOTNET [TestMethod] public void AmqpExceptionSerializeTest() { @@ -859,6 +894,7 @@ Assert.AreEqual(amqpException1.Error.Description, amqpException2.Error.Description, "Error.Description differs!"); } } +#endif static void EncodeDescribedList(ByteBuffer buffer, object descriptor, params object[] values) { @@ -871,7 +907,7 @@ static void ArrayTest(T[] array, Action validate) { - Trace.WriteLine(string.Format("Array testing for type {0}", typeof(T).ToString())); + Debug.WriteLine(string.Format("Array testing for type {0}", typeof(T).ToString())); byte[] workBuffer = new byte[4096]; ByteBuffer buffer = null; AmqpCodec.EncodeArray(array, buffer = new ByteBuffer(workBuffer)); diff --git a/test/Test.Microsoft.Azure.Amqp.Core/Assert.cs b/test/Test.Microsoft.Azure.Amqp.Core/Assert.cs new file mode 100644 index 0000000..864b1ba --- /dev/null +++ b/test/Test.Microsoft.Azure.Amqp.Core/Assert.cs @@ -0,0 +1,30 @@ +namespace Test.Microsoft.Azure.Amqp +{ + static class Assert + { + public static void IsTrue(bool b, string m = null) + { + Xunit.Assert.True(b, m); + } + + public static void IsFalse(bool b, string m = null) + { + Xunit.Assert.False(b, m); + } + + public static void IsNull(object o, string m = null) + { + Xunit.Assert.True(o == null, m); + } + + public static void IsNotNull(object o, string m = null) + { + Xunit.Assert.True(o != null, m); + } + + public static void AreEqual(object a, object b, string m = null) + { + Xunit.Assert.Equal(a, b); + } + } +} diff --git a/test/Test.Microsoft.Azure.Amqp.Core/project.json b/test/Test.Microsoft.Azure.Amqp.Core/project.json new file mode 100644 index 0000000..b98ec17 --- /dev/null +++ b/test/Test.Microsoft.Azure.Amqp.Core/project.json @@ -0,0 +1,32 @@ +{ + "version": "1.0.0-*", + "testRunner": "xunit", + "dependencies": { + "Microsoft.Azure.Amqp.Core": "1.1.0-*", + "System.Diagnostics.Debug": "4.0.11-rc2-24027", + "xunit": "2.1.0", + "dotnet-test-xunit": "1.0.0-rc2-build10025" + }, + "buildOptions": { + "compile": [ + "Assert.cs", + "../Test.Microsoft.Amqp/Common/*.cs", + "../Test.Microsoft.Amqp/TestCases/AmqpCodecTests.cs" + ], + "define": [ "TRACE", "DOTNET" ] + }, + "frameworks": { + "netcoreapp1.0": { + "dependencies": { + "Microsoft.NETCore.App": { + "type": "platform", + "version": "1.0.0-rc2-3002702" + } + }, + "imports": [ + "dnxcore50", + "portable-net45+win8" + ] + } + } +} \ No newline at end of file