http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/Injection/TestListInjection.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/Injection/TestListInjection.cs b/lang/cs/Tests/TangTests/Injection/TestListInjection.cs new file mode 100644 index 0000000..1c05516 --- /dev/null +++ b/lang/cs/Tests/TangTests/Injection/TestListInjection.cs @@ -0,0 +1,565 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ClassHierarchyProto; +using Org.Apache.Reef.Tang.Annotations; +using Org.Apache.Reef.Tang.Implementations; +using Org.Apache.Reef.Tang.Interface; +using Org.Apache.Reef.Tang.Types; +using Org.Apache.Reef.Tang.Util; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Org.Apache.Reef.Tang.Test.Injection +{ + /// <summary> + /// Test injection with a List + /// </summary> + [TestClass] + public class TestListInjection + { + /// <summary> + /// Tests the string inject default. + /// </summary> + [TestMethod] + public void TestStringInjectDefault() + { + StringClass b = TangFactory.GetTang().NewInjector().GetInstance<StringClass>(); + + IList<string> actual = b.StringList; + + Assert.IsTrue(actual.Contains("one")); + Assert.IsTrue(actual.Contains("two")); + Assert.IsTrue(actual.Contains("three")); + } + + /// <summary> + /// Tests the int inject default. + /// </summary> + [TestMethod] + public void TestIntInjectDefault() + { + IntClass b = TangFactory.GetTang().NewInjector().GetInstance<IntClass>(); + + IList<int> actual = b.IntList; + + Assert.IsTrue(actual.Contains(1)); + Assert.IsTrue(actual.Contains(2)); + Assert.IsTrue(actual.Contains(3)); + } + + /// <summary> + /// Tests the string inject configuration builder. + /// </summary> + [TestMethod] + public void TestStringInjectConfigurationBuilder() + { + ICsClassHierarchy classH = TangFactory.GetTang().GetDefaultClassHierarchy(); + INamedParameterNode np = (INamedParameterNode)classH.GetNode(typeof(StringList)); + IList<string> injected = new List<string>(); + injected.Add("hi"); + injected.Add("hello"); + injected.Add("bye"); + + IConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindList(np, injected); + + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + IList<string> actual = ((StringClass)i.GetInstance(typeof(StringClass))).StringList; + + Assert.IsTrue(actual.Contains("hi")); + Assert.IsTrue(actual.Contains("hello")); + Assert.IsTrue(actual.Contains("bye")); + Assert.AreEqual(actual.Count, 3); + } + + /// <summary> + /// Tests the bool list with named parameter. + /// </summary> + [TestMethod] + public void TestBoolListWithNamedParameter() + { + ICsClassHierarchy classH = TangFactory.GetTang().GetDefaultClassHierarchy(); + INamedParameterNode np = (INamedParameterNode)classH.GetNode(typeof(BoolListClass.NamedBoolList)); + IList<string> injected = new List<string>(); + injected.Add("true"); + injected.Add("false"); + injected.Add("true"); + + IConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindList(np, 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); + } + + /// <summary> + /// Tests the type of the bool list with generic. + /// </summary> + [TestMethod] + 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); + } + + /// <summary> + /// Tests the int list with named parameter. + /// </summary> + [TestMethod] + public void TestIntListWithNamedParameter() + { + ICsClassHierarchy classH = TangFactory.GetTang().GetDefaultClassHierarchy(); + INamedParameterNode np = (INamedParameterNode)classH.GetNode(typeof(IntListClass.NamedIntList)); + IList<string> injected = new List<string>(); + injected.Add("1"); + injected.Add("2"); + injected.Add("3"); + + IConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindList(np, 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); + } + + /// <summary> + /// Tests the type of the int list with generic. + /// </summary> + [TestMethod] + 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); + } + + /// <summary> + /// Tests the string inject. + /// </summary> + [TestMethod] + public void TestStringInject() + { + IList<string> injected = new List<string>(); + injected.Add("hi"); + injected.Add("hello"); + injected.Add("bye"); + + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindList<StringList, string>(GenericType<StringList>.Class, injected); + + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + IList<string> actual = ((StringClass)i.GetInstance(typeof(StringClass))).StringList; + + Assert.IsTrue(actual.Contains("hi")); + Assert.IsTrue(actual.Contains("hello")); + Assert.IsTrue(actual.Contains("bye")); + Assert.AreEqual(actual.Count, 3); + } + + /// <summary> + /// Tests the node inject and bind volatile instance. + /// </summary> + [TestMethod] + public void TestNodeInjectAndBindVolatileInstance() + { + ICsClassHierarchy classH = TangFactory.GetTang().GetDefaultClassHierarchy(); + INamedParameterNode np = (INamedParameterNode)classH.GetNode(typeof(ListOfClasses)); + IList<INode> injected = new List<INode>(); + injected.Add(classH.GetNode(typeof(TestSetInjection.Integer))); + injected.Add(classH.GetNode(typeof(TestSetInjection.Float))); + + IConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindList(np, 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.IsTrue(actual.Contains(new TestSetInjection.Integer(42))); + Assert.IsTrue(actual.Contains(new TestSetInjection.Float(42.0001f))); + } + + /// <summary> + /// Tests the class name inject with named parameter node. + /// </summary> + [TestMethod] + public void TestClassNameInjectWithNamedParameterNode() + { + ICsClassHierarchy classH = TangFactory.GetTang().GetDefaultClassHierarchy(); + INamedParameterNode np = (INamedParameterNode)classH.GetNode(typeof(ListOfClasses)); + + IList<string> injected = new List<string>(); + injected.Add(typeof(TestSetInjection.Integer).AssemblyQualifiedName); + injected.Add(typeof(TestSetInjection.Float).AssemblyQualifiedName); + + IConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindList(np, 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.IsTrue(actual.Contains(new TestSetInjection.Integer(42))); + Assert.IsTrue(actual.Contains(new TestSetInjection.Float(42.0001f))); + } + + /// <summary> + /// Tests the name of the class name inject with named parameter. + /// </summary> + [TestMethod] + public void TestClassNameInjectWithNamedParameterName() + { + IList<string> injected = new List<string>(); + injected.Add(typeof(TestSetInjection.Integer).AssemblyQualifiedName); + injected.Add(typeof(TestSetInjection.Float).AssemblyQualifiedName); + + IConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindList(typeof(ListOfClasses).AssemblyQualifiedName, 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.IsTrue(actual.Contains(new TestSetInjection.Integer(42))); + Assert.IsTrue(actual.Contains(new TestSetInjection.Float(42.0001f))); + } + + /// <summary> + /// Tests the object inject with injectable subclasses. + /// </summary> + [TestMethod] + public void TestObjectInjectWithInjectableSubclasses() + { + 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.BindList(typeof(ListOfClasses).AssemblyQualifiedName, injected); + cb.BindNamedParameter<TestSetInjection.Integer1.NamedInt, int>(GenericType<TestSetInjection.Integer1.NamedInt>.Class, "5"); + cb.BindNamedParameter<TestSetInjection.Integer3.NamedInt, int>(GenericType<TestSetInjection.Integer3.NamedInt>.Class, "10"); + + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + + IList<INumber> actual = ((PoolListClass)i.GetInstance(typeof(PoolListClass))).Numbers; + + Assert.IsTrue(actual.Count == 3); + Assert.IsTrue(actual.Contains(new TestSetInjection.Integer1(5))); + Assert.IsTrue(actual.Contains(new TestSetInjection.Integer2())); + Assert.IsTrue(actual.Contains(new TestSetInjection.Integer3(10))); + } + + /// <summary> + /// Tests the object inject with injectable subclasses multiple instances. + /// </summary> + [TestMethod] + 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.BindNamedParameter<TestSetInjection.Integer1.NamedInt, int>(GenericType<TestSetInjection.Integer1.NamedInt>.Class, "5"); + cb.BindNamedParameter<TestSetInjection.Float1.NamedFloat, float>(GenericType<TestSetInjection.Float1.NamedFloat>.Class, "12.5"); + 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.IsTrue(actual.Count == 3); + Assert.IsTrue(actual.Contains(new TestSetInjection.Integer1(5))); + Assert.IsTrue(actual.Contains(new TestSetInjection.Integer1(5))); + Assert.IsTrue(actual.Contains(new TestSetInjection.Float1(12.5f))); + } + + /// <summary> + /// Tests the object inject with injectable subclasses and typeof named parameter. + /// </summary> + [TestMethod] + 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.IsTrue(actual.Count == 3); + Assert.IsTrue(actual.Contains(new TestSetInjection.Integer1(5))); + Assert.IsTrue(actual.Contains(new TestSetInjection.Integer2())); + Assert.IsTrue(actual.Contains(new TestSetInjection.Integer3(10))); + } + + /// <summary> + /// Tests the object inject with names configuration builder. + /// </summary> + [TestMethod] + public void TestObjectInjectWithNames() + { + ICsClassHierarchy classH = TangFactory.GetTang().GetDefaultClassHierarchy(); + IList<INode> injected = new List<INode>(); + injected.Add(classH.GetNode(typeof(TestSetInjection.Integer))); + injected.Add(classH.GetNode(typeof(TestSetInjection.Float))); + + IConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindList(typeof(ListOfClasses).AssemblyQualifiedName, 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.IsTrue(actual.Contains(new TestSetInjection.Integer(42))); + Assert.IsTrue(actual.Contains(new TestSetInjection.Float(42.0001f))); + } + + /// <summary> + /// Tests the object inject with type type cs configuration builder. + /// </summary> + [TestMethod] + 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.IsTrue(actual.Contains(new TestSetInjection.Integer(42))); + Assert.IsTrue(actual.Contains(new TestSetInjection.Float(42.0001f))); + } + + ///// <summary> + ///// Tests the subclass inject with multiple instances. + ///// </summary> + //[TestMethod] + //public void TestSubclassInjectWithMultipleInstances() + //{ + // ICsConfigurationBuilder cb1 = TangFactory.GetTang().NewConfigurationBuilder() + // .BindNamedParameter<TestSetInjection.Integer3.NamedInt, int>(GenericType<TestSetInjection.Integer3.NamedInt>.Class, "10"); + // TestSetInjection.Integer3 integer3 = TangFactory.GetTang().NewInjector(cb1.Build()).GetInstance<TestSetInjection.Integer3>(); + + // ICsClassHierarchy classH = TangFactory.GetTang().GetDefaultClassHierarchy(); + // INamedParameterNode np = (INamedParameterNode)classH.GetNode(typeof(ListOfClasses)); + // IList<object> injected = new List<object>(); + // injected.Add(new TestSetInjection.Integer1(42)); //instance from the same class + // injected.Add(new TestSetInjection.Integer1(30)); //instance from the same class + // injected.Add(new TestSetInjection.Float1(42.0001f)); //instance from another subclass of the same interface + // injected.Add(typeof(TestSetInjection.Float1).AssemblyQualifiedName); //inject from another subclass of the same interface + // injected.Add(typeof(TestSetInjection.Integer1).AssemblyQualifiedName); //inject using configuration + // injected.Add(typeof(TestSetInjection.Integer2).AssemblyQualifiedName); //inject using default + // injected.Add(integer3); //add pre injected instance + + // ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + // cb.BindNamedParameter<TestSetInjection.Integer1.NamedInt, int>(GenericType<TestSetInjection.Integer1.NamedInt>.Class, "5"); + // cb.BindNamedParameter<TestSetInjection.Float1.NamedFloat, float>(GenericType<TestSetInjection.Float1.NamedFloat>.Class, "12.5"); + // cb.BindList(np, injected); + + // IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + // IList<INumber> actual = ((PoolListClass)i.GetInstance(typeof(PoolListClass))).Numbers; + // Assert.IsTrue(actual.Count == 7); + // Assert.IsTrue(actual.Contains(new TestSetInjection.Integer1(42))); + // Assert.IsTrue(actual.Contains(new TestSetInjection.Integer1(30))); + // Assert.IsTrue(actual.Contains(new TestSetInjection.Float1(42.0001f))); + // Assert.IsTrue(actual.Contains(new TestSetInjection.Float1(12.5f))); + // Assert.IsTrue(actual.Contains(new TestSetInjection.Integer1(5))); + // Assert.IsTrue(actual.Contains(new TestSetInjection.Integer2())); + // Assert.IsTrue(actual.Contains(new TestSetInjection.Integer3(10))); + //} + } + + [NamedParameter(DefaultValues = new string[] { "one", "two", "three" })] + internal class StringList : Name<IList<string>> + { + } + + [NamedParameter(DefaultValues = new string[] { "1", "2", "3" })] + internal class IntList : Name<IList<int>> + { + } + + [NamedParameter(DefaultValues = new string[] { "1", "2", "3" })] + internal class IntegerList : Name<IList<TestSetInjection.Integer>> + { + } + + internal class StringClass + { + [Inject] + private StringClass([Parameter(typeof(StringList))] IList<string> stringList) + { + this.StringList = stringList; + } + + public IList<string> StringList { get; set; } + } + + internal class IntClass + { + [Inject] + private IntClass([Parameter(typeof(IntList))] IList<int> integerList) + { + this.IntList = integerList; + } + + public IList<int> IntList { get; set; } + } + + internal class IntegerListClass + { + [Inject] + private IntegerListClass([Parameter(typeof(IntegerList))] IList<TestSetInjection.Integer> integerList) + { + this.IntegerList = integerList; + } + + public IList<TestSetInjection.Integer> IntegerList { get; set; } + } + + [NamedParameter(DefaultClasses = new Type[] { typeof(TestSetInjection.Integer), typeof(TestSetInjection.Float) })] + internal class ListOfClasses : Name<IList<INumber>> + { + } + + internal class PoolListClass + { + [Inject] + private PoolListClass([Parameter(typeof(ListOfClasses))] IList<INumber> numbers) + { + this.Numbers = numbers; + } + + public IList<INumber> Numbers { get; set; } + } + + internal class BoolListClass + { + private readonly IList<bool> b; + + [Inject] + public BoolListClass([Parameter(typeof(NamedBoolList))] IList<bool> b) + { + this.b = b; + } + + public void Verify(IList<bool> v) + { + Assert.AreEqual(v.Count, b.Count); + foreach (bool bv in v) + { + Assert.IsTrue(b.Contains(bv)); + } + } + + [NamedParameter] + public class NamedBoolList : Name<IList<bool>> + { + } + } + + internal class IntListClass + { + private readonly IList<int> l; + + [Inject] + public IntListClass([Parameter(typeof(NamedIntList))] IList<int> b) + { + this.l = b; + } + + public void Verify(IList<int> v) + { + Assert.AreEqual(v.Count, l.Count); + foreach (int iv in v) + { + Assert.IsTrue(l.Contains(iv)); + } + } + + [NamedParameter] + public class NamedIntList : Name<IList<int>> + { + } + } +}
http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/Injection/TestMissingParameters.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/Injection/TestMissingParameters.cs b/lang/cs/Tests/TangTests/Injection/TestMissingParameters.cs new file mode 100644 index 0000000..09fdd78 --- /dev/null +++ b/lang/cs/Tests/TangTests/Injection/TestMissingParameters.cs @@ -0,0 +1,151 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +using System; +using Org.Apache.Reef.Tang.Annotations; +using Org.Apache.Reef.Tang.Exceptions; +using Org.Apache.Reef.Tang.Implementations; +using Org.Apache.Reef.Tang.Interface; +using Org.Apache.Reef.Tang.Util; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Org.Apache.Reef.Tang.Test.Injection +{ + [TestClass] + public class TestMissingParameters + { + [TestMethod] + public void MultipleParameterTest() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindNamedParameter<MultiParameterConstructor.NamedString, string>(GenericType<MultiParameterConstructor.NamedString>.Class, "foo"); + cb.BindNamedParameter<MultiParameterConstructor.NamedInt, int>(GenericType<MultiParameterConstructor.NamedInt>.Class, "8"); + cb.BindNamedParameter<MultiParameterConstructor.NamedBool, bool>(GenericType<MultiParameterConstructor.NamedBool>.Class, "true"); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + var o = i.GetInstance<MultiParameterConstructor>(); + o.Verify("foo", 8, true); + } + + [TestMethod] + public void MissingAllParameterTest() + { + //Org.Apache.Reef.Tang.Test.Injection.MultiParameterConstructor, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + //missing arguments: [ + //Org.Apache.Reef.Tang.Test.Injection.MultiParameterConstructor+NamedBool, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + //Org.Apache.Reef.Tang.Test.Injection.MultiParameterConstructor+NamedString, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + //Org.Apache.Reef.Tang.Test.Injection.MultiParameterConstructor+NamedInt, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + //] + MultiParameterConstructor obj = null; + try + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + obj = i.GetInstance<MultiParameterConstructor>(); + } + catch (InjectionException e) + { + System.Diagnostics.Debug.WriteLine(e); + } + Assert.IsNull(obj); + } + + [TestMethod] + public void MissingTwoParameterTest() + { + //Cannot inject Org.Apache.Reef.Tang.Test.Injection.MultiParameterConstructor, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: + //Org.Apache.Reef.Tang.Test.Injection.MultiParameterConstructor, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + //missing arguments: [ + //Org.Apache.Reef.Tang.Test.Injection.MultiParameterConstructor+NamedString, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + //Org.Apache.Reef.Tang.Test.Injection.MultiParameterConstructor+NamedInt, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + //] + MultiParameterConstructor obj = null; + try + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindNamedParameter<MultiParameterConstructor.NamedBool, bool>(GenericType<MultiParameterConstructor.NamedBool>.Class, "true"); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + obj = i.GetInstance<MultiParameterConstructor>(); + } + catch (InjectionException e) + { + System.Diagnostics.Debug.WriteLine(e); + } + Assert.IsNull(obj); + } + + [TestMethod] + public void MissingOneParameterTest() + { + //Cannot inject Org.Apache.Reef.Tang.Test.Injection.MultiParameterConstructor, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: + //Org.Apache.Reef.Tang.Test.Injection.MultiParameterConstructor, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + //missing argument Org.Apache.Reef.Tang.Test.Injection.MultiParameterConstructor+NamedInt, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + MultiParameterConstructor obj = null; + try + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindNamedParameter<MultiParameterConstructor.NamedBool, bool>(GenericType<MultiParameterConstructor.NamedBool>.Class, "true"); + cb.BindNamedParameter<MultiParameterConstructor.NamedString, string>(GenericType<MultiParameterConstructor.NamedString>.Class, "foo"); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + obj = i.GetInstance<MultiParameterConstructor>(); + } + catch (InjectionException e) + { + System.Diagnostics.Debug.WriteLine(e); + } + Assert.IsNull(obj); + } + } + + public class MultiParameterConstructor + { + private readonly string str; + private readonly int iVal; + private readonly bool bVal; + + [Inject] + public MultiParameterConstructor([Parameter(typeof(NamedBool))] bool b, [Parameter(typeof(NamedString))] string s, [Parameter(typeof(NamedInt))] int i) + { + this.str = s; + this.iVal = i; + this.bVal = b; + } + + public void Verify(string s, int i, bool b) + { + Assert.AreEqual(str, s); + Assert.AreEqual(iVal, i); + Assert.AreEqual(bVal, b); + } + + [NamedParameter] + public class NamedString : Name<string> + { + } + + [NamedParameter] + public class NamedInt : Name<int> + { + } + + [NamedParameter] + public class NamedBool : Name<bool> + { + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/Injection/TestMissingParamtersInNested.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/Injection/TestMissingParamtersInNested.cs b/lang/cs/Tests/TangTests/Injection/TestMissingParamtersInNested.cs new file mode 100644 index 0000000..25ddb0c --- /dev/null +++ b/lang/cs/Tests/TangTests/Injection/TestMissingParamtersInNested.cs @@ -0,0 +1,142 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +using System; +using Org.Apache.Reef.Tang.Annotations; +using Org.Apache.Reef.Tang.Exceptions; +using Org.Apache.Reef.Tang.Implementations; +using Org.Apache.Reef.Tang.Interface; +using Org.Apache.Reef.Tang.Util; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Org.Apache.Reef.Tang.Test.Injection +{ + [TestClass] + public class TestMissingParamtersInNested + { + [TestMethod] + public void InnerParameterTest() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindNamedParameter<ReferencedClass.NamedInt, int>(GenericType<ReferencedClass.NamedInt>.Class, "8"); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + var o = i.GetInstance<ReferencedClass>(); + Assert.IsNotNull(o); + } + + [TestMethod] + public void NestedParameterTest() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindNamedParameter<OuterClass.NamedString, string>(GenericType<OuterClass.NamedString>.Class, "foo"); + cb.BindNamedParameter<ReferencedClass.NamedInt, int>(GenericType<ReferencedClass.NamedInt>.Class, "8"); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + var o = i.GetInstance<OuterClass>(); + Assert.IsNotNull(o); + } + + [TestMethod] + public void MissingAllParameterTest() + { + //Cannot inject Org.Apache.Reef.Tang.Test.Injection.OuterClass, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: + //Org.Apache.Reef.Tang.Test.Injection.ReferencedClass, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + //missing argument Org.Apache.Reef.Tang.Test.Injection.ReferencedClass+NamedInt, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + OuterClass obj = null; + try + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + obj = i.GetInstance<OuterClass>(); + } + catch (InjectionException e) + { + System.Diagnostics.Debug.WriteLine(e); + } + Assert.IsNull(obj); + } + + [TestMethod] + public void MissingInnerParameterTest() + { + //Cannot inject Org.Apache.Reef.Tang.Test.Injection.OuterClass, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: + //Org.Apache.Reef.Tang.Test.Injection.ReferencedClass, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + //missing argument Org.Apache.Reef.Tang.Test.Injection.ReferencedClass+NamedInt, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + OuterClass obj = null; + try + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindNamedParameter<OuterClass.NamedString, string>(GenericType<OuterClass.NamedString>.Class, "foo"); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + obj = i.GetInstance<OuterClass>(); + } + catch (InjectionException e) + { + System.Diagnostics.Debug.WriteLine(e); + } + Assert.IsNull(obj); + } + + [TestMethod] + public void MissingOuterParameterTest() + { + //Cannot inject Org.Apache.Reef.Tang.Test.Injection.OuterClass, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: + //Org.Apache.Reef.Tang.Test.Injection.OuterClass, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + //missing argument Org.Apache.Reef.Tang.Test.Injection.OuterClass+NamedString, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + OuterClass obj = null; + try + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindNamedParameter<ReferencedClass.NamedInt, int>(GenericType<ReferencedClass.NamedInt>.Class, "8"); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + obj = i.GetInstance<OuterClass>(); + } + catch (InjectionException e) + { + System.Diagnostics.Debug.WriteLine(e); + } + Assert.IsNull(obj); + } + } + + class OuterClass + { + [Inject] + public OuterClass([Parameter(typeof(NamedString))] string s, ReferencedClass refCls) + { + } + + [NamedParameter] + public class NamedString : Name<string> + { + } + } + + class ReferencedClass + { + [Inject] + public ReferencedClass([Parameter(typeof(NamedInt))] int i) + { + } + + [NamedParameter] + public class NamedInt : Name<int> + { + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/Injection/TestMultipleConstructors.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/Injection/TestMultipleConstructors.cs b/lang/cs/Tests/TangTests/Injection/TestMultipleConstructors.cs new file mode 100644 index 0000000..bc3d25a --- /dev/null +++ b/lang/cs/Tests/TangTests/Injection/TestMultipleConstructors.cs @@ -0,0 +1,360 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +using System; +using Org.Apache.Reef.Tang.Annotations; +using Org.Apache.Reef.Tang.Exceptions; +using Org.Apache.Reef.Tang.Implementations; +using Org.Apache.Reef.Tang.Interface; +using Org.Apache.Reef.Tang.Util; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Org.Apache.Reef.Tang.Test.Injection +{ + public interface IH + { + } + + [TestClass] + public class TestMultipleConstructors + { + [TestMethod] + public void TestMissingAllParameters() + { + //Multiple infeasible plans: Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest + //, Org.Apache.Reef.Tang.Test + //, Version=1.0.0.0 + //, Culture=neutral + //, PublicKeyToken=null: + + // [ Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest = new Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest + // ( System.Boolean Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedBool = + // , System.Int32 Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedInt = + // ) + // | new Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest + // ( System.String Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedString = + // , System.Int32 Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedInt = + // ) + // | new Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest + // ( System.Boolean Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedBool = + // , System.String Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedString = + // , System.Int32 Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedInt = + // ) + // ] + MultiConstructorTest obj = null; + try + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + obj = i.GetInstance<MultiConstructorTest>(); + } + catch (InjectionException) + { + } + Assert.IsNull(obj); + } + + [TestMethod] + public void TestMissingIntParameter() + { + //Multiple infeasible plans: Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest + //, Org.Apache.Reef.Tang.Test + //, Version=1.0.0.0 + //, Culture=neutral + //, PublicKeyToken=null: + // [ Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest = new Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest + // ( System.Boolean Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedBool = False + // , System.Int32 Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedInt = + // ) + // | new Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest + // ( System.String Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedString = foo + // , System.Int32 Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedInt = + // ) + // | new Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest + // ( System.Boolean Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedBool = False + // , System.String Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedString = foo + // , System.Int32 Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedInt = + // ) + // ] + MultiConstructorTest obj = null; + try + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindNamedParameter<MultiConstructorTest.NamedString, string>(GenericType<MultiConstructorTest.NamedString>.Class, "foo"); + cb.BindNamedParameter<MultiConstructorTest.NamedBool, bool>(GenericType<MultiConstructorTest.NamedBool>.Class, "true"); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + obj = i.GetInstance<MultiConstructorTest>(); + } + catch (InjectionException) + { + } + Assert.IsNull(obj); + } + + [TestMethod] + public void TestOnlyBoolParameter() + { + //Multiple infeasible plans: Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest + //, Org.Apache.Reef.Tang.Test + //, Version=1.0.0.0 + //, Culture=neutral + //, PublicKeyToken=null: + // [ Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest = new Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest + // ( System.Boolean Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedBool = False + // , System.Int32 Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedInt = + // ) + // | new Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest + // ( System.String Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedString = + // , System.Int32 Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedInt = + // ) + // | new Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest + // ( System.Boolean Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedBool = False + // , System.String Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedString = + // , System.Int32 Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedInt = + // ) + // ] + MultiConstructorTest obj = null; + try + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindNamedParameter<MultiConstructorTest.NamedBool, bool>(GenericType<MultiConstructorTest.NamedBool>.Class, "true"); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + obj = i.GetInstance<MultiConstructorTest>(); + } + catch (InjectionException) + { + } + Assert.IsNull(obj); + } + + [TestMethod] + public void TestOnlyIntParameter() + { + //Multiple infeasible plans: Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest + //, Org.Apache.Reef.Tang.Test + //, Version=1.0.0.0 + //, Culture=neutral + //, PublicKeyToken=null: + // [ Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest = new Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest + // ( System.Boolean Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedBool = + // , System.Int32 Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedInt = 8 + // ) + // | new Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest + // ( System.String Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedString = + // , System.Int32 Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedInt = 8 + // ) + // | new Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest + // ( System.Boolean Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedBool = + // , System.String Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedString = + // , System.Int32 Org.Apache.Reef.Tang.Test.Injection.MultiConstructorTest+NamedInt = 8 + // ) + // ] + MultiConstructorTest obj = null; + try + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindNamedParameter<MultiConstructorTest.NamedInt, int>(GenericType<MultiConstructorTest.NamedInt>.Class, "8"); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + obj = i.GetInstance<MultiConstructorTest>(); + } + catch (InjectionException) + { + } + Assert.IsNull(obj); + } + + [TestMethod] + public void TestMultipleConstructor() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindNamedParameter<MultiConstructorTest.NamedString, string>(GenericType<MultiConstructorTest.NamedString>.Class, "foo"); + cb.BindNamedParameter<MultiConstructorTest.NamedInt, int>(GenericType<MultiConstructorTest.NamedInt>.Class, "8"); + cb.BindNamedParameter<MultiConstructorTest.NamedBool, bool>(GenericType<MultiConstructorTest.NamedBool>.Class, "true"); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + var o = i.GetInstance<MultiConstructorTest>(); + o.Verify("foo", 8, true); + } + + [TestMethod] + public void TestMultiLayersWithMiddleLayerFirst() + { + TangImpl.Reset(); + ICsConfigurationBuilder cb2 = TangFactory.GetTang().NewConfigurationBuilder(); + cb2.BindImplementation(typeof(IH), typeof(M)); + IInjector i2 = TangFactory.GetTang().NewInjector(cb2.Build()); + var o2 = i2.GetInstance<IH>(); + Assert.IsTrue(o2 is M); + + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindImplementation(typeof(IH), typeof(L)); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + var o = i.GetInstance<IH>(); + Assert.IsTrue(o is L); + } + + [TestMethod] + public void TestMultiLayersWithLowerLayerFirst() + { + TangImpl.Reset(); + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindImplementation(typeof(IH), typeof(L)); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + var o = i.GetInstance<IH>(); + Assert.IsTrue(o is L); + + ICsConfigurationBuilder cb2 = TangFactory.GetTang().NewConfigurationBuilder(); + cb2.BindImplementation(typeof(IH), typeof(M)); + IInjector i2 = TangFactory.GetTang().NewInjector(cb2.Build()); + var o2 = i2.GetInstance<IH>(); + Assert.IsTrue(o2 is M); + } + + [TestMethod] + public void TestMultiLayersWithBindImpl() + { + TangImpl.Reset(); + + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindImplementation(typeof(IH), typeof(L)); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + var o = i.GetInstance<IH>(); + Assert.IsTrue(o is L); + + ICsConfigurationBuilder cb2 = TangFactory.GetTang().NewConfigurationBuilder(); + cb2.BindImplementation(typeof(IH), typeof(M)); + cb2.BindImplementation(typeof(M), typeof(L)); //construcotr of L is explicitly bound + IInjector i2 = TangFactory.GetTang().NewInjector(cb2.Build()); + var o2 = i2.GetInstance<IH>(); + Assert.IsTrue(o2 is L); + } + + [TestMethod] + public void TestMultiLayersWithNoInjectableDefaultConstructor() + { + TangImpl.Reset(); + + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindImplementation(typeof(IH), typeof(L1)); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + var o = i.GetInstance<IH>(); + Assert.IsNotNull(o); + + ICsConfigurationBuilder cb2 = TangFactory.GetTang().NewConfigurationBuilder(); + cb2.BindImplementation(typeof(IH), typeof(M1)); //M1 doesn't have injectable default constructor, no implementation L1 is bound to M1 + IInjector i2 = TangFactory.GetTang().NewInjector(cb2.Build()); + string msg = null; + try + { + var o2 = i2.GetInstance<IH>(); + Assert.IsTrue(o2 is L1); + } + catch (Exception e) + { + //Cannot inject Org.Apache.Reef.Tang.Test.Injection.IH, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: + //No known implementations / injectable constructors for Org.Apache.Reef.Tang.Test.Injection.M1, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + msg = e.Message; + } + Assert.IsNotNull(msg); + } + } + + public class MultiConstructorTest + { + private readonly string str; + private readonly int iVal; + private readonly bool bVal; + + [Inject] + public MultiConstructorTest([Parameter(typeof(NamedBool))] bool b, [Parameter(typeof(NamedInt))] int i) + { + this.bVal = b; + this.iVal = i; + this.bVal = false; + } + + [Inject] + public MultiConstructorTest([Parameter(typeof(NamedString))] string s, [Parameter(typeof(NamedInt))] int i) + { + this.str = s; + this.iVal = i; + this.bVal = false; + } + + [Inject] + public MultiConstructorTest([Parameter(typeof(NamedBool))] bool b, [Parameter(typeof(NamedString))] string s, [Parameter(typeof(NamedInt))] int i) + { + this.str = s; + this.iVal = i; + this.bVal = b; + } + + public void Verify(string s, int i, bool b) + { + Assert.AreEqual(str, s); + Assert.AreEqual(iVal, i); + Assert.AreEqual(bVal, b); + } + + [NamedParameter] + public class NamedString : Name<string> + { + } + + [NamedParameter] + public class NamedInt : Name<int> + { + } + + [NamedParameter] + public class NamedBool : Name<bool> + { + } + } + + class M : IH + { + [Inject] + public M() + { + } + } + + class L : M + { + [Inject] + public L() + { + } + } + + class M1 : IH + { + public M1() + { + } + } + + class L1 : M1 + { + [Inject] + public L1() + { + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/Injection/TestNamedParameter.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/Injection/TestNamedParameter.cs b/lang/cs/Tests/TangTests/Injection/TestNamedParameter.cs new file mode 100644 index 0000000..9bcfd40 --- /dev/null +++ b/lang/cs/Tests/TangTests/Injection/TestNamedParameter.cs @@ -0,0 +1,357 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +using System; +using Org.Apache.Reef.Tang.Annotations; +using Org.Apache.Reef.Tang.Implementations; +using Org.Apache.Reef.Tang.Interface; +using Org.Apache.Reef.Tang.Util; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Org.Apache.Reef.Tang.Test.Injection +{ + [TestClass] + public class TestNamedParameter + { + [TestMethod] + public void TestOptionalParameter() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindNamedParameter<StringTest.NamedString, string>(GenericType<StringTest.NamedString>.Class, "foo"); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + var o = i.GetInstance<StringTest>(); + o.Verify("foo"); + } + + [TestMethod] + public void TestOptionalParameterWithDefault() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + var o = i.GetInstance<StringTest>(); + o.Verify(" "); + } + + [TestMethod] + public void TestBoolParameter() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindNamedParameter<BooleanTest.NamedBool, bool>(GenericType<BooleanTest.NamedBool>.Class, "true"); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + var o = i.GetInstance<BooleanTest>(); + o.Verify(true); + } + + [TestMethod] + public void TestBoolUpperCaseParameter() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindNamedParameter<BooleanTest.NamedBool, bool>(GenericType<BooleanTest.NamedBool>.Class, "True"); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + var o = i.GetInstance<BooleanTest>(); + o.Verify(true); + } + + [TestMethod] + public void TestBoolParameterWithDefault() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + var o = i.GetInstance<BooleanTest>(); + o.Verify(false); + } + + [TestMethod] + public void TestByteParameter() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindNamedParameter<ByteTest.NamedByte, byte>(GenericType<ByteTest.NamedByte>.Class, "6"); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + var o = i.GetInstance<ByteTest>(); + o.Verify(6); + } + + [TestMethod] + public void TestByteArrayParameter() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + string input = "abcde"; + cb.BindNamedParameter<ByteArrayTest.NamedByteArray, byte[]>(GenericType<ByteArrayTest.NamedByteArray>.Class, input); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + var o = i.GetInstance<ByteArrayTest>(); + + byte[] bytes = new byte[input.Length * sizeof(char)]; + System.Buffer.BlockCopy(input.ToCharArray(), 0, bytes, 0, bytes.Length); + Assert.IsTrue(o.Verify(bytes)); + } + + [TestMethod] + public void TestCharParameter() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindNamedParameter<CharTest.NamedChar, char>(GenericType<CharTest.NamedChar>.Class, "C"); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + var o = i.GetInstance<CharTest>(); + o.Verify('C'); + } + + [TestMethod] + public void TestShortParameter() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindNamedParameter<Int16Test.NamedShort, short>(GenericType<Int16Test.NamedShort>.Class, "8"); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + var o = i.GetInstance<Int16Test>(); + o.Verify(8); + } + + [TestMethod] + public void TestIntParameter() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindNamedParameter<Int32Test.NamedInt, int>(GenericType<Int32Test.NamedInt>.Class, "8"); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + var o = i.GetInstance<Int32Test>(); + o.Verify(8); + } + + [TestMethod] + public void TestLongParameter() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindNamedParameter<Int64Test.NamedLong, long>(GenericType<Int64Test.NamedLong>.Class, "8777"); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + var o = i.GetInstance<Int64Test>(); + o.Verify(8777); + } + + [TestMethod] + public void TestFloatParameter() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindNamedParameter<FloatTest.NamedSingle, float>(GenericType<FloatTest.NamedSingle>.Class, "3.5"); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + var o = i.GetInstance<FloatTest>(); + float x = 3.5F; + o.Verify(x); + } + } + + public class StringTest + { + private readonly string str; + + [Inject] + public StringTest([Parameter(typeof(NamedString))] string s) + { + this.str = s; + } + + public void Verify(string s) + { + Assert.AreEqual(s, str); + } + + [NamedParameter(DefaultValue = " ")] + public class NamedString : Name<string> + { + } + } + + public class CharTest + { + private readonly char c; + + [Inject] + public CharTest([Parameter(typeof(NamedChar))] char c) + { + this.c = c; + } + + public void Verify(char s) + { + Assert.AreEqual(s, c); + } + + [NamedParameter(DefaultValue = " ")] + public class NamedChar : Name<char> + { + } + } + + public class ByteTest + { + private readonly byte b; + + [Inject] + public ByteTest([Parameter(typeof(NamedByte))] byte b) + { + this.b = b; + } + + public void Verify(byte v) + { + Assert.AreEqual(v, b); + } + + [NamedParameter(DefaultValue = "7")] + public class NamedByte : Name<byte> + { + } + } + + public class BooleanTest + { + private readonly bool b; + + [Inject] + public BooleanTest([Parameter(typeof(NamedBool))] bool b) + { + this.b = b; + } + + public void Verify(bool v) + { + Assert.AreEqual(v, b); + } + + [NamedParameter(DefaultValue = "false")] + public class NamedBool : Name<bool> + { + } + } + + public class ByteArrayTest + { + private readonly byte[] b; + + [Inject] + public ByteArrayTest([Parameter(typeof(NamedByteArray))] byte[] b) + { + this.b = b; + } + + public bool Verify(byte[] v) + { + if (v.Length != b.Length) + { + return false; + } + + for (int i = 0; i < v.Length; i++) + { + if (v[i] != b[i]) + { + return false; + } + } + + return true; + } + + [NamedParameter] + public class NamedByteArray : Name<byte[]> + { + } + } + + public class Int16Test + { + private readonly short s; + + [Inject] + public Int16Test([Parameter(typeof(NamedShort))] short s) + { + this.s = s; + } + + public void Verify(short v) + { + Assert.AreEqual(v, s); + } + + [NamedParameter(DefaultValue = "3")] + public class NamedShort : Name<short> + { + } + } + + public class Int32Test + { + private readonly int i; + + [Inject] + public Int32Test([Parameter(typeof(NamedInt))] int i) + { + this.i = i; + } + + public void Verify(int v) + { + Assert.AreEqual(v, i); + } + + [NamedParameter(DefaultValue = "3")] + public class NamedInt : Name<int> + { + } + } + + public class Int64Test + { + private readonly long l; + + [Inject] + public Int64Test([Parameter(typeof(NamedLong))] long l) + { + this.l = l; + } + + public void Verify(int v) + { + Assert.AreEqual(v, l); + } + + [NamedParameter(DefaultValue = "34567")] + public class NamedLong : Name<long> + { + } + } + + public class FloatTest + { + private readonly float f; + + [Inject] + public FloatTest([Parameter(typeof(NamedSingle))] float f) + { + this.f = f; + } + + public void Verify(float v) + { + Assert.AreEqual(v, f); + } + + [NamedParameter(DefaultValue = "12.5")] + public class NamedSingle : Name<float> + { + } + } +} \ No newline at end of file
