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 2013/01/26 19:04:22 UTC
svn commit: r1438930 - in /commons/proper/configuration/trunk/src:
main/java/org/apache/commons/configuration/ConfigurationUtils.java
test/java/org/apache/commons/configuration/TestConfigurationUtils.java
Author: oheger
Date: Sat Jan 26 18:04:22 2013
New Revision: 1438930
URL: http://svn.apache.org/viewvc?rev=1438930&view=rev
Log:
Added a method for converting an object to an EventSource.
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationUtils.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationUtils.java
Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationUtils.java?rev=1438930&r1=1438929&r2=1438930&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationUtils.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationUtils.java Sat Jan 26 18:04:22 2013
@@ -30,6 +30,7 @@ import java.util.Iterator;
import org.apache.commons.configuration.event.ConfigurationErrorEvent;
import org.apache.commons.configuration.event.ConfigurationErrorListener;
+import org.apache.commons.configuration.event.ConfigurationListener;
import org.apache.commons.configuration.event.EventSource;
import org.apache.commons.configuration.reloading.Reloadable;
import org.apache.commons.configuration.tree.ExpressionEngine;
@@ -75,6 +76,31 @@ public final class ConfigurationUtils
private static final Class<?>[] IMMUTABLE_HIERARCHICAL_CONFIG_IFCS = {
ImmutableHierarchicalConfiguration.class
};
+ /**
+ * A dummy event source that is returned by {@code asEventSource()} if a
+ * mock object has to be returned. It provides empty dummy implementations
+ * for all interface methods.
+ */
+ private static final EventSource DUMMY_EVENT_SOURCE = new EventSource()
+ {
+ public void addConfigurationListener(ConfigurationListener l)
+ {
+ }
+
+ public boolean removeConfigurationListener(ConfigurationListener l)
+ {
+ return false;
+ }
+
+ public void addErrorListener(ConfigurationErrorListener l)
+ {
+ }
+
+ public boolean removeErrorListener(ConfigurationErrorListener l)
+ {
+ return false;
+ }
+ };
/** The logger.*/
private static final Log LOG = LogFactory.getLog(ConfigurationUtils.class);
@@ -853,8 +879,9 @@ public final class ConfigurationUtils
}
/**
- * Helper method for creating a proxy for an unmodifiable configuration.
- * The interfaces the proxy should implement are passed as argument.
+ * Helper method for creating a proxy for an unmodifiable configuration. The
+ * interfaces the proxy should implement are passed as argument.
+ *
* @param ifcs an array with the interface classes the proxy must implement
* @param c the configuration object to be wrapped
* @return a proxy object for an immutable configuration
@@ -867,4 +894,36 @@ public final class ConfigurationUtils
ConfigurationUtils.class.getClassLoader(), ifcs,
new ImmutableConfigurationInvocationHandler(c));
}
+
+ /**
+ * Casts the specified object to an {@code EventSource} if possible. The
+ * boolean argument determines the method's behavior if the object does not
+ * implement the {@code EventSource} event: if set to <b>false</b>, a
+ * {@code ConfigurationRuntimeException} is thrown; if set to <b>true</b>, a
+ * dummy {@code EventSource} is returned; on this object all methods can be
+ * called, but they do not have any effect.
+ *
+ * @param obj the object to be cast as {@code EventSource}
+ * @param mockIfUnsupported a flag whether a mock object should be returned
+ * if necessary
+ * @return an {@code EventSource}
+ * @throws ConfigurationRuntimeException if the object cannot be cast to
+ * {@code EventSource} and the mock flag is <b>false</b>
+ * @since 2.0
+ */
+ public static EventSource asEventSource(Object obj,
+ boolean mockIfUnsupported)
+ {
+ if (obj instanceof EventSource)
+ {
+ return (EventSource) obj;
+ }
+
+ if (!mockIfUnsupported)
+ {
+ throw new ConfigurationRuntimeException(
+ "Cannot cast to EventSource: " + obj);
+ }
+ return DUMMY_EVENT_SOURCE;
+ }
}
Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationUtils.java?rev=1438930&r1=1438929&r2=1438930&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationUtils.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationUtils.java Sat Jan 26 18:04:22 2013
@@ -35,8 +35,12 @@ import java.util.Map;
import junitx.framework.ListAssert;
import org.apache.commons.configuration.builder.XMLBuilderParametersImpl;
+import org.apache.commons.configuration.event.ConfigurationErrorListener;
+import org.apache.commons.configuration.event.ConfigurationListener;
+import org.apache.commons.configuration.event.EventSource;
import org.apache.commons.configuration.tree.DefaultExpressionEngine;
import org.apache.commons.configuration.tree.ExpressionEngine;
+import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -605,4 +609,40 @@ public class TestConfigurationUtils
{
ConfigurationUtils.loadClass("a non existing class!");
}
+
+ /**
+ * Tests asEventSource() if the passed in object implements this interface.
+ */
+ @Test
+ public void testAsEventSourceSupported()
+ {
+ XMLConfiguration src = new XMLConfiguration();
+ assertSame("Wrong result", src, ConfigurationUtils.asEventSource(src, true));
+ }
+
+ /**
+ * Tests asEventSource() if an exception is expected.
+ */
+ @Test(expected = ConfigurationRuntimeException.class)
+ public void testAsEventSourceNonSupportedEx()
+ {
+ ConfigurationUtils.asEventSource(this, false);
+ }
+
+ /**
+ * Tests asEventSource() if a mock object has to be returned.
+ */
+ @Test
+ public void testAsEventSourceUnsupportedMock()
+ {
+ ConfigurationListener cl = EasyMock.createMock(ConfigurationListener.class);
+ ConfigurationErrorListener el = EasyMock.createMock(ConfigurationErrorListener.class);
+ EasyMock.replay(cl, el);
+ EventSource source = ConfigurationUtils.asEventSource(this, true);
+ source.addConfigurationListener(cl);
+ source.addErrorListener(el);
+ assertFalse("Wrong result (1)", source.removeConfigurationListener(cl));
+ assertFalse("Wrong result (2)", source.removeErrorListener(el));
+ source.addConfigurationListener(null);
+ }
}