Merge pull request #1936 from esdrubal/DotNetRelativeOrAbsolute
[mono.git] / mcs / class / System.ComponentModel.DataAnnotations / Test / System.ComponentModel.DataAnnotations / ValidationAttributeTest.cs
index edafd4b74354c47fddbe26eb61fe28745bcedb72..cc19b39d2cf45fb0feddabceadf8bd2aa5dc39c9 100644 (file)
@@ -31,6 +31,7 @@ using System.ComponentModel.DataAnnotations;
 using System.Text;
 
 using NUnit.Framework;
+using MonoTests.Common;
 
 namespace MonoTests.System.ComponentModel.DataAnnotations
 {
@@ -88,46 +89,11 @@ namespace MonoTests.System.ComponentModel.DataAnnotations
 
                        attr.ErrorMessage = "Test";
                        Assert.AreEqual ("Test", attr.ErrorMessage, "#A2");
-#if NET_4_0
                        attr.ErrorMessage = String.Empty;
                        Assert.AreEqual (String.Empty, attr.ErrorMessage, "#A3");
 
                        attr.ErrorMessage = null;
                        Assert.IsNull (attr.ErrorMessage, "#A4");
-#else
-                       try {
-                               attr.ErrorMessage = String.Empty;
-                               Assert.Fail ("#A3");
-                       } catch (InvalidOperationException) {
-                               // success
-                       }
-
-                       attr = new ValidateFooAttribute ("Test");
-                       try {
-                               attr.ErrorMessage = null;
-                               Assert.Fail ("#A4");
-                       } catch (ArgumentException) {
-                               // success
-                       }
-
-                       attr = new ValidateFooAttribute ("Test");
-                       try {
-                               attr.ErrorMessage = String.Empty;
-                               Assert.Fail ("#A4");
-                       } catch (ArgumentException) {
-                               // success
-                       }
-
-                       attr = new ValidateFooAttribute ();
-                       attr.ErrorMessageResourceName = "ErrorProperty1";
-
-                       try {
-                               attr.ErrorMessage = "Test Message";
-                               Assert.Fail ("#E1");
-                       } catch (InvalidOperationException) {
-                               // success
-                       }
-#endif
                        
                }
 
@@ -141,47 +107,12 @@ namespace MonoTests.System.ComponentModel.DataAnnotations
                        attr.ErrorMessageResourceName = "Test";
                        Assert.IsNotNull (attr.ErrorMessageResourceName, "#A2-1");
                        Assert.AreEqual ("Test", attr.ErrorMessageResourceName, "#A2-2");
-#if NET_4_0
                        attr.ErrorMessageResourceName = String.Empty;
                        Assert.IsNotNull (attr.ErrorMessageResourceName, "#A3-1");
                        Assert.AreEqual (String.Empty, attr.ErrorMessageResourceName, "#A3-2");
 
                        attr.ErrorMessageResourceName = null;
                        Assert.IsNull (attr.ErrorMessageResourceName, "#A3-1");
-#else
-                       try {
-                               attr.ErrorMessageResourceName = String.Empty;
-                               Assert.Fail ("#A3-1");
-                       } catch (InvalidOperationException) {
-                               // success
-                       }
-
-                       attr = new ValidateFooAttribute ("Test");
-                       try {
-                               attr.ErrorMessageResourceName = String.Empty;
-                               Assert.Fail ("#A3-2");
-                       } catch (ArgumentException) {
-                               // success
-                       }
-
-                       attr = new ValidateFooAttribute ("Test");
-                       try {
-                               attr.ErrorMessageResourceName = null;
-                               Assert.Fail ("#A3-3");
-                       } catch (ArgumentException) {
-                               // success
-                       }
-
-                       attr = new ValidateFooAttribute ();
-                       attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
-
-                       try {
-                               attr.ErrorMessageResourceName = "NoSuchProperty";
-                               Assert.Fail ("#A3-4");
-                       } catch (InvalidOperationException) {
-                               // success
-                       }
-#endif
                }
 
                [Test]
@@ -194,17 +125,6 @@ namespace MonoTests.System.ComponentModel.DataAnnotations
                        attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
                        Assert.IsNotNull (attr.ErrorMessageResourceType, "#A2-1");
                        Assert.AreEqual (typeof (FooErrorMessageProvider), attr.ErrorMessageResourceType, "#A2-2");
