7e98ecca3f32da0d632353cdb65d9660fac4e81c
[mono.git] / mcs / class / System.ComponentModel.DataAnnotations / Test / System.ComponentModel.DataAnnotations / ValidationAttributeTest.cs
1 //
2 // ValidationAttributeTest.cs
3 //
4 // Authors:
5 //      Marek Habersack <mhabersack@novell.com>
6 //
7 // Copyright (C) 2010 Novell, Inc. (http://novell.com/)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.Collections.Generic;
30 using System.ComponentModel.DataAnnotations;
31 using System.Text;
32
33 using NUnit.Framework;
34 using MonoTests.Common;
35
36 namespace MonoTests.System.ComponentModel.DataAnnotations
37 {
38         [TestFixture]
39         public class ValidationAttributeTest
40         {
41                 const string TEST_ERROR_MESSAGE = "Test Error Message";
42                 string ErrorMessageAccessor ()
43                 {
44                         return TEST_ERROR_MESSAGE;
45                 }
46
47                 [Test]
48                 public void Constructor ()
49                 {
50                         var attr = new ValidateFooAttribute ();
51
52                         Assert.IsNull (attr.ErrorMessage, "#A1");
53                         Assert.IsNull (attr.ErrorMessageResourceName, "#A2");
54                         Assert.IsNull (attr.ErrorMessageResourceType, "#A3");
55                         Assert.IsNotNull (attr.GetErrorMessageString (), "#A4");
56                 }
57
58                 [Test]
59                 public void Constructor_Func ()
60                 {
61                         var attr = new ValidateFooAttribute (ErrorMessageAccessor);
62
63                         Assert.IsNull (attr.ErrorMessage, "#A1");
64                         Assert.IsNull (attr.ErrorMessageResourceName, "#A2");
65                         Assert.IsNull (attr.ErrorMessageResourceType, "#A3");
66                         Assert.IsNotNull (attr.GetErrorMessageString (), "#A4");
67                         Assert.AreEqual (TEST_ERROR_MESSAGE, attr.GetErrorMessageString (), "#A4");
68                 }
69
70                 [Test]
71                 public void Constructor_String ()
72                 {
73                         var attr = new ValidateFooAttribute ("Another Test Error Message");
74
75                         Assert.IsNull (attr.ErrorMessage, "#A1");
76                         Assert.IsNull (attr.ErrorMessageResourceName, "#A2");
77                         Assert.IsNull (attr.ErrorMessageResourceType, "#A3");
78                         Assert.IsNotNull (attr.GetErrorMessageString (), "#A4");
79                         Assert.IsNotNull (attr.GetErrorMessageString (), "#A4-1");
80                         Assert.AreEqual ("Another Test Error Message", attr.GetErrorMessageString (), "#A4-2");
81                 }
82
83                 [Test]
84                 public void ErrorMessage ()
85                 {
86                         var attr = new ValidateFooAttribute ();
87
88                         Assert.IsNull (attr.ErrorMessage, "#A1");
89
90                         attr.ErrorMessage = "Test";
91                         Assert.AreEqual ("Test", attr.ErrorMessage, "#A2");
92 #if NET_4_0
93                         attr.ErrorMessage = String.Empty;
94                         Assert.AreEqual (String.Empty, attr.ErrorMessage, "#A3");
95
96                         attr.ErrorMessage = null;
97                         Assert.IsNull (attr.ErrorMessage, "#A4");
98 #else
99                         try {
100                                 attr.ErrorMessage = String.Empty;
101                                 Assert.Fail ("#A3");
102                         } catch (InvalidOperationException) {
103                                 // success
104                         }
105
106                         attr = new ValidateFooAttribute ("Test");
107                         try {
108                                 attr.ErrorMessage = null;
109                                 Assert.Fail ("#A4");
110                         } catch (ArgumentException) {
111                                 // success
112                         }
113
114                         attr = new ValidateFooAttribute ("Test");
115                         try {
116                                 attr.ErrorMessage = String.Empty;
117                                 Assert.Fail ("#A4");
118                         } catch (ArgumentException) {
119                                 // success
120                         }
121
122                         attr = new ValidateFooAttribute ();
123                         attr.ErrorMessageResourceName = "ErrorProperty1";
124
125                         try {
126                                 attr.ErrorMessage = "Test Message";
127                                 Assert.Fail ("#E1");
128                         } catch (InvalidOperationException) {
129                                 // success
130                         }
131 #endif
132                         
133                 }
134
135                 [Test]
136                 public void ErrorMessageResourceName ()
137                 {
138                         var attr = new ValidateFooAttribute ();
139
140                         Assert.IsNull (attr.ErrorMessageResourceName, "#A1");
141
142                         attr.ErrorMessageResourceName = "Test";
143                         Assert.IsNotNull (attr.ErrorMessageResourceName, "#A2-1");
144                         Assert.AreEqual ("Test", attr.ErrorMessageResourceName, "#A2-2");
145 #if NET_4_0
146                         attr.ErrorMessageResourceName = String.Empty;
147                         Assert.IsNotNull (attr.ErrorMessageResourceName, "#A3-1");
148                         Assert.AreEqual (String.Empty, attr.ErrorMessageResourceName, "#A3-2");
149
150                         attr.ErrorMessageResourceName = null;
151                         Assert.IsNull (attr.ErrorMessageResourceName, "#A3-1");
152 #else
153                         try {
154                                 attr.ErrorMessageResourceName = String.Empty;
155                                 Assert.Fail ("#A3-1");
156                         } catch (InvalidOperationException) {
157                                 // success
158                         }
159
160                         attr = new ValidateFooAttribute ("Test");
161                         try {
162                                 attr.ErrorMessageResourceName = String.Empty;
163                                 Assert.Fail ("#A3-2");
164                         } catch (ArgumentException) {
165                                 // success
166                         }
167
168                         attr = new ValidateFooAttribute ("Test");
169                         try {
170                                 attr.ErrorMessageResourceName = null;
171                                 Assert.Fail ("#A3-3");
172                         } catch (ArgumentException) {
173                                 // success
174                         }
175
176                         attr = new ValidateFooAttribute ();
177                         attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
178
179                         try {
180                                 attr.ErrorMessageResourceName = "NoSuchProperty";
181                                 Assert.Fail ("#A3-4");
182                         } catch (InvalidOperationException) {
183                                 // success
184                         }
185 #endif
186                 }
187
188                 [Test]
189                 public void ErrorMessageResourceType ()
190                 {
191                         var attr = new ValidateFooAttribute ();
192
193                         Assert.IsNull (attr.ErrorMessageResourceType, "#A1");
194
195                         attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
196                         Assert.IsNotNull (attr.ErrorMessageResourceType, "#A2-1");
197                         Assert.AreEqual (typeof (FooErrorMessageProvider), attr.ErrorMessageResourceType, "#A2-2");
198 #if !NET_4_0
199                         attr = new ValidateFooAttribute ();
200                         attr.ErrorMessageResourceName = "NoSuchProperty";
201                         
202                         try {
203                                 attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
204                                 Assert.Fail ("#A3");
205                         } catch (InvalidOperationException) {
206                                 // success
207                         }
208 #endif
209                 }
210
211                 [Test]
212                 public void ErrorMessageString ()
213                 {
214                         var attr = new ValidateFooAttribute ();
215
216                         Assert.IsNotNull (attr.GetErrorMessageString (), "#A1-1");
217                         Assert.IsTrue (attr.GetErrorMessageString ().Length > 0, "#A1-2");
218
219                         attr = new ValidateFooAttribute ();
220                         attr.ErrorMessageResourceName = "TestResource";
221                         try {
222                                 attr.GetErrorMessageString ();
223                                 Assert.Fail ("#A2-1");
224                         } catch (InvalidOperationException) {
225                                 // success
226                         }
227 #if NET_4_0
228                         attr = new ValidateFooAttribute ();
229                         attr.ErrorMessageResourceName = String.Empty;
230                         try {
231                                 attr.GetErrorMessageString ();
232                                 Assert.Fail ("#A2-1");
233                         } catch (InvalidOperationException) {
234                                 // success
235                         }
236
237                         attr = new ValidateFooAttribute ();
238                         attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
239                         attr.ErrorMessageResourceName = null;
240                         
241                         try {
242                                 attr.GetErrorMessageString ();
243                                 Assert.Fail ("#A3-1");
244                         } catch (InvalidOperationException) {
245                                 // success
246                         }
247
248                         attr = new ValidateFooAttribute ();
249                         attr.ErrorMessageResourceName = String.Empty;
250                         attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
251                         try {
252                                 string s = attr.GetErrorMessageString ();
253                                 Assert.Fail ("#A3-2");
254                         } catch (InvalidOperationException) {
255                                 // success
256                         }
257
258                         attr = new ValidateFooAttribute ();
259                         attr.ErrorMessageResourceName = "NoSuchProperty";
260                         attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
261                         try {
262                                 attr.GetErrorMessageString ();
263                                 Assert.Fail ("#A4");
264                         } catch (InvalidOperationException) {
265                                 // success
266                         }
267
268                         attr = new ValidateFooAttribute ();
269                         attr.ErrorMessageResourceName = "ErrorProperty2";
270                         attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
271                         try {
272                                 attr.GetErrorMessageString ();
273                                 Assert.Fail ("#A5");
274                         } catch (InvalidOperationException) {
275                                 // success
276                         }
277
278                         attr = new ValidateFooAttribute ();
279                         attr.ErrorMessageResourceName = "ErrorProperty3";
280                         attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
281                         try {
282                                 attr.GetErrorMessageString ();
283                                 Assert.Fail ("#A5");
284                         } catch (InvalidOperationException) {
285                                 // success
286                         }
287
288                         attr = new ValidateFooAttribute ();
289                         attr.ErrorMessageResourceName = "ErrorProperty4";
290                         attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
291                         try {
292                                 attr.GetErrorMessageString ();
293                                 Assert.Fail ("#A6");
294                         } catch (InvalidOperationException) {
295                                 // success
296                         }
297
298                         attr = new ValidateFooAttribute ();
299                         attr.ErrorMessageResourceName = "ErrorProperty5";
300                         attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
301                         try {
302                                 attr.GetErrorMessageString ();
303                                 Assert.Fail ("#A7");
304                         } catch (InvalidOperationException) {
305                                 // success
306                         }
307
308                         attr = new ValidateFooAttribute ();
309                         attr.ErrorMessageResourceName = "ErrorField1";
310                         attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
311                         try {
312                                 attr.GetErrorMessageString ();
313                                 Assert.Fail ("#B1");
314                         } catch (InvalidOperationException) {
315                                 // success
316                         }
317
318                         attr = new ValidateFooAttribute ();
319                         attr.ErrorMessageResourceName = "ErrorField2";
320                         attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
321                         try {
322                                 attr.GetErrorMessageString ();
323                                 Assert.Fail ("#B2");
324                         } catch (InvalidOperationException) {
325                                 // success
326                         }
327 #endif
328
329                         attr = new ValidateFooAttribute ();
330                         attr.ErrorMessageResourceName = "ErrorProperty1";
331                         attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
332                         Assert.IsNotNull (attr.GetErrorMessageString (), "#C1-1");
333                         Assert.AreEqual ("Error Message 1", attr.GetErrorMessageString (), "#C1-2");
334
335                         attr = new ValidateFooAttribute (ErrorMessageAccessor);
336                         Assert.IsNotNull (attr.GetErrorMessageString (), "#D1-1");
337                         Assert.AreEqual (TEST_ERROR_MESSAGE, attr.GetErrorMessageString (), "#D1-2");
338
339                         attr = new ValidateFooAttribute ();
340                         attr.ErrorMessageResourceName = "ErrorProperty1";
341                         attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
342                         Assert.IsNotNull (attr.GetErrorMessageString (), "#D1-3");
343                         Assert.AreEqual ("Error Message 1", attr.GetErrorMessageString (), "#D1-4");
344 #if NET_4_0
345                         attr.ErrorMessage = "Test Message";
346                         try {
347                                 attr.GetErrorMessageString ();
348                                 Assert.Fail ("#E1");
349                         } catch (InvalidOperationException) {
350                                 // success
351                         }
352 #endif
353                 }
354
355                 [Test]
356                 public void FormatErrorMessage ()
357                 {
358                         var attr = new ValidateFooAttribute ();
359
360                         Assert.IsNotNull (attr.FormatErrorMessage ("SomeField"), "#A1-1");
361                         Assert.AreEqual ("The field SomeField is invalid.", attr.FormatErrorMessage ("SomeField"), "#A1-2");
362
363                         attr.ErrorMessage = "Test: {0}";
364                         Assert.IsNotNull (attr.FormatErrorMessage ("SomeField"), "#A2-1");
365                         Assert.AreEqual ("Test: SomeField", attr.FormatErrorMessage ("SomeField"), "#A2-2");
366 #if !NET_4_0
367                         attr = new ValidateFooAttribute ();
368 #else
369                         attr.ErrorMessage = null;
370 #endif
371                         attr.ErrorMessageResourceName = "ErrorProperty1";
372                         attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
373                         Assert.IsNotNull (attr.FormatErrorMessage ("SomeField"), "#B1-1");
374                         Assert.AreEqual ("Error Message 1", attr.FormatErrorMessage ("SomeField"), "#B1-2");
375 #if !NET_4_0
376                         attr = new ValidateFooAttribute ();
377 #endif
378                         attr.ErrorMessageResourceName = "ErrorProperty6";
379                         attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
380                         Assert.IsNotNull (attr.FormatErrorMessage ("SomeField"), "#B2-1");
381                         Assert.AreEqual ("Error Message 6: SomeField", attr.FormatErrorMessage ("SomeField"), "#B2-2");
382 #if !NET_4_0
383                         attr = new ValidateFooAttribute ();
384 #endif
385                         attr.ErrorMessageResourceName = "ErrorProperty6";
386                         attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
387                         Assert.IsNotNull (attr.FormatErrorMessage ("SomeField"), "#B3-1");
388                         Assert.AreEqual ("Error Message 6: ", attr.FormatErrorMessage (null), "#B3-2");
389                 }
390 #if NET_4_0
391                 [Test]
392                 public void GetValidationResult ()
393                 {
394                         var attr = new ValidateBarAttribute ();
395
396                         try {
397                                 attr.GetValidationResult ("stuff", null);
398                                 Assert.Fail ("#A1");
399                         } catch (ArgumentNullException) {
400                                 // success
401                         }
402
403                         var vc = new ValidationContext ("stuff", null, null);
404                         vc.DisplayName = "MyStuff";
405                         var vr = attr.GetValidationResult ("stuff", vc);
406                         Assert.IsNull (vr, "#A2");
407
408                         vr = attr.GetValidationResult (null, vc);
409                         Assert.IsNotNull(vr, "#A3-1");
410                         Assert.IsNotNull (vr.ErrorMessage, "#A3-2");
411                         Assert.AreEqual ("The field MyStuff is invalid.", vr.ErrorMessage, "#A3-3");
412
413                         attr.ErrorMessage = "My Error Message: {0}";
414                         vr = attr.GetValidationResult (null, vc);
415                         Assert.IsNotNull (vr, "#A4-1");
416                         Assert.IsNotNull (vr.ErrorMessage, "#A4-2");
417                         Assert.AreEqual ("My Error Message: MyStuff", vr.ErrorMessage, "#A4-3");
418
419                         attr.ErrorMessage = null;
420                         attr.ErrorMessageResourceName = "ErrorProperty1";
421                         attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
422                         vr = attr.GetValidationResult (null, vc);
423                         Assert.IsNotNull (vr, "#A5-1");
424                         Assert.IsNotNull (vr.ErrorMessage, "#A5-2");
425                         Assert.AreEqual ("Error Message 1", vr.ErrorMessage, "#A5-3");
426
427                         attr.ErrorMessage = "My Error Message: {0}";
428                         attr.ErrorMessageResourceName = null;
429                         attr.ErrorMessageResourceType = null;
430                         vr = attr.GetValidationResult (null, vc);
431                         Assert.IsNotNull (vr, "#A6-1");
432                         Assert.IsNotNull (vr.MemberNames, "#A6-2");
433                         int count = 0;
434                         foreach (string s in vr.MemberNames)
435                                 count++;
436                         Assert.AreEqual (0, count, "#A6-3");
437                         Assert.AreEqual ("My Error Message: MyStuff", vr.ErrorMessage, "#A6-4");
438
439                         attr.ValidationResultErrorMessage = "My VR message";
440                         vr = attr.GetValidationResult (null, vc);
441                         Assert.IsNotNull (vr, "#A7-1");
442                         Assert.AreEqual ("My VR message", vr.ErrorMessage, "#A7-2");
443                 }
444
445                 [Test]
446                 public void IsValid_Object ()
447                 {
448                         var attr = new ValidateFooAttribute ();
449
450                         AssertExtensions.Throws <NotImplementedException> (() => {
451                                 // It calls IsValid (object, validationContext) which throws the NIEX, but when that overload is called directly, there's
452                                 // no exception.
453                                 //
454                                 // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value, ValidationContext validationContext)
455                                 // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value)
456                                 // at MonoTests.System.ComponentModel.DataAnnotations.ValidationAttributeTest.IsValid_Object() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 450
457                                 attr.IsValid (null);
458                         }, "#A1-1");
459                         
460                         AssertExtensions.Throws <NotImplementedException> (() => {
461                                 attr.IsValid ("stuff");
462                         }, "#A1-2");
463                 }
464
465                 [Test]
466                 public void IsValid_Object_ValidationContext ()
467                 {
468                         var attr = new ValidateBarAttribute ();
469
470                         AssertExtensions.Throws <NullReferenceException> (() => {
471                                 attr.CallIsValid (null, null);
472                         }, "#A1");
473
474                         var vc = new ValidationContext ("stuff", null, null);
475                         var vr = attr.CallIsValid (null, vc);
476                         Assert.IsNotNull (vr, "#A2-1");
477                         Assert.IsNotNull (vr.ErrorMessage, "#A2-2");
478                         Assert.AreEqual ("The field String is invalid.", vr.ErrorMessage, "#A2-3");
479                         Assert.IsNotNull (vr.MemberNames, "#A2-4"); 
480                         
481                         int count = 0;
482                         foreach (string s in vr.MemberNames)
483                                 count++;
484                         Assert.AreEqual (0, count, "#A2-5");
485
486                         vc.MemberName = "SomeMember";
487                         vr = attr.CallIsValid (null, vc);
488                         Assert.IsNotNull (vr, "#A3-1");
489                         Assert.IsNotNull (vr.ErrorMessage, "#A3-2");
490                         Assert.AreEqual ("The field String is invalid.", vr.ErrorMessage, "#A3-3");
491                         Assert.IsNotNull (vr.MemberNames, "#A3-4");
492
493                         var list = new List <string> ();
494                         foreach (string s in vr.MemberNames)
495                                 list.Add (s);
496                         Assert.AreEqual (1, list.Count, "#A3-5");
497                         Assert.AreEqual ("SomeMember", list [0], "#A3-6");
498                 }
499
500                 [Test]
501                 public void IsValid_Object_ValidationContext_CrossCallsWithNIEX ()
502                 {
503                         var attr = new ValidateSomethingAttribute ();
504
505                         AssertExtensions.Throws<NotImplementedException> (() => {
506                                 // Thrown from the IsValid (object, ValidationContext) overload!
507                                 //
508                                 // MonoTests.System.ComponentModel.DataAnnotations.ValidationAttributeTest.IsValid_Object_ValidationContext_02:
509                                 // System.NotImplementedException : IsValid(object value) has not been implemented by this class.  The preferred entry point is GetValidationResult() and classes should override IsValid(object value, ValidationContext context).
510                                 //
511                                 // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value, ValidationContext validationContext)
512                                 // at MonoTests.System.ComponentModel.DataAnnotations.ValidateSomethingAttribute.IsValid(Object value) in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 639
513                                 // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value, ValidationContext validationContext)
514                                 // at MonoTests.System.ComponentModel.DataAnnotations.ValidateSomethingAttribute.IsValid(Object value) in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 639
515                                 // at MonoTests.System.ComponentModel.DataAnnotations.ValidationAttributeTest.IsValid_Object_ValidationContext_02() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 514
516                                 attr.IsValid ("stuff");
517                         }, "#A1");
518
519                         AssertExtensions.Throws<NotImplementedException> (() => {
520                                 // And this one is thrown from the IsValid (object) overload!
521                                 //
522                                 // MonoTests.System.ComponentModel.DataAnnotations.ValidationAttributeTest.IsValid_Object_ValidationContext_CrossCallsWithNIEX:
523                                 // System.NotImplementedException : IsValid(object value) has not been implemented by this class.  The preferred entry point is GetValidationResult() and classes should override IsValid(object value, ValidationContext context).
524                                 //
525                                 // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value)
526                                 // at MonoTests.System.ComponentModel.DataAnnotations.ValidateSomethingAttribute.IsValid(Object value, ValidationContext validationContext) in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 660
527                                 // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value)
528                                 // at MonoTests.System.ComponentModel.DataAnnotations.ValidateSomethingAttribute.IsValid(Object value, ValidationContext validationContext) in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 660
529                                 // at MonoTests.System.ComponentModel.DataAnnotations.ValidateSomethingAttribute.CallIsValid(Object value, ValidationContext validationContext) in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 667
530                                 // at MonoTests.System.ComponentModel.DataAnnotations.ValidationAttributeTest.IsValid_Object_ValidationContext_CrossCallsWithNIEX() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 530
531
532                                 attr.CallIsValid ("stuff", null);
533                         }, "#A2");
534                 }
535                 [Test]
536                 public void Validate_Object_ValidationContext ()
537                 {
538                         var attr = new ValidateBazAttribute ();
539
540                         try {
541                                 attr.Validate ("stuff", (ValidationContext) null);
542                                 Assert.Fail ("#A1");
543                         } catch (ArgumentNullException) {
544                                 // success
545                         }
546
547                         var vc = new ValidationContext ("stuff", null, null);
548                         try {
549                                 attr.Validate (null, vc);
550                                 Assert.Fail ("#A2-1");
551                         } catch (ValidationException) {
552                                 // success
553                         }
554                         Assert.AreEqual (3, attr.Calls.Count, "#A2-1");
555                         Assert.AreEqual ("ValidationResult IsValid (object value, ValidationContext validationContext)", attr.Calls [0], "#A2-2");
556                         Assert.AreEqual ("bool IsValid (object value)", attr.Calls [1], "#A2-3");
557                         Assert.AreEqual ("string FormatErrorMessage (string name)", attr.Calls [2], "#A2-4");
558                 }
559 #endif
560                 [Test]
561                 public void Validate_Object_String ()
562                 {
563                         var attr = new ValidateBazAttribute ();
564
565                         try {
566                                 attr.Validate (null, (string) null);
567                                 Assert.Fail ("#A2");
568                         } catch (ValidationException) {
569                                 // success
570                         }
571
572                         Assert.AreEqual (2, attr.Calls.Count, "#A2-1");
573                         Assert.AreEqual ("bool IsValid (object value)", attr.Calls [0], "#A2-2");
574                         Assert.AreEqual ("string FormatErrorMessage (string name)", attr.Calls [1], "#A2-3");
575                 }
576         }
577
578         class ValidateFooAttribute : ValidationAttribute
579         {
580                 public ValidateFooAttribute ()
581                         : base ()
582                 { }
583
584                 public ValidateFooAttribute (Func<string> errorMessageAccessor)
585                         : base (errorMessageAccessor)
586                 { }
587
588                 public ValidateFooAttribute (string errorMessage)
589                         : base (errorMessage)
590                 { }
591
592                 public string GetErrorMessageString ()
593                 {
594                         return ErrorMessageString;
595                 }
596 #if !NET_4_0
597                 public override bool IsValid (object value)
598                 {
599                         return value != null;
600                 }
601 #endif
602         }
603
604         class ValidateBarAttribute : ValidateFooAttribute
605         {
606                 public string ValidationResultErrorMessage
607                 {
608                         get;
609                         set;
610                 }
611
612                 public override bool IsValid (object value)
613                 {
614                         return value != null;
615                 }
616 #if NET_4_0
617                 protected override ValidationResult IsValid (object value, ValidationContext validationContext)
618                 {
619                         if (!IsValid (value))
620                                 return new ValidationResult (ValidationResultErrorMessage);
621                         return null;
622                 }
623
624                 public ValidationResult CallIsValid (object value, ValidationContext validationContext)
625                 {
626                         return base.IsValid (value, validationContext);
627                 }
628 #endif
629         }
630
631         class ValidateBazAttribute : ValidateBarAttribute
632         {
633                 public readonly List<string> Calls = new List<string> ();
634
635                 public override bool IsValid (object value)
636                 {
637                         Calls.Add ("bool IsValid (object value)");
638                         return base.IsValid (value);
639                 }
640 #if NET_4_0
641                 protected override ValidationResult IsValid (object value, ValidationContext validationContext)
642                 {
643                         Calls.Add ("ValidationResult IsValid (object value, ValidationContext validationContext)");
644                         return base.IsValid (value, validationContext);
645                 }
646 #endif
647                 public override string FormatErrorMessage (string name)
648                 {
649                         Calls.Add ("string FormatErrorMessage (string name)");
650                         return base.FormatErrorMessage (name);
651                 }
652         }
653 #if NET_4_0
654         class ValidateSomethingAttribute : ValidationAttribute
655         {
656                 public override bool IsValid (object value)
657                 {
658                         return base.IsValid (value, null) == ValidationResult.Success;
659                 }
660
661                 protected override ValidationResult IsValid (object value, ValidationContext validationContext)
662                 {
663                         if (base.IsValid (value))
664                                 return ValidationResult.Success;
665                         return new ValidationResult ("failed to validate in base class");
666                 }
667
668                 public ValidationResult CallIsValid (object value, ValidationContext validationContext)
669                 {
670                         return IsValid (value, validationContext);
671                 }
672         }
673 #endif
674         class FooErrorMessageProvider
675         {
676                 public static string ErrorProperty1
677                 {
678                         get { return "Error Message 1"; }
679                 }
680
681                 public static int ErrorProperty2
682                 {
683                         get { return 1; }
684                 }
685
686                 public string ErrorProperty3
687                 {
688                         get { return "Error Message 2"; }
689                 }
690
691                 protected static string ErrorProperty4
692                 {
693                         get { return "Error Message 3"; }
694                 }
695
696                 public static string ErrorProperty5
697                 {
698                         set { }
699                 }
700
701                 public static string ErrorProperty6
702                 {
703                         get { return "Error Message 6: {0}"; }
704                 }
705
706                 public string ErrorField1 = "Error Message 4";
707                 public static string ErrorField2 = "Error Message 5";
708         }
709 }