Search in sources :

Example 31 with PropertyEditor

use of java.beans.PropertyEditor in project spring-framework by spring-projects.

the class URLEditorTests method testStandardURI.

@Test
public void testStandardURI() throws Exception {
    PropertyEditor urlEditor = new URLEditor();
    urlEditor.setAsText("mailto:juergen.hoeller@interface21.com");
    Object value = urlEditor.getValue();
    assertTrue(value instanceof URL);
    URL url = (URL) value;
    assertEquals(url.toExternalForm(), urlEditor.getAsText());
}
Also used : PropertyEditor(java.beans.PropertyEditor) URL(java.net.URL) Test(org.junit.Test)

Example 32 with PropertyEditor

use of java.beans.PropertyEditor in project spring-framework by spring-projects.

the class URLEditorTests method testWithNonExistentResource.

@Test(expected = IllegalArgumentException.class)
public void testWithNonExistentResource() throws Exception {
    PropertyEditor urlEditor = new URLEditor();
    urlEditor.setAsText("gonna:/freak/in/the/morning/freak/in/the.evening");
}
Also used : PropertyEditor(java.beans.PropertyEditor) Test(org.junit.Test)

Example 33 with PropertyEditor

use of java.beans.PropertyEditor in project spring-framework by spring-projects.

the class AbstractPropertyBindingResult method getCustomEditor.

/**
	 * Retrieve the custom PropertyEditor for the given field, if any.
	 * @param fixedField the fully qualified field name
	 * @return the custom PropertyEditor, or {@code null}
	 */
protected PropertyEditor getCustomEditor(String fixedField) {
    Class<?> targetType = getPropertyAccessor().getPropertyType(fixedField);
    PropertyEditor editor = getPropertyAccessor().findCustomEditor(targetType, fixedField);
    if (editor == null) {
        editor = BeanUtils.findEditorByConvention(targetType);
    }
    return editor;
}
Also used : PropertyEditor(java.beans.PropertyEditor)

Example 34 with PropertyEditor

use of java.beans.PropertyEditor in project spring-framework by spring-projects.

the class AbstractPropertyBindingResult method findEditor.

/**
	 * This implementation exposes a PropertyEditor adapter for a Formatter,
	 * if applicable.
	 */
@Override
public PropertyEditor findEditor(String field, Class<?> valueType) {
    Class<?> valueTypeForLookup = valueType;
    if (valueTypeForLookup == null) {
        valueTypeForLookup = getFieldType(field);
    }
    PropertyEditor editor = super.findEditor(field, valueTypeForLookup);
    if (editor == null && this.conversionService != null) {
        TypeDescriptor td = null;
        if (field != null) {
            TypeDescriptor ptd = getPropertyAccessor().getPropertyTypeDescriptor(fixedField(field));
            if (valueType == null || valueType.isAssignableFrom(ptd.getType())) {
                td = ptd;
            }
        }
        if (td == null) {
            td = TypeDescriptor.valueOf(valueTypeForLookup);
        }
        if (this.conversionService.canConvert(TypeDescriptor.valueOf(String.class), td)) {
            editor = new ConvertingPropertyEditorAdapter(this.conversionService, td);
        }
    }
    return editor;
}
Also used : ConvertingPropertyEditorAdapter(org.springframework.core.convert.support.ConvertingPropertyEditorAdapter) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) PropertyEditor(java.beans.PropertyEditor)

Example 35 with PropertyEditor

use of java.beans.PropertyEditor in project spring-framework by spring-projects.

the class ResourceEditorTests method sunnyDay.

@Test
public void sunnyDay() throws Exception {
    PropertyEditor editor = new ResourceEditor();
    editor.setAsText("classpath:org/springframework/core/io/ResourceEditorTests.class");
    Resource resource = (Resource) editor.getValue();
    assertNotNull(resource);
    assertTrue(resource.exists());
}
Also used : PropertyEditor(java.beans.PropertyEditor) Test(org.junit.Test)

Aggregations

PropertyEditor (java.beans.PropertyEditor)98 Test (org.junit.Test)59 TestBean (org.springframework.tests.sample.beans.TestBean)10 File (java.io.File)9 URI (java.net.URI)8 ITestBean (org.springframework.tests.sample.beans.ITestBean)6 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)6 Path (java.nio.file.Path)5 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 StringArrayPropertyEditor (org.springframework.beans.propertyeditors.StringArrayPropertyEditor)4 IOException (java.io.IOException)3 URL (java.net.URL)3 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)3 Resource (org.springframework.core.io.Resource)3 BooleanTestBean (org.springframework.tests.sample.beans.BooleanTestBean)3 NumberTestBean (org.springframework.tests.sample.beans.NumberTestBean)3 BindStatus (org.springframework.web.servlet.support.BindStatus)3 StringReader (java.io.StringReader)2