jeremias 2004/04/23 15:47:39
Modified: io/src/test/org/apache/commons/io FileUtilsTestCase.java
io/src/java/org/apache/commons/io FileUtils.java
Log:
Bugzilla 28496
Fix for: org.apache.commons.io.FileUtils.copyFile shouldn't allow to copy a file on
itself
Submitted by: dany rizzi <drizzi.at.largesys.it>
Revision Changes Path
1.18 +23 -2
jakarta-commons/io/src/test/org/apache/commons/io/FileUtilsTestCase.java
Index: FileUtilsTestCase.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/io/src/test/org/apache/commons/io/FileUtilsTestCase.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- FileUtilsTestCase.java 23 Apr 2004 22:30:42 -0000 1.17
+++ FileUtilsTestCase.java 23 Apr 2004 22:47:39 -0000 1.18
@@ -180,6 +180,7 @@
} finally {
fis.close();
}
+ //TODO Maybe test copy to itself like for copyFile()
}
// forceMkdir
@@ -279,6 +280,19 @@
assertTrue("Check last modified date preserved",
testFile1.lastModified() == destination.lastModified());
}
+
+ public void testCopyToSelf() throws Exception {
+ File destination = new File(getTestDirectory(), "copy3.txt");
+ //Prepare a test file
+ FileUtils.copyFile(testFile1, destination);
+
+ try {
+ FileUtils.copyFile(destination, destination);
+ fail("file copy to self should not be possible");
+ } catch (IOException ioe) {
+ //we want the exception, copy to self should be illegal
+ }
+ }
public void testCopyFile2WithoutFileDatePreservation() throws Exception {
File destination = new File(getTestDirectory(), "copy2.txt");
@@ -330,7 +344,14 @@
assertTrue("Check Full copy", destination.length() == testFile1Size);
/* disabled: Thread.sleep doesn't work reliantly for this case
assertTrue("Check last modified date preserved",
- testFile1.lastModified() == destination.lastModified());*/
+ testFile1.lastModified() == destination.lastModified());*/
+
+ try {
+ FileUtils.copyFileToDirectory(destination, directory);
+ fail("Should not be able to copy a file into the same directory as
itself");
+ } catch (IOException ioe) {
+ //we want that, cannot copy to the same directory as the original file
+ }
}
public void testCopyFile2ToDir() throws Exception {
1.30 +8 -1 jakarta-commons/io/src/java/org/apache/commons/io/FileUtils.java
Index: FileUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/FileUtils.java,v
retrieving revision 1.29
retrieving revision 1.30
diff -u -r1.29 -r1.30
--- FileUtils.java 12 Mar 2004 22:21:38 -0000 1.29
+++ FileUtils.java 23 Apr 2004 22:47:39 -0000 1.30
@@ -416,6 +416,13 @@
throw new IOException(message);
}
+ //makes sure it is not the same file
+ if (source.getCanonicalPath().equals(destination.getCanonicalPath())) {
+ String message =
+ "Unable to write file " + source + " on itself.";
+ throw new IOException(message);
+ }
+
FileInputStream input = new FileInputStream(source);
try {
FileOutputStream output = new FileOutputStream(destination);
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]