gt: 		PersonName personName = new PersonName();
context: /**
   * This Source Code Form is subject to the terms of the Mozilla Public License,
   * v. 2.0. If a copy of the MPL was not distributed with this file, You can
   * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
   * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
   *
   * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
   * graphic logo is a trademark of OpenMRS Inc.
   */
  package org.openmrs.validator;
  
  import junit.framework.Assert;
  
  import org.apache.commons.lang.StringUtils;
  import org.junit.Before;
  import org.junit.Ignore;
  import org.junit.Test;
  import org.openmrs.GlobalProperty;
  import org.openmrs.PersonName;
  import org.openmrs.api.context.Context;
  import org.openmrs.test.BaseContextSensitiveTest;
  import org.openmrs.test.Verifies;
  import org.openmrs.util.OpenmrsConstants;
  import org.springframework.validation.BindException;
  import org.springframework.validation.Errors;
  import org.springframework.validation.MapBindingResult;
  
  import java.util.HashMap;
  
  /**
   * Tests methods on the {@link PersonNameValidator} class.
   */
  public class PersonNameValidatorTest extends BaseContextSensitiveTest {
  	
  	/**
  	 * Set the GP to a regex used during validation
  	 */
  	@Before
  	public void createNameRegex() {
  		Context.getAdministrationService().saveGlobalProperty(
  		    new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_NAME_REGEX, "^[a-zA-Z \\-]+$"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validate(java.lang.Object, org.springframework.validation.Errors)
  	 */
  	@Test
  	@Verifies(value = "should fail validation if PersonName object is null", method = "validate(java.lang.Object, org.springframework.validation.Errors)")
  	public void validate_shouldFailValidationIfPersonNameObjectIsNull() throws Exception {
  		PersonName personName = new PersonName();
  		Errors errors = new BindException(personName, "personName");
  		new PersonNameValidator().validate(null, errors);
  		Assert.assertTrue(errors.hasErrors());
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should fail validation if PersonName.givenName is null", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldFailValidationIfPersonNameGivenNameIsNull() throws Exception {
  		PersonName personName = new PersonName();
  		Errors errors = new BindException(personName, "givenName");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertTrue(errors.hasFieldErrors("givenName"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should fail validation if PersonName.givenName is empty", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldFailValidationIfPersonNameGivenNameIsEmpty() throws Exception {
  		PersonName personName = new PersonName();
  		personName.setGivenName("");
  		Errors errors = new BindException(personName, "givenName");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertTrue(errors.hasFieldErrors("givenName"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should fail validation if PersonName.givenName is just spaces", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldFailValidationIfPersonNameGivenNameIsJustSpaces() throws Exception {
  		PersonName personName = new PersonName();
  		personName.setGivenName("    ");
  		Errors errors = new BindException(personName, "givenName");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertTrue(errors.hasFieldErrors("givenName"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should fail validation if PersonName.givenName is spaces surrounded by quotation marks", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldFailValidationIfPersonNameGivenNameIsSpacesSurroundedByQuotationMarks() throws Exception {
  		PersonName personName = new PersonName();
  		personName.setGivenName("\"   \"");
  		Errors errors = new BindException(personName, "givenName");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertTrue(errors.hasFieldErrors("givenName"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should pass validation if PersonName.givenName is not blank", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldPassValidationIfPersonNameGivenNameIsNotBlank() throws Exception {
  		PersonName personName = new PersonName();
  		personName.setGivenName("Fred");
  		Errors errors = new BindException(personName, "givenName");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertFalse(errors.hasFieldErrors("givenName"));
  		
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should fail validation if PersonName.familyName is null", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldFailValidationIfPersonNameFamilyNameIsNull() throws Exception {
  		PersonName personName = new PersonName();
  		Errors errors = new BindException(personName, "familyName");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertTrue(errors.hasFieldErrors("familyName"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should fail validation if PersonName.familyName is empty", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldFailValidationIfPersonNameFamilyNameIsEmpty() throws Exception {
  		PersonName personName = new PersonName();
  		personName.setFamilyName("");
  		Errors errors = new BindException(personName, "familyName");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertTrue(errors.hasFieldErrors("familyName"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should fail validation if PersonName.familyName is just spaces", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldFailValidationIfPersonNameFamilyNameIsJustSpaces() throws Exception {
  		PersonName personName = new PersonName();
  		personName.setFamilyName("    ");
  		Errors errors = new BindException(personName, "familyName");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertTrue(errors.hasFieldErrors("familyName"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should fail validation if PersonName.familyName is spaces surrounded by quotation marks", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldFailValidationIfPersonNameFamilyNameIsSpacesSurroundedByQuotationMarks() throws Exception {
  		PersonName personName = new PersonName();
  		personName.setFamilyName("\"   \"");
  		Errors errors = new BindException(personName, "familyName");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertTrue(errors.hasFieldErrors("familyName"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should pass validation if PersonName.familyName is not blank", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldPassValidationIfPersonNameFamilyNameIsNotBlank() throws Exception {
  		PersonName personName = new PersonName();
  		personName.setFamilyName("Rogers");
  		Errors errors = new BindException(personName, "familyName");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertFalse(errors.hasFieldErrors("familyName"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should fail validation if PersonName.prefix is too long", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldFailValidationIfPersonNamePrefixIsTooLong() throws Exception {
  		PersonName personName = new PersonName();
  		personName.setGivenName("givenName");
  		personName.setFamilyName("familyName");
  		personName
  		        .setPrefix("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"); // 100 characters long
  		Errors errors = new BindException(personName, "prefix");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertTrue(errors.hasFieldErrors("prefix"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should pass validation if PersonName.prefix is exactly max length", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldPassValidationIfPersonNamePrefixIsExactlyMaxLength() throws Exception {
  		PersonName personName = new PersonName();
  		personName.setPrefix("12345678901234567890123456789012345678901234567890"); // exactly 50 characters long
  		Errors errors = new BindException(personName, "prefix");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertFalse(errors.hasFieldErrors("prefix"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should pass validation if PersonName.prefix is less than maximum field length", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldPassValidationIfPersonNamePrefixIsLessThanMaxFieldLength() throws Exception {
  		PersonName personName = new PersonName();
  		personName.setPrefix("1234567890");
  		Errors errors = new BindException(personName, "prefix");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertFalse(errors.hasFieldErrors("prefix"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should fail validation if PersonName.givenName is too long", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldFailValidationIfPersonNameGivenNameIsTooLong() throws Exception {
  		PersonName personName = new PersonName();
  		personName
  		        .setGivenName("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"); // 100 characters long
  		Errors errors = new BindException(personName, "givenName");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertTrue(errors.hasFieldErrors("givenName"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should pass validation if PersonName.givenName is exactly max length", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldPassValidationIfPersonNameGivenNameIsExactlyMaxLength() throws Exception {
  		PersonName personName = new PersonName();
  		personName.setGivenName("abcdefghijabcdefghijabcdefghijabcdefghijabcdefghij"); // exactly 50 characters long
  		Errors errors = new BindException(personName, "givenName");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertFalse(errors.hasFieldErrors("givenName"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should pass validation if PersonName.givenName is less than maximum field length", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldPassValidationIfPersonNameGivenNameIsLessThanMaxFieldLength() throws Exception {
  		PersonName personName = new PersonName();
  		personName.setGivenName("abcdefghij");
  		Errors errors = new BindException(personName, "givenName");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertFalse(errors.hasFieldErrors("givenName"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should fail validation if PersonName.middleName is too long", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldFailValidationIfPersonNameMiddleNameIsTooLong() throws Exception {
  		PersonName personName = new PersonName();
  		personName
  		        .setMiddleName("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"); // 100 characters long
  		Errors errors = new BindException(personName, "middleName");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertTrue(errors.hasFieldErrors("middleName"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should pass validation if PersonName.middleName is exactly max length", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldPassValidationIfPersonNameMiddleNameIsExactlyMaxLength() throws Exception {
  		PersonName personName = new PersonName();
  		personName.setMiddleName("abcdefghijabcdefghijabcdefghijabcdefghijabcdefghij"); // exactly 50 characters long
  		Errors errors = new BindException(personName, "middleName");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertFalse(errors.hasFieldErrors("middleName"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should pass validation if PersonName.middleName is less than maximum field length", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldPassValidationIfPersonNameMiddleNameIsLessThanMaxFieldLength() throws Exception {
  		PersonName personName = new PersonName();
  		personName.setMiddleName("abcdefghij");
  		Errors errors = new BindException(personName, "middleName");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertFalse(errors.hasFieldErrors("middleName"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should fail validation if PersonName.familyNamePrefix is too long", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldFailValidationIfPersonNameFamilyNamePrefixIsTooLong() throws Exception {
  		PersonName personName = new PersonName();
  		personName.setGivenName("givenName");
  		personName.setFamilyName("familyName");
  		personName
  		        .setFamilyNamePrefix("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"); // 100 characters long
  		Errors errors = new BindException(personName, "familyNamePrefix");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertTrue(errors.hasFieldErrors("familyNamePrefix"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should pass validation if PersonName.familyNamePrefix is exactly max length", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldPassValidationIfPersonNameFamilyNamePrefixIsExactlyMaxLength() throws Exception {
  		PersonName personName = new PersonName();
  		personName.setFamilyNamePrefix("12345678901234567890123456789012345678901234567890"); // exactly 50 characters long
  		Errors errors = new BindException(personName, "familyNamePrefix");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertFalse(errors.hasFieldErrors("familyNamePrefix"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should pass validation if PersonName.familyNamePrefix is less than maximum field length", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldPassValidationIfPersonNameFamilyNamePrefixIsLessThanMaxFieldLength() throws Exception {
  		PersonName personName = new PersonName();
  		personName.setFamilyNamePrefix("1234567890");
  		Errors errors = new BindException(personName, "familyNamePrefix");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertFalse(errors.hasFieldErrors("familyNamePrefix"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should fail validation if PersonName.familyName is too long", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldFailValidationIfPersonNameFamilyNameIsTooLong() throws Exception {
  		PersonName personName = new PersonName();
  		personName
  		        .setFamilyName("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"); // 100 characters long
  		Errors errors = new BindException(personName, "familyName");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertTrue(errors.hasFieldErrors("familyName"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should pass validation if PersonName.familyName is exactly max length", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldPassValidationIfPersonNameFamilyNameIsExactlyMaxLength() throws Exception {
  		PersonName personName = new PersonName();
  		personName.setFamilyName("abcdefghijabcdefghijabcdefghijabcdefghijabcdefghij"); // exactly 50 characters long
  		Errors errors = new BindException(personName, "familyName");
  		new PersonNameValidator().validatePersonName(personName, errors, false, true);
  		Assert.assertFalse(errors.hasFieldErrors("familyName"));
  	}
  	
  	/**
  	 * @see PatientNameValidator#validatePersonName(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)
  	 */
  	@Test
  	@Verifies(value = "should pass validation if PersonName.familyName is less than maximum field length", method = "validate(java.lang.Object, org.springframework.validation.Errors, boolean, boolean)")
  	public void validate_shouldPassValidationIfPersonNameFamilyNameIsLessThanMaxFieldLength() throws Exception {
  