From 191a136a3c310ba23ab3f5d30db62122f1cbfedf Mon Sep 17 00:00:00 2001 From: Steve Bohlen Date: Thu, 4 Oct 2012 13:01:15 -0400 Subject: [PATCH] SPRNET-1524 check for array index bounds merged, test added --- .../Util/ReflectionUtilsTests.cs | 48 ++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/test/Spring/Spring.Core.Tests/Util/ReflectionUtilsTests.cs b/test/Spring/Spring.Core.Tests/Util/ReflectionUtilsTests.cs index 350a5b45..90d343ee 100644 --- a/test/Spring/Spring.Core.Tests/Util/ReflectionUtilsTests.cs +++ b/test/Spring/Spring.Core.Tests/Util/ReflectionUtilsTests.cs @@ -893,6 +893,33 @@ namespace Spring.Util Assert.AreEqual("System.Int32", types[1].FullName); } + [Test] + public void GetMethodByArgumentValuesCanResolveWhenAmbiguousMatchIsOnlyDifferentiatedByParams() + { + GetMethodByArgumentValuesTarget.DummyArgumentType[] typedArg = new GetMethodByArgumentValuesTarget.DummyArgumentType[] { }; + GetMethodByArgumentValuesTarget foo = new GetMethodByArgumentValuesTarget(1, typedArg); + + Type type = typeof(GetMethodByArgumentValuesTarget); + MethodInfo[] candidateMethods = new MethodInfo[] + { + type.GetMethod("ParamOverloadedMethod", new Type[] {typeof(string), typeof(string), typeof(string)}) + ,type.GetMethod("ParamOverloadedMethod", new Type[] {typeof(string), typeof(string), typeof(bool)}) + ,type.GetMethod("ParamOverloadedMethod", new Type[] {typeof(string), typeof(string), typeof(string), typeof(string), typeof(object[])}) + }; + + // ensure noone changed our test class + Assert.IsNotNull(candidateMethods[0]); + Assert.IsNotNull(candidateMethods[1]); + Assert.IsNotNull(candidateMethods[2]); + Assert.AreEqual("ThreeStringsOverload", foo.ParamOverloadedMethod(string.Empty, string.Empty, string.Empty)); + Assert.AreEqual("TwoStringsAndABoolOverload", foo.ParamOverloadedMethod(string.Empty, string.Empty, default(bool))); + Assert.AreEqual("FourStringsAndAParamsCollectionOverload", foo.ParamOverloadedMethod(string.Empty, string.Empty, string.Empty, string.Empty, typedArg)); + + MethodInfo resolvedMethod = ReflectionUtils.GetMethodByArgumentValues(candidateMethods, new object[] { string.Empty, string.Empty, string.Empty, string.Empty, typedArg }); + Assert.AreSame(candidateMethods[2], resolvedMethod); + } + + [Test] public void GetMethodByArgumentValuesResolvesToExactMatchIfAvailable() { @@ -1182,14 +1209,14 @@ namespace Spring.Util [Test] public void IsTypeNullable_WhenTrue() { - Type type = typeof (int?); + Type type = typeof(int?); Assert.That(ReflectionUtils.IsNullableType(type), Is.True); } [Test] public void IsTypeNullable_WhenFalse() { - Type type = typeof (int); + Type type = typeof(int); Assert.That(ReflectionUtils.IsNullableType(type), Is.False); } @@ -1509,6 +1536,23 @@ namespace Spring.Util { return "NullableArgumentMatch"; } + + public string ParamOverloadedMethod(string s1, string s2, string s3) + { + return "ThreeStringsOverload"; + } + + public string ParamOverloadedMethod(string s1, string s2, bool b1) + { + return "TwoStringsAndABoolOverload"; + } + + public string ParamOverloadedMethod(string s1, string s2, string s3, string s4, params object[] args) + { + return "FourStringsAndAParamsCollectionOverload"; + } + + } public sealed class MyCustomAttribute : Attribute