Using Localized Resources with Enterprise Library 3 Validation
page 7 of 9
by Brian Mains
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 37336/ 69

Unit Tests

Each validator has an attribute associated with it, but I left it out because there is not anything special done with the attribute classes, other than passing the validator values straight to the validator and exposing the validator properties in the attribute constructor.  Rather, I would like to move to the unit tests that validate the data.  Below are some of the unit tests run against the source and it works successfully.

To make the unit test validation easier, I created a base class that has these two helper methods.

Listing 14

protected void ChangeCulture(string cultureName)
{
  Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
  Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);
}
 
protected List < ValidationResult > GetListOfErrors(ValidationResults results)
{
  return new List < ValidationResult > ((IEnumerable < ValidationResult > )
    results);
}

From this, it makes it easier to process validation results and change the culture for culture-specific tests.  I have three categories of phone number tests: area code tests, correct number tests, and incorrect number tests, each of which is tested for the included cultures provided in the resource files.  A sample of each of the tests is shown below.

Listing 15

[Test]
public void TestEnglishValidatingAreaCodes()
{
  this.ChangeCulture("en-US");
  PhoneNumberValidator validator = new PhoneNumberValidator("717""724""814",
    "610""412");
  List < ValidationResult > results = this.GetListOfErrors(validator.Validate(
    "(724) 555-8712"));
  Assert.IsNotNull(results);
  Assert.AreEqual(0, results.Count);
 
  results = this.GetListOfErrors(validator.Validate("(212) 412-5432"));
  Assert.IsNotNull(results);
  Assert.AreEqual(1, results.Count);
}
 
[Test]
public void TestItalianValidatingCorrectData()
{
  this.ChangeCulture("it-IT");
  PhoneNumberValidator validator = new PhoneNumberValidator(true);
 
  List < ValidationResult > results = this.GetListOfErrors(validator.Validate(
    "23 345 6112"));
  Assert.IsNotNull(results);
  Assert.AreEqual(0, results.Count);
 
  results = this.GetListOfErrors(validator.Validate("234 345 112"));
  Assert.IsNotNull(results);
  Assert.AreEqual(0, results.Count);
}
 
[Test]
public void TestFrenchValidatingIncorrectData()
{
  this.ChangeCulture("fr-FR");
  PhoneNumberValidator validator = new PhoneNumberValidator(true);
 
  List < ValidationResult > results = this.GetListOfErrors(validator.Validate(
    "91 45 65 12 34"));
  Assert.IsNotNull(results);
  Assert.AreEqual(2, results.Count);
 
  results = this.GetListOfErrors(validator.Validate("07 23 34 45 67"));
  Assert.IsNotNull(results);
  Assert.AreEqual(2, results.Count);
 
  results = this.GetListOfErrors(validator.Validate("07 23 34 45 67 56"));
  Assert.IsNotNull(results);
  Assert.AreEqual(3, results.Count);
}

From this, we can ensure the range of tests of valid or invalid data is validated correctly and we can always refactor the tests to include more phone number combinations.  Some other data related points to note from the examples above.

·         Italian phone numbers can have a two-digit area code, which means it has a three digit exchange and a four digit suffix.  But it can have a three-digit area code, which means it has a three digit exchange/suffix.

·         French phone numbers have five sets of two digits, with the area code being a number from 01-09, excluding 07.  As you can see, some of the tests above include 07 phone numbers, which generates an error.


View Entire Article

User Comments

Title: Great Help   
Name: Tarun
Date: 2007-04-26 6:04:28 AM
Comment:
well its really a good help regarding the Enterprise Library 3.0.

I want to same thing for Exception Application block in which i have to localized the exception messages.

Can any one please help me about it.

Thanks.

Regards
Tarun

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-01 8:46:20 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search