22// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. 
33
44using  System . Collections . Generic ; 
5+ using  System . Collections . Specialized ; 
56using  System . ComponentModel . DataAnnotations ; 
67using  System . Linq ; 
78using  System . Web . Http . Metadata ; 
89using  System . Web . Http . Metadata . Providers ; 
10+ using  System . Web . WebPages . TestUtils ; 
911using  Microsoft . TestCommon ; 
1012using  Moq ; 
1113using  Moq . Protected ; 
@@ -47,6 +49,90 @@ public void ValuesSet()
4749            Assert . Same ( attribute ,  validator . Attribute ) ; 
4850        } 
4951
52+         public  static   TheoryDataSet < NameValueCollection >  FalseAppSettingsData 
53+         { 
54+             get 
55+             { 
56+                 return  new  TheoryDataSet < NameValueCollection > 
57+                 { 
58+                     new  NameValueCollection ( ) , 
59+                     new  NameValueCollection 
60+                     { 
61+                         {  DataAnnotationsModelValidator . UseLegacyValidationMemberNameKey ,  "false"  } , 
62+                     } , 
63+                     new  NameValueCollection 
64+                     { 
65+                         {  DataAnnotationsModelValidator . UseLegacyValidationMemberNameKey ,  "False"  } , 
66+                     } , 
67+                     new  NameValueCollection 
68+                     { 
69+                         {  DataAnnotationsModelValidator . UseLegacyValidationMemberNameKey ,  "false"  } , 
70+                         {  DataAnnotationsModelValidator . UseLegacyValidationMemberNameKey ,  "true"  } , 
71+                     } , 
72+                     new  NameValueCollection 
73+                     { 
74+                         {  DataAnnotationsModelValidator . UseLegacyValidationMemberNameKey ,  "garbage"  } , 
75+                     } , 
76+                 } ; 
77+             } 
78+         } 
79+ 
80+         [ Theory ] 
81+         [ PropertyData ( "FalseAppSettingsData" ) ] 
82+         public  void  GetUseLegacyValidationMemberName_ReturnsFalse ( NameValueCollection  appSettings ) 
83+         { 
84+             // Arrange & Act 
85+             var  result  =  DataAnnotationsModelValidator . GetUseLegacyValidationMemberName ( appSettings ) ; 
86+ 
87+             // Assert 
88+             Assert . False ( result ) ; 
89+         } 
90+ 
91+         public  static   TheoryDataSet < NameValueCollection >  TrueAppSettingsData 
92+         { 
93+             get 
94+             { 
95+                 return  new  TheoryDataSet < NameValueCollection > 
96+                 { 
97+                     new  NameValueCollection 
98+                     { 
99+                         {  DataAnnotationsModelValidator . UseLegacyValidationMemberNameKey ,  "true"  } , 
100+                     } , 
101+                     new  NameValueCollection 
102+                     { 
103+                         {  DataAnnotationsModelValidator . UseLegacyValidationMemberNameKey ,  "True"  } , 
104+                     } , 
105+                     new  NameValueCollection 
106+                     { 
107+                         {  DataAnnotationsModelValidator . UseLegacyValidationMemberNameKey ,  "true"  } , 
108+                         {  DataAnnotationsModelValidator . UseLegacyValidationMemberNameKey ,  "false"  } , 
109+                     } , 
110+                     new  NameValueCollection 
111+                     { 
112+                         {  DataAnnotationsModelValidator . UseLegacyValidationMemberNameKey ,  "true"  } , 
113+                         {  DataAnnotationsModelValidator . UseLegacyValidationMemberNameKey ,  "false"  } , 
114+                         {  DataAnnotationsModelValidator . UseLegacyValidationMemberNameKey ,  "garbage"  } , 
115+                     } , 
116+                     new  NameValueCollection 
117+                     { 
118+                         {  DataAnnotationsModelValidator . UseLegacyValidationMemberNameKey ,  "True"  } , 
119+                         {  DataAnnotationsModelValidator . UseLegacyValidationMemberNameKey ,  "garbage"  } , 
120+                     } , 
121+                 } ; 
122+             } 
123+         } 
124+ 
125+         [ Theory ] 
126+         [ PropertyData ( "TrueAppSettingsData" ) ] 
127+         public  void  GetUseLegacyValidationMemberName_ReturnsTrue ( NameValueCollection  appSettings ) 
128+         { 
129+             // Arrange & Act 
130+             var  result  =  DataAnnotationsModelValidator . GetUseLegacyValidationMemberName ( appSettings ) ; 
131+ 
132+             // Assert 
133+             Assert . True ( result ) ; 
134+         } 
135+ 
50136        public  static   TheoryDataSet < ModelMetadata ,  string >  ValidateSetsMemberNamePropertyDataSet 
51137        { 
52138            get 
@@ -57,10 +143,14 @@ public static TheoryDataSet<ModelMetadata, string> ValidateSetsMemberNamePropert
57143                        _metadataProvider . GetMetadataForProperty ( ( )  =>  15 ,  typeof ( string ) ,  "Length" ) , 
58144                        "Length" 
59145                    } , 
146+                     { 
147+                         _metadataProvider . GetMetadataForProperty ( ( )  =>  string . Empty ,  typeof ( AnnotatedModel ) ,  "Name" ) , 
148+                         "Name" 
149+                     } , 
60150                    { 
61151                        _metadataProvider . GetMetadataForType ( ( )  =>  new  object ( ) ,  typeof ( SampleModel ) ) , 
62152                        "SampleModel" 
63-                     } 
153+                     } , 
64154                } ; 
65155            } 
66156        } 
@@ -89,6 +179,123 @@ public void ValidateSetsMemberNamePropertyOfValidationContextForProperties(Model
89179            attribute . VerifyAll ( ) ; 
90180        } 
91181
182+         [ Fact ] 
183+         public  void  ValidateSetsMemberNameProperty_UsingDisplayName ( ) 
184+         { 
185+             AppDomainUtils . RunInSeparateAppDomain ( ValidateSetsMemberNameProperty_UsingDisplayName_Inner ) ; 
186+         } 
187+ 
188+         private  static   void  ValidateSetsMemberNameProperty_UsingDisplayName_Inner ( ) 
189+         { 
190+             // Arrange 
191+             DataAnnotationsModelValidator . UseLegacyValidationMemberName  =  true ; 
192+             var  expectedMemberName  =  "Annotated Name" ; 
193+             var  attribute  =  new  Mock < ValidationAttribute >  {  CallBase  =  true  } ; 
194+             attribute 
195+                 . Protected ( ) 
196+                 . Setup < ValidationResult > ( "IsValid" ,  ItExpr . IsAny < object > ( ) ,  ItExpr . IsAny < ValidationContext > ( ) ) 
197+                 . Callback ( ( object  o ,  ValidationContext  context )  => 
198+                 { 
199+                     Assert . Equal ( expectedMemberName ,  context . MemberName ) ; 
200+                 } ) 
201+                 . Returns ( ValidationResult . Success ) 
202+                 . Verifiable ( ) ; 
203+             var  validator  =  new  DataAnnotationsModelValidator ( _noValidatorProviders ,  attribute . Object ) ; 
204+             var  metadata  =  _metadataProvider . GetMetadataForProperty ( ( )  =>  string . Empty ,  typeof ( AnnotatedModel ) ,  "Name" ) ; 
205+ 
206+             // Act 
207+             var  results  =  validator . Validate ( metadata ,  container :  null ) ; 
208+ 
209+             // Assert 
210+             Assert . Empty ( results ) ; 
211+             attribute . VerifyAll ( ) ; 
212+         } 
213+ 
214+         // Confirm explicit false setting does not change Validate(...)'s behavior from its default. 
215+         [ Fact ] 
216+         public  void  ValidateSetsMemberNameProperty_NotUsingDisplayName ( ) 
217+         { 
218+             AppDomainUtils . RunInSeparateAppDomain ( ValidateSetsMemberNameProperty_NotUsingDisplayName_Inner ) ; 
219+         } 
220+ 
221+         private  static   void  ValidateSetsMemberNameProperty_NotUsingDisplayName_Inner ( ) 
222+         { 
223+             // Arrange 
224+             DataAnnotationsModelValidator . UseLegacyValidationMemberName  =  false ; 
225+             var  expectedMemberName  =  "Name" ; 
226+             var  attribute  =  new  Mock < ValidationAttribute >  {  CallBase  =  true  } ; 
227+             attribute 
228+                 . Protected ( ) 
229+                 . Setup < ValidationResult > ( "IsValid" ,  ItExpr . IsAny < object > ( ) ,  ItExpr . IsAny < ValidationContext > ( ) ) 
230+                 . Callback ( ( object  o ,  ValidationContext  context )  => 
231+                 { 
232+                     Assert . Equal ( expectedMemberName ,  context . MemberName ) ; 
233+                 } ) 
234+                 . Returns ( ValidationResult . Success ) 
235+                 . Verifiable ( ) ; 
236+             var  validator  =  new  DataAnnotationsModelValidator ( _noValidatorProviders ,  attribute . Object ) ; 
237+             var  metadata  =  _metadataProvider . GetMetadataForProperty ( ( )  =>  string . Empty ,  typeof ( AnnotatedModel ) ,  "Name" ) ; 
238+ 
239+             // Act 
240+             var  results  =  validator . Validate ( metadata ,  container :  null ) ; 
241+ 
242+             // Assert 
243+             Assert . Empty ( results ) ; 
244+             attribute . VerifyAll ( ) ; 
245+         } 
246+ 
247+         public  static   TheoryDataSet < ModelMetadata ,  string >  ValidateSetsDisplayNamePropertyDataSet 
248+         { 
249+             get 
250+             { 
251+                 return  new  TheoryDataSet < ModelMetadata ,  string > 
252+                 { 
253+                     { 
254+                         _metadataProvider . GetMetadataForProperty ( ( )  =>  15 ,  typeof ( string ) ,  "Length" ) , 
255+                         "Length" 
256+                     } , 
257+                     { 
258+                         _metadataProvider . GetMetadataForProperty ( ( )  =>  string . Empty ,  typeof ( AnnotatedModel ) ,  "Name" ) , 
259+                         "Annotated Name" 
260+                     } , 
261+                     { 
262+                         _metadataProvider . GetMetadataForType ( ( )  =>  new  object ( ) ,  typeof ( SampleModel ) ) , 
263+                         "SampleModel" 
264+                     } , 
265+                 } ; 
266+             } 
267+         } 
268+ 
269+         [ Theory ] 
270+         [ PropertyData ( "ValidateSetsDisplayNamePropertyDataSet" ) ] 
271+         public  void  ValidateSetsDisplayNamePropertyOfValidationContextAsExpected ( 
272+             ModelMetadata  metadata , 
273+             string  expectedDisplayName ) 
274+         { 
275+             // Arrange 
276+             var  attribute  =  new  Mock < ValidationAttribute > 
277+             { 
278+                 CallBase  =  true , 
279+             } ; 
280+             attribute 
281+                 . Protected ( ) 
282+                 . Setup < ValidationResult > ( "IsValid" ,  ItExpr . IsAny < object > ( ) ,  ItExpr . IsAny < ValidationContext > ( ) ) 
283+                 . Callback ( 
284+                     ( object  o ,  ValidationContext  context )  =>  Assert . Equal ( expectedDisplayName ,  context . DisplayName ) ) 
285+                 . Returns ( ValidationResult . Success ) 
286+                 . Verifiable ( ) ; 
287+             DataAnnotationsModelValidator  validator  =  new  DataAnnotationsModelValidator ( 
288+                 _noValidatorProviders , 
289+                 attribute . Object ) ; 
290+ 
291+             // Act 
292+             IEnumerable < ModelValidationResult >  results  =  validator . Validate ( metadata ,  container :  null ) ; 
293+ 
294+             // Assert 
295+             Assert . Empty ( results ) ; 
296+             attribute . VerifyAll ( ) ; 
297+         } 
298+ 
92299        [ Fact ] 
93300        public  void  ValidateWithIsValidTrue ( ) 
94301        { 
@@ -222,5 +429,11 @@ class SampleModel
222429        { 
223430            public  string  Name  {  get ;  set ;  } 
224431        } 
432+ 
433+         private  class  AnnotatedModel 
434+         { 
435+             [ Display ( Name  =  "Annotated Name" ) ] 
436+             public  string  Name  {  get ;  set ;  } 
437+         } 
225438    } 
226439} 
0 commit comments