@After public void revertToDefaultLocale() { Context.setLocale(Locale.US); }
/** * Become a different user. (You should only be able to do this as a superuser.) * * @param systemId * @throws ContextAuthenticationException * @should change locale when become another user */ public static void becomeUser(String systemId) throws ContextAuthenticationException { log.info("systemId: {}", systemId); User user = getUserContext().becomeUser(systemId); // if assuming identity procedure finished successfully, we should change context locale parameter Locale locale = null; if (user.getUserProperties().containsKey(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCALE)) { String localeString = user.getUserProperty(OpenmrsConstants.USER_PROPERTY_DEFAULT_LOCALE); locale = LocaleUtility.fromSpecification(localeString); } // when locale parameter is not valid or does not exist if (locale == null) { locale = LocaleUtility.getDefaultLocale(); } Context.setLocale(locale); }
/** * @see LocaleUtility#getLocalesInOrder() */ @Test public void getLocalesInOrder_shouldHaveDefaultLocaleAsTheSecondElementIfUserHasAPreferredLocale() { Locale lu_UG = new Locale("lu", "UG"); Context.setLocale(lu_UG); Set<Locale> localesInOrder = LocaleUtility.getLocalesInOrder(); Iterator<Locale> it = localesInOrder.iterator(); Assert.assertEquals(lu_UG, it.next()); Assert.assertEquals(LocaleUtility.getDefaultLocale(), it.next()); }
/** * @see LocaleUtility#getLocalesInOrder() */ @Test public void getLocalesInOrder_shouldHaveDefaultLocaleAsTheFirstElementIfUserHasNoPreferredLocale() { // make sure the user doesn't have a locale Context.setLocale(null); Set<Locale> localesInOrder = LocaleUtility.getLocalesInOrder(); Assert.assertEquals(LocaleUtility.getDefaultLocale(), localesInOrder.iterator().next()); }
/** * @see LocaleUtility#getLocalesInOrder() */ @Test public void getLocalesInOrder_shouldReturnASetOfLocalesWithAPredictableOrder() { GlobalProperty gp = new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "lu, sw_KE, en_US, en_GB", "Test Allowed list of locales"); Context.getAdministrationService().saveGlobalProperty(gp); Locale lu_UG = new Locale("lu", "UG"); Context.setLocale(lu_UG); Set<Locale> localesInOrder = LocaleUtility.getLocalesInOrder(); Iterator<Locale> it = localesInOrder.iterator(); Assert.assertEquals(new Locale("lu", "UG"), it.next()); Assert.assertEquals(LocaleUtility.getDefaultLocale(), it.next()); Assert.assertEquals(new Locale("lu"), it.next()); Assert.assertEquals(new Locale("sw", "KE"), it.next()); Assert.assertEquals(new Locale("en", "US"), it.next()); Assert.assertEquals(new Locale("en"), it.next()); }
/** * This test doesn't really test anything, and it should ALWAYS be the last method in this * class. <br> * <br> * This method just resets the current user's locale so that when things are run in batches all * tests still work. */ @Test public void should_resetTheLocale() { // set user locale to nothing Context.setLocale(null); // clear out the caches GlobalProperty defaultLocale = new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE, "", "blanking out default locale"); Context.getAdministrationService().saveGlobalProperty(defaultLocale); }
@Test public void getSearchLocales_shouldIncludeCurrentlySelectedFullLocaleAndLangugage() { //given adminService.saveGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB")); User user = Context.getAuthenticatedUser(); user.setUserProperty(OpenmrsConstants.USER_PROPERTY_PROFICIENT_LOCALES, ""); Context.getUserService().saveUser(user); Context.setLocale(new Locale("en", "GB")); //when List<Locale> searchLocales = adminService.getSearchLocales(); //then assertEquals(Context.getLocale(), searchLocales.get(0)); assertEquals(new Locale(Context.getLocale().getLanguage()), searchLocales.get(1)); }
/** * This tests for being able to find concepts with names in en_GB locale when the user is in the * en_GB locale. * * @see ConceptService#getConceptByName(String) */ @Test public void getConceptByName_shouldFindConceptsWithNamesInSameSpecificLocale() { executeDataSet(INITIAL_CONCEPTS_XML); Context.setLocale(Locale.UK); // make sure that concepts are found that have a specific locale on them Assert.assertNotNull(Context.getConceptService().getConceptByName("Numeric name with en_GB locale")); }
/** * This tests for being able to find concepts with names in the en locale when the user is in * the en_GB locale * * @see ConceptService#getConceptByName(String) */ @Test public void getConceptByName_shouldFindConceptsWithNamesInMoreGenericLocales() { executeDataSet(INITIAL_CONCEPTS_XML); //prior tests have changed the locale to 'en_US', so we need to set it back Context.setLocale(Locale.UK); // make sure that concepts are found that have a specific locale on them Assert.assertNotNull(Context.getConceptService().getConceptByName("Some numeric concept name")); }
/** * This tests for being able to find concepts with names in en_GB locale when the user is in the * en locale. * * @see ConceptService#getConceptByName(String) */ @Test public void getConceptByName_shouldFindConceptsWithNamesInMoreSpecificLocales() { Locale origLocale = Context.getLocale(); executeDataSet(INITIAL_CONCEPTS_XML); Context.setLocale(Locale.ENGLISH); // make sure that concepts are found that have a specific locale on them Assert.assertNotNull(Context.getConceptService().getConceptByName("Numeric name with en_GB locale")); // find concepts with same generic locale Assert.assertNotNull(Context.getConceptService().getConceptByName("Some numeric concept name")); // reset the locale for the next test Context.setLocale(origLocale); }
@Test public void validate_shouldPassIfTheDuplicateConceptNameIsNeitherPreferredNorFullySpecified() { Context.setLocale(new Locale("en", "GB")); Concept concept = cd4Count; //use a synonym as the duplicate name ConceptName duplicateName = concept.getSynonyms(Context.getLocale()).iterator().next(); Assert.assertEquals(true, duplicateName.isSynonym()); Concept anotherConcept = weight; anotherConcept.getFullySpecifiedName(Context.getLocale()).setName(duplicateName.getName()); Errors errors = new BindException(anotherConcept, "concept"); validator.validate(anotherConcept, errors); Assert.assertEquals(false, errors.hasErrors()); }
@Test public void validate_shouldFailIfThereIsADuplicateUnretiredConceptNameInTheLocale() { Context.setLocale(new Locale("en", "GB")); concept = cd4Count; String duplicateName = concept.getFullySpecifiedName(Context.getLocale()).getName(); ConceptName newName = new ConceptName(duplicateName, Context.getLocale()); newName.setDateCreated(Calendar.getInstance().getTime()); newName.setCreator(Context.getAuthenticatedUser()); concept.addName(newName); errors = new BindException(concept, "concept"); expectedException.expect(DuplicateConceptNameException.class); expectedException .expectMessage("'" + duplicateName + "' is a duplicate name in locale '" + Context.getLocale() + "'"); validator.validate(concept, errors); }
@Test public void validate_shouldFailIfThereIsADuplicateUnretiredConceptNameInTheSameLocaleDifferentThanTheSystemLocale() { Context.setLocale(new Locale("pl")); Locale en = new Locale("en", "GB"); Concept concept = cd4Count; Assert.assertEquals(true, concept.getFullySpecifiedName(en).isFullySpecifiedName()); String duplicateName = concept.getFullySpecifiedName(en).getName(); Concept anotherConcept = weight; anotherConcept.getFullySpecifiedName(en).setName(duplicateName); Errors errors = new BindException(anotherConcept, "concept"); expectedException.expect(DuplicateConceptNameException.class); expectedException .expectMessage("'" + duplicateName + "' is a duplicate name in locale '" + en + "'"); validator.validate(concept, errors); validator.validate(anotherConcept, errors); }
/** * @see LocaleUtility#getLocalesInOrder() */ @Test public void getLocalesInOrder_shouldReturnASetOfLocalesWithNoDuplicates() { GlobalProperty gp = new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "lu_UG, lu, sw_KE, en_US, en, en, en_GB, sw_KE", "Test Allowed list of locales"); Context.getAdministrationService().saveGlobalProperty(gp); GlobalProperty defaultLocale = new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE, "lu", "Test Allowed list of locales"); Context.getAdministrationService().saveGlobalProperty(defaultLocale); Locale lu_UG = new Locale("lu", "UG"); Context.setLocale(lu_UG); //note that unique list of locales should be lu_UG, lu, sw_KE, en_US, en Assert.assertEquals(6, LocaleUtility.getLocalesInOrder().size()); Context.getAdministrationService().saveGlobalProperty( new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE, "")); }
@Test public void validate_shouldPassIfTheConceptWithADuplicateNameIsRetired() { Context.setLocale(new Locale("en", "GB")); Concept concept = cd4Count; concept.setRetired(true); conceptService.saveConcept(concept); String duplicateName = concept.getFullySpecifiedName(Context.getLocale()).getName(); Concept anotherConcept = weight; anotherConcept.getFullySpecifiedName(Context.getLocale()).setName(duplicateName); Errors errors = new BindException(anotherConcept, "concept"); validator.validate(anotherConcept, errors); Assert.assertFalse(errors.hasErrors()); }
@Test public void validate_shouldPassIfTheConceptBeingValidatedIsRetiredAndHasADuplicateName() { Context.setLocale(new Locale("en", "GB")); Concept concept = cd4Count; conceptService.saveConcept(concept); String duplicateName = concept.getFullySpecifiedName(Context.getLocale()).getName(); Concept anotherConcept = weight; anotherConcept.setRetired(true); anotherConcept.getFullySpecifiedName(Context.getLocale()).setName(duplicateName); Errors errors = new BindException(anotherConcept, "concept"); validator.validate(anotherConcept, errors); Assert.assertFalse(errors.hasErrors()); }
@Test public void validate_shouldPassIfDifferentConceptsHaveTheSameShortNames() { Context.setLocale(new Locale("en", "GB")); List<Concept> concepts = conceptService.getConceptsByName("HSM"); Assert.assertEquals(1, concepts.size()); Assert.assertEquals(true, concepts.get(0).getShortNameInLocale(Context.getLocale()).getName() .equalsIgnoreCase("HSM")); concept.setConceptClass(new ConceptClass()); concept.setDatatype(new ConceptDatatype()); ConceptName conceptFullySpecifiedName = new ConceptName("holosystolic murmur", Context.getLocale()); conceptFullySpecifiedName.setConceptNameType(ConceptNameType.FULLY_SPECIFIED); ConceptName conceptShortName = new ConceptName("HSM", Context.getLocale()); conceptShortName.setConceptNameType(ConceptNameType.SHORT); concept.addName(conceptFullySpecifiedName); concept.addName(conceptShortName); concept.addDescription(new ConceptDescription("some description",null)); validator.validate(concept, errors); Assert.assertFalse(errors.hasErrors()); }
@Test public void validate_shouldFailIfThereIsADuplicateUnretiredFullySpecifiedNameInTheSameLocale() { Context.setLocale(new Locale("en", "GB")); Assert.assertEquals(true, cd4Count.getFullySpecifiedName(Context.getLocale()).isFullySpecifiedName()); String duplicateName = cd4Count.getFullySpecifiedName(Context.getLocale()).getName(); Concept anotherConcept = weight; anotherConcept.getFullySpecifiedName(Context.getLocale()).setName(duplicateName); Errors errors = new BindException(anotherConcept, "concept"); expectedException.expect(DuplicateConceptNameException.class); expectedException .expectMessage("'" + duplicateName + "' is a duplicate name in locale '" + Context.getLocale() + "'"); validator.validate(anotherConcept, errors); }
@Test public void validate_shouldFailIfThereIsADuplicateUnretiredPreferredNameInTheSameLocale() { Context.setLocale(new Locale("en", "GB")); Concept concept = cd4Count; ConceptName preferredName = new ConceptName("preferred name", Context.getLocale()); concept.setPreferredName(preferredName); conceptService.saveConcept(concept); Assert.assertEquals("preferred name", concept.getPreferredName(Context.getLocale()).getName()); Concept anotherConcept = weight; anotherConcept.getFullySpecifiedName(Context.getLocale()).setName("preferred name"); Errors errors = new BindException(anotherConcept, "concept"); expectedException.expect(DuplicateConceptNameException.class); expectedException .expectMessage("'" + preferredName + "' is a duplicate name in locale '" + Context.getLocale() + "'"); validator.validate(anotherConcept, errors); }
/** * @see ConceptService#saveConcept(Concept) */ @Test public void saveConcept_shouldSaveAConceptNumericAsAConcept() { executeDataSet(INITIAL_CONCEPTS_XML); //This will automatically add the given locale to the list of allowed locales Context.setLocale(Locale.US); // this tests saving a previously conceptnumeric as just a concept Concept c2 = new Concept(2); ConceptName cn = new ConceptName("not a numeric anymore", Locale.US); c2.addName(cn); c2.addDescription(new ConceptDescription("some description",null)); c2.setDatatype(new ConceptDatatype(3)); c2.setConceptClass(new ConceptClass(1)); conceptService.saveConcept(c2); Concept secondConcept = conceptService.getConcept(2); // this will probably still be a ConceptNumeric object. what to do about that? // revisit this problem when discriminators are in place //assertFalse(secondConcept instanceof ConceptNumeric); // this shouldn't think its a conceptnumeric object though assertFalse(secondConcept.isNumeric()); assertEquals("not a numeric anymore", secondConcept.getName(Locale.US).getName()); }