org.apache.commons.configuration2.event.ConfigurationEvent Java Examples

The following examples show how to use org.apache.commons.configuration2.event.ConfigurationEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestBaseConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether a cloned configuration is decoupled from its original.
 */
@Test
public void testCloneModify()
{
    final EventListener<ConfigurationEvent> l = new EventListenerTestImpl(config);
    config.addEventListener(ConfigurationEvent.ANY, l);
    config.addProperty("original", Boolean.TRUE);
    final BaseConfiguration config2 = (BaseConfiguration) config.clone();

    config2.addProperty("clone", Boolean.TRUE);
    assertFalse("New key appears in original", config.containsKey("clone"));
    config2.setProperty("original", Boolean.FALSE);
    assertTrue("Wrong value of original property", config
            .getBoolean("original"));

    assertTrue("Event listener was copied", config2
            .getEventListeners(ConfigurationEvent.ANY).isEmpty());
}
 
Example #2
Source File: AbstractConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Removes the specified property from this configuration. This
 * implementation performs some preparations and then delegates to
 * {@code clearPropertyDirect()}, which will do the real work.
 *
 * @param key the key to be removed
 */
@Override
public final void clearProperty(final String key)
{
    beginWrite(false);
    try
    {
        fireEvent(ConfigurationEvent.CLEAR_PROPERTY, key, null, true);
        clearPropertyDirect(key);
        fireEvent(ConfigurationEvent.CLEAR_PROPERTY, key, null, false);
    }
    finally
    {
        endWrite();
    }
}
 
Example #3
Source File: FileTokenRepository.java    From TNT4J with Apache License 2.0 6 votes vote down vote up
@Override
public void onEvent(ConfigurationEvent event) {
	if (event.isBeforeUpdate()) {
		return;
	}
	logger.log(OpLevel.DEBUG, "configurationChanged: type={0}, {1}:{2}", event.getEventType(),
			event.getPropertyName(), event.getPropertyValue());
	if (event.getEventType() == ConfigurationEvent.ADD_PROPERTY) {
		repListener.repositoryChanged(new TokenRepositoryEvent(event.getSource(), TokenRepository.EVENT_ADD_KEY,
				event.getPropertyName(), event.getPropertyValue(), null));
	} else if (event.getEventType() == ConfigurationEvent.SET_PROPERTY) {
		repListener.repositoryChanged(new TokenRepositoryEvent(event.getSource(), TokenRepository.EVENT_SET_KEY,
				event.getPropertyName(), event.getPropertyValue(), null));
	} else if (event.getEventType() == ConfigurationEvent.CLEAR_PROPERTY) {
		repListener.repositoryChanged(new TokenRepositoryEvent(event.getSource(), TokenRepository.EVENT_CLEAR_KEY,
				event.getPropertyName(), event.getPropertyValue(), null));
	} else if (event.getEventType() == ConfigurationEvent.CLEAR) {
		repListener.repositoryChanged(new TokenRepositoryEvent(event.getSource(), TokenRepository.EVENT_CLEAR,
				event.getPropertyName(), event.getPropertyValue(), null));
	} else if (event.getEventType() == ReloadingEvent.ANY) {
		repListener.repositoryChanged(new TokenRepositoryEvent(event.getSource(), TokenRepository.EVENT_RELOAD,
				event.getPropertyName(), event.getPropertyValue(), null));
	}
}
 
Example #4
Source File: PropertiesConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Installs a layout object. It has to be ensured that the layout is
 * registered as change listener at this configuration. If there is already
 * a layout object installed, it has to be removed properly.
 *
 * @param layout the layout object to be installed
 */
private void installLayout(final PropertiesConfigurationLayout layout)
{
    // only one layout must exist
    if (this.layout != null)
    {
        removeEventListener(ConfigurationEvent.ANY, this.layout);
    }

    if (layout == null)
    {
        this.layout = createLayout();
    }
    else
    {
        this.layout = layout;
    }
    addEventListener(ConfigurationEvent.ANY, this.layout);
}
 
