Java Code Examples for java.beans.PropertyEditor#setAsText()

The following examples show how to use java.beans.PropertyEditor#setAsText() . 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 File: PathEditorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testUnqualifiedPathNameNotFound() throws Exception {
	PropertyEditor pathEditor = new PathEditor();
	String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
			ClassUtils.getShortName(getClass()) + ".clazz";
	pathEditor.setAsText(fileName);
	Object value = pathEditor.getValue();
	assertTrue(value instanceof Path);
	Path path = (Path) value;
	File file = path.toFile();
	assertFalse(file.exists());
	String absolutePath = file.getAbsolutePath();
	if (File.separatorChar == '\\') {
		absolutePath = absolutePath.replace('\\', '/');
	}
	assertTrue(absolutePath.endsWith(fileName));
}
 
Example 2
Source File: JspRuntimeLibrary.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
public static Object getValueFromPropertyEditorManager(
                 Class<?> attrClass, String attrName, String attrValue) 
    throws JasperException 
{
    try {
        PropertyEditor propEditor = 
            PropertyEditorManager.findEditor(attrClass);
        if (propEditor != null) {
            propEditor.setAsText(attrValue);
            return propEditor.getValue();
        } else {
            throw new IllegalArgumentException(
                Localizer.getMessage("jsp.error.beans.propertyeditor.notregistered"));
        }
    } catch (IllegalArgumentException ex) {
        throw new JasperException(
            Localizer.getMessage("jsp.error.beans.property.conversion",
                                 attrValue, attrClass.getName(), attrName,
                                 ex.getMessage()));
    }
}
 
Example 3
Source File: ResourceArrayPropertyEditorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 4
Source File: ResourceEditorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 5
Source File: ResourceEditorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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());
}
 
Example 6
Source File: URIEditorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void doTestURI(String uriSpec) {
	PropertyEditor uriEditor = new URIEditor();
	uriEditor.setAsText(uriSpec);
	Object value = uriEditor.getValue();
	assertTrue(value instanceof URI);
	URI uri = (URI) value;
	assertEquals(uriSpec, uri.toString());
}
 
Example 7
Source File: URLEditorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testStandardURL() throws Exception {
	PropertyEditor urlEditor = new URLEditor();
	urlEditor.setAsText("http://www.springframework.org");
	Object value = urlEditor.getValue();
	assertTrue(value instanceof URL);
	URL url = (URL) value;
	assertEquals(url.toExternalForm(), urlEditor.getAsText());
}
 
Example 8
Source File: ResourceEditorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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");
	}
}
 
Example 9
Source File: DataBinderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testBindingWithFormatter() {
	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);
	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();
	}
}
 
Example 10
Source File: URIEditorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void encodeAlreadyEncodedURI() throws Exception {
	PropertyEditor uriEditor = new URIEditor(false);
	uriEditor.setAsText("https://example.com/spaces%20and%20%E2%82%AC");
	Object value = uriEditor.getValue();
	assertTrue(value instanceof URI);
	URI uri = (URI) value;
	assertEquals(uri.toString(), uriEditor.getAsText());
	assertEquals("https://example.com/spaces%20and%20%E2%82%AC", uri.toASCIIString());
}
 
Example 11
Source File: URLEditorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testClasspathURL() throws Exception {
	PropertyEditor urlEditor = new URLEditor();
	urlEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) +
			"/" + ClassUtils.getShortName(getClass()) + ".class");
	Object value = urlEditor.getValue();
	assertTrue(value instanceof URL);
	URL url = (URL) value;
	assertEquals(url.toExternalForm(), urlEditor.getAsText());
	assertTrue(!url.getProtocol().startsWith("classpath"));
}
 
Example 12
Source File: PathEditorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testClasspathPathName() throws Exception {
	PropertyEditor pathEditor = new PathEditor();
	pathEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
			ClassUtils.getShortName(getClass()) + ".class");
	Object value = pathEditor.getValue();
	assertTrue(value instanceof Path);
	Path path = (Path) value;
	assertTrue(path.toFile().exists());
}
 
Example 13
Source File: URIEditorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void setAsTextWithNull() throws Exception {
	PropertyEditor uriEditor = new URIEditor();
	uriEditor.setAsText(null);
	assertNull(uriEditor.getValue());
	assertEquals("", uriEditor.getAsText());
}
 
Example 14
Source File: ResourceArrayPropertyEditorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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());
}
 
Example 15
Source File: ResourceEditorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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");
	}
}
 
Example 16
Source File: DataBinderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testBindingWithCustomFormatter() {
	TestBean tb = new TestBean();
	DataBinder binder = new DataBinder(tb);
	binder.addCustomFormatter(new NumberStyleFormatter(), Float.class);
	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");
		assertTrue(((Number) editor.getValue()).floatValue() == 1.6f);
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example 17
Source File: CustomEditorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testFileEditorWithAbsolutePath() {
	PropertyEditor fileEditor = new FileEditor();
	// testing on Windows
	if (new File("C:/myfile.txt").isAbsolute()) {
		fileEditor.setAsText("C:/myfile.txt");
		assertEquals(new File("C:/myfile.txt"), fileEditor.getValue());
	}
	// testing on Unix
	if (new File("/myfile.txt").isAbsolute()) {
		fileEditor.setAsText("/myfile.txt");
		assertEquals(new File("/myfile.txt"), fileEditor.getValue());
	}
}
 
Example 18
Source File: CustomEditorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testFileEditorWithRelativePath() {
	PropertyEditor fileEditor = new FileEditor();
	try {
		fileEditor.setAsText("myfile.txt");
	}
	catch (IllegalArgumentException ex) {
		// expected: should get resolved as class path resource,
		// and there is no such resource in the class path...
	}
}
 
Example 19
Source File: ResourceEditorTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void setAndGetAsTextWithWhitespaceResource() throws Exception {
	PropertyEditor editor = new ResourceEditor();
	editor.setAsText("  ");
	assertEquals("", editor.getAsText());
}
 
Example 20
Source File: ELSupport.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
public static final Object coerceToType(final Object obj,
        final Class<?> type) throws ELException {
    if (type == null || Object.class.equals(type) ||
            (obj != null && type.isAssignableFrom(obj.getClass()))) {
        return obj;
    }
    if (String.class.equals(type)) {
        return coerceToString(obj);
    }
    if (ELArithmetic.isNumberType(type)) {
        return coerceToNumber(obj, type);
    }
    if (Character.class.equals(type) || Character.TYPE == type) {
        return coerceToCharacter(obj);
    }
    if (Boolean.class.equals(type) || Boolean.TYPE == type) {
        return coerceToBoolean(obj);
    }
    if (type.isEnum()) {
        return coerceToEnum(obj, type);
    }

    // new to spec
    if (obj == null)
        return null;
    if (obj instanceof String) {
        PropertyEditor editor = PropertyEditorManager.findEditor(type);
        if (editor == null) {
            if ("".equals(obj)) {
                return null;
            }
            throw new ELException(MessageFactory.get("error.convert", obj,
                    obj.getClass(), type));
        } else {
            try {
                editor.setAsText((String) obj);
                return editor.getValue();
            } catch (RuntimeException e) {
                if ("".equals(obj)) {
                    return null;
                }
                throw new ELException(MessageFactory.get("error.convert",
                        obj, obj.getClass(), type), e);
            }
        }
    }
    throw new ELException(MessageFactory.get("error.convert",
            obj, obj.getClass(), type));
}