@Override public void registerCustomEditors(PropertyEditorRegistry registry) { registry.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false)); } });
@Override public void registerCustomEditors(PropertyEditorRegistry registry) { NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN); registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true)); } });
@Override public void registerCustomEditors(PropertyEditorRegistry registry) { registry.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false)); } });
@Override public void registerCustomEditors(PropertyEditorRegistry registry) { NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN); registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true)); } });
@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); }
@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); }
@Test public void testParseShortGreaterThanMaxValueWithoutNumberFormat() { try { CustomNumberEditor editor = new CustomNumberEditor(Short.class, true); editor.setAsText(String.valueOf(Short.MAX_VALUE + 1)); fail(Short.MAX_VALUE + 1 + " is greater than max value"); } catch (NumberFormatException ex) { // expected } }
@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))); }
@Override protected void initBinder(DataBinder binder) { binder.registerCustomEditor(Double.TYPE, "value", new CustomNumberEditor(Double.class, NumberFormat .getNumberInstance(Locale.GERMAN), true)); } };
@Test public void testCustomNumberEditorWithHex() { CustomNumberEditor editor = new CustomNumberEditor(Integer.class, false); editor.setAsText("0x" + Integer.toHexString(64)); assertEquals(new Integer(64), editor.getValue()); }
@Test public void testCustomNumberEditorWithFrenchBigDecimal() throws Exception { NumberFormat nf = NumberFormat.getNumberInstance(Locale.FRENCH); NumberTestBean tb = new NumberTestBean(); BeanWrapper bw = new BeanWrapperImpl(tb); bw.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, nf, true)); bw.setPropertyValue("bigDecimal", "1000"); assertEquals(1000.0f, tb.getBigDecimal().floatValue(), 0f); bw.setPropertyValue("bigDecimal", "1000,5"); assertEquals(1000.5f, tb.getBigDecimal().floatValue(), 0f); bw.setPropertyValue("bigDecimal", "1 000,5"); assertEquals(1000.5f, tb.getBigDecimal().floatValue(), 0f); }
@Test(expected = IllegalArgumentException.class) public void testSetCustomEditorsWithInvalidTypeName() throws Exception { DefaultPropertyEditorRegistrar mapper = new DefaultPropertyEditorRegistrar(); mapper.setCustomEditors(Collections.singletonMap("FOO", new CustomNumberEditor(Long.class, true))); }
NumberTestBean tb = new NumberTestBean(); BeanWrapper bw = new BeanWrapperImpl(tb); bw.registerCustomEditor(short.class, new CustomNumberEditor(Short.class, nf, false)); bw.registerCustomEditor(Short.class, new CustomNumberEditor(Short.class, nf, false)); bw.registerCustomEditor(int.class, new CustomNumberEditor(Integer.class, nf, false)); bw.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, nf, false)); bw.registerCustomEditor(long.class, new CustomNumberEditor(Long.class, nf, false)); bw.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, nf, false)); bw.registerCustomEditor(BigInteger.class, new CustomNumberEditor(BigInteger.class, nf, false)); bw.registerCustomEditor(float.class, new CustomNumberEditor(Float.class, nf, false)); bw.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, false)); bw.registerCustomEditor(double.class, new CustomNumberEditor(Double.class, nf, false)); bw.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, nf, false)); bw.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, nf, false));
@Test public void testCustomNumberEditorWithAllowEmpty() { NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN); NumberTestBean tb = new NumberTestBean(); BeanWrapper bw = new BeanWrapperImpl(tb); bw.registerCustomEditor(long.class, new CustomNumberEditor(Long.class, nf, true)); bw.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, nf, true)); bw.setPropertyValue("long1", "5"); bw.setPropertyValue("long2", "6"); assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1"))); assertTrue("Correct long1 value", tb.getLong1() == 5); assertTrue("Correct long2 value", new Long("6").equals(bw.getPropertyValue("long2"))); assertTrue("Correct long2 value", new Long("6").equals(tb.getLong2())); bw.setPropertyValue("long2", ""); assertTrue("Correct long2 value", bw.getPropertyValue("long2") == null); assertTrue("Correct long2 value", tb.getLong2() == null); try { bw.setPropertyValue("long1", ""); fail("Should have thrown BeansException"); } catch (BeansException ex) { // expected assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1"))); assertTrue("Correct long1 value", tb.getLong1() == 5); } }
@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()); }
@Test public void testCustomNumberEditor() { CustomNumberEditor editor = new CustomNumberEditor(Integer.class, false); editor.setAsText("5"); assertEquals(new Integer(5), editor.getValue()); assertEquals("5", editor.getAsText()); editor.setValue(null); assertEquals(null, editor.getValue()); assertEquals("", editor.getAsText()); }
@Test public void testUninitializedArrayPropertyWithCustomEditor() { IndexedTestBean bean = new IndexedTestBean(false); BeanWrapper bw = new BeanWrapperImpl(bean); PropertyEditor pe = new CustomNumberEditor(Integer.class, true); bw.registerCustomEditor(null, "list.age", pe); TestBean tb = new TestBean(); bw.setPropertyValue("list", new ArrayList<>()); bw.setPropertyValue("list[0]", tb); assertEquals(tb, bean.getList().get(0)); assertEquals(pe, bw.findCustomEditor(int.class, "list.age")); assertEquals(pe, bw.findCustomEditor(null, "list.age")); assertEquals(pe, bw.findCustomEditor(int.class, "list[0].age")); assertEquals(pe, bw.findCustomEditor(null, "list[0].age")); }
@Test public void testPaddedLongWithDefaultAndCustomEditor() throws Exception { BeanWrapperFieldSetMapper<TestObject> mapper = new BeanWrapperFieldSetMapper<>(); mapper.setTargetType(TestObject.class); FieldSet fieldSet = new DefaultFieldSet(new String[] { "00009", "78" }, new String[] { "varLong", "varInt" }); mapper.setCustomEditors(Collections.singletonMap(Long.TYPE, new CustomNumberEditor(Long.class, NumberFormat .getNumberInstance(), true))); TestObject bean = mapper.mapFieldSet(fieldSet); assertEquals(9, bean.getVarLong()); assertEquals(78, bean.getVarInt()); }
@Test public void testCustomNumberEditorWithEmptyAsNull() { CustomNumberEditor editor = new CustomNumberEditor(Integer.class, true); editor.setAsText("5"); assertEquals(new Integer(5), editor.getValue()); assertEquals("5", editor.getAsText()); editor.setAsText(""); assertEquals(null, editor.getValue()); assertEquals("", editor.getAsText()); editor.setValue(null); assertEquals(null, editor.getValue()); assertEquals("", editor.getAsText()); }
assertTrue("Took too long", sw.getLastTaskTimeMillis() < 125); accessor.registerCustomEditor(int.class, "array.somePath", new CustomNumberEditor(Integer.class, false)); sw.start("array3"); for (int i = 0; i < 1000; i++) { assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100); accessor.registerCustomEditor(int.class, "array[0].somePath", new CustomNumberEditor(Integer.class, false)); sw.start("array3"); for (int i = 0; i < 1000; i++) { assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100); accessor.registerCustomEditor(int.class, new CustomNumberEditor(Integer.class, false)); sw.start("array4"); for (int i = 0; i < 100; i++) {