You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2014/05/19 21:56:41 UTC
svn commit: r1596021 - in /commons/proper/configuration/trunk/src:
main/java/org/apache/commons/configuration/builder/
test/java/org/apache/commons/configuration/builder/
Author: oheger
Date: Mon May 19 19:56:41 2014
New Revision: 1596021
URL: http://svn.apache.org/r1596021
Log:
Implemented methods for event listeners in BasicConfigurationBuilder.
The builder class now has an EventListenerList as member. This instance manages
the listeners added to the builder.
Added:
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/BuilderEventListenerImpl.java
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ConfigurationBuilderEvent.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicConfigurationBuilder.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicConfigurationBuilderEvents.java
Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java?rev=1596021&r1=1596020&r2=1596021&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/BasicConfigurationBuilder.java Mon May 19 19:56:41 2014
@@ -32,6 +32,7 @@ import org.apache.commons.configuration.
import org.apache.commons.configuration.event.ConfigurationErrorListener;
import org.apache.commons.configuration.event.ConfigurationListener;
import org.apache.commons.configuration.event.EventListener;
+import org.apache.commons.configuration.event.EventListenerList;
import org.apache.commons.configuration.event.EventSource;
import org.apache.commons.configuration.event.EventType;
import org.apache.commons.configuration.ex.ConfigurationException;
@@ -127,6 +128,9 @@ public class BasicConfigurationBuilder<T
/** An object managing the builder listeners registered at this builder. */
private final EventListenerSupport<BuilderListener> builderListeners;
+ /** An object managing the builder listeners registered at this builder. */
+ private final EventListenerList eventListeners;
+
/** A flag whether exceptions on initializing configurations are allowed. */
private final boolean allowFailOnInit;
@@ -193,6 +197,7 @@ public class BasicConfigurationBuilder<T
configListeners = new ArrayList<ConfigurationListener>();
errorListeners = new ArrayList<ConfigurationErrorListener>();
builderListeners = EventListenerSupport.create(BuilderListener.class);
+ eventListeners = new EventListenerList();
updateParameters(params);
}
@@ -386,14 +391,24 @@ public class BasicConfigurationBuilder<T
builderListeners.removeListener(l);
}
+ /**
+ * {@inheritDoc}
+ *
+ * @throws IllegalArgumentException if the event type or the listener is
+ * <b>null</b>
+ */
@Override
- public <T1 extends ConfigurationBuilderEvent> void addEventListener(EventType<T1> eventType, EventListener<? super T1> listener) {
- //TODO implementation
+ public <T1 extends ConfigurationBuilderEvent> void addEventListener(
+ EventType<T1> eventType, EventListener<? super T1> listener)
+ {
+ eventListeners.addEventListener(eventType, listener);
}
@Override
- public <T1 extends ConfigurationBuilderEvent> void removeEventListener(EventType<T1> eventType, EventListener<? super T1> listener) {
- //TODO implementation
+ public <T1 extends ConfigurationBuilderEvent> void removeEventListener(
+ EventType<T1> eventType, EventListener<? super T1> listener)
+ {
+ eventListeners.removeEventListener(eventType, listener);
}
/**
@@ -410,6 +425,8 @@ public class BasicConfigurationBuilder<T
}
builderListeners.fire().builderReset(this);
+ eventListeners.fire(new ConfigurationBuilderEvent(this,
+ ConfigurationBuilderEvent.RESET));
}
/**
Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ConfigurationBuilderEvent.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ConfigurationBuilderEvent.java?rev=1596021&r1=1596020&r2=1596021&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ConfigurationBuilderEvent.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/ConfigurationBuilderEvent.java Mon May 19 19:56:41 2014
@@ -40,6 +40,13 @@ public class ConfigurationBuilderEvent e
new EventType<ConfigurationBuilderEvent>(Event.ANY, "BUILDER");
/**
+ * The specific event type for builder reset events. Events of this type are
+ * generated each time the builder's {@code resetResult()} method is called.
+ */
+ public static final EventType<ConfigurationBuilderEvent> RESET =
+ new EventType<ConfigurationBuilderEvent>(ANY, "RESET");
+
+ /**
* Creates a new instance of {@code ConfigurationBuilderEvent} and sets
* basic properties.
*
Added: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/BuilderEventListenerImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/BuilderEventListenerImpl.java?rev=1596021&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/BuilderEventListenerImpl.java (added)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/BuilderEventListenerImpl.java Mon May 19 19:56:41 2014
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration.builder;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.commons.configuration.event.EventListener;
+import org.apache.commons.configuration.event.EventType;
+
+/**
+ * A test implementation of an event listener for configuration builders. This
+ * class is used by some unit tests. It collects the events received by the
+ * listener and provides some methods for querying them.
+ *
+ * @version $Id$
+ */
+public class BuilderEventListenerImpl implements
+ EventListener<ConfigurationBuilderEvent>
+{
+ /** A list with the received events. */
+ private final List<ConfigurationBuilderEvent> events =
+ new LinkedList<ConfigurationBuilderEvent>();
+
+ /** An iterator for inspecting the received events. */
+ private Iterator<ConfigurationBuilderEvent> iterator;
+
+ /**
+ * {@inheritDoc} This implementation just records the event.
+ */
+ @Override
+ public void onEvent(ConfigurationBuilderEvent event)
+ {
+ events.add(event);
+ }
+
+ /**
+ * Checks whether the next received event is of the specified event type and
+ * returns it. Causes the test to fail if there are no more events or the
+ * next event is of a different event type.
+ *
+ * @param eventType the expected event type
+ * @param <T> the type of the received event
+ * @return the next received event
+ */
+ public <T extends ConfigurationBuilderEvent> T nextEvent(
+ EventType<T> eventType)
+ {
+ Iterator<ConfigurationBuilderEvent> it = initIterator();
+ assertTrue("Too few events received", it.hasNext());
+ ConfigurationBuilderEvent nextEvent = it.next();
+ assertEquals("Wrong event type", eventType, nextEvent.getEventType());
+ // Safe cast because of the comparison of the event type
+ @SuppressWarnings("unchecked")
+ T resultEvent = (T) nextEvent;
+ return resultEvent;
+ }
+
+ /**
+ * Checks that no further events have been received by this listener.
+ */
+ public void assertNoMoreEvents()
+ {
+ assertFalse("Too many events", initIterator().hasNext());
+ }
+
+ /**
+ * Ensures that the iterator for received events has been initialized.
+ *
+ * @return the iterator to be used
+ */
+ private Iterator<ConfigurationBuilderEvent> initIterator()
+ {
+ if (iterator == null)
+ {
+ iterator = events.iterator();
+ }
+ return iterator;
+ }
+}
Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicConfigurationBuilder.java?rev=1596021&r1=1596020&r2=1596021&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicConfigurationBuilder.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicConfigurationBuilder.java Mon May 19 19:56:41 2014
@@ -74,8 +74,7 @@ public class TestBasicConfigurationBuild
@Test(expected = IllegalArgumentException.class)
public void testInitNoClass()
{
- Class<Configuration> cls = null;
- new BasicConfigurationBuilder<Configuration>(cls);
+ new BasicConfigurationBuilder<Configuration>(null);
}
/**
@@ -424,41 +423,6 @@ public class TestBasicConfigurationBuild
}
/**
- * Tries to add a null builder listener.
- */
- @Test(expected = IllegalArgumentException.class)
- public void testAddBuilderListenerNull()
- {
- BasicConfigurationBuilder<PropertiesConfiguration> builder =
- new BasicConfigurationBuilder<PropertiesConfiguration>(
- PropertiesConfiguration.class);
- builder.addBuilderListener(null);
- }
-
- /**
- * Tests whether builder listeners can be added and removed.
- */
- @Test
- public void testAddAndRemoveBuilderListener()
- {
- BuilderListener l1 = EasyMock.createMock(BuilderListener.class);
- BuilderListener l2 = EasyMock.createMock(BuilderListener.class);
- BasicConfigurationBuilder<PropertiesConfiguration> builder =
- new BasicConfigurationBuilder<PropertiesConfiguration>(
- PropertiesConfiguration.class);
- builder.addBuilderListener(l1);
- builder.addBuilderListener(l2);
- l1.builderReset(builder);
- EasyMock.expectLastCall().times(2);
- l2.builderReset(builder);
- EasyMock.replay(l1, l2);
- builder.reset();
- builder.removeBuilderListener(l2);
- builder.resetResult();
- EasyMock.verify(l1, l2);
- }
-
- /**
* Tests whether event listeners can be copied to another builder.
*/
@Test
Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicConfigurationBuilderEvents.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicConfigurationBuilderEvents.java?rev=1596021&r1=1596020&r2=1596021&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicConfigurationBuilderEvents.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestBasicConfigurationBuilderEvents.java Mon May 19 19:56:41 2014
@@ -17,7 +17,9 @@
package org.apache.commons.configuration.builder;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.event.Event;
import org.apache.commons.configuration.event.EventType;
import org.junit.Test;
@@ -41,4 +43,57 @@ public class TestBasicConfigurationBuild
assertEquals("Wrong super type", Event.ANY,
builderEventType.getSuperType());
}
+
+ /**
+ * Tests whether the reset builder event type is correctly configured.
+ */
+ @Test
+ public void testBuilderResetEventType()
+ {
+ EventType<ConfigurationBuilderEvent> builderResetType =
+ ConfigurationBuilderEvent.RESET;
+ assertEquals("Wrong super type", ConfigurationBuilderEvent.ANY,
+ builderResetType.getSuperType());
+ }
+
+ /**
+ * Tests whether builder reset events are correctly distributed.
+ */
+ @Test
+ public void testBuilderResetEvent()
+ {
+ BuilderEventListenerImpl listener = new BuilderEventListenerImpl();
+ BasicConfigurationBuilder<PropertiesConfiguration> builder =
+ new BasicConfigurationBuilder<PropertiesConfiguration>(
+ PropertiesConfiguration.class);
+ builder.addEventListener(ConfigurationBuilderEvent.RESET, listener);
+
+ builder.reset();
+ builder.resetResult();
+ ConfigurationBuilderEvent event =
+ listener.nextEvent(ConfigurationBuilderEvent.RESET);
+ assertSame("Wrong builder (1)", builder, event.getSource());
+ event = listener.nextEvent(ConfigurationBuilderEvent.RESET);
+ assertSame("Wrong builder (2)", builder, event.getSource());
+ listener.assertNoMoreEvents();
+ }
+
+ /**
+ * Tests whether an event listener can be removed again.
+ */
+ @Test
+ public void testRemoveEventListener()
+ {
+ BuilderEventListenerImpl listener = new BuilderEventListenerImpl();
+ BasicConfigurationBuilder<PropertiesConfiguration> builder =
+ new BasicConfigurationBuilder<PropertiesConfiguration>(
+ PropertiesConfiguration.class);
+ builder.addEventListener(ConfigurationBuilderEvent.RESET, listener);
+
+ builder.reset();
+ builder.removeEventListener(ConfigurationBuilderEvent.RESET, listener);
+ builder.resetResult();
+ listener.nextEvent(ConfigurationBuilderEvent.RESET);
+ listener.assertNoMoreEvents();
+ }
}