commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1612684 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/builder/combined/ test/java/org/apache/commons/configuration/builder/combined/
Date Tue, 22 Jul 2014 20:09:12 GMT
Author: oheger
Date: Tue Jul 22 20:09:11 2014
New Revision: 1612684

URL: http://svn.apache.org/r1612684
Log:
Fixed some problems with the handling of events from managed builders.

MultiFileConfigurationBuilder was only handling events of type RESET. Now all
builder event types are supported and are propagated to event listeners
registered at the builder.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileConfigurationBuilder.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestMultiFileConfigurationBuilder.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileConfigurationBuilder.java?rev=1612684&r1=1612683&r2=1612684&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileConfigurationBuilder.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/combined/MultiFileConfigurationBuilder.java
Tue Jul 22 20:09:11 2014
@@ -28,6 +28,7 @@ import org.apache.commons.configuration.
 import org.apache.commons.configuration.builder.BasicConfigurationBuilder;
 import org.apache.commons.configuration.builder.BuilderParameters;
 import org.apache.commons.configuration.builder.ConfigurationBuilderEvent;
+import org.apache.commons.configuration.builder.ConfigurationBuilderResultCreatedEvent;
 import org.apache.commons.configuration.builder.FileBasedConfigurationBuilder;
 import org.apache.commons.configuration.event.Event;
 import org.apache.commons.configuration.event.EventListener;
@@ -114,7 +115,7 @@ public class MultiFileConfigurationBuild
             {
                 @Override
                 public void onEvent(ConfigurationBuilderEvent event) {
-                    resetResult();
+                    handleManagedBuilderEvent(event);
                 }
             };
 
