|
1 | 1 | /* |
2 | | - * Copyright 2002-2015 the original author or authors. |
| 2 | + * Copyright 2002-2016 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
32 | 32 | import java.util.ListIterator; |
33 | 33 | import java.util.Locale; |
34 | 34 | import java.util.Map; |
| 35 | +import java.util.Optional; |
35 | 36 | import java.util.Set; |
36 | 37 | import java.util.TreeSet; |
37 | 38 |
|
@@ -1050,6 +1051,27 @@ public void testJavaBeanPropertyConventions() { |
1050 | 1051 | assertEquals(0, book.getNInStock()); |
1051 | 1052 | } |
1052 | 1053 |
|
| 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 | + |
1053 | 1075 | @Test |
1054 | 1076 | public void testValidatorNoErrors() { |
1055 | 1077 | TestBean tb = new TestBean(); |
@@ -1915,7 +1937,6 @@ public void setIntegerList(List<Integer> integerList) { |
1915 | 1937 | } |
1916 | 1938 |
|
1917 | 1939 |
|
1918 | | - @SuppressWarnings("unused") |
1919 | 1940 | private static class Book { |
1920 | 1941 |
|
1921 | 1942 | private String Title; |
@@ -1950,6 +1971,30 @@ public void setNInStock(int nInStock) { |
1950 | 1971 | } |
1951 | 1972 |
|
1952 | 1973 |
|
| 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 | + |
1953 | 1998 | private static class TestBeanValidator implements Validator { |
1954 | 1999 |
|
1955 | 2000 | @Override |
|
0 commit comments