You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@archiva.apache.org by ol...@apache.org on 2014/05/01 02:50:48 UTC

svn commit: r1591521 - in /archiva/redback/redback-components/trunk/spring-registry: spring-registry-api/src/main/java/org/apache/archiva/redback/components/registry/ spring-registry-commons/src/main/java/org/apache/archiva/redback/components/registry/...

Author: olamy
Date: Thu May  1 00:50:47 2014
New Revision: 1591521

URL: http://svn.apache.org/r1591521
Log:
fix removing registry listeners

Modified:
    archiva/redback/redback-components/trunk/spring-registry/spring-registry-api/src/main/java/org/apache/archiva/redback/components/registry/Registry.java
    archiva/redback/redback-components/trunk/spring-registry/spring-registry-commons/src/main/java/org/apache/archiva/redback/components/registry/commons/CommonsConfigurationRegistry.java
    archiva/redback/redback-components/trunk/spring-registry/spring-registry-commons/src/main/java/org/apache/archiva/redback/components/registry/commons/ConfigurationListenerDelegate.java
    archiva/redback/redback-components/trunk/spring-registry/spring-registry-commons/src/test/java/org/apache/archiva/redback/components/registry/CommonsConfigurationRegistryTest.java

Modified: archiva/redback/redback-components/trunk/spring-registry/spring-registry-api/src/main/java/org/apache/archiva/redback/components/registry/Registry.java
URL: http://svn.apache.org/viewvc/archiva/redback/redback-components/trunk/spring-registry/spring-registry-api/src/main/java/org/apache/archiva/redback/components/registry/Registry.java?rev=1591521&r1=1591520&r2=1591521&view=diff
==============================================================================
--- archiva/redback/redback-components/trunk/spring-registry/spring-registry-api/src/main/java/org/apache/archiva/redback/components/registry/Registry.java (original)
+++ archiva/redback/redback-components/trunk/spring-registry/spring-registry-api/src/main/java/org/apache/archiva/redback/components/registry/Registry.java Thu May  1 00:50:47 2014
@@ -224,6 +224,13 @@ public interface Registry
     void addChangeListener( RegistryListener listener );
 
     /**
+     * @param listener
+     * @return <code>true</code> if has been removed
+     * @since 2.3
+     */
+    boolean removeChangeListener( RegistryListener listener );
+
+    /**
      * Get all the keys in this registry. Keys are only retrieved at a depth of 1.
      *
      * @return the set of keys

Modified: archiva/redback/redback-components/trunk/spring-registry/spring-registry-commons/src/main/java/org/apache/archiva/redback/components/registry/commons/CommonsConfigurationRegistry.java
URL: http://svn.apache.org/viewvc/archiva/redback/redback-components/trunk/spring-registry/spring-registry-commons/src/main/java/org/apache/archiva/redback/components/registry/commons/CommonsConfigurationRegistry.java?rev=1591521&r1=1591520&r2=1591521&view=diff
==============================================================================
--- archiva/redback/redback-components/trunk/spring-registry/spring-registry-commons/src/main/java/org/apache/archiva/redback/components/registry/commons/CommonsConfigurationRegistry.java (original)
+++ archiva/redback/redback-components/trunk/spring-registry/spring-registry-commons/src/main/java/org/apache/archiva/redback/components/registry/commons/CommonsConfigurationRegistry.java Thu May  1 00:50:47 2014
@@ -185,9 +185,26 @@ public class CommonsConfigurationRegistr
 
     public void addChangeListener( RegistryListener listener )
     {
-        EventSource configuration = (EventSource) this.configuration;
+        EventSource eventSource = EventSource.class.cast( this.configuration );
 
-        configuration.addConfigurationListener( new ConfigurationListenerDelegate( listener, this ) );
+        eventSource.addConfigurationListener( new ConfigurationListenerDelegate( listener, this ) );
+    }
+
+    @Override
+    public boolean removeChangeListener( RegistryListener listener )
+    {
+        EventSource eventSource = EventSource.class.cast( this.configuration );
+
+        boolean removed =
+            eventSource.removeConfigurationListener( new ConfigurationListenerDelegate( listener, this ) );
+
+        return removed;
+    }
+
+
+    public int getChangeListenersSize()
+    {
+        return EventSource.class.cast( this.configuration ).getConfigurationListeners().size();
     }
 
     public Collection<String> getKeys()

Modified: archiva/redback/redback-components/trunk/spring-registry/spring-registry-commons/src/main/java/org/apache/archiva/redback/components/registry/commons/ConfigurationListenerDelegate.java
URL: http://svn.apache.org/viewvc/archiva/redback/redback-components/trunk/spring-registry/spring-registry-commons/src/main/java/org/apache/archiva/redback/components/registry/commons/ConfigurationListenerDelegate.java?rev=1591521&r1=1591520&r2=1591521&view=diff
==============================================================================
--- archiva/redback/redback-components/trunk/spring-registry/spring-registry-commons/src/main/java/org/apache/archiva/redback/components/registry/commons/ConfigurationListenerDelegate.java (original)
+++ archiva/redback/redback-components/trunk/spring-registry/spring-registry-commons/src/main/java/org/apache/archiva/redback/components/registry/commons/ConfigurationListenerDelegate.java Thu May  1 00:50:47 2014
@@ -61,4 +61,17 @@ public class ConfigurationListenerDelega
             }
         }
     }
+
+    @Override
+    public int hashCode()
+    {
+        return this.listener.hashCode();
+    }
+
+    @Override
+    public boolean equals( Object obj )
+    {
+        ConfigurationListenerDelegate delegate = ConfigurationListenerDelegate.class.cast( obj );
+        return delegate.listener == this.listener;
+    }
 }

Modified: archiva/redback/redback-components/trunk/spring-registry/spring-registry-commons/src/test/java/org/apache/archiva/redback/components/registry/CommonsConfigurationRegistryTest.java
URL: http://svn.apache.org/viewvc/archiva/redback/redback-components/trunk/spring-registry/spring-registry-commons/src/test/java/org/apache/archiva/redback/components/registry/CommonsConfigurationRegistryTest.java?rev=1591521&r1=1591520&r2=1591521&view=diff
==============================================================================
--- archiva/redback/redback-components/trunk/spring-registry/spring-registry-commons/src/test/java/org/apache/archiva/redback/components/registry/CommonsConfigurationRegistryTest.java (original)
+++ archiva/redback/redback-components/trunk/spring-registry/spring-registry-commons/src/test/java/org/apache/archiva/redback/components/registry/CommonsConfigurationRegistryTest.java Thu May  1 00:50:47 2014
@@ -19,9 +19,12 @@ package org.apache.archiva.redback.compo
  * under the License.
  */
 
