Author: oheger
Date: Sun Jul 13 20:05:59 2014
New Revision: 1610296
URL: http://svn.apache.org/r1610296
Log:
Adapted DatabaseConfiguration 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/DatabaseConfiguration.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestDatabaseConfiguration.java
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DatabaseConfiguration.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DatabaseConfiguration.java?rev=1610296&r1=1610295&r2=1610296&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DatabaseConfiguration.java
(original)
+++
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DatabaseConfiguration.java
Sun Jul 13 20:05:59 2014
@@ -31,6 +31,9 @@ import java.util.List;
import org.apache.commons.configuration.convert.DisabledListDelimiterHandler;
import org.apache.commons.configuration.convert.ListDelimiterHandler;
+import org.apache.commons.configuration.event.ConfigurationErrorEvent;
+import org.apache.commons.configuration.event.ConfigurationEvent;
+import org.apache.commons.configuration.event.EventType;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.LogFactory;
@@ -314,7 +317,9 @@ public class DatabaseConfiguration exten
@Override
protected Object getPropertyInternal(final String key)
{
- JdbcOperation<Object> op = new
JdbcOperation<Object>(EVENT_READ_PROPERTY, key, null)
+ JdbcOperation<Object> op =
+ new JdbcOperation<Object>(ConfigurationErrorEvent.READ,
+ ConfigurationErrorEvent.READ, key, null)
{
@Override
protected Object performOperation() throws SQLException
@@ -362,7 +367,8 @@ public class DatabaseConfiguration exten
@Override
protected void addPropertyDirect(final String key, final Object obj)
{
- new JdbcOperation<Void>(EVENT_ADD_PROPERTY, key, obj)
+ new JdbcOperation<Void>(ConfigurationErrorEvent.WRITE,
+ ConfigurationEvent.ADD_PROPERTY, key, obj)
{
@Override
protected Void performOperation() throws SQLException
@@ -434,7 +440,9 @@ public class DatabaseConfiguration exten
@Override
protected boolean isEmptyInternal()
{
- JdbcOperation<Integer> op = new
JdbcOperation<Integer>(EVENT_READ_PROPERTY, null, null)
+ JdbcOperation<Integer> op =
+ new JdbcOperation<Integer>(ConfigurationErrorEvent.READ,
+ ConfigurationErrorEvent.READ, null, null)
{
@Override
protected Integer performOperation() throws SQLException
@@ -463,7 +471,9 @@ public class DatabaseConfiguration exten
@Override
protected boolean containsKeyInternal(final String key)
{
- JdbcOperation<Boolean> op = new
JdbcOperation<Boolean>(EVENT_READ_PROPERTY, key, null)
+ JdbcOperation<Boolean> op =
+ new JdbcOperation<Boolean>(ConfigurationErrorEvent.READ,
+ ConfigurationErrorEvent.READ, key, null)
{
@Override
protected Boolean performOperation() throws SQLException
@@ -492,7 +502,8 @@ public class DatabaseConfiguration exten
@Override
protected void clearPropertyDirect(final String key)
{
- new JdbcOperation<Void>(EVENT_CLEAR_PROPERTY, key, null)
+ new JdbcOperation<Void>(ConfigurationErrorEvent.WRITE,
+ ConfigurationEvent.CLEAR_PROPERTY, key, null)
{
@Override
protected Void performOperation() throws SQLException
@@ -515,7 +526,8 @@ public class DatabaseConfiguration exten
@Override
protected void clearInternal()
{
- new JdbcOperation<Void>(EVENT_CLEAR, null, null)
+ new JdbcOperation<Void>(ConfigurationErrorEvent.WRITE,
+ ConfigurationEvent.CLEAR, null, null)
{
@Override
protected Void performOperation() throws SQLException
@@ -541,7 +553,8 @@ public class DatabaseConfiguration exten
protected Iterator<String> getKeysInternal()
{
final Collection<String> keys = new ArrayList<String>();
- new JdbcOperation<Collection<String>>(EVENT_READ_PROPERTY, null, null)
+ new JdbcOperation<Collection<String>>(ConfigurationErrorEvent.READ,
+ ConfigurationErrorEvent.READ, null, null)
{
@Override
protected Collection<String> performOperation() throws SQLException
@@ -672,7 +685,10 @@ public class DatabaseConfiguration exten
private ResultSet resultSet;
/** The type of the event to send in case of an error. */
- private final int errorEventType;
+ private final EventType<? extends ConfigurationErrorEvent>
errorEventType;
+
+ /** The type of the operation which caused an error. */
+ private final EventType<?> operationEventType;
/** The property configurationName for an error event. */
private final String errorPropertyName;
@@ -681,17 +697,20 @@ public class DatabaseConfiguration exten
private final Object errorPropertyValue;
/**
- * Creates a new instance of {@code JdbcOperation} and initializes
- * the properties related to the error event.
+ * Creates a new instance of {@code JdbcOperation} and initializes the
+ * properties related to the error event.
*
* @param errEvType the type of the error event
+ * @param opType the operation event type
* @param errPropName the property configurationName for the error
event
* @param errPropVal the property value for the error event
*/
- protected JdbcOperation(int errEvType, String errPropName,
- Object errPropVal)
+ protected JdbcOperation(
+ EventType<? extends ConfigurationErrorEvent> errEvType,
+ EventType<?> opType, String errPropName, Object errPropVal)
{
errorEventType = errEvType;
+ operationEventType = opType;
errorPropertyName = errPropName;
errorPropertyValue = errPropVal;
}
@@ -721,7 +740,7 @@ public class DatabaseConfiguration exten
}
catch (SQLException e)
{
- fireError(errorEventType, errorPropertyName,
+ fireError(errorEventType, operationEventType,
errorPropertyName,
errorPropertyValue, e);
}
finally
@@ -817,7 +836,8 @@ public class DatabaseConfiguration exten
protected ResultSet openResultSet(String sql, boolean nameCol,
Object... params) throws SQLException
{
- return initStatement(sql, nameCol, params).executeQuery();
+ resultSet = initStatement(sql, nameCol, params).executeQuery();
+ return resultSet;
}
/**
Modified:
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestDatabaseConfiguration.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestDatabaseConfiguration.java?rev=1610296&r1=1610295&r2=1610296&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestDatabaseConfiguration.java
(original)
+++
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestDatabaseConfiguration.java
Sun Jul 13 20:05:59 2014
@@ -21,16 +21,19 @@ import static org.junit.Assert.assertEqu
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import javax.sql.DataSource;
import java.sql.Clob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
-import javax.sql.DataSource;
-
import
org.apache.commons.configuration.builder.fluent.DatabaseBuilderParameters;
import org.apache.commons.configuration.convert.DefaultListDelimiterHandler;
+import org.apache.commons.configuration.event.ConfigurationErrorEvent;
+import org.apache.commons.configuration.event.ConfigurationEvent;
+import org.apache.commons.configuration.event.ErrorListenerTestImpl;
+import org.apache.commons.configuration.event.EventType;
import org.apache.commons.configuration.ex.ConfigurationException;
import org.easymock.EasyMock;
import org.junit.After;
@@ -50,7 +53,7 @@ public class TestDatabaseConfiguration
private static final String CONFIG_NAME2 = "anotherTestConfig";
/** An error listener for testing whether internal errors occurred.*/
- private ConfigurationErrorListenerImpl listener;
+ private ErrorListenerTestImpl listener;
/** The test helper. */
private DatabaseConfigurationTestHelper helper;
@@ -76,7 +79,7 @@ public class TestDatabaseConfiguration
// if an error listener is defined, we check whether an error occurred
if(listener != null)
{
- assertEquals("An internal error occurred", 0,
listener.getErrorCount());
+ listener.done();
}
helper.tearDown();
}
@@ -101,9 +104,9 @@ public class TestDatabaseConfiguration
private void setUpErrorListener(PotentialErrorDatabaseConfiguration config)
{
// remove log listener to avoid exception longs
-
config.removeErrorListener(config.getErrorListeners().iterator().next());
- listener = new ConfigurationErrorListenerImpl();
- config.addErrorListener(listener);
+ config.clearErrorListeners();
+ listener = new ErrorListenerTestImpl(config);
+ config.addEventListener(ConfigurationErrorEvent.ANY, listener);
config.failOnConnect = true;
}
@@ -127,17 +130,16 @@ public class TestDatabaseConfiguration
* error event will be compared with the expected values.
*
* @param type the expected type of the error event
+ * @param opType the expected operation type
* @param key the expected property key
* @param value the expected property value
*/
- private void checkErrorListener(int type, String key, Object value)
+ private void checkErrorListener(
+ EventType<? extends ConfigurationErrorEvent> type,
+ EventType<?> opType, String key, Object value)
{
- listener.verify(type, key, value);
- assertTrue(
- "Wrong event source",
- listener.getLastEvent().getSource() instanceof
DatabaseConfiguration);
- assertTrue("Wrong exception",
- listener.getLastEvent().getCause() instanceof SQLException);
+ Throwable exception = listener.checkEvent(type, opType, key, value);
+ assertTrue("Wrong exception", exception instanceof SQLException);
listener = null; // mark as checked
}
@@ -372,7 +374,8 @@ public class TestDatabaseConfiguration
public void testLogErrorListener() throws ConfigurationException
{
DatabaseConfiguration config = helper.setUpConfig();
- assertEquals("No error listener registered", 1,
config.getErrorListeners().size());
+ assertEquals("No error listener registered", 1, config
+ .getEventListeners(ConfigurationErrorEvent.ANY).size());
}
/**
@@ -382,7 +385,8 @@ public class TestDatabaseConfiguration
public void testGetPropertyError() throws ConfigurationException
{
setUpErrorConfig().getProperty("key1");
- checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, "key1",
null);
+ checkErrorListener(ConfigurationErrorEvent.READ,
+ ConfigurationErrorEvent.READ, "key1", null);
}
/**
@@ -392,7 +396,8 @@ public class TestDatabaseConfiguration
public void testAddPropertyError() throws ConfigurationException
{
setUpErrorConfig().addProperty("key1", "value");
- checkErrorListener(AbstractConfiguration.EVENT_ADD_PROPERTY, "key1",
"value");
+ checkErrorListener(ConfigurationErrorEvent.WRITE,
+ ConfigurationEvent.ADD_PROPERTY, "key1", "value");
}
/**
@@ -402,7 +407,8 @@ public class TestDatabaseConfiguration
public void testIsEmptyError() throws ConfigurationException
{
assertTrue("Wrong return value for failure",
setUpErrorConfig().isEmpty());
- checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, null,
null);
+ checkErrorListener(ConfigurationErrorEvent.READ,
+ ConfigurationErrorEvent.READ, null, null);
}
/**
@@ -412,7 +418,8 @@ public class TestDatabaseConfiguration
public void testContainsKeyError() throws ConfigurationException
{
assertFalse("Wrong return value for failure",
setUpErrorConfig().containsKey("key1"));
- checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, "key1",
null);
+ checkErrorListener(ConfigurationErrorEvent.READ,
+ ConfigurationErrorEvent.READ, "key1", null);
}
/**
@@ -422,7 +429,8 @@ public class TestDatabaseConfiguration
public void testClearPropertyError() throws ConfigurationException
{
setUpErrorConfig().clearProperty("key1");
- checkErrorListener(AbstractConfiguration.EVENT_CLEAR_PROPERTY, "key1",
null);
+ checkErrorListener(ConfigurationErrorEvent.WRITE,
+ ConfigurationEvent.CLEAR_PROPERTY, "key1", null);
}
/**
@@ -432,7 +440,8 @@ public class TestDatabaseConfiguration
public void testClearError() throws ConfigurationException
{
setUpErrorConfig().clear();
- checkErrorListener(AbstractConfiguration.EVENT_CLEAR, null, null);
+ checkErrorListener(ConfigurationErrorEvent.WRITE,
+ ConfigurationEvent.CLEAR, null, null);
}
/**
@@ -442,7 +451,8 @@ public class TestDatabaseConfiguration
public void testGetKeysError() throws ConfigurationException
{
Iterator<String> it = setUpErrorConfig().getKeys();
- checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, null,
null);
+ checkErrorListener(ConfigurationErrorEvent.READ,
+ ConfigurationErrorEvent.READ, null, null);
assertFalse("Iteration is not empty", it.hasNext());
}