Example #5
Source File: DatabaseConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Removes the specified value from this configuration. If this causes a
 * database error, an error event will be generated of type
 * {@code CLEAR_PROPERTY} with the causing exception. The
 * event's {@code propertyName} will be set to the passed in key, the
 * {@code propertyValue} will be undefined.
 *
 * @param key the key of the property to be removed
 */
@Override
protected void clearPropertyDirect(final String key)
{
    new JdbcOperation<Void>(ConfigurationErrorEvent.WRITE,
            ConfigurationEvent.CLEAR_PROPERTY, key, null)
    {
        @Override
        protected Void performOperation() throws SQLException
        {
            try (final PreparedStatement ps = initStatement(String.format(
                    SQL_CLEAR_PROPERTY, table, keyColumn), true, key))
            {
                ps.executeUpdate();
                return null;
            }
        }
    }
    .execute();
}
 
Example #6
Source File: DatabaseConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Removes all entries from this configuration. If this causes a database
 * error, an error event will be generated of type
 * {@code CLEAR} with the causing exception. Both the
 * event's {@code propertyName} and the {@code propertyValue}
 * will be undefined.
 */
@Override
protected void clearInternal()
{
    new JdbcOperation<Void>(ConfigurationErrorEvent.WRITE,
            ConfigurationEvent.CLEAR, null, null)
    {
        @Override
        protected Void performOperation() throws SQLException
        {
            initStatement(String.format(SQL_CLEAR,
                    table), true).executeUpdate();
            return null;
        }
    }
    .execute();
}
 
Example #7
Source File: AutoSaveListener.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc} This implementation checks whether an auto-safe operation
 * should be performed. This is the case if the event indicates that an
 * update of the configuration has been performed and currently no load
 * operation is in progress.
 */
@Override
public void onEvent(final ConfigurationEvent event)
{
    if (autoSaveRequired(event))
    {
        try
        {
            builder.save();
        }
        catch (final ConfigurationException ce)
        {
            log.warn("Auto save failed!", ce);
        }
    }
}
 
Example #8
Source File: AbstractHierarchicalConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a collection of nodes at the specified position of the configuration
 * tree. This method works similar to {@code addProperty()}, but
 * instead of a single property a whole collection of nodes can be added -
 * and thus complete configuration sub trees. E.g. with this method it is
 * possible to add parts of another {@code BaseHierarchicalConfiguration}
 * object to this object. If the passed in key refers to
 * an existing and unique node, the new nodes are added to this node.
 * Otherwise a new node will be created at the specified position in the
 * hierarchy. Implementation node: This method performs some book-keeping
 * and then delegates to {@code addNodesInternal()}.
 *
 * @param key the key where the nodes are to be added; can be <b>null</b>,
 * then they are added to the root node
 * @param nodes a collection with the {@code Node} objects to be
 * added
 */
@Override
public final void addNodes(final String key, final Collection<? extends T> nodes)
{
    if (nodes == null || nodes.isEmpty())
    {
        return;
    }

    beginWrite(false);
    try
    {
        fireEvent(ConfigurationEvent.ADD_NODES, key, nodes, true);
        addNodesInternal(key, nodes);
        fireEvent(ConfigurationEvent.ADD_NODES, key, nodes, false);
    }
    finally
    {
        endWrite();
    }
}
 
Example #9
Source File: AbstractHierarchicalConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Removes all values of the property with the given name and of keys that
 * start with this name. So if there is a property with the key
 * &quot;foo&quot; and a property with the key &quot;foo.bar&quot;, a call
 * of {@code clearTree("foo")} would remove both properties.
 *
 * @param key the key of the property to be removed
 */
@Override
public final void clearTree(final String key)
{
    beginWrite(false);
    try
    {
        fireEvent(ConfigurationEvent.CLEAR_TREE, key, null, true);
        final Object nodes = clearTreeInternal(key);
        fireEvent(ConfigurationEvent.CLEAR_TREE, key, nodes, false);
    }
    finally
    {
        endWrite();
    }
}
 
Example #10
Source File: TestMapConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests if the cloned configuration is decoupled from the original.
 */
