Beispiel #1
0
        public void TestCollections()
        {
            IMutablePicoContainer mpc = new DefaultPicoContainer();

            IParameter[] parameters = new IParameter[]
            {
                new ComponentParameter(typeof(Cod), false),
                new ComponentParameter(typeof(Fish), false)
            };

            mpc.RegisterComponentImplementation(typeof(CollectedBowl), typeof(CollectedBowl), parameters);
            mpc.RegisterComponentImplementation(typeof(Cod));
            mpc.RegisterComponentImplementation(typeof(Shark));
            Cod           cod  = (Cod)mpc.GetComponentInstanceOfType(typeof(Cod));
            CollectedBowl bowl = (CollectedBowl)mpc.GetComponentInstance(typeof(CollectedBowl));

            Assert.AreEqual(1, bowl.cods.Length);
            Assert.AreEqual(2, bowl.fishes.Length);
            Assert.AreSame(cod, bowl.cods[0]);

            try
            {
                Assert.AreSame(bowl.fishes[0], bowl.fishes[1]);
                Assert.Fail("The fishes should not be the same");
            }
            catch (AssertionException)
            {
            }
        }
Beispiel #2
0
        public void ThangCanBeInstantiatedWithArrayList()
        {
            IMutablePicoContainer pico = new DefaultPicoContainer();

            pico.RegisterComponentImplementation(typeof(Thingie));
            pico.RegisterComponentImplementation(typeof(ArrayList));
            Assert.IsNotNull(pico.GetComponentInstance(typeof(Thingie)));
        }
        public void ShouldBeAbleToInstantiateNonPublicClassesWithNonPublicConstructors()
        {
            DefaultPicoContainer pico = new DefaultPicoContainer(new ConstructorInjectionComponentAdapterFactory(true));

            pico.RegisterComponentImplementation(typeof(Private));
            pico.RegisterComponentImplementation(typeof(NotYourBusiness));
            Assert.IsNotNull(pico.GetComponentInstance(typeof(NotYourBusiness)));
        }
		private IMutablePicoContainer GetDefaultPicoContainer()
		{
			IMutablePicoContainer mpc = new DefaultPicoContainer();
			mpc.RegisterComponentImplementation(typeof (Bowl));
			mpc.RegisterComponentImplementation(typeof (Cod));
			mpc.RegisterComponentImplementation(typeof (Shark));
			return mpc;
		}
Beispiel #5
0
        public void TestChildGetsFromParent()
        {
            parent.RegisterComponentImplementation(typeof(SimpleTouchable));
            child.RegisterComponentImplementation(typeof(DependsOnTouchable));
            DependsOnTouchable dependsOnTouchable =
                (DependsOnTouchable)child.GetComponentInstance(typeof(DependsOnTouchable));

            Assert.IsNotNull(dependsOnTouchable);
        }
Beispiel #6
0
        private static IMutablePicoContainer GetDefaultPicoContainer()
        {
            IMutablePicoContainer mpc = new DefaultPicoContainer();

            mpc.RegisterComponentImplementation(typeof(Bowl));
            mpc.RegisterComponentImplementation(typeof(Cod));
            mpc.RegisterComponentImplementation(typeof(Shark));
            return(mpc);
        }
Beispiel #7
0
        public void CanInstantiateReplacedComponent()
        {
            IComponentAdapter componentAdapter = CreateComponentAdapter();

            picoContainer.RegisterComponent(componentAdapter);
            object o = picoContainer.ComponentInstances;

            picoContainer.UnregisterComponent(typeof(ITouchable));
            picoContainer.RegisterComponentImplementation(typeof(ITouchable), typeof(AlternativeTouchable));

            Assert.AreEqual(1, picoContainer.ComponentInstances.Count, "Container should container 1 component");
        }
		public void ParentComponentRegisteredAsClassShouldBePreffered()
		{
			DefaultPicoContainer parent = new DefaultPicoContainer();
			DefaultPicoContainer child = new DefaultPicoContainer(parent);

			parent.RegisterComponentImplementation(typeof (ITouchable), typeof (AlternativeTouchable));
			child.RegisterComponentImplementation("key", typeof (SimpleTouchable));
			child.RegisterComponentImplementation(typeof (DependsOnTouchable));

			DependsOnTouchable dot = (DependsOnTouchable) child.GetComponentInstanceOfType(typeof (DependsOnTouchable));
			Assert.AreEqual(typeof (AlternativeTouchable), dot.getTouchable().GetType());
		}
Beispiel #9
0
        public void OnlyStartableComponentsAreInstantiatedOnStart()
        {
            IMutablePicoContainer pico = new DefaultPicoContainer();

            pico.RegisterComponentImplementation("recording", typeof(StringBuilder));
            pico.RegisterComponentImplementation(typeof(A));
            pico.RegisterComponentImplementation(typeof(NotStartable));
            pico.Start();

            pico.Stop();
            pico.Dispose();
            Assert.AreEqual("<AA>!A", pico.GetComponentInstance("recording").ToString());
        }
