diff --git a/spring-core/src/main/java/org/springframework/core/Constants.java b/spring-core/src/main/java/org/springframework/core/Constants.java index c18b8d289528..048dd0bc941d 100644 --- a/spring-core/src/main/java/org/springframework/core/Constants.java +++ b/spring-core/src/main/java/org/springframework/core/Constants.java @@ -264,7 +264,7 @@ public Set getValuesForSuffix(String nameSuffix) { * @throws ConstantException if the value wasn't found */ public String toCode(Object value, String namePrefix) throws ConstantException { - String prefixToUse = (namePrefix != null ? namePrefix.trim().toUpperCase(Locale.ENGLISH) : null); + String prefixToUse = (namePrefix != null ? namePrefix.trim().toUpperCase(Locale.ENGLISH) : ""); for (Map.Entry entry : this.fieldCache.entrySet()) { if (entry.getKey().startsWith(prefixToUse) && entry.getValue().equals(value)) { return entry.getKey(); diff --git a/spring-core/src/test/java/org/springframework/core/ConstantsTests.java b/spring-core/src/test/java/org/springframework/core/ConstantsTests.java index ee7b7187eb12..47342b35b511 100644 --- a/spring-core/src/test/java/org/springframework/core/ConstantsTests.java +++ b/spring-core/src/test/java/org/springframework/core/ConstantsTests.java @@ -148,19 +148,28 @@ public void testToCode() { assertEquals(c.toCode(new Integer(0), "D"), "DOG"); assertEquals(c.toCode(new Integer(0), "DO"), "DOG"); assertEquals(c.toCode(new Integer(0), "DoG"), "DOG"); + assertEquals(c.toCode(new Integer(0), null), "DOG"); assertEquals(c.toCode(new Integer(66), ""), "CAT"); assertEquals(c.toCode(new Integer(66), "C"), "CAT"); assertEquals(c.toCode(new Integer(66), "ca"), "CAT"); assertEquals(c.toCode(new Integer(66), "cAt"), "CAT"); + assertEquals(c.toCode(new Integer(66), null), "CAT"); assertEquals(c.toCode("", ""), "S1"); assertEquals(c.toCode("", "s"), "S1"); assertEquals(c.toCode("", "s1"), "S1"); + assertEquals(c.toCode("", null), "S1"); try { c.toCode("bogus", "bogus"); fail("Should have thrown ConstantException"); } catch (ConstantException expected) { } + try { + c.toCode("bogus", null); + fail("Should have thrown ConstantException"); + } + catch (ConstantException expected) { + } assertEquals(c.toCodeForProperty(new Integer(1), "myProperty"), "MY_PROPERTY_NO"); assertEquals(c.toCodeForProperty(new Integer(2), "myProperty"), "MY_PROPERTY_YES");