Java Code Examples for org.springframework.beans.propertyeditors.CustomNumberEditor

The following examples show how to use org.springframework.beans.propertyeditors.CustomNumberEditor. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-analysis-note   Source File: BeanWrapperGenericsTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testGenericMapWithCollectionValue() {
	GenericBean<?> gb = new GenericBean<>();
	BeanWrapper bw = new BeanWrapperImpl(gb);
	bw.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
	Map<String, Collection> input = new HashMap<>();
	HashSet<Integer> value1 = new HashSet<>();
	value1.add(new Integer(1));
	input.put("1", value1);
	ArrayList<Boolean> value2 = new ArrayList<>();
	value2.add(Boolean.TRUE);
	input.put("2", value2);
	bw.setPropertyValue("collectionMap", input);
	assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
	assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList);
}
 
Example 2
@Test
public void testCustomEditor() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
			registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true));
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("myFloat", "1,1");
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPropertyValues(pvs);
	lbf.registerBeanDefinition("testBean", bd);
	TestBean testBean = (TestBean) lbf.getBean("testBean");
	assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
 
Example 3
@Test
public void testCustomEditorWithBeanReference() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
			registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true));
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("myFloat", new RuntimeBeanReference("myFloat"));
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPropertyValues(pvs);
	lbf.registerBeanDefinition("testBean", bd);
	lbf.registerSingleton("myFloat", "1,1");
	TestBean testBean = (TestBean) lbf.getBean("testBean");
	assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
 
Example 4
Source Project: java-technology-stack   Source File: BeanWrapperGenericsTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testGenericMapWithCollectionValue() {
	GenericBean<?> gb = new GenericBean<>();
	BeanWrapper bw = new BeanWrapperImpl(gb);
	bw.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
	Map<String, Collection> input = new HashMap<>();
	HashSet<Integer> value1 = new HashSet<>();
	value1.add(new Integer(1));
	input.put("1", value1);
	ArrayList<Boolean> value2 = new ArrayList<>();
	value2.add(Boolean.TRUE);
	input.put("2", value2);
	bw.setPropertyValue("collectionMap", input);
	assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
	assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList);
}
 
Example 5
@Test
public void testCustomEditor() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
			registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true));
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("myFloat", "1,1");
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPropertyValues(pvs);
	lbf.registerBeanDefinition("testBean", bd);
	TestBean testBean = (TestBean) lbf.getBean("testBean");
	assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
 
Example 6
@Test
public void testCustomEditorWithBeanReference() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
			registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true));
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("myFloat", new RuntimeBeanReference("myFloat"));
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPropertyValues(pvs);
	lbf.registerBeanDefinition("testBean", bd);
	lbf.registerSingleton("myFloat", "1,1");
	TestBean testBean = (TestBean) lbf.getBean("testBean");
	assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
 
Example 7
@Test
public void testGenericMapWithCollectionValue() {
	GenericBean<?> gb = new GenericBean<Object>();
	BeanWrapper bw = new BeanWrapperImpl(gb);
	bw.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
	Map<String, Collection> input = new HashMap<String, Collection>();
	HashSet<Integer> value1 = new HashSet<Integer>();
	value1.add(new Integer(1));
	input.put("1", value1);
	ArrayList<Boolean> value2 = new ArrayList<Boolean>();
	value2.add(Boolean.TRUE);
	input.put("2", value2);
	bw.setPropertyValue("collectionMap", input);
	assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
	assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList);
}
 
Example 8
@Test
public void testCustomEditor() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
			registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true));
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("myFloat", "1,1");
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPropertyValues(pvs);
	lbf.registerBeanDefinition("testBean", bd);
	TestBean testBean = (TestBean) lbf.getBean("testBean");
	assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
 
Example 9
@Test
public void testCustomEditorWithBeanReference() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
			registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true));
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("myFloat", new RuntimeBeanReference("myFloat"));
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPropertyValues(pvs);
	lbf.registerBeanDefinition("testBean", bd);
	lbf.registerSingleton("myFloat", "1,1");
	TestBean testBean = (TestBean) lbf.getBean("testBean");
	assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
 
Example 10
Source Project: AlgoTrader   Source File: GrailsDataBinder.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Registers all known
 *
 * @param registry
 * @param locale
 */
