Add NoData as valid return state for Statements with params
[mono.git] / mcs / class / System.ComponentModel.DataAnnotations / Test / System.ComponentModel.DataAnnotations / CustomValidationAttributeTest.cs
1 //
2 // Authors:
3 //      Marek Habersack <grendel@twistedcode.net>
4 //
5 // Copyright (C) 2011 Novell, Inc. (http://novell.com/)
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
14 // 
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 // 
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 //
26 using System;
27 using System.Collections.Generic;
28 using System.ComponentModel.DataAnnotations;
29 using System.ComponentModel.Design;
30 using System.Text;
31
32 using NUnit.Framework;
33 using MonoTests.Common;
34
35 namespace MonoTests.System.ComponentModel.DataAnnotations
36 {
37 #if NET_4_0
38         [TestFixture]
39         public class CustomValidationAttributeTest
40         {
41                 [Test]
42                 public void Constructor ()
43                 {
44                         var attr = new CustomValidationAttribute (null, "MyMethod");
45                         Assert.IsNull (attr.ValidatorType, "#A1-1");
46                         Assert.AreEqual ("MyMethod", attr.Method, "#A1-2");
47
48                         attr = new CustomValidationAttribute (typeof (string), null);
49                         Assert.AreEqual (typeof (string), attr.ValidatorType, "#A2-1");
50                         Assert.IsNull (attr.Method, "#A2-2");
51
52                         attr = new CustomValidationAttribute (null, null);
53                         Assert.IsNull (attr.ValidatorType, "#A3-1");
54                         Assert.IsNull (attr.Method, "#A3-2");
55
56                         attr = new CustomValidationAttribute (typeof (string), "NoSuchMethod");
57                         Assert.AreEqual (typeof (string), attr.ValidatorType, "#A5-1");
58                         Assert.AreEqual ("NoSuchMethod", attr.Method, "#A5-2");
59                 }
60
61                 [Test]
62                 public void TypeId ()
63                 {
64                         var attr = new CustomValidationAttribute (null, "MyMethod");
65                         Assert.IsNotNull (attr.TypeId, "#A1-1");
66                         Assert.AreEqual (typeof (Tuple<string, Type>), attr.TypeId.GetType (), "#A1-2");
67
68                         var typeid = attr.TypeId as Tuple <string, Type>;
69                         Assert.IsNotNull (typeid.Item1, "#A2-1");
70                         Assert.AreEqual ("MyMethod", typeid.Item1, "#A2-2");
71                         Assert.IsNull (typeid.Item2, "#A2-3");
72
73                         attr = new CustomValidationAttribute (typeof (CustomValidationAttributeTest), "MyMethod");
74                         typeid = attr.TypeId as Tuple<string, Type>;
75                         Assert.IsNotNull (typeid.Item1, "#A3-1");
76                         Assert.AreEqual ("MyMethod", typeid.Item1, "#A3-2");
77                         Assert.IsNotNull (typeid.Item2, "#A3-3");
78                         Assert.AreEqual (typeof (CustomValidationAttributeTest), typeid.Item2, "#A3-4");
79
80                         var typeid2 = attr.TypeId as Tuple<string, Type>;
81                         Assert.IsTrue (Object.ReferenceEquals (typeid, typeid2), "#A4");
82                 }
83
84                 [Test]
85                 public void FormatErrorMessage ()
86                 {
87                         var attr = new CustomValidationAttribute (null, null);
88                         string msg = null;
89
90                         AssertExtensions.Throws<InvalidOperationException> (() => {
91                                 // MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
92                                 // System.InvalidOperationException : The CustomValidationAttribute.ValidatorType was not specified.
93                                 //
94                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
95                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
96                                 // at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 88
97
98                                 msg = attr.FormatErrorMessage (null);
99                         }, "#A1");
100
101                         attr = new CustomValidationAttribute (typeof (string), null);
102                         AssertExtensions.Throws<InvalidOperationException> (() => {
103                                 // MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
104                                 // System.InvalidOperationException : The CustomValidationAttribute.Method was not specified.
105                                 //
106                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
107                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
108                                 // at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 102
109
110                                 msg = attr.FormatErrorMessage (null);
111                         }, "#A2");
112
113                         attr = new CustomValidationAttribute (typeof (string), String.Empty);
114                         AssertExtensions.Throws<InvalidOperationException> (() => {
115                                 // MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
116                                 // System.InvalidOperationException : The CustomValidationAttribute.Method was not specified.
117                                 //
118                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
119                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
120                                 // at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 117
121
122                                 msg = attr.FormatErrorMessage (null);
123                         }, "#A3");
124
125                         attr = new CustomValidationAttribute (typeof (string), "NoSuchMethod");
126                         AssertExtensions.Throws<InvalidOperationException> (() => {
127                                 // MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
128                                 // System.InvalidOperationException : The CustomValidationAttribute method 'NoSuchMethod' does not exist in type 'String' or is not public and static.
129                                 //
130                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
131                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
132                                 // at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 126
133
134                                 msg = attr.FormatErrorMessage (null);
135                         }, "#A4");
136
137                         attr = new CustomValidationAttribute (typeof (PrivateValidatorMethodContainer), "MethodOne");
138                         AssertExtensions.Throws<InvalidOperationException> (() => {
139                                 // MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
140                                 // System.InvalidOperationException : The custom validation type 'PrivateValidatorMethodContainer' must be public.
141                                 //
142                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
143                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
144                                 // at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 138
145
146                                 msg = attr.FormatErrorMessage (null);
147                         }, "#A5");
148
149                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodOne");
150                         AssertExtensions.Throws<InvalidOperationException> (() => {
151                                 // MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
152                                 // System.InvalidOperationException : The CustomValidationAttribute method 'MethodOne' in type 'PublicValidatorMethodContainer' 
153                                 //        must return System.ComponentModel.DataAnnotations.ValidationResult.  Use System.ComponentModel.DataAnnotations.ValidationResult.Success 
154                                 //        to represent success.
155                                 //
156                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
157                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
158                                 // at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 150
159                                 msg = attr.FormatErrorMessage (null);
160                         }, "#A6");
161
162                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodTwo");
163                         AssertExtensions.Throws<InvalidOperationException> (() => {
164                                 // MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
165                                 // System.InvalidOperationException : The CustomValidationAttribute method 'MethodTwo' in type 'PublicValidatorMethodContainer' must match the expected signature: public static ValidationResult MethodTwo(object value, ValidationContext context).  The value can be strongly typed.  The ValidationContext parameter is optional.
166                                 //
167                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
168                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
169                                 // at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 163
170                                 msg = attr.FormatErrorMessage (null);
171                         }, "#A7");
172
173                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodThree");
174                         msg = attr.FormatErrorMessage (null);
175                         Assert.IsNotNull (msg, "#A8-1");
176                         Assert.IsTrue (msg.Length > 0, "#A8-2");
177                         Assert.AreEqual (" is not valid.", msg, "#A8-3");
178                         
179                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodFour");
180                         msg = attr.FormatErrorMessage ("test");
181                         Assert.IsNotNull (msg, "#A9-1");
182                         Assert.IsTrue (msg.Length > 0, "#A9-2");
183                         Assert.AreEqual ("test is not valid.", msg, "#A9-3");
184                         
185                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodFive");
186                         AssertExtensions.Throws<InvalidOperationException> (() => {
187                                 // MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
188                                 // System.InvalidOperationException : The CustomValidationAttribute method 'MethodFive' in type 'PublicValidatorMethodContainer' must match the expected signature: public static ValidationResult MethodFive(object value, ValidationContext context).  The value can be strongly typed.  The ValidationContext parameter is optional.
189                                 //
190                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
191                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
192                                 // at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 180
193                                 msg = attr.FormatErrorMessage (null);
194                         }, "#A10");
195
196                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodSix");
197                         AssertExtensions.Throws<InvalidOperationException> (() => {
198                                 // MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
199                                 // System.InvalidOperationException : The CustomValidationAttribute method 'MethodSix' in type 'PublicValidatorMethodContainer' must match the expected signature: public static ValidationResult MethodSix(object value, ValidationContext context).  The value can be strongly typed.  The ValidationContext parameter is optional.
200                                 //
201                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
202                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
203                                 // at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 191
204                                 msg = attr.FormatErrorMessage (null);
205                         }, "#A11");
206                 }
207
208                 [Test]
209                 public void IsValid ()
210                 {
211                         var attr = new CustomValidationAttribute (null, null);
212
213                         AssertExtensions.Throws<InvalidOperationException> (() => {
214                                 attr.IsValid ("test");
215                         }, "#A1");
216
217                         attr = new CustomValidationAttribute (typeof (string), null);
218                         AssertExtensions.Throws<InvalidOperationException> (() => {
219                                 attr.IsValid ("test");
220                         }, "#A2");
221
222                         attr = new CustomValidationAttribute (typeof (string), String.Empty);
223                         AssertExtensions.Throws<InvalidOperationException> (() => {
224                                 attr.IsValid ("test");
225                         }, "#A3");
226
227                         attr = new CustomValidationAttribute (typeof (string), "NoSuchMethod");
228                         AssertExtensions.Throws<InvalidOperationException> (() => {
229                                 attr.IsValid ("test");
230                         }, "#A4");
231
232                         attr = new CustomValidationAttribute (typeof (PrivateValidatorMethodContainer), "MethodOne");
233                         AssertExtensions.Throws<InvalidOperationException> (() => {
234                                 attr.IsValid ("test");
235                         }, "#A5");
236
237                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodOne");
238                         AssertExtensions.Throws<InvalidOperationException> (() => {
239                                 attr.IsValid ("test");
240                         }, "#A6");
241
242                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodTwo");
243                         AssertExtensions.Throws<InvalidOperationException> (() => {
244                                 attr.IsValid ("test");
245                         }, "#A7");
246
247                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodThree");
248                         bool valid = attr.IsValid ("test");
249                         Assert.IsTrue (valid, "#A8-1");
250                         valid = attr.IsValid (null);
251                         Assert.IsFalse (valid, "#A8-2");
252                         valid = attr.IsValid ("failTest");
253                         Assert.IsFalse (valid, "#A8-3");
254
255                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodFour");
256                         valid = attr.IsValid ("test");
257                         Assert.IsTrue (valid, "#A9-1");
258                         valid = attr.IsValid (null);
259                         Assert.IsFalse (valid, "#A9-2");
260                         valid = attr.IsValid ("failTest");
261                         Assert.IsFalse (valid, "#A9-3");
262
263                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodFive");
264                         AssertExtensions.Throws<InvalidOperationException> (() => {
265                                 attr.IsValid ("test");
266                         }, "#A10");
267
268                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodSix");
269                         AssertExtensions.Throws<InvalidOperationException> (() => {
270                                 attr.IsValid ("test");
271                         }, "#A11");
272
273                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodSeven");
274                         AssertExtensions.Throws<ApplicationException> (() => {
275                                 attr.IsValid ("test");
276                         }, "#A12");
277                 }
278
279                 class PrivateValidatorMethodContainer
280                 {
281                         public static void MethodOne ()
282                         { }
283                 }
284
285                 public class PublicValidatorMethodContainer
286                 {
287                         public static void MethodOne ()
288                         { }
289
290                         public static ValidationResult MethodTwo ()
291                         {
292                                 return ValidationResult.Success;
293                         }
294
295                         public static ValidationResult MethodThree (object o)
296                         {
297                                 if (o == null)
298                                         return new ValidationResult ("Object cannot be null");
299                                 string s = o as string;
300                                 if (s == null || s != "failTest")
301                                         return ValidationResult.Success;
302                                 return new ValidationResult ("Test failed as requested");
303                         }
304
305                         public static ValidationResult MethodFour (object o, ValidationContext ctx)
306                         {
307                                 if (o == null)
308                                         return new ValidationResult ("Object cannot be null");
309                                 string s = o as string;
310                                 if (s == null || s != "failTest")
311                                         return ValidationResult.Success;
312                                 return new ValidationResult ("Test failed as requested");
313                         }
314
315                         public static ValidationResult MethodFive (object o, string s)
316                         {
317                                 return ValidationResult.Success;
318                         }
319
320                         public static ValidationResult MethodSix (object o, ValidationContext ctx, string s)
321                         {
322                                 return ValidationResult.Success;
323                         }
324
325                         public static ValidationResult MethodSeven (object o, ValidationContext ctx)
326                         {
327                                 throw new ApplicationException ("SNAFU");
328                         }
329                 }
330         }
331 #endif
332 }