@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... } }
@Override public String getAsText() { File value = (File) getValue(); return (value != null ? value.getPath() : ""); }
public void setAsText(String text) throws IllegalArgumentException { if (StringUtils.isBlank(text)) { return; } super.setAsText(text); } }
@Test(expected = IllegalArgumentException.class) public void testWithNonExistentResource() throws Exception { PropertyEditor propertyEditor = new FileEditor(); propertyEditor.setAsText("classpath:no_way_this_file_is_found.doc"); }
@Override public String getAsText() { File value = (File) getValue(); return (value != null ? value.getPath() : ""); }
@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()); } }
@Override public String getAsText() { File value = (File) getValue(); return (value != null ? value.getPath() : ""); }
@Test public void testAbsoluteFileName() throws Exception { PropertyEditor fileEditor = new FileEditor(); fileEditor.setAsText("/no_way_this_file_is_found.doc"); Object value = fileEditor.getValue(); assertTrue(value instanceof File); File file = (File) value; assertTrue(!file.exists()); }
@Test public void testWithNonExistentFile() throws Exception { PropertyEditor fileEditor = new FileEditor(); fileEditor.setAsText("file:no_way_this_file_is_found.doc"); Object value = fileEditor.getValue(); assertTrue(value instanceof File); File file = (File) value; assertTrue(!file.exists()); }
@Override public String getAsText() { File value = (File) getValue(); return (value != null ? value.getPath() : ""); }
@Test public void testFileEditor() { PropertyEditor fileEditor = new FileEditor(); fileEditor.setAsText("file:myfile.txt"); assertEquals(new File("myfile.txt"), fileEditor.getValue()); assertEquals((new File("myfile.txt")).getPath(), fileEditor.getAsText()); }
@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 testClasspathFileName() throws Exception { PropertyEditor fileEditor = new FileEditor(); fileEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass()) + ".class"); Object value = fileEditor.getValue(); assertTrue(value instanceof File); File file = (File) value; assertTrue(file.exists()); }