Login using System.Noticeably.Different.WebSite;
November 19, 2008 NoticeablyDifferent
Search for
 
Testing The Bridge Pattern 
    #region Code
public interface IAbstraction {
    void Operation();
}
 
public interface IImplementor {
    void OperationImp();
}
 
public class ConcreteImplementorA : IImplementor {
    public void OperationImp() { }
}
 
public class RefinedAbstraction : IAbstraction {
    IImplementor implementor;
 
    public RefinedAbstraction() {
        implementor = ImplementorFactory.Instance.Implementor;
    }
 
    public void Operation() {
        implementor.OperationImp();
    }
}
 
public class ImplementorFactory {
 
    private System.Collections.Hashtable implementorCollection = new System.Collections.Hashtable();
 
    public static readonly ImplementorFactory Instance = new ImplementorFactory();
 
    private ImplementorFactory() {
        ResetImplementors();
    }
 
    public void ReplaceImplementor(System.Type type, IImplementor implementor) {
        implementorCollection[type] = implementor;
    }
 
    private void ResetImplementors() {
        implementorCollection.Clear();
        implementorCollection.Add(typeof(IImplementor), new ConcreteImplementorA());
    }
 
    public void Reset() {
        ResetImplementors();
    }
 
    public IImplementor Implementor {
        get { return (IImplementor)implementorCollection[typeof(IImplementor)]; }
    }
}
    #endregion
 
    #region Unit Tests
public class MockImplementor : IImplementor {
    public bool operationImpCalled = false;
 
    public void OperationImp() {
        operationImpCalled = true;
    }
}
 
[TestFixture]
public class RefinedAbstractionTests {
    RefinedAbstraction abstraction;
    MockImplementor implementor;
 
    [SetUp]
    public void SetUp() {
        implementor = new MockImplementor();
        ImplementorFactory.Instance.ReplaceImplementor(typeof(IImplementor), implementor);
        abstraction = new RefinedAbstraction();
    }
 
    [TearDown]
    public void TearDown() {
            ImplementorFactory.Instance.Reset();
}
 
    [Test]
    public void OperationCallsOperationImp() {
        Assert.IsFalse(implementor.operationImpCalled, "OperationImp not called");
        abstraction.Operation();
        Assert.IsTrue(implementor.operationImpCalled, "OperationImp called");
        //TODO: Assert Operation performs its intended action(s)
    }
}
 
[TestFixture]
public class ConcreteImplementorATests {
    ConcreteImplementorA implementor;
 
    [SetUp]
    public void SetUp() {
        implementor = new ConcreteImplementorA();
    }
 
    [Test]
    public void OperationImpPerformsExpectedActions() {
        //TODO: Assert OperationImp performs expected action(s)
    }
}
 
[TestFixture]
public class ImplementorFactoryTests {
 
    IImplementor implementor = new MockImplementor();
 
    public ImplementorFactoryTests() { }
 
    [SetUp]
    public void SetUp() {
        ImplementorFactory.Instance.Reset();
    }
 
    [Test]
    public void RealImplementorIsDefault() {
        Assert.AreEqual(typeof(ConcreteImplementorA), ImplementorFactory.Instance.Implementor.GetType());
    }
 
    [Test]
    public void ImplementorReplacedAndReturned() {
        Assert.AreEqual(typeof(ConcreteImplementorA), ImplementorFactory.Instance.Implementor.GetType());
        ImplementorFactory.Instance.ReplaceImplementor(typeof(IImplementor), new MockImplementor());
        Assert.AreEqual(typeof(MockImplementor), ImplementorFactory.Instance.Implementor.GetType());
        ImplementorFactory.Instance.Reset();
        Assert.AreEqual(typeof(ConcreteImplementorA), ImplementorFactory.Instance.Implementor.GetType());
    }
}
    #endregion