Update of /cvsroot/springnet/Spring.Net/test/Spring/Spring.Aop.Tests/Aop/Framework
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17589/test/Spring/Spring.Aop.Tests/Aop/Framework
Added Files:
CountingBeforeAdvice.cs MethodCounter.cs ProxyFactoryTests.cs
Log Message:
Ported the first two test methods of ProxyFactoryTests from Spring.Java.
--- NEW FILE: MethodCounter.cs ---
#region License
/*
* Copyright 2002-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.Collections;
using System.Reflection;
#endregion
namespace Spring.Aop.Framework
{
/// <summary>
/// Useful base class for counting advices etc.
/// </summary>
/// <author>Rod Johnson</author>
/// <author>Choy Rim (.NET)</author>
/// <version>$Id: MethodCounter.cs,v 1.1 2004/08/01 09:02:08 choyrim Exp $</version>
public class MethodCounter
{
/// <summary>Method name --> count, does not understand overloading </summary>
private Hashtable map = new Hashtable();
private int allCount;
protected internal virtual void Count(MethodBase m)
{
Count(m.Name);
}
protected internal virtual void Count(string methodName)
{
int count = GetCalls(methodName);
++count;
map[methodName] = count;
++allCount;
}
public virtual int GetCalls(string methodName)
{
int count = 0;
if ( map.ContainsKey(methodName) )
{
count = (int) map[methodName];
}
return count;
}
public virtual int GetCalls()
{
return allCount;
}
}
}
--- NEW FILE: CountingBeforeAdvice.cs ---
#region License
/*
* Copyright 2002-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 Spring.Aop.Framework;
#endregion
namespace Spring.Aop.Framework
{
/// <summary>
/// Summary description for CountingBeforeAdvice.
/// </summary>
public class CountingBeforeAdvice: MethodCounter,
IMethodBeforeAdvice
{
#region IMethodBeforeAdvice Members
public void Before(MethodBase method, object[] args, object target)
{
Count(method);
}
#endregion
}
}
--- NEW FILE: ProxyFactoryTests.cs ---
#region License
/*
* Copyright 2002-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.Collections;
using NUnit.Framework;
using AopAlliance.Intercept;
using Spring.Aop.Interceptor;
using Spring.Aop.Support;
#endregion
namespace Spring.Aop.Framework
{
/// <summary>
/// Unit tests for ProxyFactory.
/// </summary>
/// <author>Rod Johnson</author>
/// <author>Choy Rim (.NET)</author>
/// <version>$Id: ProxyFactoryTests.cs,v 1.1 2004/08/01 09:02:08 choyrim Exp $</version>
[TestFixture]
public class ProxyFactoryTests
{
[Test]
[ExpectedException(typeof(AopConfigException))]
public void testNullTarget()
{
// Use the constructor taking Object
new ProxyFactory((System.Object) null);
Assert.Fail("Should't allow proxy with null target");
}
[Test]
public void testIndexOfMethods()
{
TestObject target = new TestObject();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
IAdvisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice());
IAdvised advised = (IAdvised) pf.GetProxy();
// Can use advised and ProxyFactory interchangeably
advised.AddInterceptor(nop);
pf.AddAdvisor(advisor);
Assert.AreEqual(- 1, pf.IndexOf((IInterceptor) null));
Assert.AreEqual(- 1, pf.IndexOf(new NopInterceptor()));
Assert.AreEqual(0, pf.IndexOf(nop));
Assert.AreEqual(- 1, advised.IndexOf((IAdvisor) null));
Assert.AreEqual(1, pf.IndexOf(advisor));
Assert.AreEqual(- 1, advised.IndexOf(new DefaultPointcutAdvisor(null)));
}
#region Copied from Spring.Core.Tests
// If we try to add reference to Spring.Core.Tests we get circular dependency
internal interface INestedTestObject
{
string Company { get; set; }
}
internal interface ITestObject
{
int Age { get; set; }
INestedTestObject Doctor { get; }
INestedTestObject Lawyer { get; }
string Name { get; set; }
ITestObject Spouse { get; set; }
void Exceptional (Exception t);
object ReturnsThis ();
//IndexedTestObject getNestedIndexedBean();
}
internal class NestedTestObject : INestedTestObject
{
internal string company = "";
public NestedTestObject()
{
}
public NestedTestObject(string comp)
{
Company = comp;
}
virtual public string Company
{
get
{
return this.company;
}
set
{
this.company = value;
}
}
/*
public override bool Equals(System.Object obj)
{
if (!(obj is NestedTestObject))
{
return false;
}
NestedTestObject ntb = (NestedTestObject) obj;
return new EqualsBuilder().append(company, ntb.company).isEquals();
}
public override int GetHashCode()
{
return new HashCodeBuilder(23, 91).append(company).toHashCode();
}
*/
}
internal class TestObject : ITestObject, System.IComparable
{
virtual public string Touchy
{
get
{
return touchy;
}
set
{
if (value.IndexOf((System.Char) '.') != - 1)
throw new System.Exception("Can't contain a .");
if (value.IndexOf((System.Char) ',') != - 1)
throw new System.FormatException("Number format exception: contains a ,");
this.touchy = value;
}
}
virtual public bool PostProcessed
{
get
{
return postProcessed;
}
set
{
this.postProcessed = value;
}
}
virtual public string Name
{
get
{
return name;
}
set
{
this.name = value;
}
}
virtual public string Nickname
{
get
{
return nickname;
}
set
{
this.nickname = value;
}
}
/// <summary>A simple integer age property</summary>
virtual public int Age
{
get
{
return age;
}
set
{
this.age = value;
}
}
/// <summary>
/// A simple date property
/// </summary>
virtual public System.DateTime Date
{
get
{
return date;
}
set
{
this.date = value;
}
}
virtual public System.Single MyFloat
{
get
{
return myFloat;
}
set
{
this.myFloat = value;
}
}
virtual public ITestObject Spouse
{
get
{
return spouse;
}
set
{
this.spouse = value;
}
}
virtual public INestedTestObject Doctor
{
get
{
return doctor;
}
set
{
this.doctor = value;
}
}
virtual public INestedTestObject Lawyer
{
get
{
return lawyer;
}
set
{
this.lawyer = value;
}
}
/// <summary>
/// A collection of friends.
/// </summary>
virtual public ICollection Friends
{
get
{
return friends;
}
set
{
this.friends = value;
}
}
virtual public IDictionary SomeMap
{
get
{
return someMap;
}
set
{
this.someMap = value;
}
}
[NonSerialized]
private bool postProcessed;
/// <summary>Holds value of property age. </summary>
[NonSerialized]
private int age;
/// <summary>Holds value of property name. </summary>
[NonSerialized]
private string name;
[NonSerialized]
private string nickname;
[NonSerialized]
private ITestObject spouse;
[NonSerialized]
private string touchy;
[NonSerialized]
private System.Collections.ICollection friends = new Spring.Util.LinkedList ();
[NonSerialized]
private System.Collections.IDictionary someMap = new System.Collections.Hashtable();
[NonSerialized]
private System.DateTime date = System.DateTime.Now;
[NonSerialized]
private System.Single myFloat = (float) 0.0;
[NonSerialized] private INestedTestObject doctor = new NestedTestObject();
[NonSerialized] private INestedTestObject lawyer = new NestedTestObject();
public TestObject()
{
}
public TestObject(string name, int age)
{
this.name = name;
this.age = age;
}
public override bool Equals(object other)
{
if (other == null || !(other is TestObject))
return false;
TestObject tb2 = (TestObject) other;
if (tb2.age != age)
return false;
if ((object) name == null)
return (object) tb2.name == null;
if (!tb2.name.Equals(name))
return false;
return true;
}
/// <summary>
/// The hashcode of the property
/// </summary>
/// <returns>the hashcode of the property</returns>
public override int GetHashCode()
{
return name.GetHashCode();
}
public virtual int CompareTo(object other)
{
if ((object) this.name != null && other is TestObject)
{
return String.CompareOrdinal(this.name, ((TestObject) other).name);
}
else
{
return 1;
}
}
/// <summary>
/// String representation of the class
/// </summary>
/// <returns>string represention</returns>
public override string ToString()
{
string s = "name=" + name + "; age=" + age + "; touchy=" + touchy;
s += ("; spouse={" + (spouse != null?spouse.Name:null) + "}");
return s;
}
/// <summary>
/// Throw the given exception
/// </summary>
/// <param name="t">An exception to throw.</param>
public virtual void Exceptional (Exception t)
{
if (t != null)
{
throw t;
}
}
/// <summary>
/// Return a reference to the object itself. 'Return this'
/// </summary>
/// <returns>a reference to the object itse.f</returns>
public virtual object ReturnsThis ()
{
return this;
}
/// <summary>
/// Funny Named method.
/// </summary>
public virtual void Absquatulate ()
{
//System.out.println("IOther.absquatulate");
}
}
#endregion
}
}
|