Example #1
0
        public void TestBindList()
        {
            IList <string> injected = new List <string>();

            injected.Add("hi");
            injected.Add("hello");
            injected.Add("bye");

            ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();

            cb.BindList <StringList, string>(injected);

            IInjector      i      = TangFactory.GetTang().NewInjector(cb.Build());
            IList <string> actual = ((StringClass)i.GetInstance(typeof(StringClass))).StringList;

            Assert.True(actual.Contains("hi"));
            Assert.True(actual.Contains("hello"));
            Assert.True(actual.Contains("bye"));
            Assert.Equal(actual.Count, 3);
        }
Example #2
0
        public void TestObjectInjectWithTypeType()
        {
            IList <Type> injected = new List <Type>();

            injected.Add(typeof(TestSetInjection.Integer));
            injected.Add(typeof(TestSetInjection.Float));

            ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();

            cb.BindList(typeof(ListOfClasses), injected);

            IInjector i = TangFactory.GetTang().NewInjector(cb.Build());

            i.BindVolatileInstance(GenericType <TestSetInjection.Integer> .Class, new TestSetInjection.Integer(42));
            i.BindVolatileInstance(GenericType <TestSetInjection.Float> .Class, new TestSetInjection.Float(42.0001f));
            IList <INumber> actual = ((PoolListClass)i.GetInstance(typeof(PoolListClass))).Numbers;

            Assert.True(actual.Contains(new TestSetInjection.Integer(42)));
            Assert.True(actual.Contains(new TestSetInjection.Float(42.0001f)));
        }
Example #3
0
        public void TestIntListWithGenericType()
        {
            IList <string> injected = new List <string>();

            injected.Add("1");
            injected.Add("2");
            injected.Add("3");

            ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();

            cb.BindList <IntListClass.NamedIntList, int>(GenericType <IntListClass.NamedIntList> .Class, injected);

            IInjector    i = TangFactory.GetTang().NewInjector(cb.Build());
            IntListClass o = i.GetInstance <IntListClass>();

            IList <int> expected = new List <int>();

            expected.Add(1);
            expected.Add(2);
            expected.Add(3);
            o.Verify(expected);
        }
Example #4
0
        public void TestBoolListWithGenericType()
        {
            IList <string> injected = new List <string>();

            injected.Add("true");
            injected.Add("false");
            injected.Add("true");

            ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();

            cb.BindList <BoolListClass.NamedBoolList, bool>(GenericType <BoolListClass.NamedBoolList> .Class, injected);

            IInjector     i = TangFactory.GetTang().NewInjector(cb.Build());
            BoolListClass o = i.GetInstance <BoolListClass>();

            IList <bool> expected = new List <bool>();

            expected.Add(true);
            expected.Add(false);
            expected.Add(true);
            o.Verify(expected);
        }
Example #5
0
        public void TestObjectInjectWithInjectableSubclassesMultipleInstances()
        {
            IList <string> injected = new List <string>();

            injected.Add(typeof(TestSetInjection.Integer1).AssemblyQualifiedName);
            injected.Add(typeof(TestSetInjection.Integer1).AssemblyQualifiedName);
            injected.Add(typeof(TestSetInjection.Float1).AssemblyQualifiedName);

            ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();

            cb.BindIntNamedParam <TestSetInjection.Integer1.NamedInt>("5");
            cb.BindNamedParam <TestSetInjection.Float1.NamedFloat, float>("12.5");
            cb.BindList <ListOfClasses, INumber>(injected);

            IInjector i = TangFactory.GetTang().NewInjector(cb.Build());

            IList <INumber> actual = ((PoolListClass)i.GetInstance(typeof(PoolListClass))).Numbers;

            Assert.True(actual.Count == 3);
            Assert.True(actual.Contains(new TestSetInjection.Integer1(5)));
            Assert.True(actual.Contains(new TestSetInjection.Integer1(5)));
            Assert.True(actual.Contains(new TestSetInjection.Float1(12.5f)));
        }
