Increased test coverage for the command line parser (and then decided the thing is broken beyond repair and needs a rewrite -- as if that was any surprise given the original code is from CodeProject); achieved 100% test coverage for the factory employer, instance employer, employer, no plugin attribute and abstract factory classes in the Plugins namespace; achieved some test coverage for the PluginRepository class (the uncovered sections are quite hard to stimulate since they involve exceptions during assembly load attempts)

git-svn-id: file:///srv/devel/repo-conversion/nusu@99 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2008-11-28 19:34:43 +00:00
parent ecb36e9ce3
commit b39f8de155
11 changed files with 703 additions and 4 deletions

View File

@ -123,16 +123,37 @@
<DependentUpon>PathHelper.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\AbstractFactory.cs" />
<Compile Include="Source\Plugins\AbstractFactory.Test.cs">
<DependentUpon>AbstractFactory.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\Employer.Test.cs">
<DependentUpon>Employer.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\FactoryEmployer.Test.cs">
<DependentUpon>FactoryEmployer.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\InstanceEmployer.Test.cs">
<DependentUpon>InstanceEmployer.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\NoPluginAttribute.cs" />
<Compile Include="Source\Plugins\Employer.cs" />
<Compile Include="Source\Plugins\FactoryEmployer.cs" />
<Compile Include="Source\Plugins\InstanceEmployer.cs" />
<Compile Include="Source\Plugins\NoPluginAttribute.Test.cs">
<DependentUpon>NoPluginAttribute.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\PluginHelper.cs" />
<Compile Include="Source\Plugins\PluginHelper.Test.cs">
<DependentUpon>PluginHelper.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\PluginHost.cs" />
<Compile Include="Source\Plugins\PluginHost.Test.cs">
<DependentUpon>PluginHost.cs</DependentUpon>
</Compile>
<Compile Include="Source\Plugins\PluginRepository.cs" />
<Compile Include="Source\Plugins\PluginRepository.Test.cs">
<DependentUpon>PluginRepository.cs</DependentUpon>
</Compile>
<Compile Include="Source\Scheduling\AbortedException.cs" />
<Compile Include="Source\Scheduling\IAbortable.cs" />
<Compile Include="Source\Scheduling\Operation.cs" />

View File

