You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by gg...@apache.org on 2007/02/04 23:15:12 UTC
svn commit: r503497 - in /jakarta/commons/proper/io/trunk/src:
java/org/apache/commons/io/FileUtils.java
test/org/apache/commons/io/FileUtilsTestCase.java
test/org/apache/commons/io/testtools/FileBasedTestCase.java
Author: ggregory
Date: Sun Feb 4 14:15:11 2007
New Revision: 503497
URL: http://svn.apache.org/viewvc?view=rev&rev=503497
Log:
[IO-112] NPE in FileUtils.openOutputStream(File) when file has no parent in path.
Modified:
jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/testtools/FileBasedTestCase.java
Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java?view=diff&rev=503497&r1=503496&r2=503497
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java (original)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java Sun Feb 4 14:15:11 2007
@@ -169,7 +169,7 @@
}
} else {
File parent = file.getParentFile();
- if (parent.exists() == false) {
+ if (parent != null && parent.exists() == false) {
if (parent.mkdirs() == false) {
throw new IOException("File '" + file + "' could not be created");
}
Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java?view=diff&rev=503497&r1=503496&r2=503497
==============================================================================
--- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java (original)
+++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java Sun Feb 4 14:15:11 2007
@@ -143,6 +143,36 @@
}
//-----------------------------------------------------------------------
+ void openOutputStream_noParent(boolean createFile) throws Exception {
+ File file = new File("test.txt");
+ assertNull(file.getParentFile());
+ try {
+ if (createFile) {
+ createLineBasedFile(file, new String[]{"Hello"});}
+ FileOutputStream out = null;
+ try {
+ out = FileUtils.openOutputStream(file);
+ out.write(0);
+ } finally {
+ IOUtils.closeQuietly(out);
+ }
+ assertEquals(true, file.exists());
+ } finally {
+ if (file.delete() == false) {
+ file.deleteOnExit();
+ }
+ }
+ }
+
+ public void test_openOutputStream_noParentCreateFile() throws Exception {
+ openOutputStream_noParent(true);
+ }
+
+ public void test_openOutputStream_noParentNoFile() throws Exception {
+ openOutputStream_noParent(false);
+ }
+
+
public void test_openOutputStream_exists() throws Exception {
File file = new File(getTestDirectory(), "test.txt");
createLineBasedFile(file, new String[] {"Hello"});
Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/testtools/FileBasedTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/testtools/FileBasedTestCase.java?view=diff&rev=503497&r1=503496&r2=503497
==============================================================================
--- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/testtools/FileBasedTestCase.java (original)
+++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/testtools/FileBasedTestCase.java Sun Feb 4 14:15:11 2007
@@ -93,12 +93,10 @@
}
protected void createLineBasedFile(File file, String[] data) throws IOException {
- if (!file.getParentFile().exists()) {
- throw new IOException("Cannot create file " + file
- + " as the parent directory does not exist");
+ if (file.getParentFile() != null && !file.getParentFile().exists()) {
+ throw new IOException("Cannot create file " + file + " as the parent directory does not exist");
}
- PrintWriter output = new PrintWriter(
- new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
+ PrintWriter output = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
try {
for (int i = 0; i < data.length; i++) {
output.println(data[i]);
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org