Refine search
/** * Convert the given text value using the given property editor. * @param oldValue the previous value, if available (may be {@code null}) * @param newTextValue the proposed text value * @param editor the PropertyEditor to use * @return the converted value */ private Object doConvertTextValue(@Nullable Object oldValue, String newTextValue, PropertyEditor editor) { try { editor.setValue(oldValue); } catch (Exception ex) { if (logger.isDebugEnabled()) { logger.debug("PropertyEditor [" + editor.getClass().getName() + "] does not support setValue call", ex); } // Swallow and proceed. } editor.setAsText(newTextValue); return editor.getValue(); }
@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 testSystemPropertyReplacement() { PropertyEditor editor = new ResourceArrayPropertyEditor(); 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"); } }
@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 testWithNonExistentPath() throws Exception { PropertyEditor pathEditor = new PathEditor(); pathEditor.setAsText("file:/no_way_this_file_is_found.doc"); Object value = pathEditor.getValue(); assertTrue(value instanceof Path); Path path = (Path) value; assertTrue(!path.toFile().exists()); }
@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"); } }
@Test public void classpathURLWithWhitespace() throws Exception { PropertyEditor uriEditor = new URIEditor(getClass().getClassLoader()); uriEditor.setAsText(" classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) + ".class "); Object value = uriEditor.getValue(); assertTrue(value instanceof URI); URI uri = (URI) value; assertEquals(uri.toString(), uriEditor.getAsText()); assertTrue(!uri.getScheme().startsWith("classpath")); }
@Test public void testUnqualifiedFileNameFound() throws Exception { PropertyEditor fileEditor = new FileEditor(); String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) + ".class"; fileEditor.setAsText(fileName); Object value = fileEditor.getValue(); assertTrue(value instanceof File); File file = (File) value; assertTrue(file.exists()); String absolutePath = file.getAbsolutePath().replace('\\', '/'); assertTrue(absolutePath.endsWith(fileName)); }
@Test public void testUnqualifiedFileNameNotFound() throws Exception { PropertyEditor fileEditor = new FileEditor(); String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) + ".clazz"; fileEditor.setAsText(fileName); Object value = fileEditor.getValue(); assertTrue(value instanceof File); File file = (File) value; assertFalse(file.exists()); String absolutePath = file.getAbsolutePath().replace('\\', '/'); assertTrue(absolutePath.endsWith(fileName)); }
@Test public void classpathURL() throws Exception { PropertyEditor uriEditor = new URIEditor(getClass().getClassLoader()); uriEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) + ".class"); Object value = uriEditor.getValue(); assertTrue(value instanceof URI); URI uri = (URI) value; assertEquals(uri.toString(), uriEditor.getAsText()); assertTrue(!uri.getScheme().startsWith("classpath")); }
@Test public void testClassEditor() { PropertyEditor classEditor = new ClassEditor(); classEditor.setAsText(TestBean.class.getName()); assertEquals(TestBean.class, classEditor.getValue()); assertEquals(TestBean.class.getName(), classEditor.getAsText()); classEditor.setAsText(null); assertEquals("", classEditor.getAsText()); classEditor.setAsText(""); assertEquals("", classEditor.getAsText()); classEditor.setAsText("\t "); assertEquals("", classEditor.getAsText()); }
@Test public void standardURLWithWhitespace() throws Exception { PropertyEditor uriEditor = new URIEditor(); uriEditor.setAsText(" http://www.springframework.org "); Object value = uriEditor.getValue(); assertTrue(value instanceof URI); URI uri = (URI) value; assertEquals("http://www.springframework.org", uri.toString()); }
@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 testVanillaResource() throws Exception { PropertyEditor editor = new ResourceArrayPropertyEditor(); editor.setAsText("classpath:org/springframework/core/io/support/ResourceArrayPropertyEditor.class"); Resource[] resources = (Resource[]) editor.getValue(); assertNotNull(resources); assertTrue(resources[0].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()); }