Beispiel #10
0
        public void ParentComponentRegisteredAsClassShouldBePreffered()
        {
            DefaultPicoContainer parent = new DefaultPicoContainer();
            DefaultPicoContainer child  = new DefaultPicoContainer(parent);

            parent.RegisterComponentImplementation(typeof(ITouchable), typeof(AlternativeTouchable));
            child.RegisterComponentImplementation("key", typeof(SimpleTouchable));
            child.RegisterComponentImplementation(typeof(DependsOnTouchable));

            DependsOnTouchable dot = (DependsOnTouchable)child.GetComponentInstanceOfType(typeof(DependsOnTouchable));

            Assert.AreEqual(typeof(AlternativeTouchable), dot.getTouchable().GetType());
        }
Beispiel #11
0
        public void ComponentsAreStartedBreadthFirstAndStoppedDepthFirst()
        {
            IMutablePicoContainer parent = new DefaultPicoContainer();

            parent.RegisterComponentImplementation("recording", typeof(StringBuilder));
            parent.RegisterComponentImplementation(typeof(A));
            IMutablePicoContainer child = parent.MakeChildContainer();

            child.RegisterComponentImplementation(typeof(B));
            parent.Start();
            parent.Stop();

            Assert.AreEqual("<A<BB>A>", parent.GetComponentInstance("recording").ToString());
        }
		public void ComponensRegisteredWithClassKeyTakePrecedenceOverOthersWhenThereAreMultipleImplementations()
		{
			DefaultPicoContainer pico = new DefaultPicoContainer();
			pico.RegisterComponentImplementation("default", typeof (SimpleTouchable));

			/*
			 * By using a class as key, this should take precedence over the other Touchable (Simple)
			 */
			pico.RegisterComponentImplementation(typeof (ITouchable),
			                                     typeof (DecoratedTouchable),
			                                     new IParameter[] {new ComponentParameter("default")});

			ITouchable touchable = (ITouchable) pico.GetComponentInstanceOfType(typeof (ITouchable));
			Assert.AreEqual(typeof (DecoratedTouchable), touchable.GetType());
		}
        protected virtual IPicoContainer WrapComponentInstances(Type decoratingComponentAdapterClass,
                                                                IPicoContainer picoContainer,
                                                                object[] wrapperDependencies)
        {
            Assert.IsTrue(typeof (DecoratingComponentAdapter).IsAssignableFrom(decoratingComponentAdapterClass));
            IMutablePicoContainer mutablePicoContainer = new DefaultPicoContainer();
            int size = (wrapperDependencies != null ? wrapperDependencies.Length : 0) + 1;
            ICollection allComponentAdapters = picoContainer.ComponentAdapters;

            foreach (object adapter in allComponentAdapters)
            {
                IParameter[] parameters = new IParameter[size];
                parameters[0] = new ConstantParameter(adapter);
                for (int i = 1; i < parameters.Length; i++)
                {
                    parameters[i] = new ConstantParameter(wrapperDependencies[i - 1]);
                }
                IMutablePicoContainer instantiatingPicoContainer =
                    new DefaultPicoContainer(new ConstructorInjectionComponentAdapterFactory());
                instantiatingPicoContainer.RegisterComponentImplementation("decorator", decoratingComponentAdapterClass,
                                                                           parameters);
                mutablePicoContainer.RegisterComponent(
                    (IComponentAdapter) instantiatingPicoContainer.GetComponentInstance("decorator"));
            }
            return mutablePicoContainer;
        }
		public void ConstantParameterRespectsExpectedType()
		{
			IMutablePicoContainer picoContainer = new DefaultPicoContainer();
			IParameter parameter = new ConstantParameter(new SimpleTouchable());
			IComponentAdapter adapter = picoContainer.RegisterComponentImplementation(typeof (ITouchable), typeof (SimpleTouchable));
			Assert.IsFalse(parameter.IsResolvable(picoContainer, adapter, typeof (TestFixtureAttribute)));
		}
		public void testIComponentAdapterResolutionIsFirstLookedForByClassKeyToTheTopOfTheContainerHierarchy()
		{
			DefaultPicoContainer pico = new DefaultPicoContainer();
			pico.RegisterComponentImplementation("default", typeof (SimpleTouchable));

			pico.RegisterComponentImplementation(typeof (ITouchable), typeof (DecoratedTouchable), new IParameter[]
				{
					new ComponentParameter("default")
				});

			DefaultPicoContainer grandChild = new DefaultPicoContainer(new DefaultPicoContainer(new DefaultPicoContainer(pico)));

			ITouchable touchable = (ITouchable) grandChild.GetComponentInstanceOfType(typeof (ITouchable));
			Assert.AreEqual(typeof (DecoratedTouchable), touchable.GetType());

		}
        public void InstantiationExceptionThrownInCtorIsRethrownInsideInvocationTargetExeption()
        {
            DefaultPicoContainer picoContainer = new DefaultPicoContainer();

            picoContainer.RegisterComponentImplementation(typeof(InstantiationExceptionThrowing));
            picoContainer.GetComponentInstance(typeof(InstantiationExceptionThrowing));
        }
 public void ComponentParameterRespectsExpectedType()
 {
     IMutablePicoContainer picoContainer = new DefaultPicoContainer();
     IComponentAdapter adapter =
         picoContainer.RegisterComponentImplementation(typeof (ITouchable), typeof (SimpleTouchable));
     Assert.IsNull(
         ComponentParameter.DEFAULT.ResolveInstance(picoContainer, adapter, typeof (TestFixtureAttribute)));
 }
