Browse Source

Polishing

pull/925/merge
Juergen Hoeller 9 years ago
parent
commit
b28310d0b9
  1. 40
      spring-beans/src/main/java/org/springframework/beans/propertyeditors/ResourceBundleEditor.java
  2. 13
      spring-beans/src/test/java/org/springframework/beans/propertyeditors/ResourceBundleEditorTests.java

40
spring-beans/src/main/java/org/springframework/beans/propertyeditors/ResourceBundleEditor.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -24,14 +24,13 @@ import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
* {@link java.beans.PropertyEditor} implementation for * {@link java.beans.PropertyEditor} implementation for standard JDK
* {@link java.util.ResourceBundle ResourceBundles}. * {@link java.util.ResourceBundle ResourceBundles}.
* *
* <p>Only supports conversion <i>from</i> a String, but not * <p>Only supports conversion <i>from</i> a String, but not <i>to</i> a String.
* <i>to</i> a String.
* *
* Find below some examples of using this class in a * Find below some examples of using this class in a (properly configured)
* (properly configured) Spring container using XML-based metadata: * Spring container using XML-based metadata:
* *
* <pre class="code"> &lt;bean id="errorDialog" class="..."&gt; * <pre class="code"> &lt;bean id="errorDialog" class="..."&gt;
* &lt;!-- * &lt;!--
@ -68,13 +67,14 @@ import org.springframework.util.StringUtils;
* <p>Thanks to David Leal Valmana for the suggestion and initial prototype. * <p>Thanks to David Leal Valmana for the suggestion and initial prototype.
* *
* @author Rick Evans * @author Rick Evans
* @author Juergen Hoeller
* @since 2.0 * @since 2.0
*/ */
public class ResourceBundleEditor extends PropertyEditorSupport { public class ResourceBundleEditor extends PropertyEditorSupport {
/** /**
* The separator used to distinguish between the base name and the * The separator used to distinguish between the base name and the locale
* locale (if any) when {@link #setAsText(String) converting from a String}. * (if any) when {@link #setAsText(String) converting from a String}.
*/ */
public static final String BASE_NAME_SEPARATOR = "_"; public static final String BASE_NAME_SEPARATOR = "_";
@ -82,25 +82,23 @@ public class ResourceBundleEditor extends PropertyEditorSupport {
@Override @Override
public void setAsText(String text) throws IllegalArgumentException { public void setAsText(String text) throws IllegalArgumentException {
Assert.hasText(text, "'text' must not be empty"); Assert.hasText(text, "'text' must not be empty");
ResourceBundle bundle; String name = text.trim();
String rawBaseName = text.trim();
int indexOfBaseNameSeparator = rawBaseName.indexOf(BASE_NAME_SEPARATOR); int separator = name.indexOf(BASE_NAME_SEPARATOR);
if (indexOfBaseNameSeparator == -1) { if (separator == -1) {
bundle = ResourceBundle.getBundle(rawBaseName); setValue(ResourceBundle.getBundle(name));
} }
else { else {
// it potentially has locale information // Tge basename potentially has locale information
String baseName = rawBaseName.substring(0, indexOfBaseNameSeparator); String baseName = name.substring(0, separator);
if (!StringUtils.hasText(baseName)) { if (!StringUtils.hasText(baseName)) {
throw new IllegalArgumentException("Bad ResourceBundle name : received '" + text + "' as argument to 'setAsText(String value)'."); throw new IllegalArgumentException("Invalid ResourceBundle name: '" + text + "'");
} }
String localeString = rawBaseName.substring(indexOfBaseNameSeparator + 1); String localeString = name.substring(separator + 1);
Locale locale = StringUtils.parseLocaleString(localeString); Locale locale = StringUtils.parseLocaleString(localeString);
bundle = (StringUtils.hasText(localeString)) setValue((StringUtils.hasText(localeString)) ? ResourceBundle.getBundle(baseName, locale) :
? ResourceBundle.getBundle(baseName, locale) ResourceBundle.getBundle(baseName));
: ResourceBundle.getBundle(baseName);
} }
setValue(bundle);
} }
} }

13
spring-beans/src/test/java/org/springframework/beans/propertyeditors/ResourceBundleEditorTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2006 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,9 +28,10 @@ import static org.junit.Assert.*;
* @author Rick Evans * @author Rick Evans
* @author Chris Beams * @author Chris Beams
*/ */
public final class ResourceBundleEditorTests { public class ResourceBundleEditorTests {
private static final String BASE_NAME = ResourceBundleEditorTests.class.getName(); private static final String BASE_NAME = ResourceBundleEditorTests.class.getName();
private static final String MESSAGE_KEY = "punk"; private static final String MESSAGE_KEY = "punk";
@ -94,25 +95,25 @@ public final class ResourceBundleEditorTests {
assertEquals("ned", string); assertEquals("ned", string);
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testSetAsTextWithNull() throws Exception { public void testSetAsTextWithNull() throws Exception {
ResourceBundleEditor editor = new ResourceBundleEditor(); ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText(null); editor.setAsText(null);
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testSetAsTextWithEmptyString() throws Exception { public void testSetAsTextWithEmptyString() throws Exception {
ResourceBundleEditor editor = new ResourceBundleEditor(); ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText(""); editor.setAsText("");
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testSetAsTextWithWhiteSpaceString() throws Exception { public void testSetAsTextWithWhiteSpaceString() throws Exception {
ResourceBundleEditor editor = new ResourceBundleEditor(); ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText(" "); editor.setAsText(" ");
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testSetAsTextWithJustSeparatorString() throws Exception { public void testSetAsTextWithJustSeparatorString() throws Exception {
ResourceBundleEditor editor = new ResourceBundleEditor(); ResourceBundleEditor editor = new ResourceBundleEditor();
editor.setAsText("_"); editor.setAsText("_");

Loading…
Cancel
Save