@Test
public void testCloneModify()
{
    final MapConfiguration config = (MapConfiguration) getConfiguration();
    config.addEventListener(ConfigurationEvent.ANY, new EventListenerTestImpl(config));
    final MapConfiguration copy = (MapConfiguration) config.clone();
    assertTrue("Event listeners were copied", copy
            .getEventListeners(ConfigurationEvent.ANY).isEmpty());

    config.addProperty("cloneTest", Boolean.TRUE);
    assertFalse("Map not decoupled", copy.containsKey("cloneTest"));
    copy.clearProperty("key1");
    assertEquals("Map not decoupled (2)", "value1", config
            .getString("key1"));
}
 
Example #11
Source File: TestConfigurationUtils.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether runtime exceptions can be enabled.
 */
@Test(expected = ConfigurationRuntimeException.class)
public void testEnableRuntimeExceptions()
{
    final PropertiesConfiguration config = new PropertiesConfiguration()
    {
        @Override
        protected void addPropertyDirect(final String key, final Object value)
        {
            // always simulate an exception
            fireError(ConfigurationErrorEvent.WRITE,
                    ConfigurationEvent.ADD_PROPERTY, key, value,
                    new RuntimeException("A faked exception!"));
        }
    };
    config.clearErrorListeners();
    ConfigurationUtils.enableRuntimeExceptions(config);
    config.addProperty("test", "testValue");
}
 
Example #12
Source File: TestBuilderConfigurationWrapperFactory.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests the EventSource support level 'dummy'.
 */
@Test
public void testEventSourceSupportDummy()
{
    final BaseHierarchicalConfiguration conf =
            new BaseHierarchicalConfiguration();
    final ConfigurationBuilder<BaseHierarchicalConfiguration> builder =
            createBuilderMock(conf);
    EasyMock.replay(builder);
    final BuilderConfigurationWrapperFactory factory =
            new BuilderConfigurationWrapperFactory(EventSourceSupport.DUMMY);
    final EventSource src =
            (EventSource) factory.createBuilderConfigurationWrapper(
                    HierarchicalConfiguration.class, builder);
    src.addEventListener(ConfigurationEvent.ANY, null);
}
 
Example #13
Source File: TestBuilderConfigurationWrapperFactory.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether event source support of level builder is possible even for a
 * mock builder.
 */
@Test
public void testEventSourceSupportMockBuilder()
{
    final BaseHierarchicalConfiguration conf =
            new BaseHierarchicalConfiguration();
    final ConfigurationBuilder<BaseHierarchicalConfiguration> builder =
            createBuilderMock(conf);
    final EventListenerTestImpl listener = new EventListenerTestImpl(null);
    builder.addEventListener(ConfigurationEvent.ANY, listener);
    EasyMock.replay(builder);

    final BuilderConfigurationWrapperFactory factory =
            new BuilderConfigurationWrapperFactory(EventSourceSupport.BUILDER);
    final EventSource src =
            (EventSource) factory.createBuilderConfigurationWrapper(
                    HierarchicalConfiguration.class, builder);
    src.addEventListener(ConfigurationEvent.ANY, listener);
    EasyMock.verify(builder);
}
 
Example #14
Source File: TestEventListenerParameters.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether an event listener with its type can be added.
 */
@Test
public void testAddEventListener()
{
    final EventListenerTestImpl listener = new EventListenerTestImpl(null);
    final EventListenerParameters parameters = new EventListenerParameters();
    assertSame("Wrong result", parameters, parameters.addEventListener(
            ConfigurationEvent.ADD_PROPERTY, listener));
    assertEquals("Wrong number of registrations", 1, parameters
            .getListeners().getRegistrations().size());
    final EventListenerRegistrationData<?> reg =
            parameters.getListeners().getRegistrations().get(0);
    assertEquals("Wrong event type", ConfigurationEvent.ADD_PROPERTY,
            reg.getEventType());
    assertEquals("Wrong listener", listener, reg.getListener());
}
 
Example #15
Source File: TestBasicConfigurationBuilder.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether configuration listeners can be added.
 */
