Author: oheger
Date: Sat Jul 21 08:26:19 2007
New Revision: 558330

URL: http://svn.apache.org/viewvc?view=rev&rev=558330
Log:
Minor refactoring of the unit tests that check error listeners: a generic mock 
event listener class was extracted.

Added:
    
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/ConfigurationErrorListenerImpl.java
   (with props)
Modified:
    
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDatabaseConfiguration.java
    
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
    
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestJNDIConfiguration.java

Added: 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/ConfigurationErrorListenerImpl.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/ConfigurationErrorListenerImpl.java?view=auto&rev=558330
==============================================================================
--- 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/ConfigurationErrorListenerImpl.java
 (added)
+++ 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/ConfigurationErrorListenerImpl.java
 Sat Jul 21 08:26:19 2007
@@ -0,0 +1,101 @@
+/*
+ * 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;
+
+import junit.framework.Assert;
+
+import org.apache.commons.configuration.event.ConfigurationErrorEvent;
+import org.apache.commons.configuration.event.ConfigurationErrorListener;
+
+/**
+ * An implementation of the <code>ConfigurationErrorListener</code> interface
+ * that can be used in unit tests. This implementation just records received
+ * events and allows to test whether expected errors occurred.
+ *
+ * @author Oliver Heger
+ * @version $Id$
+ */
+public class ConfigurationErrorListenerImpl implements
+        ConfigurationErrorListener
+{
+    /** Stores the last received error event. */
+    private ConfigurationErrorEvent event;
+
+    /** Stores the number of calls to configurationError(). */
+    private int errorCount;
+
+    /**
+     * An error event is received. Updates the internal counter and stores the
+     * event.
+     *
+     * @param event the error event
+     */
+    public void configurationError(ConfigurationErrorEvent event)
+    {
+        this.event = event;
+        errorCount++;
+    }
+
+    /**
+     * Returns the last received error event.
+     *
+     * @return the last error event (may be <b>null</b>)
+     */
+    public ConfigurationErrorEvent getLastEvent()
+    {
+        return event;
+    }
+
+    /**
+     * Returns the number of received error events.
+     *
+     * @return the number of error events
+     */
+    public int getErrorCount()
+    {
+        return errorCount;
+    }
+
+    /**
+     * Checks whether no error event was received.
+     */
+    public void verify()
+    {
+        Assert.assertEquals("Error events received", 0, errorCount);
+    }
+
+    /**
+     * Checks whether an expected error event was received. This is a
+     * convenience method for checking whether exactly one event of a certain
+     * type was received.
+     *
+     * @param type the type of the event
+     * @param propName the name of the property
+     * @param propValue the value of the property
+     */
+    public void verify(int type, String propName, Object propValue)
+    {
+        Assert.assertEquals("Wrong number of error events", 1, errorCount);
+        Assert.assertEquals("Wrong event type", type, event.getType());
+        Assert.assertTrue("Wrong property name", (propName == null) ? event
+                .getPropertyName() == null : propName.equals(event
+                .getPropertyName()));
+        Assert.assertTrue("Wrong property value", (propValue == null) ? event
+                .getPropertyValue() == null : propValue.equals(event
+                .getPropertyValue()));
+    }
+}

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

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

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

Modified: 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDatabaseConfiguration.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDatabaseConfiguration.java?view=diff&rev=558330&r1=558329&r2=558330
==============================================================================
--- 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDatabaseConfiguration.java
 (original)
+++ 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDatabaseConfiguration.java
 Sat Jul 21 08:26:19 2007
@@ -27,7 +27,6 @@
 
 import junit.framework.TestCase;
 
-import org.apache.commons.configuration.event.ConfigurationErrorEvent;
 import org.apache.commons.configuration.event.ConfigurationErrorListener;
 import org.apache.commons.configuration.test.HsqlDB;
 import org.apache.commons.dbcp.BasicDataSource;
@@ -74,7 +73,7 @@
     private DataSource datasource;
 
     /** An error listener for testing whether internal errors occurred.*/
-    private TestErrorListener listener;
+    private ConfigurationErrorListenerImpl listener;
 
     protected void setUp() throws Exception
     {
@@ -121,7 +120,7 @@
         // if an error listener is defined, we check whether an error occurred
         if(listener != null)
         {
-            assertEquals("An internal error occurred", 0, listener.errorCount);
+            assertEquals("An internal error occurred", 0, 
listener.getErrorCount());
         }
         super.tearDown();
     }
@@ -156,7 +155,7 @@
     {
         // remove log listener to avoid exception longs
         config.removeErrorListener((ConfigurationErrorListener) 
config.getErrorListeners().iterator().next());
-        listener = new TestErrorListener();
+        listener = new ConfigurationErrorListenerImpl();
         config.addErrorListener(listener);
         config.failOnConnect = true;
     }
@@ -184,16 +183,12 @@
      */
     private void checkErrorListener(int type, String key, Object value)
     {
-        assertEquals("Wrong number of errors", 1, listener.errorCount);
-        assertEquals("Wrong event type", type, listener.event.getType());
-        assertTrue("Wrong event source", listener.event.getSource() instanceof 
DatabaseConfiguration);
-        assertTrue("Wrong exception", listener.event.getCause() instanceof 
SQLException);
-        assertTrue("Wrong property key", (key == null) ? listener.event
-                .getPropertyName() == null : key.equals(listener.event
-                .getPropertyName()));
-        assertTrue("Wrong property value", (value == null) ? listener.event
-                .getPropertyValue() == null : value.equals(listener.event
-                .getPropertyValue()));
+        listener.verify(type, key, value);
+        assertTrue(
+                "Wrong event source",
+                listener.getLastEvent().getSource() instanceof 
DatabaseConfiguration);
+        assertTrue("Wrong exception",
+                listener.getLastEvent().getCause() instanceof SQLException);
         listener = null; // mark as checked
     }
 
