public void CanRegisterTypeMappingOnRootContainer()
        {
            TestableRootCompositionContainer root = new TestableRootCompositionContainer();

            root.RegisterTypeMapping<IFoo, Foo>();

            Type mappedType = root.GetMappedType<IFoo>();

            Assert.AreEqual(typeof (Foo), mappedType);
        }
        public void CanRegisterMultipleTypeMappings()
        {
            TestableRootCompositionContainer root = new TestableRootCompositionContainer();

            root.RegisterTypeMapping<IFoo, Foo>();
            root.RegisterTypeMapping<IBar, Bar>();

            Assert.AreEqual(typeof (Bar), root.GetMappedType<IBar>());
            Assert.AreEqual(typeof (Foo), root.GetMappedType<IFoo>());
        }
        public void RegisteredModuleServiceInstanceGetsAddedToModuleServicesCollection()
        {
            CompositionContainer rootContainer = new TestableRootCompositionContainer();
            CompositionContainer childContainer = rootContainer.Containers.AddNew<CompositionContainer>();
            ServiceInfo info = new ServiceInfo(typeof (IMockService), typeof (MockService), ServiceScope.Module);
            ServiceLoaderService serviceLoader = new ServiceLoaderService();

            serviceLoader.Load(childContainer, info);

            Assert.IsTrue(childContainer.Services.Contains(typeof (IMockService)));
            Assert.AreEqual(typeof (MockService), childContainer.Services.Get<IMockService>(true).GetType());
        }
        public void PolicyShouldRequestTypeMappingFromContainer()
        {
            TestableRootCompositionContainer container = new TestableRootCompositionContainer();
            container.RegisterTypeMapping<IFoo, Foo>();

            ContainerAwareTypeMappingPolicy policy = new ContainerAwareTypeMappingPolicy();

            string requestId = "My mapped object";
            DependencyResolutionLocatorKey requestKey =
                new DependencyResolutionLocatorKey(typeof (IFoo), requestId);
            DependencyResolutionLocatorKey result = policy.Map(container.Locator, requestKey);

            Assert.AreEqual(typeof (Foo), result.Type);
            Assert.AreEqual(requestId, result.ID);
        }
        public void PolicyCallsUpToParentContainerToGetMapping()
        {
            TestableRootCompositionContainer container = new TestableRootCompositionContainer();
            container.RegisterTypeMapping<IFoo, Foo>();
            CompositionContainer child = container.Containers.AddNew<CompositionContainer>();

            ContainerAwareTypeMappingPolicy policy = new ContainerAwareTypeMappingPolicy();

            string requestId = "My mapped object";
            DependencyResolutionLocatorKey requestKey =
                new DependencyResolutionLocatorKey(typeof (IFoo), requestId);
            DependencyResolutionLocatorKey result = policy.Map(child.Locator, requestKey);

            Assert.AreEqual(typeof (Foo), result.Type);
            Assert.AreEqual(requestId, result.ID);
        }
        public void StrategyShouldDoTypeMapping()
        {
            TestableRootCompositionContainer root = new TestableRootCompositionContainer();
            root.RegisterTypeMapping<IFoo, Foo>();

            ContainerAwareTypeMappingStrategy strategy = new ContainerAwareTypeMappingStrategy();
            MockBuilderContext builderContext =
                new MockBuilderContext(strategy, new ReturnRequestedTypeStrategy());
            builderContext.Policies.SetDefault<IContainerAwareTypeMappingPolicy>(
                new ContainerAwareTypeMappingPolicy());
            builderContext.Locator = root.Locator;

            object result = strategy.BuildUp(builderContext, typeof (IFoo), null, "Test object");
            DependencyResolutionLocatorKey key = result as DependencyResolutionLocatorKey;
            Assert.IsNotNull(key);
            Assert.AreEqual(typeof (Foo), key.Type);
        }
        public void GetContainerReturnsContainerOfModuleInSubFolder()
        {
            TestableRootCompositionContainer rootContainer = new TestableRootCompositionContainer();
            CompositionContainer module1Container = rootContainer.Containers.AddNew<CompositionContainer>("Module1");
            rootContainer.Containers.AddNew<CompositionContainer>("Shell");
            MockEnumeratorService enumerator = rootContainer.Services.AddNew<MockEnumeratorService, IModuleEnumerator>();
            enumerator.ModuleInfos = new ModuleInfo[]
                {
                    new ModuleInfo("Shell", "Shell.Assembly", "~/"),
                    new ModuleInfo("Module1", "Module1.Assembly", "~/Module1")
                };
            ModuleContainerLocatorService service = rootContainer.Services.AddNew<ModuleContainerLocatorService>();

            CompositionContainer result = service.GetContainer("~/Module1/Page.aspx");

            Assert.AreSame(module1Container, result);
        }
        public void TestServicesAreTornDownWhenContainerIsDisposed()
        {
            TestableRootCompositionContainer container = new TestableRootCompositionContainer();

            MockService svc = container.Services.AddNew<MockService>();

            container.Dispose();

            Assert.IsTrue(svc.OnTearingDownCalled);
        }
 public void RootContainerParentIsNull()
 {
     TestableRootCompositionContainer container = new TestableRootCompositionContainer();
     Assert.IsNull(container.Parent);
 }
        public void TerminatingCompositionContainerCausesItToBeRemovedFromParent()
        {
            TestableRootCompositionContainer parent = new TestableRootCompositionContainer();
            CompositionContainer child = parent.Containers.AddNew<CompositionContainer>();

            Assert.AreEqual(1, parent.Containers.Count);
            child.Dispose();
            Assert.AreEqual(0, parent.Containers.Count);
        }
        public void RequestingTypeMappingOnChildReadsFromParent()
        {
            TestableRootCompositionContainer parent = new TestableRootCompositionContainer();
            CompositionContainer child = parent.Containers.AddNew<CompositionContainer>();

            parent.RegisterTypeMapping<IFoo, Foo>();

            Assert.AreEqual(typeof (Foo), child.GetMappedType<IFoo>());
        }
        public void RootContainerCanAccessToRootContainer()
        {
            TestableRootCompositionContainer root = new TestableRootCompositionContainer();

            Assert.AreEqual(root, root.RootContainer);
        }
        public void CreateInstancenotInjectNonRequiredNotPresentServiceDependency()
        {
            PolicyList policies = new PolicyList();
            MockBuilderContext ctx = BuildContext(policies);
            TestableRootCompositionContainer compositionContainer = new TestableRootCompositionContainer();
            ctx.Locator.Add(new DependencyResolutionLocatorKey(typeof (CompositionContainer), null), compositionContainer);

            MockDependencyDependingObjectNotRequired mockInstance =
                (MockDependencyDependingObjectNotRequired)
                ctx.HeadOfChain.BuildUp(ctx, typeof (MockDependencyDependingObjectNotRequired), null, null);

            Assert.IsNotNull(mockInstance);
            Assert.IsNull(mockInstance.DependentObject);
        }
        public void RequestingTypeMappingForUnmappedTypeReturnsRequestedType()
        {
            TestableRootCompositionContainer root = new TestableRootCompositionContainer();

            Type mappedType = root.GetMappedType<Foo>();

            Assert.AreEqual(typeof (Foo), mappedType);
        }
        public void GetContainerReturnsShellContainerEvenIfThereIsAFoundationalModule()
        {
            TestableRootCompositionContainer rootContainer = new TestableRootCompositionContainer();
            rootContainer.Containers.AddNew<CompositionContainer>("Module1");
            CompositionContainer shellContainer = rootContainer.Containers.AddNew<CompositionContainer>("Shell");
            MockEnumeratorService enumerator = rootContainer.Services.AddNew<MockEnumeratorService, IModuleEnumerator>();
            enumerator.ModuleInfos = new ModuleInfo[]
                {
                    new ModuleInfo("Shell", "Shell.Assembly", "~/"),
                    new ModuleInfo("FoundationalModule", "FoundationalModule.Assembly", null),
                    new ModuleInfo("Module1", "Module1.Assembly", "~/Module1")
                };
            ModuleContainerLocatorService service = rootContainer.Services.AddNew<ModuleContainerLocatorService>();

            CompositionContainer result = service.GetContainer("~/Page.aspx");

            Assert.AreSame(shellContainer, result);
        }
        public void LoadCallsLoadOnModuleInitializer()
        {
            CompositionContainer mockContainer = new TestableRootCompositionContainer();
            ModuleLoaderService loader = new ModuleLoaderService();

            loader.Load(mockContainer,
                        new ModuleInfo("TestModule", generatedAssemblies["TestModule"].FullName, "~/TestModule"));

            bool loadCalled =
                (bool)
                generatedAssemblies["TestModule"].GetType("TestModule.TestModuleInitializer").GetField("LoadCalled").GetValue(null);
            Assert.IsTrue(loadCalled);
        }
        public void LoadCreatesNewContainerForModule()
        {
            CompositionContainer mockContainer = new TestableRootCompositionContainer();
            ModuleLoaderService loader = new ModuleLoaderService();

            loader.Load(mockContainer,
                        new ModuleInfo("TestModuleName", generatedAssemblies["TestModule"].FullName, "~/TestModule"));

            CompositionContainer moduleContainer = mockContainer.Containers["TestModuleName"];
            Assert.IsNotNull(moduleContainer);
        }
        public void GetContainerReturnsRootContainerIfNoModules()
        {
            TestableRootCompositionContainer rootContainer = new TestableRootCompositionContainer();
            rootContainer.Services.AddNew<MockEnumeratorService, IModuleEnumerator>();
            ModuleContainerLocatorService service = rootContainer.Services.AddNew<ModuleContainerLocatorService>();

            CompositionContainer result = service.GetContainer("~/SubFolder/Page.aspx");
            CompositionContainer result2 = service.GetContainer("~/Page.aspx");

            Assert.AreSame(rootContainer, result);
            Assert.AreSame(result, result2);
        }
        public void LoadRegistersServicesUsingIServiceLoaderService()
        {
            CompositionContainer mockContainer = new TestableRootCompositionContainer();
            MockServiceLoaderService serviceLoaderService = new MockServiceLoaderService();
            mockContainer.Services.Add<IServiceLoaderService>(serviceLoaderService);
            ModuleLoaderService loader = new ModuleLoaderService();
            DependantModuleInfo moduleInfo =
                new DependantModuleInfo("TestModule", generatedAssemblies["TestModule"].FullName, "~/TestModule");
            moduleInfo.Services =
                new ServiceInfo[1] {new ServiceInfo(typeof (IMockService), typeof (MockService), ServiceScope.Global)};

            loader.Load(mockContainer, moduleInfo);

            Assert.IsNotNull(serviceLoaderService.UsedServices);
            Assert.AreEqual(1, serviceLoaderService.UsedServices.Length);
            Assert.AreEqual(typeof (IMockService), serviceLoaderService.UsedServices[0].RegisterAs);
            Assert.IsNotNull(serviceLoaderService.UsedCompositionContainer);
            Assert.AreEqual(mockContainer.Containers["TestModule"], serviceLoaderService.UsedCompositionContainer);
        }
 public MockWebClientApplication()
 {
     _rootContainer = new TestableRootCompositionContainer();
 }
        public void CreateInstancenotInjectsRegisteredInterfaceService()
        {
            PolicyList policies = new PolicyList();
            MockBuilderContext ctx = BuildContext(policies);
            TestableRootCompositionContainer compositionContainer = new TestableRootCompositionContainer();
            ctx.Locator.Add(new DependencyResolutionLocatorKey(typeof (CompositionContainer), null), compositionContainer);
            MockDependentObject mockDependency =
                compositionContainer.Services.AddNew<MockDependentObject, IMockDependentObject>();

            MockDependencyDependingInterface mockInstance =
                (MockDependencyDependingInterface)
                ctx.HeadOfChain.BuildUp(ctx, typeof (MockDependencyDependingInterface), null, null);

            Assert.IsNotNull(mockInstance);
            Assert.IsNotNull(mockInstance.DependentObject);
            Assert.AreSame(mockDependency, mockInstance.DependentObject);
        }
 public void TypeMappingsMustBeTypeCompatible()
 {
     TestableRootCompositionContainer root = new TestableRootCompositionContainer();
     root.RegisterTypeMapping(typeof (IBar), typeof (Foo));
 }
        public void DisposingContainerCausesContainedObjectsToBeTornDown()
        {
            TestableRootCompositionContainer cc = new TestableRootCompositionContainer();
            MockTearDownStrategy strategy = new MockTearDownStrategy();
            cc.Builder.Strategies.Add(strategy, WCSFBuilderStage.PreCreation);

            cc.Dispose();

            Assert.IsTrue(strategy.TearDownCalled);
        }
        public void ChildContainerAreDisposedWithParent()
        {
            TestableRootCompositionContainer parent = new TestableRootCompositionContainer();
            CompositionContainer child = parent.Containers.AddNew<CompositionContainer>();

            MockService svc = child.Services.AddNew<MockService>();
            parent.Dispose();

            Assert.IsTrue(svc.OnTearingDownCalled);
        }
        public void NestedContainerIntegrationTest()
        {
            CompositionContainer parentContainer = new TestableRootCompositionContainer();
            CompositionContainer childContainer = parentContainer.Containers.AddNew<CompositionContainer>();

            MockA a = new MockA();
            MockA b = new MockA();

            parentContainer.Services.Add(typeof (MockA), a);
            childContainer.Services.Add(typeof (MockA), b);

            Assert.AreSame(a, parentContainer.Services.Get<MockA>());
            Assert.AreSame(b, childContainer.Services.Get<MockA>());

            MockB c = new MockB();

            parentContainer.Services.Add(typeof (MockB), c);

            // Throws ArgumentException
            childContainer.Services.AddOnDemand(typeof (MockB));
        }
        public void CanRegisterTypeMappingViaTypeObjects()
        {
            TestableRootCompositionContainer root = new TestableRootCompositionContainer();
            root.RegisterTypeMapping(typeof (IFoo), typeof (Foo));

            Type mappedType = root.GetMappedType(typeof (IFoo));

            Assert.AreEqual(typeof (Foo), mappedType);
        }
        public void ChildContainerCanAccessToRootContainer()
        {
            TestableRootCompositionContainer root = new TestableRootCompositionContainer();
            CompositionContainer child = root.Containers.AddNew<CompositionContainer>();
            CompositionContainer innerChild = child.Containers.AddNew<CompositionContainer>();

            Assert.AreEqual(root, innerChild.RootContainer);
        }
        public void GetContainerReturnsRootContainerIfNoModulesFoundWithGivenVirtualPath()
        {
            TestableRootCompositionContainer rootContainer = new TestableRootCompositionContainer();
            rootContainer.Containers.AddNew<CompositionContainer>("Module1");
            MockEnumeratorService enumerator = rootContainer.Services.AddNew<MockEnumeratorService, IModuleEnumerator>();
            enumerator.ModuleInfos = new ModuleInfo[] {new ModuleInfo("Module1", "Module1.Assembly", "~/Module1")};
            ModuleContainerLocatorService service = rootContainer.Services.AddNew<ModuleContainerLocatorService>();

            CompositionContainer result = service.GetContainer("~/Page.aspx");

            Assert.AreSame(rootContainer, result);
        }
        public void ChildContainerHasParent()
        {
            TestableRootCompositionContainer parent = new TestableRootCompositionContainer();
            CompositionContainer child = parent.Containers.AddNew<CompositionContainer>();

            Assert.AreSame(parent, child.Parent);
        }
        public void ChildContainersCanOverrideParentTypeMapping()
        {
            TestableRootCompositionContainer parent = new TestableRootCompositionContainer();
            CompositionContainer child = parent.Containers.AddNew<CompositionContainer>();

            parent.RegisterTypeMapping<IFoo, Foo>();
            child.RegisterTypeMapping<IFoo, Foo2>();

            Assert.AreEqual(typeof (Foo), parent.GetMappedType<IFoo>());
            Assert.AreEqual(typeof (Foo2), child.GetMappedType<IFoo>());
        }
        public void InitializationExceptionsAreWrapped()
        {
            CompositionContainer mockContainer = new TestableRootCompositionContainer();
            ModuleLoaderService loader = new ModuleLoaderService();

            try
            {
                loader.Load(mockContainer,
                            new ModuleInfo("ModuleThrowingException", generatedAssemblies["ModuleThrowingException"].FullName,
                                           "~/ModuleThrowingException"));

                Assert.Fail("ModuleLoadException was not thrown");
            }
            catch (Exception ex)
            {
                if (!(ex is ModuleLoadException))
                    Assert.Fail("Exception is not of type ModuleLoadException");

                if (!(((ModuleLoadException) ex).InnerException is NotImplementedException))
                    Assert.Fail("The actual inner exception was not wrapped correctly");
            }
        }