1
1
using System . Collections . Generic ;
2
- using System . Reflection ;
3
2
using NHibernate . Transform ;
4
3
using NUnit . Framework ;
5
4
@@ -8,55 +7,25 @@ namespace NHibernate.Test.NHSpecificTest.NH3957
8
7
[ TestFixture ]
9
8
public class ResultTransformerEqualityFixture
10
9
{
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
+
51
15
// Non reg test case
52
16
[ Test ]
53
17
public void AliasToEntityMapEquality ( )
54
18
{
55
19
var transf1 = new AliasToEntityMapResultTransformer ( ) ;
56
20
var transf2 = new AliasToEntityMapResultTransformer ( ) ;
21
+ var custom = new CustomAliasToEntityMapResultTransformer ( ) ;
22
+ HashSet < IResultTransformer > set = new HashSet < IResultTransformer > ( ) { transf1 , transf2 , custom , } ;
57
23
24
+ Assert . That ( set . Count , Is . EqualTo ( 2 ) ) ;
58
25
Assert . IsTrue ( transf1 . Equals ( transf2 ) ) ;
59
26
Assert . IsTrue ( transf2 . Equals ( transf1 ) ) ;
27
+ Assert . False ( custom . Equals ( transf1 ) ) ;
28
+ Assert . False ( transf1 . Equals ( custom ) ) ;
60
29
}
61
30
62
31
[ Test ]
@@ -75,9 +44,14 @@ public void DistinctRootEntityEquality()
75
44
{
76
45
var transf1 = new DistinctRootEntityResultTransformer ( ) ;
77
46
var transf2 = new DistinctRootEntityResultTransformer ( ) ;
47
+ var custom = new CustomDistinctRootEntityResultTransformer ( ) ;
48
+ HashSet < IResultTransformer > set = new HashSet < IResultTransformer > ( ) { transf1 , transf2 , custom , } ;
78
49
50
+ Assert . That ( set . Count , Is . EqualTo ( 2 ) ) ;
79
51
Assert . IsTrue ( transf1 . Equals ( transf2 ) ) ;
80
52
Assert . IsTrue ( transf2 . Equals ( transf1 ) ) ;
53
+ Assert . False ( custom . Equals ( transf1 ) ) ;
54
+ Assert . False ( transf1 . Equals ( custom ) ) ;
81
55
}
82
56
83
57
// Non reg test case
@@ -86,9 +60,14 @@ public void PassThroughEquality()
86
60
{
87
61
var transf1 = new PassThroughResultTransformer ( ) ;
88
62
var transf2 = new PassThroughResultTransformer ( ) ;
63
+ var custom = new CustomPassThroughResultTransformer ( ) ;
64
+ HashSet < IResultTransformer > set = new HashSet < IResultTransformer > ( ) { transf1 , transf2 , custom , } ;
89
65
66
+ Assert . That ( set . Count , Is . EqualTo ( 2 ) ) ;
90
67
Assert . IsTrue ( transf1 . Equals ( transf2 ) ) ;
91
68
Assert . IsTrue ( transf2 . Equals ( transf1 ) ) ;
69
+ Assert . False ( custom . Equals ( transf1 ) ) ;
70
+ Assert . False ( transf1 . Equals ( custom ) ) ;
92
71
}
93
72
94
73
[ Test ]
@@ -107,9 +86,14 @@ public void RootEntityEquality()
107
86
{
108
87
var transf1 = new RootEntityResultTransformer ( ) ;
109
88
var transf2 = new RootEntityResultTransformer ( ) ;
89
+ var custom = new CustomRootEntityResultTransformer ( ) ;
90
+ HashSet < IResultTransformer > set = new HashSet < IResultTransformer > ( ) { transf1 , transf2 , custom , } ;
110
91
92
+ Assert . That ( set . Count , Is . EqualTo ( 2 ) ) ;
111
93
Assert . IsTrue ( transf1 . Equals ( transf2 ) ) ;
112
94
Assert . IsTrue ( transf2 . Equals ( transf1 ) ) ;
95
+ Assert . False ( custom . Equals ( transf1 ) ) ;
96
+ Assert . False ( transf1 . Equals ( custom ) ) ;
113
97
}
114
98
115
99
[ Test ]
@@ -128,9 +112,14 @@ public void ToListEquality()
128
112
{
129
113
var transf1 = new ToListResultTransformer ( ) ;
130
114
var transf2 = new ToListResultTransformer ( ) ;
115
+ var custom = new CustomRootEntityResultTransformer ( ) ;
116
+ HashSet < IResultTransformer > set = new HashSet < IResultTransformer > ( ) { transf1 , transf2 , custom , } ;
131
117
118
+ Assert . That ( set . Count , Is . EqualTo ( 2 ) ) ;
132
119
Assert . IsTrue ( transf1 . Equals ( transf2 ) ) ;
133
120
Assert . IsTrue ( transf2 . Equals ( transf1 ) ) ;
121
+ Assert . False ( custom . Equals ( transf1 ) ) ;
122
+ Assert . False ( transf1 . Equals ( custom ) ) ;
134
123
}
135
124
}
136
- }
125
+ }
0 commit comments