-#if !NET_4_0
-                       attr = new ValidateFooAttribute ();
-                       attr.ErrorMessageResourceName = "NoSuchProperty";
-                       
-                       try {
-                               attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
-                               Assert.Fail ("#A3");
-                       } catch (InvalidOperationException) {
-                               // success
-                       }
-#endif
                }
 
                [Test]
@@ -223,7 +143,6 @@ namespace MonoTests.System.ComponentModel.DataAnnotations
                        } catch (InvalidOperationException) {
                                // success
                        }
-#if NET_4_0
                        attr = new ValidateFooAttribute ();
                        attr.ErrorMessageResourceName = String.Empty;
                        try {
@@ -323,7 +242,6 @@ namespace MonoTests.System.ComponentModel.DataAnnotations
                        } catch (InvalidOperationException) {
                                // success
                        }
-#endif
 
                        attr = new ValidateFooAttribute ();
                        attr.ErrorMessageResourceName = "ErrorProperty1";
@@ -340,7 +258,6 @@ namespace MonoTests.System.ComponentModel.DataAnnotations
                        attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
                        Assert.IsNotNull (attr.GetErrorMessageString (), "#D1-3");
                        Assert.AreEqual ("Error Message 1", attr.GetErrorMessageString (), "#D1-4");
-#if NET_4_0
                        attr.ErrorMessage = "Test Message";
                        try {
                                attr.GetErrorMessageString ();
@@ -348,7 +265,6 @@ namespace MonoTests.System.ComponentModel.DataAnnotations
                        } catch (InvalidOperationException) {
                                // success
                        }
-#endif
                }
 
                [Test]
@@ -362,31 +278,20 @@ namespace MonoTests.System.ComponentModel.DataAnnotations
                        attr.ErrorMessage = "Test: {0}";
                        Assert.IsNotNull (attr.FormatErrorMessage ("SomeField"), "#A2-1");
                        Assert.AreEqual ("Test: SomeField", attr.FormatErrorMessage ("SomeField"), "#A2-2");
-#if !NET_4_0
-                       attr = new ValidateFooAttribute ();
-#else
                        attr.ErrorMessage = null;
-#endif
                        attr.ErrorMessageResourceName = "ErrorProperty1";
                        attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
                        Assert.IsNotNull (attr.FormatErrorMessage ("SomeField"), "#B1-1");
                        Assert.AreEqual ("Error Message 1", attr.FormatErrorMessage ("SomeField"), "#B1-2");
-#if !NET_4_0
-                       attr = new ValidateFooAttribute ();
-#endif
                        attr.ErrorMessageResourceName = "ErrorProperty6";
                        attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
                        Assert.IsNotNull (attr.FormatErrorMessage ("SomeField"), "#B2-1");
                        Assert.AreEqual ("Error Message 6: SomeField", attr.FormatErrorMessage ("SomeField"), "#B2-2");
-#if !NET_4_0
-                       attr = new ValidateFooAttribute ();
-#endif
                        attr.ErrorMessageResourceName = "ErrorProperty6";
                        attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
                        Assert.IsNotNull (attr.FormatErrorMessage ("SomeField"), "#B3-1");
                        Assert.AreEqual ("Error Message 6: ", attr.FormatErrorMessage (null), "#B3-2");
                }