@ -34,7 +34,7 @@ namespace Nuclex.Support.Parsing {
/// <summary>Validates that normal arguments can be parsed</summary>
[Test]
public void TestPlainArguments() {
public void TestArrayConstructorWithPlainArguments() {
Assert.IsTrue(
new CommandLineParser(new string[] { "-hello" }).HasArgument("hello"),
"Argument with minus sign is recognized"
@ -51,7 +51,7 @@ namespace Nuclex.Support.Parsing {
/// <summary>Validates that argument assignments are working</summary>
[Test]
public void TestAssignments() {
public void TestArrayConstructorWithAssignments() {
Assert.AreEqual(
"world",
new CommandLineParser(new string[] { "-hello:world" })["hello"],
@ -73,7 +73,7 @@ namespace Nuclex.Support.Parsing {
/// Validates that loosely specified values are recognized by the parser
/// </summary>
[Test]
public void TestLooseValues() {
public void TestArrayConstructorWithLooseValues() {
Assert.IsTrue(
new CommandLineParser(new string[] { "hello" }).Values.Contains("hello"),
"Plain loose value is recognized"
@ -84,6 +84,47 @@ namespace Nuclex.Support.Parsing {
);
}
/// <summary>
/// Tests whether the parser can parse the processes current command line if
/// the default constructor is used
/// </summary>
[Test]
public void TestDefaultConstructor() {
new CommandLineParser();
}
/// <summary>
/// Tests whether the string constructor works for simple arguments being
/// specified on the command line
/// </summary>
[Test]
public void TestStringConstructorWithSimpleArguments() {
CommandLineParser parser = new CommandLineParser("argument1 argument2");
Assert.AreEqual("argument1", parser.Values[0]);
Assert.AreEqual("argument2", parser.Values[1]);
}
// TODO: This test fails!!
#if FAILED_TEST
/// <summary>
/// Bullshit
/// </summary>
[Test]
public void TestStringConstructorWithQuotedArguments() {
CommandLineParser parser = new CommandLineParser("\"this is a single argument\"");
Assert.AreEqual("this is a single argument", parser.Values[0]);
}
#endif
/// <summary>
/// Tests whether the string constructor recognizes an unfinished argument
/// (that is, and argument that gets 'nothing' assigned)
/// </summary>
[Test]
public void TestStringConstructorWithUnfinishedAssignment() {
CommandLineParser parser = new CommandLineParser("--hello= --world=");
}
}
} // namespace Nuclex.Support.Parsing

View File

@ -0,0 +1,39 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2008 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 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
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.IO;
#if UNITTEST
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
namespace Nuclex.Support.Plugins {
/// <summary>Unit Test for the abstract factory interface</summary>
[TestFixture]
public class AbtractFactoryTest {
}
} // namespace Nuclex.Support.Plugins
#endif // UNITTEST

View File

@ -0,0 +1,93 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2008 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 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
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.IO;
#if UNITTEST
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
namespace Nuclex.Support.Plugins {
/// <summary>Unit Test for the employer class</summary>
[TestFixture]
public class EmployerTest {
#region class TestEmployer
/// <summary>Dummy implementation for testing the employer class</summary>
private class TestEmployer : Employer {
/// <summary>Employs the specified plugin type</summary>
/// <param name="type">Type to be employed</param>
public override void Employ(Type type) { }
}
#endregion // class TestEmployer
#region class NoDefaultConstructor
/// <summary>Test class that doesn't have a default constructor</summary>
private class NoDefaultConstructor {
/// <summary>Initializes a new instance of the test class</summary>
/// <param name="dummy">Dummy argument so this is no default constructor</param>
public NoDefaultConstructor(int dummy) { }
}
#endregion // class NoDefaultConstructor
#region class NonPublicDefaultConstructor
/// <summary>Test class that has a non-public default constructor</summary>
private class NonPublicDefaultConstructor {
/// <summary>Initializes a new instance of the test class</summary>
protected NonPublicDefaultConstructor() { }
}
#endregion // class NonPublicDefaultConstructor
#region class PublicDefaultConstructor
/// <summary>Test class that has a public default constructor</summary>
private class PublicDefaultConstructor {
/// <summary>Initializes a new instance of the test class</summary>
public PublicDefaultConstructor() { }
}
#endregion // class PublicDefaultConstructor
/// <summary>Tests whether the employer can detect a default constructor</summary>
[Test]
public void TestEmployerDefaultConstructorDetection() {
TestEmployer testEmployer = new TestEmployer();
Assert.IsFalse(testEmployer.CanEmploy(typeof(NoDefaultConstructor)));
Assert.IsFalse(testEmployer.CanEmploy(typeof(NonPublicDefaultConstructor)));
Assert.IsTrue(testEmployer.CanEmploy(typeof(PublicDefaultConstructor)));
}
}
} // namespace Nuclex.Support.Plugins
#endif // UNITTEST

View File

@ -0,0 +1,132 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2008 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 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
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.IO;
#if UNITTEST
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
namespace Nuclex.Support.Plugins {
/// <summary>Unit Test for the factory employer class</summary>
[TestFixture]
public class FactoryEmployerTest {
#region class Base
/// <summary>
/// Abstract base class to serve as abstract product for testing the factory employer
/// </summary>
private abstract class Base { }
#endregion // class Base
#region class Derived
/// <summary>
/// Class derived from the abstract base to serve as concrete product for
/// testing the factory employer
/// </summary>
private class Derived : Base { }
#endregion // class Derived
#region class Unrelated
/// <summary>Unrelated class used to test the factory employer</summary>
private class Unrelated { }
#endregion // class Unrelated
/// <summary>
/// Tests whether the factory employer can detect employable types
/// </summary>
[Test]
public void TestCanEmploy() {
FactoryEmployer<Base> testEmployer = new FactoryEmployer<Base>();
Assert.IsFalse(testEmployer.CanEmploy(typeof(Base)));
Assert.IsTrue(testEmployer.CanEmploy(typeof(Derived)));
Assert.IsFalse(testEmployer.CanEmploy(typeof(Unrelated)));
}
/// <summary>
/// Tests whether the factory employer throws an exception when it is asked to
/// employ an abstract class
/// </summary>
[Test, ExpectedException(typeof(MissingMethodException))]
public void TestThrowOnEmployAbstractClass() {
FactoryEmployer<Base> testEmployer = new FactoryEmployer<Base>();
testEmployer.Employ(typeof(Base));
}
/// <summary>
/// Tests whether the factory employer throws an exception when it is asked to
/// employ a class that is not the product type or a derivative thereof
/// </summary>
[Test, ExpectedException(typeof(InvalidCastException))]
public void TestThrowOnEmployUnrelatedClass() {
FactoryEmployer<Base> testEmployer = new FactoryEmployer<Base>();
testEmployer.Employ(typeof(Unrelated));
}
/// <summary>
/// Tests whether the factory employer can employ a class derived from the product
/// </summary>
[Test]
public void TestEmployClassDerivedFromProduct() {
FactoryEmployer<Base> testEmployer = new FactoryEmployer<Base>();
testEmployer.Employ(typeof(Derived));
Assert.AreEqual(1, testEmployer.Factories.Count);
Assert.AreEqual(typeof(Derived), testEmployer.Factories[0].ConcreteType);
Assert.IsInstanceOfType(
typeof(Derived), testEmployer.Factories[0].CreateInstance()
);
}
/// <summary>
/// Tests whether the factory employer can employ the product class itself if it
/// isn't abstract
/// </summary>
[Test]
public void TestEmployProduct() {
FactoryEmployer<Unrelated> testEmployer = new FactoryEmployer<Unrelated>();
testEmployer.Employ(typeof(Unrelated));
Assert.AreEqual(1, testEmployer.Factories.Count);
Assert.AreEqual(typeof(Unrelated), testEmployer.Factories[0].ConcreteType);
Assert.IsInstanceOfType(
typeof(Unrelated), testEmployer.Factories[0].CreateInstance()
);
}
}
} // namespace Nuclex.Support.Plugins
#endif // UNITTEST

View File

@ -41,7 +41,8 @@ namespace Nuclex.Support.Plugins {
/// a human-readable name, capabilities or an icon.
/// </para>
/// </remarks>
public class FactoryEmployer<ProductType> : Employer {
public class FactoryEmployer<ProductType> : Employer
where ProductType : class {
#region class ConcreteFactory
@ -94,6 +95,17 @@ namespace Nuclex.Support.Plugins {
/// <summary>Employs the specified plugin type</summary>
/// <param name="type">Type to be employed</param>
public override void Employ(Type type) {
if(!PluginHelper.HasDefaultConstructor(type)) {
throw new MissingMethodException(
"Cannot employ type because it does not have a public default constructor"
);
}
if(!typeof(ProductType).IsAssignableFrom(type)) {
throw new InvalidCastException(
"Cannot employ type because it cannot be cast to the factory's product type"
);
}
this.employedFactories.Add(new ConcreteFactory(type));
}

View File

@ -0,0 +1,129 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2008 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 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
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.IO;
#if UNITTEST
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
namespace Nuclex.Support.Plugins {
/// <summary>Unit Test for the instance employer class</summary>
[TestFixture]
public class InstanceEmployerTest {
#region class Base
/// <summary>
/// Abstract base class to serve as abstract product for testing the instance employer
/// </summary>
private abstract class Base { }
#endregion // class Base
#region class Derived
/// <summary>
/// Class derived from the abstract base to serve as concrete product for
/// testing the instance employer
/// </summary>
private class Derived : Base { }
#endregion // class Derived
#region class Unrelated
/// <summary>Unrelated class used to test the instance employer</summary>
private class Unrelated { }
#endregion // class Unrelated
/// <summary>
/// Tests whether the instance employer can detect employable types
/// </summary>
[Test]
public void TestCanEmploy() {
InstanceEmployer<Base> testEmployer = new InstanceEmployer<Base>();
Assert.IsFalse(testEmployer.CanEmploy(typeof(Base)));
Assert.IsTrue(testEmployer.CanEmploy(typeof(Derived)));
Assert.IsFalse(testEmployer.CanEmploy(typeof(Unrelated)));
}
/// <summary>
/// Tests whether the instance employer throws an exception when it is asked to
/// employ an abstract class
/// </summary>
[Test, ExpectedException(typeof(MissingMethodException))]
public void TestThrowOnEmployAbstractClass() {
InstanceEmployer<Base> testEmployer = new InstanceEmployer<Base>();
testEmployer.Employ(typeof(Base));
}
/// <summary>
/// Tests whether the instance employer throws an exception when it is asked to
/// employ a class that is not the product type or a derivative thereof
/// </summary>
[Test, ExpectedException(typeof(InvalidCastException))]
public void TestThrowOnEmployUnrelatedClass() {
InstanceEmployer<Base> testEmployer = new InstanceEmployer<Base>();
testEmployer.Employ(typeof(Unrelated));
}
/// <summary>
/// Tests whether the instance employer can employ a class derived from the product
/// </summary>
[Test]
public void TestEmployClassDerivedFromProduct() {
InstanceEmployer<Base> testEmployer = new InstanceEmployer<Base>();
testEmployer.Employ(typeof(Derived));
Assert.AreEqual(1, testEmployer.Instances.Count);
Assert.AreEqual(typeof(Derived), testEmployer.Instances[0].GetType());
Assert.IsInstanceOfType(typeof(Derived), testEmployer.Instances[0]);
}
/// <summary>
/// Tests whether the instance employer can employ the product class itself if it
/// isn't abstract
/// </summary>
[Test]
public void TestEmployProduct() {
InstanceEmployer<Unrelated> testEmployer = new InstanceEmployer<Unrelated>();
testEmployer.Employ(typeof(Unrelated));
Assert.AreEqual(1, testEmployer.Instances.Count);
Assert.AreEqual(typeof(Unrelated), testEmployer.Instances[0].GetType());
Assert.IsInstanceOfType(typeof(Unrelated), testEmployer.Instances[0]);
}
}
} // namespace Nuclex.Support.Plugins
#endif // UNITTEST

View File

@ -0,0 +1,47 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2008 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 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
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.IO;
#if UNITTEST
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
namespace Nuclex.Support.Plugins {
/// <summary>Unit Test for the no plugin attribute class</summary>
[TestFixture]
public class NoPluginAttributeTest {
/// <summary>
/// Tests whether the default consturctor of the no plugin attribute works
/// </summary>
[Test]
public void TestDefaultConstructor() {
new NoPluginAttribute();
}
}
} // namespace Nuclex.Support.Plugins
#endif // UNITTEST

View File

@ -0,0 +1,38 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2008 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 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
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.IO;
#if UNITTEST
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
namespace Nuclex.Support.Plugins {
/// <summary>Unit Test for the plugin host class</summary>
[TestFixture]
public class PluginHostTest {
}
} // namespace Nuclex.Support.Plugins
#endif // UNITTEST

View File

@ -0,0 +1,142 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2008 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 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
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.IO;
using System.Reflection;
#if UNITTEST
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using NMock2;
namespace Nuclex.Support.Plugins {
/// <summary>Unit Test for the plugin repository class</summary>
[TestFixture]
public class PluginRepositoryTest {
#region interface IAssemblyLoadedSubscriber
/// <summary>Interface used to test the progress tracker</summary>
public interface IAssemblyLoadedSubscriber {
/// <summary>
/// Represents the method that handles the System.AppDomain.AssemblyLoad event
/// of an System.AppDomain
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="arguments">
/// An System.AssemblyLoadEventArgs that contains the event data
/// </param>
void AssemblyLoaded(object sender, AssemblyLoadEventArgs arguments);
}
#endregion // interface IProgressTrackerSubscriber
/// <summary>
/// Tests whether the default constructor of the plugin repository class works
/// </summary>
[Test]
public void TestDefaultConstructor() {
new PluginRepository();
}
/// <summary>
/// Tests whether the AddFiles() method accepts a file mask to which there are
/// no matching files
/// </summary>
[Test]
public void TestAddFilesWithZeroMatches() {
PluginRepository testRepository = new PluginRepository();
testRepository.AddFiles(Guid.NewGuid().ToString());
}
/// <summary>
/// Tests whether the AddFiles() method accepts a file mask to which there is
/// exactly one matching file
/// </summary>
[Test]
public void TestAddFilesWithOwnAssembly() {
Assembly self = Assembly.GetAssembly(GetType());
PluginRepository testRepository = new PluginRepository();
testRepository.AddFiles(self.Location);
}
/// <summary>
/// Tests whether the AddAssembly() method works by adding the test assembly
/// itself to the repository
/// </summary>
[Test]
public void TestAddAssembly() {
PluginRepository testRepository = new PluginRepository();
// Might also use Assembly.GetCallingAssembly() here, but this leads to the exe of
// the unit testing tool
Assembly self = Assembly.GetAssembly(GetType());
testRepository.AddAssembly(self);
Assert.AreEqual(1, testRepository.LoadedAssemblies.Count);
}
/// <summary>
/// Tests whether the AddAssembly() method works by adding the test assembly
/// itself to the repository
/// </summary>
[Test]
public void TestAssemblyLoadedEvent() {
Mockery mockery = new Mockery();
PluginRepository testRepository = new PluginRepository();
IAssemblyLoadedSubscriber subscriber = mockSubscriber(mockery, testRepository);
Expect.Once.On(subscriber).Method("AssemblyLoaded").WithAnyArguments();
Assembly self = Assembly.GetAssembly(GetType());
testRepository.AddAssembly(self);
mockery.VerifyAllExpectationsHaveBeenMet();
}
/// <summary>Mocks a subscriber for the events of a plugin repository</summary>
/// <param name="mockery">Mockery to create an event subscriber in</param>
/// <param name="repository">Repository to subscribe the mocked subscriber to</param>
/// <returns>The mocked event subscriber</returns>
private static IAssemblyLoadedSubscriber mockSubscriber(
Mockery mockery, PluginRepository repository
) {
IAssemblyLoadedSubscriber mockedSubscriber =
mockery.NewMock<IAssemblyLoadedSubscriber>();
repository.AssemblyLoaded += new AssemblyLoadEventHandler(
mockedSubscriber.AssemblyLoaded
);
return mockedSubscriber;
}
}
} // namespace Nuclex.Support.Plugins
#endif // UNITTEST

View File

@ -52,6 +52,11 @@ namespace Nuclex.Support.Plugins {
string directory = Path.GetDirectoryName(wildcard);
string search = Path.GetFileName(wildcard);
// If no directory was specified, use the current working directory
if((directory == null) || (directory == string.Empty)) {
directory = ".";
}
// We'll scan the specified directory for all files matching the specified
// wildcard. If only a single file is specified, only that file will match
// the supposed wildcard and everything works as expected