@Test(expected = IllegalArgumentException.class) public void testCtorWithNullCollectionType() throws Exception { new CustomCollectionEditor(null); }
@Test(expected = IllegalArgumentException.class) @SuppressWarnings("unchecked") public void testCtorWithNonCollectionType() throws Exception { new CustomCollectionEditor((Class) String.class); }
this.defaultEditors.put(Collection.class, new CustomCollectionEditor(Collection.class)); this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class)); this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class)); this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class)); this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class));
@Test(expected = IllegalArgumentException.class) public void testWithCollectionTypeThatDoesNotExposeAPublicNoArgCtor() throws Exception { CustomCollectionEditor editor = new CustomCollectionEditor(CollectionTypeWithNoNoArgCtor.class); editor.setValue("1"); }
@Test public void testSunnyDaySetValue() throws Exception { CustomCollectionEditor editor = new CustomCollectionEditor(ArrayList.class); editor.setValue(new int[] {0, 1, 2}); Object value = editor.getValue(); assertNotNull(value); assertTrue(value instanceof ArrayList); List<?> list = (List<?>) value; assertEquals("There must be 3 elements in the converted collection", 3, list.size()); assertEquals(new Integer(0), list.get(0)); assertEquals(new Integer(1), list.get(1)); assertEquals(new Integer(2), list.get(2)); }
@Test public void testWhenTargetTypeIsExactlyTheCollectionInterfaceUsesFallbackCollectionType() throws Exception { CustomCollectionEditor editor = new CustomCollectionEditor(Collection.class); editor.setValue("0, 1, 2"); Collection<?> value = (Collection<?>) editor.getValue(); assertNotNull(value); assertEquals("There must be 1 element in the converted collection", 1, value.size()); assertEquals("0, 1, 2", value.iterator().next()); }
this.defaultEditors.put(Collection.class, new CustomCollectionEditor(Collection.class)); this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class)); this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class)); this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class)); this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class));
@Test public void testSunnyDaySetAsTextYieldsSingleValue() throws Exception { CustomCollectionEditor editor = new CustomCollectionEditor(ArrayList.class); editor.setValue("0, 1, 2"); Object value = editor.getValue(); assertNotNull(value); assertTrue(value instanceof ArrayList); List<?> list = (List<?>) value; assertEquals("There must be 1 element in the converted collection", 1, list.size()); assertEquals("0, 1, 2", list.get(0)); }
@Test public void testConversionToOldCollections() throws PropertyVetoException { OldCollectionsBean tb = new OldCollectionsBean(); BeanWrapper bw = new BeanWrapperImpl(tb); bw.registerCustomEditor(Vector.class, new CustomCollectionEditor(Vector.class)); bw.registerCustomEditor(Hashtable.class, new CustomMapEditor(Hashtable.class)); bw.setPropertyValue("vector", new String[] {"a", "b"}); assertEquals(2, tb.getVector().size()); assertEquals("a", tb.getVector().get(0)); assertEquals("b", tb.getVector().get(1)); bw.setPropertyValue("hashtable", Collections.singletonMap("foo", "bar")); assertEquals(1, tb.getHashtable().size()); assertEquals("bar", tb.getHashtable().get("foo")); }
@Test public void testBindingNullToEmptyCollection() { IndexedTestBean tb = new IndexedTestBean(); DataBinder binder = new DataBinder(tb, "tb"); binder.registerCustomEditor(Set.class, new CustomCollectionEditor(TreeSet.class, true)); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("set", null); binder.bind(pvs); assertTrue(tb.getSet() instanceof TreeSet); assertTrue(tb.getSet().isEmpty()); }
@Test public void testBindingStringArrayToIntegerSet() { IndexedTestBean tb = new IndexedTestBean(); DataBinder binder = new DataBinder(tb, "tb"); binder.registerCustomEditor(Set.class, new CustomCollectionEditor(TreeSet.class) { @Override protected Object convertElement(Object element) { return new Integer(element.toString()); } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("set", new String[] {"10", "20", "30"}); binder.bind(pvs); assertEquals(tb.getSet(), binder.getBindingResult().getFieldValue("set")); assertTrue(tb.getSet() instanceof TreeSet); assertEquals(3, tb.getSet().size()); assertTrue(tb.getSet().contains(new Integer(10))); assertTrue(tb.getSet().contains(new Integer(20))); assertTrue(tb.getSet().contains(new Integer(30))); pvs = new MutablePropertyValues(); pvs.add("set", null); binder.bind(pvs); assertNull(tb.getSet()); }
this.defaultEditors.put(Collection.class, new CustomCollectionEditor(Collection.class)); this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class)); this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class)); this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class)); this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class));
public class InvoiceController extends SimpleFormController { protected void initBinder(HttpServletRequest request, ServletRequetDataBinder binder) throws Exception { binder.registerCustomEditor(List.class, "lineItems", new CustomCollectionEditor(List.class)() { protected Object convertElement(Object lineItem) { LineItem li = (LineItem) lineItem; // StringUtils is a jakarta Commons lang static class return (StringUtils.isBlank(li.getProductName())) ? null : li; } }); } }
@InitBinder protected void initBinder(WebDataBinder binder) throws Exception{ binder.registerCustomEditor(Set.class,"categoryList", new CustomCollectionEditor(Set.class){ protected Object convertElement(Object element){ if (element instanceof String) { Category category = categoryCache.get(Integer.parseInt(element.toString())); return role; } return null; } }); }
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { binder.registerCustomEditor( Set.class, "[nameOfTheClassMemberItShouldGoto]", new CustomCollectionEditor(Set.class) { protected Object convertElement(Object element) { return (element == null ? null : getQueryService().getJoker(element.toString())); } } ); }
@InitBinder protected void initBinder(WebDataBinder binder) { binder.registerCustomEditor(List.class, "authors ", new CustomCollectionEditor(List.class) { @Override protected Object convertElement(Object element) { Long id = null; if(element instanceof Long) { //From the database 'element' will be a Long id = (Long) element; } return id != null ? authorService.loadAuthorById(id) : null; } }); }
@InitBinder("userForm") private void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Set.class, "computers", new CustomCollectionEditor(Set.class) { @Override protected Object convertElement(Object element) { String pcName = null; Set<Computer> computerSet = new LinkedHashSet<>(); if (element instanceof String && !((String) element).equals("")) { pcName = (String) element; } return pcName != null ? computerService.getComputerByName(pcName) : null; } }); }
@RequestMapping(method = RequestMethod.POST) public String add(@ModelAttribute("user") User user, BindingResult result) { validator.validate(user, result); if (result.hasErrors()) return "/user"; userDao.update(user); return "redirect:/users"; } @InitBinder public void initBinder(ServletRequestDataBinder binder) { binder.registerCustomEditor(List.class, "roles", new CustomCollectionEditor(List.class) { protected Object convertElement(Object element) { if (element != null) { Integer roleId = Integer.parseInt(element.toString()); Role role = (Role) roleDao.getById(roleId); return role; } return null; } }); }
@InitBinder protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { // super.initBinder(request, binder); binder.registerCustomEditor(List.class, "roles",new CustomCollectionEditor(List.class){ @Override protected Object convertElement(Object element) { Role role = new Role(); if (element != null && (element.toString().equals("1") || element.toString().equals("2") || element.toString().equals("3") || element.toString().equals("4"))) { role.setId(Integer.parseInt(element.toString())); } return role; } }); }
/** * Register the property editors provided by uimaFIT in the given property editor registry. * * @param aRegistry a property editor registry */ public static void registerUimaFITEditors(PropertyEditorRegistry aRegistry) { aRegistry.registerCustomEditor(Locale.class, new LocaleEditor()); aRegistry.registerCustomEditor(String.class, new GetAsTextStringEditor(aRegistry)); aRegistry.registerCustomEditor(LinkedList.class, new CustomCollectionEditor(LinkedList.class)); } }