#region License
/**
* Ingenious MVC : An MVC framework for .NET 2.0
* Copyright (C) 2006, JDP Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors: - Kent Boogaart (kentcb@internode.on.net)
*/
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using NUnit.Framework;
namespace Ingenious.Mvc.UnitTest{
/// <summary>
/// A generic unit test class for exceptions. This is used to complete more code coverage.
/// </summary>
public abstract class ExceptionTest<T> : UnitTestBase
where T : Exception, new()
{
private T _exception;
private readonly Type _type = typeof(T);
[Test]
public void TestDefaultConstructor()
{
_exception = new T();
Assert.AreEqual("Exception of type '" + _type.FullName + "' was thrown.", _exception.Message);
Assert.IsNull(_exception.InnerException);
Assert.IsNull(_exception.StackTrace);
}
[Test]
public void TestMessageConstructorNull()
{
ConstructorInfo constructor = typeof(T).GetConstructor(new Type[] { typeof(string) });
Assert.IsNotNull(constructor, "No constructor was found taking a string in exception type '" + _type.FullName + "'");
_exception = constructor.Invoke(new object[] { null }) as T;
Assert.IsNotNull(_exception);
Assert.AreEqual("Exception of type '" + _type.FullName + "' was thrown.", _exception.Message);
Assert.IsNull(_exception.InnerException);
Assert.IsNull(_exception.StackTrace);
}
[Test]
public void TestMessageConstructor()
{
string message = "my message";
ConstructorInfo constructor = typeof(T).GetConstructor(new Type[] {typeof(string)});
Assert.IsNotNull(constructor, "No constructor was found taking a string in exception type '" + _type.FullName + "'");
_exception = constructor.Invoke(new object[] {message}) as T;
Assert.IsNotNull(_exception);
Assert.AreEqual(message, _exception.Message);
Assert.IsNull(_exception.InnerException);
Assert.IsNull(_exception.StackTrace);
}
[Test]
public void TestInnerExceptionConstructorNull()
{
string message = "my message";
ConstructorInfo constructor = typeof(T).GetConstructor(new Type[] { typeof(string), typeof(Exception) });
Assert.IsNotNull(constructor, "No constructor was found taking a string and Exception in exception type '" + _type.FullName + "'");
_exception = constructor.Invoke(new object[] { message, null }) as T;
Assert.IsNotNull(_exception);
Assert.AreEqual(message, _exception.Message);
Assert.IsNull(_exception.InnerException);
Assert.IsNull(_exception.StackTrace);
}
[Test]
public void TestInnerExceptionConstructor()
{
string message = "my message";
Exception inner = new NullReferenceException();
ConstructorInfo constructor = typeof(T).GetConstructor(new Type[] { typeof(string), typeof(Exception) });
Assert.IsNotNull(constructor, "No constructor was found taking a string and Exception in exception type '" + _type.FullName + "'");
_exception = constructor.Invoke(new object[] { message, inner }) as T;
Assert.IsNotNull(_exception);
Assert.AreEqual(message, _exception.Message);
Assert.AreEqual(inner, _exception.InnerException);
Assert.IsNull(_exception.StackTrace);
}
[Test]
public void TestSerialization()
{
IFormatter formatter = new BinaryFormatter();
string message = "my message";
Exception inner = new NullReferenceException();
ConstructorInfo constructor = typeof(T).GetConstructor(new Type[] { typeof(string), typeof(Exception) });
Assert.IsNotNull(constructor, "No constructor was found taking a string and Exception in exception type '" + _type.FullName + "'");
_exception = constructor.Invoke(new object[] { message, inner }) as T;
using (MemoryStream stream = new MemoryStream())
{
formatter.Serialize(stream, _exception);
stream.Flush();
stream.Position = 0;
_exception = (T)formatter.Deserialize(stream);
}
Assert.IsNotNull(_exception);
Assert.AreEqual(message, _exception.Message);
Assert.IsNotNull(_exception.InnerException);
Assert.IsNull(_exception.StackTrace);
}
}
}
|