#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2012 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
#if UNITTEST
using System;
using System.IO;
using NUnit.Framework;
namespace Nuclex.Support.Plugins {
/// Unit Test for the prototype-based factory class
[TestFixture]
internal class PrototypeFactoryTest {
#region interface IProduct
/// Interface used for the product in the unit test
private interface IProduct {
/// Some value associated with the product
int Value { get; }
}
#endregion // interface IProduct
#region class ConcretePrototype
///
/// Class derived from the abstract base to serve as concrete product for
/// testing the factory employer
///
private class ConcretePrototype : IProduct, ICloneable, IDisposable {
/// Initializes a new instance of the prototype product
/// Value that will be associated with this instance
public ConcretePrototype(int value) {
this.value = value;
}
/// Immediately releases all resources owned by the instance
public void Dispose() {
this.disposed = true;
}
/// Value the product has been associated with
public int Value { get { return this.value; } }
/// Whether the prototype instance has been disposed
public bool IsDisposed {
get { return this.disposed; }
}
/// Creates an identical copy of the instance
/// An identical copy of the instance
object ICloneable.Clone() {
return new ConcretePrototype(this.value);
}
/// Value associated with the product
private int value;
/// Whether the instance has been disposed
private bool disposed;
}
#endregion // class ConcretePrototype
///
/// Tests whether the prototype-based factory behaves correctly by creating
/// new instances of its product using clones of its assigned prototype.
///
[Test]
public void TestGenericInstanceCreation() {
ConcretePrototype template = new ConcretePrototype(42);
IAbstractFactory factory = new PrototypeFactory<
IProduct, ConcretePrototype
>(template);
IProduct factoryCreatedProduct = factory.CreateInstance();
Assert.AreEqual(template.Value, factoryCreatedProduct.Value);
}
///
/// Tests whether the prototype-based factory behaves correctly by creating
/// new instances of its product using clones of its assigned prototype.
///
[Test]
public void TestInstanceCreation() {
ConcretePrototype template = new ConcretePrototype(42);
IAbstractFactory factory = new PrototypeFactory<
IProduct, ConcretePrototype
>(template);
IProduct factoryCreatedProduct = (IProduct)factory.CreateInstance();
Assert.AreEqual(template.Value, factoryCreatedProduct.Value);
}
///
/// Tests whether the prototype is disposed if it implements the IDisposable
/// interface and the factory is explicitely disposed.
///
[Test]
public void TestPrototypeDisposal() {
ConcretePrototype template = new ConcretePrototype(42);
PrototypeFactory factory = new PrototypeFactory<
IProduct, ConcretePrototype
>(template);
Assert.IsFalse(template.IsDisposed);
factory.Dispose();
Assert.IsTrue(template.IsDisposed);
}
}
} // namespace Nuclex.Support.Plugins
#endif // UNITTEST