BeanWrapper auto-growing support for EnumSet / EnumMap

Issue: SPR-12483
This commit is contained in:
Juergen Hoeller
2014-11-29 20:49:06 +01:00
parent 7635e7b7f2
commit bfbd25a0e9
4 changed files with 92 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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.
@@ -16,8 +16,12 @@
package org.springframework.beans;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Test;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.tests.sample.beans.CustomEnum;
import org.springframework.tests.sample.beans.GenericBean;
@@ -121,4 +125,52 @@ public final class BeanWrapperEnumTests {
assertTrue(gb.getCustomEnumSet().contains(CustomEnum.VALUE_2));
}
@Test
public void testStandardEnumSetWithMultipleValues() {
GenericBean<?> gb = new GenericBean<Object>();
BeanWrapper bw = new BeanWrapperImpl(gb);
bw.setConversionService(new DefaultConversionService());
assertNull(gb.getStandardEnumSet());
bw.setPropertyValue("standardEnumSet", new String[] {"VALUE_1", "VALUE_2"});
assertEquals(2, gb.getStandardEnumSet().size());
assertTrue(gb.getStandardEnumSet().contains(CustomEnum.VALUE_1));
assertTrue(gb.getStandardEnumSet().contains(CustomEnum.VALUE_2));
}
@Test
public void testStandardEnumSetWithAutoGrowing() {
GenericBean<?> gb = new GenericBean<Object>();
BeanWrapper bw = new BeanWrapperImpl(gb);
bw.setAutoGrowNestedPaths(true);
assertNull(gb.getStandardEnumSet());
bw.getPropertyValue("standardEnumSet.class");
assertEquals(0, gb.getStandardEnumSet().size());
}
@Test
public void testStandardEnumMapWithMultipleValues() {
GenericBean<?> gb = new GenericBean<Object>();
BeanWrapper bw = new BeanWrapperImpl(gb);
bw.setConversionService(new DefaultConversionService());
assertNull(gb.getStandardEnumMap());
Map<String, Integer> map = new LinkedHashMap<String, Integer>();
map.put("VALUE_1", 1);
map.put("VALUE_2", 2);
bw.setPropertyValue("standardEnumMap", map);
assertEquals(2, gb.getStandardEnumMap().size());
assertEquals(new Integer(1), gb.getStandardEnumMap().get(CustomEnum.VALUE_1));
assertEquals(new Integer(2), gb.getStandardEnumMap().get(CustomEnum.VALUE_2));
}
@Test
public void testStandardEnumMapWithAutoGrowing() {
GenericBean<?> gb = new GenericBean<Object>();
BeanWrapper bw = new BeanWrapperImpl(gb);
bw.setAutoGrowNestedPaths(true);
assertNull(gb.getStandardEnumMap());
bw.setPropertyValue("standardEnumMap[VALUE_1]", 1);
assertEquals(1, gb.getStandardEnumMap().size());
assertEquals(new Integer(1), gb.getStandardEnumMap().get(CustomEnum.VALUE_1));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2014 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.
@@ -19,6 +19,8 @@ package org.springframework.tests.sample.beans;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@@ -67,6 +69,10 @@ public class GenericBean<T> {
private Set<CustomEnum> customEnumSet;
private EnumSet<CustomEnum> standardEnumSet;
private EnumMap<CustomEnum, Integer> standardEnumMap;
private T genericProperty;
private List<T> genericListProperty;
@@ -267,6 +273,22 @@ public class GenericBean<T> {
}
}
public EnumSet<CustomEnum> getStandardEnumSet() {
return standardEnumSet;
}
public void setStandardEnumSet(EnumSet<CustomEnum> standardEnumSet) {
this.standardEnumSet = standardEnumSet;
}
public EnumMap<CustomEnum, Integer> getStandardEnumMap() {
return standardEnumMap;
}
public void setStandardEnumMap(EnumMap<CustomEnum, Integer> standardEnumMap) {
this.standardEnumMap = standardEnumMap;
}
public static GenericBean createInstance(Set<Integer> integerSet) {
return new GenericBean(integerSet);
}