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)));
		}
示例#2
0
		public IMutablePicoContainer Compose()
		{
			DefaultPicoContainer p = new DefaultPicoContainer(parent);

			p.RegisterComponentInstance("hello", "C#");
			return p;
		}
示例#3
0
        public void BasicInstantiationAndContainment()
        {
            DefaultPicoContainer pico = (DefaultPicoContainer)CreatePicoContainerWithTouchableAndDependsOnTouchable();

            Assert.IsTrue(typeof(ITouchable).IsAssignableFrom(
                              pico.GetComponentAdapterOfType(typeof(ITouchable)).ComponentImplementation));
        }
 public void ConstantParameter()
 {
     Object value = new Object();
     ConstantParameter parameter = new ConstantParameter(value);
     IMutablePicoContainer picoContainer = new DefaultPicoContainer();
     Assert.AreSame(value, parameter.ResolveInstance(picoContainer, null, typeof (object)));
 }
		public void DEF_verifyWithoutDependencyWorks()
		{
			IMutablePicoContainer picoContainer = new DefaultPicoContainer(CreateDefaultComponentAdapterFactory());
			IComponentAdapter componentAdapter = prepDEF_verifyWithoutDependencyWorks(picoContainer);
			Assert.AreSame(GetComponentAdapterType(), componentAdapter.GetType());
			componentAdapter.Verify(picoContainer);
		}
示例#6
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)
            {
            }
        }
        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;
        }
示例#8
0
        public IMutablePicoContainer MakeChildContainer()
        {
            DefaultPicoContainer pc = new DefaultPicoContainer(componentAdapterFactory, this, lifecycleManager);

            AddChildContainer(pc);
            return(pc);
        }
		public void NanoContainerCanBeBuiltFromCodeOnTheFly()
		{
			string code = @"
				using PicoContainer;
				using PicoContainer.Defaults;
				namespace Test 
				{
					public class NameTranslator 
					{
						private IPicoContainer parent;
						public IPicoContainer Parent {
							set { parent = value; } 
						}
				
						public IMutablePicoContainer Compose() {
							DefaultPicoContainer p = new DefaultPicoContainer(parent);
							p.RegisterComponentInstance(10, 1000);
							return p; 
						}
					}
				}";

			IMutablePicoContainer parent = new DefaultPicoContainer();
			ContainerBuilderFacade cbf = new CSharpContainerBuilderFacade(ScriptFixture.BuildStreamReader(code));
			IPicoContainer pico = cbf.Build(parent, new ArrayList());

			Assert.IsNotNull(pico);
			Assert.AreSame(parent, pico.Parent);
			Assert.AreEqual(1000, pico.GetComponentInstance(10));
		}
        public void InstantiationExceptionThrownInCtorIsRethrownInsideInvocationTargetExeption()
        {
            DefaultPicoContainer picoContainer = new DefaultPicoContainer();

            picoContainer.RegisterComponentImplementation(typeof(InstantiationExceptionThrowing));
            picoContainer.GetComponentInstance(typeof(InstantiationExceptionThrowing));
        }
		public void NanoContainerCanBeBuiltFromCodeOnTheFly()
		{
			// Boo relies on indentation so start at column 1. MUST NOT BE REFORMATTED
			string code = @"
import PicoContainer from PicoContainer
import PicoContainer.Defaults from PicoContainer

class BooInjector:
	
	[Property(Parent)]
	_parent as IPicoContainer
	
	def Compose() as IMutablePicoContainer:
		p = DefaultPicoContainer(_parent)
		p.RegisterComponentInstance(10, 1000)
		return p

			";

			IMutablePicoContainer parent = new DefaultPicoContainer();
			ContainerBuilderFacade cbf = new BooContainerBuilderFacade(ScriptFixture.BuildStreamReader(code));
			IPicoContainer pico = cbf.Build(parent, new ArrayList());

			Assert.IsNotNull(pico);
			Assert.AreSame(parent, pico.Parent);
			Assert.AreEqual(1000, pico.GetComponentInstance(10));
		}
示例#12
0
        public void GetComponentInstancesOnParentContainerHostedChildContainerDoesntReturnParentAdapter()
        {
            IMutablePicoContainer parent = new DefaultPicoContainer();
            DefaultPicoContainer  child  = (DefaultPicoContainer)parent.MakeChildContainer();

            Assert.AreEqual(0, child.ComponentInstances.Count);
        }
