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
39 public class CustomValidationAttributeTest
42 public void Constructor ()
44 var attr = new CustomValidationAttribute (null, "MyMethod");
45 Assert.IsNull (attr.ValidatorType, "#A1-1");
46 Assert.AreEqual ("MyMethod", attr.Method, "#A1-2");
48 attr = new CustomValidationAttribute (typeof (string), null);
49 Assert.AreEqual (typeof (string), attr.ValidatorType, "#A2-1");
50 Assert.IsNull (attr.Method, "#A2-2");
52 attr = new CustomValidationAttribute (null, null);
53 Assert.IsNull (attr.ValidatorType, "#A3-1");
54 Assert.IsNull (attr.Method, "#A3-2");
56 attr = new CustomValidationAttribute (typeof (string), "NoSuchMethod");
57 Assert.AreEqual (typeof (string), attr.ValidatorType, "#A5-1");
58 Assert.AreEqual ("NoSuchMethod", attr.Method, "#A5-2");
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");
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");
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");
80 var typeid2 = attr.TypeId as Tuple<string, Type>;
81 Assert.IsTrue (Object.ReferenceEquals (typeid, typeid2), "#A4");
85 public void FormatErrorMessage ()
87 var attr = new CustomValidationAttribute (null, null);
90 AssertExtensions.Throws<InvalidOperationException> (() => {
91 // MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
92 // System.InvalidOperationException : The CustomValidationAttribute.ValidatorType was not specified.
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
98 msg = attr.FormatErrorMessage (null);
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.
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
110 msg = attr.FormatErrorMessage (null);
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.
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
122 msg = attr.FormatErrorMessage (null);
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.
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
134 msg = attr.FormatErrorMessage (null);
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.
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
146 msg = attr.FormatErrorMessage (null);
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.
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);
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.
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);
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");
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");
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.
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);
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.
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);
209 public void IsValid ()
211 var attr = new CustomValidationAttribute (null, null);
213 AssertExtensions.Throws<InvalidOperationException> (() => {
214 attr.IsValid ("test");
217 attr = new CustomValidationAttribute (typeof (string), null);
218 AssertExtensions.Throws<InvalidOperationException> (() => {
219 attr.IsValid ("test");
222 attr = new CustomValidationAttribute (typeof (string), String.Empty);
223 AssertExtensions.Throws<InvalidOperationException> (() => {
224 attr.IsValid ("test");
227 attr = new CustomValidationAttribute (typeof (string), "NoSuchMethod");
228 AssertExtensions.Throws<InvalidOperationException> (() => {
229 attr.IsValid ("test");
232 attr = new CustomValidationAttribute (typeof (PrivateValidatorMethodContainer), "MethodOne");
233 AssertExtensions.Throws<InvalidOperationException> (() => {
234 attr.IsValid ("test");
237 attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodOne");
238 AssertExtensions.Throws<InvalidOperationException> (() => {
239 attr.IsValid ("test");
242 attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodTwo");
243 AssertExtensions.Throws<InvalidOperationException> (() => {
244 attr.IsValid ("test");
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");
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");
263 attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodFive");
264 AssertExtensions.Throws<InvalidOperationException> (() => {
265 attr.IsValid ("test");
268 attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodSix");
269 AssertExtensions.Throws<InvalidOperationException> (() => {
270 attr.IsValid ("test");
273 attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodSeven");
274 AssertExtensions.Throws<ApplicationException> (() => {
275 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");