This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit dde54c8273f6cbc4f06d0efba04eda0608bd8cf6 Author: Benoit Tellier <btell...@linagora.com> AuthorDate: Thu Mar 21 15:23:33 2019 +0700 MAILBOX-388 MailRepositoryPath::hasPrefix --- .../mailrepository/api/MailRepositoryPath.java | 10 ++++ .../mailrepository/api/MailRepositoryPathTest.java | 66 ++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/server/mailrepository/mailrepository-api/src/main/java/org/apache/james/mailrepository/api/MailRepositoryPath.java b/server/mailrepository/mailrepository-api/src/main/java/org/apache/james/mailrepository/api/MailRepositoryPath.java index 5bb9d79..f2d8686 100644 --- a/server/mailrepository/mailrepository-api/src/main/java/org/apache/james/mailrepository/api/MailRepositoryPath.java +++ b/server/mailrepository/mailrepository-api/src/main/java/org/apache/james/mailrepository/api/MailRepositoryPath.java @@ -64,6 +64,16 @@ public class MailRepositoryPath implements Comparable<MailRepositoryPath> { return from(value + PATH_DELIMITER + suffix); } + public boolean hasPrefix(MailRepositoryPath other) { + if (value.isEmpty()) { + return false; + } + if (other.value.isEmpty()) { + return true; + } + return value.startsWith(other.value + PATH_DELIMITER); + } + public String asString() { return value; } diff --git a/server/mailrepository/mailrepository-api/src/test/java/org/apache/james/mailrepository/api/MailRepositoryPathTest.java b/server/mailrepository/mailrepository-api/src/test/java/org/apache/james/mailrepository/api/MailRepositoryPathTest.java index bb16d0f..31201e7 100644 --- a/server/mailrepository/mailrepository-api/src/test/java/org/apache/james/mailrepository/api/MailRepositoryPathTest.java +++ b/server/mailrepository/mailrepository-api/src/test/java/org/apache/james/mailrepository/api/MailRepositoryPathTest.java @@ -92,4 +92,70 @@ class MailRepositoryPathTest { assertThatThrownBy(() -> MailRepositoryPath.from("abc").subPath("/def")) .isInstanceOf(IllegalArgumentException.class); } + + @Test + void hasPrefixShouldReturnFalseWhenEquals() { + assertThat(MailRepositoryPath.from("abc/def").hasPrefix(MailRepositoryPath.from("abc/def"))) + .isFalse(); + } + + @Test + void hasPrefixShouldReturnFalseWhenSiblings() { + assertThat(MailRepositoryPath.from("abc/def").hasPrefix(MailRepositoryPath.from("abc/ghi"))) + .isFalse(); + } + + @Test + void hasPrefixShouldReturnFalseWhenAncestor() { + assertThat(MailRepositoryPath.from("abc").hasPrefix(MailRepositoryPath.from("abc/ghi"))) + .isFalse(); + } + + @Test + void hasPrefixShouldReturnTrueWhenDescendant() { + assertThat(MailRepositoryPath.from("abc/ghi").hasPrefix(MailRepositoryPath.from("abc"))) + .isTrue(); + } + + @Test + void hasPrefixShouldReturnTrueWhenDescendantStartingWithSlash() { + assertThat(MailRepositoryPath.from("/abc/ghi").hasPrefix(MailRepositoryPath.from("/abc"))) + .isTrue(); + } + + @Test + void hasPrefixShouldReturnFalseWhenDescendantAdditionalFirstSlash() { + assertThat(MailRepositoryPath.from("abc/ghi").hasPrefix(MailRepositoryPath.from("/abc"))) + .isFalse(); + } + + @Test + void hasPrefixShouldReturnFalseWhenDescendantMissingFirstSlash() { + assertThat(MailRepositoryPath.from("/abc/ghi").hasPrefix(MailRepositoryPath.from("abc"))) + .isFalse(); + } + + @Test + void hasPrefixShouldReturnTrueWhenFarDescendant() { + assertThat(MailRepositoryPath.from("abc/ghi/klm").hasPrefix(MailRepositoryPath.from("abc"))) + .isTrue(); + } + + @Test + void hasPrefixShouldReturnTrueWhenEmpty() { + assertThat(MailRepositoryPath.from("abc").hasPrefix(MailRepositoryPath.from(""))) + .isTrue(); + } + + @Test + void hasPrefixShouldReturnFalseWhenBothEmpty() { + assertThat(MailRepositoryPath.from("").hasPrefix(MailRepositoryPath.from(""))) + .isFalse(); + } + + @Test + void hasPrefixShouldReturnFalseWhenMissingSlah() { + assertThat(MailRepositoryPath.from("abcghi").hasPrefix(MailRepositoryPath.from("abc"))) + .isFalse(); + } } \ 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