Author: niallp
Date: Wed Aug 4 23:56:03 2010
New Revision: 982433
URL: http://svn.apache.org/viewvc?rev=982433&view=rev
Log:
IO-219 - Throw FileExistsException when moving a file or directory if the
destination already exists
Added:
commons/proper/io/trunk/src/java/org/apache/commons/io/FileExistsException.java
(with props)
Modified:
commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
Added:
commons/proper/io/trunk/src/java/org/apache/commons/io/FileExistsException.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/FileExistsException.java?rev=982433&view=auto
==============================================================================
---
commons/proper/io/trunk/src/java/org/apache/commons/io/FileExistsException.java
(added)
+++
commons/proper/io/trunk/src/java/org/apache/commons/io/FileExistsException.java
Wed Aug 4 23:56:03 2010
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * Indicates that a file already exists.
+ *
+ * @version $Id$
+ * @since Commons IO 2.0
+ */
+public class FileExistsException extends IOException {
+
+ /**
+ * Defines the serial version UID.
+ */
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * Default Constructor.
+ */
+ public FileExistsException() {
+ super();
+ }
+
+ /**
+ * Construct an instance with the specified message.
+ *
+ * @param message The error message
+ */
+ public FileExistsException(String message) {
+ super(message);
+ }
+
+ /**
+ * Construct an instance with the specified file.
+ *
+ * @param file The file that exists
+ */
+ public FileExistsException(File file) {
+ super("File " + file + " exists");
+ }
+
+}
Propchange:
commons/proper/io/trunk/src/java/org/apache/commons/io/FileExistsException.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/io/trunk/src/java/org/apache/commons/io/FileExistsException.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Modified: commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java?rev=982433&r1=982432&r2=982433&view=diff
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
(original)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java Wed
Aug 4 23:56:03 2010
@@ -1797,7 +1797,7 @@ public class FileUtils {
throw new IOException("Source '" + srcDir + "' is not a
directory");
}
if (destDir.exists()) {
- throw new IOException("Destination '" + destDir + "' already
exists");
+ throw new FileExistsException("Destination '" + destDir + "'
already exists");
}
boolean rename = srcDir.renameTo(destDir);
if (!rename) {
@@ -1869,7 +1869,7 @@ public class FileUtils {
throw new IOException("Source '" + srcFile + "' is a directory");
}
if (destFile.exists()) {
- throw new IOException("Destination '" + destFile + "' already
exists");
+ throw new FileExistsException("Destination '" + destFile + "'
already exists");
}
if (destFile.isDirectory()) {
throw new IOException("Destination '" + destFile + "' is a
directory");
Modified:
commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
URL:
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java?rev=982433&r1=982432&r2=982433&view=diff
==============================================================================
---
commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
(original)
+++
commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
Wed Aug 4 23:56:03 2010
@@ -1421,8 +1421,8 @@ public class FileUtilsTestCase extends F
createFile(testDestFile, 0);
try {
FileUtils.moveFile(testSourceFile, testDestFile);
- fail("Expected IOException when dest already exists");
- } catch (IOException e) {
+ fail("Expected FileExistsException when dest already exists");
+ } catch (FileExistsException e) {
// expected
}
@@ -1554,11 +1554,12 @@ public class FileUtilsTestCase extends F
}
File testSrcFile = new File(getTestDirectory(),
"testMoveDirectorySource");
File testDestFile = new File(getTestDirectory(),
"testMoveDirectoryDest");
+ testSrcFile.mkdir();
testDestFile.mkdir();
try {
FileUtils.moveDirectory(testSrcFile, testDestFile);
- fail("Expected IOException when dest already exists");
- } catch (IOException e) {
+ fail("Expected FileExistsException when dest already exists");
+ } catch (FileExistsException e) {
// expected
}