-#if NET_4_0
                [Test]
                public void GetValidationResult ()
                {
@@ -446,19 +351,19 @@ namespace MonoTests.System.ComponentModel.DataAnnotations
                {
                        var attr = new ValidateFooAttribute ();
 
-                       try {
+                       AssertExtensions.Throws <NotImplementedException> (() => {
+                               // It calls IsValid (object, validationContext) which throws the NIEX, but when that overload is called directly, there's
+                               // no exception.
+                               //
+                               // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value, ValidationContext validationContext)
+                               // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value)
+                               // 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
                                attr.IsValid (null);
-                               Assert.Fail ("#A1-1");
-                       } catch (NotImplementedException) {
-                               // success
-                       }
-
-                       try {
+                       }, "#A1-1");
+                       
+                       AssertExtensions.Throws <NotImplementedException> (() => {
                                attr.IsValid ("stuff");
-                               Assert.Fail ("#A1-2");
-                       } catch (NotImplementedException) {
-                               // success
-                       }
+                       }, "#A1-2");
                }
 
                [Test]
@@ -466,13 +371,9 @@ namespace MonoTests.System.ComponentModel.DataAnnotations
                {
                        var attr = new ValidateBarAttribute ();
 
-                       try {
-                               // ...
+                       AssertExtensions.Throws <NullReferenceException> (() => {
                                attr.CallIsValid (null, null);
-                               Assert.Fail ("#A1");
-                       } catch (NullReferenceException) {
-                               // success
-                       }
+                       }, "#A1");
 
                        var vc = new ValidationContext ("stuff", null, null);
                        var vr = attr.CallIsValid (null, vc);
@@ -500,6 +401,41 @@ namespace MonoTests.System.ComponentModel.DataAnnotations
                        Assert.AreEqual ("SomeMember", list [0], "#A3-6");
                }
 
+               [Test]
+               public void IsValid_Object_ValidationContext_CrossCallsWithNIEX ()
+               {
+                       var attr = new ValidateSomethingAttribute ();
+
+                       AssertExtensions.Throws<NotImplementedException> (() => {
+                               // Thrown from the IsValid (object, ValidationContext) overload!
+                               //
+                               // MonoTests.System.ComponentModel.DataAnnotations.ValidationAttributeTest.IsValid_Object_ValidationContext_02:
+                               // 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).
+                               //
+                               // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value, ValidationContext validationContext)
+                               // 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
+                               // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value, ValidationContext validationContext)
+                               // 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
+                               // 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
+                               attr.IsValid ("stuff");
+                       }, "#A1");
+
+                       AssertExtensions.Throws<NotImplementedException> (() => {
+                               // And this one is thrown from the IsValid (object) overload!
+                               //
+                               // MonoTests.System.ComponentModel.DataAnnotations.ValidationAttributeTest.IsValid_Object_ValidationContext_CrossCallsWithNIEX:
+                               // 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).
+                               //
+                               // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value)
+                               // 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
+                               // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value)
+                               // 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
+                               // 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
+                               // 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
+
+                               attr.CallIsValid ("stuff", null);
+                       }, "#A2");
+               }
                [Test]
                public void Validate_Object_ValidationContext ()
                {
@@ -524,7 +460,6 @@ namespace MonoTests.System.ComponentModel.DataAnnotations
                        Assert.AreEqual ("bool IsValid (object value)", attr.Calls [1], "#A2-3");
                        Assert.AreEqual ("string FormatErrorMessage (string name)", attr.Calls [2], "#A2-4");
                }
-#endif
                [Test]
                public void Validate_Object_String ()
                {
@@ -561,12 +496,6 @@ namespace MonoTests.System.ComponentModel.DataAnnotations
                {
                        return ErrorMessageString;
                }
-#if !NET_4_0
-               public override bool IsValid (object value)
-               {
-                       return value != null;
-               }
-#endif
        }
 
        class ValidateBarAttribute : ValidateFooAttribute
@@ -581,7 +510,6 @@ namespace MonoTests.System.ComponentModel.DataAnnotations
                {
                        return value != null;
                }
-#if NET_4_0
                protected override ValidationResult IsValid (object value, ValidationContext validationContext)
                {
                        if (!IsValid (value))
@@ -593,7 +521,6 @@ namespace MonoTests.System.ComponentModel.DataAnnotations
                {
                        return base.IsValid (value, validationContext);
                }
-#endif
        }
 
        class ValidateBazAttribute : ValidateBarAttribute
@@ -605,20 +532,36 @@ namespace MonoTests.System.ComponentModel.DataAnnotations
                        Calls.Add ("bool IsValid (object value)");
                        return base.IsValid (value);
                }
-#if NET_4_0
                protected override ValidationResult IsValid (object value, ValidationContext validationContext)
                {
                        Calls.Add ("ValidationResult IsValid (object value, ValidationContext validationContext)");
                        return base.IsValid (value, validationContext);
                }
-#endif
                public override string FormatErrorMessage (string name)
                {
                        Calls.Add ("string FormatErrorMessage (string name)");
                        return base.FormatErrorMessage (name);
                }
        }
+       class ValidateSomethingAttribute : ValidationAttribute
+       {
+               public override bool IsValid (object value)
+               {
+                       return base.IsValid (value, null) == ValidationResult.Success;
+               }
+
+               protected override ValidationResult IsValid (object value, ValidationContext validationContext)
+               {
+                       if (base.IsValid (value))
+                               return ValidationResult.Success;
+                       return new ValidationResult ("failed to validate in base class");
+               }
 
+               public ValidationResult CallIsValid (object value, ValidationContext validationContext)
+               {
+                       return IsValid (value, validationContext);
+               }
+       }
        class FooErrorMessageProvider
        {
                public static string ErrorProperty1