Example #6
0
        public void TestObjectInjectWithInjectableSubclassesAndTypeofNamedParameter()
        {
            IList <string> injected = new List <string>();

            injected.Add(typeof(TestSetInjection.Integer1).AssemblyQualifiedName);
            injected.Add(typeof(TestSetInjection.Integer2).AssemblyQualifiedName);
            injected.Add(typeof(TestSetInjection.Integer3).AssemblyQualifiedName);

            ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();

            cb.BindNamedParameter <TestSetInjection.Integer1.NamedInt, int>(GenericType <TestSetInjection.Integer1.NamedInt> .Class, "5");
            cb.BindNamedParameter <TestSetInjection.Integer3.NamedInt, int>(GenericType <TestSetInjection.Integer3.NamedInt> .Class, "10");
            cb.BindList <ListOfClasses, INumber>(GenericType <ListOfClasses> .Class, injected);

            IInjector i = TangFactory.GetTang().NewInjector(cb.Build());

            IList <INumber> actual = ((PoolListClass)i.GetInstance(typeof(PoolListClass))).Numbers;

            Assert.True(actual.Count == 3);
            Assert.True(actual.Contains(new TestSetInjection.Integer1(5)));
            Assert.True(actual.Contains(new TestSetInjection.Integer2()));
            Assert.True(actual.Contains(new TestSetInjection.Integer3(10)));
        }
Example #7
0
        public IConfiguration Build()
        {
            ConfigurationModule c = DeepCopy();

            if (!c.reqSet.ContainsAll(c.Builder.ReqDecl))
            {
                ISet <FieldInfo> missingSet = new MonotonicHashSet <FieldInfo>();
                foreach (FieldInfo f in c.Builder.ReqDecl)
                {
                    if (!c.reqSet.Contains(f))
                    {
                        missingSet.Add(f);
                    }
                }
                var e = new BindException(
                    "Attempt to build configuration before setting required option(s): "
                    + Builder.ToString(missingSet));
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
            }

            foreach (Type clazz in c.Builder.FreeImpls.Keys)
            {
                object i = c.Builder.FreeImpls.Get(clazz);
                if (c.setImpls.ContainsKey(i))
                {
                    var cb = (ICsInternalConfigurationBuilder)c.Builder.B;
                    cb.Bind(clazz, c.setImpls.Get(i));
                }
                else if (c.setLateImpls.ContainsKey(i))
                {
                    c.Builder.B.Bind(ReflectionUtilities.GetAssemblyQualifiedName(clazz), c.setLateImpls.Get(i));
                }
                else if (c.setImplSets.ContainsKey(i) || c.setLateImplSets.ContainsKey(i))
                {
                    foreach (Type clz in c.setImplSets.GetValuesForKey(i))
                    {
                        ICsInternalConfigurationBuilder b = (ICsInternalConfigurationBuilder)c.Builder.B;
                        b.BindSetEntry(clazz, clz);
                    }
                    foreach (string s in c.setLateImplSets.GetValuesForKey(i))
                    {
                        ICsInternalConfigurationBuilder b = (ICsInternalConfigurationBuilder)c.Builder.B;
                        b.BindSetEntry(clazz, s);
                    }
                }
                else if (c.setImplLists.ContainsKey(i))
                {
                    ICsConfigurationBuilder b = (ICsConfigurationBuilder)c.Builder.B;
                    b.BindList(clazz, setImplLists.Get(i));
                }
                else if (c.setLateImplLists.ContainsKey(i))
                {
                    ICsInternalConfigurationBuilder b = (ICsInternalConfigurationBuilder)c.Builder.B;
                    b.BindList(clazz, setLateImplLists.Get(i));
                }
            }

            foreach (Type clazz in c.Builder.FreeParams.Keys)
            {
                object p        = c.Builder.FreeParams.Get(clazz);
                string s        = c.setParams.Get(p);
                bool   foundOne = false;
                if (s != null)
                {
                    ICsConfigurationBuilder cb = c.Builder.B;
                    cb.BindNamedParameter(clazz, s);
                    foundOne = true;
                }

                IList <string> paramListStr = c.setParamLists.Get(p);
                if (paramListStr != null)
                {
                    ICsInternalConfigurationBuilder b = (ICsInternalConfigurationBuilder)c.Builder.B;
                    b.BindList(clazz, paramListStr);
                    foundOne = true;
                }

                foreach (string paramStr in c.setParamSets.GetValuesForKey(p))
                {
                    ICsInternalConfigurationBuilder b = (ICsInternalConfigurationBuilder)c.Builder.B;
                    b.BindSetEntry(clazz, paramStr);
                    foundOne = true;
                }

                if (!foundOne)
                {
                    if (!ReflectionUtilities.IsInstanceOfGeneric(p, typeof(OptionalParameter <>)))
                    {
                        Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new IllegalStateException(), LOGGER);
                    }
                }
            }
            return(c.Builder.B.Build());
        }