예제 #1
0
        public static void Execute___Should_return_registered_protocol___When_called()
        {
            // Arrange
            var systemUnderTest = new ChainOfResponsibilityProtocolFactory();

            IProtocol protocol1 = new SharedOperationProtocol1();
            IProtocol protocol2 = new SiblingOperationProtocol();
            IProtocol protocol3 = new SharedOperationProtocol2();

            var protocolFactory1 = protocol1.ToProtocolFactory();
            var protocolFactory2 = protocol2.ToProtocolFactory();
            var protocolFactory3 = protocol3.ToProtocolFactory();

            systemUnderTest.AddToEndOfChain(protocolFactory1);
            systemUnderTest.AddToEndOfChain(protocolFactory2);
            systemUnderTest.AddToEndOfChain(protocolFactory3);

            var operation1 = new GetProtocolOp(new SharedOperation());
            var operation2 = new GetProtocolOp(new SiblingOperation1());
            var operation3 = new GetProtocolOp(new SiblingOperation2());

            // Act
            var actual1 = systemUnderTest.Execute(operation1);
            var actual2 = systemUnderTest.Execute(operation2);
            var actual3 = systemUnderTest.Execute(operation3);

            // Assert
            actual1.AsTest().Must().BeSameReferenceAs(protocol1);
            actual2.AsTest().Must().BeSameReferenceAs(protocol2);
            actual3.AsTest().Must().BeSameReferenceAs(protocol2);
        }
예제 #2
0
        public static void Constructor___Should_register_protocol_factories_in_order_specified___When_called()
        {
            // Arrange
            IProtocol protocol1 = new SharedOperationProtocol1();
            IProtocol protocol2 = new SiblingOperationProtocol();
            IProtocol protocol3 = new SharedOperationProtocol2();

            var protocolFactory1 = protocol1.ToProtocolFactory();
            var protocolFactory2 = protocol2.ToProtocolFactory();
            var protocolFactory3 = protocol3.ToProtocolFactory();

            var systemUnderTest = new ChainOfResponsibilityProtocolFactory(new[] { protocolFactory1, protocolFactory2, protocolFactory3 });

            var operation1 = new GetProtocolOp(new SharedOperation());
            var operation2 = new GetProtocolOp(new SiblingOperation1());
            var operation3 = new GetProtocolOp(new SiblingOperation2());

            // Act
            var actual1 = systemUnderTest.Execute(operation1);
            var actual2 = systemUnderTest.Execute(operation2);
            var actual3 = systemUnderTest.Execute(operation3);

            // Assert
            actual1.AsTest().Must().BeSameReferenceAs(protocol1);
            actual2.AsTest().Must().BeSameReferenceAs(protocol2);
            actual3.AsTest().Must().BeSameReferenceAs(protocol2);
        }
예제 #3
0
        public static void Constructor___Should_register_protocols_in_protocolTypeToGetProtocolFuncMap___When_called()
        {
            // Arrange
            IProtocol protocol1 = new SharedOperationProtocol1();
            IProtocol protocol2 = new SiblingOperationProtocol();

            var protocolTypeToGetProtocolFuncMap = new Dictionary <Type, Func <IProtocol> >
            {
                { typeof(SharedOperationProtocol1), () => protocol1 },
                { typeof(SiblingOperationProtocol), () => protocol2 },
            };

            var systemUnderTest = new ProtocolFactory(protocolTypeToGetProtocolFuncMap);

            var operation1 = new GetProtocolOp(new SharedOperation());
            var operation2 = new GetProtocolOp(new SiblingOperation1());
            var operation3 = new GetProtocolOp(new SiblingOperation2());

            // Act
            var actual1 = systemUnderTest.Execute(operation1);
            var actual2 = systemUnderTest.Execute(operation2);
            var actual3 = systemUnderTest.Execute(operation3);

            // Assert
            actual1.AsTest().Must().BeSameReferenceAs(protocol1);
            actual2.AsTest().Must().BeSameReferenceAs(protocol2);
            actual3.AsTest().Must().BeSameReferenceAs(protocol2);
        }
