org.springframework.beans.propertyeditors.StringTrimmerEditor Java Examples

The following examples show how to use org.springframework.beans.propertyeditors.StringTrimmerEditor. 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   Author: Vip-Augus   File: CheckboxTagTests.java    License: MIT License 6 votes vote down vote up
@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());
}
 
Example #2
Source Project: spring-analysis-note   Author: Vip-Augus   File: AbstractPropertyAccessorTests.java    License: MIT License 6 votes vote down vote up
@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"));
}
 
Example #3
Source Project: java-technology-stack   Author: codeEngraver   File: CheckboxTagTests.java    License: MIT License 6 votes vote down vote up
@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());
}
 
Example #4
Source Project: java-technology-stack   Author: codeEngraver   File: AbstractPropertyAccessorTests.java    License: MIT License 6 votes vote down vote up
@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"));
}
 
Example #5
Source Project: spring4-understanding   Author: langtianya   File: CheckboxTagTests.java    License: Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #6
Source Project: spring4-understanding   Author: langtianya   File: AbstractPropertyAccessorTests.java    License: Apache License 2.0 6 votes vote down vote up
@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"));
}
 
Example #7
Source Project: dubbo-2.6.5   Author: tianheframe   File: ReferenceBeanBuilder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void preConfigureBean(Reference reference, ReferenceBean referenceBean) {
    Assert.notNull(interfaceClass, "The interface class must set first!");
    DataBinder dataBinder = new DataBinder(referenceBean);
    // Register CustomEditors for special fields
    dataBinder.registerCustomEditor(String.class, "filter", new StringTrimmerEditor(true));
    dataBinder.registerCustomEditor(String.class, "listener", new StringTrimmerEditor(true));
    dataBinder.registerCustomEditor(Map.class, "parameters", new PropertyEditorSupport() {

        public void setAsText(String text) throws java.lang.IllegalArgumentException {
            // Trim all whitespace
            String content = StringUtils.trimAllWhitespace(text);
            if (!StringUtils.hasText(content)) { // No content , ignore directly
                return;
            }
            // replace "=" to ","
            content = StringUtils.replace(content, "=", ",");
            // replace ":" to ","
            content = StringUtils.replace(content, ":", ",");
            // String[] to Map
            Map<String, String> parameters = CollectionUtils.toStringMap(commaDelimitedListToStringArray(content));
            setValue(parameters);
        }
    });

    // Bind annotation attributes
    dataBinder.bind(new AnnotationPropertyValuesAdapter(reference, applicationContext.getEnvironment(), IGNORE_FIELD_NAMES));

}
 
Example #8
Source Project: spring-analysis-note   Author: Vip-Augus   File: DataBinderTests.java    License: MIT License 5 votes vote down vote up
@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));
}
 
Example #9
Source Project: spring-analysis-note   Author: Vip-Augus   File: DataBinderTests.java    License: MIT License 5 votes vote down vote up
@Test  // SPR-14888
public void testSetAutoGrowCollectionLimitAfterInitialization() {
	DataBinder binder = new DataBinder(new BeanWithIntegerList());
	binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
	assertThatIllegalStateException().isThrownBy(() ->
			binder.setAutoGrowCollectionLimit(257))
		.withMessageContaining("DataBinder is already initialized - call setAutoGrowCollectionLimit before other configuration methods");
}
 
Example #10
Source Project: spring-analysis-note   Author: Vip-Augus   File: BeanWrapperGenericsTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testGenericListOfArraysWithElementConversion() throws MalformedURLException {
	GenericBean<String> gb = new GenericBean<>();
	ArrayList<String[]> list = new ArrayList<>();
	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]);
}
 
Example #11
Source Project: spring-analysis-note   Author: Vip-Augus   File: AbstractPropertyAccessorTests.java    License: MIT License 5 votes vote down vote up
@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]"));
}
 
Example #12
Source Project: java-technology-stack   Author: codeEngraver   File: DataBinderTests.java    License: MIT License 5 votes vote down vote up
@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));
}
 
Example #13
Source Project: java-technology-stack   Author: codeEngraver   File: DataBinderTests.java    License: MIT License 5 votes vote down vote up
@Test  // SPR-14888
public void testSetAutoGrowCollectionLimitAfterInitialization() {
	expectedException.expect(IllegalStateException.class);
	expectedException.expectMessage("DataBinder is already initialized - call setAutoGrowCollectionLimit before other configuration methods");

	DataBinder binder = new DataBinder(new BeanWithIntegerList());
	binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
	binder.setAutoGrowCollectionLimit(257);
}
 
Example #14
Source Project: java-technology-stack   Author: codeEngraver   File: BeanWrapperGenericsTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testGenericListOfArraysWithElementConversion() throws MalformedURLException {
	GenericBean<String> gb = new GenericBean<>();
	ArrayList<String[]> list = new ArrayList<>();
	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]);
}
 
Example #15
Source Project: java-technology-stack   Author: codeEngraver   File: AbstractPropertyAccessorTests.java    License: MIT License 5 votes vote down vote up
@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]"));
}
 
