11using System . Collections . Generic ;
2- using System . Reflection ;
32using NHibernate . Transform ;
43using NUnit . Framework ;
54
@@ -8,55 +7,25 @@ namespace NHibernate.Test.NHSpecificTest.NH3957
87 [ TestFixture ]
98 public class ResultTransformerEqualityFixture
109 {
11- /// <summary>
12- /// Allows to simulate a hashcode collision. Issue would be unpractical to test otherwise.
13- /// Hashcode collision must be supported for avoiding unexpected and hard to reproduce failures.
14- /// </summary>
15- private void TweakHashcode ( System . Type transformerToTweak , object hasher )
16- {
17- var hasherTargetField = transformerToTweak . GetField ( "Hasher" , BindingFlags . Static | BindingFlags . NonPublic ) ;
18- if ( ! _hasherBackup . ContainsKey ( transformerToTweak ) )
19- _hasherBackup . Add ( transformerToTweak , hasherTargetField . GetValue ( null ) ) ;
20-
21- // Though hasher is a readonly field, this works at the time of this writing. If it starts breaking and cannot be fixed,
22- // ignore those tests or throw them away.
23- hasherTargetField . SetValue ( null , hasher ) ;
24- }
25-
26- private Dictionary < System . Type , object > _hasherBackup = new Dictionary < System . Type , object > ( ) ;
27-
28- [ SetUp ]
29- public void Setup ( )
30- {
31- var hasherForAll = typeof ( AliasToEntityMapResultTransformer )
32- . GetField ( "Hasher" , BindingFlags . Static | BindingFlags . NonPublic )
33- . GetValue ( null ) ;
34- TweakHashcode ( typeof ( DistinctRootEntityResultTransformer ) , hasherForAll ) ;
35- TweakHashcode ( typeof ( PassThroughResultTransformer ) , hasherForAll ) ;
36- TweakHashcode ( typeof ( RootEntityResultTransformer ) , hasherForAll ) ;
37- TweakHashcode ( typeof ( ToListResultTransformer ) , hasherForAll ) ;
38- }
39-
40- [ TearDown ]
41- public void TearDown ( )
42- {
43- // Restore those types hashcode. (Avoid impacting perf of other tests, avoid second level query cache
44- // issues if it was holding cached entries (but would mean some tests have not cleaned up properly).)
45- foreach ( var backup in _hasherBackup )
46- {
47- TweakHashcode ( backup . Key , backup . Value ) ;
48- }
49- }
50-
10+ public class CustomAliasToEntityMapResultTransformer : AliasToEntityMapResultTransformer { }
11+ public class CustomDistinctRootEntityResultTransformer : DistinctRootEntityResultTransformer { }
12+ public class CustomPassThroughResultTransformer : PassThroughResultTransformer { }
13+ public class CustomRootEntityResultTransformer : RootEntityResultTransformer { }
14+
5115 // Non reg test case
5216 [ Test ]
5317 public void AliasToEntityMapEquality ( )
5418 {
5519 var transf1 = new AliasToEntityMapResultTransformer ( ) ;
5620 var transf2 = new AliasToEntityMapResultTransformer ( ) ;
21+ var custom = new CustomAliasToEntityMapResultTransformer ( ) ;
22+ HashSet < IResultTransformer > set = new HashSet < IResultTransformer > ( ) { transf1 , transf2 , custom , } ;
5723
24+ Assert . That ( set . Count , Is . EqualTo ( 2 ) ) ;
5825 Assert . IsTrue ( transf1 . Equals ( transf2 ) ) ;
5926 Assert . IsTrue ( transf2 . Equals ( transf1 ) ) ;
27+ Assert . False ( custom . Equals ( transf1 ) ) ;
28+ Assert . False ( transf1 . Equals ( custom ) ) ;
6029 }
6130
6231 [ Test ]
@@ -75,9 +44,14 @@ public void DistinctRootEntityEquality()
7544 {
7645 var transf1 = new DistinctRootEntityResultTransformer ( ) ;
7746 var transf2 = new DistinctRootEntityResultTransformer ( ) ;
47+ var custom = new CustomDistinctRootEntityResultTransformer ( ) ;
48+ HashSet < IResultTransformer > set = new HashSet < IResultTransformer > ( ) { transf1 , transf2 , custom , } ;
7849
50+ Assert . That ( set . Count , Is . EqualTo ( 2 ) ) ;
7951 Assert . IsTrue ( transf1 . Equals ( transf2 ) ) ;
8052 Assert . IsTrue ( transf2 . Equals ( transf1 ) ) ;
53+ Assert . False ( custom . Equals ( transf1 ) ) ;
54+ Assert . False ( transf1 . Equals ( custom ) ) ;
8155 }
8256
8357 // Non reg test case
@@ -86,9 +60,14 @@ public void PassThroughEquality()
8660 {
8761 var transf1 = new PassThroughResultTransformer ( ) ;
8862 var transf2 = new PassThroughResultTransformer ( ) ;
63+ var custom = new CustomPassThroughResultTransformer ( ) ;
64+ HashSet < IResultTransformer > set = new HashSet < IResultTransformer > ( ) { transf1 , transf2 , custom , } ;
8965
66+ Assert . That ( set . Count , Is . EqualTo ( 2 ) ) ;
9067 Assert . IsTrue ( transf1 . Equals ( transf2 ) ) ;
9168 Assert . IsTrue ( transf2 . Equals ( transf1 ) ) ;
69+ Assert . False ( custom . Equals ( transf1 ) ) ;
70+ Assert . False ( transf1 . Equals ( custom ) ) ;
9271 }
9372
9473 [ Test ]
@@ -107,9 +86,14 @@ public void RootEntityEquality()
10786 {
10887 var transf1 = new RootEntityResultTransformer ( ) ;
10988 var transf2 = new RootEntityResultTransformer ( ) ;
89+ var custom = new CustomRootEntityResultTransformer ( ) ;
90+ HashSet < IResultTransformer > set = new HashSet < IResultTransformer > ( ) { transf1 , transf2 , custom , } ;
11091
92+ Assert . That ( set . Count , Is . EqualTo ( 2 ) ) ;
11193 Assert . IsTrue ( transf1 . Equals ( transf2 ) ) ;
11294 Assert . IsTrue ( transf2 . Equals ( transf1 ) ) ;
95+ Assert . False ( custom . Equals ( transf1 ) ) ;
96+ Assert . False ( transf1 . Equals ( custom ) ) ;
11397 }
11498
11599 [ Test ]
@@ -128,9 +112,14 @@ public void ToListEquality()
128112 {
129113 var transf1 = new ToListResultTransformer ( ) ;
130114 var transf2 = new ToListResultTransformer ( ) ;
115+ var custom = new CustomRootEntityResultTransformer ( ) ;
116+ HashSet < IResultTransformer > set = new HashSet < IResultTransformer > ( ) { transf1 , transf2 , custom , } ;
131117
118+ Assert . That ( set . Count , Is . EqualTo ( 2 ) ) ;
132119 Assert . IsTrue ( transf1 . Equals ( transf2 ) ) ;
133120 Assert . IsTrue ( transf2 . Equals ( transf1 ) ) ;
121+ Assert . False ( custom . Equals ( transf1 ) ) ;
122+ Assert . False ( transf1 . Equals ( custom ) ) ;
134123 }
135124 }
136- }
125+ }
0 commit comments