JAMES-1874 Makes flag change detection easier, and tested
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/bbe8e8a1 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/bbe8e8a1 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/bbe8e8a1 Branch: refs/heads/master Commit: bbe8e8a14fd42ecb52a5a0b3f9c74db118b93c9b Parents: 82c934e Author: Benoit Tellier <btell...@linagora.com> Authored: Fri Feb 3 11:25:08 2017 +0700 Committer: Benoit Tellier <btell...@linagora.com> Committed: Tue Feb 7 08:57:45 2017 +0700 ---------------------------------------------------------------------- .../org/apache/james/mailbox/FlagsBuilder.java | 4 + .../james/mailbox/model/UpdatedFlags.java | 58 +++++- .../james/mailbox/model/UpdatedFlagsTest.java | 178 +++++++++++++++++++ 3 files changed, 239 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/bbe8e8a1/mailbox/api/src/main/java/org/apache/james/mailbox/FlagsBuilder.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/FlagsBuilder.java b/mailbox/api/src/main/java/org/apache/james/mailbox/FlagsBuilder.java index 62e0ee1..1414cbf 100644 --- a/mailbox/api/src/main/java/org/apache/james/mailbox/FlagsBuilder.java +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/FlagsBuilder.java @@ -23,6 +23,10 @@ import javax.mail.Flags; public class FlagsBuilder { + public static FlagsBuilder builder() { + return new FlagsBuilder(); + } + private final Flags internalFlags; public FlagsBuilder() { http://git-wip-us.apache.org/repos/asf/james-project/blob/bbe8e8a1/mailbox/api/src/main/java/org/apache/james/mailbox/model/UpdatedFlags.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/UpdatedFlags.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/UpdatedFlags.java index 329ed41..989729b 100644 --- a/mailbox/api/src/main/java/org/apache/james/mailbox/model/UpdatedFlags.java +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/model/UpdatedFlags.java @@ -28,6 +28,8 @@ import org.apache.james.mailbox.MessageUid; import com.google.common.base.MoreObjects; import com.google.common.base.Objects; +import com.google.common.base.Optional; +import com.google.common.base.Preconditions; /** * Represent a Flag update for a message @@ -36,6 +38,48 @@ import com.google.common.base.Objects; */ public class UpdatedFlags { + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private MessageUid uid; + private Flags oldFlags; + private Flags newFlags; + private Optional<Long> modSeq = Optional.absent(); + + private Builder() { + } + + public Builder uid(MessageUid uid) { + this.uid = uid; + return this; + } + + public Builder oldFlags(Flags oldFlags) { + this.oldFlags = oldFlags; + return this; + } + + public Builder newFlags(Flags newFlags) { + this.newFlags = newFlags; + return this; + } + + public Builder modSeq(long modSeq) { + this.modSeq = Optional.of(modSeq); + return this; + } + + public UpdatedFlags build() { + Preconditions.checkState(uid != null); + Preconditions.checkState(newFlags != null); + Preconditions.checkState(oldFlags != null); + Preconditions.checkState(modSeq.isPresent()); + return new UpdatedFlags(uid, modSeq.get(), oldFlags, newFlags); + } + } + private final MessageUid uid; private final Flags oldFlags; private final Flags newFlags; @@ -104,7 +148,19 @@ public class UpdatedFlags { public Flags getOldFlags() { return oldFlags; } - + + public boolean isModifiedToSet(Flags.Flag flag) { + return newFlags.contains(flag) && !oldFlags.contains(flag); + } + + public boolean isModifiedToUnset(Flags.Flag flag) { + return !newFlags.contains(flag) && oldFlags.contains(flag); + } + + public boolean isUnchanged(Flags.Flag flag) { + return !isModifiedToSet(flag) && !isModifiedToUnset(flag); + } + /** * Return the new {@link Flags} for the message * http://git-wip-us.apache.org/repos/asf/james-project/blob/bbe8e8a1/mailbox/api/src/test/java/org/apache/james/mailbox/model/UpdatedFlagsTest.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/model/UpdatedFlagsTest.java b/mailbox/api/src/test/java/org/apache/james/mailbox/model/UpdatedFlagsTest.java new file mode 100644 index 0000000..e660dbc --- /dev/null +++ b/mailbox/api/src/test/java/org/apache/james/mailbox/model/UpdatedFlagsTest.java @@ -0,0 +1,178 @@ +/**************************************************************** + * 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.james.mailbox.model; + +import static org.assertj.core.api.Assertions.assertThat; + +import javax.mail.Flags; + +import org.apache.james.mailbox.MessageUid; +import org.junit.Test; + +public class UpdatedFlagsTest { + + public static final MessageUid UID = MessageUid.of(45L); + public static final long MOD_SEQ = 47L; + + @Test + public void isModifiedToSetShouldReturnTrueWhenFlagOnlyInNewFlag() { + UpdatedFlags updatedFlags = UpdatedFlags.builder() + .newFlags(new Flags(Flags.Flag.RECENT)) + .oldFlags(new Flags()) + .uid(UID) + .modSeq(MOD_SEQ) + .build(); + + assertThat(updatedFlags.isModifiedToSet(Flags.Flag.RECENT)).isTrue(); + } + + @Test + public void isModifiedToSetShouldReturnFalseWhenFlagOnlyInOldFlag() { + UpdatedFlags updatedFlags = UpdatedFlags.builder() + .newFlags(new Flags()) + .oldFlags(new Flags(Flags.Flag.RECENT)) + .uid(UID) + .modSeq(MOD_SEQ) + .build(); + + assertThat(updatedFlags.isModifiedToSet(Flags.Flag.RECENT)).isFalse(); + } + + @Test + public void isModifiedToSetShouldReturnFalseWhenFlagIsOnNone() { + UpdatedFlags updatedFlags = UpdatedFlags.builder() + .newFlags(new Flags()) + .oldFlags(new Flags()) + .uid(UID) + .modSeq(MOD_SEQ) + .build(); + + assertThat(updatedFlags.isModifiedToSet(Flags.Flag.RECENT)).isFalse(); + } + + @Test + public void isModifiedToSetShouldReturnFalseWhenFlagIsOnBoth() { + UpdatedFlags updatedFlags = UpdatedFlags.builder() + .newFlags(new Flags(Flags.Flag.RECENT)) + .oldFlags(new Flags(Flags.Flag.RECENT)) + .uid(UID) + .modSeq(MOD_SEQ) + .build(); + + assertThat(updatedFlags.isModifiedToSet(Flags.Flag.RECENT)).isFalse(); + } + + @Test + public void isModifiedToUnsetShouldReturnFalseWhenFlagOnlyInNewFlag() { + UpdatedFlags updatedFlags = UpdatedFlags.builder() + .newFlags(new Flags(Flags.Flag.RECENT)) + .oldFlags(new Flags()) + .uid(UID) + .modSeq(MOD_SEQ) + .build(); + + assertThat(updatedFlags.isModifiedToUnset(Flags.Flag.RECENT)).isFalse(); + } + + @Test + public void isModifiedToUnsetShouldReturnTrueWhenFlagOnlyInOldFlag() { + UpdatedFlags updatedFlags = UpdatedFlags.builder() + .newFlags(new Flags()) + .oldFlags(new Flags(Flags.Flag.RECENT)) + .uid(UID) + .modSeq(MOD_SEQ) + .build(); + + assertThat(updatedFlags.isModifiedToUnset(Flags.Flag.RECENT)).isTrue(); + } + + @Test + public void isModifiedToUnsetShouldReturnFalseWhenFlagIsOnNone() { + UpdatedFlags updatedFlags = UpdatedFlags.builder() + .newFlags(new Flags()) + .oldFlags(new Flags()) + .uid(UID) + .modSeq(MOD_SEQ) + .build(); + + assertThat(updatedFlags.isModifiedToSet(Flags.Flag.RECENT)).isFalse(); + } + + @Test + public void isModifiedToUnsetShouldReturnFalseWhenFlagIsOnBoth() { + UpdatedFlags updatedFlags = UpdatedFlags.builder() + .newFlags(new Flags(Flags.Flag.RECENT)) + .oldFlags(new Flags(Flags.Flag.RECENT)) + .uid(UID) + .modSeq(MOD_SEQ) + .build(); + + assertThat(updatedFlags.isModifiedToUnset(Flags.Flag.RECENT)).isFalse(); + } + + @Test + public void isUnchangedShouldReturnFalseWhenFlagOnlyInNewFlag() { + UpdatedFlags updatedFlags = UpdatedFlags.builder() + .newFlags(new Flags(Flags.Flag.RECENT)) + .oldFlags(new Flags()) + .uid(UID) + .modSeq(MOD_SEQ) + .build(); + + assertThat(updatedFlags.isUnchanged(Flags.Flag.RECENT)).isFalse(); + } + + @Test + public void isUnchangedShouldReturnFalseWhenFlagOnlyInOldFlag() { + UpdatedFlags updatedFlags = UpdatedFlags.builder() + .newFlags(new Flags()) + .oldFlags(new Flags(Flags.Flag.RECENT)) + .uid(UID) + .modSeq(MOD_SEQ) + .build(); + + assertThat(updatedFlags.isUnchanged(Flags.Flag.RECENT)).isFalse(); + } + + @Test + public void isUnchangedShouldReturnTrueWhenFlagIsOnNone() { + UpdatedFlags updatedFlags = UpdatedFlags.builder() + .newFlags(new Flags()) + .oldFlags(new Flags()) + .uid(UID) + .modSeq(MOD_SEQ) + .build(); + + assertThat(updatedFlags.isUnchanged(Flags.Flag.RECENT)).isTrue(); + } + + @Test + public void isUnchangedShouldReturnTrueWhenFlagIsOnBoth() { + UpdatedFlags updatedFlags = UpdatedFlags.builder() + .newFlags(new Flags(Flags.Flag.RECENT)) + .oldFlags(new Flags(Flags.Flag.RECENT)) + .uid(UID) + .modSeq(MOD_SEQ) + .build(); + + assertThat(updatedFlags.isUnchanged(Flags.Flag.RECENT)).isTrue(); + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org