commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r394530 - in /jakarta/commons/proper/configuration/trunk: ./ src/test/org/apache/commons/configuration/event/
Date Sun, 16 Apr 2006 17:56:50 GMT
Author: oheger
Date: Sun Apr 16 10:56:48 2006
New Revision: 394530

URL: http://svn.apache.org/viewcvs?rev=394530&view=rev
Log:
Test classes for event listener support

Added:
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/AbstractTestConfigurationEvents.java
  (with props)
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/AbstractTestFileConfigurationEvents.java
  (with props)
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestEventSource.java
  (with props)
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestHierarchicalConfigurationEvents.java
  (with props)
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestMapConfigurationEvents.java
  (with props)
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestPropertiesConfigurationEvents.java
  (with props)
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestXMLConfigurationEvents.java
  (with props)
Modified:
    jakarta/commons/proper/configuration/trunk/build.xml
    jakarta/commons/proper/configuration/trunk/project.xml

Modified: jakarta/commons/proper/configuration/trunk/build.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/build.xml?rev=394530&r1=394529&r2=394530&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/build.xml (original)
+++ jakarta/commons/proper/configuration/trunk/build.xml Sun Apr 16 10:56:48 2006
@@ -148,6 +148,8 @@
           <exclude name="**/XPathTest.java">
           </exclude>
           <exclude name="**/AbstractCombinerTest.java"/>
+          <exclude name="**/AbstractTestConfigurationEvents.java"/>
+          <exclude name="**/AbstractTestFileConfigurationEvents.java"/>
         </fileset>
       </batchtest>
     </junit>

Modified: jakarta/commons/proper/configuration/trunk/project.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/project.xml?rev=394530&r1=394529&r2=394530&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/project.xml (original)
+++ jakarta/commons/proper/configuration/trunk/project.xml Sun Apr 16 10:56:48 2006
@@ -431,6 +431,8 @@
         <exclude>**/TestAbstractConfiguration.java</exclude>
         <exclude>**/XPathTest.java</exclude>
         <exclude>**/AbstractCombinerTest.java</exclude>
+        <exclude>**/AbstractTestConfigurationEvents.java</exclude>
+        <exclude>**/AbstractTestFileConfigurationEvents.java</exclude>
       </excludes>
       <resources>
         <resource>

