@Override public void setAsText(String text) throws IllegalArgumentException { Assert.hasText(text, "'text' must not be empty"); String name = text.trim(); int separator = name.indexOf(BASE_NAME_SEPARATOR); if (separator == -1) { setValue(ResourceBundle.getBundle(name)); } else { // The name potentially contains locale information String baseName = name.substring(0, separator); if (!StringUtils.hasText(baseName)) { throw new IllegalArgumentException("Invalid ResourceBundle name: '" + text + "'"); } String localeString = name.substring(separator + 1); Locale locale = StringUtils.parseLocaleString(localeString); setValue(locale != null ? ResourceBundle.getBundle(baseName, locale) : ResourceBundle.getBundle(baseName)); } }
@Test(expected = IllegalArgumentException.class) public void testSetAsTextWithWhiteSpaceString() throws Exception { ResourceBundleEditor editor = new ResourceBundleEditor(); editor.setAsText(" "); }
@Test public void testSetAsTextWithBaseNameAndLanguageCode() throws Exception { ResourceBundleEditor editor = new ResourceBundleEditor(); editor.setAsText(BASE_NAME + "Lang" + "_en"); Object value = editor.getValue(); assertNotNull("Returned ResourceBundle was null (must not be for valid setAsText(..) call).", value); assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).", value instanceof ResourceBundle); ResourceBundle bundle = (ResourceBundle) value; String string = bundle.getString(MESSAGE_KEY); assertEquals("yob", string); }
@Test public void testSetAsTextWithJustBaseName() throws Exception { ResourceBundleEditor editor = new ResourceBundleEditor(); editor.setAsText(BASE_NAME); Object value = editor.getValue(); assertNotNull("Returned ResourceBundle was null (must not be for valid setAsText(..) call).", value); assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).", value instanceof ResourceBundle); ResourceBundle bundle = (ResourceBundle) value; String string = bundle.getString(MESSAGE_KEY); assertEquals(MESSAGE_KEY, string); }
@Override public void setAsText(String text) throws IllegalArgumentException { Assert.hasText(text, "'text' must not be empty"); String name = text.trim(); int separator = name.indexOf(BASE_NAME_SEPARATOR); if (separator == -1) { setValue(ResourceBundle.getBundle(name)); } else { // The name potentially contains locale information String baseName = name.substring(0, separator); if (!StringUtils.hasText(baseName)) { throw new IllegalArgumentException("Invalid ResourceBundle name: '" + text + "'"); } String localeString = name.substring(separator + 1); Locale locale = StringUtils.parseLocaleString(localeString); setValue(locale != null ? ResourceBundle.getBundle(baseName, locale) : ResourceBundle.getBundle(baseName)); } }
@Test public void testSetAsTextWithTheKitchenSink() throws Exception { ResourceBundleEditor editor = new ResourceBundleEditor(); editor.setAsText(BASE_NAME + "LangCountryDialect" + "_en_GB_GLASGOW"); Object value = editor.getValue(); assertNotNull("Returned ResourceBundle was null (must not be for valid setAsText(..) call).", value); assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).", value instanceof ResourceBundle); ResourceBundle bundle = (ResourceBundle) value; String string = bundle.getString(MESSAGE_KEY); assertEquals("ned", string); }
@Test(expected = IllegalArgumentException.class) public void testSetAsTextWithEmptyString() throws Exception { ResourceBundleEditor editor = new ResourceBundleEditor(); editor.setAsText(""); }
@Override public void setAsText(String text) throws IllegalArgumentException { Assert.hasText(text, "'text' must not be empty"); ResourceBundle bundle; String rawBaseName = text.trim(); int indexOfBaseNameSeparator = rawBaseName.indexOf(BASE_NAME_SEPARATOR); if (indexOfBaseNameSeparator == -1) { bundle = ResourceBundle.getBundle(rawBaseName); } else { // it potentially has locale information String baseName = rawBaseName.substring(0, indexOfBaseNameSeparator); if (!StringUtils.hasText(baseName)) { throw new IllegalArgumentException("Bad ResourceBundle name : received '" + text + "' as argument to 'setAsText(String value)'."); } String localeString = rawBaseName.substring(indexOfBaseNameSeparator + 1); Locale locale = StringUtils.parseLocaleString(localeString); bundle = (StringUtils.hasText(localeString)) ? ResourceBundle.getBundle(baseName, locale) : ResourceBundle.getBundle(baseName); } setValue(bundle); }
@Test public void testSetAsTextWithBaseNameThatEndsInDefaultSeparator() throws Exception { ResourceBundleEditor editor = new ResourceBundleEditor(); editor.setAsText(BASE_NAME + "_"); Object value = editor.getValue(); assertNotNull("Returned ResourceBundle was null (must not be for valid setAsText(..) call).", value); assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).", value instanceof ResourceBundle); ResourceBundle bundle = (ResourceBundle) value; String string = bundle.getString(MESSAGE_KEY); assertEquals(MESSAGE_KEY, string); }
@Test(expected = IllegalArgumentException.class) public void testSetAsTextWithNull() throws Exception { ResourceBundleEditor editor = new ResourceBundleEditor(); editor.setAsText(null); }
@Override public void setAsText(String text) throws IllegalArgumentException { Assert.hasText(text, "'text' must not be empty"); String name = text.trim(); int separator = name.indexOf(BASE_NAME_SEPARATOR); if (separator == -1) { setValue(ResourceBundle.getBundle(name)); } else { // The name potentially contains locale information String baseName = name.substring(0, separator); if (!StringUtils.hasText(baseName)) { throw new IllegalArgumentException("Invalid ResourceBundle name: '" + text + "'"); } String localeString = name.substring(separator + 1); Locale locale = StringUtils.parseLocaleString(localeString); setValue(locale != null ? ResourceBundle.getBundle(baseName, locale) : ResourceBundle.getBundle(baseName)); } }
@Test public void testSetAsTextWithBaseNameLanguageAndCountryCode() throws Exception { ResourceBundleEditor editor = new ResourceBundleEditor(); editor.setAsText(BASE_NAME + "LangCountry" + "_en_GB"); Object value = editor.getValue(); assertNotNull("Returned ResourceBundle was null (must not be for valid setAsText(..) call).", value); assertTrue("Returned object was not a ResourceBundle (must be for valid setAsText(..) call).", value instanceof ResourceBundle); ResourceBundle bundle = (ResourceBundle) value; String string = bundle.getString(MESSAGE_KEY); assertEquals("chav", string); }
@Test(expected = IllegalArgumentException.class) public void testSetAsTextWithJustSeparatorString() throws Exception { ResourceBundleEditor editor = new ResourceBundleEditor(); editor.setAsText("_"); }