gt: 		PersonName name6 = new PersonName("acats", "and", "dogs");
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.api.db;
  
  import static org.hamcrest.MatcherAssert.assertThat;
  import static org.hamcrest.Matchers.containsInAnyOrder;
  import static org.hamcrest.Matchers.is;
  
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.List;
  import java.util.Set;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.hamcrest.FeatureMatcher;
  import org.hamcrest.Matcher;
  import org.hibernate.SessionFactory;
  import org.junit.Assert;
  import org.junit.Before;
  import org.junit.Test;
  import org.openmrs.Location;
  import org.openmrs.Patient;
  import org.openmrs.PatientIdentifier;
  import org.openmrs.PatientIdentifierType;
  import org.openmrs.PersonName;
  import org.openmrs.api.PatientService;
  import org.openmrs.api.context.Context;
  import org.openmrs.api.db.hibernate.HibernatePatientDAO;
  import org.openmrs.api.db.hibernate.HibernatePersonDAO;
  import org.openmrs.api.db.hibernate.PersonAttributeHelper;
  import org.openmrs.test.BaseContextSensitiveTest;
  import org.openmrs.test.Verifies;
  import org.openmrs.util.GlobalPropertiesTestHelper;
  import org.openmrs.util.OpenmrsConstants;
  
  public class PatientDAOTest extends BaseContextSensitiveTest {
  	
  	private final static Log log = LogFactory.getLog(PatientDAOTest.class);
  	
  	private final static String PEOPLE_FROM_THE_SHIRE_XML = "org/openmrs/api/db/hibernate/include/HibernatePersonDAOTest-people.xml";
  	
  	private final static String PATIENTS_FROM_THE_SHIRE_XML = "org/openmrs/api/db/hibernate/include/HibernatePatientDAOTest-patients.xml";
  	
  	private PatientDAO dao = null;
  	
  	private PatientService pService = null;
  	
  	private GlobalPropertiesTestHelper globalPropertiesTestHelper;
  	
  	private PersonAttributeHelper personAttributeHelper;
  	
  	/**
  	 * Run this before each unit test in this class. The "@Before" method in
  	 * {@link BaseContextSensitiveTest} is run right before this method.
  	 * 
  	 * @throws Exception
  	 */
  	@Before
  	public void runBeforeEachTest() throws Exception {
  		
  		executeDataSet(PEOPLE_FROM_THE_SHIRE_XML);
  		executeDataSet(PATIENTS_FROM_THE_SHIRE_XML);
  		
  		if (dao == null)
  			// fetch the dao from the spring application context
  			// this bean name matches the name in /metadata/spring/applicationContext-service.xml
  			dao = (PatientDAO) applicationContext.getBean("patientDAO");
  		if (pService == null)
  			pService = Context.getPatientService();
  		
  		SessionFactory sessionFactory = (SessionFactory) applicationContext.getBean("sessionFactory");
  		personAttributeHelper = new PersonAttributeHelper(sessionFactory);
  		globalPropertiesTestHelper = new GlobalPropertiesTestHelper(Context.getAdministrationService());
  		
  	}
  	
  	private void logPatientList(List<Patient> patients) {
  		for (Patient patient : patients) {
  			logPatient(patient);
  		}
  	}
  	
  	private void logPatient(Patient patient) {
  		StringBuilder builder = new StringBuilder();
  		
  		builder.append("class=").append(patient.getClass().getCanonicalName()).append(", person=")
  		        .append(patient.toString()).append(", person.names=").append(patient.getNames().toString()).append(
  		            ", person.attributes=").append(patient.getAttributes().toString());
  		
  		log.debug(builder.toString());
  	}
  	
  	/**
  	 * @see PatientDAO#getPatients(String,String,List<QPatientIdentifierType;>,null)
  	 */
  	@Test
  	@Verifies(value = "should escape an asterix character in identifier phrase", method = "getPatients(String,String,List<QPatientIdentifierType;>,null)")
  	public void getPatients_shouldEscapeAnAsterixCharacterInIdentifierPhrase() throws Exception {
  		//Note that all tests for wildcard should be pass in 2s due to the behaviour of wildcards,
  		//that is we test for the size and actual patient object returned
  		Patient patient2 = pService.getPatient(2);
  		PatientIdentifier patientIdentifier = new PatientIdentifier("*567", pService.getPatientIdentifierType(5), Context
  		        .getLocationService().getLocation(1));
  		patient2.addIdentifier(patientIdentifier);
  		pService.savePatient(patient2);
  		
  		//add closely matching identifier to a different patient
  		Patient patient6 = pService.getPatient(6);
  		PatientIdentifier patientIdentifier6 = new PatientIdentifier("4567", pService.getPatientIdentifierType(5), Context
  		        .getLocationService().getLocation(1));
  		patientIdentifier6.setPreferred(true);
  		patient6.addIdentifier(patientIdentifier6);
  		pService.savePatient(patient6);
  		
  		//we expect only one matching patient
  		int actualSize = dao.getPatients("*567", 0, null).size();
  		Assert.assertEquals(1, actualSize);
  		
  		//if actually the search returned the matching patient
  		Patient actualPatient = dao.getPatients("*567", 0, null).get(0);
  		
  		Assert.assertEquals(patient2, actualPatient);
  	}
  	
  	/**
  	 * @see PatientDAO#getPatients(String,String,List<QPatientIdentifierType;>,null)
  	 */
  	@Test
  	@Verifies(value = "should escape percentage character in identifier phrase", method = "getPatients(String,String,List<QPatientIdentifierType;>,null)")
  	public void getPatients_shouldEscapePercentageCharacterInIdentifierPhrase() throws Exception {
  		
  		Patient patient2 = pService.getPatient(2);
  		PatientIdentifier patientIdentifier = new PatientIdentifier("%567", pService.getPatientIdentifierType(5), Context
  		        .getLocationService().getLocation(1));
  		patient2.addIdentifier(patientIdentifier);
  		pService.savePatient(patient2);
  		
  		//add closely matching identifier to a different patient
  		Patient patient6 = pService.getPatient(6);
  		PatientIdentifier patientIdentifier6 = new PatientIdentifier("4567", pService.getPatientIdentifierType(5), Context
  		        .getLocationService().getLocation(1));
  		patientIdentifier6.setPreferred(true);
  		patient6.addIdentifier(patientIdentifier6);
  		pService.savePatient(patient6);
  		
  		//we expect only one matching patient
  		int actualSize = dao.getPatients("%567", 0, null).size();
  		Assert.assertEquals(1, actualSize);
  		
  		//if actually the search returned the matching patient
  		Patient actualPatient = dao.getPatients("%567", 0, null).get(0);
  		
  		Assert.assertEquals(patient2, actualPatient);
  	}
  	
  	/**
  	 * @see PatientDAO#getPatients(String,String,List<QPatientIdentifierType;>,null)
  	 */
  	@Test
  	@Verifies(value = "should escape underscore character in identifier phrase", method = "getPatients(String,String,List<QPatientIdentifierType;>,null)")
  	public void getPatients_shouldEscapeUnderscoreCharacterInIdentifierPhrase() throws Exception {
  		deleteAllData();
  		baseSetupWithStandardDataAndAuthentication();
  		Patient patient2 = pService.getPatient(2);
  		PatientIdentifier patientIdentifier = new PatientIdentifier("_567", pService.getPatientIdentifierType(5), Context
  		        .getLocationService().getLocation(1));
  		patient2.addIdentifier(patientIdentifier);
  		pService.savePatient(patient2);
  		
  		//add closely matching identifier to a different patient
  		Patient patient6 = pService.getPatient(6);
  		PatientIdentifier patientIdentifier6 = new PatientIdentifier("4567", pService.getPatientIdentifierType(5), Context
  		        .getLocationService().getLocation(1));
  		patientIdentifier6.setPreferred(true);
  		patient6.addIdentifier(patientIdentifier6);
  		pService.savePatient(patient6);
  		
  		//we expect only one matching patient
  		int actualSize = dao.getPatients("_567", 0, null).size();
  		Assert.assertEquals(1, actualSize);
  		
  		//if actually the search returned the matching patient
  		Patient actualPatient = dao.getPatients("_567", 0, null).get(0);
  		
  		Assert.assertEquals(patient2, actualPatient);
  	}
  	
  	/**
  	 * @see PatientDAO#getPatients(String,String,List<QPatientIdentifierType;>,null)
  	 */
  	@Test
  	@Verifies(value = "should escape percentage character in name phrase", method = "getPatients(String,String,List<QPatientIdentifierType;>,null)")
  	public void getPatients_shouldEscapePercentageCharacterInNamePhrase() throws Exception {
  		
  		Patient patient2 = pService.getPatient(2);
  		PersonName name = new PersonName("%cats", "and", "dogs");
  		patient2.addName(name);
  		pService.savePatient(patient2);
  		
  		//add a new closely matching identifier to another patient
  		Patient patient6 = pService.getPatient(6);
  		PersonName name6 = new PersonName("acats", "and", "dogs");
  		patient6.addName(name6);
  		patient6.getPatientIdentifier().setPreferred(true);
  		pService.savePatient(patient6);
  		
  		//we expect only one matching patient
  		int actualSize = dao.getPatients("%ca", 0, null).size();
  		Assert.assertEquals(1, actualSize);
  		
  		Patient actualPatient = dao.getPatients("%ca", 0, null).get(0);
  		//if actually the search returned the matching patient
  		Assert.assertEquals(patient2, actualPatient);
  	}
  	
  	/**
  	 * @see PatientDAO#getPatients(String,String,List<QPatientIdentifierType;>,null)
  	 */
  	@Test
  	@Verifies(value = "should escape underscore character in name phrase", method = "getPatients(String,String,List<QPatientIdentifierType;>,null)")
  	public void getPatients_shouldEscapeUnderscoreCharacterInNamePhrase() throws Exception {
  		
  		Patient patient2 = pService.getPatient(2);
  		PersonName name = new PersonName("_cats", "and", "dogs");
  		patient2.addName(name);
  		pService.savePatient(patient2);
  		
  		//add a new closely matching name to another patient
  		Patient patient6 = pService.getPatient(6);
  		PersonName name6 = new PersonName("acats", "and", "dogs");
  		patient6.addName(name6);
  		patient6.getPatientIdentifier().setPreferred(true);
  		pService.savePatient(patient6);
  		
  		//we expect only one matching patient
  		int actualSize = dao.getPatients("_ca", 0, null).size();
  		Assert.assertEquals(1, actualSize);
  		
  		//if actually the search returned the matching patient
  		Patient actualPatient = dao.getPatients("_ca", 0, null).get(0);
  		Assert.assertEquals(patient2, actualPatient);
  		
  	}
  	
  	/**
  	 * @see PatientDAO#getPatients(String,String,List<QPatientIdentifierType;>,null)
  	 */
  	@Test
  	@Verifies(value = "should escape an asterix character in name phrase", method = "getPatients(String,String,List<QPatientIdentifierType;>,null)")
  	public void getPatients_shouldEscapeAnAsterixCharacterInNamePhrase() throws Exception {
  		
  		Patient patient2 = pService.getPatient(2);
  		PersonName name = new PersonName("*cats", "and", "dogs");
  		patient2.addName(name);
  		pService.savePatient(patient2);
  		
  		//add a new closely matching name to another patient
  		Patient patient6 = pService.getPatient(6);
  