Added: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/AbstractTestConfigurationEvents.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/AbstractTestConfigurationEvents.java?rev=394530&view=auto
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/AbstractTestConfigurationEvents.java
(added)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/AbstractTestConfigurationEvents.java
Sun Apr 16 10:56:48 2006
@@ -0,0 +1,255 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.event;
+
+import java.util.LinkedList;
+
+import org.apache.commons.configuration.AbstractConfiguration;
+import org.apache.commons.configuration.event.ConfigurationEvent;
+import org.apache.commons.configuration.event.ConfigurationListener;
+
+import junit.framework.TestCase;
+
+/**
+ * Base class for testing events generated by configuration classes derived from
+ * AbstractConfiguration. This class implements a couple of tests related to
+ * event generation. Concrete sub classes only have to implement the
+ * <code>createConfiguration()</code> method for creating an instance of a
+ * specific configuration class. Because tests for detail events depend on a
+ * concrete implementation an exact sequence of events cannot be checked.
+ * Instead the corresponding test methods check whether the enclosing events
+ * (not the detail events) are of the expected type.
+ *
+ * @version $Id$
+ */
+public abstract class AbstractTestConfigurationEvents extends TestCase
+{
+    /** Constant for a test property name. */
+    static final String TEST_PROPNAME = "event.test";
+
+    /** Constant for a test property value. */
+    static final String TEST_PROPVALUE = "a value";
+
+    /** Constant for an existing property. */
+    static final String EXIST_PROPERTY = "event.property";
+
+    /** The configuration to be tested. */
+    protected AbstractConfiguration config;
+
+    /** A test event listener. */
+    protected TestConfigurationListener l;
+
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        config = createConfiguration();
+        config.addProperty(EXIST_PROPERTY, "existing value");
+        l = new TestConfigurationListener();
+        config.addConfigurationListener(l);
+    }
+
+    /**
+     * Creates the configuration instance to be tested.
+     *
+     * @return the configuration instance under test
+     */
+    protected abstract AbstractConfiguration createConfiguration();
+
+    /**
+     * Tests events generated by addProperty().
+     */
+    public void testAddPropertyEvent()
+    {
+        config.addProperty(TEST_PROPNAME, TEST_PROPVALUE);
+        l.checkEvent(AbstractConfiguration.EVENT_ADD_PROPERTY, TEST_PROPNAME,
+                TEST_PROPVALUE, true);
+        l.checkEvent(AbstractConfiguration.EVENT_ADD_PROPERTY, TEST_PROPNAME,
+                TEST_PROPVALUE, false);
+        l.done();
+    }
+
+    /**
+     * Tests events generated by addProperty() when detail events are enabled.
+     */
+    public void testAddPropertyEventWithDetails()
+    {
+        config.setDetailEvents(true);
+        config.addProperty(TEST_PROPNAME, TEST_PROPVALUE);
+        l.checkEventCount(2);
+        l.checkEvent(AbstractConfiguration.EVENT_ADD_PROPERTY, TEST_PROPNAME,
+                TEST_PROPVALUE, true);
+        l.skipToLast(AbstractConfiguration.EVENT_ADD_PROPERTY);
+        l.checkEvent(AbstractConfiguration.EVENT_ADD_PROPERTY, TEST_PROPNAME,
+                TEST_PROPVALUE, false);
+        l.done();
+    }
+
+    /**
+     * Tests events generated by clearProperty().
+     */
+    public void testClearPropertyEvent()
+    {
+        config.clearProperty(EXIST_PROPERTY);
+        l.checkEvent(AbstractConfiguration.EVENT_CLEAR_PROPERTY,
+                EXIST_PROPERTY, null, true);
+        l.checkEvent(AbstractConfiguration.EVENT_CLEAR_PROPERTY,
+                EXIST_PROPERTY, null, false);
+        l.done();
+    }
+
+    /**
+     * Tests events generated by clearProperty() when detail events are enabled.
+     */
+    public void testClearPropertyEventWithDetails()
+    {
+        config.setDetailEvents(true);
+        config.clearProperty(EXIST_PROPERTY);
+        l.checkEventCount(2);
+        l.checkEvent(AbstractConfiguration.EVENT_CLEAR_PROPERTY,
+                EXIST_PROPERTY, null, true);
+        l.skipToLast(AbstractConfiguration.EVENT_CLEAR_PROPERTY);
+        l.checkEvent(AbstractConfiguration.EVENT_CLEAR_PROPERTY,
+                EXIST_PROPERTY, null, false);
+        l.done();
+    }
+
+    /**
+     * Tests events generated by setProperty().
+     */
+    public void testSetPropertyEvent()
+    {
+        config.setProperty(EXIST_PROPERTY, TEST_PROPVALUE);
+        l.checkEvent(AbstractConfiguration.EVENT_SET_PROPERTY, EXIST_PROPERTY,
+                TEST_PROPVALUE, true);
+        l.checkEvent(AbstractConfiguration.EVENT_SET_PROPERTY, EXIST_PROPERTY,
+                TEST_PROPVALUE, false);
+        l.done();
+    }
+
+    /**
+     * Tests events generated by setProperty() when detail events are enabled.
+     */
+    public void testSetPropertyEventWithDetails()
+    {
+        config.setDetailEvents(true);
+        config.setProperty(EXIST_PROPERTY, TEST_PROPVALUE);
+        l.checkEventCount(2);
+        l.checkEvent(AbstractConfiguration.EVENT_SET_PROPERTY, EXIST_PROPERTY,
+                TEST_PROPVALUE, true);
+        l.skipToLast(AbstractConfiguration.EVENT_SET_PROPERTY);
+        l.checkEvent(AbstractConfiguration.EVENT_SET_PROPERTY, EXIST_PROPERTY,
+                TEST_PROPVALUE, false);
+        l.done();
+    }
+
+    /**
+     * Tests the events generated by the clear() method.
+     */
+    public void testClearEvent()
+    {
+        config.clear();
+        l.checkEvent(AbstractConfiguration.EVENT_CLEAR, null, null, true);
+        l.checkEvent(AbstractConfiguration.EVENT_CLEAR, null, null, false);
+        l.done();
+    }
+
+    /**
+     * Tests the events generated by the clear method when detail events are
+     * enabled.
+     */
+    public void testClearEventWithDetails()
+    {
+        config.setDetailEvents(true);
+        config.clear();
+        l.checkEventCount(2);
+        l.checkEvent(AbstractConfiguration.EVENT_CLEAR, null, null, true);
+        l.skipToLast(AbstractConfiguration.EVENT_CLEAR);
+        l.checkEvent(AbstractConfiguration.EVENT_CLEAR, null, null, false);
+        l.done();
+    }
+
+    /**
+     * A test event listener class used for testing events generated by the
+     * configuration.
+     */
+    class TestConfigurationListener implements ConfigurationListener
+    {
+        /** Stores the received events. */
+        private LinkedList events = new LinkedList();
+
+        public void configurationChanged(ConfigurationEvent event)
+        {
+            events.add(event);
+        }
+
+        /**
+         * Checks if at least <code>minEvents</code> events have been
+         * received.
+         *
+         * @param minEvents the minimum number of expected events
+         */
+        public void checkEventCount(int minEvents)
+        {
+            assertTrue("Too view events received", events.size() >= minEvents);
+        }
+
+        /**
+         * Checks an expected event.
+         *
+         * @param type the event type
+         * @param propName the expected property name
+         * @param propValue the expected property value
+         * @param before the expected before flag
+         */
+        public void checkEvent(int type, String propName, Object propValue,
+                boolean before)
+        {
+            assertFalse("Too few events received", events.isEmpty());
+            ConfigurationEvent e = (ConfigurationEvent) events.removeFirst();
+            assertEquals("Wrong event source", config, e.getSource());
+            assertEquals("Wrong event type", type, e.getType());
+            assertEquals("Wrong property name", propName, e.getPropertyName());
+            assertEquals("Wrong property value", propValue, e
+                    .getPropertyValue());
+            assertEquals("Wrong before flag", before, e.isBeforeUpdate());
+        }
+
+        /**
+         * Skips to the last received event and checks that no events of the
+         * given type have been received. This method is used by checks for
+         * detail events to ignore the detail events.
+         *
+         * @param type the event type
+         */
+        public void skipToLast(int type)
+        {
+            while (events.size() > 1)
+            {
+                ConfigurationEvent e = (ConfigurationEvent) events
+                        .removeFirst();
+                assertTrue("Found end event in details", type != e.getType());
+            }
+        }
+
+        /**
+         * Checks if all events has been processed.
+         */
+        public void done()
+        {
+            assertTrue("Too many events received", events.isEmpty());
+        }
+    }
+}

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/AbstractTestConfigurationEvents.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/AbstractTestConfigurationEvents.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/AbstractTestConfigurationEvents.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/AbstractTestFileConfigurationEvents.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/AbstractTestFileConfigurationEvents.java?rev=394530&view=auto
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/AbstractTestFileConfigurationEvents.java
(added)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/AbstractTestFileConfigurationEvents.java
Sun Apr 16 10:56:48 2006
@@ -0,0 +1,143 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.event;
+
+import java.io.IOException;
+import java.net.URL;
+
+import org.apache.commons.configuration.AbstractFileConfiguration;
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.FileConfiguration;
+import org.apache.commons.configuration.reloading.ReloadingStrategy;
+
+/**
+ * A base test class that can be used for testing file-based configurations.
+ * This class tests reload events, too.
+ *
+ * @version $Id$
+ */
+public abstract class AbstractTestFileConfigurationEvents extends
+        AbstractTestConfigurationEvents
+{
+    /**
+     * Initializes the file configuration for the tests.
+     *
+     * @throws ConfigurationException if an error occurs
+     */
+    protected void setUpFileConfiguration() throws ConfigurationException,
+            IOException
+    {
+        FileConfiguration fc = (FileConfiguration) config;
+        fc.setReloadingStrategy(new AlwaysReloadingStrategy());
+        fc.setURL(getSourceURL());
+
+        // deregister event listener before load because load will cause
+        // other events being generated
+        config.removeConfigurationListener(l);
+        fc.load();
+        config.addConfigurationListener(l);
+    }
+
+    /**
+     * Returns the URL of the file to be loaded. Must be implemented in concrete
+     * test classes.
+     *
+     * @return the URL of the file-based configuration
+     * @throws IOException if an error occurs
+     */
+    protected abstract URL getSourceURL() throws IOException;
+
+    /**
+     * Tests events generated by the reload() method.
+     */
+    public void testReloadEvent() throws ConfigurationException, IOException
+    {
+        setUpFileConfiguration();
+        config.isEmpty(); // This should cause a reload
+        l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
+                getSourceURL(), true);
+        l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
+                getSourceURL(), false);
+        l.done();
+    }
+
+    /**
+     * Tests events generated by the reload() method when detail events are
+     * enabled.
+     */
+    public void testReloadEventWithDetails() throws ConfigurationException,
+            IOException
+    {
+        setUpFileConfiguration();
+        config.setDetailEvents(true);
+        config.isEmpty(); // This should cause a reload
+        l.checkEventCount(2);
+        l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
+                getSourceURL(), true);
+        l.skipToLast(AbstractFileConfiguration.EVENT_RELOAD);
+        l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
+                getSourceURL(), false);
+        l.done();
+    }
+
+    /**
+     * Tests accessing a property during a reload event to ensure that no
+     * infinite loops are possible.
+     */
+    public void testAccessPropertiesOnReload() throws ConfigurationException,
+            IOException
+    {
+        setUpFileConfiguration();
+        config.addConfigurationListener(new ConfigurationListener()
+        {
+            public void configurationChanged(ConfigurationEvent event)
+            {
+                config.getString("test");
+            }
+        });
+        config.isEmpty();
+        l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
+                getSourceURL(), true);
+        l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
+                getSourceURL(), false);
+        l.done();
+    }
+
+    /**
+     * A dummy implementation of the ReloadingStrategy interface. This
+     * implementation will always indicate that a reload should be performed. So
+     * it can be used for testing reloading events.
+     */
+    static class AlwaysReloadingStrategy implements ReloadingStrategy
+    {
+        public void setConfiguration(FileConfiguration configuration)
+        {
+        }
+
+        public void init()
+        {
+        }
+
+        public boolean reloadingRequired()
+        {
+            return true;
+        }
+
+        public void reloadingPerformed()
+        {
+        }
+    }
+}

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/AbstractTestFileConfigurationEvents.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/AbstractTestFileConfigurationEvents.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/AbstractTestFileConfigurationEvents.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestEventSource.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestEventSource.java?rev=394530&view=auto
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestEventSource.java
(added)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestEventSource.java
Sun Apr 16 10:56:48 2006
@@ -0,0 +1,246 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.event;
+
+import java.util.Collection;
+
+import junit.framework.TestCase;
+
+/**
+ * Test class for EventSource.
+ *
+ * @version $Id$
+ */
+public class TestEventSource extends TestCase
+{
+    /** Constant for the event type used for testing. */
+    static final int TEST_TYPE = 42;
+
+    /** Constant for the event property name. */
+    static final String TEST_PROPNAME = "test.property.name";
+
+    /** Constant for the event property value. */
+    static final Object TEST_PROPVALUE = "a test property value";
+
+    /** The object under test. */
+    CountingEventSource source;
+
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        source = new CountingEventSource();
+    }
+
+    /**
+     * Tests a newly created source object.
+     */
+    public void testInit()
+    {
+        assertTrue("Listeners list is not empty", source
+                .getConfigurationListeners().isEmpty());
+        assertFalse("Removing listener", source
+                .removeConfigurationListener(new TestListener()));
+        assertFalse("Detail events are enabled", source.isDetailEvents());
+    }
+
+    /**
+     * Tests registering a new listener.
+     */
+    public void testAddConfigurationListener()
+    {
+        TestListener l = new TestListener();
+        source.addConfigurationListener(l);
+        Collection listeners = source.getConfigurationListeners();
+        assertEquals("Wrong number of listeners", 1, listeners.size());
+        assertTrue("Listener not in list", listeners.contains(l));
+    }
+
+    /**
+     * Tests adding an undefined configuration listener. This should cause an
+     * exception.
+     */
+    public void testAddNullConfigurationListener()
+    {
+        try
+        {
+            source.addConfigurationListener(null);
+            fail("Could add null listener!");
+        }
+        catch (IllegalArgumentException iex)
+        {
+            // ok
+        }
+    }
+
+    /**
+     * Tests removing a listener.
+     */
+    public void testRemoveConfigurationListener()
+    {
+        TestListener l = new TestListener();
+        assertFalse("Listener can be removed?", source
+                .removeConfigurationListener(l));
+        source.addConfigurationListener(l);
+        source.addConfigurationListener(new TestListener());
+        assertFalse("Unknown listener can be removed", source
+                .removeConfigurationListener(new TestListener()));
+        assertTrue("Could not remove listener", source
+                .removeConfigurationListener(l));
+        assertFalse("Listener still in list", source
+                .getConfigurationListeners().contains(l));
+    }
+
+    /**
+     * Tests if a null listener can be removed. This should be a no-op.
+     */
+    public void testRemoveNullConfigurationListener()
+    {
+        source.addConfigurationListener(new TestListener());
+        assertFalse("Null listener can be removed", source
+                .removeConfigurationListener(null));
+        assertEquals("Listener list was modified", 1, source
+                .getConfigurationListeners().size());
+    }
+
+    /**
+     * Tests whether the listeners list is read only.
+     */
+    public void testGetConfigurationListenersUpdate()
+    {
+        source.addConfigurationListener(new TestListener());
+        Collection list = source.getConfigurationListeners();
+        try
+        {
+            list.add("test");
+            fail("Could manipulate list!");
+        }
+        catch (Exception ex)
+        {
+            // ok
+        }
+    }
+
+    /**
+     * Tests enabling and disabling the detail events flag.
+     */
+    public void testSetDetailEvents()
+    {
+        source.setDetailEvents(true);
+        assertTrue("Detail events are disabled", source.isDetailEvents());
+        source.setDetailEvents(true);
+        source.setDetailEvents(false);
+        assertTrue("Detail events are disabled again", source.isDetailEvents());
+        source.setDetailEvents(false);
+        assertFalse("Detail events are still enabled", source.isDetailEvents());
+    }
+
+    /**
+     * Tests delivering an event to a listener.
+     */
+    public void testFireEvent()
+    {
+        TestListener l = new TestListener();
+        source.addConfigurationListener(l);
+        source.fireEvent(TEST_TYPE, TEST_PROPNAME, TEST_PROPVALUE, true);
+        assertEquals("Not 1 event created", 1, source.eventCount);
+        assertEquals("Listener not called once", 1, l.numberOfCalls);
+        assertEquals("Wrong event type", TEST_TYPE, l.lastEvent.getType());
+        assertEquals("Wrong property name", TEST_PROPNAME, l.lastEvent
+                .getPropertyName());
+        assertEquals("Wrong property value", TEST_PROPVALUE, l.lastEvent
+                .getPropertyValue());
+        assertTrue("Wrong before event flag", l.lastEvent.isBeforeUpdate());
+    }
+
+    /**
+     * Tests firering an event if there are no listeners.
+     */
+    public void testFireEventNoListeners()
+    {
+        source.fireEvent(TEST_TYPE, TEST_PROPNAME, TEST_PROPVALUE, false);
+        assertEquals("An event object was created", 0, source.eventCount);
+    }
+
+    /**
+     * Tests generating a detail event if detail events are not allowed.
+     */
+    public void testFireEventNoDetails()
+    {
+        TestListener l = new TestListener();
+        source.addConfigurationListener(l);
+        source.setDetailEvents(false);
+        source.fireEvent(TEST_TYPE, TEST_PROPNAME, TEST_PROPVALUE, false);
+        assertEquals("Event object was created", 0, source.eventCount);
+        assertEquals("Listener was called", 0, l.numberOfCalls);
+    }
+
+    /**
+     * Tests whether an event listener can deregister itself in reaction of a
+     * delivered event.
+     */
+    public void testRemoveListenerInFireEvent()
+    {
+        ConfigurationListener lstRemove = new ConfigurationListener()
+        {
+            public void configurationChanged(ConfigurationEvent event)
+            {
+                source.removeConfigurationListener(this);
+            }
+        };
+
+        source.addConfigurationListener(lstRemove);
+        TestListener l = new TestListener();
+        source.addConfigurationListener(l);
+        source.fireEvent(TEST_TYPE, TEST_PROPNAME, TEST_PROPVALUE, false);
+        assertEquals("Listener was not called", 1, l.numberOfCalls);
+        assertEquals("Listener was not removed", 1, source
+                .getConfigurationListeners().size());
+    }
+
+    /**
+     * A test event listener implementation.
+     */
+    static class TestListener implements ConfigurationListener
+    {
+        ConfigurationEvent lastEvent;
+
+        int numberOfCalls;
+
+        public void configurationChanged(ConfigurationEvent event)
+        {
+            lastEvent = event;
+            numberOfCalls++;
+        }
+    }
+
+    /**
+     * A specialized event source implementation that counts the number of
+     * created event objects. It is used to test whether the
+     * <code>fireEvent()</code> methods only creates event objects if
+     * necessary.
+     */
+    static class CountingEventSource extends EventSource
+    {
+        int eventCount;
+
+        protected ConfigurationEvent createEvent(int type, String propName,
+                Object propValue, boolean before)
+        {
+            eventCount++;
+            return super.createEvent(type, propName, propValue, before);
+        }
+    }
+}

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestEventSource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestEventSource.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestEventSource.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestHierarchicalConfigurationEvents.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestHierarchicalConfigurationEvents.java?rev=394530&view=auto
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestHierarchicalConfigurationEvents.java
(added)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestHierarchicalConfigurationEvents.java
Sun Apr 16 10:56:48 2006
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.event;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.apache.commons.configuration.AbstractConfiguration;
+import org.apache.commons.configuration.HierarchicalConfiguration;
+import org.apache.commons.configuration.tree.DefaultConfigurationNode;
+
+/**
+ * Test class for the events generated by hierarchical configurations.
+ *
+ * @version $Id$
+ */
+public class TestHierarchicalConfigurationEvents extends
+        AbstractTestConfigurationEvents
+{
+    protected AbstractConfiguration createConfiguration()
+    {
+        return new HierarchicalConfiguration();
+    }
+
+    /**
+     * Tests events generated by the clearTree() method.
+     */
+    public void testClearTreeEvent()
+    {
+        HierarchicalConfiguration hc = (HierarchicalConfiguration) config;
+        String key = EXIST_PROPERTY.substring(0, EXIST_PROPERTY.indexOf('.'));
+        Collection nodes = hc.getExpressionEngine()
+                .query(hc.getRootNode(), key);
+        hc.clearTree(key);
+        l.checkEvent(HierarchicalConfiguration.EVENT_CLEAR_TREE, key, null,
+                true);
+        l.checkEvent(HierarchicalConfiguration.EVENT_CLEAR_TREE, key, nodes,
+                false);
+        l.done();
+    }
+
+    /**
+     * Tests events generated by the addNodes() method.
+     */
+    public void testAddNodesEvent()
+    {
+        HierarchicalConfiguration hc = (HierarchicalConfiguration) config;
+        Collection nodes = new ArrayList(1);
+        nodes.add(new DefaultConfigurationNode("a_key", TEST_PROPVALUE));
+        hc.addNodes(TEST_PROPNAME, nodes);
+        l.checkEvent(HierarchicalConfiguration.EVENT_ADD_NODES, TEST_PROPNAME,
+                nodes, true);
+        l.checkEvent(HierarchicalConfiguration.EVENT_ADD_NODES, TEST_PROPNAME,
+                nodes, false);
+        l.done();
+    }
+
+    /**
+     * Tests events generated by addNodes() when the list of nodes is empty. In
+     * this case no events should be generated.
+     */
+    public void testAddNodesEmptyEvent()
+    {
+        ((HierarchicalConfiguration) config).addNodes(TEST_PROPNAME,
+                new ArrayList());
+        l.done();
+    }
+}

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestHierarchicalConfigurationEvents.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestHierarchicalConfigurationEvents.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestHierarchicalConfigurationEvents.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestMapConfigurationEvents.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestMapConfigurationEvents.java?rev=394530&view=auto
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestMapConfigurationEvents.java
(added)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestMapConfigurationEvents.java
Sun Apr 16 10:56:48 2006
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.event;
+
+import java.util.HashMap;
+
+import org.apache.commons.configuration.AbstractConfiguration;
+import org.apache.commons.configuration.MapConfiguration;
+
+/**
+ * Test class for the events generated by MapConfiguration.
+ *
+ * @version $Id$
+ */
+public class TestMapConfigurationEvents extends AbstractTestConfigurationEvents
+{
+    protected AbstractConfiguration createConfiguration()
+    {
+        return new MapConfiguration(new HashMap());
+    }
+}

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestMapConfigurationEvents.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestMapConfigurationEvents.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestMapConfigurationEvents.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestPropertiesConfigurationEvents.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestPropertiesConfigurationEvents.java?rev=394530&view=auto
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestPropertiesConfigurationEvents.java
(added)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestPropertiesConfigurationEvents.java
Sun Apr 16 10:56:48 2006
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.event;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+
+import org.apache.commons.configuration.AbstractConfiguration;
+import org.apache.commons.configuration.PropertiesConfiguration;
+
+/**
+ * Test class for the events generated by properties configurations. This class
+ * also tests reload events.
+ *
+ * @version $Id$
+ */
+public class TestPropertiesConfigurationEvents extends
+        AbstractTestFileConfigurationEvents
+{
+    /** The file to be loaded.*/
+    static final File TEST_FILE = new File("conf/test.properties");
+    
+    protected AbstractConfiguration createConfiguration()
+    {
+        return new PropertiesConfiguration();
+    }
+
+    protected URL getSourceURL() throws IOException
+    {
+        return TEST_FILE.toURL();
+    }
+}

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestPropertiesConfigurationEvents.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestPropertiesConfigurationEvents.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestPropertiesConfigurationEvents.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestXMLConfigurationEvents.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestXMLConfigurationEvents.java?rev=394530&view=auto
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestXMLConfigurationEvents.java
(added)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestXMLConfigurationEvents.java
Sun Apr 16 10:56:48 2006
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed 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.event;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+
+import org.apache.commons.configuration.AbstractConfiguration;
+import org.apache.commons.configuration.XMLConfiguration;
+
+/**
+ * Test class for events generated by XMLConfiguration.
+ *
+ * @version $Id$
+ */
+public class TestXMLConfigurationEvents extends
+        AbstractTestFileConfigurationEvents
+{
+    static final File TEST_FILE = new File("conf/test.xml");
+
+    protected URL getSourceURL() throws IOException
+    {
+        return TEST_FILE.toURL();
+    }
+
+    protected AbstractConfiguration createConfiguration()
+    {
+        return new XMLConfiguration();
+    }
+}

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestXMLConfigurationEvents.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestXMLConfigurationEvents.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/event/TestXMLConfigurationEvents.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message