1
1
using System ;
2
2
using NHibernate . Proxy ;
3
3
using NUnit . Framework ;
4
- using System . Collections . Generic ;
5
4
6
5
namespace NHibernate . Test . NHSpecificTest . ProxyValidator
7
6
{
@@ -10,15 +9,6 @@ public class Fixture
10
9
{
11
10
private readonly IProxyValidator pv = new DynProxyTypeValidator ( ) ;
12
11
13
- private void Validate ( System . Type type )
14
- {
15
- ICollection < string > errors = pv . ValidateType ( type ) ;
16
- if ( errors != null )
17
- {
18
- throw new InvalidProxyTypeException ( errors ) ;
19
- }
20
- }
21
-
22
12
public class ValidClass
23
13
{
24
14
private int privateField ;
@@ -64,12 +54,35 @@ protected int NonVirtualPrivateProperty
64
54
#pragma warning restore 67
65
55
}
66
56
57
+ [ Test ]
58
+ public void ObjectIsValid ( )
59
+ {
60
+ var errors = pv . ValidateType ( typeof ( object ) ) ;
61
+ Assert . That ( errors , Is . Null ) ;
62
+ }
63
+
67
64
[ Test ]
68
65
public void ValidClassTest ( )
69
66
{
70
- Validate ( typeof ( ValidClass ) ) ;
67
+ var errors = pv . ValidateType ( typeof ( ValidClass ) ) ;
68
+ Assert . That ( errors , Is . Null ) ;
71
69
}
72
70
71
+ public class InvalidSealedToString : ValidClass
72
+ {
73
+ public sealed override string ToString ( )
74
+ {
75
+ return base . ToString ( ) ;
76
+ }
77
+ }
78
+
79
+ [ Test ]
80
+ public void SealedObjectOverride ( )
81
+ {
82
+ var errors = pv . ValidateType ( typeof ( InvalidSealedToString ) ) ;
83
+ Assert . That ( errors , Has . Count . EqualTo ( 1 ) ) ;
84
+ }
85
+
73
86
public class InvalidPrivateConstructor : ValidClass
74
87
{
75
88
private InvalidPrivateConstructor ( )
@@ -80,7 +93,8 @@ private InvalidPrivateConstructor()
80
93
[ Test ]
81
94
public void PrivateConstructor ( )
82
95
{
83
- Assert . Throws < InvalidProxyTypeException > ( ( ) => Validate ( typeof ( InvalidPrivateConstructor ) ) ) ;
96
+ var errors = pv . ValidateType ( typeof ( InvalidPrivateConstructor ) ) ;
97
+ Assert . That ( errors , Has . Count . EqualTo ( 1 ) ) ;
84
98
}
85
99
86
100
public class InvalidNonVirtualProperty : ValidClass
@@ -95,7 +109,8 @@ public int NonVirtualProperty
95
109
[ Test ]
96
110
public void NonVirtualProperty ( )
97
111
{
98
- Assert . Throws < InvalidProxyTypeException > ( ( ) => Validate ( typeof ( InvalidNonVirtualProperty ) ) ) ;
112
+ var errors = pv . ValidateType ( typeof ( InvalidNonVirtualProperty ) ) ;
113
+ Assert . That ( errors , Has . Count . EqualTo ( 2 ) ) ;
99
114
}
100
115
101
116
public class InvalidPublicField : ValidClass
@@ -106,7 +121,8 @@ public class InvalidPublicField : ValidClass
106
121
[ Test ]
107
122
public void PublicField ( )
108
123
{
109
- Assert . Throws < InvalidProxyTypeException > ( ( ) => Validate ( typeof ( InvalidPublicField ) ) ) ;
124
+ var errors = pv . ValidateType ( typeof ( InvalidPublicField ) ) ;
125
+ Assert . That ( errors , Has . Count . EqualTo ( 1 ) ) ;
110
126
}
111
127
112
128
public class InvalidNonVirtualEvent : ValidClass
@@ -119,7 +135,8 @@ public class InvalidNonVirtualEvent : ValidClass
119
135
[ Test ]
120
136
public void NonVirtualEvent ( )
121
137
{
122
- Assert . Throws < InvalidProxyTypeException > ( ( ) => Validate ( typeof ( InvalidNonVirtualEvent ) ) ) ;
138
+ var errors = pv . ValidateType ( typeof ( InvalidNonVirtualEvent ) ) ;
139
+ Assert . That ( errors , Has . Count . EqualTo ( 2 ) ) ;
123
140
}
124
141
125
142
public interface ValidInterface
@@ -129,7 +146,8 @@ public interface ValidInterface
129
146
[ Test ]
130
147
public void Interface ( )
131
148
{
132
- Validate ( typeof ( ValidInterface ) ) ;
149
+ var errors = pv . ValidateType ( typeof ( ValidInterface ) ) ;
150
+ Assert . That ( errors , Is . Null ) ;
133
151
}
134
152
135
153
public class MultipleErrors
@@ -153,15 +171,8 @@ public int NonVirtualProperty
153
171
[ Test ]
154
172
public void MultipleErrorsReported ( )
155
173
{
156
- try
157
- {
158
- Validate ( typeof ( MultipleErrors ) ) ;
159
- Assert . Fail ( "Should have failed validation" ) ;
160
- }
161
- catch ( InvalidProxyTypeException e )
162
- {
163
- Assert . IsTrue ( e . Errors . Count > 1 ) ;
164
- }
174
+ var errors = pv . ValidateType ( typeof ( MultipleErrors ) ) ;
175
+ Assert . That ( errors , Has . Count . GreaterThan ( 1 ) ) ;
165
176
}
166
177
167
178
public class InvalidNonVirtualInternalProperty : ValidClass
@@ -183,16 +194,18 @@ public class InvalidInternalField : ValidClass
183
194
[ Test ]
184
195
public void NonVirtualInternal ( )
185
196
{
186
- Assert . Throws < InvalidProxyTypeException > ( ( ) => Validate ( typeof ( InvalidNonVirtualInternalProperty ) ) ) ;
197
+ var errors = pv . ValidateType ( typeof ( InvalidNonVirtualInternalProperty ) ) ;
198
+ Assert . That ( errors , Has . Count . EqualTo ( 2 ) ) ;
187
199
}
188
200
189
201
[ Test ]
190
202
public void InternalField ( )
191
203
{
192
- Assert . Throws < InvalidProxyTypeException > ( ( ) => Validate ( typeof ( InvalidInternalField ) ) ) ;
204
+ var errors = pv . ValidateType ( typeof ( InvalidInternalField ) ) ;
205
+ Assert . That ( errors , Has . Count . EqualTo ( 1 ) ) ;
193
206
}
194
207
195
- public class InvalidNonVirtualProtectedProperty : ValidClass
208
+ public class ValidNonVirtualProtectedProperty : ValidClass
196
209
{
197
210
protected int NonVirtualProperty
198
211
{
@@ -204,8 +217,8 @@ protected int NonVirtualProperty
204
217
[ Test ]
205
218
public void NonVirtualProtected ( )
206
219
{
207
- Validate ( typeof ( InvalidNonVirtualProtectedProperty ) ) ;
208
- Assert . IsTrue ( true , "Always should pass, protected members do not need to be virtual." ) ;
220
+ var errors = pv . ValidateType ( typeof ( ValidNonVirtualProtectedProperty ) ) ;
221
+ Assert . That ( errors , Is . Null ) ;
209
222
}
210
223
211
224
public class InvalidNonVirtualProtectedInternalProperty : ValidClass
@@ -220,7 +233,8 @@ protected internal int NonVirtualProperty
220
233
[ Test ]
221
234
public void NonVirtualProtectedInternal ( )
222
235
{
223
- Assert . Throws < InvalidProxyTypeException > ( ( ) => Validate ( typeof ( InvalidNonVirtualProtectedInternalProperty ) ) ) ;
236
+ var errors = pv . ValidateType ( typeof ( InvalidNonVirtualProtectedInternalProperty ) ) ;
237
+ Assert . That ( errors , Has . Count . EqualTo ( 2 ) ) ;
224
238
}
225
239
226
240
interface INonVirtualPublicImplementsInterface
@@ -239,7 +253,8 @@ public int NonVirtualMethodImplementsInterface
239
253
[ Test ]
240
254
public void VirtualPublicImplementsInterface ( )
241
255
{
242
- Assert . Throws < InvalidProxyTypeException > ( ( ) => Validate ( typeof ( NonVirtualPublicImplementsInterface ) ) ) ;
256
+ var errors = pv . ValidateType ( typeof ( NonVirtualPublicImplementsInterface ) ) ;
257
+ Assert . That ( errors , Has . Count . EqualTo ( 1 ) ) ;
243
258
}
244
259
245
260
public class InvalidVirtualPrivateAutoProperty : ValidClass
@@ -254,7 +269,8 @@ public virtual int NonVirtualSetterProperty
254
269
[ Test ]
255
270
public void PrivateSetterOnVirtualPropertyShouldThrows ( )
256
271
{
257
- Assert . Throws < InvalidProxyTypeException > ( ( ) => Validate ( typeof ( InvalidVirtualPrivateAutoProperty ) ) ) ;
272
+ var errors = pv . ValidateType ( typeof ( InvalidVirtualPrivateAutoProperty ) ) ;
273
+ Assert . That ( errors , Has . Count . EqualTo ( 1 ) ) ;
258
274
}
259
275
}
260
276
}
0 commit comments