示例#13
0
        public void DefaultPicoContainerRegisteredAsComponentGetsHostingContainerAsParent()
        {
            IMutablePicoContainer parent = new DefaultPicoContainer();
            DefaultPicoContainer  child  = (DefaultPicoContainer)parent.MakeChildContainer();

            Assert.AreSame(parent, child.Parent);
        }
		public void ContainerCanBeBuiltWithParent()
		{
			IMutablePicoContainer parent = new DefaultPicoContainer();
			IPicoContainer pico = BuildContainer(new VBBuilder(GetStreamReader(@"NanoContainer.Tests.TestScripts.test.vb")), parent, new ArrayList());
			Assert.IsNotNull(pico);
			Assert.AreSame(parent, pico.Parent);
			Assert.AreEqual("VB", pico.GetComponentInstance("hello"));
		}
示例#15
0
        public void ConstantParameter()
        {
            Object                value         = new Object();
            ConstantParameter     parameter     = new ConstantParameter(value);
            IMutablePicoContainer picoContainer = new DefaultPicoContainer();

            Assert.AreSame(value, parameter.ResolveInstance(picoContainer, null, typeof(object)));
        }
        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;
		}
示例#18
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)));
 }
示例#19
0
        public void ThangCanBeInstantiatedWithArrayList()
        {
            IMutablePicoContainer pico = new DefaultPicoContainer();

            pico.RegisterComponentImplementation(typeof(Thingie));
            pico.RegisterComponentImplementation(typeof(ArrayList));
            Assert.IsNotNull(pico.GetComponentInstance(typeof(Thingie)));
        }
		public void ContainerCanBeBuiltWithParent()
		{
			IMutablePicoContainer parent = new DefaultPicoContainer();
			ContainerBuilderFacade cbf = new JSharpContainerBuilderFacade(GetStreamReader(@"NanoContainer.Tests.TestScripts.test.java"));
			IPicoContainer pico = cbf.Build(parent, new ArrayList());
			Assert.IsNotNull(pico);
			Assert.AreSame(parent, pico.Parent);
			Assert.AreEqual("J#", pico.GetComponentInstance("hello"));
		}
		public void DEF_verifyDoesNotInstantiate()
		{
			IMutablePicoContainer picoContainer = new DefaultPicoContainer(CreateDefaultComponentAdapterFactory());
			IComponentAdapter componentAdapter = prepDEF_verifyDoesNotInstantiate(picoContainer);
			Assert.AreSame(GetComponentAdapterType(), componentAdapter.GetType());
			IComponentAdapter notInstantiatablecomponentAdapter = new NotInstantiatableComponentAdapter(componentAdapter);
			IPicoContainer wrappedPicoContainer = WrapComponentInstances(typeof (NotInstantiatableComponentAdapter), picoContainer, null);
			notInstantiatablecomponentAdapter.Verify(wrappedPicoContainer);
		}
示例#22
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)));
        }
示例#23
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);
		}
示例#24
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)));
        }
示例#25
0
        private static IMutablePicoContainer GetDefaultPicoContainer()
        {
            IMutablePicoContainer mpc = new DefaultPicoContainer();

            mpc.RegisterComponentImplementation(typeof(Bowl));
            mpc.RegisterComponentImplementation(typeof(Cod));
            mpc.RegisterComponentImplementation(typeof(Shark));
            return(mpc);
        }
        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)));
        }
示例#27
0
		public DefaultNanoContainer(StreamReader composition, String builderClass)
		{
			DefaultReflectionContainerAdapter defaultReflectionContainerAdapter;
			DefaultPicoContainer dpc = new DefaultPicoContainer();
			dpc.RegisterComponentInstance(composition);

			defaultReflectionContainerAdapter = new DefaultReflectionContainerAdapter(dpc);
			IComponentAdapter componentAdapter = defaultReflectionContainerAdapter.RegisterComponentImplementation(builderClass);
			containerBuilder = (ScriptedContainerBuilder) componentAdapter.GetComponentInstance(dpc);
		}
示例#28
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);
        }
		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 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);
		}
示例#31
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);
		}
示例#32
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 UsageOfADifferentComponentAdapterFactory() 
		{
			// Jira bug 212 - logical opposite
			IMutablePicoContainer parent = new DefaultPicoContainer();
			CachingPicoContainer pico = new CachingPicoContainer(new ConstructorInjectionComponentAdapterFactory(), parent);
			pico.RegisterComponentImplementation(typeof(IList), typeof(ArrayList));
			IList list1 = (IList) pico.GetComponentInstanceOfType(typeof(IList));
			IList list2 = (IList) pico.GetComponentInstanceOfType(typeof(IList));
			Assert.IsNotNull(list1);
			Assert.IsNotNull(list2);
			Assert.IsTrue(list1 == list2);
		}
