http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/ClassHierarchy/TestClassHierarchy.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/ClassHierarchy/TestClassHierarchy.cs b/lang/cs/Tests/TangTests/ClassHierarchy/TestClassHierarchy.cs new file mode 100644 index 0000000..49f9802 --- /dev/null +++ b/lang/cs/Tests/TangTests/ClassHierarchy/TestClassHierarchy.cs @@ -0,0 +1,717 @@ +/** + * 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.Exceptions; +using Org.Apache.Reef.Tang.Implementations; +using Org.Apache.Reef.Tang.Interface; +using Org.Apache.Reef.Tang.Types; +using Org.Apache.Reef.Tang.Util; +using System; +using System.Collections.Generic; + +namespace Org.Apache.Reef.Tang.Test.ClassHierarchy +{ + [TestClass] + public class TestClassHierarchy + { + public static IClassHierarchy ns; + + [ClassInitialize] + public static void ClassSetup(TestContext context) + { + TangImpl.Reset(); + ns = TangFactory.GetTang().GetClassHierarchy(new string[] { FileNames.Examples, FileNames.Common, FileNames.Tasks }); + } + + [ClassCleanup] + public static void ClassCleanup() + { + } + + [TestInitialize()] + public void TestSetup() + { + } + + [TestCleanup()] + public void TestCleanup() + { + } + + [TestMethod] + public void TestString() + { + INode n = null; + try + { + n = ns.GetNode("System"); + } + catch (ApplicationException) + { + } + catch (NameResolutionException) + { + } + Assert.IsNull(n); + + Assert.IsNotNull(ns.GetNode(typeof(System.String).AssemblyQualifiedName)); + + string msg = null; + try + { + ns.GetNode("org.apache"); + msg = "Didn't get expected exception"; + } + catch (ApplicationException) + { + } + catch (NameResolutionException) + { + + } + Assert.IsNull(msg, msg); + } + + [TestMethod] + public void TestInt() + { + INode n = null; + try + { + n = ns.GetNode("System"); + } + catch (ApplicationException) + { + } + catch (NameResolutionException) + { + } + Assert.IsNull(n); + + Assert.IsNotNull(ns.GetNode(typeof(System.Int32).AssemblyQualifiedName)); + + string msg = null; + try + { + ns.GetNode("org.apache"); + msg = "Didn't get expected exception"; + } + catch (ApplicationException) + { + } + catch (NameResolutionException) + { + + } + Assert.IsNull(msg, msg); + } + + [TestMethod] + public void TestSimpleConstructors() + { + IClassNode cls = (IClassNode)ns.GetNode(typeof(SimpleConstructors).AssemblyQualifiedName); + Assert.IsTrue(cls.GetChildren().Count == 0); + IList<IConstructorDef> def = cls.GetInjectableConstructors(); + Assert.AreEqual(3, def.Count); + } + + [TestMethod] + public void TestTimer() + { + IClassNode timerClassNode = (IClassNode)ns.GetNode(typeof(Timer).AssemblyQualifiedName); + INode secondNode = ns.GetNode(typeof(Timer.Seconds).AssemblyQualifiedName); + Assert.AreEqual(secondNode.GetFullName(), ReflectionUtilities.GetAssemblyQualifiedName(typeof(Timer.Seconds))); + + } + + [TestMethod] + public void TestNamedParameterConstructors() + { + var node = ns.GetNode(typeof(NamedParameterConstructors).AssemblyQualifiedName); + Assert.AreEqual(node.GetFullName(), ReflectionUtilities.GetAssemblyQualifiedName(typeof(NamedParameterConstructors))); + } + + [TestMethod] + public void TestArray() + { + Type t = (new string[0]).GetType(); + INode node = ns.GetNode(t.AssemblyQualifiedName); + Assert.AreEqual(node.GetFullName(), t.AssemblyQualifiedName); + } + + [TestMethod] + public void TestRepeatConstructorArg() + { + TestNegativeCase(typeof(RepeatConstructorArg), + "Repeated constructor parameter detected. Cannot inject constructor RepeatConstructorArg(int,int)."); + } + + [TestMethod] + public void TestRepeatConstructorArgClasses() + { + TestNegativeCase(typeof(RepeatConstructorArgClasses), + "Repeated constructor parameter detected. Cannot inject constructor RepeatConstructorArgClasses(A, A)."); + } + + [TestMethod] + public void testLeafRepeatedConstructorArgClasses() + { + INode node = ns.GetNode(typeof(LeafRepeatedConstructorArgClasses).AssemblyQualifiedName); + Assert.AreEqual(node.GetFullName(), typeof(LeafRepeatedConstructorArgClasses).AssemblyQualifiedName); + } + + [TestMethod] + public void TestNamedRepeatConstructorArgClasses() + { + INode node = ns.GetNode(typeof(NamedRepeatConstructorArgClasses).AssemblyQualifiedName); + Assert.AreEqual(node.GetFullName(), typeof(NamedRepeatConstructorArgClasses).AssemblyQualifiedName); + } + + [TestMethod] + public void TestResolveDependencies() + { + ns.GetNode(typeof(SimpleConstructors).AssemblyQualifiedName); + Assert.IsNotNull(ns.GetNode(typeof(string).AssemblyQualifiedName)); + } + + [TestMethod] + public void TestDocumentedLocalNamedParameter() + { + var node = ns.GetNode(typeof(DocumentedLocalNamedParameter).AssemblyQualifiedName); + Assert.AreEqual(node.GetFullName(), ReflectionUtilities.GetAssemblyQualifiedName(typeof(DocumentedLocalNamedParameter))); + } + + [TestMethod] + public void TestNamedParameterTypeMismatch() + { + TestNegativeCase(typeof(NamedParameterTypeMismatch), + "Named parameter type mismatch in NamedParameterTypeMismatch. Constructor expects a System.String but Foo is a System.Int32."); + } + + [TestMethod] + public void TestUnannotatedName() + { + TestNegativeCase(typeof(UnannotatedName), + "Named parameter UnannotatedName is missing its [NamedParameter] attribute."); + } + + [TestMethod] + public void TestAnnotatedNotName() + { + TestNegativeCase(typeof(AnnotatedNotName), + "Found illegal [NamedParameter] AnnotatedNotName does not implement Name<T>."); + } + + [TestMethod] + public void TestAnnotatedNameWrongInterface() + { + TestNegativeCase(typeof(AnnotatedNameWrongInterface), + "Found illegal [NamedParameter] AnnotatedNameWrongInterface does not implement Name<T>."); + } + + [TestMethod] + public void TestAnnotatedNameMultipleInterfaces() + { + TestNegativeCase(typeof(AnnotatedNameMultipleInterfaces), + "Named parameter Org.Apache.Reef.Tang.Implementation.AnnotatedNameMultipleInterfaces implements multiple interfaces. It is only allowed to implement Name<T>."); + } + + [TestMethod] + public void TestUnAnnotatedNameMultipleInterfaces() + { + TestNegativeCase(typeof(UnAnnotatedNameMultipleInterfaces), + "Named parameter Org.Apache.Reef.Tang.Implementation.UnAnnotatedNameMultipleInterfaces is missing its @NamedParameter annotation."); + } + + [TestMethod] + public void TestNameWithConstructor() + { + TestNegativeCase(typeof(NameWithConstructor), + "Named parameter Org.Apache.Reef.Tang.Implementation.NameWithConstructor has a constructor. Named parameters must not declare any constructors."); + } + + [TestMethod] + public void TestNameWithZeroArgInject() + { + TestNegativeCase(typeof(NameWithZeroArgInject), + "Named parameter Org.Apache.Reef.Tang.Implementation.NameWithZeroArgInject has an injectable constructor. Named parameters must not declare any constructors."); + } + + [TestMethod] + public void TestInjectNonStaticLocalArgClass() + { + var node = ns.GetNode(typeof(InjectNonStaticLocalArgClass).AssemblyQualifiedName); + Assert.AreEqual(node.GetFullName(), typeof(InjectNonStaticLocalArgClass).AssemblyQualifiedName); + } + + [TestMethod] + public void TestInjectNonStaticLocalType() + { + var node = ns.GetNode(typeof(InjectNonStaticLocalType).AssemblyQualifiedName); + Assert.AreEqual(node.GetFullName(), typeof(InjectNonStaticLocalType).AssemblyQualifiedName); + } + + [TestMethod] + public void TestOKShortNames() + { + var node = ns.GetNode(typeof(ShortNameFooA).AssemblyQualifiedName); + Assert.AreEqual(node.GetFullName(), typeof(ShortNameFooA).AssemblyQualifiedName); + } + + public void TestConflictingShortNames() + { + string msg = null; + try + { + var nodeA = ns.GetNode(typeof(ShortNameFooA).AssemblyQualifiedName); + var nodeB = ns.GetNode(typeof(ShortNameFooB).AssemblyQualifiedName); + msg = + "ShortNameFooA and ShortNameFooB have the same short name" + + nodeA.GetName() + nodeB.GetName(); + } + catch (Exception e) + { + Console.WriteLine(e); + } + Assert.IsNull(msg, msg); + } + + [TestMethod] + public void TestRoundTripInnerClassNames() + { + INode node = ns.GetNode(typeof(Nested.Inner).AssemblyQualifiedName); + Assert.AreEqual(node.GetFullName(), typeof(Nested.Inner).AssemblyQualifiedName); + } + + [TestMethod] + public void TestRoundTripAnonInnerClassNames() + { + INode node1 = ns.GetNode(typeof(AnonNested.X1).AssemblyQualifiedName); + INode node2 = ns.GetNode(typeof(AnonNested.Y1).AssemblyQualifiedName); + Assert.AreNotEqual(node1.GetFullName(), node2.GetFullName()); + + Type t1 = ReflectionUtilities.GetTypeByName(node1.GetFullName()); + Type t2 = ReflectionUtilities.GetTypeByName(node2.GetFullName()); + + Assert.AreNotSame(t1, t2); + } + + [TestMethod] + public void TestNameCantBindWrongSubclassAsDefault() + { + TestNegativeCase(typeof(BadName), + "class org.apache.reef.tang.implementation.BadName defines a default class Int32 with a type that does not extend of its target's type string"); + } + + + [TestMethod] + public void TestNameCantBindWrongSubclassOfArgumentAsDefault() + { + TestNegativeCase(typeof(BadNameForGeneric), + "class BadNameForGeneric defines a default class Int32 with a type that does not extend of its target's string in ISet<string>"); + } + + [TestMethod] + public void TestNameCantBindSubclassOfArgumentAsDefault() + { + INode node = ns.GetNode(typeof(GoodNameForGeneric).AssemblyQualifiedName); + Assert.AreEqual(node.GetFullName(), typeof(GoodNameForGeneric).AssemblyQualifiedName); + } + + [TestMethod] + public void TestInterfaceCantBindWrongImplAsDefault() + { + TestNegativeCase(typeof(IBadIfaceDefault), + "interface IBadIfaceDefault declares its default implementation to be non-subclass class string"); + } + + private void TestNegativeCase(Type clazz, string message) + { + string msg = null; + try + { + var node = ns.GetNode(typeof(IBadIfaceDefault).AssemblyQualifiedName); + msg = message + node.GetName(); + } + catch (Exception) + { + } + Assert.IsNull(msg, msg); + } + + [TestMethod] + public void TestParseableDefaultClassNotOK() + { + TestNegativeCase(typeof(BadParsableDefaultClass), + "Named parameter BadParsableDefaultClass defines default implementation for parsable type System.string"); + } + + [TestMethod] + public void testGenericTorture1() + { + g(typeof(GenericTorture1)); + } + [TestMethod] + public void testGenericTorture2() + { + g(typeof(GenericTorture2)); + } + [TestMethod] + public void testGenericTorture3() + { + g(typeof(GenericTorture3)); + } + [TestMethod] + public void testGenericTorture4() + { + g(typeof(GenericTorture4)); + } + + public string s(Type t) + { + return ReflectionUtilities.GetAssemblyQualifiedName(t); + } + public INode g(Type t) + { + INode n = ns.GetNode(s(t)); + Assert.IsNotNull(n); + return n; + } + + [TestMethod] + public void TestHelloTaskNode() + { + var node = ns.GetNode(typeof(Org.Apache.Reef.Tasks.HelloTask).AssemblyQualifiedName); + Assert.AreEqual(node.GetFullName(), ReflectionUtilities.GetAssemblyQualifiedName(typeof(Org.Apache.Reef.Tasks.HelloTask))); + } + + [TestMethod] + public void TestITackNode() + { + var node = ns.GetNode(typeof(Org.Apache.Reef.Tasks.ITask).AssemblyQualifiedName); + Assert.AreEqual(node.GetFullName(), ReflectionUtilities.GetAssemblyQualifiedName(typeof(Org.Apache.Reef.Tasks.ITask))); + } + + [TestMethod] + public void TestNamedParameterIdentifier() + { + var node = ns.GetNode(typeof(Org.Apache.Reef.Tasks.TaskConfigurationOptions.Identifier).AssemblyQualifiedName); + Assert.AreEqual(node.GetFullName(), ReflectionUtilities.GetAssemblyQualifiedName(typeof(Org.Apache.Reef.Tasks.TaskConfigurationOptions.Identifier))); + } + [TestMethod] + public void TestInterface() + { + g(typeof(A)); + g(typeof(MyInterface<int>)); + g(typeof(MyInterface<string>)); + g(typeof(B)); + + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(); + + cb.BindImplementation(GenericType<MyInterface<string>>.Class, GenericType<MyImplString>.Class); + cb.BindImplementation(GenericType<MyInterface<int>>.Class, GenericType<MyImplInt>.Class); + IConfiguration conf = cb.Build(); + IInjector i = tang.NewInjector(conf); + + var a = (A)i.GetInstance(typeof(A)); + var implString = (MyImplString)i.GetInstance(typeof(MyImplString)); + var implInt = (MyImplString)i.GetInstance(typeof(MyImplString)); + var b = (B)i.GetInstance(typeof(B)); + var c = (C)i.GetInstance(typeof(C)); + + Assert.IsNotNull(a); + Assert.IsNotNull(implString); + Assert.IsNotNull(implInt); + Assert.IsNotNull(b); + Assert.IsNotNull(c); + } + } + + [NamedParameter] + class GenericTorture1 : Name<ISet<string>> { + } + [NamedParameter] + class GenericTorture2 : Name<ISet<ISet<string>>> + { + } + [NamedParameter] + class GenericTorture3 : Name<IDictionary<ISet<string>, ISet<string>>> + { + } + [NamedParameter] + class GenericTorture4 : Name<IDictionary<string, string>> + { + } + + public interface MyInterface<T> + { + + } + + public class RepeatConstructorArg + { + [Inject] + public RepeatConstructorArg(int x, int y) + { + } + } + + public class RepeatConstructorArgClasses + { + [Inject] + public RepeatConstructorArgClasses(A x, A y) + { + } + } + + public class A : MyInterface<int>, MyInterface<string> + { + [Inject] + A() + { + } + } + + public class MyImplString : MyInterface<string> + { + [Inject] + public MyImplString() + { + } + } + + public class B + { + [Inject] + public B(MyInterface<string> b) + { + } + } + + public class MyImplInt : MyInterface<int> + { + [Inject] + public MyImplInt() + { + } + } + public class C + { + [Inject] + public C(MyInterface<int> b) + { + } + } + public class LeafRepeatedConstructorArgClasses + { + + public static class A + { + public class AA + { + } + } + + public static class B + { + public class AA + { + } + } + + public class C + { + [Inject] + public C(A.AA a, B.AA b) + { + } + } + } + + class D + { + } + [NamedParameter] + class D1 : Name<D> + { + } + + [NamedParameter] + class D2 : Name<D> + { + } + + class NamedRepeatConstructorArgClasses + { + [Inject] + public NamedRepeatConstructorArgClasses([Parameter(typeof(D1))] D x, [Parameter(typeof(D2))] D y) + { + } + } + + class NamedParameterTypeMismatch + { + [NamedParameter(Documentation = "doc.stuff", DefaultValue = "1")] + class Foo : Name<Int32> + { + } + + [Inject] + public NamedParameterTypeMismatch([Parameter(Value = typeof(Foo))] string s) + { + } + } + + class UnannotatedName : Name<string> { + } + + interface I1 + { + } + + [NamedParameter(Documentation = "c")] + class AnnotatedNotName + { + } + + [NamedParameter(Documentation = "c")] + class AnnotatedNameWrongInterface : I1 + { + } + + class UnAnnotatedNameMultipleInterfaces : Name<object>, I1 + { + } + + [NamedParameter(Documentation = "c")] + class AnnotatedNameMultipleInterfaces : Name<object>, I1 + { + } + + [NamedParameter(Documentation = "c")] + class NameWithConstructor : Name<object> + { + private NameWithConstructor(int i) + { + } + } + + [NamedParameter] + class NameWithZeroArgInject : Name<object> + { + [Inject] + public NameWithZeroArgInject() + { + } + } + + class InjectNonStaticLocalArgClass + { + class NonStaticLocal + { + } + + [Inject] + InjectNonStaticLocalArgClass(NonStaticLocal x) + { + } + } + + class InjectNonStaticLocalType + { + class NonStaticLocal + { + [Inject] + NonStaticLocal(NonStaticLocal x) + { + } + } + } + + [NamedParameter(ShortName = "foo")] + public class ShortNameFooA : Name<String> + { + } + + //when same short name is used, exception would throw when building the class hierarchy + [NamedParameter(ShortName = "foo")] + public class ShortNameFooB : Name<Int32> + { + } + + class Nested + { + public class Inner + { + } + } + + class AnonNested + { + public interface X + { + } + + public class X1 : X + { + //int i; + } + + public class Y1 : X + { + //int j; + } + + public static X XObj = new X1(); + public static X YObj = new Y1(); + } + + //Negative case: Int32 doesn't match string + [NamedParameter(DefaultClass = typeof(Int32))] + class BadName : Name<string> + { + } + + //Negative case: Int32 doesn't match string in the ISet + [NamedParameter(DefaultClass = typeof(Int32))] + class BadNameForGeneric : Name<ISet<string>> + { + } + + //Positive case: type matched. ISet is not in parsable list + [NamedParameter(DefaultClass = typeof(string))] + class GoodNameForGeneric : Name<ISet<string>> + { + } + + [DefaultImplementation(typeof(string))] + interface IBadIfaceDefault + { + } + + //negative case: type matched. However, string is in the parsable list and DefaultClass is not null. + [NamedParameter(DefaultClass = typeof(string))] + class BadParsableDefaultClass : Name<string> + { + } + } \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/ClassHierarchy/TestClassHierarchyRoundTrip.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/ClassHierarchy/TestClassHierarchyRoundTrip.cs b/lang/cs/Tests/TangTests/ClassHierarchy/TestClassHierarchyRoundTrip.cs new file mode 100644 index 0000000..e8c48f6 --- /dev/null +++ b/lang/cs/Tests/TangTests/ClassHierarchy/TestClassHierarchyRoundTrip.cs @@ -0,0 +1,69 @@ +/** + * 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.Globalization; +using System.IO; +using Org.Apache.Reef.Tang.Implementations; +using Org.Apache.Reef.Tang.Protobuf; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Org.Apache.Reef.Tang.Examples; + +namespace Org.Apache.Reef.Tang.Test.ClassHierarchy +{ + [TestClass] + public class TestClassHierarchyRoundTrip : TestClassHierarchy + { + private void setup1() + { + TangImpl.Reset(); + ns = TangFactory.GetTang().GetClassHierarchy(new string[] { FileNames.Examples, FileNames.Common, FileNames.Tasks }); + } + + private void setup2() + { + TangImpl.Reset(); + ns = new ProtocolBufferClassHierarchy(ProtocolBufferClassHierarchy.Serialize(ns)); + } + + private void setup3() + { + TangImpl.Reset(); + try + { + ProtocolBufferClassHierarchy.Serialize("testProto.bin", ns); + ns = ProtocolBufferClassHierarchy.DeSerialize("testProto.bin"); + } + catch (IOException e) + { + Assert.Fail(string.Format(CultureInfo.CurrentCulture, "IOException when serialize/deserialize proto buffer file", e)); + } + } + + //[TestMethod] + //public new void TestString() + //{ + // setup1(); + // base.TestSimpleConstructors(); + // setup2(); + // base.TestSimpleConstructors(); + // setup3(); + // base.TestSimpleConstructors(); + //} + } +} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/ClassHierarchy/TestGeneric.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/ClassHierarchy/TestGeneric.cs b/lang/cs/Tests/TangTests/ClassHierarchy/TestGeneric.cs new file mode 100644 index 0000000..80a95fe --- /dev/null +++ b/lang/cs/Tests/TangTests/ClassHierarchy/TestGeneric.cs @@ -0,0 +1,46 @@ +/** + * 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.Examples; +using Org.Apache.Reef.Tang.Implementations; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Org.Apache.Reef.Wake.RX; + +namespace Org.Apache.Reef.Tang.Test.ClassHierarchy +{ + [TestClass] + public class TestGeneric + { + [ClassInitialize] + public static void ClassSetup(TestContext context) + { + TangImpl.Reset(); + } + + [TestMethod] + public void TestGenericClassWithT() + { + List<string> appDlls = new List<string>(); + appDlls.Add(typeof(GenericArgument<>).Assembly.GetName().Name); + appDlls.Add(typeof(AbstractObserver<>).Assembly.GetName().Name); + TangFactory.GetTang().GetClassHierarchy(appDlls.ToArray()); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/ClassHierarchy/TestMultipleInterface.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/ClassHierarchy/TestMultipleInterface.cs b/lang/cs/Tests/TangTests/ClassHierarchy/TestMultipleInterface.cs new file mode 100644 index 0000000..3567a4b --- /dev/null +++ b/lang/cs/Tests/TangTests/ClassHierarchy/TestMultipleInterface.cs @@ -0,0 +1,103 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +using System; +using Org.Apache.Reef.Tang.Annotations; +using Org.Apache.Reef.Tang.Implementations; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Org.Apache.Reef.Tang.Test.ClassHierarchy +{ + [TestClass] + public class TestMultipleInterface + { + [TestMethod] + public void TestFoo() + { + var ch = TangFactory.GetTang().GetDefaultClassHierarchy(); + var fNode = ch.GetNode(typeof(Org.Apache.Reef.Tang.Test.ClassHierarchy.Foo)); + var bNode = ch.GetNode(typeof(Org.Apache.Reef.Tang.Test.ClassHierarchy.Bar)); + var fbNode = ch.GetNode(typeof(Org.Apache.Reef.Tang.Test.ClassHierarchy.BarFoo)); + } + } + + public class Foo : IObserver<int> + { + [Inject] + public Foo() + { + } + + public void OnCompleted() + { + throw new NotImplementedException(); + } + + public void OnError(Exception error) + { + throw new NotImplementedException(); + } + + public void OnNext(int value) + { + throw new NotImplementedException(); + } + } + + public class Bar : IObserver<string> + { + public void OnCompleted() + { + throw new NotImplementedException(); + } + + public void OnError(Exception error) + { + throw new NotImplementedException(); + } + + public void OnNext(string value) + { + throw new NotImplementedException(); + } + } + + public class BarFoo : IObserver<string>, IObserver<int> + { + public void OnCompleted() + { + throw new NotImplementedException(); + } + + public void OnError(Exception error) + { + throw new NotImplementedException(); + } + + public void OnNext(string value) + { + throw new NotImplementedException(); + } + + public void OnNext(int value) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/ClassHierarchy/TestParameterParser.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/ClassHierarchy/TestParameterParser.cs b/lang/cs/Tests/TangTests/ClassHierarchy/TestParameterParser.cs new file mode 100644 index 0000000..efd1ba7 --- /dev/null +++ b/lang/cs/Tests/TangTests/ClassHierarchy/TestParameterParser.cs @@ -0,0 +1,323 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +using System; +using Org.Apache.Reef.Tang.Annotations; +using Org.Apache.Reef.Tang.Implementations; +using Org.Apache.Reef.Tang.Interface; +using Org.Apache.Reef.Tang.Util; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Org.Apache.Reef.Tang.Test.ClassHierarchy +{ + [TestClass] + public class TestParameterParser + { + [TestMethod] + public void ParseIntTest() + { + var parser = new ParameterParser(); + Int32 o = (Int32)parser.Parse(typeof(Int32), "4"); + + } + + [TestMethod] + public void ParseBoolTest() + { + var parser = new ParameterParser(); + Boolean o = (Boolean)parser.Parse(typeof(Boolean), "false"); + } + + [TestMethod] + public void ParseLongTest() + { + var parser = new ParameterParser(); + long o = (long)parser.Parse(typeof(long), "8675309"); + } + + [TestMethod] + public void ParseStringTest() + { + var parser = new ParameterParser(); + string o = (string)parser.Parse(typeof(string), "hello"); + } + + [TestMethod] + public void ParseDoubleTest() + { + var parser = new ParameterParser(); + Double o = (Double)parser.Parse(typeof(double), "12.6"); + } + + [TestMethod] + public void ParseCharTest() + { + var parser = new ParameterParser(); + Char o = (Char)parser.Parse(typeof(char), "c"); + } + + [TestMethod] + public void ParseByteTest() + { + var parser = new ParameterParser(); + Byte o = (Byte)parser.Parse(typeof(byte), "8"); + } + + [TestMethod] + public void ParseShortTest() + { + var parser = new ParameterParser(); + Int16 o = (Int16)parser.Parse(typeof(short), "8"); + } + + [TestMethod] + public void ParseFloatTest() + { + var parser = new ParameterParser(); + Single o = (Single)parser.Parse(typeof(float), "8.567"); + } + + [TestMethod] + public void ParseByteArrayTest() + { + var parser = new ParameterParser(); + byte[] o = (byte[])parser.Parse(typeof(byte[]), "hello"); + } + + [TestMethod] + public void ParameterParserTest() + { + ParameterParser p = new ParameterParser(); + p.AddParser(typeof(FooParser)); + Foo f = (Foo)p.Parse(typeof(Foo), "woot"); + Assert.AreEqual(f.s, "woot"); + } + + [TestMethod] + public void TestUnregisteredParameterParser() + { + ParameterParser p = new ParameterParser(); + + //p.AddParser(typeof(FooParser)); + Foo f = null; + try + { + f = (Foo) p.Parse(typeof (Foo), "woot"); + } + catch (NotSupportedException) + { + } + Assert.IsNull(f); + } + + [TestMethod] + public void TestReturnSubclass() + { + ParameterParser p = new ParameterParser(); + p.AddParser(typeof(BarParser)); + Bar f = (Bar)p.Parse(typeof(Foo), "woot"); + Assert.AreEqual(f.s, "woot"); + } + + [TestMethod] + public void TestGoodMerge() + { + ParameterParser old = new ParameterParser(); + old.AddParser(typeof(BarParser)); + ParameterParser nw = new ParameterParser(); + nw.MergeIn(old); + Bar f = (Bar)nw.Parse(typeof(Foo), "woot"); + Assert.AreEqual(f.s, "woot"); + } + + [TestMethod] + public void TestGoodMerge2() + { + ParameterParser old = new ParameterParser(); + old.AddParser(typeof(BarParser)); + ParameterParser nw = new ParameterParser(); + nw.AddParser(typeof(BarParser)); + nw.MergeIn(old); + Bar f = (Bar)nw.Parse(typeof(Foo), "woot"); + Assert.AreEqual(f.s, "woot"); + } + + [TestMethod] + public void TestBadMerge() + { + string msg = null; + try + { + ParameterParser old = new ParameterParser(); + old.AddParser(typeof(BarParser)); + ParameterParser nw = new ParameterParser(); + nw.AddParser(typeof(FooParser)); + nw.MergeIn(old); + msg = "Conflict detected when merging parameter parsers! To parse org.apache.reef.tang.implementation.java.TestParameterParser$Foo I have a: TestParameterParser$FooParser the other instance has a: TestParameterParser$BarParser"; + } + catch (ArgumentException) + { + } + Assert.IsNull(msg); + } + + [TestMethod] + public void testEndToEnd() + { + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new Type[] {typeof(BarParser) }); + cb.BindNamedParameter<SomeNamedFoo, Foo>(GenericType<SomeNamedFoo>.Class, "hdfs://woot"); + ILikeBars ilb = tang.NewInjector(cb.Build()).GetInstance<ILikeBars>(); + Assert.IsNotNull(ilb); + } + + [TestMethod] + public void TestDelegatingParser() + { + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { }, new IConfiguration[] { }, new Type[] { typeof(TypeParser) }); + + ICsConfigurationBuilder cb2 = tang.NewConfigurationBuilder(new IConfiguration[] { cb.Build() }); + + cb2.BindNamedParameter<ParseName, ParseableType>(GenericType<ParseName>.Class, "a"); //ParseName : Name<ParseableType> + + ParseableType t = (ParseableType)tang.NewInjector(cb2.Build()).GetNamedInstance(typeof(ParseName)); + Assert.IsTrue(t is ParseTypeA); + + cb2 = tang.NewConfigurationBuilder(cb.Build()); + cb2.BindNamedParameter<ParseNameB, ParseTypeB>(GenericType<ParseNameB>.Class, "b"); //ParseNameB : Name<ParseTypeB : ParseableType> + cb2.BindNamedParameter<ParseNameA, ParseableType>(GenericType<ParseNameA>.Class, "a"); //ParseNameA : Name<ParseableType> + + tang.NewInjector(cb2.Build()).GetInstance(typeof(NeedsA)); + tang.NewInjector(cb2.Build()).GetInstance(typeof(NeedsB)); + } + + class FooParser : IExternalConstructor<Foo> + { + private Foo foo; + [Inject] + public FooParser(string s) + { + this.foo = new Foo(s); + } + + public Foo NewInstance() + { + return foo; + } + } + + class BarParser : IExternalConstructor<Foo> + { + private Bar bar; + [Inject] + public BarParser(String s) + { + this.bar = new Bar(s); + } + public Foo NewInstance() + { + return bar; + } + } + + class Foo + { + public readonly string s; + public Foo(string s) { this.s = s; } + } + class Bar : Foo + { + public Bar(string s) : base(s) { } + } + + [NamedParameter] + class SomeNamedFoo : Name<Foo> { } + + class ILikeBars + { + [Inject] + ILikeBars([Parameter(typeof(SomeNamedFoo))] Foo bar) + { + Bar b = (Bar)bar; + Assert.AreEqual(b.s, "hdfs://woot"); + } + } + + class ParseableType + { + } + + class ParseTypeA : ParseableType + { + } + + class ParseTypeB : ParseableType + { + } + + class TypeParser : IExternalConstructor<ParseableType> + { + ParseableType instance; + [Inject] + public TypeParser(String s) + { + if (s.Equals("a")) { instance = new ParseTypeA(); } + if (s.Equals("b")) { instance = new ParseTypeB(); } + } + + public ParseableType NewInstance() + { + return instance; + } + } + + [NamedParameter] + class ParseName : Name<ParseableType> + { + } + + [NamedParameter] + class ParseNameA : Name<ParseableType> + { + } + + [NamedParameter] + class ParseNameB : Name<ParseTypeB> + { + } + + class NeedsA + { + [Inject] + public NeedsA([Parameter(typeof(ParseNameA))] ParseableType a) + { + Assert.IsTrue(a is ParseTypeA); + } + } + + class NeedsB + { + [Inject] + public NeedsB([Parameter(typeof(ParseNameB))] ParseTypeB b) + { + Assert.IsTrue(b is ParseTypeB); + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/ClassHierarchy/TestSerilization.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/ClassHierarchy/TestSerilization.cs b/lang/cs/Tests/TangTests/ClassHierarchy/TestSerilization.cs new file mode 100644 index 0000000..9313fb1 --- /dev/null +++ b/lang/cs/Tests/TangTests/ClassHierarchy/TestSerilization.cs @@ -0,0 +1,234 @@ +/** + * 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.Reflection; +using Org.Apache.Reef.Tasks; +using Org.Apache.Reef.Tang.Implementations; +using Org.Apache.Reef.Tang.Interface; +using Org.Apache.Reef.Tang.Protobuf; +using Org.Apache.Reef.Tang.Types; +using Org.Apache.Reef.Tang.Util; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Org.Apache.Reef.Tang.Examples; + +namespace Org.Apache.Reef.Tang.Test.ClassHierarchy +{ + [TestClass] + public class TestSerilization + { + static Assembly asm = null; + + [ClassInitialize] + public static void ClassSetup(TestContext context) + { + asm = Assembly.Load(FileNames.Examples); + Assembly.Load(FileNames.Examples); + } + + [ClassCleanup] + public static void ClassCleanup() + { + } + + [TestInitialize()] + public void TestSetup() + { + } + + [TestCleanup()] + public void TestCleanup() + { + } + + [TestMethod] + public void TestSerializeClassHierarchy() + { + IClassHierarchy ns = TangFactory.GetTang().GetClassHierarchy(new string[] { typeof(Timer).Assembly.GetName().Name }); + ProtocolBufferClassHierarchy.Serialize("node.bin", ns); + } + + [TestMethod] + public void TestDeSerializeClassHierarchy() + { + Type timerType = typeof (Timer); + Type SecondType = typeof (Timer.Seconds); + Type simpleCOnstuctorType = typeof (SimpleConstructors); + + IClassHierarchy ns = TangFactory.GetTang().GetClassHierarchy(new string[] { typeof(Timer).Assembly.GetName().Name }); + IClassNode timerClassNode = (IClassNode)ns.GetNode(timerType.AssemblyQualifiedName); + INode secondNode = (INode)ns.GetNode(SecondType.AssemblyQualifiedName); + IClassNode SimpleConstructorsClassNode = (IClassNode)ns.GetNode(simpleCOnstuctorType.AssemblyQualifiedName); + + ProtocolBufferClassHierarchy.Serialize("node.bin", ns); + IClassHierarchy ch = ProtocolBufferClassHierarchy.DeSerialize("node.bin"); + + IClassNode timerClassNode2 = (IClassNode)ch.GetNode(timerType.AssemblyQualifiedName); + INode secondNode2 = ch.GetNode(SecondType.AssemblyQualifiedName); + IClassNode SimpleConstructorsClassNode2 = (IClassNode)ch.GetNode(simpleCOnstuctorType.AssemblyQualifiedName); + + Assert.AreEqual(timerClassNode.GetFullName(), timerClassNode2.GetFullName()); + Assert.AreEqual(secondNode.GetFullName(), secondNode2.GetFullName()); + Assert.AreEqual(SimpleConstructorsClassNode.GetFullName(), SimpleConstructorsClassNode2.GetFullName()); + + Assert.IsTrue(SimpleConstructorsClassNode2.GetChildren().Count == 0); + IList<IConstructorDef> def = SimpleConstructorsClassNode2.GetInjectableConstructors(); + Assert.AreEqual(3, def.Count); + } + + [TestMethod] + public void TestDeSerializeClassHierarchyForTask() + { + Type streamTask1Type = typeof (Org.Apache.Reef.Tasks.StreamTask1); + Type helloTaskType = typeof (Org.Apache.Reef.Tasks.HelloTask); + + IClassHierarchy ns = TangFactory.GetTang().GetClassHierarchy(new string[] { typeof(Org.Apache.Reef.Tasks.HelloTask).Assembly.GetName().Name }); + IClassNode StreamTask1ClassNode = (IClassNode)ns.GetNode(streamTask1Type.AssemblyQualifiedName); + IClassNode HelloTaskClassNode = (IClassNode)ns.GetNode(helloTaskType.AssemblyQualifiedName); + + ProtocolBufferClassHierarchy.Serialize("task.bin", ns); + IClassHierarchy ch = ProtocolBufferClassHierarchy.DeSerialize("task.bin"); + IClassNode StreamTask1ClassNode2 = (IClassNode)ch.GetNode(streamTask1Type.AssemblyQualifiedName); + IClassNode HelloTaskClassNode2 = (IClassNode)ch.GetNode(helloTaskType.AssemblyQualifiedName); + + Assert.AreEqual(StreamTask1ClassNode.GetFullName(), StreamTask1ClassNode2.GetFullName()); + Assert.AreEqual(HelloTaskClassNode.GetFullName(), HelloTaskClassNode2.GetFullName()); + } + + [TestMethod] + [DeploymentItem(@".")] + public void TestDeSerializeClassHierarchyFromJava() + { + //the file comes from Java TestClassHierarchyRoundTrip SetUp3 testSimpleConstructors + IClassHierarchy ch = ProtocolBufferClassHierarchy.DeSerialize("simpleConstructorJavaProto.bin"); + IClassNode simpleConstructorNode = (IClassNode)ch.GetNode("org.apache.reef.tang.implementation.SimpleConstructors"); + Assert.AreEqual(simpleConstructorNode.GetChildren().Count, 0); + Assert.AreEqual(simpleConstructorNode.GetInjectableConstructors().Count, 3); + } + + [TestMethod] + public void TestSerializeClassHierarchyForAvro() + { + IClassHierarchy ns = TangFactory.GetTang().GetClassHierarchy(new string[] { typeof(Microsoft.Hadoop.Avro.AvroSerializer).Assembly.GetName().Name }); + Assert.IsNotNull(ns); + ProtocolBufferClassHierarchy.Serialize("avro.bin", ns); + IClassHierarchy ch = ProtocolBufferClassHierarchy.DeSerialize("avro.bin"); + Assert.IsNotNull(ch); + } + + [TestMethod] + public void TestDeSerializeClassHierarchyAndBind() + { + Type streamTask1Type = typeof(Org.Apache.Reef.Tasks.StreamTask1); + Type helloTaskType = typeof(Org.Apache.Reef.Tasks.HelloTask); + + IClassHierarchy ns = TangFactory.GetTang().GetClassHierarchy(new string[] { typeof(Org.Apache.Reef.Tasks.HelloTask).Assembly.GetName().Name }); + IClassNode StreamTask1ClassNode = (IClassNode)ns.GetNode(streamTask1Type.AssemblyQualifiedName); + IClassNode HelloTaskClassNode = (IClassNode)ns.GetNode(helloTaskType.AssemblyQualifiedName); + + ProtocolBufferClassHierarchy.Serialize("task.bin", ns); + IClassHierarchy ch = ProtocolBufferClassHierarchy.DeSerialize("task.bin"); + IClassNode StreamTask1ClassNode2 = (IClassNode)ch.GetNode(streamTask1Type.AssemblyQualifiedName); + IClassNode HelloTaskClassNode2 = (IClassNode)ch.GetNode(helloTaskType.AssemblyQualifiedName); + + Assert.AreEqual(StreamTask1ClassNode.GetName(), StreamTask1ClassNode2.GetName()); + Assert.AreEqual(HelloTaskClassNode.GetName(), HelloTaskClassNode2.GetName()); + + //have to use original class hierarchy for the merge. ClassHierarchy from ProtoBuffer doesn't support merge. + IConfigurationBuilder cb = TangFactory.GetTang() + .NewConfigurationBuilder(ns); + cb.AddConfiguration(TaskConfiguration.ConfigurationModule + .Set(TaskConfiguration.Identifier, "Hello_From_Streaming1") + .Set(TaskConfiguration.Task, GenericType<Org.Apache.Reef.Tasks.StreamTask1>.Class) + .Build()); + + IConfiguration taskConfiguration = cb.Build(); + StreamTask1 st = TangFactory.GetTang().NewInjector(taskConfiguration).GetInstance<StreamTask1>(); + Assert.IsNotNull(st); + } + + [TestMethod] + public void TestSerirializeInjectionPlanForTimer() + { + Type timerType = typeof(Timer); + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Examples }); + cb.BindNamedParameter<Timer.Seconds, Int32>(GenericType < Timer.Seconds>.Class, "2"); + IConfiguration conf = cb.Build(); + IInjector injector = tang.NewInjector(conf); + InjectionPlan ip = injector.GetInjectionPlan(timerType); + ProtocolBufferInjectionPlan.Serialize("timerplan.bin", ip); + var ch = conf.GetClassHierarchy(); + var ip1 = ProtocolBufferInjectionPlan.DeSerialize("timerplan.bin", ch); + Assert.IsNotNull(ip1); + } + + [TestMethod] + public void TestSerirializeInjectionPlanForSimpleConstructor() + { + 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); + InjectionPlan ip = injector.GetInjectionPlan(simpleConstructorType); + + ProtocolBufferInjectionPlan.Serialize("plan.bin", ip); + var ch = conf.GetClassHierarchy(); + var ipRecovered = ProtocolBufferInjectionPlan.DeSerialize("plan.bin", ch); + Assert.IsNotNull(ipRecovered); + } + + [TestMethod] + public void TestGenericClass() + { + IClassHierarchy ns = TangFactory.GetTang().GetClassHierarchy(new string[] { typeof(Timer).Assembly.GetName().Name }); + + Type t = typeof(Timer); + IClassNode EventClassNode = (IClassNode)ns.GetNode(t.AssemblyQualifiedName); + ProtocolBufferClassHierarchy.Serialize("event.bin", ns); + IClassHierarchy ch = ProtocolBufferClassHierarchy.DeSerialize("event.bin"); + IClassNode EventClassNode1 = (IClassNode)ns.GetNode(t.AssemblyQualifiedName); + Assert.AreEqual(EventClassNode.GetName(), EventClassNode1.GetName()); + } + + [TestMethod] + public void TestGenericArgument() + { + IClassHierarchy ns = TangFactory.GetTang().GetClassHierarchy(new string[] { typeof(ClassWithGenericArgument<>).Assembly.GetName().Name }); + + Type t = typeof(ClassWithGenericArgument<>); + IClassNode classNode = (IClassNode)ns.GetNode(t.AssemblyQualifiedName); + var cons = classNode.GetAllConstructors(); + foreach (var c in cons) + { + var args = c.GetArgs(); + foreach (var a in args) + { + Assert.IsNotNull(a.GetName()); + } + } + ProtocolBufferClassHierarchy.Serialize("generic.bin", ns); + IClassHierarchy ch = ProtocolBufferClassHierarchy.DeSerialize("generic.bin"); + IClassNode classNode1 = (IClassNode)ns.GetNode(t.AssemblyQualifiedName); + Assert.AreEqual(classNode.GetName(), classNode1.GetName()); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/Configuration/TestAvroConfiguration.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/Configuration/TestAvroConfiguration.cs b/lang/cs/Tests/TangTests/Configuration/TestAvroConfiguration.cs new file mode 100644 index 0000000..7f7a80d --- /dev/null +++ b/lang/cs/Tests/TangTests/Configuration/TestAvroConfiguration.cs @@ -0,0 +1,68 @@ +/** + * 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.Tasks; +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.Configuration +{ + [TestClass] + public class TestAvroConfiguration + { + [TestMethod] + public void TestFromJsonString() + { + IConfigurationSerializer serializerImpl = (IConfigurationSerializer)TangFactory.GetTang().NewInjector().GetInstance(typeof(IConfigurationSerializer)); + + ICsConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(); + cb.BindImplementation(GenericType<ITask>.Class, GenericType<HelloTask>.Class); + IConfiguration conf = cb.Build(); + string jsonStr = serializerImpl.ToString(conf); + + IConfiguration c = serializerImpl.FromString(jsonStr); + Assert.IsNotNull(c); + + string jsonStr2 = serializerImpl.ToString(c); + + IConfiguration c1 = serializerImpl.FromString(jsonStr2); + Assert.IsNotNull(c1); + } + + private AvroConfiguration ToAvroConfiguration() + { + var a = new AvroConfiguration(); + HashSet<ConfigurationEntry> b = new HashSet<ConfigurationEntry>(); + ConfigurationEntry e1 = new ConfigurationEntry(); + e1.key = "a"; + e1.value = "a1"; + ConfigurationEntry e2 = new ConfigurationEntry(); + e2.key = "b"; + e2.value = "b1=b2"; + b.Add(e1); + b.Add(e2); + a.Bindings = b; + return a; + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/Configuration/TestAvroSerializerRoundTrip.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/Configuration/TestAvroSerializerRoundTrip.cs b/lang/cs/Tests/TangTests/Configuration/TestAvroSerializerRoundTrip.cs new file mode 100644 index 0000000..37c583d --- /dev/null +++ b/lang/cs/Tests/TangTests/Configuration/TestAvroSerializerRoundTrip.cs @@ -0,0 +1,71 @@ +/** + * 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.Interface; +using Org.Apache.Reef.Tang.Test.SmokeTest; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Org.Apache.Reef.Tang.Test.Configuration +{ + internal class TestAvroSerializerRoundTrip + { + [TestClass] + public class AvroConfigurationTest : RoundTripTest + { + public override IConfiguration RoundTrip(IConfiguration configuration) + { + AvroConfiguration aConf = new AvroConfigurationSerializer().ToAvroConfiguration(configuration); + return new AvroConfigurationSerializer().FromAvro(aConf); + } + } + + [TestClass] + public class ByteArrayTest : RoundTripTest + { + public override IConfiguration RoundTrip(IConfiguration configuration) + { + AvroConfigurationSerializer serializer = new AvroConfigurationSerializer(); + byte[] theBytes = serializer.ToByteArray(configuration); + return serializer.FromByteArray(theBytes); + } + } + + [TestClass] + public class FileTest : RoundTripTest + { + public override IConfiguration RoundTrip(IConfiguration configuration) + { + AvroConfigurationSerializer serializer = new AvroConfigurationSerializer(); + serializer.ToFile(configuration, "TangTest.avroconf"); + return serializer.FromFile("TangTest.avroconf"); + } + } + + [TestClass] + public class StringTest : RoundTripTest + { + public override IConfiguration RoundTrip(IConfiguration configuration) + { + AvroConfigurationSerializer serializer = new AvroConfigurationSerializer(); + return serializer.FromString(serializer.ToString(configuration)); + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/2ae282de/lang/cs/Tests/TangTests/Configuration/TestConfiguration.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Tests/TangTests/Configuration/TestConfiguration.cs b/lang/cs/Tests/TangTests/Configuration/TestConfiguration.cs new file mode 100644 index 0000000..1e96cc3 --- /dev/null +++ b/lang/cs/Tests/TangTests/Configuration/TestConfiguration.cs @@ -0,0 +1,566 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +using System; +using System.Collections.Generic; +using System.Reflection; +using Org.Apache.Reef.Tasks; +using Org.Apache.Reef.Tang.Annotations; +using Org.Apache.Reef.Tang.Examples; +using Org.Apache.Reef.Tang.Exceptions; +using Org.Apache.Reef.Tang.Formats; +using Org.Apache.Reef.Tang.Implementations; +using Org.Apache.Reef.Tang.Implementations.Configuration; +using Org.Apache.Reef.Tang.Interface; +using Org.Apache.Reef.Tang.Protobuf; +using Org.Apache.Reef.Tang.Util; +using Org.Apache.Reef.Tang.Test.ScenarioTest; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Org.Apache.Reef.Tang.Test.Configuration +{ + [TestClass] + public class TestConfiguration + { + [ClassInitialize] + public static void ClassSetup(TestContext context) + { + } + + [TestMethod] + public void TestDeserializedConfigMerge() + { + Type activityInterfaceType = typeof(ITask); + ITang tang = TangFactory.GetTang(); + + ICsConfigurationBuilder cb1 = tang.NewConfigurationBuilder(); + cb1.BindImplementation(GenericType<ITask>.Class, GenericType<HelloTask>.Class); + cb1.BindNamedParameter<TaskConfigurationOptions.Identifier, string>( + GenericType<TaskConfigurationOptions.Identifier>.Class, "Hello Task"); + IConfiguration conf1 = cb1.Build(); + var serializer = new AvroConfigurationSerializer(); + serializer.ToFile(conf1, "task.config"); + + ICsConfigurationBuilder cb2 = tang.NewConfigurationBuilder(); + cb2.BindNamedParameter<Timer.Seconds, Int32>(GenericType<Timer.Seconds>.Class, "2"); + IConfiguration conf2 = cb2.Build(); + serializer.ToFile(conf2, "timer.config"); + + ProtocolBufferClassHierarchy.Serialize("TaskTimer.bin", conf1.GetClassHierarchy()); + IClassHierarchy ns = ProtocolBufferClassHierarchy.DeSerialize("TaskTimer.bin"); + + AvroConfiguration taskAvroconfiguration = serializer.AvroDeseriaizeFromFile("task.config"); + IConfiguration taskConfiguration = serializer.FromAvro(taskAvroconfiguration, ns); + + AvroConfiguration timerAvroconfiguration = serializer.AvroDeseriaizeFromFile("timer.config"); + IConfiguration timerConfiguration = serializer.FromAvro(timerAvroconfiguration, ns); + + IConfiguration merged = Configurations.MergeDeserializedConfs(taskConfiguration, timerConfiguration); + + var b = merged.newBuilder().Build(); + } + + [TestMethod] + public void TestActivityConfiguration() + { + Type activityInterfaceType = typeof (ITask); + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Common, FileNames.Tasks }); + cb.BindImplementation(GenericType<ITask>.Class, GenericType<HelloTask>.Class); + cb.BindNamedParameter<TaskConfigurationOptions.Identifier, string>( + GenericType<TaskConfigurationOptions.Identifier>.Class, "Hello Task"); + IConfiguration conf = cb.Build(); + + ConfigurationFile.WriteConfigurationFile(conf, "TaskConf.txt"); + IDictionary<string, string> p = ConfigurationFile.FromFile("TaskConf.txt"); + + ITang tang1 = TangFactory.GetTang(); + ICsConfigurationBuilder cb1 = tang1.NewConfigurationBuilder(new string[] { FileNames.Common, FileNames.Tasks }); + ConfigurationFile.AddConfigurationFromFile(cb1, "TaskConf.txt"); + IConfiguration conf1 = cb1.Build(); + + IInjector injector = tang1.NewInjector(conf1); + var activityRef = (ITask) injector.GetInstance(activityInterfaceType); + Assert.IsNotNull(activityRef); + } + + [TestMethod] + public void TestMultipleConfiguration() + { + Type activityInterfaceType = typeof (ITask); + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Common, FileNames.Tasks }); + cb.BindImplementation(GenericType<ITask>.Class, GenericType<HelloTask>.Class); + cb.BindNamedParameter<TaskConfigurationOptions.Identifier, string>( + GenericType<TaskConfigurationOptions.Identifier>.Class, "Hello Task"); + IConfiguration conf = cb.Build(); + + IConfiguration httpConfiguraiton = HttpHandlerConfiguration.CONF + .Set(HttpHandlerConfiguration.P, GenericType<HttpServerReefEventHandler>.Class) + .Set(HttpHandlerConfiguration.P, GenericType<HttpServerNrtEventHandler>.Class) + .Build(); + + IInjector injector = TangFactory.GetTang().NewInjector(new IConfiguration[] {conf, httpConfiguraiton}); + var activityRef = (ITask) injector.GetInstance(activityInterfaceType); + Assert.IsNotNull(activityRef); + + RuntimeClock clock = injector.GetInstance<RuntimeClock>(); + var rh = clock.ClockRuntimeStartHandler.Get(); + Assert.AreEqual(rh.Count, 1); + } + + [TestMethod] + public void TestActivityConfigWithSeperateAssembly() + { + Type activityInterfaceType = typeof (ITask); + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Common, FileNames.Tasks }); + cb.BindImplementation(GenericType<ITask>.Class, GenericType<HelloTask>.Class); + IConfiguration conf = cb.Build(); + + ConfigurationFile.WriteConfigurationFile(conf, "TaskConf1.txt"); + IDictionary<string, string> p = ConfigurationFile.FromFile("TaskConf1.txt"); + + IInjector injector = tang.NewInjector(new string[] { FileNames.Common, FileNames.Tasks }, "TaskConf1.txt"); + var activityRef = (ITask) injector.GetInstance(activityInterfaceType); + + //combined line sample + var o = (ITask) TangFactory.GetTang() + .NewInjector(new string[] { FileNames.Common, FileNames.Tasks }, "TaskConf1.txt") + .GetInstance(typeof (ITask)); + + Assert.IsNotNull(activityRef); + } + + [TestMethod] + public void TestGetConfgiFromProtoBufClassHierarchy() + { + Type iTaskType = typeof(Org.Apache.Reef.Tasks.ITask); + Type helloTaskType = typeof(Org.Apache.Reef.Tasks.HelloTask); + Type identifierType = typeof (TaskConfigurationOptions.Identifier); + + IClassHierarchy ns = TangFactory.GetTang().GetClassHierarchy(new string[] { FileNames.Common, FileNames.Tasks }); + ProtocolBufferClassHierarchy.Serialize("Task.bin", ns); + IClassHierarchy ch = ProtocolBufferClassHierarchy.DeSerialize("Task.bin"); + ITang tang = TangFactory.GetTang(); + IConfigurationBuilder cb = tang.NewConfigurationBuilder(ch); + cb.Bind(iTaskType.AssemblyQualifiedName, helloTaskType.AssemblyQualifiedName); + cb.Bind(identifierType.AssemblyQualifiedName, "Hello Task"); + IConfiguration conf = cb.Build(); + ConfigurationFile.WriteConfigurationFile(conf, "taskConf2.txt"); + } + + [TestMethod] + public void TestActivityConfig() + { + Type activityInterfaceType = typeof (ITask); + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Examples, FileNames.Common, FileNames.Tasks }); + cb.BindImplementation(GenericType<ITask>.Class, GenericType<HelloTask>.Class); + IConfiguration conf = cb.Build(); + ConfigurationFile.WriteConfigurationFile(conf, "TaskConf.txt"); + IDictionary<string, string> p = ConfigurationFile.FromFile("TaskConf.txt"); + + IInjector injector = tang.NewInjector(new string[] { FileNames.Common, FileNames.Tasks }, "TaskConf.txt"); + var activityRef = (ITask) injector.GetInstance(activityInterfaceType); + + Assert.IsNotNull(activityRef); + } + + [TestMethod] + public void TestActivityConfigWithString() + { + Type activityInterfaceType = typeof (ITask); + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Examples, FileNames.Common, FileNames.Tasks }); + cb.BindImplementation(GenericType<ITask>.Class, GenericType<HelloTask>.Class); + IConfiguration conf = cb.Build(); + + string s = ConfigurationFile.ToConfigurationString(conf); + ICsConfigurationBuilder cb2 = tang.NewConfigurationBuilder(new string[] { FileNames.Examples, FileNames.Common, FileNames.Tasks }); + ConfigurationFile.AddConfigurationFromString(cb2, s); + IConfiguration conf2 = cb2.Build(); + + IInjector injector = tang.NewInjector(conf2); + var activityRef = (ITask) injector.GetInstance(activityInterfaceType); + + Assert.IsNotNull(activityRef); + } + + [TestMethod] + public void TestTweetConfiguration() + { + Type tweeterType = typeof (Tweeter); + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Examples }); + 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(); + + ConfigurationFile.WriteConfigurationFile(conf, "tweeterConf.txt"); + IDictionary<string, string> p = ConfigurationFile.FromFile("tweeterConf.txt"); + ITang tang1 = TangFactory.GetTang(); + ICsConfigurationBuilder cb1 = tang1.NewConfigurationBuilder(new string[] { FileNames.Examples }); + ConfigurationFile.AddConfigurationFromFile(cb1, "tweeterConf.txt"); + IConfiguration conf1 = cb1.Build(); + + IInjector injector = tang1.NewInjector(conf1); + var tweeter = (Tweeter) injector.GetInstance(tweeterType); + tweeter.sendMessage(); + } + + [TestMethod] + public void TestTweetConfig() + { + Type tweeterType = typeof (Tweeter); + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Examples }); + 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(); + + ConfigurationFile.WriteConfigurationFile(conf, "tweeterConf.txt"); + IDictionary<string, string> p = ConfigurationFile.FromFile("tweeterConf.txt"); + + IInjector injector = tang.NewInjector(new string[] { FileNames.Examples }, "tweeterConf.txt"); + var tweeter = (Tweeter) injector.GetInstance(tweeterType); + tweeter.sendMessage(); + } + + + [TestMethod] + public void TestTweetConfigWithAvroThroughFile() + { + Type tweeterType = typeof (Tweeter); + ITang tang = TangFactory.GetTang(); + IConfiguration conf = tang.NewConfigurationBuilder(new string[] { FileNames.Examples }) + .BindImplementation(GenericType<ITweetFactory>.Class, + GenericType<MockTweetFactory>.Class) + .BindImplementation(GenericType<ISMS>.Class, GenericType<MockSMS>.Class) + .BindNamedParameter<Tweeter.PhoneNumber, long>( + GenericType<Tweeter.PhoneNumber>.Class, "8675309") + .Build(); + + var serializer = new AvroConfigurationSerializer(); + serializer.ToFileStream(conf, "tweeterConfAvro.bin"); + IConfiguration conf2 = serializer.FromFileStream("tweeterConfAvro.bin"); + + IInjector injector = tang.NewInjector(conf2); + var tweeter = (Tweeter) injector.GetInstance(tweeterType); + tweeter.sendMessage(); + } + + [TestMethod] + public void TestTweetConfigAddConfigurationFromString() + { + Type tweeterType = typeof (Tweeter); + ITang tang = TangFactory.GetTang(); + IConfiguration conf = tang.NewConfigurationBuilder(new string[] { FileNames.Examples }) + .BindImplementation(GenericType<ITweetFactory>.Class, + GenericType<MockTweetFactory>.Class) + .BindImplementation(GenericType<ISMS>.Class, GenericType<MockSMS>.Class) + .BindNamedParameter<Tweeter.PhoneNumber, long>( + GenericType<Tweeter.PhoneNumber>.Class, "8675309") + .Build(); + + ConfigurationFile.WriteConfigurationFile(conf, "tweeterConf.txt"); + string s = ConfigurationFile.ToConfigurationString(conf); + ICsConfigurationBuilder cb2 = tang.NewConfigurationBuilder(); + ConfigurationFile.AddConfigurationFromString(cb2, s); + IConfiguration conf2 = cb2.Build(); + + IInjector injector = tang.NewInjector(conf2); + var tweeter = (Tweeter) injector.GetInstance(tweeterType); + tweeter.sendMessage(); + } + + [TestMethod] + public void TestTweetConfigWithAvroSerialization() + { + Type tweeterType = typeof (Tweeter); + ITang tang = TangFactory.GetTang(); + IConfiguration conf = tang.NewConfigurationBuilder(new string[] { FileNames.Examples }) + .BindImplementation(GenericType<ITweetFactory>.Class, + GenericType<MockTweetFactory>.Class) + .BindImplementation(GenericType<ISMS>.Class, GenericType<MockSMS>.Class) + .BindNamedParameter<Tweeter.PhoneNumber, long>( + GenericType<Tweeter.PhoneNumber>.Class, "8675309") + .Build(); + + var serializer = new AvroConfigurationSerializer(); + byte[] bytes = serializer.ToByteArray(conf); + IConfiguration conf2 = serializer.FromByteArray(bytes); + + IInjector injector = tang.NewInjector(conf2); + var tweeter = (Tweeter) injector.GetInstance(tweeterType); + tweeter.sendMessage(); + } + + [TestMethod] + public void TestTweetConfigGetConfigurationFromString() + { + Type tweeterType = typeof (Tweeter); + ITang tang = TangFactory.GetTang(); + IConfiguration conf = tang.NewConfigurationBuilder(new string[] { FileNames.Examples }) + .BindImplementation(GenericType<ITweetFactory>.Class, + GenericType<MockTweetFactory>.Class) + .BindImplementation(GenericType<ISMS>.Class, GenericType<MockSMS>.Class) + .BindNamedParameter<Tweeter.PhoneNumber, long>( + GenericType<Tweeter.PhoneNumber>.Class, "8675309") + .Build(); + + ConfigurationFile.WriteConfigurationFile(conf, "tweeterConf.txt"); + string s = ConfigurationFile.ToConfigurationString(conf); + IConfiguration conf2 = ConfigurationFile.GetConfiguration(s); + + IInjector injector = tang.NewInjector(conf2); + var tweeter = (Tweeter) injector.GetInstance(tweeterType); + tweeter.sendMessage(); + } + + [TestMethod] + public void TestTweetInvalidBinding() + { + string msg = null; + try + { + TangFactory.GetTang().NewConfigurationBuilder(new string[] { FileNames.Examples }) + .BindImplementation(typeof (ITweetFactory), typeof (MockSMS)) + .Build(); + } + catch (ArgumentException e) + { + msg = e.Message; + } + Assert.IsNotNull(msg); + } + + [TestMethod] + public void TestTimerConfiguration() + { + Type timerType = typeof (Timer); + ITang tang = TangFactory.GetTang(); + ICsConfigurationBuilder cb = tang.NewConfigurationBuilder(new string[] { FileNames.Examples }); + cb.BindNamedParameter<Timer.Seconds, Int32>(GenericType<Timer.Seconds>.Class, "2"); + IConfiguration conf = cb.Build(); + + ConfigurationFile.WriteConfigurationFile(conf, "timerConf.txt"); + IDictionary<string, string> p = ConfigurationFile.FromFile("timerConf.txt"); + + ITang tang1 = TangFactory.GetTang(); + ICsConfigurationBuilder cb1 = tang1.NewConfigurationBuilder(new string[] { FileNames.Examples }); + ConfigurationFile.AddConfigurationFromFile(cb1, "timerConf.txt"); + IConfiguration conf1 = cb1.Build(); + + IInjector injector = tang.NewInjector(conf1); + var timer = (Timer) injector.GetInstance(timerType); + + Assert.IsNotNull(timer); + + timer.sleep(); + } + + [TestMethod] + public void TestDocumentLoadNamedParameterConfiguration() + { + 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(); + + ConfigurationFile.WriteConfigurationFile(conf, "docLoadConf.txt"); + IDictionary<string, string> p = ConfigurationFile.FromFile("docLoadConf.txt"); + + ITang tang1 = TangFactory.GetTang(); + ICsConfigurationBuilder cb1 = tang1.NewConfigurationBuilder(new string[] { FileNames.Examples }); + ConfigurationFile.AddConfigurationFromFile(cb1, "docLoadConf.txt"); + IConfiguration conf1 = cb1.Build(); + + IInjector injector = tang1.NewInjector(conf1); + var doc = (DocumentedLocalNamedParameter) injector.GetInstance(documentedLocalNamedParameterType); + + Assert.IsNotNull(doc); + var s = doc.ToString(); + } + + [TestMethod] + public void TestTimerConfigurationWithClassHierarchy() + { + Type timerType = typeof (Timer); + ClassHierarchyImpl classHierarchyImpl = new ClassHierarchyImpl(FileNames.Examples); + + ITang tang = TangFactory.GetTang(); + IConfiguration conf = tang.NewConfigurationBuilder(classHierarchyImpl) + .BindNamedParameter<Timer.Seconds, Int32>(GenericType<Timer.Seconds>.Class, "1") + .Build(); + + ConfigurationFile.WriteConfigurationFile(conf, "timerConfH.txt"); + IDictionary<string, string> p = ConfigurationFile.FromFile("timerConfH.txt"); + + ITang tang1 = TangFactory.GetTang(); + ICsConfigurationBuilder cb1 = tang1.NewConfigurationBuilder(new string[] { FileNames.Examples }); + ConfigurationFile.AddConfigurationFromFile(cb1, "timerConfH.txt"); + IConfiguration conf1 = cb1.Build(); + + IInjector injector = tang1.NewInjector(conf1); + var timer = (Timer) injector.GetInstance(timerType); + + Assert.IsNotNull(timer); + timer.sleep(); + } + + [TestMethod] + public void TestSetConfig() + { + IConfiguration conf = TangFactory.GetTang().NewConfigurationBuilder() + .BindSetEntry<SetOfNumbers, string>(GenericType<SetOfNumbers>.Class, "four") + .BindSetEntry<SetOfNumbers, string>(GenericType<SetOfNumbers>.Class, "five") + .BindSetEntry<SetOfNumbers, string>(GenericType<SetOfNumbers>.Class, "six") + .Build(); + + Box b = (Box) TangFactory.GetTang().NewInjector(conf).GetInstance(typeof (Box)); + ConfigurationFile.WriteConfigurationFile(conf, "SetOfNumbersConf.txt"); + + string s = ConfigurationFile.ToConfigurationString(conf); + IConfiguration conf2 = ConfigurationFile.GetConfiguration(s); + + Box b2 = (Box) TangFactory.GetTang().NewInjector(conf2).GetInstance(typeof (Box)); + ISet<string> actual = b2.Numbers; + + Assert.IsTrue(actual.Contains("four")); + Assert.IsTrue(actual.Contains("five")); + Assert.IsTrue(actual.Contains("six")); + } + + [TestMethod] + public void TestSetConfigWithAvroSerialization() + { + IConfiguration conf = TangFactory.GetTang().NewConfigurationBuilder() + .BindSetEntry<SetOfNumbers, string>(GenericType<SetOfNumbers>.Class, "four") + .BindSetEntry<SetOfNumbers, string>(GenericType<SetOfNumbers>.Class, "five") + .BindSetEntry<SetOfNumbers, string>(GenericType<SetOfNumbers>.Class, "six") + .Build(); + + Box b = (Box) TangFactory.GetTang().NewInjector(conf).GetInstance(typeof (Box)); + + var serializer = new AvroConfigurationSerializer(); + byte[] bytes = serializer.ToByteArray(conf); + IConfiguration conf2 = serializer.FromByteArray(bytes); + + Box b2 = (Box) TangFactory.GetTang().NewInjector(conf2).GetInstance(typeof (Box)); + ISet<string> actual = b2.Numbers; + + Assert.IsTrue(actual.Contains("four")); + Assert.IsTrue(actual.Contains("five")); + Assert.IsTrue(actual.Contains("six")); + } + + [TestMethod] + public void TestNullStringVaue() + { + string msg = null; + try + { + TangFactory.GetTang().NewConfigurationBuilder() + .BindNamedParameter<NamedParamterNoDefault.NamedString, string>(GenericType<NamedParamterNoDefault.NamedString>.Class, null) + .Build(); + } + catch (IllegalStateException e) + { + msg = e.Message; + } + Assert.IsNotNull(msg); + } + + [TestMethod] + public void TestSetConfigNullValue() + { + string msg = null; + try + { + TangFactory.GetTang().NewConfigurationBuilder() + .BindSetEntry<SetOfNumbersNoDefault, string>(GenericType<SetOfNumbersNoDefault>.Class, null) + .BindSetEntry<SetOfNumbersNoDefault, string>(GenericType<SetOfNumbersNoDefault>.Class, "five") + .BindSetEntry<SetOfNumbersNoDefault, string>(GenericType<SetOfNumbersNoDefault>.Class, "six") + .Build(); + } + catch (IllegalStateException e) + { + msg = e.Message; + } + Assert.IsNotNull(msg); + } + } + + [NamedParameter(DefaultValues = new string[] {"one", "two", "three"})] + class SetOfNumbers : Name<ISet<string>> + { + } + + class Box + { + public ISet<string> Numbers; + + [Inject] + Box([Parameter(typeof (SetOfNumbers))] ISet<string> numbers) + { + this.Numbers = numbers; + } + } + + [NamedParameter] + class SetOfNumbersNoDefault : Name<ISet<string>> + { + } + + class BoxNoDefault + { + public ISet<string> Numbers; + + [Inject] + BoxNoDefault([Parameter(typeof(SetOfNumbersNoDefault))] ISet<string> numbers) + { + this.Numbers = numbers; + } + } + + class NamedParamterNoDefault + { + private string str; + + [NamedParameter] + public class NamedString : Name<string> + { + } + + [Inject] + NamedParamterNoDefault([Parameter(typeof (NamedString))] string str) + { + this.str = str; + } + + public string GetString() + { + return str; + } + } +} \ No newline at end of file
