Fixed a typo; fixed a bug in the plug-in library that would see generic types deriving from a required base class as suitable whereas they cannot be instantiated without a generic argument; added unit tests that reproduce the errors

git-svn-id: file:///srv/devel/repo-conversion/nusu@171 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2009-09-01 19:11:19 +00:00
parent 35b1ca742d
commit 6ef2fdb789
5 changed files with 56 additions and 3 deletions

View File

@ -50,6 +50,16 @@ namespace Nuclex.Support.Plugins {
#endregion // class Derived #endregion // class Derived
#region class GenericDerived
/// <summary>
/// Generic class derived from the abstract base to serve as concrete product
/// for testing the factory employer
/// </summary>
private class GenericDerived<SomeType> : Base { }
#endregion // class GenericDerived
#region class Unrelated #region class Unrelated
/// <summary>Unrelated class used to test the factory employer</summary> /// <summary>Unrelated class used to test the factory employer</summary>
@ -112,6 +122,19 @@ namespace Nuclex.Support.Plugins {
); );
} }
/// <summary>
/// Tests whether the factory employer throws an exception when it is asked to
/// employ a class that requires generic parameters for instantiation
/// </summary>
[Test]
public void TestThrowOnEmployGenericClass() {
FactoryEmployer<Base> testEmployer = new FactoryEmployer<Base>();
Assert.Throws<ArgumentException>(
delegate() { testEmployer.Employ(typeof(GenericDerived<>)); }
);
}
/// <summary> /// <summary>
/// Tests whether the factory employer can employ a class derived from the product /// Tests whether the factory employer can employ a class derived from the product
/// </summary> /// </summary>

View File

@ -91,7 +91,8 @@ namespace Nuclex.Support.Plugins {
public override bool CanEmploy(Type type) { public override bool CanEmploy(Type type) {
return return
PluginHelper.HasDefaultConstructor(type) && PluginHelper.HasDefaultConstructor(type) &&
typeof(ProductType).IsAssignableFrom(type); typeof(ProductType).IsAssignableFrom(type) &&
!type.ContainsGenericParameters;
} }
/// <summary>Employs the specified plugin type</summary> /// <summary>Employs the specified plugin type</summary>
@ -107,6 +108,11 @@ namespace Nuclex.Support.Plugins {
"Cannot employ type because it cannot be cast to the factory's product type" "Cannot employ type because it cannot be cast to the factory's product type"
); );
} }
if(type.ContainsGenericParameters) {
throw new ArgumentException(
"Cannot employ type because it requires generic parameters", "type"
);
}
this.employedFactories.Add(new ConcreteFactory(type)); this.employedFactories.Add(new ConcreteFactory(type));
} }

View File

@ -50,6 +50,16 @@ namespace Nuclex.Support.Plugins {
#endregion // class Derived #endregion // class Derived
#region class GenericDerived
/// <summary>
/// Generic class derived from the abstract base to serve as concrete product
/// for testing the instance employer
/// </summary>
private class GenericDerived<SomeType> : Base { }
#endregion // class GenericDerived
#region class Unrelated #region class Unrelated
/// <summary>Unrelated class used to test the instance employer</summary> /// <summary>Unrelated class used to test the instance employer</summary>
@ -96,6 +106,19 @@ namespace Nuclex.Support.Plugins {
); );
} }
/// <summary>
/// Tests whether the instance employer throws an exception when it is asked to
/// employ a class that requires generic parameters for instantiation
/// </summary>
[Test]
public void TestThrowOnEmployGenericClass() {
InstanceEmployer<Base> testEmployer = new InstanceEmployer<Base>();
Assert.Throws<ArgumentException>(
delegate() { testEmployer.Employ(typeof(GenericDerived<>)); }
);
}
/// <summary> /// <summary>
/// Tests whether the instance employer can employ a class derived from the product /// Tests whether the instance employer can employ a class derived from the product
/// </summary> /// </summary>

View File

@ -62,7 +62,8 @@ namespace Nuclex.Support.Plugins {
public override bool CanEmploy(Type type) { public override bool CanEmploy(Type type) {
return return
PluginHelper.HasDefaultConstructor(type) && PluginHelper.HasDefaultConstructor(type) &&
typeof(T).IsAssignableFrom(type); typeof(T).IsAssignableFrom(type) &&
!type.ContainsGenericParameters;
} }
/// <summary>Employs the specified plugin type</summary> /// <summary>Employs the specified plugin type</summary>

View File

@ -246,7 +246,7 @@ namespace Nuclex.Support.Tracking {
} }
/// <summary>List of event handler which have subscribed to the ended event</summary> /// <summary>Event handlers which have subscribed to the ended event</summary>
/// <remarks> /// <remarks>
/// Does not need to be volatile since it's only accessed inside /// Does not need to be volatile since it's only accessed inside
/// </remarks> /// </remarks>