Пример #1
0
        public void CreateWillNotTriggerHandlingOnSecondarySameRequestWhenDecoratedBuilderThrows()
        {
            // Fixture setup
            var builder = new DelegatingSpecimenBuilder {
                OnCreate = (r, c) =>
                {
                    throw new PrivateExpectedException("The decorated builder always throws.");
                }
            };

            var sut = new DelegatingRecursionGuard(builder)
            {
                OnHandleRecursiveRequest = o =>
                {
                    throw new PrivatUnexpectedException("Recursive handling should not be triggered.");
                }
            };

            var dummyContext = new DelegatingSpecimenContext();
            var sameRequest  = new object();

            // Exercise system and verify outcome
            Assert.Throws <PrivateExpectedException>(() => sut.Create(sameRequest, dummyContext));
            Assert.Empty(sut.UnprotectedRecordedRequests);
            Assert.Throws <PrivateExpectedException>(() => sut.Create(sameRequest, dummyContext));
            Assert.Empty(sut.UnprotectedRecordedRequests);
        }
Пример #2
0
        public void CreateWillNotTriggerHandlingUntilDeeperThanRecursionDepth()
        {
            // Fixture setup
            var requestScenario = new Queue <int>(new[] { 1, 2, 1, 3, 1, 4 });
            var builder         = new DelegatingSpecimenBuilder();

            builder.OnCreate = (r, c) => c.Resolve(requestScenario.Dequeue());

            // By setting the depth to two we expect the handle to be triggered at the third "1" occurence.
            var    sut = new DelegatingRecursionGuard(builder, 2);
            object recursiveRequest = null;

            sut.OnHandleRecursiveRequest = obj => recursiveRequest = obj;


            var container = new DelegatingSpecimenContext();

            container.OnResolve = r => sut.Create(r, container);

            // Exercise system
            sut.Create(5, container);

            // Verify outcome
            // Check that recursion was actually detected as expected
            Assert.Equal(1, recursiveRequest);
            // Check that we passed the first recursion, but didn't go any further
            Assert.Equal(4, requestScenario.Dequeue());
        }
Пример #3
0
        public void CreateWillTriggerHandlingOnSecondLevelRecursiveRequest()
        {
            // Fixture setup
            object subRequest1     = Guid.NewGuid();
            object subRequest2     = Guid.NewGuid();
            var    requestScenario = new Stack <object>(new [] { subRequest1, subRequest2, subRequest1 });
            var    builder         = new DelegatingSpecimenBuilder();

            builder.OnCreate = (r, c) => c.Resolve(requestScenario.Pop());

            var    sut = new DelegatingRecursionGuard(builder);
            object recursiveRequest = null;

            sut.OnHandleRecursiveRequest = obj => recursiveRequest = obj;

            var container = new DelegatingSpecimenContext();

            container.OnResolve = (r) => sut.Create(r, container);

            // Exercise system
            sut.Create(Guid.NewGuid(), container);

            // Verify outcome
            Assert.Same(subRequest1, recursiveRequest);
        }
Пример #4
0
 public void SutIsNode()
 {
     // Fixture setup
     var dummyBuilder = new DelegatingSpecimenBuilder();
     // Exercise system
     var sut = new DelegatingRecursionGuard(dummyBuilder);
     // Verify outcome
     Assert.IsAssignableFrom<ISpecimenBuilderNode>(sut);
     // Teardown
 }
Пример #5
0
 public void SutYieldsInjectedBuilder()
 {
     // Fixture setup
     var expected = new DelegatingSpecimenBuilder();
     var sut = new DelegatingRecursionGuard(expected);
     // Exercise system
     // Verify outcome
     Assert.Equal(expected, sut.Single());
     Assert.Equal(expected, ((System.Collections.IEnumerable)sut).Cast<object>().Single());
     // Teardown
 }
Пример #6
0
        public void SutIsNode()
        {
            // Fixture setup
            var dummyBuilder = new DelegatingSpecimenBuilder();
            // Exercise system
            var sut = new DelegatingRecursionGuard(dummyBuilder);

            // Verify outcome
            Assert.IsAssignableFrom <ISpecimenBuilderNode>(sut);
            // Teardown
        }