@@ -483,25 +478,6 @@
                 throw new SQLException("Simulated DB error");
             }
             return super.getConnection();
-        }
-    }
-
-    /**
-     * A test error listener implementation that is used for finding out 
whether
-     * error events are correctly triggered.
-     */
-    static class TestErrorListener implements ConfigurationErrorListener
-    {
-        /** Stores the number of calls. */
-        int errorCount;
-
-        /** Stores the last error event. */
-        ConfigurationErrorEvent event;
-
-        public void configurationError(ConfigurationErrorEvent event)
-        {
-            errorCount++;
-            this.event = event;
         }
     }
 }

Modified: 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java?view=diff&rev=558330&r1=558329&r2=558330
==============================================================================
--- 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
 (original)
+++ 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
 Sat Jul 21 08:26:19 2007
@@ -26,8 +26,6 @@
 import java.io.IOException;
 import java.io.PrintWriter;
 
-import org.apache.commons.configuration.event.ConfigurationErrorEvent;
-import org.apache.commons.configuration.event.ConfigurationErrorListener;
 import org.apache.commons.configuration.reloading.FileAlwaysReloadingStrategy;
 import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
 
@@ -578,20 +576,7 @@
      */
     public void testReloadError() throws ConfigurationException
     {
-        class TestConfigurationErrorListener implements
-                ConfigurationErrorListener
-        {
-            ConfigurationErrorEvent event;
-
-            int errorCount;
-
-            public void configurationError(ConfigurationErrorEvent event)
-            {
-                this.event = event;
-                errorCount++;
-            }
-        };
-        TestConfigurationErrorListener l = new 
TestConfigurationErrorListener();
+        ConfigurationErrorListenerImpl l = new 
ConfigurationErrorListenerImpl();
         PropertiesConfiguration config = new PropertiesConfiguration(
                 RESOURCE_NAME);
         config.clearErrorListeners();
@@ -600,12 +585,8 @@
         config.getString("test");
         config.setFileName("Not existing file");
         config.getString("test");
-        assertEquals("Wrong number of error events", 1, l.errorCount);
-        assertEquals("Wrong error type",
-                AbstractFileConfiguration.EVENT_RELOAD, l.event.getType());
-        assertNull("Wrong property name", l.event.getPropertyName());
-        assertNull("Wrong property value", l.event.getPropertyValue());
-        assertNotNull("Exception is not set", l.event.getCause());
+        l.verify(AbstractFileConfiguration.EVENT_RELOAD, null, null);
+        assertNotNull("Exception is not set", l.getLastEvent().getCause());
     }
 
     /**

Modified: 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestJNDIConfiguration.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestJNDIConfiguration.java?view=diff&rev=558330&r1=558329&r2=558330
==============================================================================
--- 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestJNDIConfiguration.java
 (original)
+++ 
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestJNDIConfiguration.java
 Sat Jul 21 08:26:19 2007
@@ -19,13 +19,12 @@
 
 import java.util.Hashtable;
 
-import junit.framework.TestCase;
-
 import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
 
-import org.apache.commons.configuration.event.ConfigurationErrorEvent;
+import junit.framework.TestCase;
+
 import org.apache.commons.configuration.event.ConfigurationErrorListener;
 
 /**
@@ -41,7 +40,7 @@
     private NonStringTestHolder nonStringTestHolder;
 
     /** A test error listener for counting internal errors.*/
-    private TestErrorListener listener;
+    private ConfigurationErrorListenerImpl listener;
 
     public void setUp() throws Exception {
 
@@ -52,7 +51,7 @@
         nonStringTestHolder = new NonStringTestHolder();
         nonStringTestHolder.setConfiguration(conf);
 
-        listener = new TestErrorListener();
+        listener = new ConfigurationErrorListenerImpl();
         conf.addErrorListener(listener);
     }
 
@@ -204,6 +203,8 @@
     private void checkErrorListener(int type, String propName, Object 
propValue)
     {
         listener.verify(type, propName, propValue);
+        assertTrue("Wrong exception class",
+                listener.getLastEvent().getCause() instanceof NamingException);
         listener = null;
     }
 
@@ -299,54 +300,6 @@
                 throw new NamingException("Simulated JNDI exception!");
             }
             return super.getBaseContext();
-        }
-    }
-
-    /**
-     * A test listener implementation that is used for counting and testing
-     * internal errors.
-     */
-    static class TestErrorListener implements ConfigurationErrorListener
-    {
-        /** Stores the last received error event. */
-        ConfigurationErrorEvent event;
-
-        /** Stores the number of calls. */
-        int errorCount;
-
-        public void configurationError(ConfigurationErrorEvent event)
-        {
-            this.event = event;
-            errorCount++;
-        }
-
-        /**
-         * Checks whether no error event was received.
-         */
-        public void verify()
-        {
-            assertEquals("Error events received", 0, errorCount);
-        }
-
-        /**
-         * Checks whether an expected error event was received.
-         *
-         * @param type the type of the event
-         * @param propName the name of the property
-         * @param propValue the value of the property
-         */
-        public void verify(int type, String propName, Object propValue)
-        {
-            assertEquals("Wrong number of error events", 1, errorCount);
-            assertEquals("Wrong event type", type, event.getType());
-            assertTrue("Wrong property name", (propName == null) ? event
-                    .getPropertyName() == null : propName.equals(event
-                    .getPropertyName()));
-            assertTrue("Wrong property value", (propValue == null) ? event
-                    .getPropertyValue() == null : propValue.equals(event
-                    .getPropertyValue()));
-            assertTrue("Wrong exception class",
-                    event.getCause() instanceof NamingException);
         }
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to