FormatterImpl implements Formatter { @Override public String format(Object o, Locale locale) { if (o == null) return ""; String className = o.getClass().getName(); Formatter classFormatter = classFormatters.get(getCleanClassName(className)); if (classFormatter != null) { return classFormatter.format(o, locale); } else if (o instanceof Date) { return format((Date) o, locale); } else if (o instanceof Role) { return format((Role) o, locale); } else if (o instanceof Concept) { return format((Concept) o, locale); } else if (o instanceof Person) { return format((Person) o, locale); } else if (o instanceof User) { return format((User) o, locale); } else if (o instanceof Provider) { return format((Provider) o, locale); } else if (o instanceof PatientIdentifierType) { return format((PatientIdentifierType) o, locale); } else if (o instanceof PersonAttribute) { return format((PersonAttribute) o, locale); } else if (o instanceof OpenmrsMetadata) { return format((OpenmrsMetadata) o, locale); } else if (o instanceof Obs) { return format((Obs) o, locale); } else if (o instanceof PatientIdentifier) { return format((PatientIdentifier) o, locale); } else if (o instanceof PersonName) { return format((PersonName) o, locale); } else if (o instanceof PersonAddress) { return format((PersonAddress) o, locale); } else if (o instanceof Number) { return format((Number) o, locale); } else if (o instanceof Class) { return ((Class) o).getName(); } else { return o.toString(); } } FormatterImpl(MessageSource messageSource, AdministrationService administrationService); @Override String format(Object o, Locale locale); void registerClassFormatter(String forClass, Formatter formatter);  }
@Test public void testFormattingAMetadata() throws Exception { Locale locale = Locale.ENGLISH; EncounterType encounterType = new EncounterType(); encounterType.setName("Emergency Check-In"); String output = formatter.format(encounterType, locale); assertThat(output, is(encounterType.getName())); }
@Test public void testFormattingWithOverriddenMetadataName() throws Exception { Locale locale = Locale.ENGLISH; String displayName = "Emergency Check-In"; String uuid = "a-fake-uuid"; messageSource.addMessage("ui.i18n.EncounterType.name." + uuid, displayName); EncounterType encounterType = new EncounterType(); encounterType.setUuid(uuid); String output = formatter.format(encounterType, locale); assertThat(output, is(displayName)); }
@Test public void testFormattingProxiedObjectWithOverriddenMetadataName() throws Exception { Locale locale = Locale.ENGLISH; String displayName = "Emergency Check-In"; String uuid = "a-fake-uuid"; messageSource.addMessage("ui.i18n.EncounterType.name." + uuid, displayName); EncounterType_$$_javassist_26 encounterType = new EncounterType_$$_javassist_26(); encounterType.setUuid(uuid); String output = formatter.format(encounterType, locale); assertThat(output, is(displayName)); }
@Test public void testFormattingRole() throws Exception { Locale locale = Locale.ENGLISH; Role role = new Role(); role.setRole("Admin"); String output = formatter.format(role, locale); assertThat(output, is(role.getRole())); }
@Test public void testFormattingRoleWithOverriddenMetadataName() throws Exception { Locale locale = Locale.ENGLISH; String displayName = "Administrator"; String uuid = "a-fake-uuid"; messageSource.addMessage("ui.i18n.Role.name." + uuid, displayName); Role role = new Role(); role.setRole("Admin"); role.setUuid(uuid); String output = formatter.format(role, locale); assertThat(output, is(displayName)); }
@Test public void testFormattingADateWithNoTime() throws Exception { when(administrationService.getGlobalProperty(UiFrameworkConstants.GP_FORMATTER_DATE_FORMAT, "dd.MMM.yyyy")).thenReturn("dd.MMM.yyyy"); Locale locale = Locale.ENGLISH; Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2003-02-01"); String output = formatter.format(date, locale); assertThat(output, is("01.Feb.2003")); }
@Test public void testFormattingADateWithATime() throws Exception { when(administrationService.getGlobalProperty(UiFrameworkConstants.GP_FORMATTER_DATETIME_FORMAT, "dd.MMM.yyyy, HH:mm:ss")).thenReturn("dd.MMM.yyyy, HH:mm:ss"); Locale locale = Locale.ENGLISH; Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse("2003-02-01 14:25:07.123"); String output = formatter.format(date, locale); assertThat(output, is("01.Feb.2003, 14:25:07")); }
@Test public void testFormattingConceptNumeric() throws Exception { Locale locale = Locale.ENGLISH; ConceptNumeric conceptNumeric = new ConceptNumeric(); conceptNumeric.setUnits("mg"); ConceptDatatype numericDatatype = new ConceptDatatype(); numericDatatype.setHl7Abbreviation("NM"); conceptNumeric.setDatatype(numericDatatype); conceptNumeric.setPrecise(true); Obs numericObs = new Obs(); numericObs.setConcept(conceptNumeric); numericObs.setValueNumeric(1.0); String output = formatter.format(numericObs, locale); assertThat(output, is ("1.0 mg")); }
@Test public void testFormattingConceptNumeric_shouldNotFailIfNoUnits() throws Exception { Locale locale = Locale.ENGLISH; ConceptNumeric conceptNumeric = new ConceptNumeric(); ConceptDatatype numericDatatype = new ConceptDatatype(); numericDatatype.setHl7Abbreviation("NM"); conceptNumeric.setDatatype(numericDatatype); conceptNumeric.setPrecise(true); Obs numericObs = new Obs(); numericObs.setConcept(conceptNumeric); numericObs.setValueNumeric(1.0); String output = formatter.format(numericObs, locale); assertThat(output, is ("1.0")); }
@Test public void testFormattingClass() throws Exception { String output = formatter.format(Date.class, Locale.ENGLISH); assertThat(output, is("java.util.Date")); }
@Test public void testFormattingWholeNumber() throws Exception { String output = formatter.format(100d, Locale.ENGLISH); assertThat(output, is("100")); }
@Test public void testFormattingDecimalNumber() throws Exception { String output = formatter.format(0.5, Locale.ENGLISH); assertThat(output, is("0.5")); }