using System;
using System.Collections.Generic;
using System.Text;
using Systin.Library;
using NUnit.Framework;
using System.Reflection;
using LanguageLibrary;
namespace Systin.Library.UnitTests{
/// <summary>
///
/// </summary>
[TestFixture]
public class LanguageDriverClassUnitTest
{
private List<Type> mAssemblyTypes;
/// <summary>
/// The dll for the test language driver will be setup here.
/// </summary>
[TestFixtureSetUp]
public void Init()
{
//Gets the languageDriverClass dll assembly
Assembly assembly = Util.GetAssemblyPathRelativeToCurrentAssembly("LanguageLibrary.dll", ref this.mAssemblyTypes);
}
#region Exception Tests
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void FactoryMethodDoesNotAcceptNull()
{
LanguageDriverClass.Factory(null);
}
[Test]
[ExpectedException(typeof(ArgumentException))]
public void FactoryMethodShouldNotAllowNonSystinClassesToBeAdded()
{
Type badClass = typeof(BadLanguageDriverTestClass);
LanguageDriverClass.Factory(badClass);
}
[Test]
[Category("FileAccess")]
[ExpectedException(typeof(TargetInvocationException))]
public void DriverClassTestMethodIsNotFound()
{
LanguageDriverClass ldc = Util.GetLanguageDriverUnderTest("LanguageDriverTestClass", this.mAssemblyTypes);
MethodInfo method = null;
ldc.CallMethod("BadMethodName", ref method);
}
[Test]
[Category("FileAccess")]
[ExpectedException(typeof(InvalidOperationException))]
public void TestMethodSearchWithValidMethodButIsNotTaggedWithSystinAttribute()
{
LanguageDriverClass ldc = Util.GetLanguageDriverUnderTest("LanguageDriverTestClass", this.mAssemblyTypes);
Assert.IsFalse(LanguageDriverClass.MethodExists("Bad_Method_Not_Tagged", ldc));
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void MethodExistsShouldThrowArgumentNullExceptionForMethodNameParameter()
{
LanguageDriverClass ldc = Util.GetLanguageDriverUnderTest("LanguageDriverTestClass", this.mAssemblyTypes);
Assert.IsFalse(LanguageDriverClass.MethodExists(null, ldc));
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void MethodExistsShouldThrowArgumentNullExceptionForLanguageDriverClassParameter()
{
LanguageDriverClass ldc = Util.GetLanguageDriverUnderTest("LanguageDriverTestClass", this.mAssemblyTypes);
Assert.IsFalse(LanguageDriverClass.MethodExists("nonNull", null));
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void DriverClassTestMethodRejectsNull()
{
LanguageDriverClass ldc = Util.GetLanguageDriverUnderTest("LanguageDriverTestClass", this.mAssemblyTypes);
MethodInfo method = null;
ldc.CallMethod(null, ref method);
}
[Test]
[ExpectedException(typeof(TargetInvocationException))]
public void DriverMethodCausesExceptionWhenExecutedBySystin()
{
LanguageDriverClass ldc = Util.GetLanguageDriverUnderTest("LanguageDriverTestClass", this.mAssemblyTypes);
MethodInfo method = null;
ldc.CallMethod("Bad_Method_Programmed_Incorrectly", ref method);
}
#endregion
[Test]
public void FactoryMethodCreatesNonNullObject()
{
Type classUnderTest = Util.FindClassInList("LanguageDriverTestClass", this.mAssemblyTypes);
if (classUnderTest == null) {TestCase.Fail("LanguageDriverTestClass Was not found"); }
LanguageDriverClass ldc = LanguageDriverClass.Factory(classUnderTest);
Assert.IsNotNull( ldc, "LanguageDriverClass.Factory returned a null object");
}
[Test]
public void FactoryMethodCorrectlyActivatesASystinClass() {
LanguageDriverClass ldc = Util.GetLanguageDriverUnderTest("LanguageDriverTestClass", this.mAssemblyTypes);
Assert.IsTrue(ldc.ActivatedInstance.GetType().Equals(typeof(LanguageDriverTestClass)), "Activated Class and class parameter are not of the same type");
}
[Test]
public void EmptyFactoryInstanceReturnsDefaultEmptyObject()
{
LanguageDriverClass ldc = LanguageDriverClass.FactoryEmpty();
Assert.IsNull(ldc.ActivatedInstance , "ActivatedInstance should be null");
}
[Test]
public void DriverClassTestMethodHasBeenExecuted() {
LanguageDriverClass ldc = Util.GetLanguageDriverUnderTest("LanguageDriverTestClass", this.mAssemblyTypes);
MethodInfo method = null;
ldc.CallMethod("Log_Into_system", ref method);
Stack<string> callStack = Util.GetActionTakenByMethod(ldc);
Assert.AreEqual("Log_Into_system", callStack.Pop().ToString(), "Method was not called on object");
}
[Test]
public void TestMethodSearchFindsValidTestMethod()
{
LanguageDriverClass ldc = Util.GetLanguageDriverUnderTest("LanguageDriverTestClass", this.mAssemblyTypes);
Assert.IsTrue(LanguageDriverClass.MethodExists("Log_Into_system", ldc));
}
[Test]
public void TestMethodSearchFailsToFindFalseMethod()
{
LanguageDriverClass ldc = Util.GetLanguageDriverUnderTest("LanguageDriverTestClass", this.mAssemblyTypes);
Assert.IsFalse(LanguageDriverClass.MethodExists("Message_Does_Not_Exist", ldc));
}
/// <summary>
/// This test ensures that when a test class calls other methods that they get logged accordingly
/// </summary>
[Test]
public void DriverClassInvokesMultipleMethodsInCorrectOrderFromOneInvocation() {
LanguageDriverClass ldc = Util.GetLanguageDriverUnderTest("LanguageDriverTestClass", this.mAssemblyTypes);
MethodInfo method = null;
ldc.CallMethod("Log_Into_System_fully", ref method);
Stack<string> expectedCallStack = new Stack<string>();
expectedCallStack.Push("Set_User_Name");
expectedCallStack.Push("Press_Login_Button");
expectedCallStack.Push("Set_Password");
expectedCallStack.Push("Log_Into_System_fully");
Stack<string> callStack = Util.GetActionTakenByMethod(ldc);
AssertCallStacksAreEqual(expectedCallStack, callStack);
}
private void AssertCallStacksAreEqual(Stack<string> expectedCallStack, Stack<string> actualCallStack)
{
if (expectedCallStack.Count != actualCallStack.Count)
{
Assert.Fail("Size of stacks differ");
}
int origCount = expectedCallStack.Count;
for (int i = 0; i < origCount; i++)
{
String expectedCall = expectedCallStack.Pop();
String actualCall = actualCallStack.Pop();
if (expectedCall != actualCall)
{
StringAssert.AreEqualIgnoringCase(expectedCall, actualCall, "Methods Called were incorrect");
}
}
}
[Test]
[Ignore("Not implemented yet")]
public void DriverClassTestMethodDoesNotExist() { }
}
}
|