@Test
public void testAddConfigurationListener() throws ConfigurationException
{
    final EventListener<ConfigurationEvent> l1 = createEventListener();
    final EventListener<ConfigurationEvent> l2 = createEventListener();
    EasyMock.replay(l1, l2);
    final BasicConfigurationBuilder<PropertiesConfiguration> builder =
            new BasicConfigurationBuilder<>(
                    PropertiesConfiguration.class);
    builder.addEventListener(ConfigurationEvent.ANY, l1);
    final PropertiesConfiguration config = builder.getConfiguration();
    builder.addEventListener(ConfigurationEvent.ANY, l2);
    final Collection<EventListener<? super ConfigurationEvent>> listeners =
            config.getEventListeners(ConfigurationEvent.ANY);
    assertTrue("Listener 1 not registered", listeners.contains(l1));
    assertTrue("Listener 2 not registered", listeners.contains(l2));
}
 
Example #16
Source File: TestCompositeConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether clear property events are triggered.
 */
@Test
public void testEventClearProperty()
{
    cc.addConfiguration(conf1);
    final String key = "configuration.loaded";
    assertTrue("Wrong value for property", cc
            .getBoolean(key));
    final EventListenerTestImpl listener = new EventListenerTestImpl(cc);
    cc.addEventListener(ConfigurationEvent.ANY, listener);
    cc.clearProperty(key);
    assertFalse("Key still present", cc.containsKey(key));
    listener.checkEvent(ConfigurationEvent.CLEAR_PROPERTY, key, null, true);
    listener.checkEvent(ConfigurationEvent.CLEAR_PROPERTY, key, null, false);
    listener.done();
}
 
Example #17
Source File: TestBasicConfigurationBuilder.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether configuration listeners can be defined via the configure()
 * method.
 */
@Test
public void testEventListenerConfiguration() throws ConfigurationException
{
    final EventListenerTestImpl listener1 = new EventListenerTestImpl(null);
    final EventListenerRegistrationData<ConfigurationErrorEvent> regData =
            new EventListenerRegistrationData<>(
                    ConfigurationErrorEvent.WRITE,
                    new ErrorListenerTestImpl(null));
    final BasicConfigurationBuilder<PropertiesConfiguration> builder =
            new BasicConfigurationBuilder<>(
                    PropertiesConfiguration.class)
                    .configure(new EventListenerParameters()
                            .addEventListener(ConfigurationEvent.ANY,
                                    listener1).addEventListener(regData));
    final PropertiesConfiguration config = builder.getConfiguration();
    assertTrue("Configuration listener not found", config
            .getEventListeners(ConfigurationEvent.ANY).contains(listener1));
    assertTrue(
            "Error listener not found",
            config.getEventListeners(regData.getEventType()).contains(
                    regData.getListener()));
}
 
Example #18
Source File: TestBasicConfigurationBuilder.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether configuration listeners are removed from the managed
 * configuration when the builder's result object is reset.
 */
@Test
public void testRemoveConfigurationListenersOnReset()
        throws ConfigurationException
{
    final EventListenerTestImpl listener = new EventListenerTestImpl(null);
    final BasicConfigurationBuilder<PropertiesConfiguration> builder =
            new BasicConfigurationBuilder<>(
                    PropertiesConfiguration.class)
                    .configure(new EventListenerParameters()
                            .addEventListener(ConfigurationEvent.ANY,
                                    listener));
    final PropertiesConfiguration config = builder.getConfiguration();
    builder.resetResult();
    config.addProperty("foo", "bar");
    listener.done();
}
 
Example #19
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether a properties configuration can be successfully cloned. It
 * is especially checked whether the layout object is taken into account.
 */