示例#34
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);
        }
示例#35
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);
        }
示例#36
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);
        }
		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());
		}
示例#38
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());
        }
		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)));
		}
示例#40
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());
        }
示例#41
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);
        }
示例#42
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());
        }
示例#43
0
        public void StartStartCausingBarf()
        {
            DefaultPicoContainer pico = new DefaultPicoContainer();

            pico.Start();
            try
            {
                pico.Start();
                Assert.Fail("Should have barfed");
            }
            catch (Exception)
            {
                // expected;
            }
        }
		public void BuildContainerFromMoreThanOneAssembly()
		{
			StringCollection assemblies = new StringCollection();
			assemblies.Add("../../../TestCompWithAttributes/bin/Debug/TestCompWithAttributes.dll");
			assemblies.Add("../../../NotStartable/bin/Debug/NotStartable.dll");
			
			IMutablePicoContainer parent = new DefaultPicoContainer();
			parent.RegisterComponentInstance(new StringBuilder("This is needed for type NotStartable"));

			ContainerBuilderFacade cbf = new AttributeBasedContainerBuilderFacade();
			IMutablePicoContainer picoContainer = cbf.Build(parent, assemblies);

			Assert.IsNotNull(picoContainer.GetComponentInstance("testcomp3-key"));
			Assert.IsNotNull(picoContainer.GetComponentInstance("notstartable"));
		}
		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());
		}
示例#46
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 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 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 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 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 SimpleContent()
		{
			string xmlScript = @"
				<container>
					<component-instance key='Hello'>XML</component-instance>
					<component-instance key='Hei'>XMLContinerBuilder</component-instance>
				</container>";

			StreamReader scriptStream = new StreamReader(new MemoryStream(new ASCIIEncoding().GetBytes(xmlScript)));

			IMutablePicoContainer parent = new DefaultPicoContainer();
			IPicoContainer pico = BuildContainer(new XMLContainerBuilder(scriptStream), parent, new ArrayList());

			Assert.AreEqual("XML", pico.GetComponentInstance("Hello"));
			Assert.AreEqual("XMLContinerBuilder", pico.GetComponentInstance("Hei"));
		}
示例#52
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);
        }
		public void CreateSimpleContainer()
		{
			string xmlScript = @"
				<container>
					<component-implementation class='System.Text.StringBuilder'/>
					<component-implementation class='NanoContainer.Test.TestModel.DefaultWebServerConfig'/>
					<component-implementation key='NanoContainer.Test.TestModel.WebServer' class='NanoContainer.Test.TestModel.DefaultWebServer'/>
				</container>";

			StreamReader scriptStream = new StreamReader(new MemoryStream(new ASCIIEncoding().GetBytes(xmlScript)));
			IMutablePicoContainer parent = new DefaultPicoContainer();
			IPicoContainer pico = BuildContainer(new XMLContainerBuilder(scriptStream), parent, new ArrayList());

			Assert.IsNotNull(pico.GetComponentInstance(typeof (StringBuilder)));
			Assert.IsNotNull(pico.GetComponentInstance(typeof (DefaultWebServerConfig)));
			Assert.IsNotNull(pico.GetComponentInstance("NanoContainer.Test.TestModel.WebServer"));
		}
示例#54
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());
        }
