]> source.dussan.org Git - archiva.git/commitdiff
[MRM-451] correct regression in the change listener handling of the default configuration
authorBrett Porter <brett@apache.org>
Sat, 4 Aug 2007 10:11:33 +0000 (10:11 +0000)
committerBrett Porter <brett@apache.org>
Sat, 4 Aug 2007 10:11:33 +0000 (10:11 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@562681 13f79535-47bb-0310-9956-ffa450edef68

archiva-base/archiva-configuration/src/main/java/org/apache/maven/archiva/configuration/DefaultArchivaConfiguration.java
archiva-base/archiva-configuration/src/test/java/org/apache/maven/archiva/configuration/ArchivaConfigurationTest.java

index 540a8653919a280644830403e93adff784b77629..7c007bb6eb921df6668c7179fba3c2e980d6ea87 100644 (file)
@@ -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 )
index 7c6dd36c3c3e0efc6e95ae5ea55a87651a963f75..d3a485b8998afcf39ac175e0efc1f5dac50471b9 100644 (file)
@@ -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(), "<configuration/>" );
 
-        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 ? "<any>" : 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() );