update MEF to preview 9
[mcs.git] / class / System.ComponentModel.Composition / Tests / UnitTestFramework / System / UnitTesting / ExceptionAssert.cs
blobc75dcc759ba27fe585ee539d32db950cfa45ef48
1 // -----------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // -----------------------------------------------------------------------
4 using System;
5 using Microsoft.VisualStudio.TestTools.UnitTesting;
6 using System.Runtime.Serialization;
8 namespace System.UnitTesting
10 public static class ExceptionAssert
12 // NOTE: To catch state corrupting exceptions, it by design that
13 // the ThrowsXXX methods retry by default. To prevent this in a
14 // test, simply use one of the overloads that takes a RetryMode.
16 /// <summary>
17 /// Verifies that the exception has the default message generated by the base Exception class.
18 /// </summary>
19 public static void HasDefaultMessage(Exception exception)
21 Assert.IsNotNull(exception);
23 // Exception of type '[typename]' was thrown
24 StringAssert.Contains(exception.Message, exception.GetType().FullName);
27 /// <summary>
28 /// Verifies that the specified action throws a SerializationException.
29 /// </summary>
30 public static SerializationException ThrowsSerialization(string memberName, Action action)
32 var exception = Throws<SerializationException>(RetryMode.Retry, action, (actual, retryCount) =>
34 AssertSerializationMemberName(memberName, actual, retryCount);
35 });
37 return exception;
40 /// <summary>
41 /// Verifies that the specified action throws an ObjectDisposedException.
42 /// </summary>
43 public static ObjectDisposedException ThrowsDisposed(object instance, Action action)
45 var exception = Throws<ObjectDisposedException>(RetryMode.Retry, action, (actual, retryCount) =>
47 AssertObjectDisposed(instance, actual, retryCount);
48 });
50 return exception;
53 /// <summary>
54 /// Verifies that the specified action throws an ArgumentNullException.
55 /// </summary>
56 public static ArgumentNullException ThrowsArgumentNull(string parameterName, Action action)
58 return ThrowsArgument<ArgumentNullException>(parameterName, action);
61 /// <summary>
62 /// Verifies that the specified action throws an ArgumentException.
63 /// </summary>
64 public static ArgumentException ThrowsArgument(string parameterName, Action action)
66 return ThrowsArgument<ArgumentException>(parameterName, action);
69 /// <summary>
70 /// Verifies that the specified action throws an ArgumentException of type <typeparam name="T"/>.
71 /// </summary>
72 public static T ThrowsArgument<T>(string parameterName, Action action)
73 where T : ArgumentException
75 var exception = Throws<T>(RetryMode.Retry, action, (actual, retryCount) =>
77 AssertSameParameterName(parameterName, actual, retryCount);
78 });
80 return exception;
83 /// <summary>
84 /// Verifies that the specified action throws an exception of type <typeparam name="T"/>,
85 /// with the specified inner exception of type <typeparam name="TInner"/>.
86 /// </summary>
87 public static T Throws<T, TInner>(Action action)
88 where T : Exception
89 where TInner : Exception
91 return Throws<T, TInner>(RetryMode.Retry, action);
94 /// <summary>
95 /// Verifies that the specified action throws an exception of type <typeparam name="T"/>,
96 /// with the specified inner exception of type <typeparam name="TInner"/>, and indicating
97 /// whether to retry.
98 /// </summary>
99 public static T Throws<T, TInner>(RetryMode retry, Action action)
100 where T : Exception
101 where TInner : Exception
103 return Throws<T, TInner>(retry, action, (Action<T, int>)null);
106 /// <summary>
107 /// Verifies that the specified action throws an exception of type <typeparam name="T"/>,
108 /// with the specified inner exception of type <typeparam name="TInner"/>, indicating
109 /// whether to retry and running the specified validator.
110 /// </summary>
111 public static T Throws<T, TInner>(RetryMode retry, Action action, Action<T, int> validator)
112 where T : Exception
113 where TInner : Exception
115 var exception = Throws<T>(retry, action, (actual, retryCount) =>
117 AssertIsExactInstanceOfInner(typeof(TInner), actual, retryCount);
119 if (validator != null)
121 validator(actual, retryCount);
125 return exception;
128 /// <summary>
129 /// Verifies that the specified action throws an exception of type <typeparam name="T"/>,
130 /// with the specified inner exception.
131 /// </summary>
132 public static T Throws<T>(Exception innerException, Action action)
133 where T : Exception
135 return Throws<T>(innerException, RetryMode.Retry, action, (Action<T, int>)null);
138 /// <summary>
139 /// Verifies that the specified action throws an exception of type <typeparam name="T"/>,
140 /// with the specified inner exception, and indicating whether to retry.
141 /// </summary>
142 public static T Throws<T>(Exception innerException, RetryMode retry, Action action)
143 where T : Exception
145 return Throws<T>(innerException, RetryMode.Retry, action, (Action<T, int>)null);
148 /// <summary>
149 /// Verifies that the specified action throws an exception of type <typeparam name="T"/>,
150 /// with the specified inner exception, indicating whether to retry and running the
151 /// specified validator.
152 /// </summary>
153 public static T Throws<T>(Exception innerException, RetryMode retry, Action action, Action<T, int> validator)
154 where T : Exception
156 T exception = Throws<T>(retry, action, (actual, retryCount) =>
158 AssertSameInner(innerException, actual, retryCount);
160 if (validator != null)
162 validator(actual, retryCount);
166 return exception;
169 /// <summary>
170 /// Verifies that the specified action throws an exception of type <typeparam name="T"/>.
171 /// </summary>
172 public static T Throws<T>(Action action)
173 where T : Exception
175 return Throws<T>(RetryMode.Retry, action, (Action<T, int>)null);
178 /// <summary>
179 /// Verifies that the specified action throws an exception of type <typeparam name="T"/>,
180 /// indicating whether to retry.
181 /// </summary>
182 public static T Throws<T>(RetryMode retry, Action action)
183 where T : Exception
185 return Throws<T>(retry, action, (Action<T, int>)null);
188 /// <summary>
189 /// Verifies that the specified action throws an exception of type <typeparam name="T"/>,
190 /// indicating whether to retry and running the specified validator.
191 /// </summary>
192 public static T Throws<T>(RetryMode retry, Action action, Action<T, int> validator)
193 where T : Exception
195 var exception = (T)Run(retry, action, (actual, retryCount) =>
197 AssertIsExactInstanceOf(typeof(T), actual, retryCount);
199 if (validator != null)
201 validator((T)actual, retryCount);
205 return exception;
208 /// <summary>
209 /// Verifies that the specified action throws the specified exception.
210 /// </summary>
211 public static void Throws(Exception expected, Action action)
213 Throws(expected, RetryMode.Retry, action);
216 /// <summary>
217 /// Verifies that the specified action throws the specified exception,
218 /// indicating whether to retry.
219 /// </summary>
220 public static void Throws(Exception expected, RetryMode retry, Action action)
222 Throws(expected, retry, action, (Action<Exception, int>)null);
225 /// <summary>
226 /// Verifies that the specified action throws the specified exception,
227 /// indicating whether to retry and running the specified validator.
228 /// </summary>
229 public static void Throws(Exception expected, RetryMode retry, Action action, Action<Exception, int> validator)
231 Run(retry, action, (actual, retryCount) =>
233 AssertSame(expected, actual, retryCount);
235 if (validator != null)
237 validator(actual, retryCount);
242 private static Exception Run(RetryMode retry, Action action, Action<Exception, int> validator)
244 Exception exception = null;
246 for (int i = -1; i < (int)retry; i++)
248 exception = Run(action);
250 validator(exception, i + 2);
253 return exception;
256 private static Exception Run(Action action)
260 action();
261 return null;
263 catch (Exception ex)
265 return ex;
269 private static void AssertSerializationMemberName(string memberName, SerializationException actual, int retryCount)
271 // Unfortunately, SerializationException does not provide a way to get our hands on the
272 // the actual member that was missing from the SerializationInfo, so we need to grok the
273 // message string.
275 // Member '[memberName]' was not found.
276 StringAssert.Contains(actual.Message, "'" + memberName + "'", "Retry Count {0}: Expected SerializationException MemberName to be '{1}'", retryCount, memberName);
279 private static void AssertObjectDisposed(object instance, ObjectDisposedException actual, int retryCount)
281 string objectName = instance.GetType().FullName;
283 Assert.AreEqual(objectName, actual.ObjectName, "Retry Count {0}: Expected {1}.ObjectName to be '{2}', however, '{3}' is.", retryCount, actual.GetType().Name, objectName, actual.ObjectName);
286 private static void AssertSameParameterName(string parameterName, ArgumentException actual, int retryCount)
288 #if !SILVERLIGHT
289 Assert.AreEqual(parameterName, actual.ParamName, "Retry Count {0}: Expected {1}.ParamName to be '{2}', however, '{3}' is.", retryCount, actual.GetType().Name, parameterName, actual.ParamName);
290 #else
291 // Silverlight doesn't have ArgumentException.ParamName
292 StringAssert.Contains(actual.Message, parameterName, "Retry Count {0}: Expected {1}.ParamName to be '{2}'", retryCount, actual.GetType().Name, parameterName);
293 #endif
296 private static void AssertSame(Exception expected, Exception actual, int retryCount)
298 Assert.AreSame(expected, actual, "Retry Count {0}: Expected '{1}' to be thrown, however, '{2}' was thrown.", retryCount, expected, actual);
301 private static void AssertSameInner(Exception innerException, Exception actual, int retryCount)
303 Assert.AreSame(innerException, actual.InnerException, "Retry Count {0}: Expected '{1}' to be the inner exception, however, '{2}' is.", retryCount, innerException, actual.InnerException);
306 private static void AssertIsExactInstanceOf(Type expectedType, Exception actual, int retryCount)
308 if (actual == null)
309 Assert.Fail("Retry Count {0}: Expected '{1}' to be thrown", retryCount, expectedType);
311 Type actualType = actual.GetType();
313 Assert.AreSame(expectedType, actualType, "Retry Count {0}: Expected '{1}' to be thrown, however, '{2}' was thrown.", retryCount, expectedType, actualType);
316 private static void AssertIsExactInstanceOfInner(Type expectedType, Exception actual, int retryCount)
318 if (actual.InnerException == null)
319 Assert.Fail("Retry Count {0}: Expected '{1}' be the inner exception, however, it is null.", retryCount, expectedType);
321 Type actualType = actual.InnerException.GetType();
323 Assert.AreSame(expectedType, actualType, "Retry Count {0}: Expected '{1}' to be the inner exception, however, '{2}' is.", retryCount, expectedType, actualType);