Beispiel #18
0
        public void ComponensRegisteredWithClassKeyTakePrecedenceOverOthersWhenThereAreMultipleImplementations()
        {
            DefaultPicoContainer pico = new DefaultPicoContainer();

            pico.RegisterComponentImplementation("default", typeof(SimpleTouchable));

            /*
             * By using a class as key, this should take precedence over the other Touchable (Simple)
             */
            pico.RegisterComponentImplementation(typeof(ITouchable),
                                                 typeof(DecoratedTouchable),
                                                 new IParameter[] { new ComponentParameter("default") });

            ITouchable touchable = (ITouchable)pico.GetComponentInstanceOfType(typeof(ITouchable));

            Assert.AreEqual(typeof(DecoratedTouchable), touchable.GetType());
        }
Beispiel #19
0
        public void WillRemoveComponentsWithMatchingKeyFromParent()
        {
            IMutablePicoContainer parent = new DefaultPicoContainer();

            parent.RegisterComponentImplementation("Tom", typeof(Cod));
            parent.RegisterComponentImplementation("Dick", typeof(Cod));
            parent.RegisterComponentImplementation("Harry", typeof(Cod));

            IMutablePicoContainer child = new DefaultPicoContainer(parent);

            child.RegisterComponentImplementation("Dick", typeof(Shark));
            child.RegisterComponentImplementation(typeof(Bowl));
            Bowl bowl = (Bowl)child.GetComponentInstance(typeof(Bowl));

            Assert.AreEqual(3, bowl.fishes.Length);
            Assert.AreEqual(2, bowl.cods.Length);
        }
Beispiel #20
0
        public void ConstantParameterRespectsExpectedType()
        {
            IMutablePicoContainer picoContainer = new DefaultPicoContainer();
            IParameter            parameter     = new ConstantParameter(new SimpleTouchable());
            IComponentAdapter     adapter       =
                picoContainer.RegisterComponentImplementation(typeof(ITouchable), typeof(SimpleTouchable));

            Assert.IsFalse(parameter.IsResolvable(picoContainer, adapter, typeof(TestFixtureAttribute)));
        }
Beispiel #21
0
        public void ComponentParameterRespectsExpectedType()
        {
            IMutablePicoContainer picoContainer = new DefaultPicoContainer();
            IComponentAdapter     adapter       =
                picoContainer.RegisterComponentImplementation(typeof(ITouchable), typeof(SimpleTouchable));

            Assert.IsNull(
                ComponentParameter.DEFAULT.ResolveInstance(picoContainer, adapter, typeof(TestFixtureAttribute)));
        }
		public void ComponentParameterExcludesSelf()
		{
			DefaultPicoContainer pico = new DefaultPicoContainer();
			IComponentAdapter adapter = pico.RegisterComponentImplementation(typeof (ITouchable), typeof (SimpleTouchable));

			Assert.IsNotNull(pico.GetComponentInstance(typeof (ITouchable)));
			ITouchable touchable = (ITouchable) ComponentParameter.DEFAULT.ResolveInstance(pico, adapter, typeof (ITouchable));
			Assert.IsNull(touchable);
		}
Beispiel #23
0
        public void testIComponentAdapterResolutionIsFirstLookedForByClassKeyToTheTopOfTheContainerHierarchy()
        {
            DefaultPicoContainer pico = new DefaultPicoContainer();

            pico.RegisterComponentImplementation("default", typeof(SimpleTouchable));

            pico.RegisterComponentImplementation(typeof(ITouchable), typeof(DecoratedTouchable), new IParameter[]
            {
                new ComponentParameter
                    ("default")
            });

            DefaultPicoContainer grandChild =
                new DefaultPicoContainer(new DefaultPicoContainer(new DefaultPicoContainer(pico)));

            ITouchable touchable = (ITouchable)grandChild.GetComponentInstanceOfType(typeof(ITouchable));

            Assert.AreEqual(typeof(DecoratedTouchable), touchable.GetType());
        }
