commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1462196 - 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 Thu, 28 Mar 2013 16:19:57 GMT
Author: oheger
Date: Thu Mar 28 16:19:57 2013
New Revision: 1462196

URL: http://svn.apache.org/r1462196
Log:
FileHandler now notifies registered listeners about location updates and IO operations.

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=1462196&r1=1462195&r2=1462196&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
Thu Mar 28 16:19:57 2013
@@ -28,6 +28,8 @@ import java.io.UnsupportedEncodingExcept
 import java.io.Writer;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
 
 import org.apache.commons.configuration.ConfigurationException;
 import org.apache.commons.configuration.ConfigurationUtils;
@@ -87,6 +89,10 @@ public class FileHandler
     /** Stores the location of the associated file. */
     private final FileSpec fileSpec;
 
+    /** A collection with the registered listeners. */
+    private final List<FileHandlerListener> listeners =
+            new CopyOnWriteArrayList<FileHandlerListener>();
+
     /**
      * Creates a new instance of {@code FileHandler} which is not associated
      * with a {@code FileBased} object and thus does not have a content. Objects
@@ -141,6 +147,32 @@ public class FileHandler
     }
 
     /**
+     * Adds a listener to this {@code FileHandler}. It is notified about
+     * property changes and IO operations.
+     *
+     * @param l the listener to be added (must not be <b>null</b>)
+     * @throws IllegalArgumentException if the listener is <b>null</b>
+     */
+    public void addFileHandlerListener(FileHandlerListener l)
+    {
+        if (l == null)
+        {
+            throw new IllegalArgumentException("Listener must not be null!");
+        }
+        listeners.add(l);
+    }
+
+    /**
+     * Removes the specified listener from this object.
+     *
+     * @param l the listener to be removed
+     */
+    public void removeFileHandlerListener(FileHandlerListener l)
+    {
+        listeners.remove(l);
+    }
+
+    /**
      * Return the name of the file.
      *
      * @return the file name
@@ -168,6 +200,7 @@ public class FileHandler
             fileSpec.setFileName(name);
             fileSpec.setSourceURL(null);
         }
+        fireLocationChangedEvent();
     }
 
     /**
@@ -205,6 +238,7 @@ public class FileHandler
             fileSpec.setBasePath(path);
             fileSpec.setSourceURL(null);
         }
+        fireLocationChangedEvent();
     }
 
     /**
@@ -259,6 +293,7 @@ public class FileHandler
                     .getParentFile().getAbsolutePath() : null);
             fileSpec.setSourceURL(null);
         }
+        fireLocationChangedEvent();
     }
 
     /**
@@ -335,6 +370,7 @@ public class FileHandler
         {
             initFileSpecWithURL(fileSpec, url);
         }
+        fireLocationChangedEvent();
     }
 
     /**
@@ -390,6 +426,7 @@ public class FileHandler
         {
             fileSpec.setEncoding(encoding);
         }
+        fireLocationChangedEvent();
     }
 
     /**
@@ -423,6 +460,7 @@ public class FileHandler
         {
             fileSpec.setFileSystem(fs);
         }
+        fireLocationChangedEvent();
     }
 
     /**
@@ -801,6 +839,7 @@ public class FileHandler
      */
     private void loadFromReader(Reader in) throws ConfigurationException
     {
+        fireLoadingEvent();
         try
         {
             getContent().read(in);
@@ -809,6 +848,10 @@ public class FileHandler
         {
             throw new ConfigurationException(ioex);
         }
+        finally
+        {
+            fireLoadedEvent();
+        }
     }
 
     /**
@@ -984,6 +1027,7 @@ public class FileHandler
      */
     private void saveToWriter(Writer out) throws ConfigurationException
     {
+        fireSavingEvent();
         try
         {
             getContent().write(out);
@@ -992,6 +1036,10 @@ public class FileHandler
         {
             throw new ConfigurationException(ioex);
         }
+        finally
+        {
+            fireSavedEvent();
+        }
     }
 
     /**
@@ -1039,6 +1087,61 @@ public class FileHandler
     }
 
     /**
+     * Notifies the registered listeners about the start of a load operation.
+     */
+    private void fireLoadingEvent()
+    {
+        for (FileHandlerListener l : listeners)
+        {
+            l.loading(this);
+        }
+    }
+
+    /**
+     * Notifies the registered listeners about a completed load operation.
+     */
+    private void fireLoadedEvent()
+    {
+        for (FileHandlerListener l : listeners)
+        {
+            l.loaded(this);
+        }
+    }
+
+    /**
+     * Notifies the registered listeners about the start of a save operation.
+     */
+    private void fireSavingEvent()
+    {
+        for (FileHandlerListener l : listeners)
+        {
+            l.saving(this);
+        }
+    }
+
+    /**
+     * Notifies the registered listeners about a completed save operation.
+     */
+    private void fireSavedEvent()
+    {
+        for (FileHandlerListener l : listeners)
+        {
+            l.saved(this);
+        }
+    }
+
+    /**
+     * Notifies the registered listeners about a property update.
+     */
+    private void fireLocationChangedEvent()
+    {
+        for (FileHandlerListener l : listeners)
+        {
+            l.locationChanged(this);
+        }
+    }
+
+    /**
      * Initializes a {@code FileSpec} object with a URL. This method ensures
      * that base path and file name are set correctly.
      *

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=1462196&r1=1462195&r2=1462196&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
Thu Mar 28 16:19:57 2013
@@ -950,6 +950,144 @@ public class TestFileHandler
     }
 
     /**
+     * Tries to add a null listener.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testAddFileHandlerListenerNull()
+    {
+        new FileHandler().addFileHandlerListener(null);
+    }
+
+    /**
+     * Tests notifications about load operations.
+     */
+    @Test
+    public void testLoadEvents() throws ConfigurationException
+    {
+        FileHandler handler = new FileHandler(new FileBasedTestImpl());
+        FileHandlerListenerTestImpl listener =
+                new FileHandlerListenerTestImpl(handler);
+        handler.addFileHandlerListener(listener);
+        handler.load(createTestFile());
+        listener.checkMethods("loadingloaded");
+    }
+
+    /**
+     * Tests notifications about save operations.
+     */
+    @Test
+    public void testSaveEvents() throws IOException, ConfigurationException
+    {
+        FileHandler handler = new FileHandler(new FileBasedTestImpl());
+        FileHandlerListenerTestImpl listener =
+                new FileHandlerListenerTestImpl(handler);
+        handler.addFileHandlerListener(listener);
+        File f = folder.newFile();
+        handler.save(f);
+        listener.checkMethods("savingsaved");
+    }
+
+    /**
+     * Tests a notification about a changed file name.
+     */
+    @Test
+    public void testLocationChangedFileName()
+    {
+        FileHandler handler = new FileHandler();
+        FileHandlerListenerTestImpl listener =
+                new FileHandlerListenerTestImpl(handler);
+        handler.addFileHandlerListener(listener);
+        handler.setFileName(TEST_FILENAME);
+        listener.checkMethods("locationChanged");
+    }
+
+    /**
+     * Tests a notification about a changed base path.
+     */
+    @Test
+    public void testLocationChangedBasePath()
+    {
+        FileHandler handler = new FileHandler();
+        FileHandlerListenerTestImpl listener =
+                new FileHandlerListenerTestImpl(handler);
+        handler.addFileHandlerListener(listener);
+        handler.setBasePath(TEST_FILENAME);
+        listener.checkMethods("locationChanged");
+    }
+
+    /**
+     * Tests a notification about a changed file.
+     */
+    @Test
+    public void testLocationChangedFile() throws IOException
+    {
+        FileHandler handler = new FileHandler();
+        FileHandlerListenerTestImpl listener =
+                new FileHandlerListenerTestImpl(handler);
+        handler.addFileHandlerListener(listener);
+        handler.setFile(folder.newFile());
+        listener.checkMethods("locationChanged");
+    }
+
+    /**
+     * Tests a notification about a changed path.
+     */
+    @Test
+    public void testLocationChangedPath()
+    {
+        FileHandler handler = new FileHandler();
+        FileHandlerListenerTestImpl listener =
+                new FileHandlerListenerTestImpl(handler);
+        handler.addFileHandlerListener(listener);
+        handler.setPath(TEST_FILENAME);
+        listener.checkMethods("locationChanged");
+    }
+
+    /**
+     * Tests a notification about a changed file system.
+     */
+    @Test
+    public void testLocationChangedFileSystem()
+    {
+        FileSystem fs = EasyMock.createMock(FileSystem.class);
+        FileHandler handler = new FileHandler();
+        FileHandlerListenerTestImpl listener =
+                new FileHandlerListenerTestImpl(handler);
+        handler.addFileHandlerListener(listener);
+        handler.setFileSystem(fs);
+        listener.checkMethods("locationChanged");
+    }
+
+    /**
+     * Tests a notification about a changed URL.
+     */
+    @Test
+    public void testLocationChangedURL() throws IOException
+    {
+        FileHandler handler = new FileHandler();
+        FileHandlerListenerTestImpl listener =
+                new FileHandlerListenerTestImpl(handler);
+        handler.addFileHandlerListener(listener);
+        URL url = folder.newFile().toURI().toURL();
+        handler.setURL(url);
+        listener.checkMethods("locationChanged");
+    }
+
+    /**
+     * Tests a notification about a changed encoding.
+     */
+    @Test
+    public void testLocationChangedEncoding()
+    {
+        FileHandler handler = new FileHandler();
+        FileHandlerListenerTestImpl listener =
+                new FileHandlerListenerTestImpl(handler);
+        handler.addFileHandlerListener(listener);
+        handler.setEncoding("UTF-8");
+        listener.checkMethods("locationChanged");
+    }
+
+    /**
      * An implementation of the FileBased interface used for test purposes.
      */
     private static class FileBasedTestImpl implements FileBased
@@ -1031,4 +1169,81 @@ public class TestFileHandler
             super.write(out);
         }
     }
