[TAMAYA-232] Removed meta-data from the frozen configuration.
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/2c2a55a9 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/2c2a55a9 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/2c2a55a9 Branch: refs/heads/master Commit: 2c2a55a9809da7e243115c132931589730966363 Parents: e82d3bd Author: Oliver B. Fischer <[email protected]> Authored: Sun Feb 12 13:32:15 2017 +0100 Committer: Oliver B. Fischer <[email protected]> Committed: Sat Feb 25 00:02:59 2017 +0100 ---------------------------------------------------------------------- modules/events/pom.xml | 4 + .../events/ConfigurationChangeBuilder.java | 18 ++--- .../tamaya/events/FrozenConfiguration.java | 45 +++++++++-- .../internal/DefaultConfigEventManagerSpi.java | 2 +- .../tamaya/events/FrozenConfigurationTest.java | 80 ++++++++++++++++++++ 5 files changed, 133 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/2c2a55a9/modules/events/pom.xml ---------------------------------------------------------------------- diff --git a/modules/events/pom.xml b/modules/events/pom.xml index bd96532..b29d680 100644 --- a/modules/events/pom.xml +++ b/modules/events/pom.xml @@ -78,6 +78,10 @@ under the License. <artifactId>commons-io</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + </dependency> </dependencies> </project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/2c2a55a9/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationChangeBuilder.java ---------------------------------------------------------------------- diff --git a/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationChangeBuilder.java b/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationChangeBuilder.java index 93726d8..e622869 100644 --- a/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationChangeBuilder.java +++ b/modules/events/src/main/java/org/apache/tamaya/events/ConfigurationChangeBuilder.java @@ -90,20 +90,20 @@ public final class ConfigurationChangeBuilder { /** * Compares the two property config/configurations and creates a collection with all changes - * that must be applied to render {@code map1} into {@code map2}. + * that must be applied to render {@code original} into {@code target}. * - * @param map1 the source map, not null. - * @param map2 the target map, not null. - * @return a collection current change events, never null. + * @param original the original map, not null. + * @param target the target map, not null. + * @return a collection current change events, never {@code null}. */ - public static Collection<PropertyChangeEvent> compare(Configuration map1, Configuration map2) { + public static Collection<PropertyChangeEvent> compare(Configuration original, Configuration target) { List<PropertyChangeEvent> changes = new ArrayList<>(); - for (Map.Entry<String, String> en : map1.getProperties().entrySet()) { - String val = map2.get(en.getKey()); + for (Map.Entry<String, String> en : original.getProperties().entrySet()) { + String val = target.get(en.getKey()); if (val == null) { - changes.add(new PropertyChangeEvent(map1, en.getKey(), null, en.getValue())); + changes.add(new PropertyChangeEvent(original, en.getKey(), null, en.getValue())); } else if (!val.equals(en.getValue())) { - changes.add(new PropertyChangeEvent(map1, en.getKey(), val, en.getValue())); + changes.add(new PropertyChangeEvent(original, en.getKey(), val, en.getValue())); } } for (Map.Entry<String, String> en : map2.getProperties().entrySet()) { http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/2c2a55a9/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 2a4540c..295a609 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 @@ -41,10 +41,13 @@ import java.util.logging.Logger; */ public final class FrozenConfiguration implements Configuration, Serializable { private static final long serialVersionUID = -6373137316556444171L; + /** * The properties frozen. */ private Map<String, String> properties = new HashMap<>(); + private long frozenAt = System.nanoTime(); + private UUID id = UUID.randomUUID(); /** * Constructor. @@ -53,10 +56,6 @@ public final class FrozenConfiguration implements Configuration, Serializable { */ private FrozenConfiguration(Configuration config) { this.properties.putAll(config.getProperties()); - 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); } @@ -176,19 +175,53 @@ public final class FrozenConfiguration implements Configuration, Serializable { if (o == null || getClass() != o.getClass()) { return false; } + FrozenConfiguration that = (FrozenConfiguration) o; - return properties.equals(that.properties); + + if (frozenAt != that.frozenAt) { + return false; + } + if (properties != null ? !properties.equals(that.properties) : that.properties != null) { + return false; + } + return id != null ? id.equals(that.id) : that.id == null; } @Override public int hashCode() { - return properties.hashCode(); + int result = properties != null ? properties.hashCode() : 0; + result = 31 * result + (int) (frozenAt ^ (frozenAt >>> 32)); + result = 31 * result + (id != null ? id.hashCode() : 0); + return result; } @Override public String toString() { return "FrozenConfiguration{" + + "id=" + getId() + "," + + "frozenAt=" + getFrozenAt() + "," + "properties=" + properties + '}'; } + + /** + * <p>Returns the moment in time when this frozen configuration has been created.</p> + * + * <p>The time is taken from {@linkplain System#currentTimeMillis()}</p> + * + * @see {@linkplain System#currentTimeMillis()} + * @return the moment in time when this configruration has been created + */ + public long getFrozenAt() { + return frozenAt; + } + + /** + * <p>Returns the unique id of this frozen configuration.</p> + * + * @return the unique id of this frozen configuration, never {@code null} + */ + public UUID getId() { + return id; + } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/2c2a55a9/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigEventManagerSpi.java ---------------------------------------------------------------------- diff --git a/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigEventManagerSpi.java b/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigEventManagerSpi.java index 586df5c..c92b7e8 100644 --- a/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigEventManagerSpi.java +++ b/modules/events/src/main/java/org/apache/tamaya/events/internal/DefaultConfigEventManagerSpi.java @@ -184,7 +184,7 @@ public class DefaultConfigEventManagerSpi implements ConfigEventManagerSpi { /** * Tasks to inform observers on detected configuration changes. */ - private static final class PublishConfigChangeTask implements Runnable{ + private static final class PublishConfigChangeTask implements Runnable { private final ConfigEventListener l; private final ConfigEvent<?> changes; http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/2c2a55a9/modules/events/src/test/java/org/apache/tamaya/events/FrozenConfigurationTest.java ---------------------------------------------------------------------- diff --git a/modules/events/src/test/java/org/apache/tamaya/events/FrozenConfigurationTest.java b/modules/events/src/test/java/org/apache/tamaya/events/FrozenConfigurationTest.java new file mode 100644 index 0000000..dbb2ead --- /dev/null +++ b/modules/events/src/test/java/org/apache/tamaya/events/FrozenConfigurationTest.java @@ -0,0 +1,80 @@ +/* + * 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.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import java.util.HashMap; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.doReturn; + +public class FrozenConfigurationTest { + + @Test + public void getFrozenAtReturnsTheCorrectTimestamp() { + Configuration source = Mockito.mock(Configuration.class); + + long poiStart = System.nanoTime(); + + FrozenConfiguration fc = FrozenConfiguration.of(source); + + long poiEnd = System.nanoTime(); + + assertThat(fc.getFrozenAt()).isGreaterThan(poiStart) + .isLessThan(poiEnd); + } + + + @Test + public void idMustBeNotNull() { + Configuration source = Mockito.mock(Configuration.class); + + FrozenConfiguration fc = FrozenConfiguration.of(source); + + assertThat(fc.getId()).isNotNull(); + } + + /* + * All tests for equals() and hashCode() go here... + */ + + @Test + public void twoFrozenAreDifferentIfTheyHaveADifferentIdAndFrozenAtTimestamp() { + Map<String, String> properties = new HashMap<>(); + properties.put("key", "value"); + + Configuration configuration = Mockito.mock(Configuration.class); + doReturn(properties).when(configuration).getProperties(); + + FrozenConfiguration fcA = FrozenConfiguration.of(configuration); + FrozenConfiguration fcB = FrozenConfiguration.of(configuration); + + assertThat(fcA.getId()).isNotEqualTo(fcB.getId()); + assertThat(fcA).isNotEqualTo(fcB); + } + + /* + * END OF ALL TESTS for equals() and hashCode() + */ +} \ No newline at end of file
