Repository: james-project Updated Branches: refs/heads/master 255e6cd30 -> 7d3c8efae
JAMES-1951 add ApplicableFlagBuilder Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/587c0eb6 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/587c0eb6 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/587c0eb6 Branch: refs/heads/master Commit: 587c0eb6ba6b20063a0d60a80b942e00e79b3bc3 Parents: 255e6cd Author: Luc DUZAN <ldu...@linagora.com> Authored: Tue Feb 28 15:59:54 2017 +0100 Committer: Antoine Duprat <adup...@linagora.com> Committed: Fri Jun 9 21:52:45 2017 +0200 ---------------------------------------------------------------------- .../james/mailbox/ApplicableFlagBuilder.java | 71 ++++++++++++ .../mailbox/ApplicableFlagBuilderTest.java | 109 +++++++++++++++++++ 2 files changed, 180 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/587c0eb6/mailbox/api/src/main/java/org/apache/james/mailbox/ApplicableFlagBuilder.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/ApplicableFlagBuilder.java b/mailbox/api/src/main/java/org/apache/james/mailbox/ApplicableFlagBuilder.java new file mode 100644 index 0000000..91496d8 --- /dev/null +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/ApplicableFlagBuilder.java @@ -0,0 +1,71 @@ +/**************************************************************** + * 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; + +import javax.mail.Flags; + +import com.google.common.annotations.VisibleForTesting; + +public class ApplicableFlagBuilder { + + @VisibleForTesting static final Flags DEFAULT_APPLICABLE_FLAGS = FlagsBuilder.builder() + .add(Flags.Flag.ANSWERED, + Flags.Flag.DELETED, + Flags.Flag.DRAFT, + Flags.Flag.FLAGGED, + Flags.Flag.SEEN) + .build(); + + public static ApplicableFlagBuilder builder() { + return new ApplicableFlagBuilder(); + } + + public static ApplicableFlagBuilder from(Flags... flags) { + return new ApplicableFlagBuilder() + .add(flags); + } + + public static ApplicableFlagBuilder from(String... flags) { + return new ApplicableFlagBuilder() + .add(flags); + } + + private final FlagsBuilder builder; + + private ApplicableFlagBuilder() { + builder = FlagsBuilder.builder().add(DEFAULT_APPLICABLE_FLAGS); + } + + public ApplicableFlagBuilder add(String... flags) { + builder.add(flags); + return this; + } + + public ApplicableFlagBuilder add(Flags... flags) { + builder.add(flags); + return this; + } + + public Flags build() { + Flags flags = builder.build(); + flags.remove(Flags.Flag.RECENT); + flags.remove(Flags.Flag.USER); + return flags; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/587c0eb6/mailbox/api/src/test/java/org/apache/james/mailbox/ApplicableFlagBuilderTest.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/ApplicableFlagBuilderTest.java b/mailbox/api/src/test/java/org/apache/james/mailbox/ApplicableFlagBuilderTest.java new file mode 100644 index 0000000..ad2ad76 --- /dev/null +++ b/mailbox/api/src/test/java/org/apache/james/mailbox/ApplicableFlagBuilderTest.java @@ -0,0 +1,109 @@ +/**************************************************************** + * 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; + +import static org.assertj.core.api.Assertions.assertThat; + +import javax.mail.Flags; + +import org.assertj.core.api.JUnitSoftAssertions; +import org.junit.Rule; +import org.junit.Test; + +public class ApplicableFlagBuilderTest { + + @Rule + public final JUnitSoftAssertions softly = new JUnitSoftAssertions(); + + @Test + public void shouldAtLeastContainAllDefaultApplicativeFlag() { + assertThat(ApplicableFlagBuilder.builder().build()) + .isEqualTo(ApplicableFlagBuilder.DEFAULT_APPLICABLE_FLAGS); + } + + @Test + public void shouldNeverRetainRecentAndUserFlag() { + Flags result = ApplicableFlagBuilder.builder() + .add(new Flags(Flags.Flag.RECENT)) + .add(new Flags(Flags.Flag.USER)) + .build(); + + softly.assertThat(result.contains(Flags.Flag.RECENT)).isFalse(); + softly.assertThat(result.contains(Flags.Flag.USER)).isFalse(); + } + + @Test + public void shouldAddCustomUserFlagIfProvidedToDefaultFlag() { + Flags result = ApplicableFlagBuilder.builder() + .add("yolo", "vibe") + .build(); + + softly.assertThat(result.contains(ApplicableFlagBuilder.DEFAULT_APPLICABLE_FLAGS)).isTrue(); + softly.assertThat(result.contains("yolo")).isTrue(); + softly.assertThat(result.contains("vibe")).isTrue(); + } + + @Test + public void shouldAcceptUserCustomFlagInsideFlags() { + Flags result = ApplicableFlagBuilder.builder() + .add(new Flags("yolo")) + .build(); + + assertThat(result.contains("yolo")).isTrue(); + } + + @Test + public void shouldAcceptFlagsThatContainMultipleFlag() { + Flags flags = FlagsBuilder.builder() + .add("yolo", "vibes") + .build(); + + Flags result = ApplicableFlagBuilder.builder() + .add(flags) + .build(); + + softly.assertThat(result.contains("yolo")).isTrue(); + softly.assertThat(result.contains("vibes")).isTrue(); + } + + @Test + public void addShouldAddMultipleFlagsAtOnce() { + Flags flags = new Flags("cartman"); + Flags flags2 = new Flags("butters"); + Flags result = ApplicableFlagBuilder.builder() + .add(flags, flags2) + .build(); + + softly.assertThat(result.contains(flags)).isTrue(); + softly.assertThat(result.contains(flags2)).isTrue(); + } + + @Test + public void shouldAcceptMultipleFlagAtOnce() { + Flags result = ApplicableFlagBuilder.builder() + .add("cartman", "butters") + .add("chef", "randy") + .build(); + + softly.assertThat(result.contains("cartman")).isTrue(); + softly.assertThat(result.contains("butters")).isTrue(); + softly.assertThat(result.contains("chef")).isTrue(); + softly.assertThat(result.contains("randy")).isTrue(); + } +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org