summaryrefslogtreecommitdiffstats
path: root/src/DotNetOAuth.Test/Messaging/Reflection
diff options
context:
space:
mode:
Diffstat (limited to 'src/DotNetOAuth.Test/Messaging/Reflection')
-rw-r--r--src/DotNetOAuth.Test/Messaging/Reflection/MessageDescriptionTests.cs21
-rw-r--r--src/DotNetOAuth.Test/Messaging/Reflection/MessageDictionaryTests.cs (renamed from src/DotNetOAuth.Test/Messaging/Reflection/MessageDictionaryTest.cs)48
-rw-r--r--src/DotNetOAuth.Test/Messaging/Reflection/MessagePartTests.cs46
-rw-r--r--src/DotNetOAuth.Test/Messaging/Reflection/ValueMappingTests.cs21
4 files changed, 112 insertions, 24 deletions
diff --git a/src/DotNetOAuth.Test/Messaging/Reflection/MessageDescriptionTests.cs b/src/DotNetOAuth.Test/Messaging/Reflection/MessageDescriptionTests.cs
new file mode 100644
index 0000000..04c1df8
--- /dev/null
+++ b/src/DotNetOAuth.Test/Messaging/Reflection/MessageDescriptionTests.cs
@@ -0,0 +1,21 @@
+namespace DotNetOAuth.Test.Messaging.Reflection {
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using Microsoft.VisualStudio.TestTools.UnitTesting;
+ using DotNetOAuth.Messaging.Reflection;
+
+ [TestClass]
+ public class MessageDescriptionTests : MessagingTestBase {
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void GetNull() {
+ MessageDescription.Get(null);
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentException))]
+ public void GetNonMessageType() {
+ MessageDescription.Get(typeof(string));
+ }
+ }
+}
diff --git a/src/DotNetOAuth.Test/Messaging/Reflection/MessageDictionaryTest.cs b/src/DotNetOAuth.Test/Messaging/Reflection/MessageDictionaryTests.cs
index f6b1f38..b1ae0b6 100644
--- a/src/DotNetOAuth.Test/Messaging/Reflection/MessageDictionaryTest.cs
+++ b/src/DotNetOAuth.Test/Messaging/Reflection/MessageDictionaryTests.cs
@@ -15,7 +15,7 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
using System.Xml;
[TestClass]
- public class MessageDictionaryTest : MessagingTestBase {
+ public class MessageDictionaryTests : MessagingTestBase {
private Mocks.TestMessage message;
[TestInitialize]
@@ -34,7 +34,7 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
/// A test for System.Collections.Generic.IDictionary<System.String,System.String>.Values
/// </summary>
[TestMethod]
- public void ValuesTest() {
+ public void Values() {
IDictionary<string, string> target = new MessageDictionary(this.message);
Collection<string> expected = new Collection<string> {
this.message.Age.ToString(),
@@ -60,7 +60,9 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
/// A test for System.Collections.Generic.IDictionary&lt;System.String,System.String>.Keys
/// </summary>
[TestMethod]
- public void KeysTest() {
+ public void Keys() {
+ // We expect that non-nullable value type fields will automatically have keys
+ // in the dictionary for them.
IDictionary<string, string> target = new MessageDictionary(this.message);
Collection<string> expected = new Collection<string> {
"age",
@@ -79,7 +81,7 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
/// A test for System.Collections.Generic.IDictionary&lt;System.String,System.String>.Item
/// </summary>
[TestMethod]
- public void ItemTest() {
+ public void Item() {
IDictionary<string, string> target = new MessageDictionary(this.message);
// Test setting of declared message properties.
@@ -92,13 +94,17 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
target["extra"] = "fun";
Assert.AreEqual("fun", target["extra"]);
Assert.AreEqual("fun", ((IProtocolMessage)this.message).ExtraData["extra"]);
+
+ // Test clearing extra fields
+ target["extra"] = null;
+ Assert.IsFalse(target.ContainsKey("extra"));
}
/// <summary>
/// A test for System.Collections.Generic.ICollection&lt;System.Collections.Generic.KeyValuePair&lt;System.String,System.String&lt;&lt;.IsReadOnly
/// </summary>
[TestMethod]
- public void IsReadOnlyTest() {
+ public void IsReadOnly() {
ICollection<KeyValuePair<string, string>> target = new MessageDictionary(this.message);
Assert.IsFalse(target.IsReadOnly);
}
@@ -107,7 +113,7 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
/// A test for System.Collections.Generic.ICollection&lt;System.Collections.Generic.KeyValuePair&lt;System.String,System.String&lt;&lt;.Count
/// </summary>
[TestMethod]
- public void CountTest() {
+ public void Count() {
ICollection<KeyValuePair<string, string>> target = new MessageDictionary(this.message);
IDictionary<string, string> targetDictionary = (IDictionary<string, string>)target;
Assert.AreEqual(targetDictionary.Keys.Count, target.Count);
@@ -119,7 +125,7 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
/// A test for System.Collections.Generic.IEnumerable&lt;System.Collections.Generic.KeyValuePair&lt;System.String,System.String&lt;&lt;.GetEnumerator
/// </summary>
[TestMethod]
- public void GetEnumeratorTest() {
+ public void GetEnumerator() {
IEnumerable<KeyValuePair<string, string>> target = new MessageDictionary(this.message);
IDictionary<string, string> targetDictionary = (IDictionary<string, string>)target;
var keys = targetDictionary.Keys.GetEnumerator();
@@ -142,7 +148,7 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
}
[TestMethod]
- public void GetEnumeratorUntypedTest() {
+ public void GetEnumeratorUntyped() {
IEnumerable target = new MessageDictionary(this.message);
IDictionary<string, string> targetDictionary = (IDictionary<string, string>)target;
var keys = targetDictionary.Keys.GetEnumerator();
@@ -169,7 +175,7 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
/// A test for System.Collections.Generic.IDictionary&lt;System.String,System.String>.TryGetValue
/// </summary>
[TestMethod]
- public void TryGetValueTest() {
+ public void TryGetValue() {
IDictionary<string, string> target = new MessageDictionary(this.message);
this.message.Name = "andrew";
string name;
@@ -206,7 +212,7 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
/// A test for System.Collections.Generic.IDictionary&lt;System.String,System.String>.ContainsKey
/// </summary>
[TestMethod]
- public void ContainsKeyTest() {
+ public void ContainsKey() {
IDictionary<string, string> target = new MessageDictionary(this.message);
Assert.IsTrue(target.ContainsKey("age"), "Value type declared element should have a key.");
Assert.IsFalse(target.ContainsKey("Name"), "Null declared element should NOT have a key.");
@@ -220,7 +226,7 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
/// A test for System.Collections.Generic.IDictionary&lt;System.String,System.String&gt;.Add
/// </summary>
[TestMethod]
- public void AddByKeyAndValueTest() {
+ public void AddByKeyAndValue() {
IDictionary<string, string> target = new MessageDictionary(this.message);
target.Add("extra", "value");
Assert.IsTrue(target.Contains(new KeyValuePair<string, string>("extra", "value")));
@@ -228,25 +234,31 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
Assert.AreEqual("Andrew", this.message.Name);
}
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void AddNullValue() {
+ IDictionary<string, string> target = new MessageDictionary(this.message);
+ target.Add("extra", null);
+ }
+
/// <summary>
/// A test for System.Collections.Generic.ICollection&lt;System.Collections.Generic.KeyValuePair&lt;System.String,System.String&lt;&lt;.Add
/// </summary>
[TestMethod]
- public void AddByKeyValuePairTest() {
+ public void AddByKeyValuePair() {
IDictionary<string, string> target = new MessageDictionary(this.message);
target.Add(new KeyValuePair<string, string>("extra", "value"));
Assert.IsTrue(target.Contains(new KeyValuePair<string, string>("extra", "value")));
}
[TestMethod, ExpectedException(typeof(ArgumentException))]
- public void AddExtraFieldThatAlreadyExistsTest() {
+ public void AddExtraFieldThatAlreadyExists() {
IDictionary<string, string> target = new MessageDictionary(this.message);
target.Add("extra", "value");
target.Add("extra", "value");
}
[TestMethod, ExpectedException(typeof(ArgumentException))]
- public void AddDeclaredValueThatAlreadyExistsTest() {
+ public void AddDeclaredValueThatAlreadyExists() {
IDictionary<string, string> target = new MessageDictionary(this.message);
target.Add("Name", "andrew");
target.Add("Name", "andrew");
@@ -273,7 +285,7 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
/// A test for System.Collections.Generic.ICollection&lt;System.Collections.Generic.KeyValuePair&lt;System.String,System.String&lt;&lt;.Remove
/// </summary>
[TestMethod]
- public void RemoveByKeyValuePairTest() {
+ public void RemoveByKeyValuePair() {
ICollection<KeyValuePair<string, string>> target = new MessageDictionary(this.message);
this.message.Name = "Andrew";
Assert.IsFalse(target.Remove(new KeyValuePair<string, string>("Name", "andrew")));
@@ -286,7 +298,7 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
/// A test for System.Collections.Generic.ICollection&lt;System.Collections.Generic.KeyValuePair&lt;System.String,System.String&lt;&lt;.CopyTo
/// </summary>
[TestMethod]
- public void CopyToTest() {
+ public void CopyTo() {
ICollection<KeyValuePair<string, string>> target = new MessageDictionary(this.message);
IDictionary<string, string> targetAsDictionary = ((IDictionary<string, string>)target);
KeyValuePair<string, string>[] array = new KeyValuePair<string, string>[target.Count + 1];
@@ -303,7 +315,7 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
/// A test for System.Collections.Generic.ICollection&lt;System.Collections.Generic.KeyValuePair&lt;System.String,System.String&lt;&lt;.Contains
/// </summary>
[TestMethod]
- public void ContainsKeyValuePairTest() {
+ public void ContainsKeyValuePair() {
ICollection<KeyValuePair<string, string>> target = new MessageDictionary(this.message);
IDictionary<string, string> targetAsDictionary = ((IDictionary<string, string>)target);
Assert.IsFalse(target.Contains(new KeyValuePair<string, string>("age", "1")));
@@ -319,7 +331,7 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
/// A test for System.Collections.Generic.ICollection&lt;System.Collections.Generic.KeyValuePair&lt;System.String,System.String&lt;&lt;.Clear
/// </summary>
[TestMethod]
- public void ClearTest() {
+ public void Clear() {
ICollection<KeyValuePair<string, string>> target = new MessageDictionary(this.message);
IDictionary<string, string> targetAsDictionary = ((IDictionary<string, string>)target);
this.message.Name = "Andrew";
diff --git a/src/DotNetOAuth.Test/Messaging/Reflection/MessagePartTests.cs b/src/DotNetOAuth.Test/Messaging/Reflection/MessagePartTests.cs
index bfc8192..5c032e6 100644
--- a/src/DotNetOAuth.Test/Messaging/Reflection/MessagePartTests.cs
+++ b/src/DotNetOAuth.Test/Messaging/Reflection/MessagePartTests.cs
@@ -5,18 +5,19 @@ using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DotNetOAuth.Messaging.Reflection;
using System.Reflection;
+using DotNetOAuth.Test.Mocks;
namespace DotNetOAuth.Test.Messaging.Reflection {
[TestClass]
- public class MessagePartTests :MessagingTestBase {
- class MessageWithNonNullableOptionalStruct {
+ public class MessagePartTests :MessagingTestBase {
+ class MessageWithNonNullableOptionalStruct : TestMessage {
/// <summary>
/// Optional structs like int must be nullable for Optional to make sense.
/// </summary>
[MessagePart(IsRequired = false)]
internal int optionalInt = 0;
}
- class MessageWithNonNullableRequiredStruct {
+ class MessageWithNonNullableRequiredStruct : TestMessage {
/// <summary>
/// This should work because a required field will always have a value so it
/// need not be nullable.
@@ -24,7 +25,7 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
[MessagePart(IsRequired = true)]
internal int optionalInt = 0;
}
- class MessageWithNullableOptionalStruct {
+ class MessageWithNullableOptionalStruct : TestMessage {
/// <summary>
/// Optional structs like int must be nullable for Optional to make sense.
/// </summary>
@@ -47,10 +48,43 @@ namespace DotNetOAuth.Test.Messaging.Reflection {
ParameterizedMessageTypeTest(typeof(MessageWithNullableOptionalStruct));
}
- private void ParameterizedMessageTypeTest(Type messageType) {
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void CtorNullMember() {
+ new MessagePart(null, new MessagePartAttribute());
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void CtorNullAttribute() {
+ FieldInfo field = typeof(MessageWithNullableOptionalStruct).GetField("optionalInt", BindingFlags.NonPublic | BindingFlags.Instance);
+ new MessagePart(field, null);
+ }
+
+ [TestMethod]
+ public void SetValue() {
+ var message = new MessageWithNonNullableRequiredStruct();
+ MessagePart part = ParameterizedMessageTypeTest(message.GetType());
+ part.SetValue(message, "5");
+ Assert.AreEqual(5, message.optionalInt);
+ }
+
+ [TestMethod]
+ public void GetValue() {
+ var message = new MessageWithNonNullableRequiredStruct();
+ message.optionalInt = 8;
+ MessagePart part = ParameterizedMessageTypeTest(message.GetType());
+ Assert.AreEqual("8", part.GetValue(message));
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentException))]
+ public void NonFieldOrPropertyMember() {
+ MemberInfo method = typeof(MessageWithNullableOptionalStruct).GetMethod("Equals", BindingFlags.Public | BindingFlags.Instance);
+ new MessagePart(method, new MessagePartAttribute());
+ }
+
+ private MessagePart ParameterizedMessageTypeTest(Type messageType) {
FieldInfo field = messageType.GetField("optionalInt", BindingFlags.NonPublic | BindingFlags.Instance);
MessagePartAttribute attribute = field.GetCustomAttributes(typeof(MessagePartAttribute), true).OfType<MessagePartAttribute>().Single();
- new MessagePart(field, attribute);
+ return new MessagePart(field, attribute);
}
}
}
diff --git a/src/DotNetOAuth.Test/Messaging/Reflection/ValueMappingTests.cs b/src/DotNetOAuth.Test/Messaging/Reflection/ValueMappingTests.cs
new file mode 100644
index 0000000..9142a0c
--- /dev/null
+++ b/src/DotNetOAuth.Test/Messaging/Reflection/ValueMappingTests.cs
@@ -0,0 +1,21 @@
+namespace DotNetOAuth.Test.Messaging.Reflection {
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using Microsoft.VisualStudio.TestTools.UnitTesting;
+ using DotNetOAuth.Messaging.Reflection;
+
+ [TestClass]
+ public class ValueMappingTests {
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void CtorNullToString() {
+ new ValueMapping(null, str => new object());
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void CtorNullToObject() {
+ new ValueMapping(obj => obj.ToString(), null);
+ }
+ }
+}