You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by oh...@apache.org on 2007/01/17 22:35:29 UTC
svn commit: r497181 - in /jakarta/commons/proper/configuration/trunk/src:
java/org/apache/commons/configuration/JNDIConfiguration.java
test/org/apache/commons/configuration/TestJNDIConfiguration.java
Author: oheger
Date: Wed Jan 17 13:35:28 2007
New Revision: 497181
URL: http://svn.apache.org/viewvc?view=rev&rev=497181
Log:
CONFIGURATION-245: JNDIConfiguration will now generate error events in case on an internal error.
Modified:
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/JNDIConfiguration.java
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestJNDIConfiguration.java
Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/JNDIConfiguration.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/JNDIConfiguration.java?view=diff&rev=497181&r1=497180&r2=497181
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/JNDIConfiguration.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/JNDIConfiguration.java Wed Jan 17 13:35:28 2007
@@ -29,6 +29,7 @@
import javax.naming.NameNotFoundException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
+import javax.naming.NotContextException;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.LogFactory;
@@ -103,6 +104,7 @@
this.context = context;
this.prefix = prefix;
setLogger(LogFactory.getLog(getClass()));
+ addErrorLogListener();
}
/**
@@ -212,7 +214,7 @@
}
catch (NamingException e)
{
- getLogger().error(e.getMessage(), e);
+ fireError(EVENT_READ_PROPERTY, null, null, e);
return new ArrayList().iterator();
}
}
@@ -296,7 +298,7 @@
}
catch (NamingException e)
{
- getLogger().error(e.getMessage(), e);
+ fireError(EVENT_READ_PROPERTY, null, null, e);
return true;
}
}
@@ -350,7 +352,7 @@
}
catch (NamingException e)
{
- getLogger().error(e.getMessage(), e);
+ fireError(EVENT_READ_PROPERTY, key, null, e);
return false;
}
}
@@ -400,9 +402,14 @@
// expected exception, no need to log it
return null;
}
+ catch (NotContextException nctxex)
+ {
+ // expected exception, no need to log it
+ return null;
+ }
catch (NamingException e)
{
- getLogger().error(e.getMessage(), e);
+ fireError(EVENT_READ_PROPERTY, key, null, e);
return null;
}
}
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=497181&r1=497180&r2=497181
==============================================================================
--- 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 Wed Jan 17 13:35:28 2007
@@ -19,14 +19,15 @@
import junit.framework.TestCase;
+import javax.naming.Context;
import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+import org.apache.commons.configuration.event.ConfigurationErrorEvent;
+import org.apache.commons.configuration.event.ConfigurationErrorListener;
/**
- * Test to see if the JNDIConfiguration works properly. Currently excluded
- * in the project.xml unitTest section as our JNDI provider doesn't
- * properly support the listBindings() method.
- *
- * This does work fine with Tomcat's JNDI provider however.
+ * Test to see if the JNDIConfiguration works properly.
*
* @version $Id$
*/
@@ -38,14 +39,33 @@
private JNDIConfiguration conf;
private NonStringTestHolder nonStringTestHolder;
+ /** A test error listener for counting internal errors.*/
+ private TestErrorListener listener;
+
public void setUp() throws Exception {
System.setProperty("java.naming.factory.initial", CONTEXT_FACTORY);
- conf = new JNDIConfiguration();
+ conf = new PotentialErrorJNDIConfiguration();
nonStringTestHolder = new NonStringTestHolder();
nonStringTestHolder.setConfiguration(conf);
+
+ listener = new TestErrorListener();
+ conf.addErrorListener(listener);
+ }
+
+ /**
+ * Clears the test environment. If an error listener is defined, checks
+ * whether no error event was received.
+ */
+ protected void tearDown() throws Exception
+ {
+ if (listener != null)
+ {
+ listener.verify();
+ }
+ super.tearDown();
}
public void testBoolean() throws Exception {
@@ -162,4 +182,153 @@
assertEquals("'boolean' property", "true", conf.getString("boolean"));
}
+ /**
+ * Configures the test config to throw an exception.
+ */
+ private PotentialErrorJNDIConfiguration setUpErrorConfig()
+ {
+ ((PotentialErrorJNDIConfiguration) conf).failOnGetCtx = true;
+ conf.removeErrorListener((ConfigurationErrorListener) conf
+ .getErrorListeners().iterator().next());
+ return (PotentialErrorJNDIConfiguration) conf;
+ }
+
+ /**
+ * Tests whether the expected error events have been received.
+ *
+ * @param type the expected event type
+ * @param propName the name of the property
+ * @param propValue the property value
+ */
+ private void checkErrorListener(int type, String propName, Object propValue)
+ {
+ listener.verify(type, propName, propValue);
+ listener = null;
+ }
+
+ /**
+ * Tests whether a JNDI configuration registers an error log listener.
+ */
+ public void testLogListener() throws NamingException
+ {
+ conf = new JNDIConfiguration();
+ assertEquals("No error log listener registered", 1, conf
+ .getErrorListeners().size());
+ }
+
+ /**
+ * Tests handling of errors in getKeys().
+ */
+ public void testGetKeysError()
+ {
+ assertFalse("Iteration not empty", setUpErrorConfig().getKeys()
+ .hasNext());
+ checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, null,
+ null);
+ }
+
+ /**
+ * Tests handling of errors in isEmpty().
+ */
+ public void testIsEmptyError() throws NamingException
+ {
+ assertTrue("Error config not empty", setUpErrorConfig().isEmpty());
+ checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, null,
+ null);
+ }
+
+ /**
+ * Tests handling of errors in the containsKey() method.
+ */
+ public void testContainsKeyError()
+ {
+ assertFalse("Key contained after error", setUpErrorConfig()
+ .containsKey("key"));
+ checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, "key",
+ null);
+ }
+
+ /**
+ * Tests handling of errors in getProperty().
+ */
+ public void testGetPropertyError()
+ {
+ assertNull("Wrong property value after error", setUpErrorConfig()
+ .getProperty("key"));
+ checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, "key",
+ null);
+ }
+
+ /**
+ * A special JNDI configuration implementation that can be configured to
+ * throw an exception when accessing the base context. Used for testing the
+ * exception handling.
+ */
+ static class PotentialErrorJNDIConfiguration extends JNDIConfiguration
+ {
+ /** A flag whether an exception should be thrown. */
+ boolean failOnGetCtx;
+
+ public PotentialErrorJNDIConfiguration() throws NamingException
+ {
+ super();
+ }
+
+ public Context getBaseContext() throws NamingException
+ {
+ if (failOnGetCtx)
+ {
+ 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: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org