commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1029104 - /commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
Date Sat, 30 Oct 2010 15:58:45 GMT
Author: oheger
Date: Sat Oct 30 15:58:45 2010
New Revision: 1029104

URL: http://svn.apache.org/viewvc?rev=1029104&view=rev
Log:
Improved handling of temporary test files.

Modified:
    commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java

Modified: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java?rev=1029104&r1=1029103&r2=1029104&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
(original)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
Sat Oct 30 15:58:45 2010
@@ -24,7 +24,9 @@ import java.io.FileWriter;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.net.URL;
+import java.util.Collection;
 import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.Properties;
 
 import junit.framework.TestCase;
@@ -50,9 +52,81 @@ public class TestFileConfiguration exten
     /** Constant for a test file.*/
     private static final File TEST_FILE = new File(TEST_DIR, 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 tempFiles = new LinkedList();
+
+    /**
+     * Initializes the test environment. This implementation ensures that the
+     * test output file does not exist.
+     */
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        removeOutFile();
+    }
+
+    /**
+     * Performs cleanup after a test case. This implementation removes temporary
+     * files that have been created.
+     */
+    protected void tearDown() throws Exception
+    {
+        for(Iterator it = tempFiles.iterator(); it.hasNext();)
+        {
+            File file = (File) it.next();
+            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
@@ -103,54 +177,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());
     }
 
     /**
@@ -163,43 +212,31 @@ public class TestFileConfiguration exten
     {
         File dir = new File("conf");
         File file = new File(dir, "testFileConfiguration.properties");
+        addTemporaryFile(file);
 
-        if (file.exists())
-        {
-            assertTrue("File cannot be deleted", file.delete());
-        }
-
-        try
+        ConfigurationFactory factory = new ConfigurationFactory();
+        factory.setConfigurationURL(new File(dir,
+                "testDigesterConfiguration2.xml").toURI().toURL());
+        CompositeConfiguration cc =
+                (CompositeConfiguration) factory.getConfiguration();
+        PropertiesConfiguration config = null;
+        for (int i = 0; config == null; i++)
         {
-            ConfigurationFactory factory = new ConfigurationFactory();
-            factory.setConfigurationURL(new File(dir, "testDigesterConfiguration2.xml").toURI().toURL());
-            CompositeConfiguration cc = (CompositeConfiguration) factory.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.getName());
-            assertTrue(file.exists());
-            config = new PropertiesConfiguration();
-            config.setFile(file);
-            config.load();
+        config.setProperty("test", "yes");
+        config.save(file.getName());
+        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"));
     }
 
     /**
@@ -243,6 +280,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");
@@ -286,10 +324,6 @@ public class TestFileConfiguration exten
                     ioex.printStackTrace();
                 }
             }
-            if (tempFile.exists())
-            {
-                assertTrue(tempFile.delete());
-            }
         }
     }
 
@@ -300,6 +334,7 @@ public class TestFileConfiguration exten
     public void testReloadingWithAutoSave() throws Exception
     {
         File configFile = new File(TARGET_DIR, TEST_FILENAME);
+        addTemporaryFile(configFile);
         PrintWriter out = null;
 
         try
@@ -324,10 +359,6 @@ public class TestFileConfiguration exten
             {
                 out.close();
             }
-            if (configFile.exists())
-            {
-                assertTrue(configFile.delete());
-            }
         }
     }
 
@@ -339,14 +370,13 @@ public class TestFileConfiguration exten
     {
         File path = new File(TARGET_DIR, "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();
@@ -371,14 +401,6 @@ public class TestFileConfiguration exten
             {
                 out.close();
             }
-            if (confFile.exists())
-            {
-                assertTrue(confFile.delete());
-            }
-            if (path.exists())
-            {
-                assertTrue(path.delete());
-            }
         }
     }
 
@@ -388,21 +410,16 @@ public class TestFileConfiguration exten
      */
     public void testPathWithPlus() throws ConfigurationException, IOException
     {
-        File saveFile = new File(TARGET_DIR, "test+config.properties")
-                .getAbsoluteFile();
+        File saveFile =
+                new File(TARGET_DIR, "test+config.properties")
+                        .getAbsoluteFile();
         saveFile.createNewFile();
-        try
-        {
-            FileConfiguration config = new PropertiesConfiguration(saveFile);
-            config.addProperty("test", Boolean.TRUE);
-            config.save();
-            File configFile = config.getFile();
-            assertEquals("Wrong configuration file", saveFile, configFile);
-        }
-        finally
-        {
-            assertTrue("Could not remove test file", saveFile.delete());
-        }
+        tempFiles.add(saveFile);
+        FileConfiguration config = new PropertiesConfiguration(saveFile);
+        config.addProperty("test", Boolean.TRUE);
+        config.save();
+        File configFile = config.getFile();
+        assertEquals("Wrong configuration file", saveFile, configFile);
     }
 
     /**



Mime
View raw message