11using System ;
22using NHibernate . Proxy ;
33using NUnit . Framework ;
4- using System . Collections . Generic ;
54
65namespace NHibernate . Test . NHSpecificTest . ProxyValidator
76{
@@ -10,15 +9,6 @@ public class Fixture
109 {
1110 private readonly IProxyValidator pv = new DynProxyTypeValidator ( ) ;
1211
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-
2212 public class ValidClass
2313 {
2414 private int privateField ;
@@ -64,12 +54,35 @@ protected int NonVirtualPrivateProperty
6454#pragma warning restore 67
6555 }
6656
57+ [ Test ]
58+ public void ObjectIsValid ( )
59+ {
60+ var errors = pv . ValidateType ( typeof ( object ) ) ;
61+ Assert . That ( errors , Is . Null ) ;
62+ }
63+
6764 [ Test ]
6865 public void ValidClassTest ( )
6966 {
70- Validate ( typeof ( ValidClass ) ) ;
67+ var errors = pv . ValidateType ( typeof ( ValidClass ) ) ;
68+ Assert . That ( errors , Is . Null ) ;
7169 }
7270
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+
7386 public class InvalidPrivateConstructor : ValidClass
7487 {
7588 private InvalidPrivateConstructor ( )
@@ -80,7 +93,8 @@ private InvalidPrivateConstructor()
8093 [ Test ]
8194 public void PrivateConstructor ( )
8295 {
83- Assert . Throws < InvalidProxyTypeException > ( ( ) => Validate ( typeof ( InvalidPrivateConstructor ) ) ) ;
96+ var errors = pv . ValidateType ( typeof ( InvalidPrivateConstructor ) ) ;
97+ Assert . That ( errors , Has . Count . EqualTo ( 1 ) ) ;
8498 }
8599
86100 public class InvalidNonVirtualProperty : ValidClass
@@ -95,7 +109,8 @@ public int NonVirtualProperty
95109 [ Test ]
96110 public void NonVirtualProperty ( )
97111 {
98- Assert . Throws < InvalidProxyTypeException > ( ( ) => Validate ( typeof ( InvalidNonVirtualProperty ) ) ) ;
112+ var errors = pv . ValidateType ( typeof ( InvalidNonVirtualProperty ) ) ;
113+ Assert . That ( errors , Has . Count . EqualTo ( 2 ) ) ;
99114 }
100115
101116 public class InvalidPublicField : ValidClass
@@ -106,7 +121,8 @@ public class InvalidPublicField : ValidClass
106121 [ Test ]
107122 public void PublicField ( )
108123 {
109- Assert . Throws < InvalidProxyTypeException > ( ( ) => Validate ( typeof ( InvalidPublicField ) ) ) ;
124+ var errors = pv . ValidateType ( typeof ( InvalidPublicField ) ) ;
125+ Assert . That ( errors , Has . Count . EqualTo ( 1 ) ) ;
110126 }
111127
112128 public class InvalidNonVirtualEvent : ValidClass
@@ -119,7 +135,8 @@ public class InvalidNonVirtualEvent : ValidClass
119135 [ Test ]
120136 public void NonVirtualEvent ( )
121137 {
122- Assert . Throws < InvalidProxyTypeException > ( ( ) => Validate ( typeof ( InvalidNonVirtualEvent ) ) ) ;
138+ var errors = pv . ValidateType ( typeof ( InvalidNonVirtualEvent ) ) ;
139+ Assert . That ( errors , Has . Count . EqualTo ( 2 ) ) ;
123140 }
124141
125142 public interface ValidInterface
@@ -129,7 +146,8 @@ public interface ValidInterface
129146 [ Test ]
130147 public void Interface ( )
131148 {
132- Validate ( typeof ( ValidInterface ) ) ;
149+ var errors = pv . ValidateType ( typeof ( ValidInterface ) ) ;
150+ Assert . That ( errors , Is . Null ) ;
133151 }
134152
135153 public class MultipleErrors
@@ -153,15 +171,8 @@ public int NonVirtualProperty
153171 [ Test ]
154172 public void MultipleErrorsReported ( )
155173 {
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 ) ) ;
165176 }
166177
167178 public class InvalidNonVirtualInternalProperty : ValidClass
@@ -183,16 +194,18 @@ public class InvalidInternalField : ValidClass
183194 [ Test ]
184195 public void NonVirtualInternal ( )
185196 {
186- Assert . Throws < InvalidProxyTypeException > ( ( ) => Validate ( typeof ( InvalidNonVirtualInternalProperty ) ) ) ;
197+ var errors = pv . ValidateType ( typeof ( InvalidNonVirtualInternalProperty ) ) ;
198+ Assert . That ( errors , Has . Count . EqualTo ( 2 ) ) ;
187199 }
188200
189201 [ Test ]
190202 public void InternalField ( )
191203 {
192- Assert . Throws < InvalidProxyTypeException > ( ( ) => Validate ( typeof ( InvalidInternalField ) ) ) ;
204+ var errors = pv . ValidateType ( typeof ( InvalidInternalField ) ) ;
205+ Assert . That ( errors , Has . Count . EqualTo ( 1 ) ) ;
193206 }
194207
195- public class InvalidNonVirtualProtectedProperty : ValidClass
208+ public class ValidNonVirtualProtectedProperty : ValidClass
196209 {
197210 protected int NonVirtualProperty
198211 {
@@ -204,8 +217,8 @@ protected int NonVirtualProperty
204217 [ Test ]
205218 public void NonVirtualProtected ( )
206219 {
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 ) ;
209222 }
210223
211224 public class InvalidNonVirtualProtectedInternalProperty : ValidClass
@@ -220,7 +233,8 @@ protected internal int NonVirtualProperty
220233 [ Test ]
221234 public void NonVirtualProtectedInternal ( )
222235 {
223- Assert . Throws < InvalidProxyTypeException > ( ( ) => Validate ( typeof ( InvalidNonVirtualProtectedInternalProperty ) ) ) ;
236+ var errors = pv . ValidateType ( typeof ( InvalidNonVirtualProtectedInternalProperty ) ) ;
237+ Assert . That ( errors , Has . Count . EqualTo ( 2 ) ) ;
224238 }
225239
226240 interface INonVirtualPublicImplementsInterface
@@ -239,7 +253,8 @@ public int NonVirtualMethodImplementsInterface
239253 [ Test ]
240254 public void VirtualPublicImplementsInterface ( )
241255 {
242- Assert . Throws < InvalidProxyTypeException > ( ( ) => Validate ( typeof ( NonVirtualPublicImplementsInterface ) ) ) ;
256+ var errors = pv . ValidateType ( typeof ( NonVirtualPublicImplementsInterface ) ) ;
257+ Assert . That ( errors , Has . Count . EqualTo ( 1 ) ) ;
243258 }
244259
245260 public class InvalidVirtualPrivateAutoProperty : ValidClass
@@ -254,7 +269,8 @@ public virtual int NonVirtualSetterProperty
254269 [ Test ]
255270 public void PrivateSetterOnVirtualPropertyShouldThrows ( )
256271 {
257- Assert . Throws < InvalidProxyTypeException > ( ( ) => Validate ( typeof ( InvalidVirtualPrivateAutoProperty ) ) ) ;
272+ var errors = pv . ValidateType ( typeof ( InvalidVirtualPrivateAutoProperty ) ) ;
273+ Assert . That ( errors , Has . Count . EqualTo ( 1 ) ) ;
258274 }
259275 }
260276}
0 commit comments