Skip to content

Commit eb6368c

Browse files
committed
CustomBooleanEditor avoids potential NPE in case of allowEmpty=false
Issue: SPR-13010 (cherry picked from commit 0cd7fed)
1 parent ed9769a commit eb6368c

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

spring-beans/src/main/java/org/springframework/beans/propertyeditors/CustomBooleanEditor.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -97,27 +97,28 @@ public CustomBooleanEditor(String trueString, String falseString, boolean allowE
9797
this.allowEmpty = allowEmpty;
9898
}
9999

100+
100101
@Override
101102
public void setAsText(String text) throws IllegalArgumentException {
102103
String input = (text != null ? text.trim() : null);
103104
if (this.allowEmpty && !StringUtils.hasLength(input)) {
104105
// Treat empty String as null value.
105106
setValue(null);
106107
}
107-
else if (this.trueString != null && input.equalsIgnoreCase(this.trueString)) {
108+
else if (this.trueString != null && this.trueString.equalsIgnoreCase(input)) {
108109
setValue(Boolean.TRUE);
109110
}
110-
else if (this.falseString != null && input.equalsIgnoreCase(this.falseString)) {
111+
else if (this.falseString != null && this.falseString.equalsIgnoreCase(input)) {
111112
setValue(Boolean.FALSE);
112113
}
113114
else if (this.trueString == null &&
114-
(input.equalsIgnoreCase(VALUE_TRUE) || input.equalsIgnoreCase(VALUE_ON) ||
115-
input.equalsIgnoreCase(VALUE_YES) || input.equals(VALUE_1))) {
115+
(VALUE_TRUE.equalsIgnoreCase(input) || VALUE_ON.equalsIgnoreCase(input) ||
116+
VALUE_YES.equalsIgnoreCase(input) || VALUE_1.equals(input))) {
116117
setValue(Boolean.TRUE);
117118
}
118119
else if (this.falseString == null &&
119-
(input.equalsIgnoreCase(VALUE_FALSE) || input.equalsIgnoreCase(VALUE_OFF) ||
120-
input.equalsIgnoreCase(VALUE_NO) || input.equals(VALUE_0))) {
120+
(VALUE_FALSE.equalsIgnoreCase(input) || VALUE_OFF.equalsIgnoreCase(input) ||
121+
VALUE_NO.equalsIgnoreCase(input) || VALUE_0.equals(input))) {
121122
setValue(Boolean.FALSE);
122123
}
123124
else {

spring-beans/src/test/java/org/springframework/beans/propertyeditors/CustomEditorTests.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -59,7 +59,6 @@
5959
* @author Rob Harrop
6060
* @author Arjen Poutsma
6161
* @author Chris Beams
62-
*
6362
* @since 10.06.2003
6463
*/
6564
public class CustomEditorTests {
@@ -302,8 +301,8 @@ public void testCustomBooleanEditorWithAllowEmpty() {
302301

303302
@Test
304303
public void testCustomBooleanEditorWithSpecialTrueAndFalseStrings() throws Exception {
305-
final String trueString = "pechorin";
306-
final String falseString = "nash";
304+
String trueString = "pechorin";
305+
String falseString = "nash";
307306

308307
CustomBooleanEditor editor = new CustomBooleanEditor(trueString, falseString, false);
309308

@@ -320,6 +319,14 @@ public void testCustomBooleanEditorWithSpecialTrueAndFalseStrings() throws Excep
320319
editor.setAsText(falseString.toUpperCase());
321320
assertFalse(((Boolean) editor.getValue()).booleanValue());
322321
assertEquals(falseString, editor.getAsText());
322+
323+
try {
324+
editor.setAsText(null);
325+
fail("Should have thrown IllegalArgumentException");
326+
}
327+
catch (IllegalArgumentException ex) {
328+
// expected
329+
}
323330
}
324331

325332
@Test
@@ -423,7 +430,7 @@ public void testCustomNumberEditorWithoutAllowEmpty() {
423430
assertTrue("Correct bigDecimal value", new BigDecimal("4.5").equals(tb.getBigDecimal()));
424431
}
425432

426-
@Test(expected=IllegalArgumentException.class)
433+
@Test(expected = IllegalArgumentException.class)
427434
public void testCustomNumberEditorCtorWithNullNumberType() throws Exception {
428435
new CustomNumberEditor(null, true);
429436
}
@@ -543,7 +550,7 @@ public void testCharacterEditorWithAllowEmpty() {
543550
assertNull(cb.getMyCharacter());
544551
}
545552

546-
@Test(expected=IllegalArgumentException.class)
553+
@Test(expected = IllegalArgumentException.class)
547554
public void testCharacterEditorSetAsTextWithStringLongerThanOneCharacter() throws Exception {
548555
PropertyEditor charEditor = new CharacterEditor(false);
549556
charEditor.setAsText("ColdWaterCanyon");
@@ -562,7 +569,7 @@ public void testCharacterEditorGetAsTextReturnsEmptyStringIfValueIsNull() throws
562569
assertEquals(" ", charEditor.getAsText());
563570
}
564571

565-
@Test(expected=IllegalArgumentException.class)
572+
@Test(expected = IllegalArgumentException.class)
566573
public void testCharacterEditorSetAsTextWithNullNotAllowingEmptyAsNull() throws Exception {
567574
PropertyEditor charEditor = new CharacterEditor(false);
568575
charEditor.setAsText(null);
@@ -583,7 +590,7 @@ public void testClassEditor() {
583590
assertEquals("", classEditor.getAsText());
584591
}
585592

586-
@Test(expected=IllegalArgumentException.class)
593+
@Test(expected = IllegalArgumentException.class)
587594
public void testClassEditorWithNonExistentClass() throws Exception {
588595
PropertyEditor classEditor = new ClassEditor();
589596
classEditor.setAsText("hairdresser.on.Fire");
@@ -685,26 +692,40 @@ public void testPatternEditor() {
685692
@Test
686693
public void testCustomBooleanEditor() {
687694
CustomBooleanEditor editor = new CustomBooleanEditor(false);
695+
688696
editor.setAsText("true");
689697
assertEquals(Boolean.TRUE, editor.getValue());
690698
assertEquals("true", editor.getAsText());
699+
691700
editor.setAsText("false");
692701
assertEquals(Boolean.FALSE, editor.getValue());
693702
assertEquals("false", editor.getAsText());
703+
694704
editor.setValue(null);
695705
assertEquals(null, editor.getValue());
696706
assertEquals("", editor.getAsText());
707+
708+
try {
709+
editor.setAsText(null);
710+
fail("Should have thrown IllegalArgumentException");
711+
}
712+
catch (IllegalArgumentException ex) {
713+
// expected
714+
}
697715
}
698716

699717
@Test
700718
public void testCustomBooleanEditorWithEmptyAsNull() {
701719
CustomBooleanEditor editor = new CustomBooleanEditor(true);
720+
702721
editor.setAsText("true");
703722
assertEquals(Boolean.TRUE, editor.getValue());
704723
assertEquals("true", editor.getAsText());
724+
705725
editor.setAsText("false");
706726
assertEquals(Boolean.FALSE, editor.getValue());
707727
assertEquals("false", editor.getAsText());
728+
708729
editor.setValue(null);
709730
assertEquals(null, editor.getValue());
710731
assertEquals("", editor.getAsText());
@@ -750,7 +771,7 @@ public void testCustomDateEditorWithExactDateLength() {
750771
}
751772
catch (IllegalArgumentException ex) {
752773
// expected
753-
assertTrue(ex.getMessage().indexOf("10") != -1);
774+
assertTrue(ex.getMessage().contains("10"));
754775
}
755776
}
756777

0 commit comments

Comments
 (0)