예제 #4
0
        public static void Execute___Should_return_last_registered_protocol___When_second_protocol_supporting_same_operation_is_registered_with_ProtocolAlreadyRegisteredForOperationStrategy_Replace()
        {
            // Arrange
            var systemUnderTest = new ProtocolFactory();

            IProtocol protocol1 = new SharedOperationProtocol1();
            IProtocol protocol2 = new SiblingOperationProtocol();

            systemUnderTest.RegisterProtocolForSupportedOperations(typeof(SharedOperationProtocol1), () => new SharedOperationProtocol1());
            systemUnderTest.RegisterProtocolForSupportedOperations(typeof(SiblingOperationProtocol), () => new SiblingOperationProtocol());

            systemUnderTest.RegisterProtocolForSupportedOperations(typeof(SharedOperationProtocol1), () => protocol1, ProtocolAlreadyRegisteredForOperationStrategy.Replace);
            systemUnderTest.RegisterProtocolForSupportedOperations(typeof(SiblingOperationProtocol), () => protocol2, ProtocolAlreadyRegisteredForOperationStrategy.Replace);

            var operation1 = new GetProtocolOp(new SharedOperation());
            var operation2 = new GetProtocolOp(new SiblingOperation1());
            var operation3 = new GetProtocolOp(new SiblingOperation2());

            // Act
            var actual1 = systemUnderTest.Execute(operation1);
            var actual2 = systemUnderTest.Execute(operation2);
            var actual3 = systemUnderTest.Execute(operation3);

            // Assert
            actual1.AsTest().Must().BeSameReferenceAs(protocol1);
            actual2.AsTest().Must().BeSameReferenceAs(protocol2);
            actual3.AsTest().Must().BeSameReferenceAs(protocol2);
        }
예제 #5
0
        public static void Execute___Should_return_registered_protocol___When_called()
        {
            // Arrange
            var systemUnderTest = new ProtocolFactory();

            IProtocol protocol1 = new SharedOperationProtocol1();
            IProtocol protocol2 = new SiblingOperationProtocol();

            systemUnderTest.RegisterProtocolForSupportedOperations(typeof(SharedOperationProtocol1), () => protocol1);
            systemUnderTest.RegisterProtocolForSupportedOperations(typeof(SiblingOperationProtocol), () => protocol2);

            var operation1 = new GetProtocolOp(new SharedOperation());
            var operation2 = new GetProtocolOp(new SiblingOperation1());
            var operation3 = new GetProtocolOp(new SiblingOperation2());

            // Act
            var actual1 = systemUnderTest.Execute(operation1);
            var actual2 = systemUnderTest.Execute(operation2);
            var actual3 = systemUnderTest.Execute(operation3);

            // Assert
            actual1.AsTest().Must().BeSameReferenceAs(protocol1);
            actual2.AsTest().Must().BeSameReferenceAs(protocol2);
            actual3.AsTest().Must().BeSameReferenceAs(protocol2);
        }
예제 #6
0
        public static void ShallowClone___Should_return_a_shallow_clone___When_called()
        {
            // Arrange
            var systemUnderTest1 = new ProtocolFactory();
            var systemUnderTest2 = new ProtocolFactory();

            IProtocol protocol1 = new SharedOperationProtocol1();
            IProtocol protocol2 = new SiblingOperationProtocol();

            systemUnderTest1.RegisterProtocolForSupportedOperations(typeof(SharedOperationProtocol1), () => protocol1);
            systemUnderTest2.RegisterProtocolForSupportedOperations(typeof(SharedOperationProtocol1), () => protocol1);

            // Act
            var actual1 = systemUnderTest1.ShallowClone();
            var actual2 = systemUnderTest2.ShallowClone();

            actual1.RegisterProtocolForSupportedOperations(typeof(SiblingOperationProtocol), () => protocol2);
            systemUnderTest2.RegisterProtocolForSupportedOperations(typeof(SiblingOperationProtocol), () => protocol2);

            // Assert
            var operation1            = new GetProtocolOp(new SharedOperation());
            var protocolFromOriginal1 = systemUnderTest1.Execute(operation1);
            var protocolFromOriginal2 = systemUnderTest2.Execute(operation1);
            var protocolFromClone1    = actual1.Execute(operation1);
            var protocolFromClone2    = actual2.Execute(operation1);

            protocolFromOriginal1.AsTest().Must().BeSameReferenceAs(protocol1);
            protocolFromOriginal2.AsTest().Must().BeSameReferenceAs(protocol1);
            protocolFromClone1.AsTest().Must().BeSameReferenceAs(protocol1);
            protocolFromClone2.AsTest().Must().BeSameReferenceAs(protocol1);

            var operation2 = new GetProtocolOp(new SiblingOperation1(), MissingProtocolStrategy.ReturnNull);

            protocolFromOriginal1 = systemUnderTest1.Execute(operation2);
            protocolFromOriginal2 = systemUnderTest2.Execute(operation2);
            protocolFromClone1    = actual1.Execute(operation2);
            protocolFromClone2    = actual2.Execute(operation2);
            protocolFromOriginal1.AsTest().Must().BeNull();
            protocolFromOriginal2.AsTest().Must().BeSameReferenceAs(protocol2);
            protocolFromClone1.AsTest().Must().BeSameReferenceAs(protocol2);
            protocolFromClone2.AsTest().Must().BeNull();
        }