下面列出了java.beans.PropertyEditor#setAsText ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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()));
}
}
@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));
}
@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"));
}
@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...
}
}
@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());
}
}
@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();
}
}
@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");
}
}
@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());
}
@Test
public void setAsTextWithNull() throws Exception {
PropertyEditor uriEditor = new URIEditor();
uriEditor.setAsText(null);
assertNull(uriEditor.getValue());
assertEquals("", uriEditor.getAsText());
}
@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());
}
@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());
}
@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());
}
@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();
}
}
@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");
}
}
@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());
}
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());
}
@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());
}
@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());
}
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));
}
@Test
public void setAndGetAsTextWithWhitespaceResource() throws Exception {
PropertyEditor editor = new ResourceEditor();
editor.setAsText(" ");
assertEquals("", editor.getAsText());
}