Example #1
0
        public void TestAutoInflationDouble()
        {
            pool.instanceProvider = new TestInstanceProvider();

            ClassToBeInjected instance1 = pool.GetInstance();

            Assert.IsNotNull(instance1);
            Assert.AreEqual(1, pool.instanceCount);                     //First call creates one instance
            Assert.AreEqual(0, pool.available);                         //Nothing available

            ClassToBeInjected instance2 = pool.GetInstance();

            Assert.IsNotNull(instance2);
            Assert.AreNotSame(instance1, instance2);
            Assert.AreEqual(2, pool.instanceCount);                     //Second call doubles. We have 2
            Assert.AreEqual(0, pool.available);                         //Nothing available

            ClassToBeInjected instance3 = pool.GetInstance();

            Assert.IsNotNull(instance3);
            Assert.AreEqual(4, pool.instanceCount);                     //Third call doubles. We have 4
            Assert.AreEqual(1, pool.available);                         //One allocated. One available.

            ClassToBeInjected instance4 = pool.GetInstance();

            Assert.IsNotNull(instance4);
            Assert.AreEqual(4, pool.instanceCount);                     //Fourth call. No doubling since one was available.
            Assert.AreEqual(0, pool.available);

            ClassToBeInjected instance5 = pool.GetInstance();

            Assert.IsNotNull(instance5);
            Assert.AreEqual(8, pool.instanceCount);                     //Fifth call. Double to 8.
            Assert.AreEqual(3, pool.available);                         //Three left unallocated.
        }
        public void TestGetFromPool()
        {
            IPool <ClassToBeInjected> pool = new Pool <ClassToBeInjected> ();

            // Format the pool
            pool.size             = 4;
            pool.instanceProvider = new TestInstanceProvider();

            IInjectionBinding binding = new InjectionBinding(resolver);

            binding.Bind <IPool <ClassToBeInjected> > ().To <Pool <ClassToBeInjected> > ().ToValue(pool);

            IPool <ClassToBeInjected> myPool = factory.Get(binding) as Pool <ClassToBeInjected>;

            Assert.NotNull(myPool);

            ClassToBeInjected instance1 = myPool.GetInstance() as ClassToBeInjected;

            Assert.NotNull(instance1);

            ClassToBeInjected instance2 = myPool.GetInstance() as ClassToBeInjected;

            Assert.NotNull(instance2);

            Assert.AreNotSame(instance1, instance2);
        }
		public ConstructorNamedInjection(
			[Name("First")] ClassToBeInjected first,
			[Name("Second")] ClassToBeInjected second)
		{
			this.first = first;
			this.second = second;
		}
        public void PseudoConstruct(
			[Name("First")] ClassToBeInjected first,
			[Name("Second")] ClassToBeInjected second)
        {
            this.first = first;
            this.second = second;
        }
Example #5
0
 public ConstructorNamedInjection(
     [Name("First")] ClassToBeInjected first,
     [Name("Second")] ClassToBeInjected second)
 {
     this.first  = first;
     this.second = second;
 }
Example #6
0
        public void TestAutoInflationIncrement()
        {
            pool.instanceProvider = new TestInstanceProvider();
            pool.inflationType    = PoolInflationType.INCREMENT;

            int testCount = 10;

            Stack stack = new Stack();

            //Calls should simply increment. There will never be unallocated
            for (int a = 0; a < testCount; a++)
            {
                ClassToBeInjected instance = pool.GetInstance();
                Assert.IsNotNull(instance);
                Assert.AreEqual(a + 1, pool.instanceCount);
                Assert.AreEqual(0, pool.available);
                stack.Push(instance);
            }

            //Now return the instances
            for (int a = 0; a < testCount; a++)
            {
                ClassToBeInjected instance = stack.Pop() as ClassToBeInjected;
                pool.ReturnInstance(instance);

                Assert.AreEqual(a + 1, pool.available, "This one");
                Assert.AreEqual(testCount, pool.instanceCount, "Or this one");
            }
        }
Example #7
0
        public void TestGetNamedInstance2()
        {
            binder.Bind <ClassToBeInjected> ().To <ClassToBeInjected> ().ToName <MarkerClass>();

            ClassToBeInjected instance = binder.GetInstance <ClassToBeInjected> (typeof(MarkerClass)) as ClassToBeInjected;

            Assert.IsNotNull(instance);
            Assert.That(instance is ClassToBeInjected);
        }
Example #8
0
        public void TestGetInstance2()
        {
            binder.Bind <ClassToBeInjected> ().To <ClassToBeInjected> ();

            ClassToBeInjected instance = binder.GetInstance <ClassToBeInjected> () as ClassToBeInjected;

            Assert.IsNotNull(instance);
            Assert.That(instance is ClassToBeInjected);
        }
Example #9
0
        public void TestGetNamedInstance3()
        {
            binder.Bind <ClassToBeInjected> ().To <ClassToBeInjected> ().ToName(SomeEnum.ONE);

            ClassToBeInjected instance = binder.GetInstance(typeof(ClassToBeInjected), SomeEnum.ONE) as ClassToBeInjected;

            Assert.IsNotNull(instance);
            Assert.That(instance is ClassToBeInjected);
        }