public static void registerCustomEditors(PropertyEditorRegistry registry, Locale locale) {
	// Formatters for the different number types.
	NumberFormat floatFormat = NumberFormat.getInstance(locale);
	NumberFormat integerFormat = NumberFormat.getIntegerInstance(locale);

	DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT, locale);

	registry.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	registry.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, floatFormat, true));
	registry.registerCustomEditor(BigInteger.class, new CustomNumberEditor(BigInteger.class, floatFormat, true));
	registry.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, floatFormat, true));
	registry.registerCustomEditor(double.class, new CustomNumberEditor(Double.class, floatFormat, true));
	registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, floatFormat, true));
	registry.registerCustomEditor(float.class, new CustomNumberEditor(Float.class, floatFormat, true));
	registry.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, integerFormat, true));
	registry.registerCustomEditor(long.class, new CustomNumberEditor(Long.class, integerFormat, true));
	registry.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, integerFormat, true));
	registry.registerCustomEditor(int.class, new CustomNumberEditor(Integer.class, integerFormat, true));
	registry.registerCustomEditor(Short.class, new CustomNumberEditor(Short.class, integerFormat, true));
	registry.registerCustomEditor(short.class, new CustomNumberEditor(Short.class, integerFormat, true));
	registry.registerCustomEditor(Date.class, new StructuredDateEditor(dateFormat, true));
	registry.registerCustomEditor(Calendar.class, new StructuredDateEditor(dateFormat, true));

	registerCustomEditors(registry);
}
 
Example 11
Source Project: webcurator   Source File: SitePermissionHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
	super.initBinder(request, binder);
	
       NumberFormat nf = NumberFormat.getInstance(request.getLocale());	
       
       // Register the binders.
       binder.registerCustomEditor(Long.class, "selectedPermission", new CustomNumberEditor(Long.class, nf, true));
	binder.registerCustomEditor(java.util.Date.class, "startDate", DateUtils.get().getFullDateEditor(true));
	binder.registerCustomEditor(java.util.Date.class, "endDate", DateUtils.get().getFullDateEditor(true));
	
	// If the session model is available, we want to register the Permission's
	// authorising agency editor.
	if(getEditorContext(request) != null) {
		//binder.registerCustomEditor(AuthorisingAgent.class, new PermissionAuthAgencyEditor(sessionModel.getAuthorisingAgents()));
		binder.registerCustomEditor(AuthorisingAgent.class, "authorisingAgent", new EditorContextObjectEditor(getEditorContext(request), AuthorisingAgent.class));
		binder.registerCustomEditor(Set.class, "urls", new UrlPatternCollectionEditor(Set.class, true, getEditorContext(request)));
	}
}
 
Example 12
Source Project: webcurator   Source File: SitePermissionController.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Initialise some special binders for this command. (Overrides Spring
 * method).
 * @param request The HttpServletRequest.
 * @param binder  The binder.
 */
@Override
public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
	super.initBinder(request, binder);
	
       NumberFormat nf = NumberFormat.getInstance(request.getLocale());	
       
       // Register the binders.
       binder.registerCustomEditor(Long.class, "selectedPermission", new CustomNumberEditor(Long.class, nf, true));
	binder.registerCustomEditor(java.util.Date.class, "startDate", DateUtils.get().getFullDateEditor(true));
	binder.registerCustomEditor(java.util.Date.class, "endDate", DateUtils.get().getFullDateEditor(true));
	binder.registerCustomEditor(java.util.Date.class, "openAccessDate", DateUtils.get().getFullDateEditor(true));
	
	// If the session model is available, we want to register the Permission's
	// authorising agency editor.
	if(getEditorContext(request) != null) {
		binder.registerCustomEditor(AuthorisingAgent.class, "authorisingAgent", new EditorContextObjectEditor(getEditorContext(request), AuthorisingAgent.class));
		binder.registerCustomEditor(Set.class, "urls", new UrlPatternCollectionEditor(Set.class, true, getEditorContext(request)));
		binder.registerCustomEditor(Integer.class, "deleteExclusionIndex", new CustomNumberEditor(Integer.class, true));
	}
}
 
Example 13
Source Project: webcurator   Source File: EditScheduleController.java    License: Apache License 2.0 6 votes vote down vote up
@Override
  public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
// Determine the necessary formats.
      NumberFormat nf = NumberFormat.getInstance(request.getLocale());
      
      // Register the binders.
      binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, nf, true));
      binder.registerCustomEditor(Integer.class, "scheduleType", new CustomNumberEditor(Integer.class, nf, true));
      binder.registerCustomEditor(java.util.Date.class, org.webcurator.ui.util.DateUtils.get().getFullDateEditor(true));
      binder.registerCustomEditor(Time.class, new TimeEditor(false));
  }
 
Example 14
Source Project: ankush   Source File: BaseController.java    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Set up a custom property editor for converting form inputs to real
 * objects.
 *
 * @param request the current request
 * @param binder the data binder
 */