@Test
public void testClone() throws ConfigurationException
{
    final PropertiesConfiguration copy = (PropertiesConfiguration) conf.clone();
    assertNotSame("Copy has same layout object", conf.getLayout(),
            copy.getLayout());
    assertEquals("Wrong number of event listeners for original", 1, conf
            .getEventListeners(ConfigurationEvent.ANY).size());
    assertEquals("Wrong number of event listeners for clone", 1, copy
            .getEventListeners(ConfigurationEvent.ANY).size());
    assertSame("Wrong event listener for original", conf.getLayout(), conf
            .getEventListeners(ConfigurationEvent.ANY).iterator().next());
    assertSame("Wrong event listener for clone", copy.getLayout(), copy
            .getEventListeners(ConfigurationEvent.ANY).iterator().next());
    final StringWriter outConf = new StringWriter();
    new FileHandler(conf).save(outConf);
    final StringWriter outCopy = new StringWriter();
    new FileHandler(copy).save(outCopy);
    assertEquals("Output from copy is different", outConf.toString(), outCopy.toString());
}
 
Example #20
Source File: TestPropertiesConfigurationLayout.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests a newly created instance.
 */
@Test
public void testInit()
{
    assertTrue("Object contains keys", layout.getKeys().isEmpty());
    assertNull("Header comment not null", layout.getHeaderComment());
    final Iterator<EventListener<? super ConfigurationEvent>> it =
            config.getEventListeners(ConfigurationEvent.ANY).iterator();
    assertTrue("No event listener registered", it.hasNext());
    assertSame("Layout not registered as event listener", layout, it.next());
    assertFalse("Multiple event listeners registered", it.hasNext());
    assertFalse("Force single line flag set", layout.isForceSingleLine());
    assertNull("Got a global separator", layout.getGlobalSeparator());
}
 
Example #21
Source File: TestPropertiesConfigurationLayout.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests adding a property multiple time through an event. The property
 * should then be a multi-line property.
 */
@Test
public void testEventAddMultiple()
{
    final ConfigurationEvent event = new ConfigurationEvent(this,
            ConfigurationEvent.ADD_PROPERTY, TEST_KEY, TEST_VALUE,
            false);
    layout.onEvent(event);
    layout.onEvent(event);
    assertFalse("No multi-line property", layout.isSingleLine(TEST_KEY));
}
 
Example #22
Source File: TestPropertiesConfigurationLayout.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests if a property add event is correctly processed.
 */
@Test
public void testEventAdd()
{
    final ConfigurationEvent event = new ConfigurationEvent(this,
            ConfigurationEvent.ADD_PROPERTY, TEST_KEY, TEST_VALUE,
            false);
    layout.onEvent(event);
    assertTrue("Property not stored", layout.getKeys().contains(TEST_KEY));
    assertEquals("Blanc lines before new property", 0, layout
            .getBlancLinesBefore(TEST_KEY));
    assertTrue("No single line property", layout.isSingleLine(TEST_KEY));
    assertEquals("Wrong separator", " = ", layout.getSeparator(TEST_KEY));
}
 
Example #23
Source File: CombinedConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Event listener call back for configuration update events. This method is
 * called whenever one of the contained configurations was modified. It
 * invalidates this combined configuration.
 *
 * @param event the update event
 */
@Override
public void onEvent(final ConfigurationEvent event)
{
    if (event.isBeforeUpdate())
    {
        invalidate();
    }
}
 
Example #24
Source File: TestPropertiesConfigurationLayout.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests if an add event is correctly processed if the affected property is
 * already stored in the layout object.
 */
@Test
public void testEventAddExisting() throws ConfigurationException
{
    builder.addComment(TEST_COMMENT);
    builder.addProperty(TEST_KEY, TEST_VALUE);
    layout.load(config, builder.getReader());
    final ConfigurationEvent event = new ConfigurationEvent(this,
            ConfigurationEvent.ADD_PROPERTY, TEST_KEY, TEST_VALUE,
            false);
    layout.onEvent(event);
    assertFalse("No multi-line property", layout.isSingleLine(TEST_KEY));
    assertEquals("Comment was modified", TEST_COMMENT, layout
            .getCanonicalComment(TEST_KEY, false));
}
 
Example #25
Source File: PropertiesConfigurationLayout.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * The event listener callback. Here event notifications of the
 * configuration object are processed to update the layout object properly.
 *
 * @param event the event object
 */