+
+    /**
+     * A test listener implementation.
+     */
+    private static class FileHandlerListenerTestImpl extends
+            FileHandlerListenerAdapter
+    {
+        /** The expected file handler. */
+        private final FileHandler expHandler;
+
+        /** A buffer for recording method invocations. */
+        private final StringBuilder methods;
+
+        public FileHandlerListenerTestImpl(FileHandler fh)
+        {
+            expHandler = fh;
+            methods = new StringBuilder();
+        }
+
+        /**
+         * Tests whether the expected listener methods have been called.
+         *
+         * @param expMethods the expected methods as plain string
+         */
+        public void checkMethods(String expMethods)
+        {
+            assertEquals("Wrong listener methods", expMethods,
+                    methods.toString());
+        }
+
+        @Override
+        public void loading(FileHandler handler)
+        {
+            super.loading(handler);
+            methodCalled(handler, "loading");
+        }
+
+        @Override
+        public void loaded(FileHandler handler)
+        {
+            super.loaded(handler);
+            methodCalled(handler, "loaded");
+        }
+
+        @Override
+        public void saving(FileHandler handler)
+        {
+            super.saving(handler);
+            methodCalled(handler, "saving");
+        }
+
+        @Override
+        public void saved(FileHandler handler)
+        {
+            super.saved(handler);
+            methodCalled(handler, "saved");
+        }
+
+        @Override
+        public void locationChanged(FileHandler handler)
+        {
+            super.locationChanged(handler);
+            methodCalled(handler, "locationChanged");
+        }
+
+        /**
+         * One of the listener methods was called. Records this invocation.
+         *
+         * @param handler the file handler
+         * @param method the called method
+         */
+        private void methodCalled(FileHandler handler, String method)
+        {
+            assertEquals("Wrong file handler", expHandler, handler);
+            methods.append(method);
+        }
+    }
 }



Mime
View raw message