[mcs] Remove NET_4_5 ifdef from the source files
[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         [TestFixture]
38         public class CustomValidationAttributeTest
39         {
40                 [Test]
41                 public void Constructor ()
42                 {
43                         var attr = new CustomValidationAttribute (null, "MyMethod");
44                         Assert.IsNull (attr.ValidatorType, "#A1-1");
45                         Assert.AreEqual ("MyMethod", attr.Method, "#A1-2");
46
47                         attr = new CustomValidationAttribute (typeof (string), null);
48                         Assert.AreEqual (typeof (string), attr.ValidatorType, "#A2-1");
49                         Assert.IsNull (attr.Method, "#A2-2");
50
51                         attr = new CustomValidationAttribute (null, null);
52                         Assert.IsNull (attr.ValidatorType, "#A3-1");
53                         Assert.IsNull (attr.Method, "#A3-2");
54
55                         attr = new CustomValidationAttribute (typeof (string), "NoSuchMethod");
56                         Assert.AreEqual (typeof (string), attr.ValidatorType, "#A5-1");
57                         Assert.AreEqual ("NoSuchMethod", attr.Method, "#A5-2");
58                 }
59
60                 [Test]
61                 public void TypeId ()
62                 {
63                         var attr = new CustomValidationAttribute (null, "MyMethod");
64                         Assert.IsNotNull (attr.TypeId, "#A1-1");
65                         Assert.AreEqual (typeof (Tuple<string, Type>), attr.TypeId.GetType (), "#A1-2");
66
67                         var typeid = attr.TypeId as Tuple <string, Type>;
68                         Assert.IsNotNull (typeid.Item1, "#A2-1");
69                         Assert.AreEqual ("MyMethod", typeid.Item1, "#A2-2");
70                         Assert.IsNull (typeid.Item2, "#A2-3");
71
72                         attr = new CustomValidationAttribute (typeof (CustomValidationAttributeTest), "MyMethod");
73                         typeid = attr.TypeId as Tuple<string, Type>;
74                         Assert.IsNotNull (typeid.Item1, "#A3-1");
75                         Assert.AreEqual ("MyMethod", typeid.Item1, "#A3-2");
76                         Assert.IsNotNull (typeid.Item2, "#A3-3");
77                         Assert.AreEqual (typeof (CustomValidationAttributeTest), typeid.Item2, "#A3-4");
78
79                         var typeid2 = attr.TypeId as Tuple<string, Type>;
80                         Assert.IsTrue (Object.ReferenceEquals (typeid, typeid2), "#A4");
81                 }
82
83                 [Test]
84                 public void FormatErrorMessage ()
85                 {
86                         var attr = new CustomValidationAttribute (null, null);
87                         string msg = null;
88
89                         AssertExtensions.Throws<InvalidOperationException> (() => {
90                                 // MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
91                                 // System.InvalidOperationException : The CustomValidationAttribute.ValidatorType was not specified.
92                                 //
93                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
94                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
95                                 // 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
96
97                                 msg = attr.FormatErrorMessage (null);
98                         }, "#A1");
99
100                         attr = new CustomValidationAttribute (typeof (string), null);
101                         AssertExtensions.Throws<InvalidOperationException> (() => {
102                                 // MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
103                                 // System.InvalidOperationException : The CustomValidationAttribute.Method was not specified.
104                                 //
105                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
106                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
107                                 // 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
108
109                                 msg = attr.FormatErrorMessage (null);
110                         }, "#A2");
111
112                         attr = new CustomValidationAttribute (typeof (string), String.Empty);
113                         AssertExtensions.Throws<InvalidOperationException> (() => {
114                                 // MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
115                                 // System.InvalidOperationException : The CustomValidationAttribute.Method was not specified.
116                                 //
117                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
118                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
119                                 // 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
120
121                                 msg = attr.FormatErrorMessage (null);
122                         }, "#A3");
123
124                         attr = new CustomValidationAttribute (typeof (string), "NoSuchMethod");
125                         AssertExtensions.Throws<InvalidOperationException> (() => {
126                                 // MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
127                                 // System.InvalidOperationException : The CustomValidationAttribute method 'NoSuchMethod' does not exist in type 'String' or is not public and static.
128                                 //
129                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
130                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
131                                 // 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
132
133                                 msg = attr.FormatErrorMessage (null);
134                         }, "#A4");
135
136                         attr = new CustomValidationAttribute (typeof (PrivateValidatorMethodContainer), "MethodOne");
137                         AssertExtensions.Throws<InvalidOperationException> (() => {
138                                 // MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
139                                 // System.InvalidOperationException : The custom validation type 'PrivateValidatorMethodContainer' must be public.
140                                 //
141                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
142                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
143                                 // 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
144
145                                 msg = attr.FormatErrorMessage (null);
146                         }, "#A5");
147
148                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodOne");
149                         AssertExtensions.Throws<InvalidOperationException> (() => {
150                                 // MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
151                                 // System.InvalidOperationException : The CustomValidationAttribute method 'MethodOne' in type 'PublicValidatorMethodContainer' 
152                                 //        must return System.ComponentModel.DataAnnotations.ValidationResult.  Use System.ComponentModel.DataAnnotations.ValidationResult.Success 
153                                 //        to represent success.
154                                 //
155                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
156                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
157                                 // 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
158                                 msg = attr.FormatErrorMessage (null);
159                         }, "#A6");
160
161                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodTwo");
162                         AssertExtensions.Throws<InvalidOperationException> (() => {
163                                 // MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
164                                 // 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.
165                                 //
166                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
167                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
168                                 // 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
169                                 msg = attr.FormatErrorMessage (null);
170                         }, "#A7");
171
172                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodThree");
173                         msg = attr.FormatErrorMessage (null);
174                         Assert.IsNotNull (msg, "#A8-1");
175                         Assert.IsTrue (msg.Length > 0, "#A8-2");
176                         Assert.AreEqual (" is not valid.", msg, "#A8-3");
177                         
178                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodFour");
179                         msg = attr.FormatErrorMessage ("test");
180                         Assert.IsNotNull (msg, "#A9-1");
181                         Assert.IsTrue (msg.Length > 0, "#A9-2");
182                         Assert.AreEqual ("test is not valid.", msg, "#A9-3");
183                         
184                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodFive");
185                         AssertExtensions.Throws<InvalidOperationException> (() => {
186                                 // MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
187                                 // 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.
188                                 //
189                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
190                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
191                                 // 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
192                                 msg = attr.FormatErrorMessage (null);
193                         }, "#A10");
194
195                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodSix");
196                         AssertExtensions.Throws<InvalidOperationException> (() => {
197                                 // MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
198                                 // 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.
199                                 //
200                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
201                                 // at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
202                                 // 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
203                                 msg = attr.FormatErrorMessage (null);
204                         }, "#A11");
205                 }
206
207                 [Test]
208                 public void IsValid ()
209                 {
210                         var attr = new CustomValidationAttribute (null, null);
211
212                         AssertExtensions.Throws<InvalidOperationException> (() => {
213                                 attr.IsValid ("test");
214                         }, "#A1");
215
216                         attr = new CustomValidationAttribute (typeof (string), null);
217                         AssertExtensions.Throws<InvalidOperationException> (() => {
218                                 attr.IsValid ("test");
219                         }, "#A2");
220
221                         attr = new CustomValidationAttribute (typeof (string), String.Empty);
222                         AssertExtensions.Throws<InvalidOperationException> (() => {
223                                 attr.IsValid ("test");
224                         }, "#A3");
225
226                         attr = new CustomValidationAttribute (typeof (string), "NoSuchMethod");
227                         AssertExtensions.Throws<InvalidOperationException> (() => {
228                                 attr.IsValid ("test");
229                         }, "#A4");
230
231                         attr = new CustomValidationAttribute (typeof (PrivateValidatorMethodContainer), "MethodOne");
232                         AssertExtensions.Throws<InvalidOperationException> (() => {
233                                 attr.IsValid ("test");
234                         }, "#A5");
235
236                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodOne");
237                         AssertExtensions.Throws<InvalidOperationException> (() => {
238                                 attr.IsValid ("test");
239                         }, "#A6");
240
241                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodTwo");
242                         AssertExtensions.Throws<InvalidOperationException> (() => {
243                                 attr.IsValid ("test");
244                         }, "#A7");
245
246                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodThree");
247                         bool valid = attr.IsValid ("test");
248                         Assert.IsTrue (valid, "#A8-1");
249                         valid = attr.IsValid (null);
250                         Assert.IsFalse (valid, "#A8-2");
251                         valid = attr.IsValid ("failTest");
252                         Assert.IsFalse (valid, "#A8-3");
253
254                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodFour");
255                         valid = attr.IsValid ("test");
256                         Assert.IsTrue (valid, "#A9-1");
257                         valid = attr.IsValid (null);
258                         Assert.IsFalse (valid, "#A9-2");
259                         valid = attr.IsValid ("failTest");
260                         Assert.IsFalse (valid, "#A9-3");
261
262                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodFive");
263                         AssertExtensions.Throws<InvalidOperationException> (() => {
264                                 attr.IsValid ("test");
265                         }, "#A10");
266
267                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodSix");
268                         AssertExtensions.Throws<InvalidOperationException> (() => {
269                                 attr.IsValid ("test");
270                         }, "#A11");
271
272                         attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodSeven");
273                         AssertExtensions.Throws<ApplicationException> (() => {
274                                 attr.IsValid ("test");
275                         }, "#A12");
276                 }
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 }