commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1395165 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/io/FileHandler.java test/java/org/apache/commons/configuration/io/TestFileHandler.java
Date Sat, 06 Oct 2012 20:18:31 GMT
Author: oheger
Date: Sat Oct  6 20:18:28 2012
New Revision: 1395165

URL: http://svn.apache.org/viewvc?rev=1395165&view=rev
Log:
Added some location-related methods to FileHandler.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileHandler.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java?rev=1395165&r1=1395164&r2=1395165&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java
Sat Oct  6 20:18:28 2012
@@ -342,6 +342,33 @@ public class FileHandler
     }
 
     /**
+     * Tests whether a location is defined for this {@code FileHandler}.
+     *
+     * @return <b>true</b> if a location is defined, <b>false</b>
otherwise
+     */
+    public boolean isLocationDefined()
+    {
+        synchronized (fileSpec)
+        {
+            return fileSpec.getFileName() != null;
+        }
+    }
+
+    /**
+     * Clears the location of this {@code FileHandler}. Afterwards this handler
+     * does not point to any valid file.
+     */
+    public void clearLocation()
+    {
+        synchronized (fileSpec)
+        {
+            fileSpec.setBasePath(null);
+            fileSpec.setFileName(null);
+            fileSpec.setSourceURL(null);
+        }
+    }
+
+    /**
      * Returns the encoding of the associated file. Result can be <b>null</b>
if
      * no encoding has been set.
      *

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileHandler.java?rev=1395165&r1=1395164&r2=1395165&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileHandler.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileHandler.java
Sat Oct  6 20:18:28 2012
@@ -17,6 +17,7 @@
 package org.apache.commons.configuration.io;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
@@ -758,6 +759,87 @@ public class TestFileHandler
     }
 
     /**
+     * Tests isLocationDefined() if a File has been set.
+     */
+    @Test
+    public void testIsLocationDefinedFile()
+    {
+        FileHandler handler = new FileHandler();
+        handler.setFile(createTestFile());
+        assertTrue("Location not defined", handler.isLocationDefined());
+    }
+
+    /**
+     * Tests isLocationDefined() if a URL has been set.
+     */
+    @Test
+    public void testIsLocationDefinedURL() throws IOException
+    {
+        FileHandler handler = new FileHandler();
+        handler.setURL(createTestFile().toURI().toURL());
+        assertTrue("Location not defined", handler.isLocationDefined());
+    }
+
+    /**
+     * Tests isLocationDefined() if a path has been set.
+     */
+    @Test
+    public void testIsLocationDefinedPath()
+    {
+        FileHandler handler = new FileHandler();
+        handler.setPath(createTestFile().getAbsolutePath());
+        assertTrue("Location not defined", handler.isLocationDefined());
+    }
+
+    /**
+     * Tests isLocationDefined() if a file name has been set.
+     */
+    @Test
+    public void testIsLocationDefinedFileName()
+    {
+        FileHandler handler = new FileHandler();
+        handler.setFileName(createTestFile().getName());
+        assertTrue("Location not defined", handler.isLocationDefined());
+    }
+
+    /**
+     * Tests whether an undefined location can be queried.
+     */
+    @Test
+    public void testIsLocationDefinedFalse()
+    {
+        FileHandler handler = new FileHandler();
+        assertFalse("Location defined", handler.isLocationDefined());
+    }
+
+    /**
+     * Tests isLocationDefined() if only a base path is set.
+     */
+    @Test
+    public void testIsLocationDefinedBasePathOnly()
+    {
+        FileHandler handler = new FileHandler();
+        handler.setBasePath(createTestFile().getParent());
+        assertFalse("Location defined", handler.isLocationDefined());
+    }
+
+    /**
+     * Tests whether the location can be cleared.
+     */
+    @Test
+    public void testClearLocation()
+    {
+        FileHandler handler = new FileHandler();
+        handler.setFile(createTestFile());
+        handler.clearLocation();
+        assertFalse("Location defined", handler.isLocationDefined());
+        assertNull("Got a file", handler.getFile());
+        assertNull("Got a URL", handler.getURL());
+        assertNull("Got a base path", handler.getBasePath());
+        assertNull("Got a path", handler.getPath());
+    }
+
+    /**
      * An implementation of the FileBased interface used for test purposes.
      */
     private static class FileBasedTestImpl implements FileBased



Mime
View raw message