commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1029155 - /commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestFileConfiguration.java
Date Sat, 30 Oct 2010 18:51:39 GMT
Author: oheger
Date: Sat Oct 30 18:51:39 2010
New Revision: 1029155

URL: http://svn.apache.org/viewvc?rev=1029155&view=rev
Log:
Improved handling of temporary test files. (ported from trunk)

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestFileConfiguration.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestFileConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestFileConfiguration.java?rev=1029155&r1=1029154&r2=1029155&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestFileConfiguration.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestFileConfiguration.java
Sat Oct 30 18:51:39 2010
@@ -24,6 +24,8 @@ import java.io.FileWriter;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.net.URL;
+import java.util.Collection;
+import java.util.LinkedList;
 import java.util.Properties;
 
 import junit.framework.TestCase;
@@ -44,9 +46,82 @@ public class TestFileConfiguration exten
     /** Constant for a test file.*/
     private static final File TEST_FILE = ConfigurationAssert.getTestFile(TEST_FILENAME);
 
+    /** Constant for a test output file. */
+    private static final File OUT_FILE = new File(
+            "target/test-resources/foo/bar/test.properties");
+
     /** Constant for the name of a resource to be resolved.*/
     private static final String RESOURCE_NAME = "config/deep/deeptest.properties";
 
