Update of /cvsroot/springnet/Spring.Net/test/Spring/Spring.Core.Tests/Reflection/Dynamic
In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv20549/test/Spring/Spring.Core.Tests/Reflection/Dynamic
Modified Files:
DynamicPropertyTests.cs
Added Files:
BasePropertyTests.cs SafePropertyTests.cs
Log Message:
additional tests and minor fixes to SafeProperty and DynamicProperty
--- NEW FILE: SafePropertyTests.cs ---
(This appears to be a binary file; contents omitted.)
Index: DynamicPropertyTests.cs
===================================================================
RCS file: /cvsroot/springnet/Spring.Net/test/Spring/Spring.Core.Tests/Reflection/Dynamic/DynamicPropertyTests.cs,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** DynamicPropertyTests.cs 16 May 2008 10:02:41 -0000 1.2
--- DynamicPropertyTests.cs 17 May 2008 11:05:27 -0000 1.3
***************
*** 37,99 ****
/// <version>$Id$</version>
[TestFixture]
! public sealed class DynamicPropertyTests
{
! private Inventor tesla;
! private Inventor pupin;
! private Society ieee;
!
! #region SetUp and TearDown
!
! /// <summary>
! /// The setup logic executed before the execution of each individual test.
! /// </summary>
! [SetUp]
! public void SetUp()
! {
! ContextRegistry.Clear();
! tesla = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian");
! tesla.Inventions = new string[]
! {
! "Telephone repeater", "Rotating magnetic field principle",
! "Polyphase alternating-current system", "Induction motor",
! "Alternating-current power transmission", "Tesla coil transformer",
! "Wireless communication", "Radio", "Fluorescent lights"
! };
! tesla.PlaceOfBirth.City = "Smiljan";
!
! pupin = new Inventor("Mihajlo Pupin", new DateTime(1854, 10, 9), "Serbian");
! pupin.Inventions = new string[] { "Long distance telephony & telegraphy", "Secondary X-Ray radiation", "Sonar" };
! pupin.PlaceOfBirth.City = "Idvor";
! pupin.PlaceOfBirth.Country = "Serbia";
!
! ieee = new Society();
! ieee.Members.Add(tesla);
! ieee.Members.Add(pupin);
! ieee.Officers["president"] = pupin;
! ieee.Officers["advisors"] = new Inventor[] { tesla, pupin }; // not historically accurate, but I need an array in the map ;-)
! }
!
! [TestFixtureTearDown]
! public void TearDown()
! {
! //DynamicReflectionManager.SaveAssembly();
! }
!
! #endregion
!
! [Test]
! public void TestInstanceProperties()
{
! IDynamicProperty placeOfBirth = DynamicProperty.Create(typeof(Inventor).GetProperty("PlaceOfBirth"));
! Assert.AreEqual(tesla.PlaceOfBirth, placeOfBirth.GetValue(tesla));
!
! IDynamicProperty dateOfBirth = DynamicProperty.Create(typeof(Inventor).GetProperty("DOB"));
! Assert.AreEqual(tesla.DOB, dateOfBirth.GetValue(tesla));
! dateOfBirth.SetValue(tesla, new DateTime(2004, 8, 14));
! Assert.AreEqual(new DateTime(2004, 8, 14), dateOfBirth.GetValue(tesla));
!
! DateTime mostImportantDayInTheWorldEver = new DateTime(2004, 8, 14);
! IDynamicProperty year = DynamicProperty.Create(typeof(DateTime).GetProperty("Year"));
! Assert.AreEqual(mostImportantDayInTheWorldEver.Year, year.GetValue(mostImportantDayInTheWorldEver));
}
--- 37,45 ----
/// <version>$Id$</version>
[TestFixture]
! public class DynamicPropertyTests : BasePropertyTests
{
! protected override IDynamicProperty Create(PropertyInfo property)
{
! return DynamicProperty.Create(property);
}
***************
*** 103,107 ****
{
IDynamicProperty nonReadableProperty =
! DynamicProperty.Create(typeof(ClassWithNonReadableProperty).GetProperty("MyProperty"));
nonReadableProperty.GetValue(null);
}
--- 49,53 ----
{
IDynamicProperty nonReadableProperty =
! Create(typeof(ClassWithNonReadableProperty).GetProperty("MyProperty"));
nonReadableProperty.GetValue(null);
}
***************
*** 112,116 ****
{
IDynamicProperty nonWritableProperty =
! DynamicProperty.Create(typeof(Inventor).GetProperty("PlaceOfBirth"));
nonWritableProperty.SetValue(null, null);
}
--- 58,62 ----
{
IDynamicProperty nonWritableProperty =
! Create(typeof(Inventor).GetProperty("PlaceOfBirth"));
nonWritableProperty.SetValue(null, null);
}
***************
*** 121,139 ****
{
MyStruct myYearHolder = new MyStruct();
! IDynamicProperty year = DynamicProperty.Create(typeof(MyStruct).GetProperty("Year"));
year.SetValue(myYearHolder, 2004);
}
- [Test]
- public void TestStaticProperties()
- {
- IDynamicProperty today = DynamicProperty.Create(typeof(DateTime).GetProperty("Today"));
- Assert.AreEqual(DateTime.Today, today.GetValue(null));
-
- IDynamicProperty myProperty = DynamicProperty.Create(typeof(MyStaticClass).GetProperty("MyProperty"));
- myProperty.SetValue(null, "here we go...");
- Assert.AreEqual("here we go...", myProperty.GetValue(null));
- }
-
[Test]
[ExpectedException(typeof(InvalidOperationException))]
--- 67,74 ----
{
MyStruct myYearHolder = new MyStruct();
! IDynamicProperty year = Create(typeof(MyStruct).GetProperty("Year"));
year.SetValue(myYearHolder, 2004);
}
[Test]
[ExpectedException(typeof(InvalidOperationException))]
***************
*** 141,398 ****
{
IDynamicProperty nonWritableProperty =
! DynamicProperty.Create(typeof(DateTime).GetProperty("Today"));
nonWritableProperty.SetValue(null, null);
! }
!
! #if NET_2_0
! [Test]
! [ExpectedException(typeof(MethodAccessException))]
! public void TestForRestrictiveGetter()
! {
! Something something = new Something();
!
! IDynamicProperty third = DynamicProperty.Create(typeof(Something).GetProperty("Third"));
! //this should be ok, because both get and set of the "Third" property are public
! third.SetValue(something,456);
! Assert.AreEqual(456, third.GetValue(something));
!
! IDynamicProperty first = DynamicProperty.Create(typeof (Something).GetProperty("First"));
! first.SetValue(something,123);
! //this should cause MethodAccessException, because get is private
! Assert.AreEqual(123, first.GetValue(something));
! }
!
! [Test]
! public void TestForRestrictiveGetterWithSafeWrapper()
! {
! Something something = new Something();
! //new SafeProperty()
! IDynamicProperty third = DynamicProperty.CreateSafe(typeof(Something).GetProperty("Third"));
! //this should be ok, because both get and set of the "Third" property are public
! third.SetValue(something, 456);
! Assert.AreEqual(456, third.GetValue(something));
!
! IDynamicProperty first = DynamicProperty.CreateSafe(typeof(Something).GetProperty("First"));
! first.SetValue(something, 123);
! //this should not cause MethodAccessException, "first" is createtd using "CreateSafe"
! Assert.AreEqual(123, first.GetValue(something));
! }
!
!
! [Test]
! [ExpectedException(typeof(MethodAccessException))]
! public void TestForRestrictiveSetter()
! {
! Something something = new Something();
!
! IDynamicProperty third = DynamicProperty.Create(typeof(Something).GetProperty("Third"));
! third.SetValue(something, 456);
! //this should be ok, because both get and set of the "Third" property are public
! Assert.AreEqual(456, third.GetValue(something));
!
! IDynamicProperty second = DynamicProperty.Create(typeof(Something).GetProperty("Second"));
! Assert.AreEqual(2, second.GetValue(something));
! //this should cause MethodAccessException, because set is private in "Second" property
! second.SetValue(something, 123);
!
! //this should never execute
! Assert.AreEqual(123, second.GetValue(something));
! }
!
! [Test]
! public void TestForRestrictiveSetterWithSafeWrapper()
! {
! Something something = new Something();
!
! IDynamicProperty third = DynamicProperty.CreateSafe(typeof(Something).GetProperty("Third"));
! third.SetValue(something, 456);
! //this should be ok, because both get and set of the "Third" property are public
! Assert.AreEqual(456, third.GetValue(something));
!
! IDynamicProperty second = DynamicProperty.CreateSafe(typeof(Something).GetProperty("Second"));
! Assert.AreEqual(2, second.GetValue(something));
! //this should not cause MethodAccessException because "second" is created using "CreateSafe"
! second.SetValue(something, 123);
!
! Assert.AreEqual(123, second.GetValue(something));
! }
! #endif
!
!
! #region Performance tests
!
! private DateTime start, stop;
!
! [Test]
! [Explicit]
! public void PerformanceTests()
! {
! int n = 10000000;
! object x = null;
!
! // tesla.PlaceOfBirth
! start = DateTime.Now;
! for (int i = 0; i < n; i++)
! {
! x = tesla.PlaceOfBirth;
! }
! stop = DateTime.Now;
! PrintTest("tesla.PlaceOfBirth (direct)", n, Elapsed);
!
! start = DateTime.Now;
! IDynamicProperty placeOfBirth = DynamicProperty.Create(typeof(Inventor).GetProperty("PlaceOfBirth"));
! for (int i = 0; i < n; i++)
! {
! x = placeOfBirth.GetValue(tesla);
! }
! stop = DateTime.Now;
! PrintTest("tesla.PlaceOfBirth (dynamic reflection)", n, Elapsed);
!
! start = DateTime.Now;
! PropertyInfo placeOfBirthPi = typeof(Inventor).GetProperty("PlaceOfBirth");
! for (int i = 0; i < n; i++)
! {
! x = placeOfBirthPi.GetValue(tesla, null);
! }
! stop = DateTime.Now;
! PrintTest("tesla.PlaceOfBirth (standard reflection)", n, Elapsed);
! }
!
! private double Elapsed
! {
! get { return (stop.Ticks - start.Ticks) / 10000000f; }
! }
!
! private void PrintTest(string name, int iterations, double duration)
! {
! Debug.WriteLine(String.Format("{0,-60} {1,12:#,###} {2,12:##0.000} {3,12:#,###}", name, iterations, duration, iterations / duration));
! }
!
! #endregion
!
! }
!
! #region IL generation helper classes (they help if you look at them in Reflector ;-)
!
! public class ValueTypeProperty : IDynamicProperty
! {
! public object GetValue(object target)
! {
! return ((Inventor) target).DOB;
! }
!
! public void SetValue(object target, object value)
! {
! ((Inventor)target).DOB = (DateTime) value;
! }
! }
!
! public class ValueTypeTarget : IDynamicProperty
! {
! public object GetValue(object target)
! {
! return ((MyStruct) target).Year;
! }
!
! public void SetValue(object target, object value)
! {
! MyStruct o = (MyStruct) target;
! o.Year = (int) value;
! }
! }
!
! public class StaticProperty : IDynamicProperty
! {
! public object GetValue(object target)
! {
! return MyStaticClass.MyProperty;
! }
!
! public void SetValue(object target, object value)
! {
! MyStaticClass.MyProperty = (string) value;
! }
! }
!
! #endregion
!
! public class MyStaticClass
! {
! public const Int64 MyConst = 3456;
! public static readonly string myReadonlyField = "hohoho";
! public static string myField;
!
! public static string MyProperty
! {
! get { return myField; }
! set { myField = value; }
! }
! }
!
! public struct MyStaticStruct
! {
! public const int constant = 20;
! public static int staticYear;
!
! public static int StaticYear
! {
! get { return staticYear; }
! set { staticYear = value; }
! }
! }
!
! public struct MyStruct
! {
! public int year;
!
! public int Year
! {
! get { return year; }
! set { year = value; }
! }
! }
!
! public class ClassWithNonReadableProperty
! {
! private string myProperty;
!
! public string MyProperty
! {
! set { myProperty = value; }
! }
! }
!
! #if NET_2_0
! public class Something
! {
! public int First
! {
! private get { return first; }
! set { first = value; }
! }
!
! public int Second
! {
! get { return second; }
! private set { second = value; }
! }
!
! public int Third
! {
! get { return third; }
! set { third = value; }
! }
! public Something()
! {
! first = 1;
! second = 2;
! third = 3;
! }
! private int first;
! private int second;
! private int third;
!
}
- #endif
-
}
--- 76,82 ----
{
IDynamicProperty nonWritableProperty =
! Create(typeof(DateTime).GetProperty("Today"));
nonWritableProperty.SetValue(null, null);
! }
}
}
--- NEW FILE: BasePropertyTests.cs ---
using System;
using System.Diagnostics;
using System.Reflection;
using NUnit.Framework;
using Spring.Context.Support;
namespace Spring.Reflection.Dynamic
{
/// <summary>
/// Unit tests common for all IDynamicProperty implementations.
/// </summary>
/// <author>Aleksandar Seovic</author>
/// <author>Erich Eichinger</author>
/// <version>$Id: BasePropertyTests.cs,v 1.1 2008/05/17 11:05:27 oakinger Exp $</version>
public abstract class BasePropertyTests
{
protected Inventor tesla;
protected Inventor pupin;
protected Society ieee;
#region SetUp and TearDown
/// <summary>
/// The setup logic executed before the execution of each individual test.
/// </summary>
[SetUp]
public void SetUp()
{
ContextRegistry.Clear();
tesla = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian");
tesla.Inventions = new string[]
{
"Telephone repeater", "Rotating magnetic field principle",
"Polyphase alternating-current system", "Induction motor",
"Alternating-current power transmission", "Tesla coil transformer",
"Wireless communication", "Radio", "Fluorescent lights"
};
tesla.PlaceOfBirth.City = "Smiljan";
pupin = new Inventor("Mihajlo Pupin", new DateTime(1854, 10, 9), "Serbian");
pupin.Inventions = new string[] { "Long distance telephony & telegraphy", "Secondary X-Ray radiation", "Sonar" };
pupin.PlaceOfBirth.City = "Idvor";
pupin.PlaceOfBirth.Country = "Serbia";
ieee = new Society();
ieee.Members.Add(tesla);
ieee.Members.Add(pupin);
ieee.Officers["president"] = pupin;
ieee.Officers["advisors"] = new Inventor[] { tesla, pupin }; // not historically accurate, but I need an array in the map ;-)
}
[TestFixtureTearDown]
public void TearDown()
{
//DynamicReflectionManager.SaveAssembly();
}
#endregion
protected abstract IDynamicProperty Create(PropertyInfo property);
[Test]
public void TestInstanceProperties()
{
IDynamicProperty placeOfBirth = Create(typeof(Inventor).GetProperty("PlaceOfBirth"));
Assert.AreEqual(tesla.PlaceOfBirth, placeOfBirth.GetValue(tesla));
IDynamicProperty dateOfBirth = Create(typeof(Inventor).GetProperty("DOB"));
Assert.AreEqual(tesla.DOB, dateOfBirth.GetValue(tesla));
dateOfBirth.SetValue(tesla, new DateTime(2004, 8, 14));
Assert.AreEqual(new DateTime(2004, 8, 14), dateOfBirth.GetValue(tesla));
DateTime mostImportantDayInTheWorldEver = new DateTime(2004, 8, 14);
IDynamicProperty year = Create(typeof(DateTime).GetProperty("Year"));
Assert.AreEqual(mostImportantDayInTheWorldEver.Year, year.GetValue(mostImportantDayInTheWorldEver));
}
[Test]
public void TestStaticProperties()
{
IDynamicProperty today = Create(typeof(DateTime).GetProperty("Today"));
Assert.AreEqual(DateTime.Today, today.GetValue(null));
IDynamicProperty myProperty = Create(typeof(MyStaticClass).GetProperty("MyProperty"));
myProperty.SetValue(null, "here we go...");
Assert.AreEqual("here we go...", myProperty.GetValue(null));
}
#if NET_2_0
[Test, Ignore("test N/A anymore due to System.Reflection.Emit.DynamicMethod")]
[ExpectedException(typeof(MethodAccessException))]
public void TestForRestrictiveGetter()
{
Something something = new Something();
IDynamicProperty third = Create(typeof(Something).GetProperty("Third"));
//this should be ok, because both get and set of the "Third" property are public
third.SetValue(something, 456);
Assert.AreEqual(456, third.GetValue(something));
IDynamicProperty first = Create(typeof(Something).GetProperty("First"));
first.SetValue(something, 123);
//this should cause MethodAccessException, because get is private
Assert.AreEqual(123, first.GetValue(something));
}
[Test, Ignore("test N/A anymore due to System.Reflection.Emit.DynamicMethod")]
[ExpectedException(typeof(MethodAccessException))]
public void TestForRestrictiveSetter()
{
Something something = new Something();
IDynamicProperty third = Create(typeof(Something).GetProperty("Third"));
third.SetValue(something, 456);
//this should be ok, because both get and set of the "Third" property are public
Assert.AreEqual(456, third.GetValue(something));
IDynamicProperty second = Create(typeof(Something).GetProperty("Second"));
Assert.AreEqual(2, second.GetValue(something));
//this should cause MethodAccessException, because set is private in "Second" property
second.SetValue(something, 123);
//this should never execute
Assert.AreEqual(123, second.GetValue(something));
}
#endif
#region Performance tests
private DateTime start, stop;
[Test]
[Explicit]
public void PerformanceTests()
{
int n = 10000000;
object x = null;
// tesla.PlaceOfBirth
start = DateTime.Now;
for (int i = 0; i < n; i++)
{
x = tesla.PlaceOfBirth;
}
stop = DateTime.Now;
PrintTest("tesla.PlaceOfBirth (direct)", n, Elapsed);
start = DateTime.Now;
IDynamicProperty placeOfBirth = DynamicProperty.Create(typeof(Inventor).GetProperty("PlaceOfBirth"));
for (int i = 0; i < n; i++)
{
x = placeOfBirth.GetValue(tesla);
}
stop = DateTime.Now;
PrintTest("tesla.PlaceOfBirth (dynamic reflection)", n, Elapsed);
start = DateTime.Now;
PropertyInfo placeOfBirthPi = typeof(Inventor).GetProperty("PlaceOfBirth");
for (int i = 0; i < n; i++)
{
x = placeOfBirthPi.GetValue(tesla, null);
}
stop = DateTime.Now;
PrintTest("tesla.PlaceOfBirth (standard reflection)", n, Elapsed);
}
private double Elapsed
{
get { return (stop.Ticks - start.Ticks) / 10000000f; }
}
private void PrintTest(string name, int iterations, double duration)
{
Debug.WriteLine(String.Format("{0,-60} {1,12:#,###} {2,12:##0.000} {3,12:#,###}", name, iterations, duration, iterations / duration));
}
#endregion
}
#region IL generation helper classes (they help if you look at them in Reflector ;-)
public class ValueTypeProperty : IDynamicProperty
{
public object GetValue(object target)
{
return ((Inventor)target).DOB;
}
public void SetValue(object target, object value)
{
((Inventor)target).DOB = (DateTime)value;
}
}
public class ValueTypeTarget : IDynamicProperty
{
public object GetValue(object target)
{
return ((MyStruct)target).Year;
}
public void SetValue(object target, object value)
{
MyStruct o = (MyStruct)target;
o.Year = (int)value;
}
}
public class StaticProperty : IDynamicProperty
{
public object GetValue(object target)
{
return MyStaticClass.MyProperty;
}
public void SetValue(object target, object value)
{
MyStaticClass.MyProperty = (string)value;
}
}
#endregion
public class MyStaticClass
{
public const Int64 MyConst = 3456;
public static readonly string myReadonlyField = "hohoho";
public static string myField;
public static string MyProperty
{
get { return myField; }
set { myField = value; }
}
}
public struct MyStaticStruct
{
public const int constant = 20;
public static int staticYear;
public static int StaticYear
{
get { return staticYear; }
set { staticYear = value; }
}
}
public struct MyStruct
{
public int year;
public int Year
{
get { return year; }
set { year = value; }
}
}
public class ClassWithNonReadableProperty
{
private string myProperty;
public string MyProperty
{
set { myProperty = value; }
}
}
#if NET_2_0
public class Something
{
public int First
{
private get { return first; }
set { first = value; }
}
public int Second
{
get { return second; }
private set { second = value; }
}
public int Third
{
get { return third; }
set { third = value; }
}
public Something()
{
first = 1;
second = 2;
third = 3;
}
private int first;
private int second;
private int third;
}
#endif
}
|