summaryrefslogtreecommitdiffstats
path: root/src/DotNetOAuth.Test/Messaging
diff options
context:
space:
mode:
Diffstat (limited to 'src/DotNetOAuth.Test/Messaging')
-rw-r--r--src/DotNetOAuth.Test/Messaging/DictionaryXmlReaderTests.cs33
-rw-r--r--src/DotNetOAuth.Test/Messaging/MessageSerializerTests.cs32
-rw-r--r--src/DotNetOAuth.Test/Messaging/Reflection/MessageDictionaryTest.cs33
-rw-r--r--src/DotNetOAuth.Test/Messaging/Reflection/MessagePartTests.cs27
4 files changed, 49 insertions, 76 deletions
diff --git a/src/DotNetOAuth.Test/Messaging/DictionaryXmlReaderTests.cs b/src/DotNetOAuth.Test/Messaging/DictionaryXmlReaderTests.cs
deleted file mode 100644
index 54c47f0..0000000
--- a/src/DotNetOAuth.Test/Messaging/DictionaryXmlReaderTests.cs
+++ /dev/null
@@ -1,33 +0,0 @@
-//-----------------------------------------------------------------------
-// <copyright file="DictionaryXmlReaderTests.cs" company="Andrew Arnott">
-// Copyright (c) Andrew Arnott. All rights reserved.
-// </copyright>
-//-----------------------------------------------------------------------
-
-namespace DotNetOAuth.Test.Messaging {
- using System;
- using System.Collections.Generic;
- using System.Xml.Linq;
- using DotNetOAuth.Messaging;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
-
- [TestClass]
- public class DictionaryXmlReaderTests : TestBase {
- [TestMethod, ExpectedException(typeof(ArgumentNullException))]
- public void CreateWithNullRootElement() {
- IComparer<string> fieldSorter = new DataContractMemberComparer(typeof(Mocks.TestMessage));
- DictionaryXmlReader.Create(null, fieldSorter, new Dictionary<string, string>());
- }
-
- [TestMethod, ExpectedException(typeof(ArgumentNullException))]
- public void CreateWithNullDataContractType() {
- DictionaryXmlReader.Create(XName.Get("name", "ns"), null, new Dictionary<string, string>());
- }
-
- [TestMethod, ExpectedException(typeof(ArgumentNullException))]
- public void CreateWithNullFields() {
- IComparer<string> fieldSorter = new DataContractMemberComparer(typeof(Mocks.TestMessage));
- DictionaryXmlReader.Create(XName.Get("name", "ns"), fieldSorter, null);
- }
- }
-}
diff --git a/src/DotNetOAuth.Test/Messaging/MessageSerializerTests.cs b/src/DotNetOAuth.Test/Messaging/MessageSerializerTests.cs
index 162d456..2eb1dbf 100644
--- a/src/DotNetOAuth.Test/Messaging/MessageSerializerTests.cs
+++ b/src/DotNetOAuth.Test/Messaging/MessageSerializerTests.cs
@@ -21,18 +21,6 @@ namespace DotNetOAuth.Test.Messaging {
serializer.Serialize(null);
}
- [TestMethod, ExpectedException(typeof(ArgumentNullException))]
- public void SerializeNullFields() {
- var serializer = MessageSerializer.Get(typeof(Mocks.TestMessage));
- serializer.Serialize(null, new Mocks.TestMessage());
- }
-
- [TestMethod, ExpectedException(typeof(ArgumentNullException))]
- public void SerializeNullMessage() {
- var serializer = MessageSerializer.Get(typeof(Mocks.TestMessage));
- serializer.Serialize(new Dictionary<string, string>(), null);
- }
-
[TestMethod, ExpectedException(typeof(ArgumentException))]
public void GetInvalidMessageType() {
MessageSerializer.Get(typeof(string));
@@ -43,11 +31,6 @@ namespace DotNetOAuth.Test.Messaging {
MessageSerializer.Get(null);
}
- [TestMethod]
- public void GetReturnsSameSerializerTwice() {
- Assert.AreSame(MessageSerializer.Get(typeof(Mocks.TestMessage)), MessageSerializer.Get(typeof(Mocks.TestMessage)));
- }
-
[TestMethod, ExpectedException(typeof(ProtocolException))]
public void SerializeInvalidMessage() {
var serializer = MessageSerializer.Get(typeof(Mocks.TestMessage));
@@ -81,19 +64,6 @@ namespace DotNetOAuth.Test.Messaging {
Assert.IsFalse(actual.ContainsKey("EmptyMember"));
}
- [TestMethod]
- public void SerializeToExistingDictionary() {
- var serializer = MessageSerializer.Get(typeof(Mocks.TestMessage));
- var message = new Mocks.TestMessage { Age = 15, Name = "Andrew" };
- var fields = new Dictionary<string, string>();
- fields["someExtraField"] = "someValue";
- serializer.Serialize(fields, message);
- Assert.AreEqual(4, fields.Count);
- Assert.AreEqual("15", fields["age"]);
- Assert.AreEqual("Andrew", fields["Name"]);
- Assert.AreEqual("someValue", fields["someExtraField"]);
- }
-
[TestMethod, ExpectedException(typeof(ArgumentNullException))]
public void DeserializeNull() {
var serializer = MessageSerializer.Get(typeof(Mocks.TestMessage));
@@ -115,7 +85,7 @@ namespace DotNetOAuth.Test.Messaging {
}
/// <summary>
- /// This tests deserialization of a message that is comprised of [DataMember]'s
+ /// This tests deserialization of a message that is comprised of [MessagePart]'s
/// that are defined in multiple places in the inheritance tree.
/// </summary>
/// <remarks>
diff --git a/src/DotNetOAuth.Test/Messaging/Reflection/MessageDictionaryTest.cs b/src/DotNetOAuth.Test/Messaging/Reflection/MessageDictionaryTest.cs
index 6bbd849..f798bde 100644
--- a/src/DotNetOAuth.Test/Messaging/Reflection/MessageDictionaryTest.cs
+++ b/src/DotNetOAuth.Test/Messaging/Reflection/MessageDictionaryTest.cs
@@ -37,9 +37,6 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
IDictionary<string, string> target = new MessageDictionary(this.message);
Collection<string> expected = new Collection<string> {
this.message.Age.ToString(),
- this.message.EmptyMember,
- null, // this.message.Location.AbsoluteUri, (Location is null)
- this.message.Name,
this.message.Timestamp.ToString(),
};
CollectionAssert<string>.AreEquivalent(expected, target.Values);
@@ -50,7 +47,6 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
target["extra"] = "a";
expected = new Collection<string> {
this.message.Age.ToString(),
- this.message.EmptyMember,
this.message.Location.AbsoluteUri,
this.message.Name,
this.message.Timestamp.ToString(),
@@ -67,13 +63,12 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
IDictionary<string, string> target = new MessageDictionary(this.message);
Collection<string> expected = new Collection<string> {
"age",
- "EmptyMember",
- "Location",
- "Name",
"Timestamp",
};
CollectionAssert<string>.AreEquivalent(expected, target.Keys);
+ this.message.Name = "Andrew";
+ expected.Add("Name");
target["extraField"] = string.Empty;
expected.Add("extraField");
CollectionAssert<string>.AreEquivalent(expected, target.Keys);
@@ -213,9 +208,7 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
public void ContainsKeyTest() {
IDictionary<string, string> target = new MessageDictionary(this.message);
Assert.IsTrue(target.ContainsKey("age"), "Value type declared element should have a key.");
- Assert.IsTrue(target.ContainsKey("Name"), "Null declared element should have a key.");
- target.Remove("Name");
- Assert.IsTrue(target.ContainsKey("Name"), "Removed declared element should still have a key.");
+ Assert.IsFalse(target.ContainsKey("Name"), "Null declared element should NOT have a key.");
Assert.IsFalse(target.ContainsKey("extra"));
target["extra"] = "value";
@@ -258,6 +251,23 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
target.Add("Name", "andrew");
}
+ [TestMethod]
+ public void DefaultReferenceTypeDeclaredPropertyHasNoKey() {
+ IDictionary<string, string> target = new MessageDictionary(this.message);
+ Assert.IsFalse(target.ContainsKey("Name"), "A null value should result in no key.");
+ Assert.IsFalse(target.Keys.Contains("Name"), "A null value should result in no key.");
+ }
+
+ [TestMethod]
+ public void RemoveStructDeclaredProperty() {
+ IDictionary<string, string> target = new MessageDictionary(this.message);
+ this.message.Age = 5;
+ Assert.IsTrue(target.ContainsKey("age"));
+ target.Remove("age");
+ Assert.IsTrue(target.ContainsKey("age"));
+ Assert.AreEqual(0, this.message.Age);
+ }
+
/// <summary>
/// A test for System.Collections.Generic.ICollection&lt;System.Collections.Generic.KeyValuePair&lt;System.String,System.String&lt;&lt;.Remove
/// </summary>
@@ -314,9 +324,8 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
this.message.Name = "Andrew";
this.message.Age = 15;
targetAsDictionary["extra"] = "value";
- int countBeforeClear = targetAsDictionary.Count;
target.Clear();
- Assert.AreEqual(countBeforeClear - 1, target.Count, "Clearing with one extra parameter should reduce count by 1.");
+ Assert.AreEqual(2, target.Count, "Clearing should remove all keys except for declared non-nullable structs.");
Assert.IsFalse(targetAsDictionary.ContainsKey("extra"));
Assert.IsNull(this.message.Name);
Assert.AreEqual(0, this.message.Age);
diff --git a/src/DotNetOAuth.Test/Messaging/Reflection/MessagePartTests.cs b/src/DotNetOAuth.Test/Messaging/Reflection/MessagePartTests.cs
new file mode 100644
index 0000000..646599c
--- /dev/null
+++ b/src/DotNetOAuth.Test/Messaging/Reflection/MessagePartTests.cs
@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using DotNetOAuth.Messaging.Reflection;
+using System.Reflection;
+
+namespace DotNetOAuth.Test.Messaging.Reflection {
+ [TestClass]
+ public class MessagePartTests :MessagingTestBase {
+ class MessageWithNonNullableOptionalStruct {
+ /// <summary>
+ /// Optional structs like int must be nullable for Optional to make sense.
+ /// </summary>
+ [MessagePart(IsRequired = false)]
+ internal int optionalInt;
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentException))]
+ public void OptionalNonNullableStruct() {
+ FieldInfo field = typeof(MessageWithNonNullableOptionalStruct).GetField("optionalInt", BindingFlags.NonPublic | BindingFlags.Instance);
+ MessagePartAttribute attribute = field.GetCustomAttributes(typeof(MessagePartAttribute), true).OfType<MessagePartAttribute>().Single();
+ new MessagePart(field, attribute); // should recognize invalid optional non-nullable struct
+ }
+ }
+}