Search in sources :

Example 36 with PropertyEditor

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

the class ResourceEditorTests method testStrictSystemPropertyReplacement.

@Test(expected = IllegalArgumentException.class)
public void testStrictSystemPropertyReplacement() {
    PropertyEditor editor = new ResourceEditor(new DefaultResourceLoader(), new StandardEnvironment(), false);
    System.setProperty("test.prop", "foo");
    try {
        editor.setAsText("${test.prop}-${bar}");
        Resource resolved = (Resource) editor.getValue();
        assertEquals("foo-${bar}", resolved.getFilename());
    } finally {
        System.getProperties().remove("test.prop");
    }
}
Also used : PropertyEditor(java.beans.PropertyEditor) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Test(org.junit.Test)

Example 37 with PropertyEditor

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

the class ResourceEditorTests method testSystemPropertyReplacement.

@Test
public void testSystemPropertyReplacement() {
    PropertyEditor editor = new ResourceEditor();
    System.setProperty("test.prop", "foo");
    try {
        editor.setAsText("${test.prop}-${bar}");
        Resource resolved = (Resource) editor.getValue();
        assertEquals("foo-${bar}", resolved.getFilename());
    } finally {
        System.getProperties().remove("test.prop");
    }
}
Also used : PropertyEditor(java.beans.PropertyEditor) Test(org.junit.Test)

Example 38 with PropertyEditor

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

the class DataBinderTests method testBindingWithFormatterAgainstFields.

@Test
public void testBindingWithFormatterAgainstFields() {
    TestBean tb = new TestBean();
    DataBinder binder = new DataBinder(tb);
    FormattingConversionService conversionService = new FormattingConversionService();
    DefaultConversionService.addDefaultConverters(conversionService);
    conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter());
    binder.setConversionService(conversionService);
    binder.initDirectFieldAccess();
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("myFloat", "1,2");
    LocaleContextHolder.setLocale(Locale.GERMAN);
    try {
        binder.bind(pvs);
        assertEquals(new Float(1.2), tb.getMyFloat());
        assertEquals("1,2", binder.getBindingResult().getFieldValue("myFloat"));
        PropertyEditor editor = binder.getBindingResult().findEditor("myFloat", Float.class);
        assertNotNull(editor);
        editor.setValue(new Float(1.4));
        assertEquals("1,4", editor.getAsText());
        editor = binder.getBindingResult().findEditor("myFloat", null);
        assertNotNull(editor);
        editor.setAsText("1,6");
        assertEquals(new Float(1.6), editor.getValue());
    } finally {
        LocaleContextHolder.resetLocaleContext();
    }
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NumberStyleFormatter(org.springframework.format.number.NumberStyleFormatter) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyEditor(java.beans.PropertyEditor) FormattingConversionService(org.springframework.format.support.FormattingConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Test(org.junit.Test)

Example 39 with PropertyEditor

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

the class ResourceArrayPropertyEditorTests method testPatternResource.

@Test
public void testPatternResource() throws Exception {
    // N.B. this will sometimes fail if you use classpath: instead of classpath*:.
    // The result depends on the classpath - if test-classes are segregated from classes
    // and they come first on the classpath (like in Maven) then it breaks, if classes
    // comes first (like in Spring Build) then it is OK.
    PropertyEditor editor = new ResourceArrayPropertyEditor();
    editor.setAsText("classpath*:org/springframework/core/io/support/Resource*Editor.class");
    Resource[] resources = (Resource[]) editor.getValue();
    assertNotNull(resources);
    assertTrue(resources[0].exists());
}
Also used : Resource(org.springframework.core.io.Resource) PropertyEditor(java.beans.PropertyEditor) Test(org.junit.Test)

Example 40 with PropertyEditor

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

the class ResourceArrayPropertyEditorTests method testStrictSystemPropertyReplacement.

@Test(expected = IllegalArgumentException.class)
public void testStrictSystemPropertyReplacement() {
    PropertyEditor editor = new ResourceArrayPropertyEditor(new PathMatchingResourcePatternResolver(), new StandardEnvironment(), false);
    System.setProperty("test.prop", "foo");
    try {
        editor.setAsText("${test.prop}-${bar}");
        Resource[] resources = (Resource[]) editor.getValue();
        assertEquals("foo-${bar}", resources[0].getFilename());
    } finally {
        System.getProperties().remove("test.prop");
    }
}
Also used : Resource(org.springframework.core.io.Resource) PropertyEditor(java.beans.PropertyEditor) StandardEnvironment(org.springframework.core.env.StandardEnvironment) 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