@@ -281,7 +282,7 @@ public class MultiFileConfigurationBuild
     {
         for (FileBasedConfigurationBuilder<T> b : getManagedBuilders().values())
         {
-            b.removeEventListener(ConfigurationBuilderEvent.RESET,
+            b.removeEventListener(ConfigurationBuilderEvent.ANY,
                     managedBuilderDelegationListener);
         }
         getManagedBuilders().clear();
@@ -424,7 +425,7 @@ public class MultiFileConfigurationBuild
     private void initListeners(FileBasedConfigurationBuilder<T> newBuilder)
     {
         copyEventListeners(newBuilder, configurationListeners);
-        newBuilder.addEventListener(ConfigurationBuilderEvent.RESET,
+        newBuilder.addEventListener(ConfigurationBuilderEvent.ANY,
                 managedBuilderDelegationListener);
     }
 
@@ -462,6 +463,54 @@ public class MultiFileConfigurationBuild
     }
 
     /**
+     * Handles events received from managed configuration builders. This method
+     * creates a new event with a source pointing to this builder and propagates
+     * it to all registered listeners.
+     *
+     * @param event the event received from a managed builder
+     */
+    private void handleManagedBuilderEvent(ConfigurationBuilderEvent event)
+    {
+        if (ConfigurationBuilderEvent.RESET.equals(event.getEventType()))
+        {
+            resetResult();
+        }
+        else
+        {
+            fireBuilderEvent(createEventWithChangedSource(event));
+        }
+    }
+
+    /**
+     * Creates a new {@code ConfigurationBuilderEvent} based on the passed in
+     * event, but with the source changed to this builder. This method is called
+     * when an event was received from a managed builder. In this case, the
+     * event has to be passed to the builder listeners registered at this
+     * object, but with the correct source property.
+     *
+     * @param event the event received from a managed builder
+     * @return the event to be propagated
+     */
+    private ConfigurationBuilderEvent createEventWithChangedSource(
+            ConfigurationBuilderEvent event)
+    {
+        if (ConfigurationBuilderResultCreatedEvent.RESULT_CREATED.equals(event
+                .getEventType()))
+        {
+            return new ConfigurationBuilderResultCreatedEvent(this,
+                    ConfigurationBuilderResultCreatedEvent.RESULT_CREATED,
+                    ((ConfigurationBuilderResultCreatedEvent) event)
+                            .getConfiguration());
+        }
+        @SuppressWarnings("unchecked")
+        // This is safe due to the constructor of ConfigurationBuilderEvent
+        EventType<? extends ConfigurationBuilderEvent> type =
+                (EventType<? extends ConfigurationBuilderEvent>) event
+                        .getEventType();
+        return new ConfigurationBuilderEvent(this, type);
+    }
+
+    /**
      * Creates a map with parameters for a new managed configuration builder.
      * This method merges the basic parameters set for this builder with the
      * specific parameters object for managed builders (if provided).

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestMultiFileConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestMultiFileConfigurationBuilder.java?rev=1612684&r1=1612683&r2=1612684&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestMultiFileConfigurationBuilder.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/combined/TestMultiFileConfigurationBuilder.java
Tue Jul 22 20:09:11 2014
@@ -37,6 +37,7 @@ import org.apache.commons.configuration.
 import org.apache.commons.configuration.builder.BuilderEventListenerImpl;
 import org.apache.commons.configuration.builder.BuilderParameters;
 import org.apache.commons.configuration.builder.ConfigurationBuilderEvent;
+import org.apache.commons.configuration.builder.ConfigurationBuilderResultCreatedEvent;
 import org.apache.commons.configuration.builder.FileBasedConfigurationBuilder;
 import org.apache.commons.configuration.builder.XMLBuilderParametersImpl;
 import org.apache.commons.configuration.convert.DefaultListDelimiterHandler;
@@ -346,10 +347,10 @@ public class TestMultiFileConfigurationB
     }
 
     /**
-     * Tests whether builder listeners are handled correctly.
+     * Tests whether builder reset events are handled correctly.
      */
     @Test
-    public void testBuilderListener() throws ConfigurationException
+    public void testBuilderListenerReset() throws ConfigurationException
     {
         BuilderEventListenerImpl listener = new BuilderEventListenerImpl();
         Collection<FileBasedConfigurationBuilder<XMLConfiguration>> managedBuilders
=
@@ -358,11 +359,13 @@ public class TestMultiFileConfigurationB
                 createBuilderWithAccessToManagedBuilders(managedBuilders);
         switchToConfig(1);
         builder.addEventListener(ConfigurationBuilderEvent.RESET, listener);
-        builder.getConfiguration();
+        XMLConfiguration configuration = builder.getConfiguration();
         managedBuilders.iterator().next().resetResult();
         ConfigurationBuilderEvent event =
                 listener.nextEvent(ConfigurationBuilderEvent.RESET);
         assertSame("Wrong event source", builder, event.getSource());
+        assertNotSame("Configuration not reset", configuration,
+                builder.getConfiguration());
     }
 
     /**
@@ -387,6 +390,29 @@ public class TestMultiFileConfigurationB
     }
 
     /**
+     * Tests whether builder events of other types can be received.
+     */
+    @Test
+    public void testBuilderListenerOtherTypes() throws ConfigurationException
+    {
+        BuilderEventListenerImpl listener = new BuilderEventListenerImpl();
+        MultiFileConfigurationBuilder<XMLConfiguration> builder =
+                createTestBuilder(null);
+        builder.addEventListener(ConfigurationBuilderEvent.ANY, listener);
+        switchToConfig(1);
+        builder.getConfiguration();
+        ConfigurationBuilderEvent event =
+                listener.nextEvent(ConfigurationBuilderEvent.CONFIGURATION_REQUEST);
+        assertEquals("Wrong event source of request event", builder,
+                event.getSource());
+        ConfigurationBuilderResultCreatedEvent createdEvent =
+                listener.nextEvent(ConfigurationBuilderResultCreatedEvent.RESULT_CREATED);
+        assertEquals("Wrong source of creation event", builder,
+                createdEvent.getSource());
+        listener.assertNoMoreEvents();
+    }
+
+    /**
      * Tests whether initialization parameters of managed builders are cloned
      * before they are applied.
      */



Mime
View raw message