You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@reef.apache.org by we...@apache.org on 2015/01/29 21:42:49 UTC
[05/31] incubator-reef git commit: [REEF-97] Add the REEF.NET code
base
http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/Configuration/TestCsConfigurationBuilderExtension.cs
----------------------------------------------------------------------
diff --git a/lang/cs/Tests/TangTests/Configuration/TestCsConfigurationBuilderExtension.cs b/lang/cs/Tests/TangTests/Configuration/TestCsConfigurationBuilderExtension.cs
new file mode 100644
index 0000000..f8defdf
--- /dev/null
+++ b/lang/cs/Tests/TangTests/Configuration/TestCsConfigurationBuilderExtension.cs
@@ -0,0 +1,178 @@
+/**
+ * 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.Collections.Generic;
+using Org.Apache.Reef.Tang.Implementations;
+using Org.Apache.Reef.Tang.Interface;
+using Org.Apache.Reef.Tang.Util;
+using Org.Apache.Reef.Tang.Test.Injection;
+using Org.Apache.Reef.Tang.Test.Tang;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace Org.Apache.Reef.Tang.Test.Configuration
+{
+ /// <summary>
+ /// This class is to test extension API defined in ICsConfigurationBuilder
+ /// </summary>
+ [TestClass]
+ public class TestCsConfigurationBuilderExtension
+ {
+ [TestMethod]
+ public void TestBindNamedParameter1()
+ {
+ ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();
+ cb.BindNamedParameter<AImplName, Aimpl, INamedImplA>();
+ cb.BindNamedParameter<BImplName, Bimpl, INamedImplA>();
+
+ IInjector i = TangFactory.GetTang().NewInjector(cb.Build());
+ Aimpl a1 = (Aimpl)i.GetNamedInstance<AImplName, INamedImplA>(GenericType<AImplName>.Class);
+ Aimpl a2 = (Aimpl)i.GetNamedInstance<AImplName, INamedImplA>(GenericType<AImplName>.Class);
+ Bimpl b1 = (Bimpl)i.GetNamedInstance<BImplName, INamedImplA>(GenericType<BImplName>.Class);
+ Bimpl b2 = (Bimpl)i.GetNamedInstance<BImplName, INamedImplA>(GenericType<BImplName>.Class);
+ Assert.AreSame(a1, a2);
+ Assert.AreSame(b1, b2);
+ }
+
+ [TestMethod]
+ public void TestBindStringNamedParam()
+ {
+ ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();
+ cb.BindStringNamedParam<StringTest.NamedString>("foo");
+ IInjector i = TangFactory.GetTang().NewInjector(cb.Build());
+ var o = i.GetInstance<StringTest>();
+ o.Verify("foo");
+ }
+
+ [TestMethod]
+ public void TestBindIntNamedParam()
+ {
+ ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();
+ cb.BindIntNamedParam<Int32Test.NamedInt>("8");
+ IInjector i = TangFactory.GetTang().NewInjector(cb.Build());
+ var o = i.GetInstance<Int32Test>();
+ o.Verify(8);
+ }
+
+ [TestMethod]
+ public void TestBindNamedParam()
+ {
+ ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();
+ cb.BindNamedParam<BooleanTest.NamedBool, bool>("true");
+ IInjector i = TangFactory.GetTang().NewInjector(cb.Build());
+ var o = i.GetInstance<BooleanTest>();
+ o.Verify(true);
+ }
+
+ [TestMethod]
+ public void TestBindSetEntryImplValue()
+ {
+ ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();
+ cb.BindSetEntry<TestSetInjection.SetOfClasses, TestSetInjection.Integer1, INumber>() //bind an impl to the interface of the set
+ .BindIntNamedParam<TestSetInjection.Integer1.NamedInt>("4"); //bind parameter for the impl
+
+ IInjector i = TangFactory.GetTang().NewInjector(cb.Build());
+
+ ISet<INumber> actual = i.GetInstance<TestSetInjection.Pool>().Numbers;
+ ISet<INumber> expected = new HashSet<INumber>();
+ expected.Add(new TestSetInjection.Integer1(4));
+
+ Assert.IsTrue(Utilities.Utilities.Equals<INumber>(actual, expected));
+ }
+
+ [TestMethod]
+ public void TestBindSetEntryStringValue()
+ {
+ IConfiguration conf = TangFactory.GetTang().NewConfigurationBuilder()
+ .BindSetEntry<SetOfNumbers, string>("four")
+ .BindSetEntry<SetOfNumbers, string>("five")
+ .BindSetEntry<SetOfNumbers, string>("six")
+ .Build();
+
+ Box b = (Box)TangFactory.GetTang().NewInjector(conf).GetInstance(typeof(Box));
+ ISet<string> actual = b.Numbers;
+
+ Assert.IsTrue(actual.Contains("four"));
+ Assert.IsTrue(actual.Contains("five"));
+ Assert.IsTrue(actual.Contains("six"));
+ }
+
+ [TestMethod]
+ public void TestBindImplementation()
+ {
+ ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();
+ cb.BindImplementation<Interf, Impl>();
+ Interf o = TangFactory.GetTang().NewInjector(cb.Build()).GetInstance<Interf>();
+ Assert.IsTrue(o is Impl);
+ }
+
+ [TestMethod]
+ public void TestBindList()
+ {
+ IList<string> injected = new List<string>();
+ injected.Add("hi");
+ injected.Add("hello");
+ injected.Add("bye");
+
+ ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();
+ cb.BindList<StringList, string>(injected);
+
+ IInjector i = TangFactory.GetTang().NewInjector(cb.Build());
+ IList<string> actual = ((StringClass)i.GetInstance(typeof(StringClass))).StringList;
+
+ Assert.IsTrue(actual.Contains("hi"));
+ Assert.IsTrue(actual.Contains("hello"));
+ Assert.IsTrue(actual.Contains("bye"));
+ Assert.AreEqual(actual.Count, 3);
+ }
+
+ [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.BindIntNamedParam<TestSetInjection.Integer1.NamedInt>("5");
+ cb.BindNamedParam<TestSetInjection.Float1.NamedFloat, float>("12.5");
+ cb.BindList<ListOfClasses, INumber>(injected);
+
+ IInjector i = TangFactory.GetTang().NewInjector(cb.Build());
+
+ IList<INumber> actual = ((PoolListClass)i.GetInstance(typeof(PoolListClass))).Numbers;
+
+ Assert.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)));
+ }
+
+ [TestMethod]
+ public void TestBindConstructor()
+ {
+ ICsConfigurationBuilder b = TangFactory.GetTang().NewConfigurationBuilder();
+ b.BindConstructor<TestExternalConstructors.A, TestExternalConstructors.ACons>();
+ b.BindConstructor<TestExternalConstructors.B, TestExternalConstructors.BCons>();
+
+ TangFactory.GetTang().NewInjector(b.Build()).GetInstance(typeof(TestExternalConstructors.B));
+ TangFactory.GetTang().NewInjector(b.Build()).GetInstance(typeof(TestExternalConstructors.A));
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/Format/TestConfigurationModule.cs
----------------------------------------------------------------------
diff --git a/lang/cs/Tests/TangTests/Format/TestConfigurationModule.cs b/lang/cs/Tests/TangTests/Format/TestConfigurationModule.cs
new file mode 100644
index 0000000..94d3ad2
--- /dev/null
+++ b/lang/cs/Tests/TangTests/Format/TestConfigurationModule.cs
@@ -0,0 +1,522 @@
+/**
+ * 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.Exceptions;
+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;
+
+namespace Org.Apache.Reef.Tang.Test.Format
+{
+ public interface IFoo
+ {
+ int getFooness();
+ }
+
+ interface ISuper
+ {
+ }
+
+ [TestClass]
+ public class TestConfigurationModule
+ {
+ [ClassInitialize]
+ public static void ClassSetup(TestContext context)
+ {
+ }
+
+ [ClassCleanup]
+ public static void ClassCleanup()
+ {
+ }
+
+ [TestInitialize()]
+ public void TestSetup()
+ {
+ }
+
+ [TestCleanup()]
+ public void TestCleanup()
+ {
+ }
+
+ [TestMethod]
+ public void SmokeTest()
+ {
+ // Here we set some configuration values. In true tang style,
+ // you won't be able to set them more than once ConfigurationModule's
+ // implementation is complete.
+ Type fooType = typeof(Org.Apache.Reef.Tang.Test.Format.IFoo);
+
+ IConfiguration c = MyConfigurationModule.Conf
+ .Set(MyConfigurationModule.TheFoo, GenericType<FooImpl>.Class)
+ .Set(MyConfigurationModule.FooNess, "12")
+ .Build();
+ IFoo f = (IFoo)TangFactory.GetTang().NewInjector(c).GetInstance(fooType);
+ Assert.AreEqual(f.getFooness(), 12);
+ }
+
+ [TestMethod]
+ public void SmokeTestConfig()
+ {
+ // Here we set some configuration values. In true tang style,
+ // you won't be able to set them more than once ConfigurationModule's
+ // implementation is complete.
+ Type fooType = typeof(Org.Apache.Reef.Tang.Test.Format.IFoo);
+
+ IConfiguration c = MyConfigurationModule.Conf
+ .Set(MyConfigurationModule.TheFoo, GenericType<FooImpl>.Class)
+ .Set(MyConfigurationModule.FooNess, "12")
+ .Build();
+ IFoo f = (IFoo)TangFactory.GetTang().NewInjector(c).GetInstance(fooType);
+ Assert.AreEqual(f.getFooness(), 12);
+ AvroConfigurationSerializer serializerCs = new AvroConfigurationSerializer();
+
+ serializerCs.ToFileStream(c, "TangTestCs.avroconf");
+ var c3 = serializerCs.FromFileStream("TangTestCs.avroconf");
+ IFoo f3 = (IFoo)TangFactory.GetTang().NewInjector(c3).GetInstance(fooType);
+ Assert.AreEqual(f3.getFooness(), 12);
+
+ serializerCs.ToFile(c, "TangTestCs1.avro");
+ var c4 = serializerCs.FromFile("TangTestCs1.avro");
+ IFoo f4 = (IFoo)TangFactory.GetTang().NewInjector(c4).GetInstance(fooType);
+ Assert.AreEqual(f4.getFooness(), 12);
+
+ IConfigurationSerializer serializerImpl = (IConfigurationSerializer)TangFactory.GetTang().NewInjector().GetInstance(typeof(IConfigurationSerializer));
+ serializerImpl.ToFile(c, "TangTestCs1.avro");
+ var c5 = serializerImpl.FromFile("TangTestCs1.avro");
+ IFoo f5 = (IFoo)TangFactory.GetTang().NewInjector(c5).GetInstance(fooType);
+ Assert.AreEqual(f5.getFooness(), 12);
+
+ //this is to test the file generated from Java. name,value b=must be recognized by C# class hierarchy
+ AvroConfigurationSerializer serializer = new AvroConfigurationSerializer();
+ var avroConfig = serializer.AvroDeseriaizeFromFile("Evaluator.conf");
+ Assert.IsNotNull(avroConfig);
+ }
+
+ [TestMethod]
+ public void OmitOptionalTest()
+ {
+ Type fooType = typeof(Org.Apache.Reef.Tang.Test.Format.IFoo);
+
+ IConfiguration c = MyConfigurationModule.Conf
+ .Set(MyConfigurationModule.TheFoo, GenericType<FooImpl>.Class)
+ .Build();
+ IFoo f = (IFoo)TangFactory.GetTang().NewInjector(c).GetInstance(fooType);
+ Assert.AreEqual(f.getFooness(), 42);
+ }
+
+ [TestMethod]
+ public void OmitRequiredTest()
+ {
+ string msg = null;
+ try
+ {
+ MyConfigurationModule.Conf
+ .Set(MyConfigurationModule.FooNess, "12")
+ .Build();
+ msg = "Attempt to build configuration before setting required option(s): { THE_FOO }";
+ }
+ catch (Exception)
+ {
+ }
+ Assert.IsNull(msg);
+ }
+
+ [TestMethod]
+ public void BadConfTest()
+ {
+ string msg = null;
+ try
+ {
+ object obj = MyMissingBindConfigurationModule.BadConf;
+ msg = "Found declared options that were not used in binds: { FOO_NESS }" + obj;
+ }
+ catch (Exception)
+ {
+ }
+ Assert.IsNull(msg);
+ }
+
+ [TestMethod]
+ public void NonExistentStringBindOK()
+ {
+ new MyBadConfigurationModule().BindImplementation(GenericType<IFoo>.Class, "i.do.not.exist");
+ }
+
+ [TestMethod]
+ public void NonExistentStringBindNotOK()
+ {
+ string msg = null;
+ try
+ {
+ new MyBadConfigurationModule().BindImplementation(GenericType<IFoo>.Class, "i.do.not.exist").Build();
+ msg = "ConfigurationModule refers to unknown class: i.do.not.exist";
+ }
+ catch (Exception)
+ {
+ }
+ Assert.IsNull(msg);
+ }
+
+ [TestMethod]
+ public void MultiBindTest()
+ {
+ // Here we set some configuration values. In true tang style,
+ // you won't be able to set them more than once ConfigurationModule's
+ // implementation is complete.
+ IConfiguration c = MultiBindConfigurationModule.Conf
+ .Set(MultiBindConfigurationModule.TheFoo, GenericType<FooImpl>.Class)
+ .Set(MultiBindConfigurationModule.FOONESS, "12")
+ .Build();
+ IFoo f = (IFoo)TangFactory.GetTang().NewInjector(c).GetInstance(typeof(IFoo));
+ IFoo g = (IFoo)TangFactory.GetTang().NewInjector(c).GetInstance(typeof(object));
+ Assert.AreEqual(f.getFooness(), 12);
+ Assert.AreEqual(g.getFooness(), 12);
+ Assert.IsFalse(f == g);
+ }
+
+ [TestMethod]
+ public void ForeignSetTest()
+ {
+ string msg = null;
+ try
+ {
+ MultiBindConfigurationModule.Conf.Set(MyConfigurationModule.TheFoo, GenericType<FooImpl>.Class);
+ msg = "Unknown Impl/Param when setting RequiredImpl. Did you pass in a field from some other module?";
+ }
+ catch (Exception)
+ {
+ }
+ Assert.IsNull(msg);
+ }
+
+ [TestMethod]
+ public void ForeignBindTest()
+ {
+ string msg = null;
+ try
+ {
+ new MyConfigurationModule().BindImplementation(GenericType<object>.Class, MultiBindConfigurationModule.TheFoo);
+ msg = "Unknown Impl/Param when binding RequiredImpl. Did you pass in a field from some other module?";
+ }
+ catch (Exception)
+ {
+ }
+ Assert.IsNull(msg);
+ }
+
+ [TestMethod]
+ public void SingletonTest()
+ {
+ IConfiguration c = new MyConfigurationModule()
+ .BindImplementation(GenericType<IFoo>.Class, MyConfigurationModule.TheFoo)
+ .BindNamedParameter(GenericType<Fooness>.Class, MyConfigurationModule.FooNess)
+ .Build()
+ .Set(MyConfigurationModule.TheFoo, GenericType<FooImpl>.Class)
+ .Build();
+ IInjector i = TangFactory.GetTang().NewInjector(c);
+ Assert.IsTrue(i.GetInstance(typeof(IFoo)) == i.GetInstance(typeof(IFoo)));
+ }
+
+ [TestMethod]
+ public void ImmutablilityTest()
+ {
+ // builder methods return copies; the original module is immutable
+ ConfigurationModule builder1 = MyConfigurationModule.Conf
+ .Set(MyConfigurationModule.TheFoo, GenericType<FooImpl>.Class);
+
+ Assert.IsFalse(builder1 == MyConfigurationModule.Conf);
+
+ IConfiguration config1 = builder1.Build();
+
+ // reusable
+ IConfiguration config2 = MyConfigurationModule.Conf
+ .Set(MyConfigurationModule.TheFoo, GenericType<FooAltImpl>.Class)
+ .Build();
+
+ // instantiation of each just to be sure everything is fine in this situation
+ IInjector i1 = TangFactory.GetTang().NewInjector(config1);
+ IInjector i2 = TangFactory.GetTang().NewInjector(config2);
+ Assert.AreEqual(42, ((IFoo)i1.GetInstance(typeof(IFoo))).getFooness());
+ Assert.AreEqual(7, ((IFoo)i2.GetInstance(typeof(IFoo))).getFooness());
+ }
+
+ [TestMethod]
+ public void SetParamTest()
+ {
+ IConfiguration c = SetConfigurationModule.CONF
+ .Set(SetConfigurationModule.P, "a")
+ .Set(SetConfigurationModule.P, "b")
+ .Build();
+
+ ISet<string> s = (ISet<string>)TangFactory.GetTang().NewInjector(c).GetNamedInstance(typeof(SetName));
+ Assert.AreEqual(s.Count, 2);
+ Assert.IsTrue(s.Contains("a"));
+ Assert.IsTrue(s.Contains("b"));
+ }
+
+ [TestMethod]
+ public void SetClassTest()
+ {
+ IConfiguration c = SetClassConfigurationModule.CONF
+ .Set(SetClassConfigurationModule.P, GenericType<SubA>.Class)
+ .Set(SetClassConfigurationModule.P, GenericType<SubB>.Class)
+ .Build();
+ ISet<ISuper> s = (ISet<ISuper>)TangFactory.GetTang().NewInjector(c).GetNamedInstance(typeof(SetClass));
+ Assert.AreEqual(2, s.Count);
+
+ bool sawA = false, sawB = false;
+ foreach (ISuper sup in s)
+ {
+ if (sup is SubA)
+ {
+ sawA = true;
+ }
+ else if (sup is SubB)
+ {
+ sawB = true;
+ }
+ else
+ {
+ Assert.Fail();
+ }
+ }
+ Assert.IsTrue(sawA && sawB);
+ }
+
+ [TestMethod]
+ public void SetClassRoundTripTest()
+ {
+ IConfiguration c = SetClassConfigurationModule.CONF
+ .Set(SetClassConfigurationModule.P, GenericType<SubA>.Class)
+ .Set(SetClassConfigurationModule.P, GenericType<SubB>.Class)
+ .Build();
+ IConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(c);
+
+ AvroConfigurationSerializer serializer = new AvroConfigurationSerializer();
+ IConfiguration c2 = serializer.FromString(serializer.ToString(cb.Build()));
+
+ //ConfigurationFile.AddConfiguration(cb, ConfigurationFile.ToConfigurationString(c));
+ ISet<ISuper> s = (ISet<ISuper>)TangFactory.GetTang().NewInjector(c2).GetNamedInstance(typeof(SetClass));
+ Assert.AreEqual(2, s.Count);
+ bool sawA = false, sawB = false;
+ foreach (ISuper sup in s)
+ {
+ if (sup is SubA)
+ {
+ sawA = true;
+ }
+ else if (sup is SubB)
+ {
+ sawB = true;
+ }
+ else
+ {
+ Assert.Fail();
+ }
+ }
+ Assert.IsTrue(sawA && sawB);
+ }
+
+ [TestMethod]
+ public void ErrorOnStaticTimeSet()
+ {
+ string msg = null;
+ try
+ {
+ StaticTimeSet.CONF.AssertStaticClean();
+ msg =
+ " Detected statically set ConfigurationModule Parameter / Implementation. set() should only be used dynamically. Use bind...() instead.";
+ }
+ catch (ClassHierarchyException)
+ {
+ }
+ Assert.IsNull(msg);
+ }
+
+ [TestMethod]
+ public void ErrorOnSetMerge()
+ {
+ ConfigurationModuleBuilder cb = null;
+ try
+ {
+ ConfigurationModuleBuilder b = new ConfigurationModuleBuilder();
+ cb = b.Merge(StaticTimeSet.CONF);
+ }
+ catch (ClassHierarchyException e)
+ {
+ System.Diagnostics.Debug.WriteLine(e);
+ }
+ Assert.IsNull(cb);
+ }
+ }
+
+ [NamedParameter("Fooness", "Fooness", "42")]
+ public class Fooness : Name<int>
+ {
+ }
+
+ public class FooImpl : IFoo
+ {
+ private readonly int fooness;
+
+ [Inject]
+ FooImpl([Parameter(typeof(Fooness))] int fooness)
+ {
+ this.fooness = fooness;
+ }
+
+ public int getFooness()
+ {
+ return this.fooness;
+ }
+ }
+
+ public class FooAltImpl : IFoo
+ {
+ private readonly int fooness;
+
+ [Inject]
+ FooAltImpl([Parameter(Value = typeof(Fooness))] int fooness)
+ {
+ this.fooness = fooness;
+ }
+
+ public int getFooness()
+ {
+ return 7;
+ }
+ }
+
+ public sealed class MyConfigurationModule : ConfigurationModuleBuilder
+ {
+ // Tell us what implementation you want, or else!!
+ [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "Required by Tang")]
+ public static readonly RequiredImpl<IFoo> TheFoo = new RequiredImpl<IFoo>();
+
+ // If you want, you can change the fooness.
+ [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "Required by Tang")]
+ public static readonly OptionalParameter<int> FooNess = new OptionalParameter<int>();
+
+ // This binds the above to tang configuration stuff. You can use parameters more than
+ // once, but you'd better use them all at least once, or I'll throw exceptions at you.
+ [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "Required by Tang")]
+ public static readonly ConfigurationModule Conf = new MyConfigurationModule()
+ .BindImplementation(GenericType<IFoo>.Class, MyConfigurationModule.TheFoo)
+ .BindNamedParameter(GenericType<Fooness>.Class, MyConfigurationModule.FooNess)
+ .Build();
+ }
+
+ public class MyMissingBindConfigurationModule : ConfigurationModuleBuilder
+ {
+ // Tell us what implementation you want, or else!!
+ [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "Required by Tang")]
+ public static readonly RequiredImpl<IFoo> TheFoo = new RequiredImpl<IFoo>();
+
+ // If you want, you can change the fooness.
+ [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "Required by Tang")]
+ public static readonly OptionalParameter<int> FooNess = new OptionalParameter<int>();
+
+ // This conf doesn't use FOO_NESS. Expect trouble below
+ [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "Required by Tang")]
+ public static readonly ConfigurationModule BadConf = new MyMissingBindConfigurationModule()
+ .BindImplementation(GenericType<IFoo>.Class, MyMissingBindConfigurationModule.TheFoo)
+ .Build();
+ }
+
+ public class MyBadConfigurationModule : ConfigurationModuleBuilder
+ {
+ }
+
+ public class MultiBindConfigurationModule : ConfigurationModuleBuilder
+ {
+ // Tell us what implementation you want, or else!!
+ [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "Required by Tang")]
+ public static readonly RequiredImpl<IFoo> TheFoo = new RequiredImpl<IFoo>();
+
+ // If you want, you can change the fooness.
+ [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "Required by Tang")]
+ public static readonly OptionalParameter<int> FOONESS = new OptionalParameter<int>();
+
+ [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "Required by Tang")]
+ public static readonly ConfigurationModule Conf = new MultiBindConfigurationModule()
+ .BindImplementation(GenericType<IFoo>.Class, MultiBindConfigurationModule.TheFoo)
+ .BindImplementation(GenericType<object>.Class, MultiBindConfigurationModule.TheFoo)
+ .BindNamedParameter(GenericType<Fooness>.Class, MultiBindConfigurationModule.FOONESS)
+ .Build();
+ }
+
+ [NamedParameter]
+ class SetName : Name<ISet<string>>
+ {
+ }
+
+ class SetConfigurationModule : ConfigurationModuleBuilder
+ {
+ public static readonly RequiredParameter<string> P = new RequiredParameter<string>();
+
+ public static readonly ConfigurationModule CONF = new SetConfigurationModule()
+ .BindSetEntry(GenericType<SetName>.Class, SetConfigurationModule.P)
+ .Build();
+ }
+
+ [NamedParameter]
+ class SetClass : Name<ISet<ISuper>>
+ {
+ }
+
+ class SetClassConfigurationModule : ConfigurationModuleBuilder
+ {
+ public static readonly RequiredParameter<ISuper> P = new RequiredParameter<ISuper>();
+ public static readonly ConfigurationModule CONF = new SetClassConfigurationModule()
+ .BindSetEntry(GenericType<SetClass>.Class, SetClassConfigurationModule.P)
+ .Build();
+ }
+
+ class SubA : ISuper
+ {
+ [Inject]
+ public SubA()
+ {
+ }
+ }
+
+ class SubB : ISuper
+ {
+ [Inject]
+ public SubB()
+ {
+ }
+ }
+
+ class StaticTimeSet : ConfigurationModuleBuilder
+ {
+ public static readonly OptionalImpl<ISuper> X = new OptionalImpl<ISuper>();
+ public static readonly ConfigurationModule CONF = new StaticTimeSet()
+ .BindImplementation(GenericType<ISuper>.Class, X)
+ .Build()
+ .Set(X, GenericType<SubA>.Class);
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/Format/TestConfigurationModuleForList.cs
----------------------------------------------------------------------
diff --git a/lang/cs/Tests/TangTests/Format/TestConfigurationModuleForList.cs b/lang/cs/Tests/TangTests/Format/TestConfigurationModuleForList.cs
new file mode 100644
index 0000000..b560ec0
--- /dev/null
+++ b/lang/cs/Tests/TangTests/Format/TestConfigurationModuleForList.cs
@@ -0,0 +1,145 @@
+/**
+ * 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;
+
+namespace Org.Apache.Reef.Tang.Test.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/2ae282de/lang/cs/Tests/TangTests/Format/TestTaskConfiguration.cs
----------------------------------------------------------------------
diff --git a/lang/cs/Tests/TangTests/Format/TestTaskConfiguration.cs b/lang/cs/Tests/TangTests/Format/TestTaskConfiguration.cs
new file mode 100644
index 0000000..7d77602
--- /dev/null
+++ b/lang/cs/Tests/TangTests/Format/TestTaskConfiguration.cs
@@ -0,0 +1,389 @@
+/**
+ * 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;
+
+namespace Org.Apache.Reef.Tang.Test.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/2ae282de/lang/cs/Tests/TangTests/Injection/TestAmbigousConstructors.cs
----------------------------------------------------------------------
diff --git a/lang/cs/Tests/TangTests/Injection/TestAmbigousConstructors.cs b/lang/cs/Tests/TangTests/Injection/TestAmbigousConstructors.cs
new file mode 100644
index 0000000..feb34ab
--- /dev/null
+++ b/lang/cs/Tests/TangTests/Injection/TestAmbigousConstructors.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 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 TestAmbigousConstructors
+ {
+ [TestMethod]
+ public void AmbigousConstructorTest()
+ {
+ //Cannot inject Org.Apache.Reef.Tang.Test.Injection.AmbigousConstructorClass, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
+ //Ambiguous subplan Org.Apache.Reef.Tang.Test.Injection.AmbigousConstructorClass, Org.Apache.Reef.Tang.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
+ // new Org.Apache.Reef.Tang.Test.Injection.AmbigousConstructorClass(System.String Org.Apache.Reef.Tang.Test.Injection.AmbigousConstructorClass+NamedString = foo, System.Int32 Org.Apache.Reef.Tang.Test.Injection.AmbigousConstructorClass+NamedInt = 8)
+ // new Org.Apache.Reef.Tang.Test.Injection.AmbigousConstructorClass(System.Int32 Org.Apache.Reef.Tang.Test.Injection.AmbigousConstructorClass+NamedInt = 8, System.String Org.Apache.Reef.Tang.Test.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/2ae282de/lang/cs/Tests/TangTests/Injection/TestForkInjection.cs
----------------------------------------------------------------------
diff --git a/lang/cs/Tests/TangTests/Injection/TestForkInjection.cs b/lang/cs/Tests/TangTests/Injection/TestForkInjection.cs
new file mode 100644
index 0000000..2d9dde5
--- /dev/null
+++ b/lang/cs/Tests/TangTests/Injection/TestForkInjection.cs
@@ -0,0 +1,79 @@
+/**
+ * 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.Interface;
+using Org.Apache.Reef.Tang.Util;
+using System;
+using System.Reflection;
+
+namespace Org.Apache.Reef.Tang.Test.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/2ae282de/lang/cs/Tests/TangTests/Injection/TestInjection.cs
----------------------------------------------------------------------
diff --git a/lang/cs/Tests/TangTests/Injection/TestInjection.cs b/lang/cs/Tests/TangTests/Injection/TestInjection.cs
new file mode 100644
index 0000000..4e765bc
--- /dev/null
+++ b/lang/cs/Tests/TangTests/Injection/TestInjection.cs
@@ -0,0 +1,386 @@
+/**
+ * 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.Interface;
+using Org.Apache.Reef.Tang.Util;
+using Org.Apache.Reef.Tasks;
+using System;
+using System.Reflection;
+
+namespace Org.Apache.Reef.Tang.Test.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/2ae282de/lang/cs/Tests/TangTests/Injection/TestInjectionFuture.cs
----------------------------------------------------------------------
diff --git a/lang/cs/Tests/TangTests/Injection/TestInjectionFuture.cs b/lang/cs/Tests/TangTests/Injection/TestInjectionFuture.cs
new file mode 100644
index 0000000..17349e2
--- /dev/null
+++ b/lang/cs/Tests/TangTests/Injection/TestInjectionFuture.cs
@@ -0,0 +1,240 @@
+/**
+ * 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;
+
+namespace Org.Apache.Reef.Tang.Test.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