示例#55
0
        public void ContainersArePutLastAndTheOthersAreMaintainedInSamePlace()
        {
            IList l = new ArrayList();

            l.Add(new InstanceComponentAdapter("aa", "ComponentC"));
            l.Add(new InstanceComponentAdapter("aaa", new DefaultPicoContainer()));
            l.Add(new InstanceComponentAdapter("aaa1", "ComponentV"));
            l.Add(new InstanceComponentAdapter("bbb", new DefaultPicoContainer()));
            l.Add(new InstanceComponentAdapter("aa11", "ComponentD"));
            l.Add(new InstanceComponentAdapter("ccc", new DefaultPicoContainer()));
            l.Add(new InstanceComponentAdapter("casa", "asa"));

            l = DefaultPicoContainer.OrderComponentAdaptersWithContainerAdaptersLast(l);
            Assert.IsTrue(((IComponentAdapter)l[4]).GetComponentInstance(null) is DefaultPicoContainer);
            Assert.IsTrue(((IComponentAdapter)l[5]).GetComponentInstance(null) is DefaultPicoContainer);
            Assert.IsTrue(((IComponentAdapter)l[6]).GetComponentInstance(null) is DefaultPicoContainer);
        }
        public void ComponentInstancesFromParentsAreNotDirectlyAccessible()
        {
            IMutablePicoContainer a = new DefaultPicoContainer();
            IMutablePicoContainer b = new DefaultPicoContainer(a);
            IMutablePicoContainer c = new DefaultPicoContainer(b);

            object ao = new object();
            object bo = new object();
            object co = new object();

            a.RegisterComponentInstance("a", ao);
            b.RegisterComponentInstance("b", bo);
            c.RegisterComponentInstance("c", co);

            Assert.AreEqual(1, a.ComponentInstances.Count);
            Assert.AreEqual(1, b.ComponentInstances.Count);
            Assert.AreEqual(1, c.ComponentInstances.Count);
        }
示例#57
0
        public void ComponentInstancesFromParentsAreNotDirectlyAccessible()
        {
            IMutablePicoContainer a = new DefaultPicoContainer();
            IMutablePicoContainer b = new DefaultPicoContainer(a);
            IMutablePicoContainer c = new DefaultPicoContainer(b);

            object ao = new object();
            object bo = new object();
            object co = new object();

            a.RegisterComponentInstance("a", ao);
            b.RegisterComponentInstance("b", bo);
            c.RegisterComponentInstance("c", co);

            Assert.AreEqual(1, a.ComponentInstances.Count);
            Assert.AreEqual(1, b.ComponentInstances.Count);
            Assert.AreEqual(1, c.ComponentInstances.Count);
        }
		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)
			{
			}
		}
示例#59
0
		public void CreateSimpleContainer()
		{
			string xmlScript = @"
				<container>
					<assemblies>
						<element file='NanoContainer.Tests.dll'/>
					</assemblies>
					<component-implementation type='System.Text.StringBuilder'/>
					<component-implementation key='typeof(NanoContainer.Test.TestModel.WebServerConfig)' type='NanoContainer.Test.TestModel.DefaultWebServerConfig'/>
					<component-implementation key='NanoContainer.Test.TestModel.WebServer' type='NanoContainer.Test.TestModel.DefaultWebServer'/>
				</container>";

			StreamReader scriptStream = new StreamReader(new MemoryStream(new ASCIIEncoding().GetBytes(xmlScript)));
			IMutablePicoContainer parent = new DefaultPicoContainer();
			ContainerBuilderFacade cbf = new XmlContainerBuilderFacade(scriptStream);
			IPicoContainer pico = cbf.Build(parent, new ArrayList());

			Assert.IsNotNull(pico.GetComponentInstance(typeof (StringBuilder)));
			Assert.IsNotNull(pico.GetComponentInstance(typeof (WebServerConfig)));
			Assert.IsNotNull(pico.GetComponentInstance("NanoContainer.Test.TestModel.WebServer"));
		}
		public void CreateSimpleContainerWithExplicitKeysAndParameters()
		{
			string xmlScript = @"
					<container>
						<component-implementation key='aBuffer' class='System.Text.StringBuilder'/>
						<component-implementation key='NanoContainer.Test.TestModel.WebServerConfig' class='NanoContainer.Test.TestModel.DefaultWebServerConfig'/>
						<component-implementation key='NanoContainer.Test.TestModel.WebServer' class='NanoContainer.Test.TestModel.DefaultWebServer'>
				 				<parameter key='NanoContainer.Test.TestModel.WebServerConfig'/>
				 				<parameter key='aBuffer'/>
						</component-implementation>
					</container>";

			StreamReader scriptStream = new StreamReader(new MemoryStream(new ASCIIEncoding().GetBytes(xmlScript)));

			IMutablePicoContainer parent = new DefaultPicoContainer();
			IPicoContainer pico = BuildContainer(new XMLContainerBuilder(scriptStream), parent, new ArrayList());

			Assert.AreEqual(3, pico.ComponentInstances.Count);
			Assert.IsNotNull(pico.GetComponentInstance("aBuffer"));
			Assert.IsNotNull(pico.GetComponentInstance("NanoContainer.Test.TestModel.WebServerConfig"));
			Assert.IsNotNull(pico.GetComponentInstance("NanoContainer.Test.TestModel.WebServer"));
		}