Skip to content

Commit d8c15f0

Browse files
committed
NH-3487 - Added test case
1 parent 612d1b6 commit d8c15f0

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using Iesi.Collections.Generic;
3+
4+
namespace NHibernate.Test.NHSpecificTest.NH3487
5+
{
6+
[Serializable()]
7+
public class Entity
8+
{
9+
public virtual Key Id { get; set; }
10+
public virtual string Name { get; set; }
11+
12+
public override bool Equals(object obj)
13+
{
14+
if(obj is Entity)
15+
{
16+
var otherEntity = (Entity)obj;
17+
return otherEntity.Id.Equals(this.Id);
18+
}
19+
return false;
20+
}
21+
22+
public override int GetHashCode()
23+
{
24+
return Id.GetHashCode();
25+
}
26+
}
27+
28+
[Serializable()]
29+
public class Key
30+
{
31+
public virtual int Id { get; set; }
32+
33+
public override bool Equals(object obj)
34+
{
35+
if (obj is Key)
36+
{
37+
var otherEntity = (Key)obj;
38+
return otherEntity.Id == this.Id;
39+
}
40+
return false;
41+
}
42+
43+
public override int GetHashCode()
44+
{
45+
// Important to reproduce the problem - forces the keys to collide in the hash map
46+
return 1;
47+
}
48+
}
49+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
using System.Linq;
3+
using System.IO;
4+
using System.Runtime.Serialization;
5+
using System.Runtime.Serialization.Formatters.Binary;
6+
using NHibernate.Linq;
7+
using NHibernate.Collection;
8+
using NUnit.Framework;
9+
10+
namespace NHibernate.Test.NHSpecificTest.NH3487
11+
{
12+
[TestFixture]
13+
public class Fixture : BugTestCase
14+
{
15+
private Key key1;
16+
private Key key2;
17+
18+
public override string BugNumber
19+
{
20+
get { return "NH3487"; }
21+
}
22+
23+
protected override void OnSetUp()
24+
{
25+
using (ISession session = OpenSession())
26+
{
27+
using (ITransaction transaction = session.BeginTransaction())
28+
{
29+
key1 = new Key() { Id = 1 };
30+
var entity1 = new Entity { Id = key1, Name = "Bob1" };
31+
session.Save(entity1);
32+
33+
key2 = new Key() { Id = 2 };
34+
var entity2 = new Entity { Id = key2, Name = "Bob2" };
35+
session.Save(entity2);
36+
37+
session.Flush();
38+
transaction.Commit();
39+
}
40+
}
41+
}
42+
43+
protected override void OnTearDown()
44+
{
45+
using (ISession session = OpenSession())
46+
{
47+
using (ITransaction transaction = session.BeginTransaction())
48+
{
49+
session.Delete("from System.Object");
50+
51+
session.Flush();
52+
transaction.Commit();
53+
}
54+
}
55+
}
56+
57+
[Test]
58+
public void Test()
59+
{
60+
IFormatter formatter = new BinaryFormatter();
61+
byte[] serializedSessionArray;
62+
63+
using (ISession session = OpenSession())
64+
{
65+
using (session.BeginTransaction())
66+
{
67+
var entity1 = session.Get<Entity>(key1);
68+
var entity2 = session.Get<Entity>(key2);
69+
}
70+
71+
session.Disconnect();
72+
using (var serializationStream = new MemoryStream())
73+
{
74+
formatter.Serialize(serializationStream, session);
75+
serializationStream.Close();
76+
serializedSessionArray = serializationStream.ToArray();
77+
}
78+
}
79+
80+
ISession deserializedSession;
81+
using (var serializationStream = new MemoryStream(serializedSessionArray))
82+
{
83+
deserializedSession = (ISession)formatter.Deserialize(serializationStream);
84+
}
85+
86+
}
87+
}
88+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.NH3487">
3+
4+
<class name="Entity">
5+
<composite-id name="Id">
6+
<key-property name="Id" />
7+
</composite-id>
8+
<property name="Name" not-null="true" />
9+
</class>
10+
11+
</hibernate-mapping>

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,8 @@
672672
<Compile Include="NHSpecificTest\BagWithLazyExtraAndFilter\Domain.cs" />
673673
<Compile Include="NHSpecificTest\BagWithLazyExtraAndFilter\Fixture.cs" />
674674
<Compile Include="Component\Basic\ComponentWithUniqueConstraintTests.cs" />
675+
<Compile Include="NHSpecificTest\NH3487\Entity.cs" />
676+
<Compile Include="NHSpecificTest\NH3487\Fixture.cs" />
675677
<Compile Include="NHSpecificTest\NH3459\Fixture.cs" />
676678
<Compile Include="NHSpecificTest\NH3459\Order.cs" />
677679
<Compile Include="NHSpecificTest\NH2692\Fixture.cs" />
@@ -2962,6 +2964,7 @@
29622964
<EmbeddedResource Include="NHSpecificTest\NH1291AnonExample\Mappings.hbm.xml" />
29632965
</ItemGroup>
29642966
<ItemGroup>
2967+
<EmbeddedResource Include="NHSpecificTest\NH3487\Mappings.hbm.xml" />
29652968
<EmbeddedResource Include="NHSpecificTest\NH2692\Mappings.hbm.xml">
29662969
<SubType>Designer</SubType>
29672970
</EmbeddedResource>

0 commit comments

Comments
 (0)