summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test
diff options
context:
space:
mode:
authorMatthias Sohn <matthias.sohn@sap.com>2010-12-10 21:48:09 +0100
committerChris Aniszczyk <caniszczyk@gmail.com>2010-12-13 08:47:17 -0600
commitc6ca443b61372d73a7f1438c995713bad39b5277 (patch)
treea8e701858c05442169242c2781a271787fb48a22 /org.eclipse.jgit.test
parent45a020fe6a1ed4a7a9f8c46ab81dd4e655211941 (diff)
downloadjgit-c6ca443b61372d73a7f1438c995713bad39b5277.tar.gz
jgit-c6ca443b61372d73a7f1438c995713bad39b5277.zip
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>
Diffstat (limited to 'org.eclipse.jgit.test')
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java81
1 files changed, 77 insertions, 4 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java
index 26f53ed382..cc53a5a74d 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java
@@ -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());
+ }
+
}