+    /** A list with temporary files created during a test case. */
+    private Collection<File> tempFiles = new LinkedList<File>();
+
+    /**
+     * Initializes the test environment. This implementation ensures that the
+     * test output file does not exist.
+     */
+    @Override
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        removeOutFile();
+    }
+
+    /**
+     * Performs cleanup after a test case. This implementation removes temporary
+     * files that have been created.
+     */
+    @Override
+    protected void tearDown() throws Exception
+    {
+        for(File file : tempFiles)
+        {
+            removeFile(file);
+        }
+        removeOutFile();
+
+        super.tearDown();
+    }
+
+    /**
+     * Adds a temporary file used by a test case. This method removes the file
+     * if it already exists. It is then added to a list so that it is removed at
+     * the end of the test.
+     *
+     * @param file the temporary file
+     */
+    private void addTemporaryFile(File file)
+    {
+        removeFile(file);
+        tempFiles.add(file);
+    }
+
+    /**
+     * Removes a file if it exists.
+     *
+     * @param file the file to be removed
+     */
+    private static void removeFile(File file)
+    {
+        if (file.exists())
+        {
+            assertTrue("Cannot remove file: " + file, file.delete());
+        }
+    }
+
+    /**
+     * Removes the test output file if it exists. Its parent directories are
+     * also removed.
+     */
+    private static void removeOutFile()
+    {
+        removeFile(OUT_FILE);
+        File parent = OUT_FILE.getParentFile();
+        removeFile(parent);
+        parent = parent.getParentFile();
+        removeFile(parent);
+    }
+
     public void testSetURL() throws Exception
     {
         // http URL
@@ -97,54 +172,29 @@ public class TestFileConfiguration exten
 
     public void testCreateFile1() throws Exception
     {
-        File file = new File("target/test-resources/foo/bar/test.properties");
-        if (file.exists())
-        {
-            file.delete();
-            file.getParentFile().delete();
-        }
-
-        assertFalse("The file should not exist", file.exists());
+        assertFalse("The file should not exist", OUT_FILE.exists());
 
-        FileConfiguration config = new PropertiesConfiguration(file);
+        FileConfiguration config = new PropertiesConfiguration(OUT_FILE);
         config.save();
 
-        assertTrue("The file doesn't exist", file.exists());
+        assertTrue("The file doesn't exist", OUT_FILE.exists());
     }
 
     public void testCreateFile2() throws Exception
     {
-        File file = new File("target/test-resources/foo/bar/test.properties");
-        if (file.exists())
-        {
-            file.delete();
-            file.getParentFile().delete();
-        }
-
-        assertFalse("The file should not exist", file.exists());
-
         FileConfiguration config = new PropertiesConfiguration();
-        config.setFile(file);
+        config.setFile(OUT_FILE);
         config.save();
 
-        assertTrue("The file doesn't exist", file.exists());
+        assertTrue("The file doesn't exist", OUT_FILE.exists());
     }
 
     public void testCreateFile3() throws Exception
     {
-        File file = new File("target/test-resources/foo/bar/test.properties");
-        if (file.exists())
-        {
-            file.delete();
-            file.getParentFile().delete();
-        }
-
-        assertFalse("The file should not exist", file.exists());
-
         FileConfiguration config = new PropertiesConfiguration();
-        config.save(file);
+        config.save(OUT_FILE);
 
-        assertTrue("The file doesn't exist", file.exists());
+        assertTrue("The file doesn't exist", OUT_FILE.exists());
     }
 
     /**
@@ -156,43 +206,31 @@ public class TestFileConfiguration exten
     public void testWithConfigurationFactory() throws Exception
     {
         File file = ConfigurationAssert.getOutFile("testFileConfiguration.properties");
+        addTemporaryFile(file);
 
-        if (file.exists())
-        {
-            assertTrue("File cannot be deleted", file.delete());
-        }
-
-        try
+        DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
+        builder.setURL(ConfigurationAssert
+                .getTestFile("testDigesterConfiguration2.xml").toURI().toURL());
+        CombinedConfiguration cc =
+                (CombinedConfiguration) builder.getConfiguration();
+        PropertiesConfiguration config = null;
+        for (int i = 0; config == null; i++)
         {
-            DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
-            builder.setURL(ConfigurationAssert.getTestFile("testDigesterConfiguration2.xml").toURI().toURL());
-            CombinedConfiguration cc = (CombinedConfiguration) builder.getConfiguration();
-            PropertiesConfiguration config = null;
-            for (int i = 0; config == null; i++)
+            if (cc.getConfiguration(i) instanceof PropertiesConfiguration)
             {
-                if (cc.getConfiguration(i) instanceof PropertiesConfiguration)
-                {
-                    config = (PropertiesConfiguration) cc.getConfiguration(i);
-                }
+                config = (PropertiesConfiguration) cc.getConfiguration(i);
             }
+        }
 
-            config.setProperty("test", "yes");
-            config.save(file);
-            assertTrue(file.exists());
-            config = new PropertiesConfiguration();
-            config.setFile(file);
-            config.load();
+        config.setProperty("test", "yes");
+        config.save(file);
+        assertTrue(file.exists());
+        config = new PropertiesConfiguration();
+        config.setFile(file);
+        config.load();
 
-            assertEquals("yes", config.getProperty("test"));
-            assertEquals("masterOfPost", config.getProperty("mail.account.user"));
-        }
-        finally
-        {
-            if (file.exists())
-            {
-                assertTrue("File could not be deleted", file.delete());
-            }
-        }
+        assertEquals("yes", config.getProperty("test"));
+        assertEquals("masterOfPost", config.getProperty("mail.account.user"));
     }
 
     /**
@@ -236,6 +274,7 @@ public class TestFileConfiguration exten
             String path = System.getProperties().getProperty("user.home");
             File homeDir = new File(path);
             tempFile = File.createTempFile("CONF", null, homeDir);
+            tempFiles.add(tempFile);
             String fileName = tempFile.getName();
             Properties props = new Properties();
             props.setProperty("1", "one");
@@ -279,10 +318,6 @@ public class TestFileConfiguration exten
                     ioex.printStackTrace();
                 }
             }
-            if (tempFile.exists())
-            {
-                assertTrue(tempFile.delete());
-            }
         }
     }
 
@@ -293,6 +328,7 @@ public class TestFileConfiguration exten
     public void testReloadingWithAutoSave() throws Exception
     {
         File configFile = ConfigurationAssert.getOutFile(TEST_FILENAME);
+        addTemporaryFile(configFile);
         PrintWriter out = null;
 
         try
@@ -317,10 +353,6 @@ public class TestFileConfiguration exten
             {
                 out.close();
             }
-            if (configFile.exists())
-            {
-                assertTrue(configFile.delete());
-            }
         }
     }
 
@@ -332,14 +364,13 @@ public class TestFileConfiguration exten
     {
         File path = ConfigurationAssert.getOutFile("path with spaces");
         File confFile = new File(path, "config-test.properties");
+        addTemporaryFile(confFile);
+        addTemporaryFile(path);
         PrintWriter out = null;
 
         try
         {
-            if (!path.exists())
-            {
-                assertTrue(path.mkdir());
-            }
+            assertTrue(path.mkdir());
             out = new PrintWriter(new FileWriter(confFile));
             out.println("saved = false");
             out.close();
@@ -364,14 +395,6 @@ public class TestFileConfiguration exten
             {
                 out.close();
             }
-            if (confFile.exists())
-            {
-                assertTrue(confFile.delete());
-            }
-            if (path.exists())
-            {
-                assertTrue(path.delete());
-            }
         }
     }
 
@@ -384,6 +407,7 @@ public class TestFileConfiguration exten
         File saveFile = ConfigurationAssert.getOutFile("test+config.properties")
                 .getAbsoluteFile();
         saveFile.createNewFile();
+        tempFiles.add(saveFile);
         try
         {
             FileConfiguration config = new PropertiesConfiguration(saveFile);



Mime
View raw message