Beispiel #24
0
        public void StartStopStartStopAndDispose()
        {
            DefaultPicoContainer pico = new DefaultPicoContainer();

            pico.RegisterComponentImplementation(typeof(One));
            pico.RegisterComponentImplementation(typeof(Two));
            pico.RegisterComponentImplementation(typeof(Three));
            pico.RegisterComponentImplementation(typeof(Four));
            One    one = (One)pico.GetComponentInstance(typeof(One));
            object o   = pico.ComponentInstances;

            // instantiation - would be difficult to do these in the wrong order!!
            Assert.AreEqual(4, one.getInstantiating().Count);
            Assert.AreEqual("One", one.getInstantiating()[0]);
            Assert.AreEqual("Two", one.getInstantiating()[1]);
            Assert.AreEqual("Three", one.getInstantiating()[2]);
            Assert.AreEqual("Four", one.getInstantiating()[3]);
            StartStopDisposeLifecycleComps(pico, pico, pico, one);
        }
		public void ResolveFromParentByType()
		{
			IMutablePicoContainer parent = new DefaultPicoContainer();
			parent.RegisterComponentImplementation(typeof (ITouchable), typeof (SimpleTouchable));

			IMutablePicoContainer child = new DefaultPicoContainer(parent);
			child.RegisterComponentImplementation(typeof (DependsOnTouchable));

			Assert.IsNotNull(child.GetComponentInstance(typeof (DependsOnTouchable)));
		}
        public void ComponentParameterFetches()
        {
            DefaultPicoContainer pico = new DefaultPicoContainer();
            pico.RegisterComponentImplementation(typeof (ITouchable), typeof (SimpleTouchable));
            ComponentParameter parameter = new ComponentParameter(typeof (ITouchable));

            Assert.IsNotNull(pico.GetComponentInstance(typeof (ITouchable)));
            ITouchable touchable = (ITouchable) parameter.ResolveInstance(pico, null, typeof (ITouchable));
            Assert.IsNotNull(touchable);
        }
		public void DefaultPicoContainerReturnsNewInstanceForEachCallWhenUsingTransientIComponentAdapter()
		{
			DefaultPicoContainer picoContainer = new DefaultPicoContainer();
			picoContainer.RegisterComponentImplementation(typeof (Service));
			picoContainer.RegisterComponent(new ConstructorInjectionComponentAdapter(typeof (TransientComponent), typeof (TransientComponent)));
			TransientComponent c1 = (TransientComponent) picoContainer.GetComponentInstance(typeof (TransientComponent));
			TransientComponent c2 = (TransientComponent) picoContainer.GetComponentInstance(typeof (TransientComponent));
			Assert.IsFalse(c1.Equals(c2));
			Assert.AreSame(c1.service, c2.service);
		}
Beispiel #28
0
        public void OrderOfInstantiationWithoutAdapter()
        {
            DefaultPicoContainer pico = new DefaultPicoContainer();

            pico.RegisterComponentImplementation(typeof(Four));
            pico.RegisterComponentImplementation(typeof(Two));
            pico.RegisterComponentImplementation(typeof(One));
            pico.RegisterComponentImplementation(typeof(Three));

            IList componentInstances = pico.ComponentInstances;

            Assert.AreEqual(4, componentInstances.Count);

            // instantiation - would be difficult to do these in the wrong order!!
            Assert.AreEqual(typeof(One), componentInstances[0].GetType());
            Assert.AreEqual(typeof(Two), componentInstances[1].GetType());
            Assert.AreEqual(typeof(Three), componentInstances[2].GetType());
            Assert.AreEqual(typeof(Four), componentInstances[3].GetType());
        }
        public void ShouldNotConsiderNonPublicConstructors()
        {
            DefaultPicoContainer pico = new DefaultPicoContainer();

            pico.RegisterComponentImplementation(typeof(Component201));
            pico.RegisterComponentInstance(2);
            pico.RegisterComponentInstance(true);
            pico.RegisterComponentInstance("Hello");
            Assert.IsNotNull(pico.GetComponentInstance(typeof(Component201)));
        }
		public void TestDependsOnTouchableWithTouchableSpecifiedAsConstant()
		{
			DefaultPicoContainer pico = new DefaultPicoContainer();
			SimpleTouchable touchable = new SimpleTouchable();
			pico.RegisterComponentImplementation(typeof (DependsOnTouchable), typeof (DependsOnTouchable), new IParameter[]
				{
					new ConstantParameter(touchable)
				});
			object o = pico.ComponentInstances;
			Assert.IsTrue(touchable.WasTouched);
		}
Beispiel #31
0
        public void DefaultPicoContainerReturnsNewInstanceForEachCallWhenUsingTransientIComponentAdapter()
        {
            DefaultPicoContainer picoContainer = new DefaultPicoContainer();

            picoContainer.RegisterComponentImplementation(typeof(Service));
            picoContainer.RegisterComponent(new ConstructorInjectionComponentAdapter(typeof(TransientComponent)));
            TransientComponent c1 = (TransientComponent)picoContainer.GetComponentInstance(typeof(TransientComponent));
            TransientComponent c2 = (TransientComponent)picoContainer.GetComponentInstance(typeof(TransientComponent));

            Assert.IsFalse(c1.Equals(c2));
            Assert.AreSame(c1.service, c2.service);
        }