Пример #7
0
 public void ComparerIsCorrect()
 {
     // Fixture setup
     var dummyBuilder = new DelegatingSpecimenBuilder();
     var expected = new DelegatingEqualityComparer();
     var sut = new DelegatingRecursionGuard(dummyBuilder, expected);
     // Exercise system
     IEqualityComparer actual = sut.Comparer;
     // Verify outcome
     Assert.Equal(expected, actual);
     // Teardown
 }
Пример #8
0
        public void SutYieldsInjectedBuilder()
        {
            // Fixture setup
            var expected = new DelegatingSpecimenBuilder();
            var sut      = new DelegatingRecursionGuard(expected);

            // Exercise system
            // Verify outcome
            Assert.Equal(expected, sut.Single());
            Assert.Equal(expected, ((System.Collections.IEnumerable)sut).Cast <object>().Single());
            // Teardown
        }
Пример #9
0
        public void ComparerIsCorrect()
        {
            // Fixture setup
            var dummyBuilder = new DelegatingSpecimenBuilder();
            var expected     = new DelegatingEqualityComparer();
            var sut          = new DelegatingRecursionGuard(dummyBuilder, expected);
            // Exercise system
            IEqualityComparer actual = sut.Comparer;

            // Verify outcome
            Assert.Equal(expected, actual);
            // Teardown
        }
Пример #10
0
        public void CreateWillNotTriggerHandlingOnFirstRequest()
        {
            // Fixture setup
            var  sut = new DelegatingRecursionGuard(new DelegatingSpecimenBuilder());
            bool handlingTriggered = false;

            sut.OnHandleRecursiveRequest = obj => handlingTriggered = true;

            // Exercise system
            sut.Create(Guid.NewGuid(), new DelegatingSpecimenContext());

            // Verify outcome
            Assert.False(handlingTriggered);
        }
Пример #11
0
        public void CreateWillTriggerHandlingOnRecursiveRequests()
        {
            // Fixture setup
            var builder = new DelegatingSpecimenBuilder();

            builder.OnCreate = (r, c) => c.Resolve(r);
            var  sut = new DelegatingRecursionGuard(builder);
            bool handlingTriggered = false;

            sut.OnHandleRecursiveRequest = obj => handlingTriggered = true;
            var container = new DelegatingSpecimenContext();

            container.OnResolve = (r) => sut.Create(r, container);

            // Exercise system
            sut.Create(Guid.NewGuid(), container);

            // Verify outcome
            Assert.True(handlingTriggered);
        }
Пример #12
0
        public void CreateWillUseEqualityComparer()
        {
            // Fixture setup
            var builder = new DelegatingSpecimenBuilder();

            builder.OnCreate = (r, c) => c.Resolve(r);
            bool comparerUsed = false;
            var  comparer     = new DelegatingEqualityComparer {
                OnEquals = (x, y) => comparerUsed = true
            };
            var sut = new DelegatingRecursionGuard(builder, comparer);

            sut.OnHandleRecursiveRequest = (obj) => { return(null); };
            var container = new DelegatingSpecimenContext();

            container.OnResolve = (r) => sut.Create(r, container);

            // Exercise system
            sut.Create(Guid.NewGuid(), container);

            // Verify outcome
            Assert.True(comparerUsed);
        }
Пример #13
0
        public void CreateWillNotTriggerHandlingOnSecondarySameRequestWhenDecoratedBuilderThrows()
        {
            // Fixture setup
            var builder = new DelegatingSpecimenBuilder { OnCreate = (r, c) =>
            {
                throw new PrivateExpectedException("The decorated builder always throws.");
            }};

            var sut = new DelegatingRecursionGuard(builder) { OnHandleRecursiveRequest = o =>
            {
                throw new PrivatUnexpectedException("Recursive handling should not be triggered.");
            }};

            var dummyContext = new DelegatingSpecimenContext();
            var sameRequest = new object();

            // Exercise system and verify outcome
            Assert.Throws<PrivateExpectedException>(() => sut.Create(sameRequest, dummyContext));
            Assert.Empty(sut.UnprotectedRecordedRequests);
            Assert.Throws<PrivateExpectedException>(() => sut.Create(sameRequest, dummyContext));
            Assert.Empty(sut.UnprotectedRecordedRequests);
        }
