TAMAYA-190: Simplified event component, updated docs.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/commit/7505b007 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/7505b007 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/7505b007 Branch: refs/heads/master Commit: 7505b0078e61801ba3f2cc965e2d69141b0cc547 Parents: 9f3e3cf Author: anatole <[email protected]> Authored: Mon Nov 7 01:44:54 2016 +0100 Committer: anatole <[email protected]> Committed: Mon Nov 7 01:45:08 2016 +0100 ---------------------------------------------------------------------- .../org/apache/tamaya/events/ChangeType.java | 31 --- .../tamaya/events/ConfigurationChange.java | 18 +- .../events/ConfigurationContextChange.java | 210 ------------------- .../ConfigurationContextChangeBuilder.java | 174 --------------- .../tamaya/events/FrozenConfiguration.java | 10 +- .../tamaya/events/PropertySourceChange.java | 30 --- .../events/PropertySourceChangeBuilder.java | 17 +- .../tamaya/events/delta/package-info.java | 23 -- .../folderobserver/FileChangeListener.java | 144 ------------- .../folderobserver/FileChangeObserver.java | 33 --- .../ObservingPropertySourceProvider.java | 209 ------------------ .../events/folderobserver/package-info.java | 24 --- .../internal/DefaultConfigChangeObserver.java | 9 +- ...faultConfigurationContextChangeListener.java | 71 ------- ...org.apache.tamaya.events.ConfigEventListener | 19 -- .../events/ChangeableGlobalPropertySource.java | 1 - .../tamaya/events/ConfigurationChangeTest.java | 161 ++++++++++++++ .../tamaya/events/ObservedConfigTest.java | 52 +++-- .../tamaya/events/PropertySourceChangeTest.java | 180 ++++++++++++++++ .../tamaya/events/TestObservingProvider.java | 92 -------- .../events/delta/ConfigurationChangeTest.java | 163 -------------- .../delta/ConfigurationContextChangeTest.java | 138 ------------ .../events/delta/PropertySourceChangeTest.java | 209 ------------------ .../folderobserver/FileChangeListener.java | 144 +++++++++++++ .../folderobserver/FileChangeObserver.java | 33 +++ .../ObservingPropertySourceProvider.java | 190 +++++++++++++++++ .../folderobserver/TestObservingProvider.java | 91 ++++++++ .../events/folderobserver/package-info.java | 24 +++ ...org.apache.tamaya.events.ConfigEventListener | 2 +- ...org.apache.tamaya.spi.PropertySourceProvider | 2 +- 30 files changed, 882 insertions(+), 1622 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7505b007/modules/events/src/main/java/org/apache/tamaya/events/ChangeType.java ---------------------------------------------------------------------- diff --git a/modules/events/src/main/java/org/apache/tamaya/events/ChangeType.java b/modules/events/src/main/java/org/apache/tamaya/events/ChangeType.java deleted file mode 100644 index 2059017..0000000 --- a/modules/events/src/main/java/org/apache/tamaya/events/ChangeType.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.events; - -/** - * Enum describing the type of configuration change. - */ -public enum ChangeType { - /** Configuration hase been added. */ - NEW, - /** Configuration hase been removed. */ - DELETED, - /** Configuration hase been changed. */ - UPDATED, -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7505b007/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationChange.java ---------------------------------------------------------------------- diff --git a/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationChange.java b/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationChange.java index c31cda2..9cdd4fc 100644 --- a/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationChange.java +++ b/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationChange.java @@ -121,6 +121,9 @@ public final class ConfigurationChange implements ConfigEvent<Configuration>, Se public int getRemovedSize() { int removedCount = 0; for(PropertyChangeEvent ev:this.changes.values()){ + if(ev.getPropertyName().startsWith("_")){ + continue; + } if(ev.getNewValue() == null){ removedCount++; } @@ -135,6 +138,9 @@ public final class ConfigurationChange implements ConfigEvent<Configuration>, Se public int getAddedSize() { int addedCount = 0; for(PropertyChangeEvent ev:this.changes.values()){ + if(ev.getPropertyName().startsWith("_")){ + continue; + } if(ev.getOldValue() == null && ev.getNewValue() != null){ addedCount++; @@ -150,6 +156,9 @@ public final class ConfigurationChange implements ConfigEvent<Configuration>, Se public int getUpdatedSize() { int updatedCount = 0; for(PropertyChangeEvent ev:this.changes.values()){ + if(ev.getPropertyName().startsWith("_")){ + continue; + } if( ev.getOldValue()!=null && ev.getNewValue()!=null){ updatedCount++; } @@ -210,9 +219,12 @@ public final class ConfigurationChange implements ConfigEvent<Configuration>, Se @Override public String toString() { return "ConfigurationChange{" + - "configuration=" + configuration + - ", version='" + version + '\'' + - ", timestamp=" + timestamp + + "\n configuration-id = " + configuration.getOrDefault("_id", "-") + + "\n change-id = " + version + + "\n timestamp = " + timestamp + + "\n added = " + this.getAddedSize() + + "\n updated = " + this.getUpdatedSize() + + "\n removed = " + this.getRemovedSize() + '\n' + '}'; } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7505b007/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationContextChange.java ---------------------------------------------------------------------- diff --git a/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationContextChange.java b/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationContextChange.java deleted file mode 100644 index 4e12d42..0000000 --- a/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationContextChange.java +++ /dev/null @@ -1,210 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.events; - -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.PropertySource; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.UUID; - -/** - * Event that contains a set of current changes that were applied or can be applied. - * This class is immutable and thread-safe. To create instances use - * {@link PropertySourceChangeBuilder}. - * - * Created by Anatole on 22.10.2014. - */ -public final class ConfigurationContextChange implements ConfigEvent<ConfigurationContext>, Serializable{ - - private static final long serialVersionUID = 1L; - /** The base property provider/configuration. */ - private final List<PropertySourceChange> changedPropertySources = new ArrayList<>(); - /** The base version, usable for optimistic locking. */ - private String version = UUID.randomUUID().toString(); - /** The timestamp of the change set in millis from the epoch. */ - private long timestamp = System.currentTimeMillis(); - /** The configuration context. */ - private final ConfigurationContext configurationContext; - - /** - * Get an empty change set for the given provider. - * - * @param configurationContext context to use for creating changesets. - * @return an empty ConfigurationContextChange instance. - */ - public static ConfigurationContextChange emptyChangeSet(ConfigurationContext configurationContext){ - return ConfigurationContextChangeBuilder.of(configurationContext).build(); - } - - /** - * Constructor used by {@link PropertySourceChangeBuilder}. - * @param builder The builder used, not null. - */ - ConfigurationContextChange(ConfigurationContextChangeBuilder builder) { - this.changedPropertySources.addAll(builder.changedPropertySources); - if(builder.version!=null){ - this.version = builder.version; - } - if(builder.timestamp!=null){ - this.timestamp = builder.timestamp; - } - this.configurationContext = builder.configurationContext; - } - - @Override - public Class<ConfigurationContext> getResourceType() { - return ConfigurationContext.class; - } - - @Override - public ConfigurationContext getResource() { - return configurationContext; - } - - /** - * Get the base version, usable for optimistic locking. - * @return the base version. - */ - @Override - public String getVersion(){ - return version; - } - - /** - * Get the timestamp in millis from the current epoch. it is expected that the timestamp and the version are unique to - * identify a changeset. - * @return the timestamp, when this changeset was created. - */ - @Override - public long getTimestamp(){ - return timestamp; - } - - /** - * Get the changes recorded. - * @return the recorded changes, never null. - */ - public Collection<PropertySourceChange> getPropertySourceChanges(){ - return Collections.unmodifiableCollection(this.changedPropertySources); - } - - /** - * Get the property source updates. - * @return the recorded changes, never null. - */ - public Collection<PropertySourceChange> getPropertySourceUpdates(){ - List<PropertySourceChange> result = new ArrayList<>(); - for (PropertySourceChange pc : this.changedPropertySources) { - if (pc.getChangeType() == ChangeType.UPDATED) { - result.add(pc); - } - } - return result; -// return Collections.unmodifiableCollection(this.changedPropertySources).stream() -// .filter(pc -> pc.getChangeType()==ChangeType.UPDATED).collect(Collectors.toList()); - } - - /** - * Get the property sources to be removed. - * @return the recorded changes, never null. - */ - public Collection<PropertySource> getRemovedPropertySources(){ - List<PropertySource> result = new ArrayList<>(); - for (PropertySourceChange pc : this.changedPropertySources) { - if (pc.getChangeType() == ChangeType.DELETED) { - result.add(pc.getResource()); - } - } - return result; -// return getPropertySourceChanges().stream().filter(pc -> pc.getChangeType()==ChangeType.DELETED). -// map(ps -> ps.getPropertySource()).collect(Collectors.toList()); - } - - /** - * Get the property sources to be added. - * @return the recorded changes, never null. - */ - public Collection<PropertySource> getAddedPropertySources(){ - List<PropertySource> result = new ArrayList<>(); - for (PropertySourceChange pc : this.changedPropertySources) { - if (pc.getChangeType() == ChangeType.NEW) { - result.add(pc.getResource()); - } - } - return result; -// return getPropertySourceChanges().stream().filter(pc -> pc.getChangeType()==ChangeType.NEW). -// map(ps -> ps.getPropertySource()).collect(Collectors.toList()); - } - - /** - * Get the property sources to be updated. - * @return the recorded changes, never null. - */ - public Collection<PropertySource> getUpdatedPropertySources(){ - List<PropertySource> result = new ArrayList<>(); - for (PropertySourceChange pc : this.changedPropertySources) { - if (pc.getChangeType() == ChangeType.UPDATED) { - result.add(pc.getResource()); - } - } - return result; -// return getPropertySourceChanges().stream().filter(pc -> pc.getChangeType()==ChangeType.UPDATED). -// map(ps -> ps.getPropertySource()).collect(Collectors.toList()); - } - - /** - * Checks if the given propertySource is affected (added, changed or removed). - * @param propertySource the propertySource, not null. - * @return true, if the given propertySource ia affected. - */ - public boolean isAffected(PropertySource propertySource) { - for (PropertySourceChange ps : this.changedPropertySources) { - if (ps.getResource() == propertySource || - ps.getResource().getName().equals(propertySource.getName())) { - return true; - } - } - return false; -// return this.changedPropertySources.stream().filter(ps -> ps.getPropertySource()==propertySource || -// ps.getPropertySource().getName().equals(propertySource.getName())).findAny().isPresent(); - } - - /** - * CHecks if the current change set does not contain any changes. - * @return tru, if the change set is empty. - */ - public boolean isEmpty(){ - return this.changedPropertySources.isEmpty(); - } - - - @Override - public String toString() { - return "ConfigurationContextChange{" + - "changedPropertySources=" + changedPropertySources + - ", version='" + version + '\'' + - ", timestamp=" + timestamp + - '}'; - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7505b007/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationContextChangeBuilder.java ---------------------------------------------------------------------- diff --git a/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationContextChangeBuilder.java b/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationContextChangeBuilder.java deleted file mode 100644 index b586428..0000000 --- a/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationContextChangeBuilder.java +++ /dev/null @@ -1,174 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.events; - -import org.apache.tamaya.ConfigurationProvider; -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.PropertySource; - -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; - -/** - * Models a set of current changes applied to a {@link org.apache.tamaya.spi.PropertySource}. Consumers of these events - * can observe changes to property sources and - * <ol> - * <li>check if their current configuration instance ({@link org.apache.tamaya.spi.ConfigurationContext} - * contains the changed {@link org.apache.tamaya.spi.PropertySource} (Note: the reference to a property source is never affected by a - * change, it is the data of the property source only).</li> - * <li>if so, a corresponding action may be taken, such as reevaluating the configuration values (depending on - * the update policy) or reevaluating the complete {@link org.apache.tamaya.Configuration} to create a change - * event on configuration level. - * </ol> - */ -public final class ConfigurationContextChangeBuilder { - /** - * The recorded changes. - */ - final List<PropertySourceChange> changedPropertySources = new ArrayList<>(); - /** - * The version configured, or null, for generating a default. - */ - String version; - /** - * The optional timestamp in millis of this epoch. - */ - Long timestamp; - - final ConfigurationContext configurationContext; - - /** - * Constructor. - */ - private ConfigurationContextChangeBuilder(ConfigurationContext configurationContext) { - this.configurationContext = Objects.requireNonNull(configurationContext); - } - - /** - * Just creates a new ConfigurationContextBuilder using the current COnfigurationContext has root resource. - * @return a new ConfigurationContextBuilder, never null. - */ - public static ConfigurationContextChangeBuilder of() { - return of(ConfigurationProvider.getConfigurationContext()); - } - - /** - * Creates a new instance current this builder. - * - * @param context context to use for creating changesets. - * @return the builder for chaining. - */ - public static ConfigurationContextChangeBuilder of(ConfigurationContext context) { - return new ConfigurationContextChangeBuilder(context); - } - - /** - * Apply a version/UUID to the set being built. - * @param version the version to apply, or null, to let the system generate a version for you. - * @return the builder for chaining. - */ - public ConfigurationContextChangeBuilder setVersion(String version) { - this.version = version; - return this; - } - - /** - * Apply given timestamp to the set being built. - * @param timestamp timestamp to set. - * @return the builder for chaining. - */ - public ConfigurationContextChangeBuilder setTimestamp(long timestamp) { - this.timestamp = timestamp; - return this; - } - - /** - * This method records all changes to be applied to the base property provider/configuration to - * achieve the given target state. - * - * @param propertySource the new target state, not null. - * @return the builder for chaining. - */ - public ConfigurationContextChangeBuilder newPropertySource(PropertySource propertySource) { - this.changedPropertySources.add(PropertySourceChange.ofAdded(propertySource)); - return this; - } - - /** - * This method records all changes to be applied to the base property provider/configuration to - * achieve the given target state. - * - * @param propertySource the new target state, not null. - * @return the builder for chaining. - */ - public ConfigurationContextChangeBuilder removedPropertySource(PropertySource propertySource) { - this.changedPropertySources.add(PropertySourceChange.ofDeleted(propertySource)); - return this; - } - - /** - * This method records all changes to be applied to the base property provider/configuration to - * achieve the given target state. - * - * @param propertySourceChange the change state, not null. - * @return the builder for chaining. - */ - public ConfigurationContextChangeBuilder changedPropertySource(PropertySourceChange propertySourceChange) { - this.changedPropertySources.add(Objects.requireNonNull(propertySourceChange)); - return this; - } - - /** - * Checks if the change set is empty, i.e. does not contain any changes. - * - * @return true, if the set is empty. - */ - public boolean isEmpty() { - return this.changedPropertySources.isEmpty(); - } - - /** - * Resets this change set instance. This will clear all changes done to this builder, so the - * set will be empty. - */ - public void reset() { - this.changedPropertySources.clear(); - } - - /** - * Builds the corresponding change set. - * - * @return the new change set, never null. - */ - public ConfigurationContextChange build() { - return new ConfigurationContextChange(this); - } - - /* - * (non-Javadoc) - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return "ConfigurationContextChangeBuilder [propertySources=" + changedPropertySources + "]"; - } - - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7505b007/modules/events/src/main/java/org/apache/tamaya/events/FrozenConfiguration.java ---------------------------------------------------------------------- diff --git a/modules/events/src/main/java/org/apache/tamaya/events/FrozenConfiguration.java b/modules/events/src/main/java/org/apache/tamaya/events/FrozenConfiguration.java index 304ddba..2a4540c 100644 --- a/modules/events/src/main/java/org/apache/tamaya/events/FrozenConfiguration.java +++ b/modules/events/src/main/java/org/apache/tamaya/events/FrozenConfiguration.java @@ -30,10 +30,7 @@ import org.apache.tamaya.spi.ConversionContext; import org.apache.tamaya.spi.PropertyConverter; import java.io.Serializable; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; @@ -56,7 +53,10 @@ public final class FrozenConfiguration implements Configuration, Serializable { */ private FrozenConfiguration(Configuration config) { this.properties.putAll(config.getProperties()); - this.properties.put("[meta]frozenAt", String.valueOf(System.currentTimeMillis())); + this.properties.put("_frozenAt", String.valueOf(System.currentTimeMillis())); + if(!this.properties.containsKey("_id")) { + this.properties.put("_id", UUID.randomUUID().toString()); + } this.properties = Collections.unmodifiableMap(this.properties); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7505b007/modules/events/src/main/java/org/apache/tamaya/events/PropertySourceChange.java ---------------------------------------------------------------------- diff --git a/modules/events/src/main/java/org/apache/tamaya/events/PropertySourceChange.java b/modules/events/src/main/java/org/apache/tamaya/events/PropertySourceChange.java index 063612c..e7782a0 100644 --- a/modules/events/src/main/java/org/apache/tamaya/events/PropertySourceChange.java +++ b/modules/events/src/main/java/org/apache/tamaya/events/PropertySourceChange.java @@ -46,8 +46,6 @@ public final class PropertySourceChange implements ConfigEvent<PropertySource>, private long timestamp = System.currentTimeMillis(); /** The recorded changes. */ private final Map<String,PropertyChangeEvent> changes = new HashMap<>(); - /** The overall type of change. */ - private final ChangeType changeType; /** * Constructor used by {@link PropertySourceChangeBuilder}. @@ -64,15 +62,6 @@ public final class PropertySourceChange implements ConfigEvent<PropertySource>, if(builder.timestamp!=null){ this.timestamp = builder.timestamp; } - this.changeType = builder.changeType; - } - - /** - * Gets the type of change for this PropertySource. - * @return the type of change for this PropertySource, never null. - */ - public ChangeType getChangeType(){ - return this.changeType; } @Override @@ -212,28 +201,9 @@ public final class PropertySourceChange implements ConfigEvent<PropertySource>, } - /** - * Create a change event for a new PropertySource that was added. - * @param propertySource the new property source, not null. - * @return a new PropertySourceChange, representing a PropertySource that was added. - */ - public static PropertySourceChange ofAdded(PropertySource propertySource) { - return PropertySourceChangeBuilder.of(propertySource, ChangeType.NEW).build(); - } - - /** - * Create a change event for a deleted PropertySource. - * @param propertySource the deleted property source, not null. - * @return a new PropertySourceChange, representing a PropertySource that was deleted. - */ - public static PropertySourceChange ofDeleted(PropertySource propertySource) { - return PropertySourceChangeBuilder.of(propertySource, ChangeType.DELETED).build(); - } - @Override public String toString() { return "PropertySourceChange{" + - "changeType=" + changeType + ", propertySource=" + propertySource + ", version='" + version + '\'' + ", timestamp=" + timestamp + http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7505b007/modules/events/src/main/java/org/apache/tamaya/events/PropertySourceChangeBuilder.java ---------------------------------------------------------------------- diff --git a/modules/events/src/main/java/org/apache/tamaya/events/PropertySourceChangeBuilder.java b/modules/events/src/main/java/org/apache/tamaya/events/PropertySourceChangeBuilder.java index fa2cf5e..4c873bd 100644 --- a/modules/events/src/main/java/org/apache/tamaya/events/PropertySourceChangeBuilder.java +++ b/modules/events/src/main/java/org/apache/tamaya/events/PropertySourceChangeBuilder.java @@ -60,29 +60,23 @@ public final class PropertySourceChangeBuilder { */ Long timestamp; - /** The type of change. */ - ChangeType changeType; - /** * Constructor. * * @param source the underlying configuration/provider, not null. - * @param changeType kind of change. */ - private PropertySourceChangeBuilder(PropertySource source, ChangeType changeType) { + private PropertySourceChangeBuilder(PropertySource source) { this.source = Objects.requireNonNull(source); - this.changeType = Objects.requireNonNull(changeType); } /** * Creates a new instance of this builder. * * @param source the underlying property provider/configuration, not null. - * @param changeType kind of change. * @return the builder for chaining. */ - public static PropertySourceChangeBuilder of(PropertySource source, ChangeType changeType) { - return new PropertySourceChangeBuilder(source, changeType); + public static PropertySourceChangeBuilder of(PropertySource source) { + return new PropertySourceChangeBuilder(source); } /** @@ -235,10 +229,6 @@ public final class PropertySourceChangeBuilder { this.delta.clear(); } - public PropertySourceChangeBuilder setChangeType(ChangeType changeType) { - this.changeType = changeType; - return this; - } /** * Builds the corresponding change set. @@ -259,5 +249,4 @@ public final class PropertySourceChangeBuilder { ", delta=" + delta + "]"; } - } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7505b007/modules/events/src/main/java/org/apache/tamaya/events/delta/package-info.java ---------------------------------------------------------------------- diff --git a/modules/events/src/main/java/org/apache/tamaya/events/delta/package-info.java b/modules/events/src/main/java/org/apache/tamaya/events/delta/package-info.java deleted file mode 100644 index 2006717..0000000 --- a/modules/events/src/main/java/org/apache/tamaya/events/delta/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -/** - * This package contains artifacts to describe the changes (delta) of a - * Configuration or a PropertySource. - */ -package org.apache.tamaya.events.delta; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7505b007/modules/events/src/main/java/org/apache/tamaya/events/folderobserver/FileChangeListener.java ---------------------------------------------------------------------- diff --git a/modules/events/src/main/java/org/apache/tamaya/events/folderobserver/FileChangeListener.java b/modules/events/src/main/java/org/apache/tamaya/events/folderobserver/FileChangeListener.java deleted file mode 100644 index 283719e..0000000 --- a/modules/events/src/main/java/org/apache/tamaya/events/folderobserver/FileChangeListener.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.events.folderobserver; - -import org.apache.tamaya.ConfigException; - -import java.io.IOException; -import java.nio.file.FileSystem; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardWatchEventKinds; -import java.nio.file.WatchEvent; -import java.nio.file.WatchKey; -import java.nio.file.WatchService; -import java.util.logging.Level; -import java.util.logging.Logger; - - -/** - * Class that has the responsibility to watch the folder and then publish the changes to a - * {@link org.apache.tamaya.events.PropertySourceChange}. - * @see ObservingPropertySourceProvider - * This listener will wait to events and wait to one second to watch again. - * <p>If new file was created or modified will commit from this file.</p> - * <p>If a file was removed then the listener will load using all files left.</p> - * @author otaviojava - */ -class FileChangeListener implements Runnable { - - private static final Logger LOGGER = Logger.getLogger(FileChangeListener.class.getName()); - - private final WatchService watchService; - - private final FileChangeObserver observer; - - private final Path directory; - - private volatile boolean running = true; - - public FileChangeListener(Path directory, FileChangeObserver observer) { - this.observer = observer; - this.directory = directory; - this.watchService = getWatchService(); - - if (watchService!=null && directory!=null) { - try { - directory.register(watchService, - StandardWatchEventKinds.ENTRY_DELETE, - StandardWatchEventKinds.ENTRY_MODIFY, - StandardWatchEventKinds.ENTRY_CREATE); - } catch (IOException e) { - throw new FileChangeListenerException("An error happened when does try to registry to watch the folder", e); - } - } - } - - /** - * Stops the listener service from observing the target directory. - */ - public void stopListener(){ - running = false; - } - - @Override - public void run() { - if (watchService!=null || directory!=null) { - return; - } - while (running) { - watchFolder(); - } - } - - /** - * Start watching the current folder. - */ - private void watchFolder() { - try { - WatchKey watckKey = watchService.take(); - for (WatchEvent<?> event : watckKey.pollEvents()) { - Path filePath = (Path) watckKey.watchable(); - if(event.kind().equals(StandardWatchEventKinds.ENTRY_CREATE)|| - event.kind().equals(StandardWatchEventKinds.ENTRY_MODIFY) || - event.kind().equals(StandardWatchEventKinds.ENTRY_DELETE)){ - LOGGER.info("File change detected in: " + filePath.getFileName()); - observer.directoryChanged(filePath); - } - } - watckKey.reset(); - Thread.sleep(1_000L); - } catch (Exception e) { - throw new FileChangeListenerException("An error happened when does try to watch the folder", e); - } - } - - /** - * Get the watch service. - * @return the watch service, or null, if the watch service is not supported. - */ - private WatchService getWatchService() { - try { - FileSystem fileSystem = Paths.get(".").getFileSystem(); - return fileSystem.newWatchService(); - } catch (IOException e) { - LOGGER.log(Level.WARNING, "The file System does not supports WatchService", e); - return null; - } - - } - - /** - * Exception if file listening fails. - */ - static class FileChangeListenerException extends ConfigException { - /** Serialversion UID. */ - private static final long serialVersionUID = -8965486770881001513L; - - /** - * Constructor. - * @param message a message - * @param cause an (optional) root cause. - */ - public FileChangeListenerException(String message, Throwable cause) { - super(message, cause); - } - - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7505b007/modules/events/src/main/java/org/apache/tamaya/events/folderobserver/FileChangeObserver.java ---------------------------------------------------------------------- diff --git a/modules/events/src/main/java/org/apache/tamaya/events/folderobserver/FileChangeObserver.java b/modules/events/src/main/java/org/apache/tamaya/events/folderobserver/FileChangeObserver.java deleted file mode 100644 index 63d25cd..0000000 --- a/modules/events/src/main/java/org/apache/tamaya/events/folderobserver/FileChangeObserver.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.events.folderobserver; - -import java.nio.file.Path; - -/** - * Observer to be used in {@link FileChangeListener} to commit all configurations and provider. - */ -interface FileChangeObserver { - /** - * Called when a file has been modified. - * @param path the file path, not null. - */ - void directoryChanged(Path path); - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7505b007/modules/events/src/main/java/org/apache/tamaya/events/folderobserver/ObservingPropertySourceProvider.java ---------------------------------------------------------------------- diff --git a/modules/events/src/main/java/org/apache/tamaya/events/folderobserver/ObservingPropertySourceProvider.java b/modules/events/src/main/java/org/apache/tamaya/events/folderobserver/ObservingPropertySourceProvider.java deleted file mode 100644 index feddd70..0000000 --- a/modules/events/src/main/java/org/apache/tamaya/events/folderobserver/ObservingPropertySourceProvider.java +++ /dev/null @@ -1,209 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.events.folderobserver; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URISyntaxException; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.logging.Level; -import java.util.logging.Logger; - -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.events.ConfigEventManager; -import org.apache.tamaya.events.ConfigurationContextChange; -import org.apache.tamaya.events.ConfigurationContextChangeBuilder; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertySourceProvider; -import org.apache.tamaya.spisupport.BasePropertySource; - -/** - * This implementation runs in a folder taking up all files compatible with the given - * ConfigurationFormats. When a file is added, deleted or modified the PropertySourceProvider - * will adapt the changes automatically and trigger according - * {@link org.apache.tamaya.events.PropertySourceChange} events. - * The default folder is META-INF/config, but you can change it via an absolute path in the - * "-Dtamaya.configdir" parameter. - */ -public class ObservingPropertySourceProvider implements PropertySourceProvider, FileChangeObserver { - /** - * The logger. - */ - private static final Logger LOG = Logger.getLogger(ObservingPropertySourceProvider.class.getName()); - /** - * The current active property sources of this provider. - */ - private final List<PropertySource> propertySources = Collections.synchronizedList(new LinkedList<PropertySource>()); - /** - * The thread pool used. - */ - private final ExecutorService executor = Executors.newSingleThreadExecutor(); - - /** - * Constructorm using an explicit directory, ignoring all kind of configuration, if set. - * - * @param directory the target directory. If null, the default configuration and system property are used. - */ - public ObservingPropertySourceProvider(Path directory) { - if (directory == null) { - directory = getDirectory(); - } - if (directory!=null){ - synchronized (this.propertySources) { - this.propertySources.addAll(readConfiguration(directory)); - } - final Runnable runnable = new FileChangeListener(directory, this); - executor.execute(runnable); - } else { - executor.shutdown(); - } - } - - /** - * Read the initial configuration. - * - * @param directory the target directory, not null. - */ - private List<PropertySource> readConfiguration(Path directory) { - final List<PropertySource> result = new ArrayList<>(); - try { - synchronized (propertySources) { - for (final Path path : Files.newDirectoryStream(directory, "*")) { - result.addAll(getPropertySources(path)); - } - return result; - } - } catch (final IOException e) { - LOG.log(Level.WARNING, "Failed to read configuration from dir: " + directory, e); - } - return result; - } - - /** - * Read property sources from the given file. - * - * @param file source of the property sources. - * @return property sources from the given file. - */ - protected Collection<PropertySource> getPropertySources(final Path file) { - return Arrays.asList(new PropertySource[]{new BasePropertySource() { - private final Map<String,String> props = readProperties(file); - - @Override - public Map<String, String> getProperties() { - return props; - } - }}); - } - - /** - * Load a single file. - * - * @param file the file, not null. - * @return properties as read from the given file. - */ - protected static Map<String,String> readProperties(Path file) { - try (InputStream is = file.toUri().toURL().openStream()){ - final Properties props = new Properties(); - props.load(is); - final Map<String,String> result = new HashMap<>(); - for(final Map.Entry<Object,Object> en:props.entrySet()){ - result.put(String.valueOf(en.getKey()), String.valueOf(en.getValue())); - } - return result; - } catch (final Exception e) { - LOG.log(Level.INFO, "Error reading file: " + file.toString() + - ", using format: properties", e); - } - return Collections.emptyMap(); - } - - - /** - * Evaluates the target directory from system property (tamaya.configdir) or classpath. - * - * @return the directory to be read, or null. - */ - private Path getDirectory() { - final String absolutePath = System.getProperty("tamaya.configdir"); - if (null!=absolutePath) { - final Path path = Paths.get(absolutePath); - if (Files.isDirectory(path)) { - return path; - } - } - final URL resource = ObservingPropertySourceProvider.class.getResource("/META-INF/config/"); - if (null!=resource) { - try { - return Paths.get(resource.toURI()); - } catch (final URISyntaxException e) { - throw new ConfigException("An error to find the directory to watch", e); - } - } - return null; - } - - - @Override - public void directoryChanged(Path directory) { - synchronized (this.propertySources) { - final List<PropertySource> existingPropertySources = new ArrayList<>(propertySources); - propertySources.clear(); - final Collection<PropertySource> sourcesRead = readConfiguration(directory); - this.propertySources.addAll(sourcesRead); - triggerConfigChange(existingPropertySources, propertySources); - } - } - - - private void triggerConfigChange(List<PropertySource> originalPropertySources, - List<PropertySource> newPropertySources) { - final ConfigurationContextChangeBuilder b = ConfigurationContextChangeBuilder.of(); - for (final PropertySource ps : originalPropertySources) { - b.removedPropertySource(ps); - } - for (final PropertySource ps : newPropertySources) { - b.newPropertySource(ps); - } - final ConfigurationContextChange changeEvent = b.build(); - LOG.fine("Trigger Config Context Change: " + changeEvent); - ConfigEventManager.fireEvent(changeEvent); - } - - @Override - public Collection<PropertySource> getPropertySources() { - synchronized (propertySources) { - return new ArrayList<>(this.propertySources); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7505b007/modules/events/src/main/java/org/apache/tamaya/events/folderobserver/package-info.java ---------------------------------------------------------------------- diff --git a/modules/events/src/main/java/org/apache/tamaya/events/folderobserver/package-info.java b/modules/events/src/main/java/org/apache/tamaya/events/folderobserver/package-info.java deleted file mode 100644 index 347f2d8..0000000 --- a/modules/events/src/main/java/org/apache/tamaya/events/folderobserver/package-info.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -/** - * This package contains code to observe a folder for file changes and to trigger - * corresponding events, that are handled by an according {@link org.apache.tamaya.events.folderobserver.ObservingPropertySourceProvider} - * instance. - */ -package org.apache.tamaya.events.folderobserver; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7505b007/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigChangeObserver.java ---------------------------------------------------------------------- diff --git a/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigChangeObserver.java b/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigChangeObserver.java index f4457b2..34bf5eb 100644 --- a/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigChangeObserver.java +++ b/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigChangeObserver.java @@ -62,17 +62,18 @@ public class DefaultConfigChangeObserver { public void checkConfigurationUpdate() { LOG.finest("Checking configuration for changes..."); - FrozenConfiguration newConfig = FrozenConfiguration.of(ConfigurationProvider.getConfiguration()); + FrozenConfiguration frozenConfig = FrozenConfiguration.of(ConfigurationProvider.getConfiguration()); ConfigurationChange changes; if(lastConfig==null){ - changes = ConfigurationChangeBuilder.of(newConfig).putAll(newConfig.getProperties()) + lastConfig = frozenConfig; + changes = ConfigurationChangeBuilder.of().putAll(frozenConfig.getProperties()) .build(); }else{ - changes = ConfigurationChangeBuilder.of(lastConfig).addChanges(newConfig) + changes = ConfigurationChangeBuilder.of(lastConfig).addChanges(frozenConfig) .build(); } if(!changes.isEmpty()) { - LOG.info("Identified configuration changes, publishing change event..."); + LOG.info("Identified configuration changes, publishing changes:\n" + changes); ConfigEventManager.fireEvent(changes); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7505b007/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigurationContextChangeListener.java ---------------------------------------------------------------------- diff --git a/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigurationContextChangeListener.java b/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigurationContextChangeListener.java deleted file mode 100644 index 8a9ff64..0000000 --- a/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigurationContextChangeListener.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.events.internal; - -import org.apache.tamaya.ConfigurationProvider; -import org.apache.tamaya.events.ConfigEvent; -import org.apache.tamaya.events.ConfigEventListener; -import org.apache.tamaya.events.ConfigurationContextChange; -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.ConfigurationContextBuilder; -import org.apache.tamaya.spi.PropertySource; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Default ConfigEventListener for ConfigurationContextChange events that updates the current context, if resources were - * affected. - */ -public class DefaultConfigurationContextChangeListener implements ConfigEventListener { - - private static final Logger LOG = Logger.getLogger(DefaultConfigurationContextChangeListener.class.getName()); - - @Override - public void onConfigEvent(ConfigEvent<?> event) { - if(event.getClass() == ConfigurationContextChange.class) { - ConfigurationContextChange contextChange = (ConfigurationContextChange) event; - ConfigurationContext context = ConfigurationProvider.getConfigurationContext(); - List<PropertySource> affectedPropertySources = new ArrayList<>(); - for (PropertySource ps : context.getPropertySources()) { - if (contextChange.isAffected(ps)) { - affectedPropertySources.add(ps); - } - } - ConfigurationContextBuilder newContextBuilder = ConfigurationProvider.getConfigurationContextBuilder() - .setContext(context); - if (!affectedPropertySources.isEmpty()) { - Set<String> propertySourceNames = new HashSet<>(); - newContextBuilder.removePropertySources(contextChange.getRemovedPropertySources()); - } - newContextBuilder.addPropertySources(contextChange.getAddedPropertySources()); - newContextBuilder.addPropertySources(contextChange.getUpdatedPropertySources()); - ConfigurationContext newContext = newContextBuilder.build(); - try { - ConfigurationProvider.setConfigurationContext(newContext); - } catch (Exception e) { - LOG.log(Level.INFO, "Failed to update the current ConfigurationContext due to config model changes", e); - } - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7505b007/modules/events/src/main/resources/META-INF/services/org.apache.tamaya.events.ConfigEventListener ---------------------------------------------------------------------- diff --git a/modules/events/src/main/resources/META-INF/services/org.apache.tamaya.events.ConfigEventListener b/modules/events/src/main/resources/META-INF/services/org.apache.tamaya.events.ConfigEventListener deleted file mode 100644 index f9942c1..0000000 --- a/modules/events/src/main/resources/META-INF/services/org.apache.tamaya.events.ConfigEventListener +++ /dev/null @@ -1,19 +0,0 @@ -# -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy current the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. -# -org.apache.tamaya.events.internal.DefaultConfigurationContextChangeListener http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7505b007/modules/events/src/test/java/org/apache/tamaya/events/ChangeableGlobalPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/events/src/test/java/org/apache/tamaya/events/ChangeableGlobalPropertySource.java b/modules/events/src/test/java/org/apache/tamaya/events/ChangeableGlobalPropertySource.java index 0384064..476aaf6 100644 --- a/modules/events/src/test/java/org/apache/tamaya/events/ChangeableGlobalPropertySource.java +++ b/modules/events/src/test/java/org/apache/tamaya/events/ChangeableGlobalPropertySource.java @@ -20,7 +20,6 @@ package org.apache.tamaya.events; import org.apache.tamaya.core.propertysource.BasePropertySource; -import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7505b007/modules/events/src/test/java/org/apache/tamaya/events/ConfigurationChangeTest.java ---------------------------------------------------------------------- diff --git a/modules/events/src/test/java/org/apache/tamaya/events/ConfigurationChangeTest.java b/modules/events/src/test/java/org/apache/tamaya/events/ConfigurationChangeTest.java new file mode 100644 index 0000000..380c9e7 --- /dev/null +++ b/modules/events/src/test/java/org/apache/tamaya/events/ConfigurationChangeTest.java @@ -0,0 +1,161 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.events; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.junit.Test; + +import java.util.Map; + +import static org.junit.Assert.*; + +/** + * Test class for {@link ConfigurationChange}. + */ +public class ConfigurationChangeTest { + + @Test + public void testEmptyChangeSet() throws Exception { + ConfigurationChange change = ConfigurationChange.emptyChangeSet(ConfigurationProvider.getConfiguration()); + assertNotNull(change); + assertTrue(change.isEmpty()); + } + + @Test + public void testGetConfiguration() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + ConfigurationChange change = ConfigurationChangeBuilder.of(config).build(); + assertNotNull(change); + assertTrue(change.getUpdatedSize()==0); + assertTrue(change.getAddedSize()==0); + assertTrue(change.getRemovedSize()==0); + assertTrue(change.getChanges().size()==0); + for (Map.Entry<String, String> en : config.getProperties().entrySet()) { + if (!"[meta]frozenAt".equals(en.getKey())) { + if(en.getKey().contains("random.new")){ // dynamic generated value! + continue; + } + assertEquals("Error for " + en.getKey(), en.getValue(), change.getResource().get(en.getKey())); + } + } + } + + @Test + public void testGetVersion() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + ConfigurationChange change = ConfigurationChangeBuilder.of(config).build(); + assertNotNull(change.getVersion()); + change = ConfigurationChangeBuilder.of(config).setVersion("version2").build(); + assertNotNull(change.getVersion()); + assertEquals("version2", change.getVersion()); + } + + @Test + public void testGetTimestamp() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + ConfigurationChange change = ConfigurationChangeBuilder.of(config).build(); + assertTrue((System.currentTimeMillis() - change.getTimestamp()) <= 10L); + change = ConfigurationChangeBuilder.of(config).setTimestamp(10L).build(); + assertEquals(10L, change.getTimestamp()); + } + + @Test + public void testGetEvents() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + ConfigurationChange change = ConfigurationChangeBuilder.of(config).removeKey("key1", "key2").build(); + assertTrue(change.getChanges().size() == 2); + change = ConfigurationChangeBuilder.of(config).addChange("key1Added", "value1Added").build(); + assertTrue(change.getChanges().size() == 1); + } + + @Test + public void testGetRemovedSize() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + ConfigurationChange change = ConfigurationChangeBuilder.of(config).removeKey("java.version", "key2").build(); + assertTrue(change.getRemovedSize() == 2); + assertTrue(change.getAddedSize() == 0); + } + + @Test + public void testGetAddedSize() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + ConfigurationChange change = ConfigurationChangeBuilder.of(config).addChange("key1", "key2").build(); + assertTrue(change.getAddedSize() == 1); + assertTrue(change.getRemovedSize() == 0); + } + + @Test + public void testGetUpdatedSize() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + ConfigurationChange change = ConfigurationChangeBuilder.of(config).addChange("java.version", "1.8").build(); + assertTrue(change.getUpdatedSize() == 1); + } + + @Test + public void testIsRemoved() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + ConfigurationChange change = ConfigurationChangeBuilder.of(config).removeKey("java.version").build(); + assertTrue(change.isRemoved("java.version")); + } + + @Test + public void testIsAdded() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + ConfigurationChange change = ConfigurationChangeBuilder.of(config).addChange("key1", "key2").build(); + assertTrue(change.isAdded("key1")); + } + + @Test + public void testIsUpdated() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + ConfigurationChange change = ConfigurationChangeBuilder.of(config).addChange("java.version", "1.8").build(); + assertTrue(change.isUpdated("java.version")); + } + + @Test + public void testContainsKey() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + ConfigurationChange change = ConfigurationChangeBuilder.of(config).addChange("key1", "key2").build(); + assertTrue(change.isKeyAffected("key1")); + assertFalse(change.isKeyAffected("key2")); + change = ConfigurationChangeBuilder.of(config).removeKey("java.version").build(); + assertFalse(change.isKeyAffected("java.version")); + assertFalse(change.isKeyAffected("key2")); + } + + @Test + public void testIsEmpty() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + ConfigurationChange change = ConfigurationChangeBuilder.of(config).build(); + assertTrue(change.isEmpty()); + } + + @Test + public void testToString() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + ConfigurationChange change = ConfigurationChangeBuilder.of(config).addChange("key1", "key2").build(); + change = ConfigurationChangeBuilder.of(config).removeKey("java.version").build(); + assertTrue(change.toString().contains("timestamp")); + assertTrue(change.toString().contains("change-id")); + assertTrue(change.toString().contains("configuration-id")); + assertFalse(change.toString().contains("key1")); + assertFalse(change.toString().contains("key2")); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7505b007/modules/events/src/test/java/org/apache/tamaya/events/ObservedConfigTest.java ---------------------------------------------------------------------- diff --git a/modules/events/src/test/java/org/apache/tamaya/events/ObservedConfigTest.java b/modules/events/src/test/java/org/apache/tamaya/events/ObservedConfigTest.java index 0cd9e2b..22e9044 100644 --- a/modules/events/src/test/java/org/apache/tamaya/events/ObservedConfigTest.java +++ b/modules/events/src/test/java/org/apache/tamaya/events/ObservedConfigTest.java @@ -21,7 +21,7 @@ package org.apache.tamaya.events; import org.apache.commons.io.FileUtils; import org.apache.tamaya.Configuration; import org.apache.tamaya.ConfigurationProvider; -import org.junit.Ignore; +import org.apache.tamaya.events.folderobserver.TestObservingProvider; import org.junit.Test; import java.io.File; @@ -30,6 +30,7 @@ import java.util.Map; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * Test (currently manual) to test configuration changes. @@ -37,33 +38,38 @@ import static org.junit.Assert.assertEquals; public class ObservedConfigTest { @Test - @Ignore // reactivate later... public void testChangingConfig() throws IOException { - Configuration config = ConfigurationProvider.getConfiguration().with(TestConfigView.of()); - - Map<String, String> props = config.getProperties(); - assertEquals(props.get("test"), "test2"); - assertEquals(props.get("testValue1"), "value"); - assertNull(props.get("testValue2")); - - //insert a new properties file into the tempdirectory - FileUtils.writeStringToFile( - new File(TestObservingProvider.propertyLocation.toFile(), "test2.properties"), - "testValue2=anotherValue"); - - try { - Thread.sleep(10000); - } catch (InterruptedException e) { - e.printStackTrace(); + ConfigEventManager.setChangeMonitoringPeriod(100L); + ConfigEventManager.enableChangeMonitoring(true); + while(MyConfigObserver.event==null) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + ConfigEvent<?> event = MyConfigObserver.event; + if(event!=null) { + assertTrue(event instanceof ConfigurationChange); + ConfigurationChange cChange = (ConfigurationChange) event; + if(cChange.isAdded("random.new")){ + MyConfigObserver.event=null; + }else { + assertTrue(cChange.isUpdated("random.new")); + break; + } + } } - config = ConfigurationProvider.getConfiguration().with(TestConfigView.of()); + } + + public static final class MyConfigObserver implements ConfigEventListener{ - props = config.getProperties(); + public static volatile ConfigEvent<?> event; - assertEquals(props.get("test"), "test2"); - assertEquals(props.get("testValue1"), "value"); - assertEquals(props.get("testValue2"), "anotherValue"); + @Override + public void onConfigEvent(ConfigEvent<?> event) { + MyConfigObserver.event = event; + } } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7505b007/modules/events/src/test/java/org/apache/tamaya/events/PropertySourceChangeTest.java ---------------------------------------------------------------------- diff --git a/modules/events/src/test/java/org/apache/tamaya/events/PropertySourceChangeTest.java b/modules/events/src/test/java/org/apache/tamaya/events/PropertySourceChangeTest.java new file mode 100644 index 0000000..a8b230e --- /dev/null +++ b/modules/events/src/test/java/org/apache/tamaya/events/PropertySourceChangeTest.java @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.events; + +import org.apache.tamaya.core.propertysource.EnvironmentPropertySource; +import org.apache.tamaya.core.propertysource.SimplePropertySource; +import org.apache.tamaya.core.propertysource.SystemPropertySource; +import org.apache.tamaya.spi.PropertySource; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.*; + +/** + * Tests for {@link PropertySourceChange} and its builder. + */ +public class PropertySourceChangeTest { + + private static final PropertySource myPS = new SystemPropertySource(); + + @Test + public void testGetPropertySource() throws Exception { + PropertySourceChange change = PropertySourceChangeBuilder.of(myPS).build(); + assertEquals(change.getResource().getName(), myPS.getName()); + } + + @Test + public void testGetVersion() throws Exception { + PropertySourceChange change = PropertySourceChangeBuilder.of(myPS) + .setVersion("myVersion1").build(); + assertEquals(change.getVersion(), "myVersion1"); + } + + @Test + public void testGetTimestamp() throws Exception { + PropertySourceChange change = PropertySourceChangeBuilder.of(myPS) + .setTimestamp(111L).build(); + assertEquals(change.getTimestamp(), 111L); + } + + @Test + public void testGetEvents() throws Exception { + PropertySourceChange change = PropertySourceChangeBuilder.of(myPS) + .addChanges( + new EnvironmentPropertySource() + ).build(); + assertTrue(change.getChanges().size()>0); + } + + @Test + public void testGetRemovedSize() throws Exception { + PropertySourceChange change = PropertySourceChangeBuilder.of(myPS) + .addChanges( + new EnvironmentPropertySource() + ).build(); + assertTrue(change.getRemovedSize()>0); + } + + @Test + public void testGetAddedSize() throws Exception { + PropertySourceChange change = PropertySourceChangeBuilder.of(myPS) + .addChanges( + new EnvironmentPropertySource() + ).build(); + assertTrue(change.getAddedSize()>0); + } + + @Test + public void testGetUpdatedSize() throws Exception { + PropertySourceChange change = PropertySourceChangeBuilder.of(myPS) + .addChanges( + new EnvironmentPropertySource() + ).build(); + assertTrue(change.getUpdatedSize()==0); + } + + @Test + public void testIsRemoved() throws Exception { + Map<String, String> testData = new HashMap<>(); + testData.put("key1", "value1"); + testData.put("key2", "value2"); + PropertySource ps1 = new SimplePropertySource("test", testData); + testData = new HashMap<>(); + testData.put("key1", "value2"); + testData.put("key3", "value3"); + PropertySource ps2 = new SimplePropertySource("test", testData); + PropertySourceChange change = PropertySourceChangeBuilder.of(ps1) + .addChanges( + ps2 + ).build(); + assertFalse(change.isRemoved("key1")); + assertTrue(change.isRemoved("key2")); + assertFalse(change.isRemoved("key3")); + } + + @Test + public void testIsAdded() throws Exception { + Map<String, String> testData = new HashMap<>(); + testData.put("key1", "value1"); + testData.put("key2", "value2"); + PropertySource ps1 = new SimplePropertySource("test", testData); + testData = new HashMap<>(); + testData.put("key1", "value2"); + testData.put("key3", "value3"); + PropertySource ps2 = new SimplePropertySource("test", testData); + PropertySourceChange change = PropertySourceChangeBuilder.of(ps1) + .addChanges( + ps2 + ).build(); + assertTrue(change.isAdded("key3")); + assertFalse(change.isAdded("key2")); + assertFalse(change.isAdded("key1")); + } + + @Test + public void testIsUpdated() throws Exception { + Map<String, String> testData = new HashMap<>(); + testData.put("key1", "value1"); + testData.put("key2", "value2"); + PropertySource ps1 = new SimplePropertySource("test", testData); + testData = new HashMap<>(); + testData.put("key1", "value2"); + testData.put("key3", "value3"); + PropertySource ps2 = new SimplePropertySource("test", testData); + PropertySourceChange change = PropertySourceChangeBuilder.of(ps1) + .addChanges( + ps2 + ).build(); + assertTrue(change.isUpdated("key1")); + assertFalse(change.isUpdated("key2")); + assertFalse(change.isUpdated("key3")); + } + + @Test + public void testContainsKey() throws Exception { + PropertySourceChange change = PropertySourceChangeBuilder.of(new EnvironmentPropertySource()) + .addChanges( + myPS + ).build(); + assertTrue(change.isKeyAffected("java.version")); + } + + @Test + public void testIsEmpty() throws Exception { + PropertySourceChange change = PropertySourceChangeBuilder.of(new EnvironmentPropertySource()) + .build(); + assertTrue(change.isEmpty()); + change = PropertySourceChangeBuilder.of(new EnvironmentPropertySource()) + .addChanges( + myPS + ).build(); + assertFalse(change.isEmpty()); + } + + @Test + public void testToString() throws Exception { + PropertySourceChange change = PropertySourceChangeBuilder.of(myPS).build(); + String toString = change.toString(); + assertNotNull(toString); + assertTrue(toString.contains(myPS.getName())); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7505b007/modules/events/src/test/java/org/apache/tamaya/events/TestObservingProvider.java ---------------------------------------------------------------------- diff --git a/modules/events/src/test/java/org/apache/tamaya/events/TestObservingProvider.java b/modules/events/src/test/java/org/apache/tamaya/events/TestObservingProvider.java deleted file mode 100644 index 2685d3e..0000000 --- a/modules/events/src/test/java/org/apache/tamaya/events/TestObservingProvider.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.events; - -import org.apache.commons.io.FileUtils; -import org.apache.tamaya.events.folderobserver.ObservingPropertySourceProvider; - -import java.io.File; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Test configuration property source provider that observes a directory and updated the config if necessary. - */ -public class TestObservingProvider extends ObservingPropertySourceProvider{ - - public static Path propertyLocation; - - static{ - try { - // create some temporary config - Path tempDir = Files.createTempDirectory("observedFolder"); - - TestObservingProvider.propertyLocation = tempDir; - - FileUtils.copyInputStreamToFile( - TestObservingProvider.class.getResourceAsStream("/test.properties"), - new File(tempDir.toFile(), "test.properties")); - - Runtime.getRuntime().addShutdownHook(new Thread(){ - @Override - public void run(){ - try{ - // cleanup directory - Files.deleteIfExists(getTargetFile("test1.properties")); - Files.deleteIfExists(getTargetFile("test2.properties")); - Files.deleteIfExists(getTargetFile("test3.properties")); - } - catch(Exception e){ - Logger.getLogger("TestObservingProvider").log(Level.WARNING, - "Failed to cleanup config test dir", e); - } - } - }); - } - catch(Exception e){ - Logger.getLogger("TestObservingProvider").log(Level.WARNING, "Failed to init config test dir", e); - } - } - - private static Path getTargetFile(String name) { - File testFile = new File(TestObservingProvider.getTestDirectory(), name); - return Paths.get(testFile.toURI()); - } - - public TestObservingProvider(){ - super(propertyLocation); - Logger.getLogger(getClass().getName()).info("Using test directory: " + getTestPath()); - } - - public static File getTestDirectory(){ - String tempDir = System.getProperty("java.io.tmpdir"); - File dir = new File(tempDir, "tamaya-events-testdir"); - if(!dir.exists()){ - dir.mkdirs(); - } - return dir; - } - - private static String getTestPath(){ - return getTestDirectory().getAbsolutePath(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/7505b007/modules/events/src/test/java/org/apache/tamaya/events/delta/ConfigurationChangeTest.java ---------------------------------------------------------------------- diff --git a/modules/events/src/test/java/org/apache/tamaya/events/delta/ConfigurationChangeTest.java b/modules/events/src/test/java/org/apache/tamaya/events/delta/ConfigurationChangeTest.java deleted file mode 100644 index b20ebef..0000000 --- a/modules/events/src/test/java/org/apache/tamaya/events/delta/ConfigurationChangeTest.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.events.delta; - -import org.apache.tamaya.Configuration; -import org.apache.tamaya.ConfigurationProvider; -import org.apache.tamaya.events.ConfigurationChange; -import org.apache.tamaya.events.ConfigurationChangeBuilder; -import org.junit.Test; - -import java.util.Map; - -import static org.junit.Assert.*; - -/** - * Test class for {@link ConfigurationChange}. - */ -public class ConfigurationChangeTest { - - @Test - public void testEmptyChangeSet() throws Exception { - ConfigurationChange change = ConfigurationChange.emptyChangeSet(ConfigurationProvider.getConfiguration()); - assertNotNull(change); - assertTrue(change.isEmpty()); - } - - @Test - public void testGetConfiguration() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); - ConfigurationChange change = ConfigurationChangeBuilder.of(config).build(); - assertNotNull(change); - assertTrue(change.getUpdatedSize()==0); - assertTrue(change.getAddedSize()==0); - assertTrue(change.getRemovedSize()==0); - assertTrue(change.getChanges().size()==0); - for (Map.Entry<String, String> en : config.getProperties().entrySet()) { - if (!"[meta]frozenAt".equals(en.getKey())) { - if(en.getKey().contains("random.new")){ // dynamic generated value! - continue; - } - assertEquals("Error for " + en.getKey(), en.getValue(), change.getResource().get(en.getKey())); - } - } - } - - @Test - public void testGetVersion() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); - ConfigurationChange change = ConfigurationChangeBuilder.of(config).build(); - assertNotNull(change.getVersion()); - change = ConfigurationChangeBuilder.of(config).setVersion("version2").build(); - assertNotNull(change.getVersion()); - assertEquals("version2", change.getVersion()); - } - - @Test - public void testGetTimestamp() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); - ConfigurationChange change = ConfigurationChangeBuilder.of(config).build(); - assertTrue((System.currentTimeMillis() - change.getTimestamp()) <= 10L); - change = ConfigurationChangeBuilder.of(config).setTimestamp(10L).build(); - assertEquals(10L, change.getTimestamp()); - } - - @Test - public void testGetEvents() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); - ConfigurationChange change = ConfigurationChangeBuilder.of(config).removeKey("key1", "key2").build(); - assertTrue(change.getChanges().size() == 2); - change = ConfigurationChangeBuilder.of(config).addChange("key1Added", "value1Added").build(); - assertTrue(change.getChanges().size() == 1); - } - - @Test - public void testGetRemovedSize() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); - ConfigurationChange change = ConfigurationChangeBuilder.of(config).removeKey("java.version", "key2").build(); - assertTrue(change.getRemovedSize() == 2); - assertTrue(change.getAddedSize() == 0); - } - - @Test - public void testGetAddedSize() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); - ConfigurationChange change = ConfigurationChangeBuilder.of(config).addChange("key1", "key2").build(); - assertTrue(change.getAddedSize() == 1); - assertTrue(change.getRemovedSize() == 0); - } - - @Test - public void testGetUpdatedSize() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); - ConfigurationChange change = ConfigurationChangeBuilder.of(config).addChange("java.version", "1.8").build(); - assertTrue(change.getUpdatedSize() == 1); - } - - @Test - public void testIsRemoved() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); - ConfigurationChange change = ConfigurationChangeBuilder.of(config).removeKey("java.version").build(); - assertTrue(change.isRemoved("java.version")); - } - - @Test - public void testIsAdded() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); - ConfigurationChange change = ConfigurationChangeBuilder.of(config).addChange("key1", "key2").build(); - assertTrue(change.isAdded("key1")); - } - - @Test - public void testIsUpdated() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); - ConfigurationChange change = ConfigurationChangeBuilder.of(config).addChange("java.version", "1.8").build(); - assertTrue(change.isUpdated("java.version")); - } - - @Test - public void testContainsKey() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); - ConfigurationChange change = ConfigurationChangeBuilder.of(config).addChange("key1", "key2").build(); - assertTrue(change.isKeyAffected("key1")); - assertFalse(change.isKeyAffected("key2")); - change = ConfigurationChangeBuilder.of(config).removeKey("java.version").build(); - assertFalse(change.isKeyAffected("java.version")); - assertFalse(change.isKeyAffected("key2")); - } - - @Test - public void testIsEmpty() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); - ConfigurationChange change = ConfigurationChangeBuilder.of(config).build(); - assertTrue(change.isEmpty()); - } - - @Test - public void testToString() throws Exception { - Configuration config = ConfigurationProvider.getConfiguration(); - ConfigurationChange change = ConfigurationChangeBuilder.of(config).addChange("key1", "key2").build(); - change = ConfigurationChangeBuilder.of(config).removeKey("java.version").build(); - assertTrue(change.toString().contains("timestamp")); - assertTrue(change.toString().contains("version")); - assertTrue(change.toString().contains("configuration")); - assertFalse(change.toString().contains("key1")); - assertFalse(change.toString().contains("key2")); - } -} \ No newline at end of file
