http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/Injection/TestSetInjection.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/Injection/TestSetInjection.cs b/lang/cs/Tests/TangTests/Injection/TestSetInjection.cs new file mode 100644 index 0000000..82a9cf5 --- /dev/null +++ b/lang/cs/Tests/TangTests/Injection/TestSetInjection.cs @@ -0,0 +1,739 @@ +/** + * 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.Injection +{ + public interface INumber : IComparable + { + } + + public interface ITimeshift + { + string LinkId { get; } + + TimeSpan TimeshiftSpan { get; } + } + + [TestClass] + public class TestSetInjection + { + [TestMethod] + public void TestStringInjectDefault() + { + Box b = (Box)TangFactory.GetTang().NewInjector().GetInstance(typeof(Box)); + + ISet<string> actual = b.Numbers; + + ISet<string> expected = new HashSet<string>(); + expected.Add("one"); + expected.Add("two"); + expected.Add("three"); + + Assert.IsTrue(actual.Contains("one")); + Assert.IsTrue(actual.Contains("two")); + Assert.IsTrue(actual.Contains("three")); + //Assert.AreEqual(expected, actual); + } + + [TestMethod] + public void TestObjectInjectDefault() + { + IInjector i = TangFactory.GetTang().NewInjector(); + i.BindVolatileInstance(GenericType<Integer>.Class, new Integer(42)); + i.BindVolatileInstance(GenericType<Float>.Class, new Float(42.0001f)); + ISet<INumber> actual = ((Pool)i.GetInstance(typeof(Pool))).Numbers; + ISet<INumber> expected = new HashSet<INumber>(); + expected.Add(new Integer(42)); + expected.Add(new Float(42.0001f)); + + Assert.IsTrue(actual.Contains(new Integer(42))); + Assert.IsTrue(actual.Contains(new Float(42.0001f))); + //Assert.AreEqual(expected, actual); + } + + [TestMethod] + public void testStringInjectBound() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindSetEntry<SetOfNumbers, string>(GenericType<SetOfNumbers>.Class, "four"); + cb.BindSetEntry<SetOfNumbers, string>(GenericType<SetOfNumbers>.Class, "five"); + cb.BindSetEntry<SetOfNumbers, string>(GenericType<SetOfNumbers>.Class, "six"); + + Box b = (Box)TangFactory.GetTang().NewInjector(cb.Build()).GetInstance(typeof(Box)); + ISet<string> actual = b.Numbers; + ISet<string> expected = new HashSet<string>(); + expected.Add("four"); + expected.Add("five"); + expected.Add("six"); + + Assert.IsTrue(actual.Contains("four")); + Assert.IsTrue(actual.Contains("five")); + Assert.IsTrue(actual.Contains("six")); + } + + [TestMethod] + public void TestObjectInjectBound() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindSetEntry<SetOfClasses, Integer, INumber>(GenericType<SetOfClasses>.Class, GenericType<Integer>.Class); + cb.BindSetEntry<SetOfClasses, Float, INumber>(GenericType<SetOfClasses>.Class, GenericType<Float>.Class); + + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + i.BindVolatileInstance(GenericType<Integer>.Class, new Integer(4)); + i.BindVolatileInstance(GenericType<Float>.Class, new Float(42.0001f)); + + ISet<INumber> actual = i.GetInstance<Pool>().Numbers; + ISet<INumber> expected = new HashSet<INumber>(); + expected.Add(new Integer(4)); + expected.Add(new Float(42.0001f)); + Assert.IsTrue(Utilities.Utilities.Equals<INumber>(actual, expected)); + } + + [TestMethod] + public void TestSetOfClassBound() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindSetEntry<SetOfClasses, Integer1, INumber>(GenericType<SetOfClasses>.Class, GenericType<Integer1>.Class) //bind an impl to the interface of the set + .BindNamedParameter<Integer1.NamedInt, int>(GenericType<Integer1.NamedInt>.Class, "4"); //bind parameter for the impl + + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + + ISet<INumber> actual = i.GetInstance<Pool>().Numbers; + ISet<INumber> expected = new HashSet<INumber>(); + expected.Add(new Integer1(4)); + + Assert.IsTrue(Utilities.Utilities.Equals<INumber>(actual, expected)); + } + + [TestMethod] + public void TestSetOfClassWithDefault() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + + ISet<INumber> actual = i.GetInstance<Pool1>().Numbers; + Assert.IsNotNull(actual); + } + + [TestMethod] + public void TestSetOfTimeshift() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + + cb.BindSetEntry<SetOfTimeshifts, Timeshift, ITimeshift>(GenericType<SetOfTimeshifts>.Class, GenericType<Timeshift>.Class) + .BindNamedParameter<Timeshift.TimeshiftLinkId, string>(GenericType<Timeshift.TimeshiftLinkId>.Class, "123") + .BindNamedParameter<Timeshift.TimeshiftInTicks, long>(GenericType<Timeshift.TimeshiftInTicks>.Class, "10"); + + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + + ISet<ITimeshift> actual = i.GetInstance<SetofTimeShiftClass>().Timeshifts; + Assert.IsTrue(actual.Count == 1); + } + + [TestMethod] + public void TestSetOfTimeshiftMultipleInstances() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + + //when adding another Timeshift into the set for named parameter SetOfTimeshifts, it ends up the same entry. + cb.BindSetEntry<SetOfTimeshifts, Timeshift, ITimeshift>(GenericType<SetOfTimeshifts>.Class, GenericType<Timeshift>.Class); + cb.BindSetEntry<SetOfTimeshifts, Timeshift, ITimeshift>(GenericType<SetOfTimeshifts>.Class, GenericType<Timeshift>.Class); + cb.BindNamedParameter<Timeshift.TimeshiftLinkId, string>(GenericType<Timeshift.TimeshiftLinkId>.Class, "123") + .BindNamedParameter<Timeshift.TimeshiftInTicks, long>(GenericType<Timeshift.TimeshiftInTicks>.Class, "10"); + + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + + ISet<ITimeshift> actual = i.GetInstance<SetofTimeShiftClass>().Timeshifts; + Assert.IsTrue(actual.Count == 1); + } + + [TestMethod] + public void TestSetOfTimeshiftMultipleSubClasses() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + + //Adding implementations from different subclasses + cb.BindSetEntry<SetOfTimeshifts, Timeshift, ITimeshift>(GenericType<SetOfTimeshifts>.Class, GenericType<Timeshift>.Class); + cb.BindSetEntry<SetOfTimeshifts, Timeshift1, ITimeshift>(GenericType<SetOfTimeshifts>.Class, GenericType<Timeshift1>.Class); + + cb.BindNamedParameter<Timeshift.TimeshiftLinkId, string>(GenericType<Timeshift.TimeshiftLinkId>.Class, "123") + .BindNamedParameter<Timeshift.TimeshiftInTicks, long>(GenericType<Timeshift.TimeshiftInTicks>.Class, "10"); + + cb.BindNamedParameter<Timeshift1.TimeshiftLinkId, string>(GenericType<Timeshift1.TimeshiftLinkId>.Class, "456") + .BindNamedParameter<Timeshift1.TimeshiftInTicks, long>(GenericType<Timeshift1.TimeshiftInTicks>.Class, "20"); + + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + + ISet<ITimeshift> actual = i.GetInstance<SetofTimeShiftClass>().Timeshifts; + Assert.IsTrue(actual.Count == 2); + } + + [TestMethod] + public void TestSetOfTimeshiftWithDefault() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + + ISet<ITimeshift> actual = i.GetInstance<SetofTimeShiftClass>().Timeshifts; + Assert.IsTrue(actual.Count == 1); + } + + [TestMethod] + public void TestSetOfTimeshiftWithEmptySet() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + + IInjector i = TangFactory.GetTang().NewInjector(cb.Build()); + + ISet<ITimeshift> actual = i.GetInstance<SetofTimeShiftClassWithoutDefault>().Timeshifts; + Assert.IsTrue(actual.Count == 0); + } + + [TestMethod] + public void TestObjectInjectRoundTrip() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindSetEntry<SetOfClasses, Integer, INumber>(GenericType<SetOfClasses>.Class, GenericType<Integer>.Class); + cb.BindSetEntry<SetOfClasses, Float, INumber>(GenericType<SetOfClasses>.Class, GenericType<Float>.Class); + + AvroConfigurationSerializer serializer = new AvroConfigurationSerializer(); + IConfiguration c2 = serializer.FromString(serializer.ToString(cb.Build())); + + IInjector i = TangFactory.GetTang().NewInjector(c2); + i.BindVolatileInstance(GenericType<Integer>.Class, new Integer(4)); + i.BindVolatileInstance(GenericType<Float>.Class, new Float(42.0001f)); + + ISet<INumber> actual = i.GetInstance<Pool>().Numbers; + ISet<INumber> expected = new HashSet<INumber>(); + expected.Add(new Integer(4)); + expected.Add(new Float(42.0001f)); + Assert.IsTrue(Utilities.Utilities.Equals<INumber>(actual, expected)); + } + + [TestMethod] + public void TestStringInjectRoundTrip() + { + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindSetEntry<SetOfNumbers, string>(GenericType<SetOfNumbers>.Class, "four"); + cb.BindSetEntry<SetOfNumbers, string>(GenericType<SetOfNumbers>.Class, "five"); + cb.BindSetEntry<SetOfNumbers, string>(GenericType<SetOfNumbers>.Class, "six"); + + string s = ConfigurationFile.ToConfigurationString(cb.Build()); + ICsConfigurationBuilder cb2 = TangFactory.GetTang().NewConfigurationBuilder(); + ConfigurationFile.AddConfigurationFromString(cb2, s); + + ISet<string> actual = + ((Box)TangFactory.GetTang().NewInjector(cb2.Build()).GetInstance(typeof(Box))).Numbers; + + Assert.IsTrue(actual.Contains("four")); + Assert.IsTrue(actual.Contains("five")); + Assert.IsTrue(actual.Contains("six")); + } + + [TestMethod] + public void TestDefaultAsClass() + { + IInjector i = TangFactory.GetTang().NewInjector(); + i.BindVolatileInstance(GenericType<Integer>.Class, new Integer(1)); + i.BindVolatileInstance(GenericType<Float>.Class, new Float(2f)); + ISet<INumber> actual = + (ISet<INumber>) + i.GetNamedInstance<SetOfClassesDefaultClass, ISet<INumber>>(GenericType<SetOfClassesDefaultClass>.Class); + + ISet<INumber> expected = new HashSet<INumber>(); + expected.Add(new Integer(1)); + Assert.AreEqual(expected.Count, actual.Count); + Assert.IsTrue(actual.Contains(new Integer(1))); + } + + [TestMethod] + public void TestInjectionExtension() + { + IInjector i = TangFactory.GetTang().NewInjector(); + i.BindVolatileInstance<Integer>(new Integer(1)); + i.BindVolatileInstance<Float>(new Float(2f)); + ISet<INumber> actual = + (ISet<INumber>) + i.GetNamedInstance<SetOfClassesDefaultClass, ISet<INumber>>(); + + ISet<INumber> expected = new HashSet<INumber>(); + expected.Add(new Integer(1)); + Assert.AreEqual(expected.Count, actual.Count); + Assert.IsTrue(actual.Contains(new Integer(1))); + } + + [NamedParameter(DefaultValues = new string[] { "one", "two", "three" })] + public class SetOfNumbers : Name<ISet<string>> + { + } + + public class Box + { + [Inject] + public Box([Parameter(typeof(SetOfNumbers))] ISet<string> numbers) + { + this.Numbers = numbers; + } + + public ISet<string> Numbers { get; set; } + } + + [NamedParameter(DefaultClasses = new Type[] { typeof(Integer), typeof(Float) })] + public class SetOfClasses : Name<ISet<INumber>> + { + } + + public class Pool + { + [Inject] + private Pool([Parameter(typeof(SetOfClasses))] ISet<INumber> numbers) + { + this.Numbers = numbers; + } + + public ISet<INumber> Numbers { get; set; } + } + + [NamedParameter(DefaultClass = typeof(Integer))] + public class SetOfClassesDefaultClass : Name<ISet<INumber>> + { + } + + public class Integer : INumber + { + private int val; + + public Integer(int v) + { + val = v; + } + + public int CompareTo(object obj) + { + if (!(obj is Integer)) + { + return -1; + } + if (this.val == ((Integer)obj).val) + { + return 0; + } + + if (this.val < ((Integer)obj).val) + { + return -1; + } + + return 1; + } + + public override bool Equals(object obj) + { + if (!(obj is Integer)) + { + return false; + } + + if (this.val == ((Integer)obj).val) + { + return true; + } + + return false; + } + + public override int GetHashCode() + { + return val.GetHashCode(); + } + } + + public class Float : INumber + { + private float val; + + [Inject] + public Float(float v) + { + val = v; + } + + public int CompareTo(object obj) + { + if (!(obj is Float)) + { + return -1; + } + + if (val == ((Float)obj).val) + { + return 0; + } + + if (val < ((Float)obj).val) + { + return -1; + } + + return 1; + } + + public override bool Equals(object obj) + { + if (!(obj is Float)) + { + return false; + } + + if (this.val == ((Float)obj).val) + { + return true; + } + + return false; + } + + public override int GetHashCode() + { + return val.GetHashCode(); + } + } + + public class Integer1 : INumber + { + private int val; + + [Inject] + public Integer1([Parameter(typeof(NamedInt))] int v) + { + val = v; + } + + public int CompareTo(object obj) + { + if (!(obj is Integer1)) + { + return -1; + } + if (this.val == ((Integer1)obj).val) + { + return 0; + } + + if (this.val < ((Integer1)obj).val) + { + return -1; + } + + return 1; + } + + public override bool Equals(object obj) + { + if (!(obj is Integer1)) + { + return false; + } + + if (this.val == ((Integer1)obj).val) + { + return true; + } + + return false; + } + + public override int GetHashCode() + { + return val.GetHashCode(); + } + + [NamedParameter] + public class NamedInt : Name<int> + { + } + } + + public class Integer2 : INumber + { + private int val; + + [Inject] + public Integer2() + { + val = 0; + } + + public int CompareTo(object obj) + { + if (!(obj is Integer2)) + { + return -1; + } + if (this.val == ((Integer2)obj).val) + { + return 0; + } + + if (this.val < ((Integer2)obj).val) + { + return -1; + } + + return 1; + } + + public override bool Equals(object obj) + { + if (!(obj is Integer2)) + { + return false; + } + + if (this.val == ((Integer2)obj).val) + { + return true; + } + + return false; + } + + public override int GetHashCode() + { + return val.GetHashCode(); + } + } + + public class Integer3 : INumber + { + private int val; + + [Inject] + public Integer3([Parameter(typeof(NamedInt))] int v) + { + val = v; + } + + public int CompareTo(object obj) + { + if (!(obj is Integer)) + { + return -1; + } + if (this.val == ((Integer3)obj).val) + { + return 0; + } + + if (this.val < ((Integer3)obj).val) + { + return -1; + } + + return 1; + } + + public override bool Equals(object obj) + { + if (!(obj is Integer3)) + { + return false; + } + + if (this.val == ((Integer3)obj).val) + { + return true; + } + + return false; + } + + public override int GetHashCode() + { + return val.GetHashCode(); + } + + [NamedParameter] + public class NamedInt : Name<int> + { + } + } + + public class Float1 : INumber + { + private float val; + + [Inject] + public Float1([Parameter(typeof(NamedFloat))] float v) + { + val = v; + } + + public int CompareTo(object obj) + { + if (!(obj is Float)) + { + return -1; + } + + if (val == ((Float1)obj).val) + { + return 0; + } + + if (val < ((Float1)obj).val) + { + return -1; + } + + return 1; + } + + public override bool Equals(object obj) + { + if (!(obj is Float1)) + { + return false; + } + + if (this.val == ((Float1)obj).val) + { + return true; + } + + return false; + } + + public override int GetHashCode() + { + return val.GetHashCode(); + } + + [NamedParameter] + public class NamedFloat : Name<float> + { + } + } + + public class Pool1 + { + [Inject] + private Pool1([Parameter(typeof(SetOfClasseWithDefault))] ISet<INumber> numbers) + { + this.Numbers = numbers; + } + + public ISet<INumber> Numbers { get; set; } + } + + [NamedParameter(DefaultClass = typeof(Integer2))] + public class SetOfClasseWithDefault : Name<ISet<INumber>> + { + } + } + + public class Timeshift : ITimeshift + { + [Inject] + public Timeshift([Parameter(typeof(TimeshiftLinkId))] string linkId, [Parameter(typeof(TimeshiftInTicks))] long timeshiftInTicks) + { + this.LinkId = linkId; + this.TimeshiftSpan = TimeSpan.FromTicks(timeshiftInTicks); + } + + public string LinkId { get; private set; } + + public TimeSpan TimeshiftSpan { get; private set; } + + [NamedParameter("TimeshiftLinkId", "TimeshiftLinkId", "myid")] + public class TimeshiftLinkId : Name<string> + { + } + + [NamedParameter("TimeshiftInTicks", "TimeshiftInTicks", "10")] + public class TimeshiftInTicks : Name<long> + { + } + } + + public class Timeshift1 : ITimeshift + { + [Inject] + public Timeshift1([Parameter(typeof(TimeshiftLinkId))] string linkId, [Parameter(typeof(TimeshiftInTicks))] long timeshiftInTicks) + { + this.LinkId = linkId; + this.TimeshiftSpan = TimeSpan.FromTicks(timeshiftInTicks); + } + + public string LinkId { get; private set; } + + public TimeSpan TimeshiftSpan { get; private set; } + + [NamedParameter("TimeshiftLinkId1", "TimeshiftLinkId1", "myid")] + public class TimeshiftLinkId : Name<string> + { + } + + [NamedParameter("TimeshiftInTicks1", "TimeshiftInTicks1", "10")] + public class TimeshiftInTicks : Name<long> + { + } + } + + [NamedParameter(DefaultClass = typeof(Timeshift))] + public class SetOfTimeshifts : Name<ISet<ITimeshift>> + { + } + + public class SetofTimeShiftClass + { + [Inject] + public SetofTimeShiftClass([Parameter(typeof(SetOfTimeshifts))] ISet<ITimeshift> timeshifts) + { + this.Timeshifts = timeshifts; + } + + public ISet<ITimeshift> Timeshifts { get; set; } + } + [NamedParameter] + public class SetOfTimeshiftsWithoutDefaultClass : Name<ISet<ITimeshift>> + { + } + + public class SetofTimeShiftClassWithoutDefault + { + [Inject] + public SetofTimeShiftClassWithoutDefault([Parameter(typeof(SetOfTimeshiftsWithoutDefaultClass))] ISet<ITimeshift> timeshifts) + { + this.Timeshifts = timeshifts; + } + + public ISet<ITimeshift> Timeshifts { get; set; } + } +} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/Properties/AssemblyInfo.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/Properties/AssemblyInfo.cs b/lang/cs/Tests/TangTests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..02a072f --- /dev/null +++ b/lang/cs/Tests/TangTests/Properties/AssemblyInfo.cs @@ -0,0 +1,55 @@ +/** + * 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.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Org.Apache.Reef.Tang.Tests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Org.Apache.Reef.Tang.Tests")] +[assembly: AssemblyCopyright("Copyright © 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("fd47238d-600b-42cd-b62d-0724171a2bc4")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/ScenarioTest/HttpHandlerConfiguration.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/ScenarioTest/HttpHandlerConfiguration.cs b/lang/cs/Tests/TangTests/ScenarioTest/HttpHandlerConfiguration.cs new file mode 100644 index 0000000..41d6c91 --- /dev/null +++ b/lang/cs/Tests/TangTests/ScenarioTest/HttpHandlerConfiguration.cs @@ -0,0 +1,35 @@ +/** + * 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.Formats; +using Org.Apache.Reef.Tang.Util; + +namespace Org.Apache.Reef.Tang.Test.ScenarioTest +{ + public class HttpHandlerConfiguration : ConfigurationModuleBuilder + { + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "Not Applicable")] + public static readonly OptionalParameter<IHttpHandler> P = new OptionalParameter<IHttpHandler>(); + + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "Not Applicable")] + public static readonly ConfigurationModule CONF = new HttpHandlerConfiguration().Merge(HttpRuntimeConfiguration.CONF) + .BindSetEntry<HttpEventHanlders, IHttpHandler>(GenericType<HttpEventHanlders>.Class, HttpHandlerConfiguration.P) + .Build(); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/ScenarioTest/HttpRuntimeConfiguration.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/ScenarioTest/HttpRuntimeConfiguration.cs b/lang/cs/Tests/TangTests/ScenarioTest/HttpRuntimeConfiguration.cs new file mode 100644 index 0000000..7d91b6c --- /dev/null +++ b/lang/cs/Tests/TangTests/ScenarioTest/HttpRuntimeConfiguration.cs @@ -0,0 +1,36 @@ +/** + * 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.Formats; +using Org.Apache.Reef.Tang.Util; + +namespace Org.Apache.Reef.Tang.Test.ScenarioTest +{ + public class HttpRuntimeConfiguration : ConfigurationModuleBuilder + { + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "Not Applicable")] + public static readonly ConfigurationModule CONF = new HttpRuntimeConfiguration() + .BindSetEntry<RuntimeStartHandler, HttpRunTimeStartHandler, IObserver<RuntimeStart>>( + GenericType<RuntimeStartHandler>.Class, GenericType<HttpRunTimeStartHandler>.Class) + .BindSetEntry<RuntimeStopHandler, HttpRunTimeStopHandler, IObserver<RuntimeStop>>( + GenericType<RuntimeStopHandler>.Class, GenericType<HttpRunTimeStopHandler>.Class) + .Build(); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/ScenarioTest/HttpRuntimeStartHandler.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/ScenarioTest/HttpRuntimeStartHandler.cs b/lang/cs/Tests/TangTests/ScenarioTest/HttpRuntimeStartHandler.cs new file mode 100644 index 0000000..38acda6 --- /dev/null +++ b/lang/cs/Tests/TangTests/ScenarioTest/HttpRuntimeStartHandler.cs @@ -0,0 +1,50 @@ +/** + * 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; + +namespace Org.Apache.Reef.Tang.Test.ScenarioTest +{ + public class HttpRunTimeStartHandler : IObserver<RuntimeStart> + { + [Inject] + public HttpRunTimeStartHandler(HttpServer httpServer) + { + Server = httpServer; + } + + public HttpServer Server { get; set; } + + public void OnCompleted() + { + throw new NotImplementedException(); + } + + public void OnError(Exception error) + { + throw new NotImplementedException(); + } + + public void OnNext(RuntimeStart value) + { + Server.StartServer(); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/ScenarioTest/HttpRuntimeStopHandler.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/ScenarioTest/HttpRuntimeStopHandler.cs b/lang/cs/Tests/TangTests/ScenarioTest/HttpRuntimeStopHandler.cs new file mode 100644 index 0000000..7b687e6 --- /dev/null +++ b/lang/cs/Tests/TangTests/ScenarioTest/HttpRuntimeStopHandler.cs @@ -0,0 +1,50 @@ +/** + * 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; + +namespace Org.Apache.Reef.Tang.Test.ScenarioTest +{ + public class HttpRunTimeStopHandler : IObserver<RuntimeStop> + { + [Inject] + public HttpRunTimeStopHandler(HttpServer httpServer) + { + Server = httpServer; + } + + public HttpServer Server { get; set; } + + public void OnCompleted() + { + throw new NotImplementedException(); + } + + public void OnError(Exception error) + { + throw new NotImplementedException(); + } + + public void OnNext(RuntimeStop value) + { + Server.StopServer(); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/ScenarioTest/HttpServer.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/ScenarioTest/HttpServer.cs b/lang/cs/Tests/TangTests/ScenarioTest/HttpServer.cs new file mode 100644 index 0000000..beea575 --- /dev/null +++ b/lang/cs/Tests/TangTests/ScenarioTest/HttpServer.cs @@ -0,0 +1,49 @@ +/** + * 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; + +namespace Org.Apache.Reef.Tang.Test.ScenarioTest +{ + public class HttpServer + { + private Server _server; + + [Inject] + public HttpServer(JettyHandler jettyHandler) + { + JettyHandler = jettyHandler; + _server = new Server(8080); + _server.SetHandler(jettyHandler); + } + + public JettyHandler JettyHandler { get; set; } + + public void StartServer() + { + _server.Start(); + _server.Join(); + } + + public void StopServer() + { + _server.Stop(); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/ScenarioTest/IHttpHandler.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/ScenarioTest/IHttpHandler.cs b/lang/cs/Tests/TangTests/ScenarioTest/IHttpHandler.cs new file mode 100644 index 0000000..031c29c --- /dev/null +++ b/lang/cs/Tests/TangTests/ScenarioTest/IHttpHandler.cs @@ -0,0 +1,27 @@ +/** + * 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. + */ +namespace Org.Apache.Reef.Tang.Test.ScenarioTest +{ + public interface IHttpHandler + { + string GetUriSpecification(); + + void OnHttpRequest(HttpRequest request, Httpresponse response); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/ScenarioTest/JettyHandler.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/ScenarioTest/JettyHandler.cs b/lang/cs/Tests/TangTests/ScenarioTest/JettyHandler.cs new file mode 100644 index 0000000..b027e91 --- /dev/null +++ b/lang/cs/Tests/TangTests/ScenarioTest/JettyHandler.cs @@ -0,0 +1,44 @@ +/** + * 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.Annotations; + +namespace Org.Apache.Reef.Tang.Test.ScenarioTest +{ + [NamedParameter()] + public class HttpEventHanlders : Name<ISet<IHttpHandler>> + { + } + + public class JettyHandler //: AbstractHandler + { + [Inject] + public JettyHandler([Parameter(typeof(HttpEventHanlders))] ISet<IHttpHandler> httpeventHanlders) + { + HttpeventHanlders = httpeventHanlders; + } + + public ISet<IHttpHandler> HttpeventHanlders { get; set; } + + public void handle() + { + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/ScenarioTest/TestDefaultConstructor.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/ScenarioTest/TestDefaultConstructor.cs b/lang/cs/Tests/TangTests/ScenarioTest/TestDefaultConstructor.cs new file mode 100644 index 0000000..fd09bbc --- /dev/null +++ b/lang/cs/Tests/TangTests/ScenarioTest/TestDefaultConstructor.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 Org.Apache.Reef.Tang.Annotations; +using Org.Apache.Reef.Tang.Implementations; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Org.Apache.Reef.Tang.Test.ScenarioTest +{ + [TestClass] + public class TestDefaultConstructor + { + [TestMethod] + public void TestDefaultCOnstructorWithoutBinding() + { + var r = (A)TangFactory.GetTang().NewInjector().GetInstance(typeof(A)); + System.Diagnostics.Debug.WriteLine(r.Instance); + } + } + + class A + { + [Inject] + public A() + { + Instance = "default"; + } + [Inject] + public A(B b) + { + Instance = "non default"; + } + + public string Instance { get; set; } + } + + class B + { + [Inject] + public B() + { + Instance = "default"; + } + [Inject] + public B(C c) + { + Instance = "non default"; + } + + public string Instance { get; set; } + } + + class C + { + [Inject] + public C() + { + Instance = "default"; + } + + public string Instance { get; set; } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/ScenarioTest/TestHttpService.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/ScenarioTest/TestHttpService.cs b/lang/cs/Tests/TangTests/ScenarioTest/TestHttpService.cs new file mode 100644 index 0000000..8d1e9d7 --- /dev/null +++ b/lang/cs/Tests/TangTests/ScenarioTest/TestHttpService.cs @@ -0,0 +1,194 @@ +/** + * 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.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.ScenarioTest +{ + [TestClass] + public class TestHttpService + { + [TestMethod] + public void HttpEventHanldersTest() + { + ConfigurationModule module = + new ConfigurationModuleBuilder() + .BindSetEntry<HttpEventHanlders, HttpServerReefEventHandler, IHttpHandler>(GenericType<HttpEventHanlders>.Class, GenericType<HttpServerReefEventHandler>.Class) + .BindSetEntry<HttpEventHanlders, HttpServerNrtEventHandler, IHttpHandler>(GenericType<HttpEventHanlders>.Class, GenericType<HttpServerNrtEventHandler>.Class) + .Build(); + + IConfiguration c = module.Build(); + var service = TangFactory.GetTang().NewInjector(c).GetInstance<HttpServer>(); + Assert.IsNotNull(service); + + var j = TangFactory.GetTang().NewInjector(c).GetInstance<HttpRunTimeStartHandler>(); + Assert.IsNotNull(j); + } + + [TestMethod] + public void RuntimeStartHandlerTest() + { + ConfigurationModule module = + new ConfigurationModuleBuilder() + .BindSetEntry<RuntimeStartHandler, HttpRunTimeStartHandler, IObserver<RuntimeStart>>( + GenericType<RuntimeStartHandler>.Class, GenericType<HttpRunTimeStartHandler>.Class) + .Build(); + IConfiguration clockConfiguraiton = module.Build(); + + RuntimeClock clock = TangFactory.GetTang().NewInjector(clockConfiguraiton).GetInstance<RuntimeClock>(); + var rh = clock.ClockRuntimeStartHandler.Get(); + Assert.AreEqual(rh.Count, 1); + foreach (var e in rh) + { + Assert.IsTrue(e is HttpRunTimeStartHandler); + HttpRunTimeStartHandler r = (HttpRunTimeStartHandler)e; + var s = r.Server; + Assert.AreEqual(s.JettyHandler.HttpeventHanlders.Count, 0); // no handlers are bound + } + } + + [TestMethod] + public void RuntimeStartStopHandlerTest() + { + IConfiguration clockConfiguraiton = HttpRuntimeConfiguration.CONF.Build(); + RuntimeClock clock = TangFactory.GetTang().NewInjector(clockConfiguraiton).GetInstance<RuntimeClock>(); + var starts = clock.ClockRuntimeStartHandler.Get(); + var stops = clock.ClockRuntimeStopHandler.Get(); + + HttpRunTimeStartHandler start = null; + HttpRunTimeStopHandler stop = null; + + Assert.AreEqual(starts.Count, 1); + foreach (var e in starts) + { + Assert.IsTrue(e is HttpRunTimeStartHandler); + start = (HttpRunTimeStartHandler)e; + } + + Assert.AreEqual(stops.Count, 1); + foreach (var e in stops) + { + Assert.IsTrue(e is HttpRunTimeStopHandler); + stop = (HttpRunTimeStopHandler)e; + } + + Assert.AreEqual(start.Server, stop.Server); + Assert.AreEqual(start.Server.JettyHandler.HttpeventHanlders, stop.Server.JettyHandler.HttpeventHanlders); + Assert.AreSame(start.Server, stop.Server); + } + + [TestMethod] + public void RuntimeStartHandlerMergeTest() + { + IConfiguration clockConfiguraiton = HttpHandlerConfiguration.CONF + .Set(HttpHandlerConfiguration.P, + GenericType<HttpServerReefEventHandler>.Class) + .Set(HttpHandlerConfiguration.P, + GenericType<HttpServerNrtEventHandler>.Class) + .Build(); + + RuntimeClock clock = TangFactory.GetTang().NewInjector(clockConfiguraiton).GetInstance<RuntimeClock>(); + + var rh = clock.ClockRuntimeStartHandler.Get(); + Assert.AreEqual(rh.Count, 1); + foreach (var e in rh) + { + Assert.IsTrue(e is HttpRunTimeStartHandler); + HttpRunTimeStartHandler r = (HttpRunTimeStartHandler)e; + var s = r.Server; + foreach (IHttpHandler h in s.JettyHandler.HttpeventHanlders) + { + System.Diagnostics.Debug.WriteLine(h.GetUriSpecification()); + } + } + } + } + + public class HttpRequest + { + } + + public class Httpresponse + { + } + + public class HttpServerReefEventHandler : IHttpHandler + { + [Inject] + public HttpServerReefEventHandler() + { + } + + public string GetUriSpecification() + { + return "/Reef"; + } + + public void OnHttpRequest(HttpRequest request, Httpresponse response) + { + //handle the event + } + } + + public class HttpServerNrtEventHandler : IHttpHandler + { + [Inject] + public HttpServerNrtEventHandler() + { + } + + public string GetUriSpecification() + { + return "/NRT"; + } + + public void OnHttpRequest(HttpRequest request, Httpresponse response) + { + } + } + + public class Server + { + public Server(int port) + { + } + + public void Start() + { + } + + public void Stop() + { + } + + public void Join() + { + } + + public void SetHandler(JettyHandler handler) + { + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/ScenarioTest/TestRuntimeClock.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/ScenarioTest/TestRuntimeClock.cs b/lang/cs/Tests/TangTests/ScenarioTest/TestRuntimeClock.cs new file mode 100644 index 0000000..c5ce694 --- /dev/null +++ b/lang/cs/Tests/TangTests/ScenarioTest/TestRuntimeClock.cs @@ -0,0 +1,253 @@ +/** + * 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.ScenarioTest +{ + [DefaultImplementation(typeof(RealTimer))] + public interface ITimer + { + int GetCurrent(); + + int GetDuration(int time); + + bool IsReady(int time); + } + + [DefaultImplementation(typeof(RuntimeClock))] + public interface IClock + { + DateTime CurrentTime(); + } + + public interface IEventHandler<T> + { + void OnNext(T value); + } + + [TestClass] + public class TestSenarios + { + [TestMethod] + public void TestRuntimeClock() + { + var r = (RuntimeClock)TangFactory.GetTang().NewInjector().GetInstance(typeof(RuntimeClock)); + Assert.IsNotNull(r); + r.CurrentTime(); + } + + [TestMethod] + public void TestEvaluatorRuntime() + { + ConfigurationModule module = + new ConfigurationModuleBuilder() + .BindSetEntry<RuntimeStartHandler, EvaluatorRuntime, IObserver<RuntimeStart>>(GenericType<RuntimeStartHandler>.Class, GenericType<EvaluatorRuntime>.Class) + .Build(); + IConfiguration clockConfiguraiton = module.Build(); + + RuntimeClock clock = TangFactory.GetTang().NewInjector(clockConfiguraiton).GetInstance<RuntimeClock>(); + var r = clock.ClockRuntimeStartHandler.Get(); + Assert.AreEqual(r.Count, 1); + foreach (var e in r) + { + Assert.IsTrue(e is EvaluatorRuntime); + } + } + } + + public class RuntimeClock : IClock + { + [Inject] + public RuntimeClock(ITimer timer, + [Parameter(typeof(StartHandler))] IInjectionFuture<ISet<IEventHandler<StartTime>>> startHandler, + [Parameter(typeof(RuntimeStartHandler))] IInjectionFuture<ISet<IObserver<RuntimeStart>>> runtimeStartHandler, + [Parameter(typeof(RuntimeStopHandler))] IInjectionFuture<ISet<IObserver<RuntimeStop>>> runtimeStopHandler) + { + this.ClockStartHandler = startHandler; + this.ClockRuntimeStartHandler = runtimeStartHandler; + this.ClockRuntimeStopHandler = runtimeStopHandler; + } + + public IInjectionFuture<ISet<IObserver<RuntimeStart>>> ClockRuntimeStartHandler { get; set; } + + public IInjectionFuture<ISet<IObserver<RuntimeStop>>> ClockRuntimeStopHandler { get; set; } + + public IInjectionFuture<ISet<IEventHandler<StartTime>>> ClockStartHandler { get; set; } + + public DateTime CurrentTime() + { + return DateTime.Now; + } + } + + [NamedParameter(DefaultClass = typeof(MissingStartHandlerHandler), + Documentation = "Will be called upon the start event")] + public class StartHandler : Name<ISet<IEventHandler<StartTime>>> + { + } + + /// <summary> + /// Bind this to an event handler to statically subscribe to the RuntimeStart Event + /// </summary> + [NamedParameter(Documentation = "Will be called upon the runtime start event", + DefaultClass = typeof(LoggingEventHandler<RuntimeStart>))] + public class RuntimeStartHandler : Name<ISet<IObserver<RuntimeStart>>> + { + } + + [NamedParameter(documentation: "Will be called upon the runtime start event", + defaultClass: typeof(LoggingEventHandler<RuntimeStop>))] + public class RuntimeStopHandler : Name<ISet<IObserver<RuntimeStop>>> + { + } + + public class StartTime : Time + { + public StartTime(long timestamp) : base(timestamp) + { + } + } + + public class RuntimeStart : Time + { + public RuntimeStart(long timeStamp) + : base(timeStamp) + { + } + } + + public class RuntimeStop : Time + { + public RuntimeStop(long timeStamp) + : base(timeStamp) + { + } + } + + public class EvaluatorRuntime : IObserver<RuntimeStart> + { + [Inject] + public EvaluatorRuntime() + { + } + + public void OnCompleted() + { + throw new NotImplementedException(); + } + + public void OnError(Exception error) + { + throw new NotImplementedException(); + } + + public void OnNext(RuntimeStart value) + { + throw new NotImplementedException(); + } + } + + public class LoggingEventHandler<T> : IObserver<T> + { + [Inject] + public LoggingEventHandler() + { + } + + /// <summary>Logs the event</summary> + /// <param name="value">an event</param> + public void OnNext(T value) + { + throw new NotImplementedException(); + } + + public void OnError(Exception error) + { + throw new NotImplementedException(); + } + + public void OnCompleted() + { + throw new NotImplementedException(); + } + } + + public abstract class Time : IComparable<Time> + { + private long timestamp; + + public Time(long timestamp) + { + this.timestamp = timestamp; + } + + public long GetTimeStamp() + { + return this.timestamp; + } + + public int CompareTo(Time other) + { + throw new NotImplementedException(); + } + } + + public class MissingStartHandlerHandler : IEventHandler<StartTime> + { + [Inject] + public MissingStartHandlerHandler() + { + } + + public void OnNext(StartTime value) + { + } + } + + public class RealTimer : ITimer + { + [Inject] + public RealTimer() + { + } + + public int GetCurrent() + { + return DateTime.Now.Millisecond; + } + + public int GetDuration(int time) + { + return time - GetCurrent(); + } + + public bool IsReady(int time) + { + return GetDuration(time) <= 0; + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/ScenarioTest/TestTrackingURIProvider.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/ScenarioTest/TestTrackingURIProvider.cs b/lang/cs/Tests/TangTests/ScenarioTest/TestTrackingURIProvider.cs new file mode 100644 index 0000000..f97e9db --- /dev/null +++ b/lang/cs/Tests/TangTests/ScenarioTest/TestTrackingURIProvider.cs @@ -0,0 +1,59 @@ +/** + * 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.Implementations; +using Org.Apache.Reef.Tang.Interface; +using Org.Apache.Reef.Tang.Util; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Org.Apache.Reef.Tang.Test.ScenarioTest +{ + [TestClass] + public class TestTrackingURIProvider + { + [TestMethod] + public void TrackingIdThroughNamedParameterTest() + { + ICsConfigurationBuilder cba = TangFactory.GetTang().NewConfigurationBuilder(); + string trackingId = System.Environment.MachineName + ":8080"; + cba.BindNamedParameter<TrackingId, string>(GenericType<TrackingId>.Class, trackingId); + string id = (string)TangFactory.GetTang().NewInjector(cba.Build()).GetNamedInstance(typeof(TrackingId)); + Assert.AreEqual(id, trackingId); + } + + [TestMethod] + public void DefaultTrackingIdThroughInterfaceTest() + { + string trackingId = System.Environment.MachineName + ":8080"; + var id = TangFactory.GetTang().NewInjector().GetInstance<ITrackingURIProvider>(); + Assert.AreEqual(id.GetURI(), trackingId); + } + + [TestMethod] + public void TrackingIdThroughInterfaceTest() + { + ICsConfigurationBuilder cba = TangFactory.GetTang().NewConfigurationBuilder(); + cba.BindNamedParameter<PortNumber, string>(GenericType<PortNumber>.Class, "8080"); + string trackingId = System.Environment.MachineName + ":8080"; + var id = TangFactory.GetTang().NewInjector().GetInstance<ITrackingURIProvider>(); + Assert.AreEqual(id.GetURI(), trackingId); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/ScenarioTest/TrackingURIProvider.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/ScenarioTest/TrackingURIProvider.cs b/lang/cs/Tests/TangTests/ScenarioTest/TrackingURIProvider.cs new file mode 100644 index 0000000..d9200db --- /dev/null +++ b/lang/cs/Tests/TangTests/ScenarioTest/TrackingURIProvider.cs @@ -0,0 +1,63 @@ +/** + * 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; + +namespace Org.Apache.Reef.Tang.Test.ScenarioTest +{ + [DefaultImplementation(typeof(DefaultTrackingURIProvider))] + public interface ITrackingURIProvider + { + string GetURI(); + } + + [NamedParameter(DefaultValue = " ")] + public class TrackingId : Name<string> + { + } + + [NamedParameter(DefaultValue = "8080")] + public class PortNumber : Name<string> + { + } + + public class DefaultTrackingURIProvider : ITrackingURIProvider + { + private string port; + + [Inject] + public DefaultTrackingURIProvider([Parameter(typeof(PortNumber))] string port) + { + this.port = port; + } + + public string GetURI() + { + try + { + return System.Environment.MachineName + ":" + port; + } + catch (Exception) + { + return null; + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/ScenarioTest/TrackingYRIProvider.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/ScenarioTest/TrackingYRIProvider.cs b/lang/cs/Tests/TangTests/ScenarioTest/TrackingYRIProvider.cs new file mode 100644 index 0000000..47d408b --- /dev/null +++ b/lang/cs/Tests/TangTests/ScenarioTest/TrackingYRIProvider.cs @@ -0,0 +1,25 @@ +/** + * 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. + */ + +namespace Org.Apache.Reef.Tang.Test.ScenarioTest +{ + class TrackingYRIProvider + { + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/SmokeTest/AnInterface.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/SmokeTest/AnInterface.cs b/lang/cs/Tests/TangTests/SmokeTest/AnInterface.cs new file mode 100644 index 0000000..86f45cb --- /dev/null +++ b/lang/cs/Tests/TangTests/SmokeTest/AnInterface.cs @@ -0,0 +1,29 @@ +/** + * 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; + +namespace Org.Apache.Reef.Tang.Test.SmokeTest +{ + [DefaultImplementation(typeof(AnInterfaceImplementation))] + public interface IAnInterface + { + void AMethod(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/SmokeTest/AnInterfaceImplementation.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/SmokeTest/AnInterfaceImplementation.cs b/lang/cs/Tests/TangTests/SmokeTest/AnInterfaceImplementation.cs new file mode 100644 index 0000000..317cce1 --- /dev/null +++ b/lang/cs/Tests/TangTests/SmokeTest/AnInterfaceImplementation.cs @@ -0,0 +1,65 @@ +/** + * 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; + +namespace Org.Apache.Reef.Tang.Test.SmokeTest +{ + public class AnInterfaceImplementation : IAnInterface + { + private readonly int aMagicNumber; + + [Inject] + AnInterfaceImplementation() + { + this.aMagicNumber = 42; + } + + public void AMethod() + { + } + + public override bool Equals(object o) + { + if (this == o) + { + return true; + } + + if (o == null) + { + return false; + } + + AnInterfaceImplementation that = (AnInterfaceImplementation)o; + + if (aMagicNumber != that.aMagicNumber) + { + return false; + } + + return true; + } + + public override int GetHashCode() + { + return aMagicNumber; + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/SmokeTest/CyclicDependency.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/SmokeTest/CyclicDependency.cs b/lang/cs/Tests/TangTests/SmokeTest/CyclicDependency.cs new file mode 100644 index 0000000..fb3e4dd --- /dev/null +++ b/lang/cs/Tests/TangTests/SmokeTest/CyclicDependency.cs @@ -0,0 +1,70 @@ +/** + * 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; + +namespace Org.Apache.Reef.Tang.Test.SmokeTest +{ + public class CyclicDependency + { + private readonly CyclicDependencyClassOne one; + private readonly CyclicDependencyClassTwo two; + + [Inject] + CyclicDependency(CyclicDependencyClassOne one, CyclicDependencyClassTwo two) + { + this.one = one; + this.two = two; + } + + public override bool Equals(object o) + { + if (this == o) + { + return true; + } + + if (o == null) + { + return false; + } + + CyclicDependency that = (CyclicDependency)o; + + if (!one.Equals(that.one)) + { + return false; + } + + if (!two.Equals(that.two)) + { + return false; + } + + return true; + } + + public override int GetHashCode() + { + int result = one.GetHashCode(); + result = (31 * result) + two.GetHashCode(); + return result; + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/SmokeTest/CyclicDependencyClassOne.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/SmokeTest/CyclicDependencyClassOne.cs b/lang/cs/Tests/TangTests/SmokeTest/CyclicDependencyClassOne.cs new file mode 100644 index 0000000..bbc5d4d --- /dev/null +++ b/lang/cs/Tests/TangTests/SmokeTest/CyclicDependencyClassOne.cs @@ -0,0 +1,61 @@ +/** + * 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; + +namespace Org.Apache.Reef.Tang.Test.SmokeTest +{ + public class CyclicDependencyClassOne + { + private readonly CyclicDependencyClassTwo other; + + [Inject] + CyclicDependencyClassOne(CyclicDependencyClassTwo other) + { + this.other = other; + } + + public override bool Equals(object o) + { + if (this == o) + { + return true; + } + + if (o == null) + { + return false; + } + + CyclicDependencyClassOne that = (CyclicDependencyClassOne)o; + + if (!other.Equals(that.other)) + { + return false; + } + + return true; + } + + public override int GetHashCode() + { + return other.GetHashCode(); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/SmokeTest/CyclicDependencyClassTwo.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/SmokeTest/CyclicDependencyClassTwo.cs b/lang/cs/Tests/TangTests/SmokeTest/CyclicDependencyClassTwo.cs new file mode 100644 index 0000000..ea79c78 --- /dev/null +++ b/lang/cs/Tests/TangTests/SmokeTest/CyclicDependencyClassTwo.cs @@ -0,0 +1,55 @@ +/** + * 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; + +namespace Org.Apache.Reef.Tang.Test.SmokeTest +{ + public class CyclicDependencyClassTwo + { + private readonly IInjectionFuture<CyclicDependencyClassOne> other; + + [Inject] + CyclicDependencyClassTwo(IInjectionFuture<CyclicDependencyClassOne> other) + { + this.other = other; + } + + public override bool Equals(object o) + { + if (this == o) + { + return true; + } + + if (o == null) + { + return false; + } + + return true; + } + + public override int GetHashCode() + { + return other.GetHashCode(); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/SmokeTest/Handler.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/SmokeTest/Handler.cs b/lang/cs/Tests/TangTests/SmokeTest/Handler.cs new file mode 100644 index 0000000..8cf1bfb --- /dev/null +++ b/lang/cs/Tests/TangTests/SmokeTest/Handler.cs @@ -0,0 +1,26 @@ +/** + * 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. + */ + +namespace Org.Apache.Reef.Tang.Test.SmokeTest +{ + public interface IHandler<T> + { + void Process(T value); + } +} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/SmokeTest/InjectableClass.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/SmokeTest/InjectableClass.cs b/lang/cs/Tests/TangTests/SmokeTest/InjectableClass.cs new file mode 100644 index 0000000..05291c1 --- /dev/null +++ b/lang/cs/Tests/TangTests/SmokeTest/InjectableClass.cs @@ -0,0 +1,60 @@ +/** + * 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; + +namespace Org.Apache.Reef.Tang.Test.SmokeTest +{ + public class InjectableClass + { + private readonly int magicNumber = -42; + + [Inject] + InjectableClass() + { + } + + public override bool Equals(object o) + { + if (this == o) + { + return true; + } + + if (o == null) + { + return false; + } + + InjectableClass that = (InjectableClass)o; + + if (magicNumber != that.magicNumber) + { + return false; + } + + return true; + } + + public override int GetHashCode() + { + return magicNumber; + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/SmokeTest/ListOfBaseTypes.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/SmokeTest/ListOfBaseTypes.cs b/lang/cs/Tests/TangTests/SmokeTest/ListOfBaseTypes.cs new file mode 100644 index 0000000..278a543 --- /dev/null +++ b/lang/cs/Tests/TangTests/SmokeTest/ListOfBaseTypes.cs @@ -0,0 +1,108 @@ +/** + * 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 Org.Apache.Reef.Tang.Annotations; + +namespace Org.Apache.Reef.Tang.Test.SmokeTest +{ + public class ListOfBaseTypes + { + private readonly IList<int> integers; + private readonly IList<double> doubles; + private readonly IList<string> strings; + private readonly IList<int> moreIntegers; + + [Inject] + private ListOfBaseTypes([Parameter(typeof(ListOfBaseTypes.Integers))] IList<int> integers, + [Parameter(typeof(ListOfBaseTypes.Doubles))] IList<double> doubles, + [Parameter(typeof(ListOfBaseTypes.Strings))] IList<string> strings, + [Parameter(typeof(ListOfBaseTypes.MoreIntegers))] IList<int> moreIntegers) + { + this.integers = integers; + this.doubles = doubles; + this.strings = strings; + this.moreIntegers = moreIntegers; + } + + public override bool Equals(object o) + { + if (this == o) + { + return true; + } + + if (o == null || !(o is ListOfBaseTypes)) + { + return false; + } + + ListOfBaseTypes that = (ListOfBaseTypes)o; + + if (!Utilities.Utilities.Equals(doubles, that.doubles)) + { + return false; + } + + if (!Utilities.Utilities.Equals(integers, that.integers)) + { + return false; + } + + if (!Utilities.Utilities.Equals(strings, that.strings)) + { + return false; + } + + return true; + } + + public override int GetHashCode() + { + int result = integers.GetHashCode(); + result = (31 * result) + doubles.GetHashCode(); + result = (31 * result) + strings.GetHashCode(); + return result; + } + + [NamedParameter] + public class Integers : Name<IList<int>> + { + } + + [NamedParameter(DefaultValues = new string[] { "1", "2", "3" })] + public class MoreIntegers : Name<IList<int>> + { + } + + [NamedParameter] + public class Doubles : Name<IList<double>> + { + } + + [NamedParameter] + public class Strings : Name<IList<string>> + { + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/SmokeTest/ObjectTreeTest.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/SmokeTest/ObjectTreeTest.cs b/lang/cs/Tests/TangTests/SmokeTest/ObjectTreeTest.cs new file mode 100644 index 0000000..801a61b --- /dev/null +++ b/lang/cs/Tests/TangTests/SmokeTest/ObjectTreeTest.cs @@ -0,0 +1,54 @@ +/** + * 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.Implementations; +using Org.Apache.Reef.Tang.Interface; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Org.Apache.Reef.Tang.Test.SmokeTest +{ + [TestClass] + public class ObjectTreeTest + { + public static IConfiguration GetConfiguration() + { + return TestConfigurationModuleBuilder.CONF + .Set(TestConfigurationModuleBuilder.OPTIONALSTRING, TestConfigurationModuleBuilder.OptionaStringValue) + .Set(TestConfigurationModuleBuilder.REQUIREDSTRING, TestConfigurationModuleBuilder.RequiredStringValue) + .Build(); + } + + [TestMethod] + public void TestInstantiation() + { + IRootInterface root = TangFactory.GetTang().NewInjector(GetConfiguration()).GetInstance<IRootInterface>(); + Assert.IsTrue(root.IsValid(), "Object instantiation left us in an inconsistent state."); + } + + [TestMethod] + public void TestTwoInstantiations() + { + IRootInterface firstRoot = TangFactory.GetTang().NewInjector(GetConfiguration()).GetInstance<IRootInterface>(); + IRootInterface secondRoot = TangFactory.GetTang().NewInjector(GetConfiguration()).GetInstance<IRootInterface>(); + + Assert.AreNotSame(firstRoot, secondRoot, "Two instantiations of the object tree should not be the same"); + Assert.AreEqual(firstRoot, secondRoot, "Two instantiations of the object tree should be equal"); + } + } +}
