From: <fab...@us...> - 2011-03-20 20:30:59
|
Revision: 5481 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5481&view=rev Author: fabiomaulo Date: 2011-03-20 20:30:53 +0000 (Sun, 20 Mar 2011) Log Message: ----------- Added base common test for interface proxy Serialization Modified Paths: -------------- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate.Test/DynamicProxyTests/InterfaceProxySerializationTests/ trunk/nhibernate/src/NHibernate.Test/DynamicProxyTests/InterfaceProxySerializationTests/IMyProxy.cs trunk/nhibernate/src/NHibernate.Test/DynamicProxyTests/InterfaceProxySerializationTests/MyProxyImpl.cs trunk/nhibernate/src/NHibernate.Test/DynamicProxyTests/InterfaceProxySerializationTests/ProxyFixture.cs trunk/nhibernate/src/NHibernate.Test/DynamicProxyTests/InterfaceProxySerializationTests/ProxyImpl.hbm.xml Added: trunk/nhibernate/src/NHibernate.Test/DynamicProxyTests/InterfaceProxySerializationTests/IMyProxy.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/DynamicProxyTests/InterfaceProxySerializationTests/IMyProxy.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/DynamicProxyTests/InterfaceProxySerializationTests/IMyProxy.cs 2011-03-20 20:30:53 UTC (rev 5481) @@ -0,0 +1,11 @@ +namespace NHibernate.Test.DynamicProxyTests.InterfaceProxySerializationTests +{ + public interface IMyProxy + { + int Id { get; set; } + + string Name { get; set; } + + void ThrowDeepException(); + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/DynamicProxyTests/InterfaceProxySerializationTests/MyProxyImpl.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/DynamicProxyTests/InterfaceProxySerializationTests/MyProxyImpl.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/DynamicProxyTests/InterfaceProxySerializationTests/MyProxyImpl.cs 2011-03-20 20:30:53 UTC (rev 5481) @@ -0,0 +1,32 @@ +using System; + +namespace NHibernate.Test.DynamicProxyTests.InterfaceProxySerializationTests +{ + public class MyProxyImpl: IMyProxy + { + [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] + private static void Level1() + { + Level2(); + } + + [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] + private static void Level2() + { + throw new ArgumentException("thrown from Level2"); + } + + #region CastleProxy Members + + public int Id { get; set; } + + public string Name { get; set; } + + public void ThrowDeepException() + { + Level1(); + } + + #endregion + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/DynamicProxyTests/InterfaceProxySerializationTests/ProxyFixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/DynamicProxyTests/InterfaceProxySerializationTests/ProxyFixture.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/DynamicProxyTests/InterfaceProxySerializationTests/ProxyFixture.cs 2011-03-20 20:30:53 UTC (rev 5481) @@ -0,0 +1,142 @@ +using System; +using System.Collections; +using System.IO; +using System.Runtime.Serialization; +using System.Runtime.Serialization.Formatters.Binary; +using NUnit.Framework; + +namespace NHibernate.Test.DynamicProxyTests.InterfaceProxySerializationTests +{ + [TestFixture] + public class ProxyFixture : TestCase + { + protected override IList Mappings + { + get { return new[] { "DynamicProxyTests.InterfaceProxySerializationTests.ProxyImpl.hbm.xml" }; } + } + + protected override string MappingsAssembly + { + get { return "NHibernate.Test"; } + } + + private void SerializeAndDeserialize(ref ISession s) + { + // Serialize the session + using (Stream stream = new MemoryStream()) + { + IFormatter formatter = new BinaryFormatter(); + formatter.Serialize(stream, s); + + // Close the original session + s.Close(); + + // Deserialize the session + stream.Position = 0; + s = (ISession) formatter.Deserialize(stream); + } + } + + [Test] + public void ExceptionStackTrace() + { + ISession s = OpenSession(); + IMyProxy ap = new MyProxyImpl {Id = 1, Name = "first proxy"}; + s.Save(ap); + s.Flush(); + s.Close(); + + s = OpenSession(); + ap = (IMyProxy) s.Load(typeof (MyProxyImpl), ap.Id); + Assert.IsFalse(NHibernateUtil.IsInitialized(ap), "check we have a proxy"); + + try + { + ap.ThrowDeepException(); + Assert.Fail("Exception not thrown"); + } + catch (ArgumentException ae) + { + Assert.AreEqual("thrown from Level2", ae.Message); + + string[] stackTraceLines = ae.StackTrace.Split('\n'); + Assert.IsTrue(stackTraceLines[0].Contains("Level2"), "top of exception stack is Level2()"); + Assert.IsTrue(stackTraceLines[1].Contains("Level1"), "next on exception stack is Level1()"); + } + finally + { + s.Delete(ap); + s.Flush(); + s.Close(); + } + } + + [Test] + public void Proxy() + { + ISession s = OpenSession(); + IMyProxy ap = new MyProxyImpl {Id = 1, Name = "first proxy"}; + s.Save(ap); + s.Flush(); + s.Close(); + + s = OpenSession(); + ap = (IMyProxy) s.Load(typeof (MyProxyImpl), ap.Id); + Assert.IsFalse(NHibernateUtil.IsInitialized(ap)); + int id = ap.Id; + Assert.IsFalse(NHibernateUtil.IsInitialized(ap), "get id should not have initialized it."); + string name = ap.Name; + Assert.IsTrue(NHibernateUtil.IsInitialized(ap), "get name should have initialized it."); + s.Delete(ap); + s.Flush(); + s.Close(); + } + + [Test] + public void ProxySerialize() + { + ISession s = OpenSession(); + IMyProxy ap = new MyProxyImpl {Id = 1, Name = "first proxy"}; + s.Save(ap); + s.Flush(); + s.Close(); + + s = OpenSession(); + ap = (IMyProxy) s.Load(typeof (MyProxyImpl), ap.Id); + Assert.AreEqual(1, ap.Id); + s.Disconnect(); + + SerializeAndDeserialize(ref s); + + 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. + SerializeAndDeserialize(ref s); + + s.Close(); + + s = OpenSession(); + s.Delete(ap); + s.Flush(); + s.Close(); + } + + [Test] + public void SerializeNotFoundProxy() + { + ISession s = OpenSession(); + // this does not actually exists in db + var notThere = (IMyProxy) s.Load(typeof (MyProxyImpl), 5); + Assert.AreEqual(5, notThere.Id); + s.Disconnect(); + + // serialize and then deserialize the session. + SerializeAndDeserialize(ref s); + + Assert.IsNotNull(s.Load(typeof (MyProxyImpl), 5), "should be proxy - even though it doesn't exists in db"); + s.Close(); + } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/DynamicProxyTests/InterfaceProxySerializationTests/ProxyImpl.hbm.xml =================================================================== --- trunk/nhibernate/src/NHibernate.Test/DynamicProxyTests/InterfaceProxySerializationTests/ProxyImpl.hbm.xml (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/DynamicProxyTests/InterfaceProxySerializationTests/ProxyImpl.hbm.xml 2011-03-20 20:30:53 UTC (rev 5481) @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="utf-8" ?> +<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" + assembly="NHibernate.Test" + namespace="NHibernate.Test.DynamicProxyTests.InterfaceProxySerializationTests"> + <class name="MyProxyImpl" proxy="IMyProxy"> + <id name="Id"> + <generator class="assigned" /> + </id> + + <property name="Name" /> + </class> +</hibernate-mapping> \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2011-03-20 20:20:35 UTC (rev 5480) +++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2011-03-20 20:30:53 UTC (rev 5481) @@ -217,6 +217,9 @@ <Compile Include="DriverTest\Sql2008DateTime2Test.cs" /> <Compile Include="DriverTest\SqlClientDriverFixture.cs" /> <Compile Include="DriverTest\SqlServerCeDriverFixture.cs" /> + <Compile Include="DynamicProxyTests\InterfaceProxySerializationTests\IMyProxy.cs" /> + <Compile Include="DynamicProxyTests\InterfaceProxySerializationTests\MyProxyImpl.cs" /> + <Compile Include="DynamicProxyTests\InterfaceProxySerializationTests\ProxyFixture.cs" /> <Compile Include="DynamicProxyTests\InterfaceWithEqualsGethashcodeTests.cs" /> <Compile Include="EngineTest\CallableParserFixture.cs" /> <Compile Include="EngineTest\NativeSQLQueryNonScalarReturnTest.cs" /> @@ -2466,6 +2469,7 @@ <EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" /> </ItemGroup> <ItemGroup> + <EmbeddedResource Include="DynamicProxyTests\InterfaceProxySerializationTests\ProxyImpl.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\NH2565\Mappings.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\AccessAndCorrectPropertyName\DogMapping.hbm.xml" /> <EmbeddedResource Include="NHSpecificTest\AccessAndCorrectPropertyName\PersonMapping.hbm.xml" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |