http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang.Tests/Format/TestConfigurationModuleForList.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang.Tests/Format/TestConfigurationModuleForList.cs b/lang/cs/Org.Apache.REEF.Tang.Tests/Format/TestConfigurationModuleForList.cs new file mode 100644 index 0000000..94e3f03 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang.Tests/Format/TestConfigurationModuleForList.cs @@ -0,0 +1,146 @@ +/** + * 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 Org.Apache.REEF.Tang.Annotations; +using Org.Apache.REEF.Tang.Formats; +using Org.Apache.REEF.Tang.Implementations; +using Org.Apache.REEF.Tang.Interface; +using Org.Apache.REEF.Tang.Util; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Org.Apache.REEF.Tang.Implementations.Tang; + +namespace Org.Apache.REEF.Tang.Tests.Format +{ + interface IListSuper + { + } + + [TestClass] + public class TestConfigurationModuleForList + { + //ConfigurationModuleBuilder BindList<U, T>(GenericType<U> iface, IParam<IList<T>> opt) + //public ConfigurationModule Set<T>(IImpl<IList<T>> opt, IList<string> impl) + [TestMethod] + public void ListParamTest() + { + IList<string> v = new List<string>(); + v.Add("a"); + v.Add("b"); + + IConfiguration c = ListConfigurationModule.CONF + .Set(ListConfigurationModule.P, v) + .Build(); + + IList<string> s = (IList<string>)TangFactory.GetTang().NewInjector(c).GetNamedInstance(typeof(ListName)); + Assert.AreEqual(s.Count, 2); + Assert.IsTrue(s.Contains("a")); + Assert.IsTrue(s.Contains("b")); + } + + // public ConfigurationModuleBuilder BindList<U, T>(GenericType<U> iface, IImpl<IList<T>> opt) where U : Name<IList<T>> + // public ConfigurationModule Set<T>(IImpl<IList<T>> opt, IList<Type> impl) + [TestMethod] + public void ListImplTest() + { + IList<Type> v = new List<Type>(); + v.Add(typeof(ListSubA)); + v.Add(typeof(ListSubB)); + + IConfiguration c = ListClassConfigurationModule.CONF + .Set(ListClassConfigurationModule.P, v) + .Build(); + + IList<IListSuper> s = (IList<IListSuper>)TangFactory.GetTang().NewInjector(c).GetNamedInstance(typeof(ListClass)); + Assert.AreEqual(s.Count, 2); + Assert.IsTrue(s[0] is ListSubA); + Assert.IsTrue(s[1] is ListSubB); + } + + //public ConfigurationModuleBuilder BindList<U, T>(GenericType<U> iface, IList<string> impl) + [TestMethod] + public void ListStringTest() + { + IConfiguration c = ListIntConfigurationModule.CONF + .Build(); + + IList<int> i = (IList<int>)TangFactory.GetTang().NewInjector(c).GetNamedInstance(typeof(ListIntName)); + Assert.AreEqual(i.Count, 2); + Assert.IsTrue(i.Contains(1)); + Assert.IsTrue(i.Contains(2)); + } + } + + [NamedParameter] + class ListName : Name<IList<string>> + { + } + + class ListConfigurationModule : ConfigurationModuleBuilder + { + public static readonly RequiredParameter<IList<string>> P = new RequiredParameter<IList<string>>(); + + public static readonly ConfigurationModule CONF = new ListConfigurationModule() + .BindList(GenericType<ListName>.Class, ListConfigurationModule.P) + .Build(); + } + + [NamedParameter] + class ListClass : Name<IList<IListSuper>> + { + } + + class ListClassConfigurationModule : ConfigurationModuleBuilder + { + public static readonly RequiredImpl<IList<IListSuper>> P = new RequiredImpl<IList<IListSuper>>(); + + public static readonly ConfigurationModule CONF = new ListClassConfigurationModule() + .BindList(GenericType<ListClass>.Class, ListClassConfigurationModule.P) + .Build(); + } + + class ListSubA : IListSuper + { + [Inject] + public ListSubA() + { + } + } + + class ListSubB : IListSuper + { + [Inject] + public ListSubB() + { + } + } + + [NamedParameter] + class ListIntName : Name<IList<int>> + { + } + + class ListIntConfigurationModule : ConfigurationModuleBuilder + { + public static readonly ConfigurationModule CONF = new ListIntConfigurationModule() + .BindList<ListIntName, int>(GenericType<ListIntName>.Class, (new List<string>(new string[] { "1", "2" }))) + .Build(); + } +}
http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang.Tests/Format/TestTaskConfiguration.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang.Tests/Format/TestTaskConfiguration.cs b/lang/cs/Org.Apache.REEF.Tang.Tests/Format/TestTaskConfiguration.cs new file mode 100644 index 0000000..35368d7 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang.Tests/Format/TestTaskConfiguration.cs @@ -0,0 +1,390 @@ +/** + * 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 Org.Apache.REEF.Tasks; +using Org.Apache.REEF.Tang.Annotations; +using Org.Apache.REEF.Tang.Formats; +using Org.Apache.REEF.Tang.Implementations; +using Org.Apache.REEF.Tang.Interface; +using Org.Apache.REEF.Tang.Util; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Org.Apache.REEF.Tang.Implementations.Tang; + +namespace Org.Apache.REEF.Tang.Tests.Format +{ + public interface ISuspendEvent + { + string GetId(); + } + + public interface IDriverMessage + { + string GetId(); + } + + public interface ITaskMessageSource + { + string GetId(); + } + + public interface ITaskStop + { + string GetId(); + } + + public interface IEventHandler<T> + { + void OnNext(T t); + } + + public interface ITaskStart + { + string GetId(); + } + + public interface ICloseEvent + { + string GetId(); + } + + [TestClass] + public class TestTaskConfiguration + { + [TestMethod] + public void TaskConfigurationTestWith3Parameters() + { + TaskConfigurationWith3Parameters.Conf + .Set(TaskConfigurationWith3Parameters.ONCLOSE, GenericType<TaskCloseHandler>.Class) + .Build(); + } + + [TestMethod] + public void TaskConfigurationWithMyEventHandlerTest() + { + TaskConfigurationWithMyEventHandler.Conf + .Set(TaskConfigurationWithMyEventHandler.ONCLOSE2, GenericType<MyTaskCloseHandler>.Class) + .Build(); + } + + [TestMethod] + public void TaskConfigurationTest() + { + IConfiguration conf1 = TaskConfiguration.Conf + .Set(TaskConfiguration.IDENTIFIER, "sample task") + .Set(TaskConfiguration.TASK, GenericType<HelloTask>.Class) + .Set(TaskConfiguration.ONCLOSE, GenericType<TaskCloseHandler>.Class) + .Set(TaskConfiguration.MEMENTO, "Test") + .Set(TaskConfiguration.ONSUSPEND, GenericType<SuspendHandler>.Class) + .Set(TaskConfiguration.ONMESSAGE, + GenericType<DriverMessageHandler>.Class) + .Set(TaskConfiguration.ONSENDMESSAGE, GenericType<TaskMsg>.Class) + .Set(TaskConfiguration.ONTASKSTARTED, + GenericType<TaskStartHandler>.Class) + .Set(TaskConfiguration.ONTASKSTOP, + GenericType<TaskStopHandler>.Class) + .Build(); + + IInjector injector1 = TangFactory.GetTang().NewInjector(conf1); + var task1 = (Org.Apache.REEF.Tasks.HelloTask)injector1.GetInstance(typeof(ITask)); + Assert.IsNotNull(task1); + + var serializer = new AvroConfigurationSerializer(); + byte[] bytes = serializer.ToByteArray(conf1); + IConfiguration conf2 = serializer.FromByteArray(bytes); + + IInjector injector2 = TangFactory.GetTang().NewInjector(conf2); + var task2 = (Org.Apache.REEF.Tasks.HelloTask)injector2.GetInstance(typeof(ITask)); + Assert.IsNotNull(task2); + } + + [TestMethod] + public void TaskConfigurationSerializationTest() + { + IConfiguration conf1 = TaskConfiguration.Conf + .Set(TaskConfiguration.IDENTIFIER, "sample task") + .Set(TaskConfiguration.TASK, GenericType<HelloTask>.Class) + .Set(TaskConfiguration.ONCLOSE, GenericType<TaskCloseHandler>.Class) + .Set(TaskConfiguration.MEMENTO, "Test") + .Set(TaskConfiguration.ONSUSPEND, GenericType<SuspendHandler>.Class) + .Set(TaskConfiguration.ONMESSAGE, GenericType<DriverMessageHandler>.Class) + .Set(TaskConfiguration.ONSENDMESSAGE, GenericType<TaskMsg>.Class) + .Set(TaskConfiguration.ONTASKSTARTED, GenericType<TaskStartHandler>.Class) + .Set(TaskConfiguration.ONTASKSTOP, GenericType<TaskStopHandler>.Class) + .Build(); + + IInjector injector1 = TangFactory.GetTang().NewInjector(conf1); + var task1 = (Org.Apache.REEF.Tasks.HelloTask)injector1.GetInstance(typeof(ITask)); + Assert.IsNotNull(task1); + + var serializer = new AvroConfigurationSerializer(); + byte[] bytes = serializer.ToByteArray(conf1); + IConfiguration conf2 = serializer.FromByteArray(bytes); + + IInjector injector2 = TangFactory.GetTang().NewInjector(conf2); + var task2 = (Org.Apache.REEF.Tasks.HelloTask)injector2.GetInstance(typeof(ITask)); + Assert.IsNotNull(task2); + + serializer.ToFileStream(conf1, "TaskConfiguration.bin"); + IConfiguration conf3 = serializer.FromFileStream("TaskConfiguration.bin"); + + IInjector injector3 = TangFactory.GetTang().NewInjector(conf3); + var task3 = (Org.Apache.REEF.Tasks.HelloTask)injector3.GetInstance(typeof(ITask)); + Assert.IsNotNull(task3); + } + } + + public class TaskConfigurationWith3Parameters : ConfigurationModuleBuilder + { + public static readonly OptionalImpl<IEventHandler<ICloseEvent>> ONCLOSE = new OptionalImpl<IEventHandler<ICloseEvent>>(); + + public static ConfigurationModule Conf + { + get + { + return new TaskConfigurationWith3Parameters() + .BindNamedParameter<TaskConfigurationOptions.CloseHandler, IEventHandler<ICloseEvent>, IEventHandler<ICloseEvent>>(GenericType<TaskConfigurationOptions.CloseHandler>.Class, ONCLOSE) + .Build(); + } + } + } + + public class TaskConfiguration : ConfigurationModuleBuilder + { + public static readonly OptionalImpl<IEventHandler<ICloseEvent>> ONCLOSE = new OptionalImpl<IEventHandler<ICloseEvent>>(); + public static readonly RequiredParameter<string> IDENTIFIER = new RequiredParameter<string>(); + public static readonly RequiredImpl<ITask> TASK = new RequiredImpl<ITask>(); + public static readonly OptionalImpl<IEventHandler<ISuspendEvent>> ONSUSPEND = new OptionalImpl<IEventHandler<ISuspendEvent>>(); + public static readonly OptionalImpl<IEventHandler<IDriverMessage>> ONMESSAGE = new OptionalImpl<IEventHandler<IDriverMessage>>(); + public static readonly OptionalParameter<string> MEMENTO = new OptionalParameter<string>(); + public static readonly OptionalImpl<ITaskMessageSource> ONSENDMESSAGE = new OptionalImpl<ITaskMessageSource>(); + public static readonly OptionalImpl<IEventHandler<ITaskStart>> ONTASKSTARTED = new OptionalImpl<IEventHandler<ITaskStart>>(); + public static readonly OptionalImpl<IEventHandler<ITaskStop>> ONTASKSTOP = new OptionalImpl<IEventHandler<ITaskStop>>(); + + public static ConfigurationModule Conf + { + get + { + return new TaskConfiguration() + .BindNamedParameter(GenericType<TaskConfigurationOptions.Identifier>.Class, IDENTIFIER) + .BindImplementation(GenericType<ITask>.Class, TASK) + .BindNamedParameter(GenericType<TaskConfigurationOptions.Memento>.Class, MEMENTO) + .BindNamedParameter(GenericType<TaskConfigurationOptions.CloseHandler>.Class, ONCLOSE) + .BindNamedParameter(GenericType<TaskConfigurationOptions.SuspendHandler>.Class, ONSUSPEND) + .BindNamedParameter(GenericType<TaskConfigurationOptions.MessageHandler>.Class, ONMESSAGE) + .BindSetEntry(GenericType<TaskConfigurationOptions.TaskMessageSources>.Class, ONSENDMESSAGE) + .BindSetEntry(GenericType<TaskConfigurationOptions.StartHandlers>.Class, ONTASKSTARTED) + .BindSetEntry(GenericType<TaskConfigurationOptions.StopHandlers>.Class, ONTASKSTOP) + .Build(); + } + } + } + + public class TaskConfigurationWithMyEventHandler : ConfigurationModuleBuilder + { + public static readonly OptionalImpl<MyEventHandler<ICloseEvent>> ONCLOSE2 = new OptionalImpl<MyEventHandler<ICloseEvent>>(); + + public static ConfigurationModule Conf + { + get + { + return new TaskConfigurationWithMyEventHandler() + .BindNamedParameter<MyTaskConfigurationOptions.MyCloseHandler, MyEventHandler<ICloseEvent>, IEventHandler<ICloseEvent>>(GenericType<MyTaskConfigurationOptions.MyCloseHandler>.Class, ONCLOSE2) + .Build(); + } + } + } + + public class MyEventHandler<T> : IEventHandler<T> + { + public void OnNext(T t) + { + } + } + + public class TaskConfigurationOptions + { + [NamedParameter(DefaultValue = "Unnamed Task", Documentation = "The Identifier of the Task")] + public class Identifier : Name<string> + { + } + + [NamedParameter(Documentation = "The event handler that receives the close event", DefaultClass = typeof(DefaultCloseHandler))] + public class CloseHandler : Name<IEventHandler<ICloseEvent>> + { + } + + [NamedParameter(Documentation = "The memento to be used for the Task.")] + public class Memento : Name<string> + { + } + + [NamedParameter(Documentation = "The event handler that receives the suspend event", DefaultClass = typeof(DefaultSuspendHandler))] + public class SuspendHandler : Name<IEventHandler<ISuspendEvent>> + { + } + + [NamedParameter(Documentation = "The event handler that receives messages from the driver", DefaultClass = typeof(DefaultDriverMessageHandler))] + public class MessageHandler : Name<IEventHandler<IDriverMessage>> + { + } + + [NamedParameter(Documentation = "TaskMessageSource instances.")] + public class TaskMessageSources : Name<ISet<ITaskMessageSource>> + { + } + + [NamedParameter(Documentation = "The set of event handlers for the TaskStart event.")] + public class StartHandlers : Name<ISet<IEventHandler<ITaskStart>>> + { + } + + [NamedParameter(Documentation = "The set of event handlers for the TaskStop event.")] + public class StopHandlers : Name<ISet<IEventHandler<ITaskStop>>> + { + } + } + + public class MyTaskConfigurationOptions + { + [NamedParameter(Documentation = "The event handler that receives the close event", DefaultClass = typeof(MyDefaultCloseHandler))] + public class MyCloseHandler : Name<IEventHandler<ICloseEvent>> + { + } + } + + public class DefaultCloseHandler : IEventHandler<ICloseEvent> + { + [Inject] + public DefaultCloseHandler() + { + } + + public void OnNext(ICloseEvent closeEvent) + { + } + } + + public class MyDefaultCloseHandler : MyEventHandler<ICloseEvent> + { + [Inject] + public MyDefaultCloseHandler() + { + } + } + + public class TaskCloseHandler : IEventHandler<ICloseEvent> + { + [Inject] + public TaskCloseHandler() + { + } + + public void OnNext(ICloseEvent closeEvent) + { + } + } + + public class MyTaskCloseHandler : MyEventHandler<ICloseEvent> + { + [Inject] + public MyTaskCloseHandler() + { + } + } + + public class DefaultSuspendHandler : IEventHandler<ISuspendEvent> + { + [Inject] + public DefaultSuspendHandler() + { + } + + public void OnNext(ISuspendEvent suspendEvent) + { + throw new Exception("No handler for SuspendEvent registered. event: " + suspendEvent); + } + } + + public class SuspendHandler : IEventHandler<ISuspendEvent> + { + public void OnNext(ISuspendEvent suspendEvent) + { + } + } + + public class DefaultDriverMessageHandler : IEventHandler<IDriverMessage> + { + [Inject] + public DefaultDriverMessageHandler() + { + } + + public void OnNext(IDriverMessage driverMessage) + { + throw new Exception("No DriverMessage handler bound. Message received:" + driverMessage); + } + } + + public class DriverMessageHandler : IEventHandler<IDriverMessage> + { + public void OnNext(IDriverMessage driverMessage) + { + } + } + + public class TaskStartHandler : IEventHandler<ITaskStart> + { + public void OnNext(ITaskStart t) + { + throw new NotImplementedException(); + } + } + + public class TaskStopHandler : IEventHandler<ITaskStop> + { + public void OnNext(ITaskStop t) + { + throw new NotImplementedException(); + } + } + + public class TaskMsg : ITask, ITaskMessageSource + { + [Inject] + public TaskMsg() + { + } + + public byte[] Call(byte[] memento) + { + throw new NotImplementedException(); + } + + public string GetId() + { + throw new NotImplementedException(); + } + + public void Dispose() + { + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestAmbigousConstructors.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestAmbigousConstructors.cs b/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestAmbigousConstructors.cs new file mode 100644 index 0000000..57ac1ea --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestAmbigousConstructors.cs @@ -0,0 +1,81 @@ +/** + * 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; +using Org.Apache.REEF.Tang.Implementations.Tang; + +namespace Org.Apache.REEF.Tang.Tests.Injection +{ + [TestClass] + public class TestAmbigousConstructors + { + [TestMethod] + public void AmbigousConstructorTest() + { + //Cannot inject Org.Apache.REEF.Tang.Tests.Injection.AmbigousConstructorClass, Org.Apache.REEF.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + //Ambiguous subplan Org.Apache.REEF.Tang.Tests.Injection.AmbigousConstructorClass, Org.Apache.REEF.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + // new Org.Apache.REEF.Tang.Tests.Injection.AmbigousConstructorClass(System.String Org.Apache.REEF.Tang.Tests.Injection.AmbigousConstructorClass+NamedString = foo, System.Int32 Org.Apache.REEF.Tang.Tests.Injection.AmbigousConstructorClass+NamedInt = 8) + // new Org.Apache.REEF.Tang.Tests.Injection.AmbigousConstructorClass(System.Int32 Org.Apache.REEF.Tang.Tests.Injection.AmbigousConstructorClass+NamedInt = 8, System.String Org.Apache.REEF.Tang.Tests.Injection.AmbigousConstructorClass+NamedString = foo) + //] + AmbigousConstructorClass obj = null; + try + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindNamedParameter<AmbigousConstructorClass.NamedString, string>(GenericType<AmbigousConstructorClass.NamedString>.Class, "foo"); + cb.BindNamedParameter<AmbigousConstructorClass.NamedInt, int>(GenericType<AmbigousConstructorClass.NamedInt>.Class, "8"); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + obj = i.GetInstance<AmbigousConstructorClass>(); + } + catch (InjectionException e) + { + System.Diagnostics.Debug.WriteLine(e); + } + Assert.IsNull(obj); + } + } + + class AmbigousConstructorClass + { + [Inject] + public AmbigousConstructorClass([Parameter(typeof(NamedString))] string s, [Parameter(typeof(NamedInt))] int i) + { + } + + [Inject] + public AmbigousConstructorClass([Parameter(typeof(NamedInt))] int i, [Parameter(typeof(NamedString))] string s) + { + } + + [NamedParameter] + public class NamedString : Name<string> + { + } + + [NamedParameter] + public class NamedInt : Name<int> + { + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestForkInjection.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestForkInjection.cs b/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestForkInjection.cs new file mode 100644 index 0000000..485ca23 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestForkInjection.cs @@ -0,0 +1,80 @@ +/** + * 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 Microsoft.VisualStudio.TestTools.UnitTesting; +using Org.Apache.REEF.Tang.Examples; +using Org.Apache.REEF.Tang.Implementations; +using Org.Apache.REEF.Tang.Implementations.Tang; +using Org.Apache.REEF.Tang.Interface; +using Org.Apache.REEF.Tang.Util; +using System; +using System.Reflection; + +namespace Org.Apache.REEF.Tang.Tests.Injection +{ + [TestClass] + public class TestForkInjection + { + static Assembly asm = null; + + [ClassInitialize] + public static void ClassSetup(TestContext context) + { + asm = Assembly.Load(FileNames.Examples); + } + + [ClassCleanup] + public static void ClassCleanup() + { + } + + [TestInitialize()] + public void TestSetup() + { + } + + [TestCleanup()] + public void TestCleanup() + { + } + + [TestMethod] + public void TestForksInjectorInConstructor() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(new string[] { FileNames.Examples }); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + var o = i.GetInstance(typeof(ForksInjectorInConstructor)); + } + + [TestMethod] + public void TestForkWorks() + { + Type checkChildIfaceType = typeof(CheckChildIface); + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(new string[] { FileNames.Examples }); + cb.BindImplementation(GenericType<CheckChildIface>.Class, GenericType<CheckChildImpl>.Class); + + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + IInjector i1 = i.ForkInjector(); + CheckChildIface c1 = (CheckChildIface)i1.GetInstance(checkChildIfaceType); + IInjector i2 = i.ForkInjector(); + CheckChildIface c2 = (CheckChildIface)i2.GetInstance(checkChildIfaceType); + Assert.AreNotEqual(c1, c2); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestInjection.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestInjection.cs b/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestInjection.cs new file mode 100644 index 0000000..1f015ca --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestInjection.cs @@ -0,0 +1,388 @@ +/** + * 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 Microsoft.VisualStudio.TestTools.UnitTesting; +using Org.Apache.REEF.Tang.Annotations; +using Org.Apache.REEF.Tang.Examples; +using Org.Apache.REEF.Tang.Implementations; +using Org.Apache.REEF.Tang.Implementations.ClassHierarchy; +using Org.Apache.REEF.Tang.Implementations.Tang; +using Org.Apache.REEF.Tang.Interface; +using Org.Apache.REEF.Tang.Util; +using Org.Apache.REEF.Tasks; +using System; +using System.Reflection; + +namespace Org.Apache.REEF.Tang.Tests.Injection +{ + [DefaultImplementation(typeof(AReferenceClass))] + internal interface IAInterface + { + } + + [TestClass] + public class TestInjection + { + static Assembly asm = null; + + [ClassInitialize] + public static void ClassSetup(TestContext context) + { + asm = Assembly.Load(FileNames.Examples); + } + + [ClassCleanup] + public static void ClassCleanup() + { + } + + [TestInitialize()] + public void TestSetup() + { + } + + [TestCleanup()] + public void TestCleanup() + { + } + + [TestMethod] + public void TestTimer() + { + Type timerType = typeof(Timer); + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Examples }); + cb.BindNamedParameter<Timer.Seconds, int>(GenericType<Timer.Seconds>.Class, "2"); + IConfiguration conf = cb.Build(); + IInjector injector = tang.NewInjector(conf); + var timer = (Timer)injector.GetInstance(timerType); + + Assert.IsNotNull(timer); + + timer.sleep(); + } + + [TestMethod] + public void TestTimerWithClassHierarchy() + { + Type timerType = typeof(Timer); + + ClassHierarchyImpl classHierarchyImpl = new ClassHierarchyImpl(FileNames.Examples); + + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder((ICsClassHierarchy)classHierarchyImpl); + + cb.BindNamedParameter<Timer.Seconds, int>(GenericType<Timer.Seconds>.Class, "2"); + IConfiguration conf = cb.Build(); + + IInjector injector = tang.NewInjector(conf); + var timer = (Timer)injector.GetInstance(timerType); + + Assert.IsNotNull(timer); + + timer.sleep(); + } + + [TestMethod] + public void TestDocumentLoadNamedParameter() + { + Type documentedLocalNamedParameterType = typeof(DocumentedLocalNamedParameter); + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Examples }); + cb.BindNamedParameter<DocumentedLocalNamedParameter.Foo, string>(GenericType<DocumentedLocalNamedParameter.Foo>.Class, "Hello"); + IConfiguration conf = cb.Build(); + IInjector injector = tang.NewInjector(conf); + var doc = (DocumentedLocalNamedParameter)injector.GetInstance(documentedLocalNamedParameterType); + + Assert.IsNotNull(doc); + } + + [TestMethod] + public void TestDocumentLoadNamedParameterWithDefaultValue() + { + ITang tang = TangFactory.GetTang(); + IConfiguration conf = tang.NewConfigurationBuilder(new string[] { FileNames.Examples }).Build(); + IInjector injector = tang.NewInjector(conf); + var doc = (DocumentedLocalNamedParameter)injector.GetInstance(typeof(DocumentedLocalNamedParameter)); + + Assert.IsNotNull(doc); + } + + [TestMethod] + public void TestSimpleConstructor() + { + Type simpleConstructorType = typeof(SimpleConstructors); + + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Examples }); + IConfiguration conf = cb.Build(); + IInjector injector = tang.NewInjector(conf); + var simpleConstructor = (SimpleConstructors)injector.GetInstance(simpleConstructorType); + Assert.IsNotNull(simpleConstructor); + } + + [TestMethod] + public void TestActivity() + { + Type activityType = typeof(HelloTask); + + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Tasks, FileNames.Common }); + IConfiguration conf = cb.Build(); + IInjector injector = tang.NewInjector(conf); + var activityRef = (ITask)injector.GetInstance(activityType); + Assert.IsNotNull(activityRef); + } + + [TestMethod] + public void TestStreamActivity1() + { + Type activityType = typeof(StreamTask1); + + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Tasks, FileNames.Common }); + IConfiguration conf = cb.Build(); + IInjector injector = tang.NewInjector(conf); + var activityRef = (ITask)injector.GetInstance(activityType); + Assert.IsNotNull(activityRef); + } + + [TestMethod] + public void TestStreamActivity2() + { + Type activityType = typeof(StreamTask2); + + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Tasks, FileNames.Common }); + IConfiguration conf = cb.Build(); + IInjector injector = tang.NewInjector(conf); + var activityRef = (ITask)injector.GetInstance(activityType); + Assert.IsNotNull(activityRef); + } + + [TestMethod] + public void TestMultipleAssemlies() + { + Type activityInterfaceType1 = typeof(ITask); + Type tweeterType = typeof(Tweeter); + + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Examples, FileNames.Tasks, FileNames.Common }); + cb.BindImplementation(GenericType<ITask>.Class, GenericType<HelloTask>.Class); + cb.BindImplementation(GenericType<ITweetFactory>.Class, GenericType<MockTweetFactory>.Class); + cb.BindImplementation(GenericType<ISMS>.Class, GenericType<MockSMS>.Class); + cb.BindNamedParameter<Tweeter.PhoneNumber, long>(GenericType<Tweeter.PhoneNumber>.Class, "8675309"); + + IConfiguration conf = cb.Build(); + IInjector injector = tang.NewInjector(conf); + var activityRef = (ITask)injector.GetInstance(activityInterfaceType1); + var tweeter = (Tweeter)injector.GetInstance(tweeterType); + + Assert.IsNotNull(activityRef); + Assert.IsNotNull(tweeter); + + tweeter.sendMessage(); + } + + [TestMethod] + public void TestActivityWithBinding() + { + Type activityInterfaceType = typeof(ITask); + + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Tasks, FileNames.Common }); + cb.BindImplementation(GenericType<ITask>.Class, GenericType<HelloTask>.Class); + cb.BindNamedParameter<TaskConfigurationOptions.Identifier, string>(GenericType<TaskConfigurationOptions.Identifier>.Class, "Hello Task"); + + IConfiguration conf = cb.Build(); + IInjector injector = tang.NewInjector(conf); + ITask activityRef1 = injector.GetInstance<ITask>(); + var activityRef2 = (ITask)injector.GetInstance(activityInterfaceType); + Assert.IsNotNull(activityRef2); + Assert.IsNotNull(activityRef1); + Assert.AreEqual(activityRef1, activityRef2); + } + + [TestMethod] + public void TestHelloStreamingActivityWithBinding() + { + Type activityInterfaceType = typeof(ITask); + + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Tasks, FileNames.Common }); + + cb.BindImplementation(GenericType<ITask>.Class, GenericType<HelloTask>.Class); + cb.BindNamedParameter<TaskConfigurationOptions.Identifier, string>(GenericType<TaskConfigurationOptions.Identifier>.Class, "Hello Stereamingk"); + cb.BindNamedParameter<StreamTask1.IpAddress, string>(GenericType<StreamTask1.IpAddress>.Class, "127.0.0.0"); + IConfiguration conf = cb.Build(); + IInjector injector = tang.NewInjector(conf); + var activityRef = (ITask)injector.GetInstance(activityInterfaceType); + Assert.IsNotNull(activityRef); + } + + [TestMethod] + public void TestTweetExample() + { + Type tweeterType = typeof(Tweeter); + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Examples }); + + IConfiguration conf = cb.BindImplementation(GenericType<ITweetFactory>.Class, GenericType<MockTweetFactory>.Class) + .BindImplementation(GenericType<ISMS>.Class, GenericType<MockSMS>.Class) + .BindNamedParameter<Tweeter.PhoneNumber, long>(GenericType<Tweeter.PhoneNumber>.Class, "8675309") + .Build(); + IInjector injector = tang.NewInjector(conf); + var tweeter = (Tweeter)injector.GetInstance(tweeterType); + tweeter.sendMessage(); + + var sms = (ISMS)injector.GetInstance(typeof(ISMS)); + var factory = (ITweetFactory)injector.GetInstance(typeof(ITweetFactory)); + Assert.IsNotNull(sms); + Assert.IsNotNull(factory); + } + + [TestMethod] + public void TestReferenceType() + { + AReferenceClass o = (AReferenceClass)TangFactory.GetTang().NewInjector().GetInstance(typeof(IAInterface)); + } + + [TestMethod] + public void TestGeneric() + { + var o = (AGenericClass<int>)TangFactory.GetTang().NewInjector().GetInstance(typeof(AGenericClass<int>)); + var o2 = (AClassWithGenericArgument<int>)TangFactory.GetTang().NewInjector().GetInstance(typeof(AClassWithGenericArgument<int>)); + Assert.IsNotNull(o); + Assert.IsNotNull(o2); + } + + [TestMethod] + public void TestNestedClass() + { + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(); + + IConfiguration conf = cb + .BindNamedParameter<ClassParameter.Named1, int>(GenericType<ClassParameter.Named1>.Class, "5") + .BindNamedParameter<ClassParameter.Named2, string>(GenericType<ClassParameter.Named2>.Class, "hello") + .Build(); + + IInjector injector = tang.NewInjector(conf); + ClassHasNestedClass h = injector.GetInstance<ClassHasNestedClass>(); + + Assert.IsNotNull(h); + } + + [TestMethod] + public void TestExternalObject() + { + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(); + + IInjector injector = tang.NewInjector(cb.Build()); + + //bind an object to the injetor so that Tang will get this instance from cache directly instead of inject it when injecting ClassWithExternalObject + injector.BindVolatileInstance(GenericType<ExternalClass>.Class, new ExternalClass()); + ClassWithExternalObject o = injector.GetInstance<ClassWithExternalObject>(); + + Assert.IsNotNull(o.ExternalObject is ExternalClass); + } + } + + class AReferenceClass : IAInterface + { + [Inject] + public AReferenceClass(AReferenced refclass) + { + } + } + + class AReferenced + { + [Inject] + public AReferenced() + { + } + } + + class AGenericClass<T> + { + [Inject] + public AGenericClass() + { + } + } + + class AClassWithGenericArgument<T> + { + [Inject] + public AClassWithGenericArgument(AGenericClass<T> g) + { + } + } + + class ClassHasNestedClass + { + [Inject] + public ClassHasNestedClass(ClassParameter h1) + { + } + } + + class ClassParameter + { + private int i; + private string s; + + [Inject] + public ClassParameter([Parameter(typeof(Named1))] int i, [Parameter(typeof(Named2))] string s) + { + this.i = i; + this.s = s; + } + + [NamedParameter] + public class Named1 : Name<int> + { + } + + [NamedParameter] + public class Named2 : Name<string> + { + } + } + + class ClassWithExternalObject + { + [Inject] + public ClassWithExternalObject(ExternalClass ec) + { + ExternalObject = ec; + } + + public ExternalClass ExternalObject { get; set; } + } + + class ExternalClass + { + public ExternalClass() + { + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestInjectionFuture.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestInjectionFuture.cs b/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestInjectionFuture.cs new file mode 100644 index 0000000..d9f81f7 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestInjectionFuture.cs @@ -0,0 +1,242 @@ +/** + * 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 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; +using Org.Apache.REEF.Tang.Implementations.InjectionPlan; +using Org.Apache.REEF.Tang.Implementations.Tang; + +namespace Org.Apache.REEF.Tang.Tests.Injection +{ + interface IAinj + { + } + + [DefaultImplementation(typeof(C), "C")] + interface IBinj : IAinj + { + } + + [TestClass] + public class TestInjectionFuture + { + [TestMethod] + public void TestProactiveFutures() + { + IInjector i = TangFactory.GetTang().NewInjector(); + IsFuture.Instantiated = false; + i.GetInstance(typeof(NeedsFuture)); + Assert.IsTrue(IsFuture.Instantiated); + } + + [TestMethod] + public void testFutures() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + IInjector i2 = TangFactory.GetTang().NewInjector(cb.Build()); + + Futurist f = (Futurist)i.GetInstance(typeof(Futurist)); + Assert.IsTrue(f == f.getMyCar().getDriver()); + Assert.IsTrue(f.getMyCar() == f.getMyCar().getDriver().getMyCar()); + + Futurist f2 = (Futurist)i2.GetInstance(typeof(Futurist)); + Assert.IsTrue(f2 == f2.getMyCar().getDriver()); + Assert.IsTrue(f2.getMyCar() == f2.getMyCar().getDriver().getMyCar()); + + Assert.IsTrue(f != f2.getMyCar().getDriver()); + Assert.IsTrue(f.getMyCar() != f2.getMyCar().getDriver().getMyCar()); + } + + [TestMethod] + public void testFutures2() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + IInjector i2 = i.ForkInjector(new IConfiguration[] { }); + + FlyingCar c = (FlyingCar)i.GetInstance(typeof(FlyingCar)); + Assert.IsTrue(c == c.getDriver().getMyCar()); + Assert.IsTrue(c.getDriver() == c.getDriver().getMyCar().getDriver()); + + FlyingCar c2 = (FlyingCar)i2.GetInstance(typeof(FlyingCar)); + Assert.IsTrue(c2 == c2.getDriver().getMyCar()); + Assert.IsTrue(c2.getDriver() == c2.getDriver().getMyCar().getDriver()); + + Assert.IsTrue(c2 != c.getDriver().getMyCar()); + Assert.IsTrue(c2.getDriver() != c.getDriver().getMyCar().getDriver()); + } + + [TestMethod] + public void TestNamedParameterInjectionFuture() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindImplementation(GenericType<FlyingCar>.Class, GenericType<FlyingCar>.Class); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + PickyFuturist f = (PickyFuturist)i.GetInstance(typeof(PickyFuturist)); + Assert.IsNotNull(f.getMyCar()); + } + + [TestMethod] + public void TestNamedParameterInjectionFutureDefaultImpl() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + PickyFuturist f = (PickyFuturist)i.GetInstance(typeof(PickyFuturist)); + Assert.IsNotNull(f.getMyCar()); + } + + [TestMethod] + public void TestNamedParameterInjectionFutureBindImpl() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindImplementation(GenericType<Futurist>.Class, GenericType<PickyFuturist>.Class); + cb.BindNamedParameter<MyFlyingCar, BigFlyingCar, FlyingCar>(GenericType<MyFlyingCar>.Class, GenericType<BigFlyingCar>.Class); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + PickyFuturist f = (PickyFuturist)i.GetInstance(typeof(PickyFuturist)); + Assert.IsNotNull((BigFlyingCar)f.getMyCar()); + } + + [TestMethod] + public void TestNamedParameterBoundToDelegatingInterface() + { + IInjector i = TangFactory.GetTang().NewInjector(); + C c = (C)i.GetNamedInstance(typeof(AName)); + Assert.IsNotNull(c); + } + + [TestMethod] + public void TestBoundToDelegatingInterface() + { + IInjector i = TangFactory.GetTang().NewInjector(); + C c = (C)i.GetInstance(typeof(IBinj)); + Assert.IsNotNull(c); + } + + [DefaultImplementation(typeof(Futurist), "Futurist")] + public class Futurist + { + private IInjectionFuture<FlyingCar> fcar; + [Inject] + public Futurist(IInjectionFuture<FlyingCar> car) + { + this.fcar = car; + } + + public virtual FlyingCar getMyCar() + { + FlyingCar c = fcar.Get(); + return c; + } + } + + public class PickyFuturist : Futurist + { + private IInjectionFuture<FlyingCar> fCar; + [Inject] + public PickyFuturist([Parameter(typeof(MyFlyingCar))] IInjectionFuture<FlyingCar> myFlyingCar) : base(myFlyingCar) + { + fCar = myFlyingCar; + } + + public override FlyingCar getMyCar() + { + FlyingCar c = fCar.Get(); + return c; + } + } + + [DefaultImplementation(typeof(FlyingCar), "")] + public class FlyingCar + { + private string color; + private Futurist driver; + + [Inject] + public FlyingCar([Parameter(typeof(Color))] string color, Futurist driver) + { + this.color = color; + this.driver = driver; + } + + public string getColor() + { + return color; + } + + public Futurist getDriver() + { + return driver; + } + } + + [NamedParameter(DefaultValue = "blue")] + public class Color : Name<string> + { + } + + public class BigFlyingCar : FlyingCar + { + [Inject] + BigFlyingCar([Parameter(typeof(Color))] string color, Futurist driver) : base(color, driver) + { + } + } + + [NamedParameter(DefaultClass = typeof(FlyingCar))] + public class MyFlyingCar : Name<FlyingCar> + { + } + } + + [NamedParameter(DefaultClass = typeof(IBinj))] + class AName : Name<IAinj> + { + } + + class C : IBinj + { + [Inject] + C() + { + } + } + + class IsFuture + { + [Inject] + IsFuture(NeedsFuture nf) + { + Instantiated = true; + } + + public static bool Instantiated { get; set; } + } + + class NeedsFuture + { + [Inject] + NeedsFuture(IInjectionFuture<IsFuture> isFut) + { + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestListInjection.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestListInjection.cs b/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestListInjection.cs new file mode 100644 index 0000000..2af4b1b --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestListInjection.cs @@ -0,0 +1,566 @@ +/** + * 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; +using Org.Apache.REEF.Tang.Implementations.Tang; + +namespace Org.Apache.REEF.Tang.Tests.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/c1b5200f/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestMissingParameters.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestMissingParameters.cs b/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestMissingParameters.cs new file mode 100644 index 0000000..9c88639 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Tang.Tests/Injection/TestMissingParameters.cs @@ -0,0 +1,152 @@ +/** + * 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; +using Org.Apache.REEF.Tang.Implementations.Tang; + +namespace Org.Apache.REEF.Tang.Tests.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.Tests.Injection.MultiParameterConstructor, Org.Apache.REEF.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + //missing arguments: [ + //Org.Apache.REEF.Tang.Tests.Injection.MultiParameterConstructor+NamedBool, Org.Apache.REEF.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + //Org.Apache.REEF.Tang.Tests.Injection.MultiParameterConstructor+NamedString, Org.Apache.REEF.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + //Org.Apache.REEF.Tang.Tests.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.Tests.Injection.MultiParameterConstructor, Org.Apache.REEF.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: + //Org.Apache.REEF.Tang.Tests.Injection.MultiParameterConstructor, Org.Apache.REEF.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + //missing arguments: [ + //Org.Apache.REEF.Tang.Tests.Injection.MultiParameterConstructor+NamedString, Org.Apache.REEF.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + //Org.Apache.REEF.Tang.Tests.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.Tests.Injection.MultiParameterConstructor, Org.Apache.REEF.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: + //Org.Apache.REEF.Tang.Tests.Injection.MultiParameterConstructor, Org.Apache.REEF.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null + //missing argument Org.Apache.REEF.Tang.Tests.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