Пример #14
0
        public void CreateWillTriggerHandlingOnRecursiveRequests()
        {
            // Fixture setup
            var builder = new DelegatingSpecimenBuilder();
            builder.OnCreate = (r, c) => c.Resolve(r);
            var sut = new DelegatingRecursionGuard(builder);
            bool handlingTriggered = false;
            sut.OnHandleRecursiveRequest = obj => handlingTriggered = true;
            var container = new DelegatingSpecimenContext();
            container.OnResolve = (r) => sut.Create(r, container);

            // Exercise system
            sut.Create(Guid.NewGuid(), container);

            // Verify outcome
            Assert.True(handlingTriggered);
        }
Пример #15
0
        public void CreateWillTriggerHandlingOnSecondLevelRecursiveRequest()
        {
            // Fixture setup
            object subRequest1 = Guid.NewGuid();
            object subRequest2 = Guid.NewGuid();
            var requestScenario = new Stack<object>(new [] { subRequest1, subRequest2, subRequest1 });
            var builder = new DelegatingSpecimenBuilder();
            builder.OnCreate = (r, c) => c.Resolve(requestScenario.Pop());

            var sut = new DelegatingRecursionGuard(builder);
            object recursiveRequest = null;
            sut.OnHandleRecursiveRequest = obj => recursiveRequest = obj;

            var container = new DelegatingSpecimenContext();
            container.OnResolve = (r) => sut.Create(r, container);

            // Exercise system
            sut.Create(Guid.NewGuid(), container);

            // Verify outcome
            Assert.Same(subRequest1, recursiveRequest);
        }
Пример #16
0
        public void CreateWillNotTriggerHandlingUntilDeeperThanRecursionDepth()
        {
            // Fixture setup
            var requestScenario = new Queue<int>(new[] { 1, 2, 1, 3, 1, 4 });
            var builder = new DelegatingSpecimenBuilder();
            builder.OnCreate = (r, c) => c.Resolve(requestScenario.Dequeue());

            // By setting the depth to two we expect the handle to be triggered at the third "1" occurence.
            var sut = new DelegatingRecursionGuard(builder, 2);
            object recursiveRequest = null;

            sut.OnHandleRecursiveRequest = obj => recursiveRequest = obj;
            

            var container = new DelegatingSpecimenContext();
            container.OnResolve = r => sut.Create(r, container);

            // Exercise system
            sut.Create(5, container);

            // Verify outcome
            // Check that recursion was actually detected as expected
            Assert.Equal(1, recursiveRequest);
            // Check that we passed the first recursion, but didn't go any further
            Assert.Equal(4, requestScenario.Dequeue());
        }
Пример #17
0
        public void CreateWillUseEqualityComparer()
        {
            // Fixture setup
            var builder = new DelegatingSpecimenBuilder();
            builder.OnCreate = (r, c) => c.Resolve(r);
            bool comparerUsed = false;
            var comparer = new DelegatingEqualityComparer { OnEquals = (x, y) => comparerUsed = true };
            var sut = new DelegatingRecursionGuard(builder, comparer);
            sut.OnHandleRecursiveRequest = (obj) => { return null; };
            var container = new DelegatingSpecimenContext();
            container.OnResolve = (r) => sut.Create(r, container);

            // Exercise system
            sut.Create(Guid.NewGuid(), container);

            // Verify outcome
            Assert.True(comparerUsed);
        }
Пример #18
0
        public void CreateWillNotTriggerHandlingOnSubsequentSimilarRequests()
        {
            // Fixture setup
            var sut = new DelegatingRecursionGuard(new DelegatingSpecimenBuilder());
            bool handlingTriggered = false;
            object request = Guid.NewGuid();
            sut.OnHandleRecursiveRequest = obj => handlingTriggered = true;

            // Exercise system
            sut.Create(request, new DelegatingSpecimenContext());
            sut.Create(request, new DelegatingSpecimenContext());

            // Verify outcome
            Assert.False(handlingTriggered);
        }