Inheritance: System.ComponentModel.DataAnnotations.DataTypeAttribute
Beispiel #1
0
        public static void Validate_throws_InvalidOperationException_if_ErrorMessage_is_null()
        {
            var attribute = new FileExtensionsAttribute();

            attribute.ErrorMessage = null; // note: this overrides the default value
            Assert.Throws <InvalidOperationException>(() => attribute.Validate("someFile.nonContainedExtension", s_testValidationContext));
        }
 public static void Validate_throws_InvalidOperationException_if_ErrorMessage_and_ErrorMessageResourceName_are_set()
 {
     var attribute = new FileExtensionsAttribute();
     attribute.ErrorMessage = "SomeErrorMessage";
     attribute.ErrorMessageResourceName = "SomeErrorMessageResourceName";
     Assert.Throws<InvalidOperationException>(() => attribute.Validate("someFile.nonContainedExtension", s_testValidationContext));
 }
Beispiel #3
0
        public static void FileExtensionsAttribute_creation_DataType_and_CustomDataType()
        {
            var attribute = new FileExtensionsAttribute();

            Assert.Equal(DataType.Upload, attribute.DataType);
            Assert.Null(attribute.CustomDataType);
        }
Beispiel #4
0
        public static void Validate_throws_InvalidOperationException_if_ErrorMessageResourceName_set_but_ErrorMessageResourceType_not_set()
        {
            var attribute = new FileExtensionsAttribute();

            attribute.ErrorMessageResourceName = "SomeErrorMessageResourceName";
            attribute.ErrorMessageResourceType = null;
            Assert.Throws <InvalidOperationException>(() => attribute.Validate("someFile.nonContainedExtension", s_testValidationContext));
        }
        public static void Validate_throws_for_non_contained_extensions()
        {
            var attribute = new FileExtensionsAttribute();
            Assert.Throws<ValidationException>(() => attribute.Validate(string.Empty, s_testValidationContext));
            Assert.Throws<ValidationException>(() => attribute.Validate("someFile.nonContainedExtension", s_testValidationContext));

            // mixture of dotted and non-dotted extensions, separated by spaces as well as commas
            attribute.Extensions = "myExt, .otherExt, UPPERCASE_extension";
            Assert.Throws<ValidationException>(() => attribute.Validate("someFile.nonContainedExtension", s_testValidationContext));
        }
        public static void Validate_successful_for_contained_extensions()
        {
            var attribute = new FileExtensionsAttribute();
            AssertEx.DoesNotThrow(() => attribute.Validate(".jpeg", s_testValidationContext));

            // mixture of dotted and non-dotted extensions, separated by spaces as well as commas
            attribute.Extensions = "myExt, .otherExt, UPPERCASE_extension";
            AssertEx.DoesNotThrow(() => attribute.Validate("myfile.myExt", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("some.Other.File.otherext", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("Case.Does.Not.matter.uppercase_EXTENSION", s_testValidationContext));
        }
Beispiel #7
0
        public static void Validate_throws_for_non_contained_extensions()
        {
            var attribute = new FileExtensionsAttribute();

            Assert.Throws <ValidationException>(() => attribute.Validate(string.Empty, s_testValidationContext));
            Assert.Throws <ValidationException>(() => attribute.Validate("someFile.nonContainedExtension", s_testValidationContext));

            // mixture of dotted and non-dotted extensions, separated by spaces as well as commas
            attribute.Extensions = "myExt, .otherExt, UPPERCASE_extension";
            Assert.Throws <ValidationException>(() => attribute.Validate("someFile.nonContainedExtension", s_testValidationContext));
        }
        public void ErrorMessageTest()
        {
            var attribute = new FileExtensionsAttribute();
            attribute.ErrorMessage = "SampleErrorMessage";

            const string invalidValue = "a";

            var result = attribute.GetValidationResult(invalidValue, new ValidationContext(0, null, null));

            Assert.AreEqual("SampleErrorMessage", result.ErrorMessage);
        }
Beispiel #9
0
        public static void GetValidationResult_returns_DefaultErrorMessage_if_ErrorMessage_is_not_set()
        {
            var attribute = new FileExtensionsAttribute();

            attribute.Extensions = "";
            var toBeTested        = new FileExtensionsClassToBeTested();
            var validationContext = new ValidationContext(toBeTested);

            validationContext.MemberName = "FileExtensionsPropertyToBeTested";
            AssertEx.DoesNotThrow(() => attribute.GetValidationResult(toBeTested, validationContext));
        }
Beispiel #10
0
        public static void Validate_successful_for_contained_extensions()
        {
            var attribute = new FileExtensionsAttribute();

            AssertEx.DoesNotThrow(() => attribute.Validate(".jpeg", s_testValidationContext));

            // mixture of dotted and non-dotted extensions, separated by spaces as well as commas
            attribute.Extensions = "myExt, .otherExt, UPPERCASE_extension";
            AssertEx.DoesNotThrow(() => attribute.Validate("myfile.myExt", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("some.Other.File.otherext", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("Case.Does.Not.matter.uppercase_EXTENSION", s_testValidationContext));
        }
        public void ErrorResourcesTest()
        {
            var attribute = new FileExtensionsAttribute();
            attribute.ErrorMessageResourceName = "ErrorMessage";
            attribute.ErrorMessageResourceType = typeof(ErrorResources);

            const string invalidValue = "a";

            var result = attribute.GetValidationResult(invalidValue, new ValidationContext(0, null, null));

            Assert.AreEqual("error message", result.ErrorMessage);
        }
Beispiel #12
0
        public static void GetValidationResult_returns_ErrorMessage_if_ErrorMessage_overrides_default()
        {
            var attribute = new FileExtensionsAttribute();

            attribute.ErrorMessage = "SomeErrorMessage";
            var toBeTested        = new FileExtensionsClassToBeTested();
            var validationContext = new ValidationContext(toBeTested);

            validationContext.MemberName = "FileExtensionsPropertyToBeTested";
            var validationResult = attribute.GetValidationResult(toBeTested, validationContext);

            Assert.Equal("SomeErrorMessage", validationResult.ErrorMessage);
        }
        public void GlobalizedErrorResourcesTest()
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-MX");

            var attribute = new FileExtensionsAttribute();
            attribute.ErrorMessageResourceName = "ErrorMessage";
            attribute.ErrorMessageResourceType = typeof(ErrorResources);

            const string invalidValue = "a";

            var result = attribute.GetValidationResult(invalidValue, new ValidationContext(0, null, null));

            Assert.AreEqual("mensaje de error", result.ErrorMessage);
        }
		public void IsValid ()
		{
			var sla = new FileExtensionsAttribute () {
				Extensions = "txt,jpg"
			};

			Assert.IsTrue (sla.IsValid (null), "#A1-1");
			Assert.IsFalse (sla.IsValid (String.Empty), "#A1-2");
			Assert.IsFalse (sla.IsValid ("string"), "#A1-3");
			Assert.IsTrue (sla.IsValid ("file.txt"), "#A1-4");
			Assert.IsTrue (sla.IsValid ("file.jpg"), "#A1-5");
			Assert.IsTrue (sla.IsValid ("file.xxx.txt"), "#A-6");
			Assert.IsFalse (sla.IsValid (true), "#A1-7");
			Assert.IsFalse (sla.IsValid (DateTime.Now), "#A1-8");
		}
        public void IsValidWithCustomArgumentsTests()
        {
            var attribute = new FileExtensionsAttribute("pdf|doc|docx|rtf");

            Assert.IsTrue(attribute.IsValid(null));  // Optional values are always valid
            Assert.IsTrue(attribute.IsValid("foo.pdf"));
            Assert.IsTrue(attribute.IsValid("foo.doc"));
            Assert.IsTrue(attribute.IsValid("foo.docx"));
            Assert.IsTrue(attribute.IsValid("foo.rtf"));
            Assert.IsTrue(attribute.IsValid(@"C:\Foo\bar.pdf"));
            Assert.IsFalse(attribute.IsValid("foo"));
            Assert.IsFalse(attribute.IsValid("foo.png"));
            Assert.IsFalse(attribute.IsValid("foo.jpeg"));
            Assert.IsFalse(attribute.IsValid("foo.doc.txt"));
        }
        public void IsValidWithNoArgumentTests()
        {
            var attribute = new FileExtensionsAttribute();

            Assert.IsTrue(attribute.IsValid(null));  // Optional values are always valid
            Assert.IsTrue(attribute.IsValid("foo.png"));
            Assert.IsTrue(attribute.IsValid("foo.jpeg"));
            Assert.IsTrue(attribute.IsValid("foo.jpg"));
            Assert.IsTrue(attribute.IsValid("foo.gif"));
            Assert.IsTrue(attribute.IsValid(@"C:\Foo\bar.png"));
            Assert.IsFalse(attribute.IsValid("foo"));
            Assert.IsFalse(attribute.IsValid("foo.doc"));
            Assert.IsFalse(attribute.IsValid("foo.txt"));
            Assert.IsFalse(attribute.IsValid("foo.png.txt"));
        }
Beispiel #17
0
        public static void GetValidationResult_returns_ErrorMessage_from_resource_if_ErrorMessageResourceName_and_ErrorMessageResourceType_both_set()
        {
            var attribute = new FileExtensionsAttribute();

            attribute.ErrorMessageResourceName = "InternalErrorMessageTestProperty";
            attribute.ErrorMessageResourceType = typeof(ErrorMessageResources);
            var toBeTested        = new FileExtensionsClassToBeTested();
            var validationContext = new ValidationContext(toBeTested);

            validationContext.MemberName = "FileExtensionsPropertyToBeTested";
            var validationResult = attribute.GetValidationResult(toBeTested, validationContext);

            Assert.Equal(
                "Error Message from ErrorMessageResources.InternalErrorMessageTestProperty",
                validationResult.ErrorMessage);
        }
        public static void Can_get_and_set_Extensions()
        {
            var attribute = new FileExtensionsAttribute();
            Assert.Equal("png,jpg,jpeg,gif", attribute.Extensions); // default value
            attribute.Extensions = "test1, test2, test3";
            Assert.Equal("test1, test2, test3", attribute.Extensions);

            // reverts to default value if set to empty
            attribute.Extensions = string.Empty;
            Assert.Equal("png,jpg,jpeg,gif", attribute.Extensions);

            // reverts to default value if set to whitespace
            attribute.Extensions = " \t\r\n";
            Assert.Equal("png,jpg,jpeg,gif", attribute.Extensions);

            // reverts to default value if set to null
            attribute.Extensions = null;
            Assert.Equal("png,jpg,jpeg,gif", attribute.Extensions);
        }
Beispiel #19
0
        public static void Can_get_and_set_Extensions()
        {
            var attribute = new FileExtensionsAttribute();

            Assert.Equal("png,jpg,jpeg,gif", attribute.Extensions); // default value
            attribute.Extensions = "test1, test2, test3";
            Assert.Equal("test1, test2, test3", attribute.Extensions);

            // reverts to default value if set to empty
            attribute.Extensions = string.Empty;
            Assert.Equal("png,jpg,jpeg,gif", attribute.Extensions);

            // reverts to default value if set to whitespace
            attribute.Extensions = " \t\r\n";
            Assert.Equal("png,jpg,jpeg,gif", attribute.Extensions);

            // reverts to default value if set to null
            attribute.Extensions = null;
            Assert.Equal("png,jpg,jpeg,gif", attribute.Extensions);
        }
 public static void GetValidationResult_returns_DefaultErrorMessage_if_ErrorMessage_is_not_set()
 {
     var attribute = new FileExtensionsAttribute();
     attribute.Extensions = "";
     var toBeTested = new FileExtensionsClassToBeTested();
     var validationContext = new ValidationContext(toBeTested);
     validationContext.MemberName = "FileExtensionsPropertyToBeTested";
     AssertEx.DoesNotThrow(() => attribute.GetValidationResult(toBeTested, validationContext));
 }
 public FileUploadExtensionsAttribute()
     : base(DataType.Upload)
 {
     ErrorMessage = new FileExtensionsAttribute() { Extensions = this.Extensions }.ErrorMessage;
 }
 public static void Validate_throws_InvalidOperationException_if_ErrorMessageResourceType_set_but_ErrorMessageResourceName_not_set()
 {
     var attribute = new FileExtensionsAttribute();
     attribute.ErrorMessageResourceName = null;
     attribute.ErrorMessageResourceType = typeof(ErrorMessageResources);
     Assert.Throws<InvalidOperationException>(() => attribute.Validate("someFile.nonContainedExtension", s_testValidationContext));
 }
 public static void Validate_throws_InvalidOperationException_if_ErrorMessage_is_null()
 {
     var attribute = new FileExtensionsAttribute();
     attribute.ErrorMessage = null; // note: this overrides the default value
     Assert.Throws<InvalidOperationException>(() => attribute.Validate("someFile.nonContainedExtension", s_testValidationContext));
 }
Beispiel #24
0
        public static void Validate_successful_for_null_value()
        {
            var attribute = new FileExtensionsAttribute();

            AssertEx.DoesNotThrow(() => attribute.Validate(null, s_testValidationContext)); // Null is valid
        }
 public static void GetValidationResult_returns_ErrorMessage_if_ErrorMessage_overrides_default()
 {
     var attribute = new FileExtensionsAttribute();
     attribute.ErrorMessage = "SomeErrorMessage";
     var toBeTested = new FileExtensionsClassToBeTested();
     var validationContext = new ValidationContext(toBeTested);
     validationContext.MemberName = "FileExtensionsPropertyToBeTested";
     var validationResult = attribute.GetValidationResult(toBeTested, validationContext);
     Assert.Equal("SomeErrorMessage", validationResult.ErrorMessage);
 }
 public static void Validate_successful_for_null_value()
 {
     var attribute = new FileExtensionsAttribute();
     AssertEx.DoesNotThrow(() => attribute.Validate(null, s_testValidationContext)); // Null is valid
 }
 public static void FileExtensionsAttribute_creation_DataType_and_CustomDataType()
 {
     var attribute = new FileExtensionsAttribute();
     Assert.Equal(DataType.Upload, attribute.DataType);
     Assert.Null(attribute.CustomDataType);
 }
 public static void GetValidationResult_returns_ErrorMessage_from_resource_if_ErrorMessageResourceName_and_ErrorMessageResourceType_both_set()
 {
     var attribute = new FileExtensionsAttribute();
     attribute.ErrorMessageResourceName = "InternalErrorMessageTestProperty";
     attribute.ErrorMessageResourceType = typeof(ErrorMessageResources);
     var toBeTested = new FileExtensionsClassToBeTested();
     var validationContext = new ValidationContext(toBeTested);
     validationContext.MemberName = "FileExtensionsPropertyToBeTested";
     var validationResult = attribute.GetValidationResult(toBeTested, validationContext);
     Assert.Equal(
         "Error Message from ErrorMessageResources.InternalErrorMessageTestProperty",
         validationResult.ErrorMessage);
 }
        public void GetClientValidationRules_ReturnsEmptyRuleSet()
        {
            // Arrange
            var attribute = new FileExtensionsAttribute();
            var validator = new DataAnnotationsModelValidator<FileExtensionsAttribute>(attribute);

            var metadata = _metadataProvider.GetMetadataForProperty(
                containerType: typeof(string),
                propertyName: nameof(string.Length));

            var serviceCollection = new ServiceCollection();
            var requestServices = serviceCollection.BuildServiceProvider();

            var context = new ClientModelValidationContext(metadata, _metadataProvider, requestServices);

            // Act
            var results = validator.GetClientValidationRules(context);

            // Assert
            Assert.Empty(results);
        }