diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2008-09-20 12:09:56 -0700 |
---|---|---|
committer | Andrew <andrewarnott@gmail.com> | 2008-09-20 12:09:56 -0700 |
commit | ccd062ccfaa33aed1da1e39e62c07348392f4367 (patch) | |
tree | 870eea6e76aad96882d972f8a2cbeb0dfa380108 | |
parent | 74784effd199c28cae39676082b988454a7b37fb (diff) | |
download | DotNetOpenAuth-ccd062ccfaa33aed1da1e39e62c07348392f4367.zip DotNetOpenAuth-ccd062ccfaa33aed1da1e39e62c07348392f4367.tar.gz DotNetOpenAuth-ccd062ccfaa33aed1da1e39e62c07348392f4367.tar.bz2 |
Worked on test coverage.
-rw-r--r-- | src/DotNetOAuth.Test/DotNetOAuth.Test.csproj | 4 | ||||
-rw-r--r-- | src/DotNetOAuth.Test/Messaging/ChannelTests.cs | 33 | ||||
-rw-r--r-- | src/DotNetOAuth.Test/Messaging/MessageSerializerTests.cs | 23 | ||||
-rw-r--r-- | src/DotNetOAuth.Test/Messaging/MessagingTestBase.cs | 102 | ||||
-rw-r--r-- | src/DotNetOAuth.Test/Messaging/Reflection/MessageDescriptionTests.cs | 21 | ||||
-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.cs | 46 | ||||
-rw-r--r-- | src/DotNetOAuth.Test/Messaging/Reflection/ValueMappingTests.cs | 21 | ||||
-rw-r--r-- | src/DotNetOAuth.Test/Mocks/TestMessage.cs | 4 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/Reflection/MessageDescription.cs | 15 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/Reflection/MessagePart.cs | 6 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/Reflection/MessagePartAttribute.cs | 14 |
12 files changed, 233 insertions, 104 deletions
diff --git a/src/DotNetOAuth.Test/DotNetOAuth.Test.csproj b/src/DotNetOAuth.Test/DotNetOAuth.Test.csproj index b65b5c6..dfa7fef 100644 --- a/src/DotNetOAuth.Test/DotNetOAuth.Test.csproj +++ b/src/DotNetOAuth.Test/DotNetOAuth.Test.csproj @@ -60,7 +60,8 @@ <ItemGroup>
<Compile Include="Messaging\CollectionAssert.cs" />
<Compile Include="Messaging\MessageSerializerTests.cs" />
- <Compile Include="Messaging\Reflection\MessageDictionaryTest.cs" />
+ <Compile Include="Messaging\Reflection\MessageDescriptionTests.cs" />
+ <Compile Include="Messaging\Reflection\MessageDictionaryTests.cs" />
<Compile Include="Messaging\MessagingTestBase.cs" />
<Compile Include="Messaging\MessagingUtilitiesTests.cs" />
<Compile Include="Messaging\ChannelTests.cs" />
@@ -68,6 +69,7 @@ <Compile Include="Messaging\ProtocolExceptionTests.cs" />
<Compile Include="Messaging\Bindings\StandardExpirationBindingElementTests.cs" />
<Compile Include="Messaging\Reflection\MessagePartTests.cs" />
+ <Compile Include="Messaging\Reflection\ValueMappingTests.cs" />
<Compile Include="Mocks\MockTransformationBindingElement.cs" />
<Compile Include="Mocks\MockReplayProtectionBindingElement.cs" />
<Compile Include="Mocks\TestBaseMessage.cs" />
diff --git a/src/DotNetOAuth.Test/Messaging/ChannelTests.cs b/src/DotNetOAuth.Test/Messaging/ChannelTests.cs index e9c39ee..7fef82d 100644 --- a/src/DotNetOAuth.Test/Messaging/ChannelTests.cs +++ b/src/DotNetOAuth.Test/Messaging/ChannelTests.cs @@ -65,19 +65,21 @@ namespace DotNetOAuth.Test.Messaging { [TestMethod]
public void SendIndirectMessage301Get() {
- IProtocolMessage message = new TestDirectedMessage(MessageTransport.Indirect) {
- Age = 15,
- Name = "Andrew",
- Location = new Uri("http://host/path"),
- Recipient = new Uri("http://provider/path"),
- };
+ TestDirectedMessage message = new TestDirectedMessage(MessageTransport.Indirect);
+ GetStandardTestMessage(FieldFill.CompleteBeforeBindings, message);
+ message.Recipient = new Uri("http://provider/path");
+ var expected = GetStandardTestFields(FieldFill.CompleteBeforeBindings);
+
this.Channel.Send(message);
Response response = this.Channel.DequeueIndirectOrResponseMessage();
Assert.AreEqual(HttpStatusCode.Redirect, response.Status);
StringAssert.StartsWith(response.Headers[HttpResponseHeader.Location], "http://provider/path");
- StringAssert.Contains(response.Headers[HttpResponseHeader.Location], "age=15");
- StringAssert.Contains(response.Headers[HttpResponseHeader.Location], "Name=Andrew");
- StringAssert.Contains(response.Headers[HttpResponseHeader.Location], "Location=http%3a%2f%2fhost%2fpath");
+ foreach (var pair in expected) {
+ string key = HttpUtility.UrlEncode(pair.Key);
+ string value = HttpUtility.UrlEncode(pair.Value);
+ string substring = string.Format("{0}={1}", key, value);
+ StringAssert.Contains(response.Headers[HttpResponseHeader.Location], substring);
+ }
}
[TestMethod, ExpectedException(typeof(ArgumentNullException))]
@@ -199,16 +201,14 @@ namespace DotNetOAuth.Test.Messaging { [TestMethod]
public void ReadFromRequestWithContext() {
// TODO: make this a request with a message in it.
- var fields = new Dictionary<string, string>() {
- { "age", "15" },
- { "Timestamp", XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc) },
- };
+ var fields = GetStandardTestFields(FieldFill.AllRequired);
+ TestMessage expectedMessage = GetStandardTestMessage(FieldFill.AllRequired);
HttpRequest request = new HttpRequest("somefile", "http://someurl", MessagingUtilities.CreateQueryString(fields));
HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter()));
IProtocolMessage message = this.Channel.ReadFromRequest();
Assert.IsNotNull(message);
Assert.IsInstanceOfType(message, typeof(TestMessage));
- Assert.AreEqual(15, ((TestMessage)message).Age);
+ Assert.AreEqual(expectedMessage.Age, ((TestMessage)message).Age);
}
[TestMethod, ExpectedException(typeof(InvalidOperationException))]
@@ -309,10 +309,7 @@ namespace DotNetOAuth.Test.Messaging { [TestMethod, ExpectedException(typeof(ProtocolException))]
public void IncomingMessageMissingRequiredParameters() {
- var fields = new Dictionary<string, string> {
- { "age", "15" },
- // missing required Timestamp parameter.
- };
+ var fields = GetStandardTestFields(FieldFill.IdentifiableButNotAllRequired);
this.Channel.ReadFromRequest(CreateHttpRequestInfo("GET", fields));
}
}
diff --git a/src/DotNetOAuth.Test/Messaging/MessageSerializerTests.cs b/src/DotNetOAuth.Test/Messaging/MessageSerializerTests.cs index f034981..a41753b 100644 --- a/src/DotNetOAuth.Test/Messaging/MessageSerializerTests.cs +++ b/src/DotNetOAuth.Test/Messaging/MessageSerializerTests.cs @@ -15,7 +15,7 @@ namespace DotNetOAuth.Test.Messaging { /// Tests for the <see cref="MessageSerializer"/> class.
/// </summary>
[TestClass()]
- public class MessageSerializerTests : TestBase {
+ public class MessageSerializerTests : MessagingTestBase {
[TestMethod, ExpectedException(typeof(ArgumentNullException))]
public void SerializeNull() {
var serializer = MessageSerializer.Get(typeof(Mocks.TestMessage));
@@ -35,12 +35,8 @@ namespace DotNetOAuth.Test.Messaging { [TestMethod()]
public void SerializeTest() {
var serializer = MessageSerializer.Get(typeof(Mocks.TestMessage));
- var message = new Mocks.TestMessage {
- Age = 15,
- Name = "Andrew",
- Location = new Uri("http://localhost"),
- Timestamp = DateTime.SpecifyKind(DateTime.Parse("1/1/1990"), DateTimeKind.Utc),
- };
+ var message = GetStandardTestMessage(FieldFill.CompleteBeforeBindings);
+ var expected = GetStandardTestFields(FieldFill.CompleteBeforeBindings);
IDictionary<string, string> actual = serializer.Serialize(message);
Assert.AreEqual(4, actual.Count);
@@ -49,10 +45,10 @@ namespace DotNetOAuth.Test.Messaging { Assert.IsTrue(actual.ContainsKey("age"));
// Test contents of dictionary
- Assert.AreEqual("15", actual["age"]);
- Assert.AreEqual("Andrew", actual["Name"]);
- Assert.AreEqual("http://localhost/", actual["Location"]);
- Assert.AreEqual("1990-01-01T00:00:00Z", actual["Timestamp"]);
+ Assert.AreEqual(expected["age"], actual["age"]);
+ Assert.AreEqual(expected["Name"], actual["Name"]);
+ Assert.AreEqual(expected["Location"], actual["Location"]);
+ Assert.AreEqual(expected["Timestamp"], actual["Timestamp"]);
Assert.IsFalse(actual.ContainsKey("EmptyMember"));
}
@@ -126,9 +122,8 @@ namespace DotNetOAuth.Test.Messaging { [TestMethod, ExpectedException(typeof(ProtocolException))]
public void DeserializeInvalidMessage() {
var serializer = MessageSerializer.Get(typeof(Mocks.TestMessage));
- Dictionary<string, string> fields = new Dictionary<string, string>(StringComparer.Ordinal);
- // Set an disallowed value.
- fields["age"] = "-1";
+ var fields = GetStandardTestFields(FieldFill.AllRequired);
+ fields["age"] = "-1"; // Set an disallowed value.
serializer.Deserialize(fields);
}
}
diff --git a/src/DotNetOAuth.Test/Messaging/MessagingTestBase.cs b/src/DotNetOAuth.Test/Messaging/MessagingTestBase.cs index e75087f..34cf1df 100644 --- a/src/DotNetOAuth.Test/Messaging/MessagingTestBase.cs +++ b/src/DotNetOAuth.Test/Messaging/MessagingTestBase.cs @@ -85,31 +85,89 @@ namespace DotNetOAuth.Test { return new TestChannel(typeProvider, bindingElements.ToArray());
}
+ internal enum FieldFill {
+ /// <summary>
+ /// An empty dictionary is returned.
+ /// </summary>
+ None,
+
+ /// <summary>
+ /// Only enough fields for the <see cref="TestMessageTypeProvider"/>
+ /// to identify the message are included.
+ /// </summary>
+ IdentifiableButNotAllRequired,
+
+ /// <summary>
+ /// All fields marked as required are included.
+ /// </summary>
+ AllRequired,
+
+ /// <summary>
+ /// All user-fillable fields in the message, leaving out those whose
+ /// values are to be set by channel binding elements.
+ /// </summary>
+ CompleteBeforeBindings,
+ }
+
+ internal static IDictionary<string, string> GetStandardTestFields(FieldFill fill) {
+ TestMessage expectedMessage = GetStandardTestMessage(fill);
+
+ var fields = new Dictionary<string, string>();
+ if (fill >= FieldFill.IdentifiableButNotAllRequired) {
+ fields.Add("age", expectedMessage.Age.ToString());
+ }
+ if (fill >= FieldFill.AllRequired) {
+ fields.Add("Timestamp", XmlConvert.ToString(expectedMessage.Timestamp, XmlDateTimeSerializationMode.Utc));
+ }
+ if (fill >= FieldFill.CompleteBeforeBindings) {
+ fields.Add("Name", expectedMessage.Name);
+ fields.Add("Location", expectedMessage.Location.AbsoluteUri);
+ }
+
+ return fields;
+ }
+
+ internal static TestMessage GetStandardTestMessage(FieldFill fill) {
+ TestMessage message = new TestMessage();
+ GetStandardTestMessage(fill, message);
+ return message;
+ }
+
+ internal static void GetStandardTestMessage(FieldFill fill, TestMessage message) {
+ if (message == null) {
+ throw new ArgumentNullException("message");
+ }
+
+ if (fill >= FieldFill.IdentifiableButNotAllRequired) {
+ message.Age = 15;
+ }
+ if (fill >= FieldFill.AllRequired) {
+ message.Timestamp = DateTime.SpecifyKind(DateTime.Parse("9/19/2008 8 AM"), DateTimeKind.Utc);
+ }
+ if (fill >= FieldFill.CompleteBeforeBindings) {
+ message.Name = "Andrew";
+ message.Location = new Uri("http://localtest/path");
+ }
+ }
+
internal void ParameterizedReceiveTest(string method) {
- var fields = new Dictionary<string, string> {
- { "age", "15" },
- { "Name", "Andrew" },
- { "Location", "http://hostb/pathB" },
- { "Timestamp", XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc) },
- };
+ var fields = GetStandardTestFields(FieldFill.CompleteBeforeBindings);
+ TestMessage expectedMessage = GetStandardTestMessage(FieldFill.CompleteBeforeBindings); ;
+
IProtocolMessage requestMessage = this.Channel.ReadFromRequest(CreateHttpRequestInfo(method, fields));
Assert.IsNotNull(requestMessage);
Assert.IsInstanceOfType(requestMessage, typeof(TestMessage));
- TestMessage testMessage = (TestMessage)requestMessage;
- Assert.AreEqual(15, testMessage.Age);
- Assert.AreEqual("Andrew", testMessage.Name);
- Assert.AreEqual("http://hostb/pathB", testMessage.Location.AbsoluteUri);
+ TestMessage actualMessage = (TestMessage)requestMessage;
+ Assert.AreEqual(expectedMessage.Age, actualMessage.Age);
+ Assert.AreEqual(expectedMessage.Name, actualMessage.Name);
+ Assert.AreEqual(expectedMessage.Location, actualMessage.Location);
}
internal void ParameterizedReceiveProtectedTest(DateTime? utcCreatedDate, bool invalidSignature) {
- var fields = new Dictionary<string, string> {
- { "age", "15" },
- { "Name", "Andrew" },
- { "Location", "http://hostb/pathB" },
- { "Signature", invalidSignature ? "badsig" : MockSigningBindingElement.MessageSignature },
- { "Nonce", "someNonce" },
- { "Timestamp", XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc) },
- };
+ TestMessage expectedMessage = GetStandardTestMessage(FieldFill.CompleteBeforeBindings); ;
+ var fields = GetStandardTestFields(FieldFill.CompleteBeforeBindings);
+ fields.Add("Signature", invalidSignature ? "badsig" : MockSigningBindingElement.MessageSignature);
+ fields.Add("Nonce", "someNonce");
if (utcCreatedDate.HasValue) {
utcCreatedDate = DateTime.Parse(utcCreatedDate.Value.ToUniversalTime().ToString()); // round off the milliseconds so comparisons work later
fields.Add("created_on", XmlConvert.ToString(utcCreatedDate.Value, XmlDateTimeSerializationMode.Utc));
@@ -117,10 +175,10 @@ namespace DotNetOAuth.Test { IProtocolMessage requestMessage = this.Channel.ReadFromRequest(CreateHttpRequestInfo("GET", fields));
Assert.IsNotNull(requestMessage);
Assert.IsInstanceOfType(requestMessage, typeof(TestSignedDirectedMessage));
- TestSignedDirectedMessage testMessage = (TestSignedDirectedMessage)requestMessage;
- Assert.AreEqual(15, testMessage.Age);
- Assert.AreEqual("Andrew", testMessage.Name);
- Assert.AreEqual("http://hostb/pathB", testMessage.Location.AbsoluteUri);
+ TestSignedDirectedMessage actualMessage = (TestSignedDirectedMessage)requestMessage;
+ Assert.AreEqual(expectedMessage.Age, actualMessage.Age);
+ Assert.AreEqual(expectedMessage.Name, actualMessage.Name);
+ Assert.AreEqual(expectedMessage.Location, actualMessage.Location);
if (utcCreatedDate.HasValue) {
IExpiringProtocolMessage expiringMessage = (IExpiringProtocolMessage)requestMessage;
Assert.AreEqual(utcCreatedDate.Value, expiringMessage.UtcCreationDate);
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<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<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<System.Collections.Generic.KeyValuePair<System.String,System.String<<.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<System.Collections.Generic.KeyValuePair<System.String,System.String<<.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<System.Collections.Generic.KeyValuePair<System.String,System.String<<.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<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<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<System.String,System.String>.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<System.Collections.Generic.KeyValuePair<System.String,System.String<<.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<System.Collections.Generic.KeyValuePair<System.String,System.String<<.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<System.Collections.Generic.KeyValuePair<System.String,System.String<<.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<System.Collections.Generic.KeyValuePair<System.String,System.String<<.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<System.Collections.Generic.KeyValuePair<System.String,System.String<<.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);
+ }
+ }
+}
diff --git a/src/DotNetOAuth.Test/Mocks/TestMessage.cs b/src/DotNetOAuth.Test/Mocks/TestMessage.cs index 1bd94bf..aede676 100644 --- a/src/DotNetOAuth.Test/Mocks/TestMessage.cs +++ b/src/DotNetOAuth.Test/Mocks/TestMessage.cs @@ -26,11 +26,11 @@ namespace DotNetOAuth.Test.Mocks { [MessagePart(Name = "age", IsRequired = true)]
public int Age { get; set; }
- [MessagePart]
+ [MessagePart("Name")]
public string Name { get; set; }
[MessagePart]
public string EmptyMember { get; set; }
- [MessagePart]
+ [MessagePart(Name = null)] // null name tests that Location is still the name.
public Uri Location { get; set; }
[MessagePart(IsRequired = true)]
public DateTime Timestamp { get; set; }
diff --git a/src/DotNetOAuth/Messaging/Reflection/MessageDescription.cs b/src/DotNetOAuth/Messaging/Reflection/MessageDescription.cs index 6b898bb..9442f15 100644 --- a/src/DotNetOAuth/Messaging/Reflection/MessageDescription.cs +++ b/src/DotNetOAuth/Messaging/Reflection/MessageDescription.cs @@ -10,6 +10,7 @@ namespace DotNetOAuth.Messaging.Reflection { using System.Linq;
using System.Reflection;
using System.Globalization;
+ using System.Diagnostics;
internal class MessageDescription {
private static Dictionary<Type, MessageDescription> reflectedMessageTypes = new Dictionary<Type,MessageDescription>();
@@ -17,12 +18,14 @@ namespace DotNetOAuth.Messaging.Reflection { private Dictionary<string, MessagePart> mapping;
private MessageDescription(Type messageType) {
- if (messageType == null) {
- throw new ArgumentNullException("messageType");
- }
+ Debug.Assert(messageType != null, "messageType == null");
if (!typeof(IProtocolMessage).IsAssignableFrom(messageType)) {
- throw new ArgumentOutOfRangeException(); // TODO: better message
+ throw new ArgumentException(string.Format(
+ CultureInfo.CurrentCulture,
+ MessagingStrings.UnexpectedType,
+ typeof(IProtocolMessage),
+ messageType));
}
this.messageType = messageType;
@@ -46,10 +49,6 @@ namespace DotNetOAuth.Messaging.Reflection { return result;
}
- internal Type MessageType {
- get { return this.messageType; }
- }
-
internal IDictionary<string, MessagePart> Mapping {
get { return this.mapping; }
}
diff --git a/src/DotNetOAuth/Messaging/Reflection/MessagePart.cs b/src/DotNetOAuth/Messaging/Reflection/MessagePart.cs index 4d2ebe6..3f64e63 100644 --- a/src/DotNetOAuth/Messaging/Reflection/MessagePart.cs +++ b/src/DotNetOAuth/Messaging/Reflection/MessagePart.cs @@ -38,7 +38,11 @@ namespace DotNetOAuth.Messaging.Reflection { this.field = member as FieldInfo;
this.property = member as PropertyInfo;
if (this.field == null && this.property == null) {
- throw new ArgumentOutOfRangeException("member"); // TODO: add descriptive message
+ throw new ArgumentException(string.Format(
+ CultureInfo.CurrentCulture,
+ MessagingStrings.UnexpectedType,
+ typeof(FieldInfo).Name + ", " + typeof(PropertyInfo).Name,
+ member.GetType().Name), "member");
}
if (attribute == null) {
diff --git a/src/DotNetOAuth/Messaging/Reflection/MessagePartAttribute.cs b/src/DotNetOAuth/Messaging/Reflection/MessagePartAttribute.cs index e5c429e..92960a8 100644 --- a/src/DotNetOAuth/Messaging/Reflection/MessagePartAttribute.cs +++ b/src/DotNetOAuth/Messaging/Reflection/MessagePartAttribute.cs @@ -29,19 +29,5 @@ namespace DotNetOAuth.Messaging.Reflection { public ProtectionLevel Signed { get; set; }
public bool IsRequired { get; set; }
-
- internal void Initialize(MemberInfo member) {
- if (member == null) {
- throw new ArgumentNullException("member");
- }
-
- if (!this.initialized) {
- if (String.IsNullOrEmpty(this.Name)) {
- this.Name = member.Name;
- }
-
- this.initialized = true;
- }
- }
}
}
|