Beispiel #32
0
        public void ComponentParameterExcludesSelf()
        {
            DefaultPicoContainer pico    = new DefaultPicoContainer();
            IComponentAdapter    adapter =
                pico.RegisterComponentImplementation(typeof(ITouchable), typeof(SimpleTouchable));

            Assert.IsNotNull(pico.GetComponentInstance(typeof(ITouchable)));
            ITouchable touchable =
                (ITouchable)ComponentParameter.DEFAULT.ResolveInstance(pico, adapter, typeof(ITouchable));

            Assert.IsNull(touchable);
        }
Beispiel #33
0
        public void ComponentParameterFetches()
        {
            DefaultPicoContainer pico = new DefaultPicoContainer();

            pico.RegisterComponentImplementation(typeof(ITouchable), typeof(SimpleTouchable));
            ComponentParameter parameter = new ComponentParameter(typeof(ITouchable));

            Assert.IsNotNull(pico.GetComponentInstance(typeof(ITouchable)));
            ITouchable touchable = (ITouchable)parameter.ResolveInstance(pico, null, typeof(ITouchable));

            Assert.IsNotNull(touchable);
        }
Beispiel #34
0
        public void ResolveFromParentByType()
        {
            IMutablePicoContainer parent = new DefaultPicoContainer();

            parent.RegisterComponentImplementation(typeof(ITouchable), typeof(SimpleTouchable));

            IMutablePicoContainer child = new DefaultPicoContainer(parent);

            child.RegisterComponentImplementation(typeof(DependsOnTouchable));

            Assert.IsNotNull(child.GetComponentInstance(typeof(DependsOnTouchable)));
        }
		public void ResolveFromGrandParentByKey()
		{
			IMutablePicoContainer grandParent = new DefaultPicoContainer();
			grandParent.RegisterComponentImplementation(typeof (ITouchable), typeof (SimpleTouchable));

			IMutablePicoContainer parent = new DefaultPicoContainer(grandParent);

			IMutablePicoContainer child = new DefaultPicoContainer(parent);
			child.RegisterComponentImplementation(typeof (DependsOnTouchable), typeof (DependsOnTouchable),
			                                      new IParameter[] {new ComponentParameter(typeof (ITouchable))});

			Assert.IsNotNull(child.GetComponentInstance(typeof (DependsOnTouchable)));
		}
Beispiel #36
0
        public void TestDictionaries()
        {
            IMutablePicoContainer mpc = new DefaultPicoContainer();

            IParameter[] parameters = new IParameter[] { new ComponentParameter(typeof(Fish), false) };

            mpc.RegisterComponentImplementation(typeof(DictionaryBowl), typeof(DictionaryBowl), parameters);
            mpc.RegisterComponentImplementation(typeof(Cod));
            mpc.RegisterComponentImplementation(typeof(Shark));
            DictionaryBowl bowl = (DictionaryBowl)mpc.GetComponentInstance(typeof(DictionaryBowl));

            Assert.AreEqual(2, bowl.Fishes.Length);

            try
            {
                Assert.AreSame(bowl.Fishes[0], bowl.Fishes[1]);
                Assert.Fail("Should not be the same fish");
            }
            catch (AssertionException)
            {
            }
        }
Beispiel #37
0
        public void TestDependsOnTouchableWithTouchableSpecifiedAsConstant()
        {
            DefaultPicoContainer pico      = new DefaultPicoContainer();
            SimpleTouchable      touchable = new SimpleTouchable();

            pico.RegisterComponentImplementation(typeof(DependsOnTouchable), typeof(DependsOnTouchable),
                                                 new IParameter[]
            {
                new ConstantParameter(touchable)
            });
            object o = pico.ComponentInstances;

            Assert.IsTrue(touchable.WasTouched);
        }
Beispiel #38
0
        public void BowlWithoutTom()
        {
            IMutablePicoContainer mpc = new DefaultPicoContainer();

            mpc.RegisterComponentImplementation("Tom", typeof(Cod));
            mpc.RegisterComponentImplementation("Dick", typeof(Cod));
            mpc.RegisterComponentImplementation("Harry", typeof(Cod));
            mpc.RegisterComponentImplementation(typeof(Shark));

            IParameter[] parameters = new IParameter[]
            {
                new SampleCollectionComponentParameter(typeof(Cod), false),
                new CollectionComponentParameter(typeof(Fish), false)
            };

            mpc.RegisterComponentImplementation(typeof(CollectedBowl), typeof(CollectedBowl), parameters);

            CollectedBowl bowl = (CollectedBowl)mpc.GetComponentInstance(typeof(CollectedBowl));
            Cod           tom  = (Cod)mpc.GetComponentInstance("Tom");

            Assert.AreEqual(4, bowl.fishes.Length);
            Assert.AreEqual(2, bowl.cods.Length);
            Assert.IsFalse(new ArrayList(bowl.cods).Contains(tom));
        }
