From 6ffb0436fa55fb73048a5a9133a98e122e490119 Mon Sep 17 00:00:00 2001 From: Stevo Slavic Date: Tue, 13 Mar 2012 19:55:12 +0100 Subject: [PATCH] Allow null params as advertised in Constants#toCode* Even though the Javadoc for Constants#toCode and #toCodeForSuffix specifies that a null value for the 'namePrefix' and 'nameSuffix' parameters are respectively allowed, before this change passing a null to either would result in a NullPointerException. This change fixes constant name lookup for null values of these params as if an empty string had been passed instead. This way of handling a null value is consistent with the rest of Constants class API. Issue: SPR-8278 --- .../org/springframework/core/Constants.java | 6 +++--- .../springframework/core/ConstantsTests.java | 20 ++++++++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) 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 c18b8d28952..0dd6306fb4d 100644 --- a/spring-core/src/main/java/org/springframework/core/Constants.java +++ b/spring-core/src/main/java/org/springframework/core/Constants.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -264,7 +264,7 @@ public class Constants { * @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(); @@ -295,7 +295,7 @@ public class Constants { * @throws ConstantException if the value wasn't found */ public String toCodeForSuffix(Object value, String nameSuffix) throws ConstantException { - String suffixToUse = (nameSuffix != null ? nameSuffix.trim().toUpperCase(Locale.ENGLISH) : null); + String suffixToUse = (nameSuffix != null ? nameSuffix.trim().toUpperCase(Locale.ENGLISH) : ""); for (Map.Entry entry : this.fieldCache.entrySet()) { if (entry.getKey().endsWith(suffixToUse) && 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 ee7b7187eb1..868d925301c 100644 --- a/spring-core/src/test/java/org/springframework/core/ConstantsTests.java +++ b/spring-core/src/test/java/org/springframework/core/ConstantsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -148,19 +148,28 @@ public class ConstantsTests extends TestCase { 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"); @@ -175,19 +184,28 @@ public class ConstantsTests extends TestCase { assertEquals(c.toCodeForSuffix(new Integer(0), "G"), "DOG"); assertEquals(c.toCodeForSuffix(new Integer(0), "OG"), "DOG"); assertEquals(c.toCodeForSuffix(new Integer(0), "DoG"), "DOG"); + assertEquals(c.toCodeForSuffix(new Integer(0), null), "DOG"); assertEquals(c.toCodeForSuffix(new Integer(66), ""), "CAT"); assertEquals(c.toCodeForSuffix(new Integer(66), "T"), "CAT"); assertEquals(c.toCodeForSuffix(new Integer(66), "at"), "CAT"); assertEquals(c.toCodeForSuffix(new Integer(66), "cAt"), "CAT"); + assertEquals(c.toCodeForSuffix(new Integer(66), null), "CAT"); assertEquals(c.toCodeForSuffix("", ""), "S1"); assertEquals(c.toCodeForSuffix("", "1"), "S1"); assertEquals(c.toCodeForSuffix("", "s1"), "S1"); + assertEquals(c.toCodeForSuffix("", null), "S1"); try { c.toCodeForSuffix("bogus", "bogus"); fail("Should have thrown ConstantException"); } catch (ConstantException expected) { } + try { + c.toCodeForSuffix("bogus", null); + fail("Should have thrown ConstantException"); + } + catch (ConstantException expected) { + } } public void testGetValuesWithNullPrefix() throws Exception {