Update of /cvsroot/springnet/Spring.Net/test/Spring/Spring.Core.Tests/Reflection/Dynamic
In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv3433/test/Spring/Spring.Core.Tests/Reflection/Dynamic
Modified Files:
DynamicFieldTests.cs DynamicPropertyTests.cs
Added Files:
SafeFieldTests.cs
Log Message:
fixed some 2003 solution missing files
added SafeField tests
DynamicField.Create returns SafeField in net-2.0 now
added StopWatch to Spring.Core.Tests for performance tests
Index: DynamicFieldTests.cs
===================================================================
RCS file: /cvsroot/springnet/Spring.Net/test/Spring/Spring.Core.Tests/Reflection/Dynamic/DynamicFieldTests.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** DynamicFieldTests.cs 8 Aug 2007 04:06:01 -0000 1.1
--- DynamicFieldTests.cs 16 May 2008 10:02:41 -0000 1.2
***************
*** 37,45 ****
/// <version>$Id$</version>
[TestFixture]
! public sealed class DynamicFieldTests
{
! private Inventor tesla;
! private Inventor pupin;
! private Society ieee;
#region SetUp and TearDown
--- 37,45 ----
/// <version>$Id$</version>
[TestFixture]
! public class DynamicFieldTests
{
! protected Inventor tesla;
! protected Inventor pupin;
! protected Society ieee;
#region SetUp and TearDown
***************
*** 82,89 ****
#endregion
[Test]
public void TestInstanceFields()
{
! IDynamicField name = DynamicField.Create(typeof(Inventor).GetField("Name"));
Assert.AreEqual(tesla.Name, name.GetValue(tesla));
name.SetValue(tesla, "Tesla, Nikola");
--- 82,94 ----
#endregion
+ protected virtual IDynamicField Create(FieldInfo field)
+ {
+ return DynamicField.Create(field);
+ }
+
[Test]
public void TestInstanceFields()
{
! IDynamicField name = Create(typeof(Inventor).GetField("Name"));
Assert.AreEqual(tesla.Name, name.GetValue(tesla));
name.SetValue(tesla, "Tesla, Nikola");
***************
*** 93,97 ****
MyStruct myYearHolder = new MyStruct();
myYearHolder.Year = 2004;
! IDynamicField year = DynamicField.Create(typeof(MyStruct).GetField("year", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static));
Assert.AreEqual(2004, year.GetValue(myYearHolder));
}
--- 98,102 ----
MyStruct myYearHolder = new MyStruct();
myYearHolder.Year = 2004;
! IDynamicField year = Create(typeof(MyStruct).GetField("year", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static));
Assert.AreEqual(2004, year.GetValue(myYearHolder));
}
***************
*** 102,121 ****
{
MyStruct myYearHolder = new MyStruct();
! IDynamicField year = DynamicField.Create(typeof(MyStruct).GetField("year"));
year.SetValue(myYearHolder, 2004);
}
[Test]
! public void TestStaticFields()
{
! IDynamicField maxValue = DynamicField.Create(typeof(DateTime).GetField("MaxValue"));
! Assert.AreEqual(DateTime.MaxValue, maxValue.GetValue(null));
!
! IDynamicField myField = DynamicField.Create(typeof(MyStaticClass).GetField("myField"));
myField.SetValue(null, "here we go...");
- Assert.AreEqual("here we go...", myField.GetValue(null));
! IDynamicField int64max = DynamicField.Create(typeof(Int64).GetField("MaxValue", BindingFlags.Public | BindingFlags.Static));
Assert.AreEqual(Int64.MaxValue, int64max.GetValue(null));
}
--- 107,158 ----
{
MyStruct myYearHolder = new MyStruct();
! IDynamicField year = Create(typeof(MyStruct).GetField("year"));
year.SetValue(myYearHolder, 2004);
}
[Test]
! public void TestStaticFieldsOfClass()
{
! IDynamicField myField = Create(typeof(MyStaticClass).GetField("myField"));
myField.SetValue(null, "here we go...");
! IDynamicField myConst = Create(typeof(MyStaticClass).GetField("MyConst"));
! Assert.AreEqual(3456, myConst.GetValue(null));
! try {
! myConst.SetValue(null, 7890);
! }
! catch(InvalidOperationException){}
!
! IDynamicField myReadonlyField = Create(typeof(MyStaticClass).GetField("myReadonlyField"));
! Assert.AreEqual("hohoho", myReadonlyField.GetValue(null));
! try {
! myReadonlyField.SetValue(null, "some other string");
! }
! catch(InvalidOperationException){}
! }
!
! [Test]
! public void TestStaticFieldsOfStruct()
! {
! // static readonly
! IDynamicField maxValue = Create(typeof(DateTime).GetField("MaxValue"));
! Assert.AreEqual(DateTime.MaxValue, maxValue.GetValue(null));
! try {
! maxValue.SetValue(null, DateTime.Now);
! }
! catch(InvalidOperationException){}
!
! // const
! IDynamicField int64max = Create(typeof(Int64).GetField("MaxValue", BindingFlags.Public | BindingFlags.Static));
Assert.AreEqual(Int64.MaxValue, int64max.GetValue(null));
+ try {
+ int64max.SetValue(null, 0);
+ }
+ catch(InvalidOperationException){}
+
+ // pure static
+ IDynamicField myField = Create(typeof(MyStaticStruct).GetField("staticYear"));
+ myField.SetValue(null, 2008);
+ Assert.AreEqual(2008, myField.GetValue(null));
}
***************
*** 140,144 ****
start = DateTime.Now;
! IDynamicField placeOfBirth = DynamicField.Create(typeof(Inventor).GetField("Name"));
for (int i = 0; i < n; i++)
{
--- 177,181 ----
start = DateTime.Now;
! IDynamicField placeOfBirth = Create(typeof(Inventor).GetField("Name"));
for (int i = 0; i < n; i++)
{
--- NEW FILE: SafeFieldTests.cs ---
#region License
/*
* Copyright 2004 the original author or authors.
*
* Licensed 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.
*/
#endregion
#region Imports
using System;
using System.Reflection;
using NUnit.Framework;
#endregion
namespace Spring.Reflection.Dynamic
{
/// <summary>
/// Unit tests for the SafeField class. SafeField must pass the same tests
/// as DynamicField plus tests for accessing private members.
/// </summary>
/// <author>Erich Eichinger</author>
/// <version>$Id: SafeFieldTests.cs,v 1.1 2008/05/16 10:02:41 oakinger Exp $</version>
[TestFixture]
public class SafeFieldTests : DynamicFieldTests
{
protected override IDynamicField Create(FieldInfo field)
{
return new SafeField(field);
}
private static FieldInfo IField(Type type, string fieldName)
{
return type.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
}
private static FieldInfo SField(Type t, string fieldName)
{
return t.GetField(fieldName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void ThrowsOnNullField()
{
SafeField field = new SafeField(null);
}
[Test]
public void TestStaticMembersOfStruct()
{
TestStaticMembersOf(typeof(MyStructWithPrivateFields), MyStructWithPrivateFields.GetStaticReadonlyRefValue());
}
[Test]
public void TestStaticMembersOfClass()
{
TestStaticMembersOf(typeof(MyClassWithPrivateFields), MyClassWithPrivateFields.GetStaticReadonlyRefValue());
}
private void TestStaticMembersOf(Type type, object expectedRoRefValue)
{
// ro const int
SafeField constField = new SafeField(SField(type, "constantValue"));
Assert.AreEqual(5, constField.GetValue(null));
try { constField.SetValue(null, 3); }
catch (InvalidOperationException) { }
// ro static readonly int
SafeField roVtField = new SafeField(SField(type, "staticReadonlyVTValue"));
Assert.AreEqual(11, roVtField.GetValue(null));
try { roVtField.SetValue(null, 10); }
catch (InvalidOperationException) { }
// ro static readonly object
SafeField roRefField = new SafeField(SField(type, "staticReadonlyRefValue"));
Assert.AreSame(expectedRoRefValue, roRefField.GetValue(null));
try { roRefField.SetValue(null, new object()); }
catch (InvalidOperationException) { }
// rw static int
SafeField vtField = new SafeField(SField(type, "staticVTValue"));
vtField.SetValue(null, 10);
Assert.AreEqual(10, vtField.GetValue(null));
// rw static object
SafeField refField = new SafeField(SField(type, "staticRefValue"));
object o = new object();
refField.SetValue(null, o);
Assert.AreSame(o, refField.GetValue(null));
}
[Test]
public void TestInstanceMembersOfStruct()
{
object testref1 = new object();
object testref2 = new object();
MyStructWithPrivateFields myStruct;
// ro readonly int
myStruct = new MyStructWithPrivateFields(123, testref1, 456, testref2);
SafeField instanceReadonlyVtField = new SafeField(IField(myStruct.GetType(), "instanceReadonlyVTValue"));
Assert.AreEqual(123, instanceReadonlyVtField.GetValue(myStruct));
try { instanceReadonlyVtField.SetValue(myStruct, 10); }
catch (InvalidOperationException) { }
// ro readonly object
myStruct = new MyStructWithPrivateFields(123, testref1, 456, testref2);
SafeField instanceReadonlyRefField = new SafeField(IField(myStruct.GetType(), "instanceReadonlyRefValue"));
Assert.AreSame(testref1, instanceReadonlyRefField.GetValue(myStruct));
try { instanceReadonlyRefField.SetValue(myStruct, this); }
catch (InvalidOperationException) { }
// ro int
myStruct = new MyStructWithPrivateFields(123, testref1, 456, testref2);
SafeField instanceVtField = new SafeField(IField(myStruct.GetType(), "instanceVTValue"));
Assert.AreEqual(456, instanceVtField.GetValue(myStruct));
try { instanceVtField.SetValue(myStruct, 10); }
catch (InvalidOperationException) { }
// ro object
myStruct = new MyStructWithPrivateFields(123, testref1, 456, testref2);
SafeField instanceRefField = new SafeField(IField(myStruct.GetType(), "instanceRefValue"));
Assert.AreSame(testref2, instanceRefField.GetValue(myStruct));
try { instanceRefField.SetValue(myStruct, 10); }
catch (InvalidOperationException) { }
}
[Test]
public void TestInstanceMembersOfClass()
{
object testref1 = new object();
object testref2 = new object();
MyClassWithPrivateFields myClass;
// ro readonly int
myClass = new MyClassWithPrivateFields(123, testref1, 456, testref2);
SafeField instanceReadonlyVtField = new SafeField(IField(myClass.GetType(), "instanceReadonlyVTValue"));
Assert.AreEqual(123, instanceReadonlyVtField.GetValue(myClass));
try { instanceReadonlyVtField.SetValue(myClass, 10); }
catch (InvalidOperationException) { }
// ro readonly object
myClass = new MyClassWithPrivateFields(123, testref1, 456, testref2);
SafeField instanceReadonlyRefField = new SafeField(IField(myClass.GetType(), "instanceReadonlyRefValue"));
Assert.AreSame(testref1, instanceReadonlyRefField.GetValue(myClass));
try { instanceReadonlyRefField.SetValue(myClass, this); }
catch (InvalidOperationException) { }
// rw int
myClass = new MyClassWithPrivateFields(123, testref1, 456, testref2);
SafeField instanceVtField = new SafeField(IField(myClass.GetType(), "instanceVTValue"));
Assert.AreEqual(456, instanceVtField.GetValue(myClass));
instanceVtField.SetValue(myClass, 9182);
Assert.AreEqual(9182, instanceVtField.GetValue(myClass));
// rw object
myClass = new MyClassWithPrivateFields(123, testref1, 456, testref2);
SafeField instanceRefField = new SafeField(IField(myClass.GetType(), "instanceRefValue"));
Assert.AreSame(testref2, instanceRefField.GetValue(myClass));
instanceRefField.SetValue(myClass, testref1);
Assert.AreSame(testref1, instanceRefField.GetValue(myClass));
}
[Test, Explicit]
public void SafeFieldPerformanceTests()
{
int runs = 10000000;
object myClass = new MyClassWithPrivateFields(123, new object(), 456, new object());
FieldInfo fieldClassRefValue = IField(myClass.GetType(), "instanceRefValue");
FieldInfo fieldClassVtValue = IField(myClass.GetType(), "instanceVTValue");
StopWatch stopWatch = new StopWatch();
using (stopWatch.Start("Duration Class Set/Get field value: {0}"))
{
for(int i=0;i<runs;i++)
{
SafeField instanceRefField = new SafeField(fieldClassRefValue);
SafeField instanceVtField = new SafeField(fieldClassVtValue);
int res = (int) instanceVtField.GetValue( myClass );
object ores = instanceRefField.GetValue( myClass );
}
}
object myStruct = new MyStructWithPrivateFields(123, new object(), 456, new object());
FieldInfo fieldStructRefValue = IField(myStruct.GetType(), "instanceRefValue");
FieldInfo fieldStructVtValue = IField(myStruct.GetType(), "instanceVTValue");
using (stopWatch.Start("Duration Struct Set/Get field value: {0}"))
{
for(int i=0;i<runs;i++)
{
SafeField instanceRefField = new SafeField(fieldStructRefValue);
SafeField instanceVtField = new SafeField(fieldStructVtValue);
int res = (int) instanceVtField.GetValue( myStruct );
object ores = instanceRefField.GetValue( myStruct );
}
}
/* on my machine prints
with System.Reflection.Emit.DynamicMethod generated code:
Duration Class Set/Get field value: 00:00:03.2031250
Duration Struct Set/Get field value: 00:00:03.5625000
with standard reflection:
Duration Class Set/Get field value: 00:00:45.4218750
Duration Struct Set/Get field value: 00:00:44.5312500
*/
}
}
#region Test Classes
public struct MyStructWithPrivateFields
{
// static part
private static int staticVTValue;
private static object staticRefValue;
private static readonly int staticReadonlyVTValue = 11;
private static readonly object staticReadonlyRefValue = new object();
private const int constantValue = 5;
public static object GetStaticReadonlyRefValue() { return staticReadonlyRefValue; }
public static int GetStaticVTValue() { return staticVTValue; }
public static object GetStaticRefValue() { return staticRefValue; }
static MyStructWithPrivateFields()
{
staticVTValue = staticReadonlyVTValue + constantValue + 123; // make compiler happy and avoid "unused" warnings
staticRefValue = new object();
}
// instance part
private int instanceVTValue;
private readonly int instanceReadonlyVTValue;
private object instanceRefValue;
private readonly object instanceReadonlyRefValue;
public MyStructWithPrivateFields(int roVtValue, object roRefValue, int vtValue, object refValue)
{
instanceReadonlyVTValue = roVtValue;
instanceReadonlyRefValue = roRefValue;
instanceVTValue = vtValue;
instanceRefValue = refValue;
}
public void SetInstanceVTValue(int val) { instanceVTValue = val; }
public void SetInstanceRefValue(object val) { instanceRefValue = val; }
}
public class MyClassWithPrivateFields
{
// static part
private static int staticVTValue;
private static object staticRefValue;
private static readonly int staticReadonlyVTValue = 11;
private static readonly object staticReadonlyRefValue = new object();
private const int constantValue = 5;
public static object GetStaticReadonlyRefValue() { return staticReadonlyRefValue; }
public static int GetStaticVTValue() { return staticVTValue; }
public static object GetStaticRefValue() { return staticRefValue; }
static MyClassWithPrivateFields()
{
staticVTValue = staticReadonlyVTValue + constantValue + 654; // make compiler happy and avoid "unused" warnings
staticRefValue = new object();
}
// instance part
private int instanceVTValue;
private readonly int instanceReadonlyVTValue;
private object instanceRefValue;
private readonly object instanceReadonlyRefValue;
public MyClassWithPrivateFields(int roVtValue, object roRefValue, int vtValue, object refValue)
{
instanceReadonlyVTValue = roVtValue;
instanceReadonlyRefValue = roRefValue;
instanceVTValue = vtValue;
instanceRefValue = refValue;
}
public void SetInstanceVTValue(int val) { instanceVTValue = val; }
public void SetInstanceRefValue(object val) { instanceRefValue = val; }
}
#endregion
}
Index: DynamicPropertyTests.cs
===================================================================
RCS file: /cvsroot/springnet/Spring.Net/test/Spring/Spring.Core.Tests/Reflection/Dynamic/DynamicPropertyTests.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** DynamicPropertyTests.cs 8 Aug 2007 04:06:02 -0000 1.1
--- DynamicPropertyTests.cs 16 May 2008 10:02:41 -0000 1.2
***************
*** 320,323 ****
--- 320,324 ----
{
public const Int64 MyConst = 3456;
+ public static readonly string myReadonlyField = "hohoho";
public static string myField;
***************
*** 329,332 ****
--- 330,345 ----
}
+ 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
{
|