From 362c44967943db6c625f52aeb358dd1722fec69b Mon Sep 17 00:00:00 2001 From: Stevo Slavic Date: Tue, 13 Mar 2012 19:55:12 +0100 Subject: [PATCH] Fix constant name lookup for null name prefix Even though javadoc fo Constants.toCode specifies that null value for namePrefix parameter is allowed, before this change pasing null would throw NullPointerException. This change fixes constant name lookup for null name prefix as if empty name prefix was used so that all names match, and lookup is performed only by constant value. This way of handling null value for name prefix is consistent with the rest of Constants class API. Issue: SPR-8278 --- .../main/java/org/springframework/core/Constants.java | 2 +- .../java/org/springframework/core/ConstantsTests.java | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) 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");