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/07/13 22:06:33 UTC

svn commit: r1610297 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/JNDIConfiguration.java test/java/org/apache/commons/configuration/TestJNDIConfiguration.java

Author: oheger
Date: Sun Jul 13 20:06:33 2014
New Revision: 1610297

URL: http://svn.apache.org/r1610297
Log:
Adapted JNDIConfiguration to new event listener mechanism.

Error events are now generated using the new fireError() method provided by
BaseEventSource.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/JNDIConfiguration.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestJNDIConfiguration.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/JNDIConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/JNDIConfiguration.java?rev=1610297&r1=1610296&r2=1610297&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/JNDIConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/JNDIConfiguration.java Sun Jul 13 20:06:33 2014
@@ -17,13 +17,6 @@
 
 package org.apache.commons.configuration;
 
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
-
 import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NameClassPair;
@@ -31,7 +24,14 @@ import javax.naming.NameNotFoundExceptio
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
 import javax.naming.NotContextException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
 
+import org.apache.commons.configuration.event.ConfigurationErrorEvent;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.logging.LogFactory;
 
@@ -224,7 +224,8 @@ public class JNDIConfiguration extends A
         }
         catch (NamingException e)
         {
-            fireError(EVENT_READ_PROPERTY, null, null, e);
+            fireError(ConfigurationErrorEvent.READ,
+                    ConfigurationErrorEvent.READ, null, null, e);
             return new ArrayList<String>().iterator();
         }
     }
@@ -309,7 +310,8 @@ public class JNDIConfiguration extends A
         }
         catch (NamingException e)
         {
-            fireError(EVENT_READ_PROPERTY, null, null, e);
+            fireError(ConfigurationErrorEvent.READ,
+                    ConfigurationErrorEvent.READ, null, null, e);
             return true;
         }
     }
@@ -366,7 +368,8 @@ public class JNDIConfiguration extends A
         }
         catch (NamingException e)
         {
-            fireError(EVENT_READ_PROPERTY, key, null, e);
+            fireError(ConfigurationErrorEvent.READ,
+                    ConfigurationErrorEvent.READ, key, null, e);
             return false;
         }
     }
@@ -424,7 +427,8 @@ public class JNDIConfiguration extends A
         }
         catch (NamingException e)
         {
-            fireError(EVENT_READ_PROPERTY, key, null, e);
+            fireError(ConfigurationErrorEvent.READ,
+                    ConfigurationErrorEvent.READ, key, null, e);
             return null;
         }
     }

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestJNDIConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestJNDIConfiguration.java?rev=1610297&r1=1610296&r2=1610297&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestJNDIConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestJNDIConfiguration.java Sun Jul 13 20:06:33 2014
@@ -23,14 +23,18 @@ import static org.junit.Assert.assertNot
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
-import java.util.Hashtable;
-import java.util.Properties;
-
 import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NameNotFoundException;
 import javax.naming.NamingException;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Properties;
 
+import org.apache.commons.configuration.event.ConfigurationErrorEvent;
+import org.apache.commons.configuration.event.ErrorListenerTestImpl;
+import org.apache.commons.configuration.event.EventListener;
+import org.apache.commons.configuration.event.EventType;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -48,7 +52,7 @@ public class TestJNDIConfiguration {
     private NonStringTestHolder nonStringTestHolder;
 
     /** A test error listener for counting internal errors.*/
-    private ConfigurationErrorListenerImpl listener;
+    private ErrorListenerTestImpl listener;
 
     @Before
     public void setUp() throws Exception {
@@ -63,8 +67,8 @@ public class TestJNDIConfiguration {
         nonStringTestHolder = new NonStringTestHolder();
         nonStringTestHolder.setConfiguration(conf);
 
-        listener = new ConfigurationErrorListenerImpl();
-        conf.addErrorListener(listener);
+        listener = new ErrorListenerTestImpl(conf);
+        conf.addEventListener(ConfigurationErrorEvent.ANY, listener);
     }
 
     /**
@@ -76,7 +80,7 @@ public class TestJNDIConfiguration {
     {
         if (listener != null)
         {
-            listener.verify();
+            listener.done();
         }
     }
 
@@ -220,7 +224,10 @@ public class TestJNDIConfiguration {
     private PotentialErrorJNDIConfiguration setUpErrorConfig()
     {
         conf.installException();
-        conf.removeErrorListener(conf.getErrorListeners().iterator().next());
+        // remove log error listener to avoid output in tests
+        Iterator<EventListener<? super ConfigurationErrorEvent>> iterator =
+                conf.getEventListeners(ConfigurationErrorEvent.ANY).iterator();
+        conf.removeEventListener(ConfigurationErrorEvent.ANY, iterator.next());
         return conf;
     }
 
@@ -228,14 +235,18 @@ public class TestJNDIConfiguration {
      * Tests whether the expected error events have been received.
      *
      * @param type the expected event type
+     * @param opEventType the event type of the failed operation
      * @param propName the name of the property
      * @param propValue the property value
      */
-    private void checkErrorListener(int type, String propName, Object propValue)
+    private void checkErrorListener(
+            EventType<? extends ConfigurationErrorEvent> type,
+            EventType<?> opEventType, String propName, Object propValue)
     {
-        listener.verify(type, propName, propValue);
+        Throwable exception =
+                listener.checkEvent(type, opEventType, propName, propValue);
         assertTrue("Wrong exception class",
-                listener.getLastEvent().getCause() instanceof NamingException);
+                exception instanceof NamingException);
         listener = null;
     }
 
@@ -247,7 +258,7 @@ public class TestJNDIConfiguration {
     {
         JNDIConfiguration c = new JNDIConfiguration();
         assertEquals("No error log listener registered", 1, c
-                .getErrorListeners().size());
+                .getEventListeners(ConfigurationErrorEvent.ANY).size());
     }
 
     /**
@@ -258,8 +269,8 @@ public class TestJNDIConfiguration {
     {
         assertFalse("Iteration not empty", setUpErrorConfig().getKeys()
                 .hasNext());
-        checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, null,
-                null);
+        checkErrorListener(ConfigurationErrorEvent.READ,
+                ConfigurationErrorEvent.READ, null, null);
     }
 
     /**
@@ -269,8 +280,8 @@ public class TestJNDIConfiguration {
     public void testIsEmptyError() throws Exception
     {
         assertTrue("Error config not empty", setUpErrorConfig().isEmpty());
-        checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, null,
-                null);
+        checkErrorListener(ConfigurationErrorEvent.READ,
+                ConfigurationErrorEvent.READ, null, null);
     }
 
     /**
@@ -281,8 +292,8 @@ public class TestJNDIConfiguration {
     {
         assertFalse("Key contained after error", setUpErrorConfig()
                 .containsKey("key"));
-        checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, "key",
-                null);
+        checkErrorListener(ConfigurationErrorEvent.READ,
+                ConfigurationErrorEvent.READ, "key", null);
     }
 
     /**
@@ -293,8 +304,8 @@ public class TestJNDIConfiguration {
     {
         assertNull("Wrong property value after error", setUpErrorConfig()
                 .getProperty("key"));
-        checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, "key",
-                null);
+        checkErrorListener(ConfigurationErrorEvent.READ,
+                ConfigurationErrorEvent.READ, "key", null);
     }
 
     /**
@@ -319,7 +330,7 @@ public class TestJNDIConfiguration {
     {
         conf.installException(new NameNotFoundException("Test exception"));
         assertFalse("Got keys", conf.getKeys().hasNext());
-        listener.verify();
+        listener.done();
     }
 
     /**