3 // Marek Habersack <grendel@twistedcode.net>
5 // Copyright (C) 2011 Novell, Inc. (http://novell.com/)
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:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
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.
27 using System.Collections.Generic;
28 using System.ComponentModel.DataAnnotations;
29 using System.ComponentModel.Design;
32 using NUnit.Framework;
33 using MonoTests.Common;
35 namespace MonoTests.System.ComponentModel.DataAnnotations
38 public class CustomValidationAttributeTest
41 public void Constructor ()
43 var attr = new CustomValidationAttribute (null, "MyMethod");
44 Assert.IsNull (attr.ValidatorType, "#A1-1");
45 Assert.AreEqual ("MyMethod", attr.Method, "#A1-2");
47 attr = new CustomValidationAttribute (typeof (string), null);
48 Assert.AreEqual (typeof (string), attr.ValidatorType, "#A2-1");
49 Assert.IsNull (attr.Method, "#A2-2");
51 attr = new CustomValidationAttribute (null, null);
52 Assert.IsNull (attr.ValidatorType, "#A3-1");
53 Assert.IsNull (attr.Method, "#A3-2");
55 attr = new CustomValidationAttribute (typeof (string), "NoSuchMethod");
56 Assert.AreEqual (typeof (string), attr.ValidatorType, "#A5-1");
57 Assert.AreEqual ("NoSuchMethod", attr.Method, "#A5-2");
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");
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");
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");
79 var typeid2 = attr.TypeId as Tuple<string, Type>;
80 Assert.IsTrue (Object.ReferenceEquals (typeid, typeid2), "#A4");
84 public void FormatErrorMessage ()
86 var attr = new CustomValidationAttribute (null, null);
89 AssertExtensions.Throws<InvalidOperationException> (() => {
90 // MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
91 // System.InvalidOperationException : The CustomValidationAttribute.ValidatorType was not specified.
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
97 msg = attr.FormatErrorMessage (null);
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.
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
109 msg = attr.FormatErrorMessage (null);
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.
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
121 msg = attr.FormatErrorMessage (null);
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.
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
133 msg = attr.FormatErrorMessage (null);
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.
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
145 msg = attr.FormatErrorMessage (null);
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.
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);
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.
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);
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");
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");
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.
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);
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.
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);
208 public void IsValid ()
210 var attr = new CustomValidationAttribute (null, null);
212 AssertExtensions.Throws<InvalidOperationException> (() => {
213 attr.IsValid ("test");
216 attr = new CustomValidationAttribute (typeof (string), null);
217 AssertExtensions.Throws<InvalidOperationException> (() => {
218 attr.IsValid ("test");
221 attr = new CustomValidationAttribute (typeof (string), String.Empty);
222 AssertExtensions.Throws<InvalidOperationException> (() => {
223 attr.IsValid ("test");
226 attr = new CustomValidationAttribute (typeof (string), "NoSuchMethod");
227 AssertExtensions.Throws<InvalidOperationException> (() => {
228 attr.IsValid ("test");
231 attr = new CustomValidationAttribute (typeof (PrivateValidatorMethodContainer), "MethodOne");
232 AssertExtensions.Throws<InvalidOperationException> (() => {
233 attr.IsValid ("test");
236 attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodOne");
237 AssertExtensions.Throws<InvalidOperationException> (() => {
238 attr.IsValid ("test");
241 attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodTwo");
242 AssertExtensions.Throws<InvalidOperationException> (() => {
243 attr.IsValid ("test");
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");
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");
262 attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodFive");
263 AssertExtensions.Throws<InvalidOperationException> (() => {
264 attr.IsValid ("test");
267 attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodSix");
268 AssertExtensions.Throws<InvalidOperationException> (() => {
269 attr.IsValid ("test");
272 attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodSeven");
273 AssertExtensions.Throws<ApplicationException> (() => {
274 attr.IsValid ("test");
279 class PrivateValidatorMethodContainer
281 public static void MethodOne ()
285 public class PublicValidatorMethodContainer
287 public static void MethodOne ()
290 public static ValidationResult MethodTwo ()
292 return ValidationResult.Success;
295 public static ValidationResult MethodThree (object o)
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");
305 public static ValidationResult MethodFour (object o, ValidationContext ctx)
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");
315 public static ValidationResult MethodFive (object o, string s)
317 return ValidationResult.Success;
320 public static ValidationResult MethodSix (object o, ValidationContext ctx, string s)
322 return ValidationResult.Success;
325 public static ValidationResult MethodSeven (object o, ValidationContext ctx)
327 throw new ApplicationException ("SNAFU");