@InitBinder
protected void initBinder(HttpServletRequest request,
		ServletRequestDataBinder binder) {
	binder.registerCustomEditor(Integer.class, null,
			new CustomNumberEditor(Integer.class, null, true));
	binder.registerCustomEditor(Long.class, null, new CustomNumberEditor(
			Long.class, null, true));
	binder.registerCustomEditor(byte[].class,
			new ByteArrayMultipartFileEditor());
	SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy.MM.dd G 'at' HH:mm:ss z");
	dateFormat.setLenient(false);
	binder.registerCustomEditor(Date.class, null, new CustomDateEditor(
			dateFormat, true));
}
 
Example 15
Source Project: spring-analysis-note   Source File: DataBinderTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testBindingWithCustomEditorOnObjectField() {
	BeanWithObjectProperty tb = new BeanWithObjectProperty();
	DataBinder binder = new DataBinder(tb);
	binder.registerCustomEditor(Integer.class, "object", new CustomNumberEditor(Integer.class, true));
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("object", "1");
	binder.bind(pvs);
	assertEquals(new Integer(1), tb.getObject());
}
 
Example 16
Source Project: spring-analysis-note   Source File: BeanWrapperGenericsTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testGenericLowerBoundedSet() {
	GenericBean<?> gb = new GenericBean<>();
	BeanWrapper bw = new BeanWrapperImpl(gb);
	bw.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, true));
	Set<String> input = new HashSet<>();
	input.add("4");
	input.add("5");
	bw.setPropertyValue("numberSet", input);
	assertTrue(gb.getNumberSet().contains(new Integer(4)));
	assertTrue(gb.getNumberSet().contains(new Integer(5)));
}
 
Example 17
Source Project: spring-analysis-note   Source File: BeanWrapperGenericsTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testGenericMapElementWithCollectionValue() {
	GenericBean<?> gb = new GenericBean<>();
	gb.setCollectionMap(new HashMap<>());
	BeanWrapper bw = new BeanWrapperImpl(gb);
	bw.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
	HashSet<Integer> value1 = new HashSet<>();
	value1.add(new Integer(1));
	bw.setPropertyValue("collectionMap[1]", value1);
	assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
}
 
Example 18
Source Project: spring-analysis-note   Source File: BeanFactoryGenericsTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testGenericMapWithCollectionValueConstructor() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			registry.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
		}
	});
	RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);

	Map<String, AbstractCollection<?>> input = new HashMap<>();
	HashSet<Integer> value1 = new HashSet<>();
	value1.add(new Integer(1));
	input.put("1", value1);
	ArrayList<Boolean> value2 = new ArrayList<>();
	value2.add(Boolean.TRUE);
	input.put("2", value2);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(Boolean.TRUE);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(input);

	bf.registerBeanDefinition("genericBean", rbd);
	GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");

	assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
	assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList);
}
 
Example 19
Source Project: spring-analysis-note   Source File: BeanFactoryGenericsTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testGenericMapWithCollectionValueFactoryMethod() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			registry.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
		}
	});
	RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);
	rbd.setFactoryMethodName("createInstance");

	Map<String, AbstractCollection<?>> input = new HashMap<>();
	HashSet<Integer> value1 = new HashSet<>();
	value1.add(new Integer(1));
	input.put("1", value1);
	ArrayList<Boolean> value2 = new ArrayList<>();
	value2.add(Boolean.TRUE);
	input.put("2", value2);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(Boolean.TRUE);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(input);

	bf.registerBeanDefinition("genericBean", rbd);
	GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");

	assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
	assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList);
}
 
Example 20
Source Project: java-technology-stack   Source File: DataBinderTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testBindingWithCustomEditorOnObjectField() {
	BeanWithObjectProperty tb = new BeanWithObjectProperty();
	DataBinder binder = new DataBinder(tb);
	binder.registerCustomEditor(Integer.class, "object", new CustomNumberEditor(Integer.class, true));
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("object", "1");
	binder.bind(pvs);
	assertEquals(new Integer(1), tb.getObject());
}
 
Example 21
Source Project: java-technology-stack   Source File: BeanWrapperGenericsTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testGenericLowerBoundedSet() {
	GenericBean<?> gb = new GenericBean<>();
	BeanWrapper bw = new BeanWrapperImpl(gb);
	bw.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, true));
	Set<String> input = new HashSet<>();
	input.add("4");
	input.add("5");
	bw.setPropertyValue("numberSet", input);
	assertTrue(gb.getNumberSet().contains(new Integer(4)));
	assertTrue(gb.getNumberSet().contains(new Integer(5)));
}
 
Example 22
Source Project: java-technology-stack   Source File: BeanWrapperGenericsTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testGenericMapElementWithCollectionValue() {
	GenericBean<?> gb = new GenericBean<>();
	gb.setCollectionMap(new HashMap<>());
	BeanWrapper bw = new BeanWrapperImpl(gb);
	bw.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
	HashSet<Integer> value1 = new HashSet<>();
	value1.add(new Integer(1));
	bw.setPropertyValue("collectionMap[1]", value1);
	assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
}
 
