Click here to Skip to main content
15,895,667 members
Articles / Web Development / ASP.NET

MVP Dependency Chaining Framework

Rate me:
Please Sign up or sign in to vote.
4.45/5 (9 votes)
14 Oct 200718 min read 45.9K   373   47  
An MVP framework that generates an intercepting filter layer consisting of a linked list of presenters by parsing a set of dependency rules.
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using NUnit.Framework;
using MVPChainingFactory.Factory;
using MVPChainingFactory.Caching;
using MVPChainingFactory.Presenters;
using MVPChainingFactory.View;
using Rhino.Mocks;
using Rhino.Mocks.Interfaces;
using Rhino.Mocks.Constraints;


namespace MVPChainingFactory.Tests
{
    [TestFixture]
    [Category("Factory load order")]
    public class PresenterLoadOrderFixture
    {
        MockRepository _rep;
        ICacheItemManager<Dictionary<Type, ConstructorInfo>> _mockMetadataCacheManager;
        ICacheItemManager<PresenterSkeletonChain> _mockPresenterChainCacheManager;

        // Intercept calls to the mock PresenterChainCacheManager to validate return values
        delegate void PutSimulator(PresenterSkeletonChain head);

        private List<Type> ToList(PresenterSkeletonChain chain)
        {
            List<Type> l = new List<Type>();
            foreach (PresenterSkeleton node in chain) l.Add(node.PresenterType);
            return l;
        }

        [SetUp]
        public void Setup()
        {
            _rep = new MockRepository();
            _mockMetadataCacheManager =
                _rep.CreateMock<ICacheItemManager<Dictionary<Type, ConstructorInfo>>>();
            _mockPresenterChainCacheManager =
                _rep.CreateMock<ICacheItemManager<PresenterSkeletonChain>>();

            // Setup initial expectations for the call to InitializeFactory() in [Setup]
            // Note - Every [Test] must make a call to InitializeFactory (which is required 
            // to construct the PresenterFactory anyway)
            _mockMetadataCacheManager.CacheDependencyAssembly = null;
            LastCall.Constraints(Is.NotNull());
            _mockMetadataCacheManager.CacheInvalidated += null;
            LastCall.IgnoreArguments();

            _mockPresenterChainCacheManager.CacheDependencyAssembly = null;
            LastCall.Constraints(Is.NotNull());
            _mockPresenterChainCacheManager.CacheInvalidated += null;
            LastCall.IgnoreArguments();
            
        }

        [Test]
        public void TestPresenterLoadAndOrderingOfUsecase1()
        {
            /*
             * INPUT = 
             *      DEFAULT[TAIL] -> P1 -> P2[AFTER P4] -> P3[HEAD] -> P4[AFTER P5] -> P5
             * 
             * EXPECTED OUTPUT = 
             *      P3 -> P1 -> P5 -> P4 -> P2 -> DEFAULT
             */

            string _assemblyName = "StubPresentersUsecase1";
            int countOfPresenters = 6;
            PresenterSkeletonChain p = null;
            using (_rep.Record())
            {
                _mockPresenterChainCacheManager.Put(null);
                LastCall.Constraints(Is.NotNull()).Do((PutSimulator)
                        delegate(PresenterSkeletonChain head)
                        {
                            p = head;
                            return;
                        }
                    );
                _mockMetadataCacheManager.Put(null);
                LastCall.Constraints(Is.NotNull());
            }
            using (_rep.Playback())
            {
                PresenterFactory.InitializeFactory(_mockMetadataCacheManager, _mockPresenterChainCacheManager, _assemblyName);
                PresenterFactory.ScanPresenters();
            }
            string mesg = "Expected count={0}, actual count={1}";
            Assert.IsNotNull(p);
            Assert.AreEqual(countOfPresenters, p.Count,
                String.Format(mesg, countOfPresenters, p.Count));
            
            // Check the HEAD and TAIL nodes
            mesg = "Expected presenter type at position {0}={1}, Actual={2}";
            List<Type> orderOfPresenters = ToList(p);
            Assert.AreEqual(typeof(P3), orderOfPresenters[0],
                String.Format(mesg, 0, typeof(P3), orderOfPresenters[0]));
            Assert.AreEqual(typeof(DefaultPresenter), orderOfPresenters[5],
                String.Format(mesg, 0, typeof(DefaultPresenter), orderOfPresenters[5]));

            // Check defined dependencies
            mesg = "Expected that {0} comes after {1}. Actual index of {0} = {2} and {1} = {3}";
            int l = 0, r = 0;
            l = orderOfPresenters.IndexOf(typeof(P4));
            r = orderOfPresenters.IndexOf(typeof(P2));
            Assert.Less(l, r, String.Format(mesg, typeof(P2), typeof(P4), r, l));
            l = orderOfPresenters.IndexOf(typeof(P5));
            r = orderOfPresenters.IndexOf(typeof(P4));
            Assert.Less(l, r, String.Format(mesg, typeof(P4), typeof(P5), r, l));
        }