Beispiel #39
0
        public void ResolveFromGrandParentByKey()
        {
            IMutablePicoContainer grandParent = new DefaultPicoContainer();

            grandParent.RegisterComponentImplementation(typeof(ITouchable), typeof(SimpleTouchable));

            IMutablePicoContainer parent = new DefaultPicoContainer(grandParent);

            IMutablePicoContainer child = new DefaultPicoContainer(parent);

            child.RegisterComponentImplementation(typeof(DependsOnTouchable), typeof(DependsOnTouchable),
                                                  new IParameter[] { new ComponentParameter(typeof(ITouchable)) });

            Assert.IsNotNull(child.GetComponentInstance(typeof(DependsOnTouchable)));
        }
        public void NormalExceptionThrownInCtorIsRethrownInsideInvocationTargetExeption()
        {
            DefaultPicoContainer picoContainer = new DefaultPicoContainer();

            picoContainer.RegisterComponentImplementation(typeof(NormalExceptionThrowing));
            try
            {
                picoContainer.GetComponentInstance(typeof(NormalExceptionThrowing));
                Assert.Fail();
            }
            catch (PicoInvocationTargetInitializationException e)
            {
                Assert.AreEqual("test", e.GetBaseException().Message);
            }
        }
        public void RegisterAbstractShouldFail()
        {
            IMutablePicoContainer pico = new DefaultPicoContainer();

            try
            {
                pico.RegisterComponentImplementation(typeof(IList));
                Assert.Fail("Shouldn't be allowed to register abstract classes or interfaces.");
            }
            catch (NotConcreteRegistrationException e)
            {
                Assert.AreEqual(typeof(IList), e.ComponentImplementation);
                Assert.IsTrue(e.Message.IndexOf(typeof(IList).Name) > 0);
            }
        }
        public void PicoInitializationExceptionThrownBecauseOfFilteredConstructors()
        {
            DefaultPicoContainer picoContainer = new DefaultPicoContainer();

            try
            {
                picoContainer.RegisterComponentImplementation(typeof(IllegalAccessExceptionThrowing));
                picoContainer.GetComponentInstance(typeof(IllegalAccessExceptionThrowing));
                Assert.Fail();
            }
            catch (PicoInitializationException e)
            {
                Assert.IsTrue(e.Message.IndexOf(typeof(IllegalAccessExceptionThrowing).Name) > 0);
            }
        }
