Skip to content

Commit 700ab2f

Browse files
committed
DataBinder test for java.util.Optional property
Issue: SPR-13933
1 parent a7ad49a commit 700ab2f

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

spring-context/src/test/java/org/springframework/validation/DataBinderTests.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -32,6 +32,7 @@
3232
import java.util.ListIterator;
3333
import java.util.Locale;
3434
import java.util.Map;
35+
import java.util.Optional;
3536
import java.util.Set;
3637
import java.util.TreeSet;
3738

@@ -1050,6 +1051,27 @@ public void testJavaBeanPropertyConventions() {
10501051
assertEquals(0, book.getNInStock());
10511052
}
10521053

1054+
@Test
1055+
public void testOptionalProperty() {
1056+
OptionalHolder bean = new OptionalHolder();
1057+
DataBinder binder = new DataBinder(bean);
1058+
binder.setConversionService(new DefaultConversionService());
1059+
1060+
MutablePropertyValues pvs = new MutablePropertyValues();
1061+
pvs.add("id", "1");
1062+
pvs.add("name", null);
1063+
binder.bind(pvs);
1064+
assertEquals("1", bean.getId());
1065+
assertFalse(bean.getName().isPresent());
1066+
1067+
pvs = new MutablePropertyValues();
1068+
pvs.add("id", "2");
1069+
pvs.add("name", "myName");
1070+
binder.bind(pvs);
1071+
assertEquals("2", bean.getId());
1072+
assertEquals("myName", bean.getName().get());
1073+
}
1074+
10531075
@Test
10541076
public void testValidatorNoErrors() {
10551077
TestBean tb = new TestBean();
@@ -1915,7 +1937,6 @@ public void setIntegerList(List<Integer> integerList) {
19151937
}
19161938

19171939

1918-
@SuppressWarnings("unused")
19191940
private static class Book {
19201941

19211942
private String Title;
@@ -1950,6 +1971,30 @@ public void setNInStock(int nInStock) {
19501971
}
19511972

19521973

1974+
private static class OptionalHolder {
1975+
1976+
private String id;
1977+
1978+
private Optional<String> name;
1979+
1980+
public String getId() {
1981+
return id;
1982+
}
1983+
1984+
public void setId(String id) {
1985+
this.id = id;
1986+
}
1987+
1988+
public Optional<String> getName() {
1989+
return name;
1990+
}
1991+
1992+
public void setName(Optional<String> name) {
1993+
this.name = name;
1994+
}
1995+
}
1996+
1997+
19531998
private static class TestBeanValidator implements Validator {
19541999

19552000
@Override

0 commit comments

Comments
 (0)