        [Test]
        public void TestPresenterLoadAndOrderingOfUsecase2()
        {
            /*
             * INPUT = 
             *          P6[TAIL] -> P7 -> P8 -> P9[HEAD] -> P10[AFTER P8]
             * 
             * EXPECTED OUTPUT = 
             *          P9 -> P7 -> P8 -> P10 -> P6
             */
            string _assemblyName = "StubPresentersUsecase2";
            int countOfPresenters = 5;
            PresenterSkeletonChain p = null;
            using (_rep.Record())
            {
                _mockPresenterChainCacheManager.Put(null);
                LastCall.Constraints(Is.NotNull()).Do((PutSimulator)
                        delegate(PresenterSkeletonChain head)
                        {
                            p = head;
                            return;
                        }
                    );
                _mockMetadataCacheManager.Put(null);
                LastCall.Constraints(Is.NotNull());
            }
            using (_rep.Playback())
            {
                PresenterFactory.InitializeFactory(_mockMetadataCacheManager, _mockPresenterChainCacheManager, _assemblyName);
                PresenterFactory.ScanPresenters();
            }
            string mesg = "Expected count={0}, actual count={1}";
            Assert.IsNotNull(p);
            Assert.AreEqual(countOfPresenters, p.Count,
                String.Format(mesg, countOfPresenters, p.Count));

            // Check the HEAD and TAIL nodes
            mesg = "Expected presenter type at position {0}={1}, Actual={2}";
            List<Type> orderOfPresenters = ToList(p);
            Assert.AreEqual(typeof(P9), orderOfPresenters[0],
                String.Format(mesg, 0, typeof(P9), orderOfPresenters[0]));
            Assert.AreEqual(typeof(P6), orderOfPresenters[4],
                String.Format(mesg, 0, typeof(P6), orderOfPresenters[4]));

            // Check defined dependencies
            mesg = "Expected that {0} comes after {1}. Actual index of {0} = {2} and {1} = {3}";
            int l = 0, r = 0;
            l = orderOfPresenters.IndexOf(typeof(P8));
            r = orderOfPresenters.IndexOf(typeof(P10));
            Assert.Less(l, r, String.Format(mesg, typeof(P10), typeof(P8), r, l));            
        }

        [Test]
        public void TestPresenterLoadAndOrderingOfUsecase3()
        {
            /*
             * INPUT = 
             *          P11 -> P12[TAIL] -> P13 -> P14[HEAD] -> P15
             * EXPECTED OUTPUT = 
             *          P14 -> P11 -> P13 -> P15 -> P12
             */
            string _assemblyName = "StubPresentersUsecase3";
            int countOfPresenters = 5;
            PresenterSkeletonChain p = null;
            using (_rep.Record())
            {
                _mockPresenterChainCacheManager.Put(null);
                LastCall.Constraints(Is.NotNull()).Do((PutSimulator)
                        delegate(PresenterSkeletonChain head)
                        {
                            p = head;
                            return;
                        }
                    );
                _mockMetadataCacheManager.Put(null);
                LastCall.Constraints(Is.NotNull());
            }
            using (_rep.Playback())
            {
                PresenterFactory.InitializeFactory(
                    _mockMetadataCacheManager, _mockPresenterChainCacheManager, _assemblyName);
                PresenterFactory.ScanPresenters();
            }
            string mesg = "Expected count={0}, actual count={1}";
            Assert.IsNotNull(p);
            Assert.AreEqual(countOfPresenters, p.Count,
                String.Format(mesg, countOfPresenters, p.Count));

            // Check the HEAD and TAIL nodes
            mesg = "Expected presenter type at position {0}={1}, Actual={2}";
            List<Type> orderOfPresenters = ToList(p);
            Assert.AreEqual(typeof(P14), orderOfPresenters[0],
                String.Format(mesg, 0, typeof(P14), orderOfPresenters[0]));
            Assert.AreEqual(typeof(P12), orderOfPresenters[4],
                String.Format(mesg, 0, typeof(P12), orderOfPresenters[4]));            
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I am a tech lead working for Cap Gemini. Although I primarily work with the Microsoft technology stack (including .NET and legacy technologies) I like to keep myself informed about developments in the Java world.

Comments and Discussions