commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject cvs commit: jakarta-commons/configuration/src/test/org/apache/commons/configuration TestFileConfiguration.java
Date Fri, 19 Nov 2004 13:19:50 GMT
ebourg      2004/11/19 05:19:50

  Modified:    configuration/src/java/org/apache/commons/configuration
                        AbstractFileConfiguration.java
               configuration/xdocs changes.xml
               configuration/src/test/org/apache/commons/configuration
                        TestFileConfiguration.java
  Log:
  Saving a configuration now creates the path to the file if it doesn't exist.
  Constructing a file based configuration with a File no longer throws an exception when the
file doesn't exist.
  AbstractFileConfiguration.save(File) now throws an exception when an error occurs
  
  Revision  Changes    Path
  1.10      +41 -10    jakarta-commons/configuration/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
  
  Index: AbstractFileConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- AbstractFileConfiguration.java	17 Nov 2004 00:18:00 -0000	1.9
  +++ AbstractFileConfiguration.java	19 Nov 2004 13:19:50 -0000	1.10
  @@ -17,7 +17,6 @@
   package org.apache.commons.configuration;
   
   import java.io.File;
  -import java.io.FileNotFoundException;
   import java.io.FileOutputStream;
   import java.io.IOException;
   import java.io.InputStream;
  @@ -27,7 +26,6 @@
   import java.io.Reader;
   import java.io.UnsupportedEncodingException;
   import java.io.Writer;
  -import java.net.MalformedURLException;
   import java.net.URL;
   import java.util.Iterator;
   
  @@ -100,7 +98,10 @@
           setFile(file);
   
           // load the file
  -        load();
  +        if (file.exists())
  +        {
  +            load();
  +        }
       }
   
       /**
  @@ -176,7 +177,11 @@
           {
               load(file.toURL());
           }
  -        catch (MalformedURLException e)
  +        catch (ConfigurationException e)
  +        {
  +            throw e;
  +        }
  +        catch (Exception e)
           {
               throw new ConfigurationException(e.getMessage(), e);
           }
  @@ -198,6 +203,10 @@
               in = url.openStream();
               load(in);
           }
  +        catch (ConfigurationException e)
  +        {
  +            throw e;
  +        }
           catch (Exception e)
           {
               throw new ConfigurationException(e.getMessage(), e);
  @@ -319,8 +328,9 @@
       }
   
       /**
  -     * Save the configuration to the specified file. This doesn't change the
  -     * source of the configuration, use setFile() if you need it.
  +     * Save the configuration to the specified file. The file is created
  +     * automatically if it doesn't exist. This doesn't change the source
  +     * of the configuration, use {@link #setFile} if you need it.
        *
        * @param file
        *
  @@ -332,12 +342,14 @@
   
           try
           {
  +            // create the file if necessary
  +            createPath(file);
               out = new FileOutputStream(file);
               save(out);
           }
  -        catch (FileNotFoundException e)
  +        catch (IOException e)
           {
  -            e.printStackTrace();
  +            throw new ConfigurationException(e.getMessage(), e);
           }
           finally
           {
  @@ -469,7 +481,7 @@
               {
                   setURL(file.toURL());
               }
  -            catch (MalformedURLException e)
  +            catch (IOException e)
               {
                   e.printStackTrace();
               }
  @@ -607,5 +619,24 @@
       {
           reload();
           return super.getKeys();
  +    }
  +
  +    /**
  +     * Create the path to the specified file.
  +     */
  +    private void createPath(File file)
  +    {
  +        if (file != null)
  +        {
  +            // create the path to the file if the file doesn't exist
  +            if (!file.exists())
  +            {
  +                File parent = file.getParentFile();
  +                if (parent != null && !parent.exists())
  +                {
  +                    parent.mkdirs();
  +                }
  +            }
  +        }
       }
   }
  
  
  
  1.71      +12 -1     jakarta-commons/configuration/xdocs/changes.xml
  
  Index: changes.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/xdocs/changes.xml,v
  retrieving revision 1.70
  retrieving revision 1.71
  diff -u -r1.70 -r1.71
  --- changes.xml	19 Nov 2004 01:56:30 -0000	1.70
  +++ changes.xml	19 Nov 2004 13:19:50 -0000	1.71
  @@ -8,8 +8,19 @@
     <body>
   
       <release version="1.1-dev" date="in CVS">
  +      <action dev="ebourg" type="update">
  +        Constructing a file based configuration with a File no longer throws
  +        an exception when the file doesn't exist.
  +      </action>
  +      <action dev="ebourg" type="add">
  +        Saving a configuration now creates the path to the file if it doesn't exist.
  +      </action>
  +      <action dev="ebourg" type="update" issue="32020">
  +        AbstractFileConfiguration.save(File) no longer fails silently when
  +        an error occurs, a ConfigurationException is thrown instead.
  +      </action>
         <action dev="ebourg" type="fix">
  -        ConfigurationUtils.locate() now checks if the URL based ressources exist.
  +        ConfigurationUtils.locate() now checks if the URL based resources exist.
           This fixes a bug preventing configuration files from being found if
           the configuration descriptor is in a JAR file (reported by Grant Ingersoll).
         </action>
  
  
  
  1.2       +54 -1     jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestFileConfiguration.java
  
  Index: TestFileConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestFileConfiguration.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestFileConfiguration.java	4 Oct 2004 21:45:11 -0000	1.1
  +++ TestFileConfiguration.java	19 Nov 2004 13:19:50 -0000	1.2
  @@ -17,6 +17,7 @@
   package org.apache.commons.configuration;
   
   import java.net.URL;
  +import java.io.File;
   
   import junit.framework.TestCase;
   
  @@ -39,5 +40,57 @@
           config.setURL(new URL("file:/temp/test.properties"));
           assertEquals("base path", "/temp/", config.getBasePath());
           assertEquals("file name", "test.properties", config.getFileName());
  +    }
  +
  +    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());
  +
  +        FileConfiguration config = new PropertiesConfiguration(file);
  +        config.save();
  +
  +        assertTrue("The file doesn't exist", 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.save();
  +
  +        assertTrue("The file doesn't exist", 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);
  +
  +        assertTrue("The file doesn't exist", file.exists());
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message