public void CanCreateSingletonObjectWithDefaultObjectBuilder()
        {
            WCSFBuilder builder = new WCSFBuilder();
            Locator locator = new Locator();
            LifetimeContainer lifetime = new LifetimeContainer();
            locator.Add(typeof (ILifetimeContainer), lifetime);

            builder.Policies.Set<ISingletonPolicy>(new SingletonPolicy(true), typeof (MockObject), "foo");

            MockObject obj1 = builder.BuildUp<MockObject>(locator, "foo", null);
            MockObject obj2 = builder.BuildUp<MockObject>(locator, "foo", null);

            Assert.AreSame(obj1, obj2);
        }
        private static MockBuilderContext BuildContext()
        {
            IBuilderStrategy[] strategies = {
                                                new SimplifiedSingletonStrategy(),
                                                new MockCreationStrategy()
                                            };
            MockBuilderContext ctx = new MockBuilderContext(strategies);
            LifetimeContainer lifetimeContainer = new LifetimeContainer();

            if (!ctx.Locator.Contains(typeof (ILifetimeContainer)))
                ctx.Locator.Add(typeof (ILifetimeContainer), lifetimeContainer);

            ctx.Policies.SetDefault<ICreationPolicy>(new DefaultCreationPolicy());

            return ctx;
        }
        public void BuildUpReturnsInstanceOfStateValueWithCorrespondantValueFromSessionWhenInjectingProperties()
        {
            Locator locator = new Locator();
            LifetimeContainer container = new LifetimeContainer();
            locator.Add(typeof (ILifetimeContainer), container);
            WCSFBuilder builder = new WCSFBuilder();
            builder.Policies.SetDefault<ISingletonPolicy>(new SingletonPolicy(true));
            MockSessionStateLocatorService stateLocator = new MockSessionStateLocatorService();
            locator.Add(new DependencyResolutionLocatorKey(typeof (ISessionStateLocatorService), null), stateLocator);

            stateLocator.SessionState["intKey"] = 5;
            SampleClass builtObject =
                builder.BuildUp(locator, typeof (SampleClass), Guid.NewGuid().ToString(), null) as SampleClass;

            Assert.IsNotNull(builtObject.MyIntValue);
            Assert.AreEqual(5, builtObject.MyIntValue.Value);
        }
        public void RequestForLogWriterWithLocatorAddsSingleton()
        {
            Locator locator = new Locator();
            LifetimeContainer container = new LifetimeContainer();
            locator.Add(typeof(ILifetimeContainer), container);

            MockBuilderContext context = CreateContext(locator, new SystemConfigurationSource());

            createdObject1
                = context.HeadOfChain.BuildUp(context, typeof(LogWriter), null, null);
            createdObject2
                = context.HeadOfChain.BuildUp(context, typeof(LogWriter), null, null);

            Assert.IsNotNull(createdObject1);
            Assert.AreSame(typeof(LogWriter), createdObject1.GetType());
            Assert.IsTrue(object.ReferenceEquals(createdObject1, createdObject2));
        }
        public void RemovingNonContainedItemDoesNotThrow()
        {
            ILifetimeContainer container = new LifetimeContainer();

            container.Remove(new object());
        }
        public void SingletonPolicyBasedOnConcreteTypeRatherThanRequestedType()
        {
            WCSFBuilder builder = new WCSFBuilder();
            LifetimeContainer lifetimeContainer = new LifetimeContainer();
            Locator locator = CreateLocator(lifetimeContainer);
            builder.Policies.Set<ITypeMappingPolicy>(new TypeMappingPolicy(typeof (Foo), null), typeof (IFoo), null);
            builder.Policies.SetDefault<ISingletonPolicy>(new SingletonPolicy(true));
            GC.Collect();
            object obj1 = builder.BuildUp(locator, typeof (IFoo), null, null);
            object obj2 = builder.BuildUp(locator, typeof (IFoo), null, null);

            Assert.AreSame(obj1, obj2);
            GC.KeepAlive(lifetimeContainer);
        }
        public void BuildUpReturnsNewInstanceOfStateValue()
        {
            Locator locator = new Locator();
            LifetimeContainer container = new LifetimeContainer();
            locator.Add(typeof (ILifetimeContainer), container);
            WCSFBuilder builder = new WCSFBuilder();
            builder.Policies.SetDefault<ISingletonPolicy>(new SingletonPolicy(true));
            MockSessionStateLocatorService stateLocator = new MockSessionStateLocatorService();
            locator.Add(new DependencyResolutionLocatorKey(typeof (ISessionStateLocatorService), null), stateLocator);

            SampleClass builtObject =
                builder.BuildUp(locator, typeof (SampleClass), Guid.NewGuid().ToString(), null) as SampleClass;

            Assert.IsNotNull(builtObject.MyStringValue);
        }
        public void BuildUpThrowsWhenTryingToInjectNonIStateValueParameters()
        {
            Locator locator = new Locator();
            LifetimeContainer container = new LifetimeContainer();
            locator.Add(typeof (ILifetimeContainer), container);
            WCSFBuilder builder = new WCSFBuilder();
            builder.Policies.SetDefault<ISingletonPolicy>(new SingletonPolicy(true));
            locator.Add(new DependencyResolutionLocatorKey(typeof (ISessionStateLocatorService), null),
                        new MockSessionStateLocatorService());

            builder.BuildUp(locator, typeof (InvalidClass), Guid.NewGuid().ToString(), null);
        }
        private TestableServiceCollection CreateServiceCollection()
        {
            LifetimeContainer container = new LifetimeContainer();
            Locator locator = new Locator();
            locator.Add(typeof (ILifetimeContainer), container);

            return new TestableServiceCollection(container, locator, CreateBuilder());
        }
        private MockBuilderContext CreateContext(Locator locator)
        {
            IBuilderStrategy[] strategies = {
                                                new SingletonStrategy(),
                                                new ConstructorReflectionStrategy(),
                                                new CreationStrategy()
                                            };
            MockBuilderContext result = new MockBuilderContext(strategies);
            result.Locator = locator;

            LifetimeContainer lifetimeContainer = new LifetimeContainer();

            if (!result.Locator.Contains(typeof (ILifetimeContainer)))
                result.Locator.Add(typeof (ILifetimeContainer), lifetimeContainer);

            result.Policies.SetDefault<ICreationPolicy>(new DefaultCreationPolicy());
            result.Policies.SetDefault<ISingletonPolicy>(new SingletonPolicy(true));
            return result;
        }
        public void CreationPolicyWillRecordSingletonsUsingLocalLifetimeContainerOnly()
        {
            BuilderStrategyChain chain = new BuilderStrategyChain();
            chain.Add(new CreationStrategy());

            Locator parentLocator = new Locator();
            LifetimeContainer container = new LifetimeContainer();
            parentLocator.Add(typeof (ILifetimeContainer), container);

            Locator childLocator = new Locator(parentLocator);

            PolicyList policies = new PolicyList();
            policies.SetDefault<ICreationPolicy>(new DefaultCreationPolicy());
            policies.SetDefault<ISingletonPolicy>(new SingletonPolicy(true));

            BuilderContext ctx = new BuilderContext(chain, childLocator, policies);

            object obj = ctx.HeadOfChain.BuildUp(ctx, typeof (object), null, null);

            Assert.IsNotNull(obj);
            Assert.IsNull(childLocator.Get(new DependencyResolutionLocatorKey(typeof (object), null)));
        }