Beispiel #43
0
        public void TestCanInstantiateReplacedComponent()
        {
            picoContainer.RegisterComponentImplementation(typeof(ITouchable), typeof(SimpleTouchable));
            object o = picoContainer.ComponentInstances;

            picoContainer.UnregisterComponent(typeof(ITouchable));

            picoContainer.RegisterComponentImplementation(typeof(ITouchable), typeof(AlternativeTouchable));

            Assert.AreEqual(1, picoContainer.ComponentInstances.Count);
        }
		public void RegisterAbstractShouldFail()
		{
			IMutablePicoContainer pico = new DefaultPicoContainer();

			try
			{
				pico.RegisterComponentImplementation(typeof (IList));
				Assert.Fail("Shouldn't be allowed to register abstract classes or interfaces.");
			}
			catch (NotConcreteRegistrationException e)
			{
				Assert.AreEqual(typeof (IList), e.ComponentImplementation);
				Assert.IsTrue(e.Message.IndexOf(typeof (IList).Name) > 0);
			}
		}
		public void TestDictionaries()
		{
			IMutablePicoContainer mpc = new DefaultPicoContainer();
			IParameter[] parameters = new IParameter[] {new ComponentParameter(typeof (Fish), false)};

			mpc.RegisterComponentImplementation(typeof (DictionaryBowl), typeof (DictionaryBowl), parameters);
			mpc.RegisterComponentImplementation(typeof (Cod));
			mpc.RegisterComponentImplementation(typeof (Shark));
			DictionaryBowl bowl = (DictionaryBowl) mpc.GetComponentInstance(typeof (DictionaryBowl));
			Assert.AreEqual(2, bowl.Fishes.Length);

			try
			{
				Assert.AreSame(bowl.Fishes[0], bowl.Fishes[1]);
				Assert.Fail("Should not be the same fish");
			} 
			catch(AssertionException) {}
		}
		public void WillRemoveComponentsWithMatchingKeyFromParent() 
		{
			IMutablePicoContainer parent = new DefaultPicoContainer();
			parent.RegisterComponentImplementation("Tom", typeof(Cod));
			parent.RegisterComponentImplementation("Dick", typeof(Cod));
			parent.RegisterComponentImplementation("Harry", typeof(Cod));
			
			IMutablePicoContainer child = new DefaultPicoContainer(parent);
			child.RegisterComponentImplementation("Dick", typeof(Shark));
			child.RegisterComponentImplementation(typeof(Bowl));
			Bowl bowl = (Bowl) child.GetComponentInstance(typeof(Bowl));
			Assert.AreEqual(3, bowl.fishes.Length);
			Assert.AreEqual(2, bowl.cods.Length);
		}
		public void BowlWithoutTom()
		{
			IMutablePicoContainer mpc = new DefaultPicoContainer();
			mpc.RegisterComponentImplementation("Tom", typeof (Cod));
			mpc.RegisterComponentImplementation("Dick", typeof (Cod));
			mpc.RegisterComponentImplementation("Harry", typeof (Cod));
			mpc.RegisterComponentImplementation(typeof (Shark));

			IParameter[] parameters = new IParameter[]
				{
					new SampleCollectionComponentParameter(typeof (Cod), false),
					new CollectionComponentParameter(typeof (Fish), false)
				};

			mpc.RegisterComponentImplementation(typeof (CollectedBowl), typeof (CollectedBowl), parameters);

			CollectedBowl bowl = (CollectedBowl) mpc.GetComponentInstance(typeof (CollectedBowl));
			Cod tom = (Cod) mpc.GetComponentInstance("Tom");
			Assert.AreEqual(4, bowl.fishes.Length);
			Assert.AreEqual(2, bowl.cods.Length);
			Assert.IsFalse(new ArrayList(bowl.cods).Contains(tom));
		}
		public void Verify()
		{
			IMutablePicoContainer pico = new DefaultPicoContainer();
			CollectionComponentParameter parameterNonEmpty = CollectionComponentParameter.ARRAY;
			pico.RegisterComponentImplementation(typeof (Shark));
			parameterNonEmpty.Verify(pico, null, typeof (Fish[]));

			try
			{
				parameterNonEmpty.Verify(pico, null, typeof (Cod[]));
				Assert.Fail("PicoIntrospectionException expected");
			}
			catch (PicoIntrospectionException e)
			{
				Assert.IsTrue(e.Message.IndexOf(typeof (Cod).Name) > -1);
			}

			CollectionComponentParameter parameterEmpty = CollectionComponentParameter.ARRAY_ALLOW_EMPTY;
			parameterEmpty.Verify(pico, null, typeof (Fish[]));
			parameterEmpty.Verify(pico, null, typeof (Cod[]));
		}
		public void CollectionsAreGeneratedOnTheFly()
		{
			IMutablePicoContainer mpc = new DefaultPicoContainer();
			mpc.RegisterComponent(new ConstructorInjectionComponentAdapter(typeof (Bowl), typeof (Bowl)));
			mpc.RegisterComponentImplementation(typeof (Cod));
			Bowl bowl = (Bowl) mpc.GetComponentInstance(typeof (Bowl));
			Assert.AreEqual(1, bowl.cods.Length);
			mpc.RegisterComponentInstance("Nemo", new Cod());
			bowl = (Bowl) mpc.GetComponentInstance(typeof (Bowl));
			Assert.AreEqual(2, bowl.cods.Length);

			try
			{
				Assert.AreSame(bowl.cods[0], bowl.cods[1]);
				Assert.Fail("cods should not be the same");
			}
			catch (AssertionException)
			{
			}
		}
        public void OrderOfInstantiationWithoutAdapter()
        {
            DefaultPicoContainer pico = new DefaultPicoContainer();

            pico.RegisterComponentImplementation(typeof (Four));
            pico.RegisterComponentImplementation(typeof (Two));
            pico.RegisterComponentImplementation(typeof (One));
            pico.RegisterComponentImplementation(typeof (Three));

            IList componentInstances = pico.ComponentInstances;
            Assert.AreEqual(4, componentInstances.Count);

            // instantiation - would be difficult to do these in the wrong order!!
            Assert.AreEqual(typeof (One), componentInstances[0].GetType());
            Assert.AreEqual(typeof (Two), componentInstances[1].GetType());
            Assert.AreEqual(typeof (Three), componentInstances[2].GetType());
            Assert.AreEqual(typeof (Four), componentInstances[3].GetType());
        }
		public void ShouldBeAbleToInstantiateNonPublicClassesWithNonPublicConstructors()
		{
			DefaultPicoContainer pico = new DefaultPicoContainer(new ConstructorInjectionComponentAdapterFactory(true));
			pico.RegisterComponentImplementation(typeof (Private));
			pico.RegisterComponentImplementation(typeof (NotYourBusiness));
			Assert.IsNotNull(pico.GetComponentInstance(typeof (NotYourBusiness)));
		}
		public void HybridBeans()
		{
			SetterInjectionComponentAdapter bAdapter = new SetterInjectionComponentAdapter("b", typeof (B), null);
			SetterInjectionComponentAdapter cAdapter = new SetterInjectionComponentAdapter("c", typeof (C), null);
			SetterInjectionComponentAdapter cNullAdapter = new SetterInjectionComponentAdapter("c0", typeof (C), null);

			IMutablePicoContainer pico = new DefaultPicoContainer();
			pico.RegisterComponent(bAdapter);
			pico.RegisterComponent(cAdapter);
			pico.RegisterComponent(cNullAdapter);
			pico.RegisterComponentImplementation(typeof (ArrayList));

			C c = (C) cAdapter.GetComponentInstance(pico);
			Assert.IsTrue(c.instantiatedAsBean());
			C c0 = (C) cNullAdapter.GetComponentInstance(pico);
			Assert.IsTrue(c0.instantiatedAsBean());
		}
		public void ShouldNotConsiderNonPublicConstructors()
		{
			DefaultPicoContainer pico = new DefaultPicoContainer();
			pico.RegisterComponentImplementation(typeof (Component201));
			pico.RegisterComponentInstance(2);
			pico.RegisterComponentInstance(true);
			pico.RegisterComponentInstance("Hello");
			Assert.IsNotNull(pico.GetComponentInstance(typeof (Component201)));
		}
        public void ComponentsAreStartedBreadthFirstAndStoppedDepthFirst()
        {
            IMutablePicoContainer parent = new DefaultPicoContainer();
            parent.RegisterComponentImplementation("recording", typeof (StringBuilder));
            parent.RegisterComponentImplementation(typeof (A));
            IMutablePicoContainer child = parent.MakeChildContainer();
            child.RegisterComponentImplementation(typeof (B));
            parent.Start();
            parent.Stop();

            Assert.AreEqual("<A<BB>A>", parent.GetComponentInstance("recording").ToString());
        }
 public void StartStopStartStopAndDispose()
 {
     DefaultPicoContainer pico = new DefaultPicoContainer();
     pico.RegisterComponentImplementation(typeof (One));
     pico.RegisterComponentImplementation(typeof (Two));
     pico.RegisterComponentImplementation(typeof (Three));
     pico.RegisterComponentImplementation(typeof (Four));
     One one = (One) pico.GetComponentInstance(typeof (One));
     object o = pico.ComponentInstances;
     // instantiation - would be difficult to do these in the wrong order!!
     Assert.AreEqual(4, one.getInstantiating().Count);
     Assert.AreEqual("One", one.getInstantiating()[0]);
     Assert.AreEqual("Two", one.getInstantiating()[1]);
     Assert.AreEqual("Three", one.getInstantiating()[2]);
     Assert.AreEqual("Four", one.getInstantiating()[3]);
     StartStopDisposeLifecycleComps(pico, pico, pico, one);
 }
        public void StartStopOfDaemonizedThread()
        {
            DefaultPicoContainer pico = new DefaultPicoContainer();
            pico.RegisterComponentImplementation(typeof (FooRunnable));

            object i = pico.ComponentInstances;
            pico.Start();
            Thread.Sleep(100);
            pico.Stop();

            FooRunnable foo = (FooRunnable) pico.GetComponentInstance(typeof (FooRunnable));
            Assert.AreEqual(1, foo.runCount());
            pico.Start();
            Thread.Sleep(100);
            pico.Stop();
            Assert.AreEqual(2, foo.runCount());
        }
		public void PicoInitializationExceptionThrownBecauseOfFilteredConstructors()
		{
			DefaultPicoContainer picoContainer = new DefaultPicoContainer();
			try
			{
				picoContainer.RegisterComponentImplementation(typeof (IllegalAccessExceptionThrowing));
				picoContainer.GetComponentInstance(typeof (IllegalAccessExceptionThrowing));
				Assert.Fail();
			}
			catch (PicoInitializationException e)
			{
				Assert.IsTrue(e.Message.IndexOf(typeof (IllegalAccessExceptionThrowing).Name) > 0);
			}
		}
		public void InstantiationExceptionThrownInCtorIsRethrownInsideInvocationTargetExeption()
		{
			DefaultPicoContainer picoContainer = new DefaultPicoContainer();
			picoContainer.RegisterComponentImplementation(typeof (InstantiationExceptionThrowing));
			picoContainer.GetComponentInstance(typeof (InstantiationExceptionThrowing));
		}
		public void NormalExceptionThrownInCtorIsRethrownInsideInvocationTargetExeption()
		{
			DefaultPicoContainer picoContainer = new DefaultPicoContainer();
			picoContainer.RegisterComponentImplementation(typeof (NormalExceptionThrowing));
			try
			{
				picoContainer.GetComponentInstance(typeof (NormalExceptionThrowing));
				Assert.Fail();
			}
			catch (PicoInvocationTargetInitializationException e)
			{
				Assert.AreEqual("test", e.GetBaseException().Message);
			}
		}
        public void OnlyStartableComponentsAreInstantiatedOnStart()
        {
            IMutablePicoContainer pico = new DefaultPicoContainer();
            pico.RegisterComponentImplementation("recording", typeof (StringBuilder));
            pico.RegisterComponentImplementation(typeof (A));
            pico.RegisterComponentImplementation(typeof (NotStartable));
            pico.Start();

            pico.Stop();
            pico.Dispose();
            Assert.AreEqual("<AA>!A", pico.GetComponentInstance("recording").ToString());
        }