@Test public void withSingleValueAndEditor() throws Exception { this.bean.setName("Rob Harrop"); this.tag.setPath("name"); this.tag.setValue(" Rob Harrop"); BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME); bindingResult.getPropertyEditorRegistry().registerCustomEditor(String.class, new StringTrimmerEditor(false)); getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); // wrap the output so it is valid XML output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element checkboxElement = (Element) document.getRootElement().elements().get(0); assertEquals("input", checkboxElement.getName()); assertEquals("checkbox", checkboxElement.attribute("type").getValue()); assertEquals("name", checkboxElement.attribute("name").getValue()); assertEquals("checked", checkboxElement.attribute("checked").getValue()); assertEquals(" Rob Harrop", checkboxElement.attribute("value").getValue()); }
@Test public void setCollectionPropertyWithStringValueAndCustomEditor() { IndexedTestBean target = new IndexedTestBean(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.registerCustomEditor(String.class, "set", new StringTrimmerEditor(false)); accessor.registerCustomEditor(String.class, "list", new StringTrimmerEditor(false)); accessor.setPropertyValue("set", "set1 "); accessor.setPropertyValue("sortedSet", "sortedSet1"); accessor.setPropertyValue("list", "list1 "); assertEquals(1, target.getSet().size()); assertTrue(target.getSet().contains("set1")); assertEquals(1, target.getSortedSet().size()); assertTrue(target.getSortedSet().contains("sortedSet1")); assertEquals(1, target.getList().size()); assertTrue(target.getList().contains("list1")); accessor.setPropertyValue("list", Collections.singletonList("list1 ")); assertTrue(target.getList().contains("list1")); }
public void testWithSingleValueAndEditor() throws Exception { this.bean.setName("Rob Harrop"); this.tag.setPath("name"); this.tag.setValue(" Rob Harrop"); BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME); bindingResult.getPropertyEditorRegistry().registerCustomEditor(String.class, new StringTrimmerEditor(false)); getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); // wrap the output so it is valid XML output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element checkboxElement = (Element) document.getRootElement().elements().get(0); assertEquals("input", checkboxElement.getName()); assertEquals("checkbox", checkboxElement.attribute("type").getValue()); assertEquals("name", checkboxElement.attribute("name").getValue()); assertEquals("checked", checkboxElement.attribute("checked").getValue()); assertEquals(" Rob Harrop", checkboxElement.attribute("value").getValue()); }
@Test public void testCollectionsWithStringValuesAndCustomEditor() { IndexedTestBean tb = new IndexedTestBean(); BeanWrapper bw = new BeanWrapperImpl(tb); bw.registerCustomEditor(String.class, "set", new StringTrimmerEditor(false)); bw.registerCustomEditor(String.class, "list", new StringTrimmerEditor(false)); bw.setPropertyValue("set", "set1 "); bw.setPropertyValue("sortedSet", "sortedSet1"); bw.setPropertyValue("list", "list1 "); assertEquals(1, tb.getSet().size()); assertTrue(tb.getSet().contains("set1")); assertEquals(1, tb.getSortedSet().size()); assertTrue(tb.getSortedSet().contains("sortedSet1")); assertEquals(1, tb.getList().size()); assertTrue(tb.getList().contains("list1")); bw.setPropertyValue("list", Arrays.asList(new String[] {"list1 "})); assertTrue(tb.getList().contains("list1")); }
/** * {@inheritDoc} * Sets the require fields and the disallowed fields from the * HttpServletRequest. * * @see org.springframework.web.servlet.mvc.BaseCommandController#initBinder(javax.servlet.http.HttpServletRequest, * org.springframework.web.bind.ServletRequestDataBinder) */ @Override protected void initBinder(final HttpServletRequest request, final ServletRequestDataBinder binder) throws Exception { binder.setRequiredFields(new String[] {"description", "serviceId", "name", "allowedToProxy", "enabled", "ssoEnabled", "anonymousAccess", "evaluationOrder"}); binder.setDisallowedFields(new String[] {"id"}); binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); }
@InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); }
@InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); binder.registerCustomEditor( Object.class, new ObjectTypeEditorHelper()); }
@InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); Object target = binder.getTarget(); if (target != null) { binder.replaceValidators(supportedValidatorsFor(target.getClass())); } }
@Test public void testConversionWithInappropriateStringEditor() { DataBinder dataBinder = new DataBinder(null); DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(); dataBinder.setConversionService(conversionService); dataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); NameBean bean = new NameBean("Fred"); assertEquals("ConversionService should have invoked toString()", "Fred", dataBinder.convertIfNecessary(bean, String.class)); conversionService.addConverter(new NameBeanConverter()); assertEquals("Type converter should have been used", "[Fred]", dataBinder.convertIfNecessary(bean, String.class)); }
@Test public void testGenericListOfArraysWithElementConversion() throws MalformedURLException { GenericBean<String> gb = new GenericBean<String>(); ArrayList<String[]> list = new ArrayList<String[]>(); list.add(new String[] {"str1", "str2"}); gb.setListOfArrays(list); BeanWrapper bw = new BeanWrapperImpl(gb); bw.registerCustomEditor(String.class, new StringTrimmerEditor(false)); bw.setPropertyValue("listOfArrays[0][1]", "str3 "); assertEquals("str3", bw.getPropertyValue("listOfArrays[0][1]")); assertEquals("str3", gb.getListOfArrays().get(0)[1]); }
@Test public void propertyTypeIndexedProperty() { IndexedTestBean target = new IndexedTestBean(); AbstractPropertyAccessor accessor = createAccessor(target); assertEquals(null, accessor.getPropertyType("map[key0]")); accessor = createAccessor(target); accessor.setPropertyValue("map[key0]", "my String"); assertEquals(String.class, accessor.getPropertyType("map[key0]")); accessor = createAccessor(target); accessor.registerCustomEditor(String.class, "map[key0]", new StringTrimmerEditor(false)); assertEquals(String.class, accessor.getPropertyType("map[key0]")); }
public void initBinder(WebDataBinder binder, WebRequest request) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false)); binder.registerCustomEditor(String.class, new StringTrimmerEditor(false)); }
/** * Registers the {@link StringTrimmerEditor} * * @param webDataBinder */ @InitBinder public void initBinder(WebDataBinder webDataBinder) { StringTrimmerEditor trimmer = new StringTrimmerEditor(this.getCharsToDelete(), this.isEmptyAsNull()); webDataBinder.registerCustomEditor(String.class, trimmer); }
public void initBinder(WebDataBinder binder, WebRequest request) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false)); binder.registerCustomEditor(String.class, new StringTrimmerEditor(false)); binder.registerCustomEditor(PetType.class, new PetTypeEditor(this.clinic)); }
@InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(MediaType.class, new MediaTypeEditor( MediaType.class)); binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));/* Converts empty strings into null when a form is submitted */ }
@Test public void testTypeDeterminationForIndexedProperty() { BeanWrapper bw = new BeanWrapperImpl(IndexedTestBean.class); assertEquals(null, bw.getPropertyType("map[key0]")); bw = new BeanWrapperImpl(IndexedTestBean.class); bw.setPropertyValue("map[key0]", "my String"); assertEquals(String.class, bw.getPropertyType("map[key0]")); bw = new BeanWrapperImpl(IndexedTestBean.class); bw.registerCustomEditor(String.class, "map[key0]", new StringTrimmerEditor(false)); assertEquals(String.class, bw.getPropertyType("map[key0]")); }
@Test public void initBinderShouldRegisterStringTrimmer() { // Arrange AbstractController target = new AbstractController() { }; WebDataBinder binder = mock(WebDataBinder.class); // Act target.initBinder(binder); // Assert verify(binder).registerCustomEditor(eq(String.class), any(StringTrimmerEditor.class)); }
@Override public void initBinder(WebDataBinder binder, WebRequest request) { super.initBinder(binder, request); binder.registerCustomEditor(KualiDecimal.class, new UifCurrencyEditor()); binder.registerCustomEditor(KualiInteger.class, new UifKualiIntegerCurrencyEditor()); binder.registerCustomEditor(KualiPercent.class, new UifPercentageEditor()); binder.registerCustomEditor(java.sql.Date.class, new UifDateEditor()); binder.registerCustomEditor(java.util.Date.class, new UifDateEditor()); binder.registerCustomEditor(Timestamp.class, new UifTimestampEditor()); // TODO do we need this since we are switching to spring tags binder.registerCustomEditor(boolean.class, new UifBooleanEditor()); binder.registerCustomEditor(Boolean.class, new UifBooleanEditor()); binder.registerCustomEditor(Boolean.TYPE, new UifBooleanEditor()); // Use the spring custom number editor for Big decimals DecimalFormat bigIntFormatter = new DecimalFormat(); bigIntFormatter.setMaximumFractionDigits(340); binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, bigIntFormatter, true)); binder.registerCustomEditor(AbstractKualiDecimal.class, new CustomNumberEditor(AbstractKualiDecimal.class, bigIntFormatter, true)); // Use the spring StringTrimmerEditor editor for Strings binder.registerCustomEditor(String.class, new StringTrimmerEditor(false)); // Use the StringArrayPropertyEditor for string arrays with "," as the // separator binder.registerCustomEditor(String[].class, new StringArrayPropertyEditor(",", false)); binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor()); }
@InitBinder public void initBinder(WebDataBinder dataBinder) { dataBinder.setDisallowedFields(ID_KEY); dataBinder.setRequiredFields(NAME_KEY, START_DATE_KEY, END_DATE_KEY, EMAIL_KEY, COST_PER_LEAD_KEY, TOTAL_BUDGET_KEY, STATUS_KEY, RESPONSE_TYPE_KEY, SUCCESS_RESPONSE_KEY, FAILURE_RESPONSE_KEY, ADVERTISER_KEY); dataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(DATE_FORMAT, false)); dataBinder.registerCustomEditor(CampaignStatus.class, statusEditor); dataBinder.registerCustomEditor(CampaignResponseType.class, responseTypeEditor); dataBinder.registerCustomEditor(CampaignSupportedField.class, supportedFieldEditor); dataBinder.registerCustomEditor(CampaignFieldType.class, fieldTypeEditor); dataBinder.registerCustomEditor(CampaignFieldValidationType.class, validationTypeEditor); dataBinder.registerCustomEditor(Advertiser.class, advertiserEditor); }
@InitBinder public void initBinder(final WebDataBinder binder) { binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); }
private DataBinder initBinder() { final DataBinder binder = new DataBinder(new EncryptMessageCommand(), "command"); binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); binder.setValidator(validator); return binder; }
@InitBinder public void initBinder(WebDataBinder dataBinder){ StringTrimmerEditor stringTrimmerEditor = new StringTrimmerEditor(true); dataBinder.registerCustomEditor(String.class, stringTrimmerEditor); }
@InitBinder protected void initBinder(WebDataBinder webDataBinder) { webDataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); webDataBinder.registerCustomEditor(Date.class, new DateEditor(true)); }
@Test public void testGetObjectType() throws Exception { TestClass1 tc1 = new TestClass1(); MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean(); mcfb = new MethodInvokingFactoryBean(); mcfb.setTargetObject(tc1); mcfb.setTargetMethod("method1"); mcfb.afterPropertiesSet(); assertTrue(int.class.equals(mcfb.getObjectType())); mcfb = new MethodInvokingFactoryBean(); mcfb.setTargetClass(TestClass1.class); mcfb.setTargetMethod("voidRetvalMethod"); mcfb.afterPropertiesSet(); Class<?> objType = mcfb.getObjectType(); assertTrue(objType.equals(void.class)); // verify that we can call a method with args that are subtypes of the // target method arg types TestClass1._staticField1 = 0; mcfb = new MethodInvokingFactoryBean(); mcfb.setTargetClass(TestClass1.class); mcfb.setTargetMethod("supertypes"); mcfb.setArguments(new Object[] {new ArrayList<Object>(), new ArrayList<Object>(), "hello"}); mcfb.afterPropertiesSet(); mcfb.getObjectType(); // fail on improper argument types at afterPropertiesSet mcfb = new MethodInvokingFactoryBean(); mcfb.registerCustomEditor(String.class, new StringTrimmerEditor(false)); mcfb.setTargetClass(TestClass1.class); mcfb.setTargetMethod("supertypes"); mcfb.setArguments(new Object[] {"1", new Object()}); try { mcfb.afterPropertiesSet(); fail("Should have thrown NoSuchMethodException"); } catch (NoSuchMethodException ex) { // expected } }
@Test public void setPrimitiveArrayPropertyLargeMatching() { Assume.group(TestGroup.PERFORMANCE); Assume.notLogging(LogFactory.getLog(AbstractPropertyAccessorTests.class)); PrimitiveArrayBean target = new PrimitiveArrayBean(); AbstractPropertyAccessor accessor = createAccessor(target); int[] input = new int[1024]; StopWatch sw = new StopWatch(); sw.start("array1"); for (int i = 0; i < 1000; i++) { accessor.setPropertyValue("array", input); } sw.stop(); assertEquals(1024, target.getArray().length); assertEquals(0, target.getArray()[0]); long time1 = sw.getLastTaskTimeMillis(); assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100); accessor.registerCustomEditor(String.class, new StringTrimmerEditor(false)); sw.start("array2"); for (int i = 0; i < 1000; i++) { accessor.setPropertyValue("array", input); } sw.stop(); 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++) { accessor.setPropertyValue("array", input); } sw.stop(); 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++) { accessor.setPropertyValue("array", input); } sw.stop(); 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++) { accessor.setPropertyValue("array", input); } sw.stop(); assertEquals(1024, target.getArray().length); assertEquals(0, target.getArray()[0]); assertTrue("Took too long", sw.getLastTaskTimeMillis() > time1); }
@InitBinder private void initBinder(ServletRequestDataBinder binder){ binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); }
@InitBinder private void initBinder(ServletRequestDataBinder binder) { // bind empty strings as null binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); }
@InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(String.class, new StringTrimmerEditor(false)); }
@InitBinder public void initBinder(WebDataBinder binder, Locale locale) { binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("MM/yyyy"), true)); }
@InitBinder public void initBinder(WebDataBinder binder, Locale locale) { binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); }