Skip to content

Commit 879ca6f

Browse files
committed
Cleanup.
1 parent 06bbb63 commit 879ca6f

File tree

4 files changed

+156
-158
lines changed

4 files changed

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

src/NHibernate/Tuple/EntityModeToTuplizerMapping.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,24 @@ public abstract class EntityModeToTuplizerMapping : IDeserializationCallback
1111
{
1212

1313
// NH-1660
14-
private readonly IDictionary<EntityMode, ITuplizer> tuplizers
15-
= new LinkedHashMap<EntityMode, ITuplizer>(5, new EntityModeEqualityComparer());
16-
[NonSerialized()]
17-
private bool isFullyDeserialized = false;
14+
private readonly IDictionary<EntityMode, ITuplizer> _tuplizers
15+
= new LinkedHashMap<EntityMode, ITuplizer>(5, new EntityModeEqualityComparer());
1816

19-
public EntityModeToTuplizerMapping()
17+
/// <summary>
18+
/// This class might get called during serialization, and may therefore need to deserialize on-demand.
19+
/// </summary>
20+
[NonSerialized] private bool _isFullyDeserialized;
21+
22+
23+
protected EntityModeToTuplizerMapping()
2024
{
21-
isFullyDeserialized = true;
25+
_isFullyDeserialized = true;
2226
}
2327

2428
protected internal void AddTuplizer(EntityMode entityMode, ITuplizer tuplizer)
2529
{
2630
EnsureFullyDeserialized();
27-
tuplizers[entityMode] = tuplizer;
31+
_tuplizers[entityMode] = tuplizer;
2832
}
2933

3034
/// <summary> Given a supposed instance of an entity/component, guess its entity mode. </summary>
@@ -33,7 +37,7 @@ protected internal void AddTuplizer(EntityMode entityMode, ITuplizer tuplizer)
3337
public virtual EntityMode? GuessEntityMode(object obj)
3438
{
3539
EnsureFullyDeserialized();
36-
foreach (KeyValuePair<EntityMode, ITuplizer> entry in tuplizers)
40+
foreach (KeyValuePair<EntityMode, ITuplizer> entry in _tuplizers)
3741
{
3842
ITuplizer tuplizer = entry.Value;
3943
if (tuplizer.IsInstance(obj))
@@ -54,7 +58,7 @@ public virtual ITuplizer GetTuplizerOrNull(EntityMode entityMode)
5458
{
5559
EnsureFullyDeserialized();
5660
ITuplizer result;
57-
tuplizers.TryGetValue(entityMode, out result);
61+
_tuplizers.TryGetValue(entityMode, out result);
5862
return result;
5963
}
6064

@@ -80,16 +84,16 @@ public virtual ITuplizer GetTuplizer(EntityMode entityMode)
8084

8185
private void EnsureFullyDeserialized()
8286
{
83-
if (!isFullyDeserialized)
87+
if (!_isFullyDeserialized)
8488
{
85-
((IDeserializationCallback)this).OnDeserialization(this);
89+
((IDeserializationCallback) this).OnDeserialization(this);
8690
}
8791
}
8892

8993
void IDeserializationCallback.OnDeserialization(object sender)
9094
{
91-
((IDeserializationCallback)tuplizers).OnDeserialization(sender);
92-
isFullyDeserialized = true;
95+
((IDeserializationCallback) _tuplizers).OnDeserialization(sender);
96+
_isFullyDeserialized = true;
9397
}
9498
}
9599
}

0 commit comments

Comments
 (0)