Example 23
Source Project: java-technology-stack   Source File: BeanFactoryGenericsTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testGenericMapWithCollectionValueConstructor() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			registry.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
		}
	});
	RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);

	Map<String, AbstractCollection<?>> input = new HashMap<>();
	HashSet<Integer> value1 = new HashSet<>();
	value1.add(new Integer(1));
	input.put("1", value1);
	ArrayList<Boolean> value2 = new ArrayList<>();
	value2.add(Boolean.TRUE);
	input.put("2", value2);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(Boolean.TRUE);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(input);

	bf.registerBeanDefinition("genericBean", rbd);
	GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");

	assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
	assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList);
}
 
Example 24
Source Project: java-technology-stack   Source File: BeanFactoryGenericsTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testGenericMapWithCollectionValueFactoryMethod() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			registry.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
		}
	});
	RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);
	rbd.setFactoryMethodName("createInstance");

	Map<String, AbstractCollection<?>> input = new HashMap<>();
	HashSet<Integer> value1 = new HashSet<>();
	value1.add(new Integer(1));
	input.put("1", value1);
	ArrayList<Boolean> value2 = new ArrayList<>();
	value2.add(Boolean.TRUE);
	input.put("2", value2);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(Boolean.TRUE);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(input);

	bf.registerBeanDefinition("genericBean", rbd);
	GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");

	assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
	assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList);
}
 
Example 25
Source Project: spring4-understanding   Source File: DataBinderTests.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBindingWithCustomEditorOnObjectField() {
	BeanWithObjectProperty tb = new BeanWithObjectProperty();
	DataBinder binder = new DataBinder(tb);
	binder.registerCustomEditor(Integer.class, "object", new CustomNumberEditor(Integer.class, true));
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("object", "1");
	binder.bind(pvs);
	assertEquals(new Integer(1), tb.getObject());
}
 
Example 26
@Test
public void testGenericLowerBoundedSet() {
	GenericBean<?> gb = new GenericBean<Object>();
	BeanWrapper bw = new BeanWrapperImpl(gb);
	bw.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, true));
	Set<String> input = new HashSet<String>();
	input.add("4");
	input.add("5");
	bw.setPropertyValue("numberSet", input);
	assertTrue(gb.getNumberSet().contains(new Integer(4)));
	assertTrue(gb.getNumberSet().contains(new Integer(5)));
}
 
Example 27
@Test
public void testGenericMapElementWithCollectionValue() {
	GenericBean<?> gb = new GenericBean<Object>();
	gb.setCollectionMap(new HashMap<Number, Collection<? extends Object>>());
	BeanWrapper bw = new BeanWrapperImpl(gb);
	bw.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
	HashSet<Integer> value1 = new HashSet<Integer>();
	value1.add(new Integer(1));
	bw.setPropertyValue("collectionMap[1]", value1);
	assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
}
 
Example 28
@Test
public void testGenericMapWithCollectionValueConstructor() throws MalformedURLException {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			registry.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
		}
	});
	RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);

	Map<String, AbstractCollection<?>> input = new HashMap<String, AbstractCollection<?>>();
	HashSet<Integer> value1 = new HashSet<Integer>();
	value1.add(new Integer(1));
	input.put("1", value1);
	ArrayList<Boolean> value2 = new ArrayList<Boolean>();
	value2.add(Boolean.TRUE);
	input.put("2", value2);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(Boolean.TRUE);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(input);

	bf.registerBeanDefinition("genericBean", rbd);
	GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");

	assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
	assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList);
}
 
Example 29
@Test
public void testGenericMapWithCollectionValueFactoryMethod() throws MalformedURLException {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			registry.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
		}
	});
	RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class);
	rbd.setFactoryMethodName("createInstance");

	Map<String, AbstractCollection<?>> input = new HashMap<String, AbstractCollection<?>>();
	HashSet<Integer> value1 = new HashSet<Integer>();
	value1.add(new Integer(1));
	input.put("1", value1);
	ArrayList<Boolean> value2 = new ArrayList<Boolean>();
	value2.add(Boolean.TRUE);
	input.put("2", value2);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(Boolean.TRUE);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(input);

	bf.registerBeanDefinition("genericBean", rbd);
	GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean");

	assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
	assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList);
}
 
Example 30
Source Project: webcurator   Source File: MemberOfHandler.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @see org.webcurator.ui.util.TabHandler#initBinder(javax.servlet.http.HttpServletRequest, org.springframework.web.bind.ServletRequestDataBinder)
 */
@Override
public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
	super.initBinder(request, binder);

	// Register a number binder.
       NumberFormat nf = NumberFormat.getInstance(request.getLocale());
       binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, nf, true));
}