using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Systin.Library;
using System.Reflection;
using System.Collections;
namespace Systin.Library.UnitTests{
[TestFixture]
public class LanguageDriverClassManagerUnitTest
{
private List<Type> mAssemblyTypes;
private Assembly ClassAssembly;
private static string LanguageLibraryName = "LanguageLibrary.dll";
/// <summary>
/// Drivers the manager throws null exception on null arguments.
/// </summary>
[Test]
public void DriverManagerThrowsNullExceptionOnNullArguments() {
setup();
try
{
LanguageDriverClassManager.Factory(null);
Assert.Fail("Exception was not thrown");
}
catch (ArgumentNullException) { }
}
[Test]
public void CallMethodThreadedWrapperThrowsArgumentException()
{
setup();
try
{
Queue q = new Queue();
LanguageDriverClassManager.CallMethodThreadedWrapper(q);
Assert.Fail("Exception was not thrown");
}
catch (ArgumentException) { }
}
[Test]
public void CallMethodThreadedWrapperThrowsArgumentNullException()
{
setup();
try
{
LanguageDriverClassManager.CallMethodThreadedWrapper(null);
Assert.Fail("Exception was not thrown");
}
catch (ArgumentException) { }
}
[Test]
public void CallMethodThreadedWrapperSuccessful()
{
setup();
LanguageDriverClassManager ldc = LanguageDriverClassManager.Factory(this.ClassAssembly);
Instruction instruction = Instruction.NewInstruction("Log_Into_system");
Queue<Instruction> instructions = new Queue<Instruction>();
instructions.Enqueue(instruction);
ThreadWorkerMethodCall tworker = ThreadWorkerMethodCall.Factory(instructions, ref ldc);
LanguageDriverClassManager.CallMethodThreadedWrapper(tworker);
Assert.IsTrue(tworker.LanguageDriverClassManager.InstructionStack.Count == 1);
Assert.IsTrue(ldc.InstructionStack.Count == 1);
MethodInfo method = tworker.LanguageDriverClassManager.InstructionStack.Pop();
Assert.AreEqual(method.Name, "Log_Into_system");
Assert.IsTrue(tworker.LanguageDriverClassManager.InstructionStack.Count == 0);
Assert.IsTrue(ldc.InstructionStack.Count == 0);
}
/// <summary>
/// Drivers the manager creates correct number of language driver classes from assembly.
/// </summary>
[Test]
public void DriverManagerCreatesCorrectNumberOfLanguageDriverClassesFromAssembly() {
//How many classes are in the library under test
setup();
int classCount = GetNumberOfSystinClassesInAssembly();
LanguageDriverClassManager ldcm = LanguageDriverClassManager.Factory(this.ClassAssembly);
Assert.AreEqual( classCount, ldcm.LanguageDriverClassHash.Count, "Number in hash and number counted are different" );
}
/// <summary>
/// Drivers the manager factory method does not return null.
/// </summary>
[Test]
public void DriverManagerFactoryMethodDoesNotReturnNull()
{
Assembly assembly = Assembly.GetExecutingAssembly();
LanguageDriverClassManager ldcm = LanguageDriverClassManager.Factory(assembly);
Assert.IsNotNull(ldcm, "Manager Class returned a null Value");
}
[Test]
[ExpectedException(typeof(TargetException))]
public void CallMethodThatDoesNotExist()
{
setup();
LanguageDriverClassManager ldcm = LanguageDriverClassManager.Factory(this.ClassAssembly);
ldcm = LanguageDriverClassManager.CallMethod("Non_Existant_Method", ldcm);
}
[Test]
[ExpectedException(typeof(TargetException))]
public void CallMethodWithBadName()
{
setup();
LanguageDriverClassManager ldcm = LanguageDriverClassManager.Factory(this.ClassAssembly);
ldcm = LanguageDriverClassManager.CallMethod("text of Method in Bad type_//$$$11", ldcm);
}
/// <summary>
/// Calls the valid test method.
/// </summary>
[Test]
public void CallValidTestMethod() {
setup();
LanguageDriverClassManager ldcm = LanguageDriverClassManager.Factory(this.ClassAssembly);
ldcm = LanguageDriverClassManager.CallMethod("Log_Into_system", ldcm);
MethodInfo methodInvoked = ldcm.InstructionStack.Pop();
StringAssert.AreEqualIgnoringCase(methodInvoked.Name, "Log_Into_system", "Method Names aren't equal");
}
[Test]
public void CallValidTestCaseWithNullMethodValue()
{
try
{
setup();
LanguageDriverClassManager.CallMethod(null, null);
TestCase.Fail("ArgumentNullException should have been thrown");
}
catch (ArgumentNullException) { }
}
[Test]
public void CallMethodWithNullLanguageDriverClassObject()
{
try
{
setup();
LanguageDriverClassManager.CallMethod("Log_Into_system", null);
TestCase.Fail("ArgumentNullException should have been thrown");
}
catch (ArgumentNullException) { }
}
#region Private Methods
private int GetNumberOfSystinClassesInAssembly()
{
int classCount = 0;
foreach (Type classType in this.ClassAssembly.GetTypes())
{
if (Attribute.GetCustomAttribute(classType, typeof(LanguageDriverAttribute)) != null)
{
classCount++;
}
}
return classCount;
}
private void setup()
{
//Gets the languageDriverClass dll assembly
Assembly assembly = Util.GetAssemblyPathRelativeToCurrentAssembly(LanguageLibraryName, ref this.mAssemblyTypes);
this.ClassAssembly = assembly;
}
#endregion
}
}
|