Browse Source

File utilities for creating directories

The java.io.File methods for creating directories report failure by
returning false. To ease proper checking of return values provide
utility methods wrapping mkdir() and mkdirs() which throw IOException
on failure.

Also fix the tests to store test data under a trash folder and cleanup
after test.

Change-Id: I09c7f9909caf7e25feabda9d31e21ce154e7fcd5
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
tags/v0.10.1
Matthias Sohn 13 years ago
parent
commit
c6ca443b61

+ 77
- 4
org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java View File

@@ -49,8 +49,21 @@ import java.io.IOException;
import junit.framework.TestCase;

public class FileUtilTest extends TestCase {

private final File trash = new File(new File("target"), "trash");

@Override
protected void setUp() throws Exception {
assertTrue(trash.mkdirs());
}

@Override
protected void tearDown() throws Exception {
FileUtils.delete(trash, FileUtils.RECURSIVE | FileUtils.RETRY);
}

public void testDeleteFile() throws IOException {
File f = new File("test");
File f = new File(trash, "test");
assertTrue(f.createNewFile());
FileUtils.delete(f);
assertFalse(f.exists());
@@ -70,12 +83,12 @@ public class FileUtilTest extends TestCase {
}

public void testDeleteRecursive() throws IOException {
File f1 = new File("test/test/a");
File f1 = new File(trash, "test/test/a");
f1.mkdirs();
f1.createNewFile();
File f2 = new File("test/test/b");
File f2 = new File(trash, "test/test/b");
f2.createNewFile();
File d = new File("test");
File d = new File(trash, "test");
FileUtils.delete(d, FileUtils.RECURSIVE);
assertFalse(d.exists());

@@ -92,4 +105,64 @@ public class FileUtilTest extends TestCase {
fail("recursive deletion of non-existing directory must not fail with option SKIP_MISSING");
}
}

public void testMkdir() throws IOException {
File d = new File(trash, "test");
FileUtils.mkdir(d);
assertTrue(d.exists() && d.isDirectory());

try {
FileUtils.mkdir(d);
fail("creation of existing directory must fail");
} catch (IOException e) {
// expected
}

FileUtils.mkdir(d, true);
assertTrue(d.exists() && d.isDirectory());

assertTrue(d.delete());
File f = new File(trash, "test");
assertTrue(f.createNewFile());
try {
FileUtils.mkdir(d);
fail("creation of directory having same path as existing file must"
+ " fail");
} catch (IOException e) {
// expected
}
assertTrue(f.delete());
}

public void testMkdirs() throws IOException {
File root = new File(trash, "test");
assertTrue(root.mkdir());

File d = new File(root, "test/test");
FileUtils.mkdirs(d);
assertTrue(d.exists() && d.isDirectory());

try {
FileUtils.mkdirs(d);
fail("creation of existing directory hierarchy must fail");
} catch (IOException e) {
// expected
}

FileUtils.mkdirs(d, true);
assertTrue(d.exists() && d.isDirectory());

FileUtils.delete(root, FileUtils.RECURSIVE);
File f = new File(trash, "test");
assertTrue(f.createNewFile());
try {
FileUtils.mkdirs(d);
fail("creation of directory having path conflicting with existing"
+ " file must fail");
} catch (IOException e) {
// expected
}
assertTrue(f.delete());
}

}

+ 2
- 0
org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties View File

@@ -268,6 +268,8 @@ missingPrerequisiteCommits=missing prerequisite commits:
missingRequiredParameter=Parameter "{0}" is missing
missingSecretkey=Missing secretkey.
mixedStagesNotAllowed=Mixed stages not allowed
mkDirFailed=Creating directory {0} failed
mkDirsFailed=Creating directories for {0} failed
multipleMergeBasesFor=Multiple merge bases for:\n {0}\n {1} found:\n {2}\n {3}
need2Arguments=Need 2 arguments
needPackOut=need packOut

+ 3
- 1
org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java View File

@@ -328,6 +328,8 @@ public class JGitText extends TranslationBundle {
/***/ public String missingRequiredParameter;
/***/ public String missingSecretkey;
/***/ public String mixedStagesNotAllowed;
/***/ public String mkDirFailed;
/***/ public String mkDirsFailed;
/***/ public String multipleMergeBasesFor;
/***/ public String need2Arguments;
/***/ public String needPackOut;
@@ -486,7 +488,7 @@ public class JGitText extends TranslationBundle {
/***/ public String unknownRepositoryFormat;
/***/ public String unknownZlibError;
/***/ public String unmergedPath;
/***/ public String unmergedPaths;
/***/ public String unmergedPaths;
/***/ public String unpackException;
/***/ public String unreadablePackIndex;
/***/ public String unrecognizedRef;

+ 84
- 0
org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java View File

@@ -135,4 +135,88 @@ public class FileUtils {
}
}

/**
* Creates the directory named by this abstract pathname.
*
* @param d
* directory to be created
* @throws IOException
* if creation of {@code d} fails. This may occur if {@code d}
* did exist when the method was called. This can therefore
* cause IOExceptions during race conditions when multiple
* concurrent threads all try to create the same directory.
*/
public static void mkdir(final File d)
throws IOException {
mkdir(d, false);
}

/**
* Creates the directory named by this abstract pathname.
*
* @param d
* directory to be created
* @param skipExisting
* if {@code true} skip creation of the given directory if it
* already exists in the file system
* @throws IOException
* if creation of {@code d} fails. This may occur if {@code d}
* did exist when the method was called. This can therefore
* cause IOExceptions during race conditions when multiple
* concurrent threads all try to create the same directory.
*/
public static void mkdir(final File d, boolean skipExisting)
throws IOException {
if (!d.mkdir()) {
if (skipExisting && d.isDirectory())
return;
throw new IOException(MessageFormat.format(
JGitText.get().mkDirFailed, d.getAbsolutePath()));
}
}

/**
* Creates the directory named by this abstract pathname, including any
* necessary but nonexistent parent directories. Note that if this operation
* fails it may have succeeded in creating some of the necessary parent
* directories.
*
* @param d
* directory to be created
* @throws IOException
* if creation of {@code d} fails. This may occur if {@code d}
* did exist when the method was called. This can therefore
* cause IOExceptions during race conditions when multiple
* concurrent threads all try to create the same directory.
*/
public static void mkdirs(final File d) throws IOException {
mkdirs(d, false);
}

/**
* Creates the directory named by this abstract pathname, including any
* necessary but nonexistent parent directories. Note that if this operation
* fails it may have succeeded in creating some of the necessary parent
* directories.
*
* @param d
* directory to be created
* @param skipExisting
* if {@code true} skip creation of the given directory if it
* already exists in the file system
* @throws IOException
* if creation of {@code d} fails. This may occur if {@code d}
* did exist when the method was called. This can therefore
* cause IOExceptions during race conditions when multiple
* concurrent threads all try to create the same directory.
*/
public static void mkdirs(final File d, boolean skipExisting)
throws IOException {
if (!d.mkdirs()) {
if (skipExisting && d.isDirectory())
return;
throw new IOException(MessageFormat.format(
JGitText.get().mkDirsFailed, d.getAbsolutePath()));
}
}
}

Loading…
Cancel
Save