Example #10
0
 public void TestAddList()
 {
     pool.size = 4;
     ClassToBeInjected[] list = new ClassToBeInjected[pool.size];
     for (int a = 0; a < pool.size; a++)
     {
         list[a] = new ClassToBeInjected();
     }
     pool.Add(list);
     Assert.AreEqual(pool.size, pool.available);
 }
Example #11
0
        public void TestConstructorNamedInjection()
        {
            ClassToBeInjected class1 = new ClassToBeInjected();
            ClassToBeInjected class2 = new ClassToBeInjected();

            binder.Bind <ClassToBeInjected>().To(class1).ToName("First");
            binder.Bind <ClassToBeInjected>().To(class2).ToName("Second");
            binder.Bind <ConstructorNamedInjection>().To <ConstructorNamedInjection>();
            var instance = binder.GetInstance <ConstructorNamedInjection>() as ConstructorNamedInjection;

            Assert.That(instance.first.GetType() == typeof(ClassToBeInjected));
            Assert.That(instance.second.GetType() == typeof(ClassToBeInjected));
            Assert.That(instance.first != instance.second);
        }
Example #12
0
		public void TestConstructorNamedInjection() 
		{
			ClassToBeInjected class1 = new ClassToBeInjected();
			ClassToBeInjected class2 = new ClassToBeInjected();
			
			binder.Bind<ClassToBeInjected>().To(class1).ToName("First");
			binder.Bind<ClassToBeInjected>().To(class2).ToName("Second");
			binder.Bind<ConstructorNamedInjection>().To<ConstructorNamedInjection>();
			var instance = binder.GetInstance<ConstructorNamedInjection>() as ConstructorNamedInjection;
			
			Assert.That(instance.first.GetType() == typeof(ClassToBeInjected) );
			Assert.That(instance.second.GetType() == typeof(ClassToBeInjected) );
			Assert.That(instance.first != instance.second);
		}
Example #13
0
        public void TestRemoveList2()
        {
            pool.size = 4;
            for (int a = 0; a < pool.size; a++)
            {
                pool.Add(new ClassToBeInjected());
            }

            ClassToBeInjected[] removalList = new ClassToBeInjected[3];
            for (int a = 0; a < pool.size - 1; a++)
            {
                removalList [a] = pool.GetInstance();
            }
            pool.Remove(removalList);
            Assert.AreEqual(1, pool.available);
        }
Example #14
0
        public void TestGetInstance()
        {
            pool.size = 4;
            for (int a = 0; a < pool.size; a++)
            {
                pool.Add(new ClassToBeInjected());
            }

            for (int a = pool.size; a > 0; a--)
            {
                Assert.AreEqual(a, pool.available);
                ClassToBeInjected instance = pool.GetInstance();
                Assert.IsNotNull(instance);
                Assert.IsInstanceOf <ClassToBeInjected> (instance);
                Assert.AreEqual(a - 1, pool.available);
            }
        }
Example #15
0
        public void TestRemoveFromPool()
        {
            pool.size = 4;
            for (int a = 0; a < pool.size; a++)
            {
                pool.Add(new ClassToBeInjected());
            }

            for (int a = pool.size; a > 0; a--)
            {
                Assert.AreEqual(a, pool.available);
                ClassToBeInjected instance = pool.GetInstance();
                pool.Remove(instance);
            }

            Assert.AreEqual(0, pool.available);
        }
        public void TestGetInstance()
        {
            binding.Size = 4;
            for (int a = 0; a < binding.Size; a++)
            {
                b.To(new ClassToBeInjected());
            }

            for (int a = binding.Size; a > 0; a--)
            {
                Assert.AreEqual(a, binding.Available);
                ClassToBeInjected instance = binding.GetInstance() as ClassToBeInjected;
                Assert.IsNotNull(instance);
                Assert.IsInstanceOf <ClassToBeInjected> (instance);
                Assert.AreEqual(a - 1, binding.Available);
            }
        }
        public void TestRemoveFromPool()
        {
            binding.Size = 4;
            for (int a = 0; a < binding.Size; a++)
            {
                b.To(new ClassToBeInjected());
            }

            for (int a = binding.Size; a > 0; a--)
            {
                Assert.AreEqual(a, binding.Available);
                ClassToBeInjected instance = binding.GetInstance() as ClassToBeInjected;
                b.RemoveValue(instance);
            }

            Assert.AreEqual(0, binding.Available);
        }
Example #18
0
 public ConstructorInjectsClassToBeInjected(ClassToBeInjected injected)
 {
     this.injected = injected;
 }
 public void PseudoConstruct(ClassToBeInjected injectedClass)
 {
     InjectedClass = injectedClass;
     PseudoConstructed = true;
 }
		public ConstructorInjectsClassToBeInjected (ClassToBeInjected injected)
		{
			this.injected = injected;
		}