+import org.apache.archiva.redback.components.registry.commons.CommonsConfigurationRegistry;
 import org.apache.commons.configuration.XMLConfiguration;
 import org.codehaus.plexus.util.FileUtils;
 import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.io.File;
 import java.util.Arrays;
@@ -38,6 +41,9 @@ public class CommonsConfigurationRegistr
 {
     private Registry registry;
 
+    private final Logger logger = LoggerFactory.getLogger( getClass() );
+
+
     private static final int INT_TEST_VALUE = 8080;
 
     public String getRoleHint()
@@ -49,10 +55,10 @@ public class CommonsConfigurationRegistr
     public void testDefaultConfiguration()
         throws Exception
     {
-        registry = getRegistry ( "default" );
+        registry = getRegistry( "default" );
 
-        assertEquals( "Check system property override", System.getProperty( "user.dir" ), registry
-            .getString( "user.dir" ) );
+        assertEquals( "Check system property override", System.getProperty( "user.dir" ),
+                      registry.getString( "user.dir" ) );
         assertEquals( "Check system property", System.getProperty( "user.home" ), registry.getString( "user.home" ) );
         assertNull( "Check other properties are not loaded", registry.getString( "test.value" ) );
     }
@@ -61,11 +67,11 @@ public class CommonsConfigurationRegistr
     public void testBuilderConfiguration()
         throws Exception
     {
-        registry = getRegistry ( "builder" );
+        registry = getRegistry( "builder" );
 
         assertEquals( "Check system property override", "new user dir", registry.getString( "user.dir" ) );
-        assertEquals( "Check system property default", System.getProperty( "user.home" ), registry
-            .getString( "user.home" ) );
+        assertEquals( "Check system property default", System.getProperty( "user.home" ),
+                      registry.getString( "user.home" ) );
         assertEquals( "Check other properties are loaded", "foo", registry.getString( "test.value" ) );
         assertEquals( "Check other properties are loaded", 1, registry.getInt( "test.number" ) );
         assertTrue( "Check other properties are loaded", registry.getBoolean( "test.boolean" ) );
@@ -75,7 +81,7 @@ public class CommonsConfigurationRegistr
     public void testDump()
         throws Exception
     {
-        registry = getRegistry ( "default" );
+        registry = getRegistry( "default" );
 
         String dump = registry.dump();
         assertTrue( dump.startsWith( "Configuration Dump.\n\"" ) );
@@ -85,7 +91,7 @@ public class CommonsConfigurationRegistr
     public void testDefaults()
         throws Exception
     {
-        registry = getRegistry ( "builder" );
+        registry = getRegistry( "builder" );
 
         assertNull( "Check getString returns null", registry.getString( "foo" ) );
         assertEquals( "Check getString returns default", "bar", registry.getString( "foo", "bar" ) );
@@ -119,7 +125,7 @@ public class CommonsConfigurationRegistr
     public void testInterpolation()
         throws Exception
     {
-        registry = getRegistry ( "builder" );
+        registry = getRegistry( "builder" );
 
         assertEquals( "Check system property interpolation", System.getProperty( "user.home" ) + "/.m2/repository",
                       registry.getString( "repository" ) );
@@ -132,12 +138,12 @@ public class CommonsConfigurationRegistr
     public void testAddConfigurationXmlFile()
         throws Exception
     {
-        registry = getRegistry ( "default" );
+        registry = getRegistry( "default" );
 
         registry.addConfigurationFromFile( new File( "./src/test/resources/org/codehaus/plexus/registry/test.xml" ) );
 
-        assertEquals( "Check system property default", System.getProperty( "user.dir" ), registry
-            .getString( "user.dir" ) );
+        assertEquals( "Check system property default", System.getProperty( "user.dir" ),
+                      registry.getString( "user.dir" ) );
         assertEquals( "Check other properties are loaded", "foo", registry.getString( "test.value" ) );
     }
 
@@ -145,14 +151,13 @@ public class CommonsConfigurationRegistr
     public void testAddConfigurationPropertiesFile()
         throws Exception
     {
-        registry = getRegistry ( "default" );
+        registry = getRegistry( "default" );
 
-        registry
-            .addConfigurationFromFile(
-                new File( "./src/test/resources/org/codehaus/plexus/registry/test.properties" ) );
+        registry.addConfigurationFromFile(
+            new File( "./src/test/resources/org/codehaus/plexus/registry/test.properties" ) );
 
-        assertEquals( "Check system property default", System.getProperty( "user.dir" ), registry
-            .getString( "user.dir" ) );
+        assertEquals( "Check system property default", System.getProperty( "user.dir" ),
+                      registry.getString( "user.dir" ) );
         assertEquals( "Check other properties are loaded", "baz", registry.getString( "foo.bar" ) );
         assertNull( "Check other properties are not loaded", registry.getString( "test.value" ) );
     }
@@ -161,12 +166,12 @@ public class CommonsConfigurationRegistr
     public void testAddConfigurationXmlResource()
         throws Exception
     {
-        registry = getRegistry ( "default" );
+        registry = getRegistry( "default" );
 
         registry.addConfigurationFromResource( "org/codehaus/plexus/registry/test.xml" );
 
-        assertEquals( "Check system property default", System.getProperty( "user.dir" ), registry
-            .getString( "user.dir" ) );
+        assertEquals( "Check system property default", System.getProperty( "user.dir" ),
+                      registry.getString( "user.dir" ) );
         assertEquals( "Check other properties are loaded", "foo", registry.getString( "test.value" ) );
     }
 
@@ -174,12 +179,12 @@ public class CommonsConfigurationRegistr
     public void testAddConfigurationPropertiesResource()
         throws Exception
     {
-        registry = getRegistry ( "default" );
+        registry = getRegistry( "default" );
 
         registry.addConfigurationFromResource( "org/codehaus/plexus/registry/test.properties" );
 
-        assertEquals( "Check system property default", System.getProperty( "user.dir" ), registry
-            .getString( "user.dir" ) );
+        assertEquals( "Check system property default", System.getProperty( "user.dir" ),
+                      registry.getString( "user.dir" ) );
         assertEquals( "Check other properties are loaded", "baz", registry.getString( "foo.bar" ) );
         assertNull( "Check other properties are not loaded", registry.getString( "test.value" ) );
     }
@@ -188,7 +193,7 @@ public class CommonsConfigurationRegistr
     public void testAddConfigurationUnrecognisedType()
         throws Exception
     {
-        registry = getRegistry ( "default" );
+        registry = getRegistry( "default" );
 
         try
         {
@@ -202,8 +207,8 @@ public class CommonsConfigurationRegistr
 
         try
         {
-            registry
-                .addConfigurationFromFile( new File( "./src/test/resources/org/codehaus/plexus/registry/test.foo" ) );
+            registry.addConfigurationFromFile(
+                new File( "./src/test/resources/org/codehaus/plexus/registry/test.foo" ) );
             fail();
         }
         catch ( RegistryException e )
@@ -216,7 +221,7 @@ public class CommonsConfigurationRegistr
     public void testIsEmpty()
         throws Exception
     {
-        registry = getRegistry ( "default" );
+        registry = getRegistry( "default" );
 
         assertFalse( registry.isEmpty() );
         assertTrue( registry.getSubset( "foo" ).isEmpty() );
@@ -226,7 +231,7 @@ public class CommonsConfigurationRegistr
     public void testGetSubset()
         throws Exception
     {
-        registry = getRegistry ( "builder" );
+        registry = getRegistry( "builder" );
 
         Registry registry = this.registry.getSubset( "test" );
         assertEquals( "Check other properties are loaded", "foo", registry.getString( "value" ) );
@@ -238,7 +243,7 @@ public class CommonsConfigurationRegistr
     public void testGetSubsetList()
         throws Exception
     {
-        registry = getRegistry ( "builder" );
+        registry = getRegistry( "builder" );
 
         List list = registry.getSubsetList( "objects.object" );
         assertEquals( 2, list.size() );
@@ -252,7 +257,7 @@ public class CommonsConfigurationRegistr
     public void testGetProperties()
         throws Exception
     {
-        registry = getRegistry ( "builder" );
+        registry = getRegistry( "builder" );
 
         Properties properties = registry.getProperties( "properties" );
         assertEquals( 2, properties.size() );
@@ -264,7 +269,7 @@ public class CommonsConfigurationRegistr
     public void testGetList()
         throws Exception
     {
-        registry = getRegistry ( "builder" );
+        registry = getRegistry( "builder" );
 
         List list = registry.getList( "strings.string" );
         assertEquals( 3, list.size() );
@@ -277,7 +282,7 @@ public class CommonsConfigurationRegistr
     public void testGetSection()
         throws Exception
     {
-        registry = getRegistry ( "builder" );
+        registry = getRegistry( "builder" );
 
         Registry registry = this.registry.getSection( "properties" );
         assertNull( registry.getString( "test.value" ) );
@@ -288,7 +293,7 @@ public class CommonsConfigurationRegistr
     public void testRemoveKey()
         throws Exception
     {
-        registry = getRegistry ( "builder" );
+        registry = getRegistry( "builder" );
 
         Registry registry = this.registry.getSection( "properties" );
         assertEquals( "baz", registry.getString( "foo.bar" ) );
@@ -300,7 +305,7 @@ public class CommonsConfigurationRegistr
     public void testRemoveSubset()
         throws Exception
     {
-        registry = getRegistry ( "builder" );
+        registry = getRegistry( "builder" );
 
         registry.removeSubset( "strings" );
         assertEquals( Collections.EMPTY_LIST, registry.getList( "strings.string" ) );
@@ -330,7 +335,7 @@ public class CommonsConfigurationRegistr
     public void testGetDontForceCreateByName()
         throws Exception
     {
-        registry = getRegistry ( "noForceCreate" );
+        registry = getRegistry( "noForceCreate" );
 
         assertNull( registry.getSection( "foo" ) );
     }
@@ -343,27 +348,63 @@ public class CommonsConfigurationRegistr
         File dest = new File( "./target/test-classes/test-save.xml" );
         FileUtils.copyFile( src, dest );
 
-        registry = getRegistry ( "test-save" );
+        registry = getRegistry( "test-save" );
 
         Registry registry = this.registry.getSection( "org.codehaus.plexus.registry" );
-        assertEquals( "check list elements", Arrays.asList( new String[]{"1", "2", "3"} ),
+        assertEquals( "check list elements", Arrays.asList( new String[]{ "1", "2", "3" } ),
                       registry.getList( "listElements.listElement" ) );
 
         registry.remove( "listElements.listElement(1)" );
         registry.save();
 
-        
-        
         XMLConfiguration configuration = new XMLConfiguration( dest );
-        assertEquals( Arrays.asList( new String[]{"1", "3"} ), configuration.getList( "listElements.listElement" ) );
-        
+        assertEquals( Arrays.asList( new String[]{ "1", "3" } ), configuration.getList( "listElements.listElement" ) );
+
         // file in ${basedir}/target/conf/shared.xml
         Registry section = this.registry.getSection( "org.apache.maven.shared.app.user" );
         section.setString( "foo", "zloug" );
         section.save();
-        
-        configuration = new XMLConfiguration( new File("target/conf/shared.xml") );
+
+        configuration = new XMLConfiguration( new File( "target/conf/shared.xml" ) );
         assertNotNull( configuration.getString( "foo" ) );
-        
+
+    }
+
+
+    @Test
+    public void test_listener()
+        throws Exception
+    {
+        registry = getRegistry( "default" );
+
+        int listenerSize = CommonsConfigurationRegistry.class.cast( registry ).getChangeListenersSize();
+
+        MockChangeListener mockChangeListener = new MockChangeListener();
+
+        registry.addChangeListener( mockChangeListener );
+
+        registry.addChangeListener( new MockChangeListener() );
+
+        assertEquals( listenerSize + 2, CommonsConfigurationRegistry.class.cast( registry ).getChangeListenersSize() );
+
+        registry.removeChangeListener( mockChangeListener );
+
+        assertEquals( listenerSize + 1, CommonsConfigurationRegistry.class.cast( registry ).getChangeListenersSize() );
+    }
+
+    private static class MockChangeListener
+        implements RegistryListener
+    {
+        @Override
+        public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
+        {
+            // no op
+        }
+
+        @Override
+        public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
+        {
+            // no op
+        }
     }
 }