Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D6F0EF8FB for ; Thu, 28 Mar 2013 16:20:23 +0000 (UTC) Received: (qmail 15752 invoked by uid 500); 28 Mar 2013 16:20:23 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 15695 invoked by uid 500); 28 Mar 2013 16:20:23 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 15681 invoked by uid 99); 28 Mar 2013 16:20:23 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 28 Mar 2013 16:20:22 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 28 Mar 2013 16:20:19 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id BA1FE2388847; Thu, 28 Mar 2013 16:19:57 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@commons.apache.org From: oheger@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130328161957.BA1FE2388847@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 listeners = + new CopyOnWriteArrayList(); + /** * 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 null) + * @throws IllegalArgumentException if the listener is null + */ + 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); + } + } }