Example #16
Source Project: SpringBoot-Base-System   Author: jxnu-liguobin   File: BaseController.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 由InitBinder表示的方法,可以对WebDataBinder对象进行初始化。WebDataBinder是DataBinder的子类,
 * 用于完成由表单到JavaBean属性的绑定。 InitBinder方法不能有返回值,它必须名为void。
 * InitBinder方法的参数通常是WebDataBinder,@InitBinder可以对WebDataBinder进行初始化。
 * 
 * @time 2018年4月10日 下午5:12:31.</br>
 * @version V1.0</br>
 * @param webDataBinder</br>
 */
@InitBinder
protected void initBinder(WebDataBinder webDataBinder) {
	/**
	 * 一个用于trim 的 String类型的属性编辑器 如默认删除两边的空格,charsToDelete属性:可以设置为其他字符
	 * emptyAsNull属性:将一个空字符串转化为null值的选项。
	 */
	webDataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
	webDataBinder.registerCustomEditor(Date.class, new DateEditor(true));
}
 
Example #17
Source Project: spring-cloud-alibaba   Author: alibaba   File: DubboGenericServiceFactory.java    License: Apache License 2.0 5 votes vote down vote up
private void bindReferenceBean(ReferenceBean<GenericService> referenceBean,
		Map<String, Object> dubboTranslatedAttributes) {
	DataBinder dataBinder = new DataBinder(referenceBean);
	// Register CustomEditors for special fields
	dataBinder.registerCustomEditor(String.class, "filter",
			new StringTrimmerEditor(true));
	dataBinder.registerCustomEditor(String.class, "listener",
			new StringTrimmerEditor(true));
	dataBinder.registerCustomEditor(Map.class, "parameters",
			new PropertyEditorSupport() {

				@Override
				public void setAsText(String text)
						throws java.lang.IllegalArgumentException {
					// Trim all whitespace
					String content = StringUtils.trimAllWhitespace(text);
					if (!StringUtils.hasText(content)) { // No content , ignore
															// directly
						return;
					}
					// replace "=" to ","
					content = StringUtils.replace(content, "=", ",");
					// replace ":" to ","
					content = StringUtils.replace(content, ":", ",");
					// String[] to Map
					Map<String, String> parameters = CollectionUtils
							.toStringMap(commaDelimitedListToStringArray(content));
					setValue(parameters);
				}
			});

	// ignore "registries" field and then use RegistryConfig beans
	dataBinder.setDisallowedFields("registries");

	dataBinder.bind(new MutablePropertyValues(dubboTranslatedAttributes));

	registryConfigs.ifAvailable(referenceBean::setRegistries);
}
 
Example #18
/**
 * {@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));
}
 
Example #19
Source Project: spring-boot-doma2-sample   Author: miyabayt   File: HtmlControllerAdvice.java    License: Apache License 2.0 5 votes vote down vote up
@InitBinder
public void initBinder(WebDataBinder binder, HttpServletRequest request) {
    // 文字列フィールドが未入力の場合に、空文字ではなくNULLをバインドする
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));

    // 文字列をIDに変換する
    binder.registerCustomEditor(ID.class, new IDTypeEditor());

    // idカラムを入力値で上書きしない
    binder.setDisallowedFields("*.id");

    // versionカラムを入力値で上書きしない
    binder.setDisallowedFields("*.version");
}
 
Example #20
Source Project: spring4-understanding   Author: langtianya   File: DataBinderTests.java    License: Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #21
Source Project: spring4-understanding   Author: langtianya   File: BeanWrapperGenericsTests.java    License: Apache License 2.0 5 votes vote down vote up
@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]);
}
 
Example #22
Source Project: spring4-understanding   Author: langtianya   File: AbstractPropertyAccessorTests.java    License: Apache License 2.0 5 votes vote down vote up
@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]"));
}
 
Example #23
Source Project: springlets   Author: DISID   File: StringTrimmerAdvice.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #24
Source Project: cacheonix-core   Author: cacheonix   File: ClinicBindingInitializer.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
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));
}
 
Example #25
Source Project: podcastpedia-web   Author: PodcastpediaOrg   File: StartPageController.java    License: MIT License 5 votes vote down vote up
@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 */
}
 
Example #26
Source Project: podcastpedia-web   Author: PodcastpediaOrg   File: SearchController.java    License: MIT License 5 votes vote down vote up
@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 */		
}
 
Example #27
Source Project: spring-analysis-note   Author: Vip-Augus   File: MethodInvokingFactoryBeanTests.java    License: MIT License 4 votes vote down vote up
@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();
	assertSame(objType, 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 ArrayList<>(), 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("1", new Object());
	try {
		mcfb.afterPropertiesSet();
		fail("Should have thrown NoSuchMethodException");
	}
	catch (NoSuchMethodException ex) {
		// expected
	}
}
 
Example #28
Source Project: spring-analysis-note   Author: Vip-Augus   File: AbstractPropertyAccessorTests.java    License: MIT License 4 votes vote down vote up
@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);
}
 
Example #29
Source Project: java-technology-stack   Author: codeEngraver   File: MethodInvokingFactoryBeanTests.java    License: MIT License 4 votes vote down vote up
@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();
	assertSame(objType, 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 ArrayList<>(), 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("1", new Object());
	try {
		mcfb.afterPropertiesSet();
		fail("Should have thrown NoSuchMethodException");
	}
	catch (NoSuchMethodException ex) {
		// expected
	}
}
 
Example #30
Source Project: java-technology-stack   Author: codeEngraver   File: AbstractPropertyAccessorTests.java    License: MIT License 4 votes vote down vote up
@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);
}