Ensure that no work files are left behind

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/fileupload/trunk@1568856 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/test/java/org/apache/commons/fileupload/DiskFileItemSerializeTest.java b/src/test/java/org/apache/commons/fileupload/DiskFileItemSerializeTest.java
index 6bfc64e..dc3eb1d 100644
--- a/src/test/java/org/apache/commons/fileupload/DiskFileItemSerializeTest.java
+++ b/src/test/java/org/apache/commons/fileupload/DiskFileItemSerializeTest.java
@@ -31,6 +31,9 @@
 import java.io.OutputStream;
 
 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
+import org.apache.commons.io.FileUtils;
+import org.junit.After;
+import org.junit.Before;
 import org.junit.Test;
 
 /**
@@ -41,6 +44,27 @@
  */
 public class DiskFileItemSerializeTest {
 
+    // Use a private repo to catch any files left over by tests
+    private static final File REPO = new File(System.getProperty("java.io.tmpdir"), "diskfileitemrepo");
+
+    @Before
+    public void setUp() throws Exception {
+        if (REPO.exists()) {
+            FileUtils.deleteDirectory(REPO);
+        }
+        assertFalse("Must not exist", REPO.exists());
+        REPO.mkdir();
+    }
+
+    @After
+    public void tearDown() {
+        for(File file : FileUtils.listFiles(REPO, null, true)) {
+            System.out.println("Found leftover file " + file);
+        }
+        REPO.delete();
+        assertFalse(REPO + " is not empty", REPO.exists());
+    }
+
     /**
      * Content type for regular form items.
      */
@@ -77,7 +101,7 @@
      * Helper method to test creation of a field.
      */
     private void testInMemoryObject(byte[] testFieldValueBytes) {
-        testInMemoryObject(testFieldValueBytes, null);
+        testInMemoryObject(testFieldValueBytes, REPO);
     }
     
     /**
@@ -125,7 +149,10 @@
         compareBytes("Check", testFieldValueBytes, newItem.get());
 
         // Compare FileItem's (except byte[])
-        compareFileItems(item, newItem);
+        compareFileItems(item, newItem); 
+        
+        item.delete();
+        newItem.delete();
     }
     
     /**
@@ -135,8 +162,7 @@
     public void testValidRepository() throws Exception {
         // Create the FileItem
         byte[] testFieldValueBytes = createContentBytes(threshold);
-        File repository = new File(System.getProperty("java.io.tmpdir"));
-        testInMemoryObject(testFieldValueBytes, repository);
+        testInMemoryObject(testFieldValueBytes, REPO);
     }
     
     /**
@@ -231,7 +257,7 @@
      * Create a FileItem with the specfied content bytes.
      */
     private FileItem createFileItem(byte[] contentBytes) {
-        return createFileItem(contentBytes, null);
+        return createFileItem(contentBytes, REPO);
     }
     
     /**