From: Brett Porter Date: Sat, 4 Aug 2007 10:11:33 +0000 (+0000) Subject: [MRM-451] correct regression in the change listener handling of the default configuration X-Git-Tag: 1.0-beta-1~10 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=498d33c645a03a5132d73ae8cf61c3998f550277;p=archiva.git [MRM-451] correct regression in the change listener handling of the default configuration git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@562681 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/archiva-base/archiva-configuration/src/main/java/org/apache/maven/archiva/configuration/DefaultArchivaConfiguration.java b/archiva-base/archiva-configuration/src/main/java/org/apache/maven/archiva/configuration/DefaultArchivaConfiguration.java index 540a86539..7c007bb6e 100644 --- a/archiva-base/archiva-configuration/src/main/java/org/apache/maven/archiva/configuration/DefaultArchivaConfiguration.java +++ b/archiva-base/archiva-configuration/src/main/java/org/apache/maven/archiva/configuration/DefaultArchivaConfiguration.java @@ -33,6 +33,8 @@ import org.codehaus.plexus.util.StringUtils; import java.io.File; import java.io.IOException; import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; /** * Implementation of configuration holder that retrieves it from the registry. @@ -77,9 +79,14 @@ public class DefaultArchivaConfiguration */ private String userConfigFilename; + /** + * Listeners we've registered. + */ + private List listeners = new LinkedList(); + public String getFilteredUserConfigFilename() { - return StringUtils.replace( userConfigFilename, "${user.home}", System.getProperty( "user.home" + "" ) ); + return StringUtils.replace( userConfigFilename, "${user.home}", System.getProperty( "user.home" ) ); } public synchronized Configuration getConfiguration() @@ -176,6 +183,13 @@ public class DefaultArchivaConfiguration try { ( (Initializable) registry ).initialize(); + + for ( Iterator i = listeners.iterator(); i.hasNext(); ) + { + RegistryListener l = (RegistryListener) i.next(); + + addRegistryChangeListener( l ); + } } catch ( InitializationException e ) { @@ -186,6 +200,14 @@ public class DefaultArchivaConfiguration } public void addChangeListener( RegistryListener listener ) + { + addRegistryChangeListener( listener ); + + // keep track for later + listeners.add( listener ); + } + + private void addRegistryChangeListener( RegistryListener listener ) { Registry section = registry.getSection( KEY + ".user" ); if ( section != null ) diff --git a/archiva-base/archiva-configuration/src/test/java/org/apache/maven/archiva/configuration/ArchivaConfigurationTest.java b/archiva-base/archiva-configuration/src/test/java/org/apache/maven/archiva/configuration/ArchivaConfigurationTest.java index 7c6dd36c3..d3a485b89 100644 --- a/archiva-base/archiva-configuration/src/test/java/org/apache/maven/archiva/configuration/ArchivaConfigurationTest.java +++ b/archiva-base/archiva-configuration/src/test/java/org/apache/maven/archiva/configuration/ArchivaConfigurationTest.java @@ -20,7 +20,11 @@ package org.apache.maven.archiva.configuration; */ import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.registry.Registry; +import org.codehaus.plexus.registry.RegistryListener; import org.codehaus.plexus.util.FileUtils; +import org.easymock.AbstractMatcher; +import org.easymock.MockControl; import java.io.File; import java.util.List; @@ -33,6 +37,16 @@ import java.util.List; public class ArchivaConfigurationTest extends PlexusTestCase { + private Registry registry; + + protected void setUp() + throws Exception + { + super.setUp(); + + registry = (Registry) lookup( Registry.ROLE, "commons-configuration" ); + } + public void testGetConfigurationFromRegistryWithASingleNamedConfigurationResource() throws Exception { @@ -162,8 +176,8 @@ public class ArchivaConfigurationTest file.getParentFile().mkdirs(); FileUtils.fileWrite( file.getAbsolutePath(), "" ); - ArchivaConfiguration archivaConfiguration = - (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-save" ); + DefaultArchivaConfiguration archivaConfiguration = + (DefaultArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-save" ); Configuration configuration = new Configuration(); configuration.setVersion( "1" ); @@ -171,8 +185,23 @@ public class ArchivaConfigurationTest configuration.getWebapp().setUi( new UserInterfaceOptions() ); configuration.getWebapp().getUi().setAppletFindEnabled( false ); + // add a change listener + MockControl control = createRegistryListenerMockControl(); + RegistryListener listener = (RegistryListener) control.getMock(); + archivaConfiguration.addChangeListener( listener ); + + listener.beforeConfigurationChange( registry, "version", "1" ); + listener.beforeConfigurationChange( registry, "webapp.ui.appletFindEnabled", Boolean.FALSE ); + + listener.afterConfigurationChange( registry, "version", "1" ); + listener.afterConfigurationChange( registry, "webapp.ui.appletFindEnabled", Boolean.FALSE ); + + control.replay(); + archivaConfiguration.save( configuration ); + control.verify(); + assertTrue( "Check file exists", file.exists() ); // check it @@ -180,11 +209,30 @@ public class ArchivaConfigurationTest assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() ); // read it back - archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-read-saved" ); + archivaConfiguration = + (DefaultArchivaConfiguration) lookup( ArchivaConfiguration.class.getName(), "test-read-saved" ); configuration = archivaConfiguration.getConfiguration(); assertFalse( "check value", configuration.getWebapp().getUi().isAppletFindEnabled() ); } + private static MockControl createRegistryListenerMockControl() + { + MockControl control = MockControl.createControl( RegistryListener.class ); + control.setDefaultMatcher( new AbstractMatcher() + { + protected boolean argumentMatches( Object object, Object object1 ) + { + return object instanceof Registry || super.argumentMatches( object, object1 ); + } + + protected String argumentToString( Object object ) + { + return object instanceof Registry ? "" : super.argumentToString( object ); + } + } ); + return control; + } + public void testStoreConfigurationUser() throws Exception { @@ -236,8 +284,20 @@ public class ArchivaConfigurationTest configuration.getWebapp().setUi( new UserInterfaceOptions() ); configuration.getWebapp().getUi().setAppletFindEnabled( false ); + // add a change listener + MockControl control = createRegistryListenerMockControl(); + RegistryListener listener = (RegistryListener) control.getMock(); + archivaConfiguration.addChangeListener( listener ); + + listener.beforeConfigurationChange( registry, "webapp.ui.appletFindEnabled", Boolean.FALSE ); + listener.afterConfigurationChange( registry, "webapp.ui.appletFindEnabled", Boolean.FALSE ); + + control.replay(); + archivaConfiguration.save( configuration ); + control.verify(); + assertTrue( "Check file exists", userFile.exists() ); assertFalse( "Check file not created", baseFile.exists() );