@Override
public void onEvent(final ConfigurationEvent event)
{
    if (!event.isBeforeUpdate() && loadCounter.get() == 0)
    {
        if (ConfigurationEvent.ADD_PROPERTY.equals(event.getEventType()))
        {
            final boolean contained =
                    layoutData.containsKey(event.getPropertyName());
            final PropertyLayoutData data =
                    fetchLayoutData(event.getPropertyName());
            data.setSingleLine(!contained);
        }
        else if (ConfigurationEvent.CLEAR_PROPERTY.equals(event
                .getEventType()))
        {
            layoutData.remove(event.getPropertyName());
        }
        else if (ConfigurationEvent.CLEAR.equals(event.getEventType()))
        {
            clear();
        }
        else if (ConfigurationEvent.SET_PROPERTY.equals(event
                .getEventType()))
        {
            fetchLayoutData(event.getPropertyName());
        }
    }
}
 
Example #26
Source File: TestConfigurationUtils.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests asEventSource() if a mock object has to be returned.
 */
@Test
public void testAsEventSourceUnsupportedMock()
{
    @SuppressWarnings("unchecked")
    final
    EventListener<ConfigurationEvent> cl = EasyMock.createMock(EventListener.class);
    EasyMock.replay(cl);
    final EventSource source = ConfigurationUtils.asEventSource(this, true);
    source.addEventListener(ConfigurationEvent.ANY, cl);
    assertFalse("Wrong result (1)", source.removeEventListener(ConfigurationEvent.ANY, cl));
    source.addEventListener(ConfigurationEvent.ANY, null);
}
 
Example #27
Source File: TestAbstractConfigurationBasicFeatures.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the events generated by a copy() operation.
 */
@Test
public void testCopyEvents()
{
    final AbstractConfiguration config = setUpDestConfig();
    final Configuration srcConfig = setUpSourceConfig();
    final CollectingConfigurationListener l = new CollectingConfigurationListener();
    config.addEventListener(ConfigurationEvent.ANY, l);
    config.copy(srcConfig);
    checkCopyEvents(l, srcConfig, ConfigurationEvent.SET_PROPERTY);
}
 
Example #28
Source File: TestAbstractConfigurationBasicFeatures.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the events generated by an append() operation.
 */
@Test
public void testAppendEvents()
{
    final AbstractConfiguration config = setUpDestConfig();
    final Configuration srcConfig = setUpSourceConfig();
    final CollectingConfigurationListener l = new CollectingConfigurationListener();
    config.addEventListener(ConfigurationEvent.ANY, l);
    config.append(srcConfig);
    checkCopyEvents(l, srcConfig, ConfigurationEvent.ADD_PROPERTY);
}
 
Example #29
Source File: TestAbstractConfigurationBasicFeatures.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether the correct events are received for a copy operation.
 *
 * @param l the event listener
 * @param src the configuration that was copied
 * @param eventType the expected event type
 */
private void checkCopyEvents(final CollectingConfigurationListener l,
        final Configuration src, final EventType<?> eventType)
{
    final Map<String, ConfigurationEvent> events = new HashMap<>();
    for (final ConfigurationEvent e : l.events)
    {
        assertEquals("Wrong event type", eventType, e.getEventType());
        assertTrue("Unknown property: " + e.getPropertyName(), src
                .containsKey(e.getPropertyName()));
        if (!e.isBeforeUpdate())
        {
            assertTrue("After event without before event", events
                    .containsKey(e.getPropertyName()));
        }
        else
        {
            events.put(e.getPropertyName(), e);
        }
    }

    for (final Iterator<String> it = src.getKeys(); it.hasNext();)
    {
        final String key = it.next();
        assertTrue("No event received for key " + key, events
                .containsKey(key));
    }
}
 
Example #30
Source File: TestPropertiesConfigurationLayout.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests if a before update event is correctly ignored.
 */
@Test
public void testEventAddBefore()
{
    final ConfigurationEvent event = new ConfigurationEvent(this,
            ConfigurationEvent.ADD_PROPERTY, TEST_KEY, TEST_VALUE,
            true);
    layout.onEvent(event);
    assertFalse("Property already stored", layout.getKeys().contains(
            TEST_KEY));
}