From: Michael D. <mik...@us...> - 2004-12-16 21:51:56
|
Update of /cvsroot/nhibernate/nhibernate/src/NHibernate.Test/ProxyInterface In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16936/NHibernate.Test/ProxyInterface Added Files: CastleProxy.cs CastleProxyFixture.cs CastleProxyImpl.cs CastleProxyImpl.hbm.xml Log Message: Implementation and Test of proxies using Castle.DynamicProxy. --- NEW FILE: CastleProxyFixture.cs --- using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using NUnit.Framework; namespace NHibernate.Test.ProxyInterface { /// <summary> /// Summary description for CastleProxyFixture. /// </summary> [TestFixture] public class CastleProxyFixture : TestCase { [SetUp] public void SetUp() { ExportSchema( new string[] { "ProxyInterface.CastleProxyImpl.hbm.xml"}, true, "NHibernate.Test" ); } [Test] public void Proxy() { ISession s = sessions.OpenSession(); CastleProxy ap = new CastleProxyImpl(); ap.Id = 1; ap.Name = "first proxy"; s.Save( ap ); s.Flush(); s.Close(); s = sessions.OpenSession(); ap = (CastleProxy)s.Load( typeof(CastleProxyImpl), ap.Id ); Assert.IsFalse( NHibernate.IsInitialized( ap ) ); int id = ap.Id; Assert.IsFalse( NHibernate.IsInitialized( ap ), "get id should not have initialized it." ); string name = ap.Name; Assert.IsTrue( NHibernate.IsInitialized( ap ), "get name should have initialized it." ); s.Delete( ap ); s.Flush(); s.Close(); } [Test] public void ProxySerialize() { ISession s = sessions.OpenSession(); CastleProxy ap = new CastleProxyImpl(); ap.Id = 1; ap.Name = "first proxy"; s.Save( ap ); s.Flush(); s.Close(); s = sessions.OpenSession(); ap = (CastleProxy)s.Load( typeof(CastleProxyImpl), ap.Id ); Assert.AreEqual( 1, ap.Id ); s.Disconnect(); // serialize and then deserialize the session. Stream stream = new MemoryStream(); IFormatter formatter = new BinaryFormatter( ); formatter.Serialize(stream, s); stream.Position = 0; s = (ISession)formatter.Deserialize(stream); stream.Close(); s.Reconnect(); s.Disconnect(); // serialize and then deserialize the session again - make sure Castle.DynamicProxy // has no problem with serializing two times - earlier versions of it did. stream = new MemoryStream(); formatter = new BinaryFormatter( ); formatter.Serialize(stream, s); stream.Position = 0; s = (ISession)formatter.Deserialize(stream); stream.Close(); s.Close(); } [Test] public void SerializeNotFoundProxy() { ISession s = sessions.OpenSession(); // this does not actually exists in db CastleProxy notThere = (CastleProxy)s.Load( typeof(CastleProxyImpl), 5 ); Assert.AreEqual( 5, notThere.Id ); s.Disconnect(); // serialize and then deserialize the session. Stream stream = new MemoryStream(); IFormatter formatter = new BinaryFormatter( ); formatter.Serialize(stream, s); stream.Position = 0; s = (ISession)formatter.Deserialize(stream); stream.Close(); Assert.IsNotNull( s.Load( typeof(CastleProxyImpl), 5 ), "should be proxy - even though it doesn't exists in db" ); s.Close(); } } } --- NEW FILE: CastleProxyImpl.hbm.xml --- <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"> <class name="NHibernate.Test.ProxyInterface.CastleProxyImpl, NHibernate.Test" proxy="NHibernate.Test.ProxyInterface.CastleProxy, NHibernate.Test" table="avalon_p" > <id name="Id"> <generator class="assigned" /> </id> <property name="Name" /> </class> </hibernate-mapping> --- NEW FILE: CastleProxy.cs --- using System; namespace NHibernate.Test.ProxyInterface { /// <summary> /// Summary description for CastleProxy. /// </summary> public interface CastleProxy { int Id { get; set; } string Name { get; set; } } } --- NEW FILE: CastleProxyImpl.cs --- using System; using NHibernate.Test.ProxyInterface; namespace NHibernate.Test.ProxyInterface { /// <summary> /// Summary description for CastleProxyImpl. /// </summary> [Serializable] public class CastleProxyImpl : CastleProxy { private int _id; private string _name; #region CastleProxy Members public int Id { get { return _id; } set { _id = value; } } public string Name { get { return _name; } set { _name = value; } } #endregion } } |