MAILBOX-310 Mailbox query refactoring: Create dedicated packages for Mailbox search related classes
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/0cfb3951 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/0cfb3951 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/0cfb3951 Branch: refs/heads/master Commit: 0cfb395142d1b0261041a7772e95ccc051085652 Parents: 2c04899 Author: benwa <btell...@linagora.com> Authored: Wed Oct 4 11:29:46 2017 +0700 Committer: Matthieu Baechler <matth...@apache.org> Committed: Thu Oct 5 20:00:38 2017 +0200 ---------------------------------------------------------------------- .../apache/james/mailbox/MailboxManager.java | 2 +- .../apache/james/mailbox/model/ExactName.java | 48 - .../mailbox/model/MailboxNameExpression.java | 38 - .../james/mailbox/model/MailboxQuery.java | 316 ------ .../james/mailbox/model/PrefixedRegex.java | 112 -- .../james/mailbox/model/PrefixedWildcard.java | 47 - .../apache/james/mailbox/model/Wildcard.java | 40 - .../james/mailbox/model/search/ExactName.java | 48 + .../model/search/MailboxNameExpression.java | 38 + .../mailbox/model/search/MailboxQuery.java | 318 ++++++ .../mailbox/model/search/PrefixedRegex.java | 112 ++ .../mailbox/model/search/PrefixedWildcard.java | 47 + .../james/mailbox/model/search/Wildcard.java | 40 + .../james/mailbox/MailboxManagerTest.java | 2 +- .../james/mailbox/model/ExactNameTest.java | 67 -- .../james/mailbox/model/MailboxQueryTest.java | 266 ----- .../james/mailbox/model/PrefixedRegexTest.java | 1058 ------------------ .../mailbox/model/PrefixedWildcardTest.java | 71 -- .../james/mailbox/model/WildcardTest.java | 59 - .../mailbox/model/search/ExactNameTest.java | 67 ++ .../mailbox/model/search/MailboxQueryTest.java | 267 +++++ .../mailbox/model/search/PrefixedRegexTest.java | 1058 ++++++++++++++++++ .../model/search/PrefixedWildcardTest.java | 71 ++ .../mailbox/model/search/WildcardTest.java | 59 + .../inmemory/InMemoryMessageIdManager.java | 2 +- .../InMemoryMessageIdManagerTestSystem.java | 2 +- .../InMemoryMessageManagerTestSystem.java | 2 +- .../mailbox/store/StoreMailboxManager.java | 2 +- .../store/AbstractCombinationManagerTest.java | 2 +- .../mailbox/store/StoreMailboxManagerTest.java | 2 +- .../james/imap/processor/LSubProcessor.java | 2 +- .../james/imap/processor/ListProcessor.java | 2 +- .../base/MailboxEventAnalyserTest.java | 2 +- .../apache/james/modules/MailboxProbeImpl.java | 2 +- .../mailbox/MailboxManagerManagement.java | 2 +- .../matchers/AbstractStorageQuota.java | 2 +- .../james/jmap/methods/GetMailboxesMethod.java | 2 +- .../jmap/utils/SystemMailboxesProviderImpl.java | 2 +- ...ltMailboxesProvisioningFilterThreadTest.java | 2 +- .../webadmin/service/UserMailboxesService.java | 2 +- .../routes/UserMailboxesRoutesTest.java | 2 +- 41 files changed, 2144 insertions(+), 2141 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/0cfb3951/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxManager.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxManager.java b/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxManager.java index 38414cc..080169a 100644 --- a/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxManager.java +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/MailboxManager.java @@ -36,10 +36,10 @@ import org.apache.james.mailbox.model.MailboxAnnotationKey; import org.apache.james.mailbox.model.MailboxId; import org.apache.james.mailbox.model.MailboxMetaData; import org.apache.james.mailbox.model.MailboxPath; -import org.apache.james.mailbox.model.MailboxQuery; import org.apache.james.mailbox.model.MessageId; import org.apache.james.mailbox.model.MessageRange; import org.apache.james.mailbox.model.MultimailboxesSearchQuery; +import org.apache.james.mailbox.model.search.MailboxQuery; /** * <p> http://git-wip-us.apache.org/repos/asf/james-project/blob/0cfb3951/mailbox/api/src/main/java/org/apache/james/mailbox/model/ExactName.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/ExactName.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/ExactName.java deleted file mode 100644 index 298819e..0000000 --- a/mailbox/api/src/main/java/org/apache/james/mailbox/model/ExactName.java +++ /dev/null @@ -1,48 +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.james.mailbox.model; - -import com.google.common.base.Preconditions; - -public class ExactName implements MailboxNameExpression { - - private final String name; - - public ExactName(String name) { - Preconditions.checkNotNull(name); - this.name = name; - } - - @Override - public boolean isExpressionMatch(String mailboxName) { - Preconditions.checkNotNull(mailboxName); - return name.equals(mailboxName); - } - - @Override - public String getCombinedName() { - return name; - } - - @Override - public boolean isWild() { - return false; - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/0cfb3951/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxNameExpression.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxNameExpression.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxNameExpression.java deleted file mode 100644 index ec7d715..0000000 --- a/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxNameExpression.java +++ /dev/null @@ -1,38 +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.james.mailbox.model; - -interface MailboxNameExpression { - /** - * Use this wildcard to match every char including the hierarchy delimiter - */ - char FREEWILDCARD = '*'; - - /** - * Use this wildcard to match every char except the hierarchy delimiter - */ - char LOCALWILDCARD = '%'; - - boolean isExpressionMatch(String name); - - String getCombinedName(); - - boolean isWild(); -} http://git-wip-us.apache.org/repos/asf/james-project/blob/0cfb3951/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxQuery.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxQuery.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxQuery.java deleted file mode 100644 index 2de99e2..0000000 --- a/mailbox/api/src/main/java/org/apache/james/mailbox/model/MailboxQuery.java +++ /dev/null @@ -1,316 +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.james.mailbox.model; - -import java.util.Optional; -import java.util.StringTokenizer; -import java.util.regex.Pattern; - -import org.apache.james.mailbox.MailboxSession; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.MoreObjects; -import com.google.common.base.Preconditions; - - -/** - * Expresses select criteria for mailboxes. - */ -public final class MailboxQuery { - /** - * Use this wildcard to match every char including the hierarchy delimiter - */ - public final static char FREEWILDCARD = '*'; - - /** - * Use this wildcard to match every char except the hierarchy delimiter - */ - public final static char LOCALWILDCARD = '%'; - - private static final String EMPTY_PATH_NAME = ""; - - public static Builder builder() { - return new Builder(); - } - - public static Builder privateMailboxesBuilder(MailboxSession session) { - return builder() - .mailboxSession(session) - .username(session.getUser().getUserName()); - } - - public static class Builder { - private String expression; - @VisibleForTesting Optional<Character> pathDelimiter; - @VisibleForTesting Optional<String> username; - @VisibleForTesting Optional<String> pathName; - @VisibleForTesting Optional<String> namespace; - - private Builder() { - this.pathName = Optional.empty(); - this.namespace = Optional.empty(); - this.username = Optional.empty(); - this.pathDelimiter = Optional.empty(); - } - - public Builder base(MailboxPath base) { - this.namespace = Optional.ofNullable(base.getNamespace()); - this.username = Optional.ofNullable(base.getUser()); - this.pathName = Optional.ofNullable(base.getName()); - return this; - } - - public Builder username(String username) { - this.username = Optional.of(username); - return this; - } - - public Builder privateMailboxes() { - Preconditions.checkState(!pathName.isPresent()); - Preconditions.checkState(!namespace.isPresent()); - this.namespace = Optional.of(MailboxConstants.USER_NAMESPACE); - this.pathName = Optional.of(EMPTY_PATH_NAME); - return matchesAll(); - } - - public Builder expression(String expression) { - this.expression = expression; - return this; - } - - public Builder matchesAll() { - this.expression = String.valueOf(FREEWILDCARD); - return this; - } - - public Builder mailboxSession(MailboxSession session) { - this.pathDelimiter = Optional.of(session.getPathDelimiter()); - return this; - } - - public Builder pathDelimiter(char pathDelimiter) { - this.pathDelimiter = Optional.of(pathDelimiter); - return this; - } - - public MailboxQuery build() { - Preconditions.checkState(pathDelimiter.isPresent()); - return new MailboxQuery(namespace, username, pathName, expression, pathDelimiter.get()); - } - } - - private final String expression; - private final char pathDelimiter; - private final Pattern pattern; - private final Optional<String> namespace; - private final Optional<String> user; - private final Optional<String> baseName; - - /** - * Constructs an expression determining a set of mailbox names. - * - * @param base - * base reference name, not null - * @param expression - * mailbox match expression, not null - * @param pathDelimiter - * path delimiter to use - */ - @VisibleForTesting MailboxQuery(Optional<String> namespace, Optional<String> user, Optional<String> baseName, - String expression, char pathDelimiter) { - this.namespace = namespace; - this.user = user; - this.baseName = baseName; - if (expression == null) { - this.expression = ""; - } else { - this.expression = expression; - } - this.pathDelimiter = pathDelimiter; - pattern = constructEscapedRegex(); - } - - public Optional<String> getNamespace() { - return namespace; - } - - public Optional<String> getUser() { - return user; - } - - public Optional<String> getBaseName() { - return baseName; - } - - public boolean isPrivateMailboxes(MailboxSession session) { - MailboxSession.User sessionUser = session.getUser(); - return namespace.map(MailboxConstants.USER_NAMESPACE::equals).orElse(false) - && user.map(sessionUser::isSameUser).orElse(false); - } - - public boolean belongsToRequestedNamespaceAndUser(MailboxPath mailboxPath) { - boolean belongsToRequestedNamespace = namespace - .map(value -> value.equals(mailboxPath.getNamespace())) - .orElse(true); - boolean belongsToRequestedUser = user - .map(value -> value.equals(mailboxPath.getUser())) - .orElse(true); - - return belongsToRequestedNamespace && belongsToRequestedUser; - } - - /** - * Gets the name search expression. This may contain wildcards. - * - * @return the expression - */ - public final String getExpression() { - return expression; - } - - /** - * Gets wildcard character that matches any series of characters. - * - * @return the freeWildcard - */ - public final char getFreeWildcard() { - return FREEWILDCARD; - } - - /** - * Gets wildcard character that matches any series of characters excluding - * hierarchy delimiters. Effectively, this means that it matches any - * sequence within a name part. - * - * @return the localWildcard - */ - public final char getLocalWildcard() { - return LOCALWILDCARD; - } - - /** - * Is the given name a match for {@link #getExpression()}? - * - * @param name - * name to be matched - * @return true if the given name matches this expression, false otherwise - */ - public boolean isExpressionMatch(String name) { - if (isWild()) { - return name != null - && pattern.matcher(name).matches(); - } else { - return expression.equals(name); - } - } - - public boolean isPathMatch(MailboxPath mailboxPath) { - String baseName = this.baseName.orElse(EMPTY_PATH_NAME); - int baseNameLength = baseName.length(); - String mailboxName = mailboxPath.getName(); - - return belongsToRequestedNamespaceAndUser(mailboxPath) - && mailboxName.startsWith(baseName) - && isExpressionMatch(mailboxName.substring(baseNameLength)); - } - - /** - * Get combined name formed by adding the expression to the base using the - * given hierarchy delimiter. Note that the wildcards are retained in the - * combined name. - * - * @return {@link #getBase()} combined with {@link #getExpression()}, - * notnull - */ - public String getCombinedName() { - String baseName = this.baseName.orElse(null); - if (baseName != null && baseName.length() > 0) { - final int baseLength = baseName.length(); - if (baseName.charAt(baseLength - 1) == pathDelimiter) { - if (expression != null && expression.length() > 0) { - if (expression.charAt(0) == pathDelimiter) { - return baseName + expression.substring(1); - } else { - return baseName + expression; - } - } else { - return baseName; - } - } else { - if (expression != null && expression.length() > 0) { - if (expression.charAt(0) == pathDelimiter) { - return baseName + expression; - } else { - return baseName + pathDelimiter + expression; - } - } else { - return baseName; - } - } - } else { - return expression; - } - } - - /** - * Is this expression wild? - * - * @return true if wildcard contained, false otherwise - */ - public boolean isWild() { - return expression != null - && ( - expression.indexOf(getFreeWildcard()) >= 0 - || expression.indexOf(getLocalWildcard()) >= 0); - } - - public String toString() { - return MoreObjects.toStringHelper(this) - .add("expression", expression) - .add("pathDelimiter", pathDelimiter) - .add("pattern", pattern) - .add("namespace", namespace) - .add("user", user) - .add("baseName", baseName) - .toString(); - } - - - private Pattern constructEscapedRegex() { - StringBuilder stringBuilder = new StringBuilder(); - StringTokenizer tokenizer = new StringTokenizer(expression, "*%", true); - while (tokenizer.hasMoreTokens()) { - stringBuilder.append(getRegexPartAssociatedWithToken(tokenizer)); - } - return Pattern.compile(stringBuilder.toString()); - } - - private String getRegexPartAssociatedWithToken(StringTokenizer tokenizer) { - String token = tokenizer.nextToken(); - if (token.equals("*")) { - return ".*"; - } else if (token.equals("%")) { - return "[^" + Pattern.quote(String.valueOf(pathDelimiter)) + "]*"; - } else { - return Pattern.quote(token); - } - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/0cfb3951/mailbox/api/src/main/java/org/apache/james/mailbox/model/PrefixedRegex.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/PrefixedRegex.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/PrefixedRegex.java deleted file mode 100644 index fa1d30f..0000000 --- a/mailbox/api/src/main/java/org/apache/james/mailbox/model/PrefixedRegex.java +++ /dev/null @@ -1,112 +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.james.mailbox.model; - -import java.util.Optional; -import java.util.StringTokenizer; -import java.util.regex.Pattern; - -public class PrefixedRegex implements MailboxNameExpression { - - private final String prefix; - private final String regex; - private final Pattern pattern; - private final char pathDelimiter; - - public PrefixedRegex(String prefix, String regex, char pathDelimiter) { - this.prefix = Optional.ofNullable(prefix).orElse(""); - this.regex = Optional.ofNullable(regex).orElse(""); - this.pathDelimiter = pathDelimiter; - this.pattern = constructEscapedRegex(this.regex); - } - - @Override - public boolean isExpressionMatch(String name) { - return name.startsWith(prefix) - && regexMatching(name.substring(prefix.length())); - } - - private boolean regexMatching(String name) { - if (isWild()) { - return name != null - && pattern.matcher(name).matches(); - } else { - return regex.equals(name); - } - } - - @Override - public String getCombinedName() { - if (prefix != null && prefix.length() > 0) { - final int baseLength = prefix.length(); - if (prefix.charAt(baseLength - 1) == pathDelimiter) { - if (regex != null && regex.length() > 0) { - if (regex.charAt(0) == pathDelimiter) { - return prefix + regex.substring(1); - } else { - return prefix + regex; - } - } else { - return prefix; - } - } else { - if (regex != null && regex.length() > 0) { - if (regex.charAt(0) == pathDelimiter) { - return prefix + regex; - } else { - return prefix + pathDelimiter + regex; - } - } else { - return prefix; - } - } - } else { - return regex; - } - } - - @Override - public boolean isWild() { - return regex != null - && ( - regex.indexOf(FREEWILDCARD) >= 0 - || regex.indexOf(LOCALWILDCARD) >= 0); - } - - private Pattern constructEscapedRegex(String regex) { - StringBuilder stringBuilder = new StringBuilder(); - StringTokenizer tokenizer = new StringTokenizer(regex, "*%", true); - while (tokenizer.hasMoreTokens()) { - stringBuilder.append(getRegexPartAssociatedWithToken(tokenizer)); - } - return Pattern.compile(stringBuilder.toString()); - } - - private String getRegexPartAssociatedWithToken(StringTokenizer tokenizer) { - String token = tokenizer.nextToken(); - if (token.equals("*")) { - return ".*"; - } else if (token.equals("%")) { - return "[^" + Pattern.quote(String.valueOf(pathDelimiter)) + "]*"; - } else { - return Pattern.quote(token); - } - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/0cfb3951/mailbox/api/src/main/java/org/apache/james/mailbox/model/PrefixedWildcard.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/PrefixedWildcard.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/PrefixedWildcard.java deleted file mode 100644 index d767038..0000000 --- a/mailbox/api/src/main/java/org/apache/james/mailbox/model/PrefixedWildcard.java +++ /dev/null @@ -1,47 +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.james.mailbox.model; - -import com.google.common.base.Preconditions; - -public class PrefixedWildcard implements MailboxNameExpression { - - private final String prefix; - - public PrefixedWildcard(String prefix) { - Preconditions.checkNotNull(prefix); - this.prefix = prefix; - } - - @Override - public boolean isExpressionMatch(String name) { - return name.startsWith(prefix); - } - - @Override - public String getCombinedName() { - return prefix + FREEWILDCARD; - } - - @Override - public boolean isWild() { - return true; - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/0cfb3951/mailbox/api/src/main/java/org/apache/james/mailbox/model/Wildcard.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/Wildcard.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/Wildcard.java deleted file mode 100644 index a8c4c23..0000000 --- a/mailbox/api/src/main/java/org/apache/james/mailbox/model/Wildcard.java +++ /dev/null @@ -1,40 +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.james.mailbox.model; - -import com.google.common.base.Preconditions; - -public class Wildcard implements MailboxNameExpression { - @Override - public boolean isExpressionMatch(String name) { - Preconditions.checkNotNull(name); - return true; - } - - @Override - public String getCombinedName() { - return String.valueOf(FREEWILDCARD); - } - - @Override - public boolean isWild() { - return true; - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/0cfb3951/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/ExactName.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/ExactName.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/ExactName.java new file mode 100644 index 0000000..347e978 --- /dev/null +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/ExactName.java @@ -0,0 +1,48 @@ +/**************************************************************** + * 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.search; + +import com.google.common.base.Preconditions; + +public class ExactName implements MailboxNameExpression { + + private final String name; + + public ExactName(String name) { + Preconditions.checkNotNull(name); + this.name = name; + } + + @Override + public boolean isExpressionMatch(String mailboxName) { + Preconditions.checkNotNull(mailboxName); + return name.equals(mailboxName); + } + + @Override + public String getCombinedName() { + return name; + } + + @Override + public boolean isWild() { + return false; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/0cfb3951/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/MailboxNameExpression.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/MailboxNameExpression.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/MailboxNameExpression.java new file mode 100644 index 0000000..04a26bc --- /dev/null +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/MailboxNameExpression.java @@ -0,0 +1,38 @@ +/**************************************************************** + * 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.search; + +interface MailboxNameExpression { + /** + * Use this wildcard to match every char including the hierarchy delimiter + */ + char FREEWILDCARD = '*'; + + /** + * Use this wildcard to match every char except the hierarchy delimiter + */ + char LOCALWILDCARD = '%'; + + boolean isExpressionMatch(String name); + + String getCombinedName(); + + boolean isWild(); +} http://git-wip-us.apache.org/repos/asf/james-project/blob/0cfb3951/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/MailboxQuery.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/MailboxQuery.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/MailboxQuery.java new file mode 100644 index 0000000..9d51b78 --- /dev/null +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/MailboxQuery.java @@ -0,0 +1,318 @@ +/**************************************************************** + * 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.search; + +import java.util.Optional; +import java.util.StringTokenizer; +import java.util.regex.Pattern; + +import org.apache.james.mailbox.MailboxSession; +import org.apache.james.mailbox.model.MailboxConstants; +import org.apache.james.mailbox.model.MailboxPath; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.MoreObjects; +import com.google.common.base.Preconditions; + + +/** + * Expresses select criteria for mailboxes. + */ +public final class MailboxQuery { + /** + * Use this wildcard to match every char including the hierarchy delimiter + */ + public final static char FREEWILDCARD = '*'; + + /** + * Use this wildcard to match every char except the hierarchy delimiter + */ + public final static char LOCALWILDCARD = '%'; + + private static final String EMPTY_PATH_NAME = ""; + + public static Builder builder() { + return new Builder(); + } + + public static Builder privateMailboxesBuilder(MailboxSession session) { + return builder() + .mailboxSession(session) + .username(session.getUser().getUserName()); + } + + public static class Builder { + private String expression; + @VisibleForTesting Optional<Character> pathDelimiter; + @VisibleForTesting Optional<String> username; + @VisibleForTesting Optional<String> pathName; + @VisibleForTesting Optional<String> namespace; + + private Builder() { + this.pathName = Optional.empty(); + this.namespace = Optional.empty(); + this.username = Optional.empty(); + this.pathDelimiter = Optional.empty(); + } + + public Builder base(MailboxPath base) { + this.namespace = Optional.ofNullable(base.getNamespace()); + this.username = Optional.ofNullable(base.getUser()); + this.pathName = Optional.ofNullable(base.getName()); + return this; + } + + public Builder username(String username) { + this.username = Optional.of(username); + return this; + } + + public Builder privateMailboxes() { + Preconditions.checkState(!pathName.isPresent()); + Preconditions.checkState(!namespace.isPresent()); + this.namespace = Optional.of(MailboxConstants.USER_NAMESPACE); + this.pathName = Optional.of(EMPTY_PATH_NAME); + return matchesAll(); + } + + public Builder expression(String expression) { + this.expression = expression; + return this; + } + + public Builder matchesAll() { + this.expression = String.valueOf(FREEWILDCARD); + return this; + } + + public Builder mailboxSession(MailboxSession session) { + this.pathDelimiter = Optional.of(session.getPathDelimiter()); + return this; + } + + public Builder pathDelimiter(char pathDelimiter) { + this.pathDelimiter = Optional.of(pathDelimiter); + return this; + } + + public MailboxQuery build() { + Preconditions.checkState(pathDelimiter.isPresent()); + return new MailboxQuery(namespace, username, pathName, expression, pathDelimiter.get()); + } + } + + private final String expression; + private final char pathDelimiter; + private final Pattern pattern; + private final Optional<String> namespace; + private final Optional<String> user; + private final Optional<String> baseName; + + /** + * Constructs an expression determining a set of mailbox names. + * + * @param base + * base reference name, not null + * @param expression + * mailbox match expression, not null + * @param pathDelimiter + * path delimiter to use + */ + @VisibleForTesting MailboxQuery(Optional<String> namespace, Optional<String> user, Optional<String> baseName, + String expression, char pathDelimiter) { + this.namespace = namespace; + this.user = user; + this.baseName = baseName; + if (expression == null) { + this.expression = ""; + } else { + this.expression = expression; + } + this.pathDelimiter = pathDelimiter; + pattern = constructEscapedRegex(); + } + + public Optional<String> getNamespace() { + return namespace; + } + + public Optional<String> getUser() { + return user; + } + + public Optional<String> getBaseName() { + return baseName; + } + + public boolean isPrivateMailboxes(MailboxSession session) { + MailboxSession.User sessionUser = session.getUser(); + return namespace.map(MailboxConstants.USER_NAMESPACE::equals).orElse(false) + && user.map(sessionUser::isSameUser).orElse(false); + } + + public boolean belongsToRequestedNamespaceAndUser(MailboxPath mailboxPath) { + boolean belongsToRequestedNamespace = namespace + .map(value -> value.equals(mailboxPath.getNamespace())) + .orElse(true); + boolean belongsToRequestedUser = user + .map(value -> value.equals(mailboxPath.getUser())) + .orElse(true); + + return belongsToRequestedNamespace && belongsToRequestedUser; + } + + /** + * Gets the name search expression. This may contain wildcards. + * + * @return the expression + */ + public final String getExpression() { + return expression; + } + + /** + * Gets wildcard character that matches any series of characters. + * + * @return the freeWildcard + */ + public final char getFreeWildcard() { + return FREEWILDCARD; + } + + /** + * Gets wildcard character that matches any series of characters excluding + * hierarchy delimiters. Effectively, this means that it matches any + * sequence within a name part. + * + * @return the localWildcard + */ + public final char getLocalWildcard() { + return LOCALWILDCARD; + } + + /** + * Is the given name a match for {@link #getExpression()}? + * + * @param name + * name to be matched + * @return true if the given name matches this expression, false otherwise + */ + public boolean isExpressionMatch(String name) { + if (isWild()) { + return name != null + && pattern.matcher(name).matches(); + } else { + return expression.equals(name); + } + } + + public boolean isPathMatch(MailboxPath mailboxPath) { + String baseName = this.baseName.orElse(EMPTY_PATH_NAME); + int baseNameLength = baseName.length(); + String mailboxName = mailboxPath.getName(); + + return belongsToRequestedNamespaceAndUser(mailboxPath) + && mailboxName.startsWith(baseName) + && isExpressionMatch(mailboxName.substring(baseNameLength)); + } + + /** + * Get combined name formed by adding the expression to the base using the + * given hierarchy delimiter. Note that the wildcards are retained in the + * combined name. + * + * @return {@link #getBase()} combined with {@link #getExpression()}, + * notnull + */ + public String getCombinedName() { + String baseName = this.baseName.orElse(null); + if (baseName != null && baseName.length() > 0) { + final int baseLength = baseName.length(); + if (baseName.charAt(baseLength - 1) == pathDelimiter) { + if (expression != null && expression.length() > 0) { + if (expression.charAt(0) == pathDelimiter) { + return baseName + expression.substring(1); + } else { + return baseName + expression; + } + } else { + return baseName; + } + } else { + if (expression != null && expression.length() > 0) { + if (expression.charAt(0) == pathDelimiter) { + return baseName + expression; + } else { + return baseName + pathDelimiter + expression; + } + } else { + return baseName; + } + } + } else { + return expression; + } + } + + /** + * Is this expression wild? + * + * @return true if wildcard contained, false otherwise + */ + public boolean isWild() { + return expression != null + && ( + expression.indexOf(getFreeWildcard()) >= 0 + || expression.indexOf(getLocalWildcard()) >= 0); + } + + public String toString() { + return MoreObjects.toStringHelper(this) + .add("expression", expression) + .add("pathDelimiter", pathDelimiter) + .add("pattern", pattern) + .add("namespace", namespace) + .add("user", user) + .add("baseName", baseName) + .toString(); + } + + + private Pattern constructEscapedRegex() { + StringBuilder stringBuilder = new StringBuilder(); + StringTokenizer tokenizer = new StringTokenizer(expression, "*%", true); + while (tokenizer.hasMoreTokens()) { + stringBuilder.append(getRegexPartAssociatedWithToken(tokenizer)); + } + return Pattern.compile(stringBuilder.toString()); + } + + private String getRegexPartAssociatedWithToken(StringTokenizer tokenizer) { + String token = tokenizer.nextToken(); + if (token.equals("*")) { + return ".*"; + } else if (token.equals("%")) { + return "[^" + Pattern.quote(String.valueOf(pathDelimiter)) + "]*"; + } else { + return Pattern.quote(token); + } + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/0cfb3951/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/PrefixedRegex.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/PrefixedRegex.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/PrefixedRegex.java new file mode 100644 index 0000000..3c96a69 --- /dev/null +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/PrefixedRegex.java @@ -0,0 +1,112 @@ +/**************************************************************** + * 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.search; + +import java.util.Optional; +import java.util.StringTokenizer; +import java.util.regex.Pattern; + +public class PrefixedRegex implements MailboxNameExpression { + + private final String prefix; + private final String regex; + private final Pattern pattern; + private final char pathDelimiter; + + public PrefixedRegex(String prefix, String regex, char pathDelimiter) { + this.prefix = Optional.ofNullable(prefix).orElse(""); + this.regex = Optional.ofNullable(regex).orElse(""); + this.pathDelimiter = pathDelimiter; + this.pattern = constructEscapedRegex(this.regex); + } + + @Override + public boolean isExpressionMatch(String name) { + return name.startsWith(prefix) + && regexMatching(name.substring(prefix.length())); + } + + private boolean regexMatching(String name) { + if (isWild()) { + return name != null + && pattern.matcher(name).matches(); + } else { + return regex.equals(name); + } + } + + @Override + public String getCombinedName() { + if (prefix != null && prefix.length() > 0) { + final int baseLength = prefix.length(); + if (prefix.charAt(baseLength - 1) == pathDelimiter) { + if (regex != null && regex.length() > 0) { + if (regex.charAt(0) == pathDelimiter) { + return prefix + regex.substring(1); + } else { + return prefix + regex; + } + } else { + return prefix; + } + } else { + if (regex != null && regex.length() > 0) { + if (regex.charAt(0) == pathDelimiter) { + return prefix + regex; + } else { + return prefix + pathDelimiter + regex; + } + } else { + return prefix; + } + } + } else { + return regex; + } + } + + @Override + public boolean isWild() { + return regex != null + && ( + regex.indexOf(FREEWILDCARD) >= 0 + || regex.indexOf(LOCALWILDCARD) >= 0); + } + + private Pattern constructEscapedRegex(String regex) { + StringBuilder stringBuilder = new StringBuilder(); + StringTokenizer tokenizer = new StringTokenizer(regex, "*%", true); + while (tokenizer.hasMoreTokens()) { + stringBuilder.append(getRegexPartAssociatedWithToken(tokenizer)); + } + return Pattern.compile(stringBuilder.toString()); + } + + private String getRegexPartAssociatedWithToken(StringTokenizer tokenizer) { + String token = tokenizer.nextToken(); + if (token.equals("*")) { + return ".*"; + } else if (token.equals("%")) { + return "[^" + Pattern.quote(String.valueOf(pathDelimiter)) + "]*"; + } else { + return Pattern.quote(token); + } + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/0cfb3951/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/PrefixedWildcard.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/PrefixedWildcard.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/PrefixedWildcard.java new file mode 100644 index 0000000..cbc1934 --- /dev/null +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/PrefixedWildcard.java @@ -0,0 +1,47 @@ +/**************************************************************** + * 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.search; + +import com.google.common.base.Preconditions; + +public class PrefixedWildcard implements MailboxNameExpression { + + private final String prefix; + + public PrefixedWildcard(String prefix) { + Preconditions.checkNotNull(prefix); + this.prefix = prefix; + } + + @Override + public boolean isExpressionMatch(String name) { + return name.startsWith(prefix); + } + + @Override + public String getCombinedName() { + return prefix + FREEWILDCARD; + } + + @Override + public boolean isWild() { + return true; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/0cfb3951/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/Wildcard.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/Wildcard.java b/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/Wildcard.java new file mode 100644 index 0000000..655b5c7 --- /dev/null +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/model/search/Wildcard.java @@ -0,0 +1,40 @@ +/**************************************************************** + * 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.search; + +import com.google.common.base.Preconditions; + +public class Wildcard implements MailboxNameExpression { + @Override + public boolean isExpressionMatch(String name) { + Preconditions.checkNotNull(name); + return true; + } + + @Override + public String getCombinedName() { + return String.valueOf(FREEWILDCARD); + } + + @Override + public boolean isWild() { + return true; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/0cfb3951/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java b/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java index 2d2e438..bc74a13 100644 --- a/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java +++ b/mailbox/api/src/test/java/org/apache/james/mailbox/MailboxManagerTest.java @@ -39,7 +39,7 @@ import org.apache.james.mailbox.model.MailboxAnnotationKey; import org.apache.james.mailbox.model.MailboxId; import org.apache.james.mailbox.model.MailboxMetaData; import org.apache.james.mailbox.model.MailboxPath; -import org.apache.james.mailbox.model.MailboxQuery; +import org.apache.james.mailbox.model.search.MailboxQuery; import org.junit.Assume; import org.junit.Rule; import org.junit.Test; http://git-wip-us.apache.org/repos/asf/james-project/blob/0cfb3951/mailbox/api/src/test/java/org/apache/james/mailbox/model/ExactNameTest.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/model/ExactNameTest.java b/mailbox/api/src/test/java/org/apache/james/mailbox/model/ExactNameTest.java deleted file mode 100644 index 9072503..0000000 --- a/mailbox/api/src/test/java/org/apache/james/mailbox/model/ExactNameTest.java +++ /dev/null @@ -1,67 +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.james.mailbox.model; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import org.junit.Test; - -public class ExactNameTest { - - public static final String NAME = "toto"; - - @Test - public void constructorShouldThrowOnNullName() { - assertThatThrownBy(() -> new ExactName(null)) - .isInstanceOf(NullPointerException.class); - } - - @Test - public void isWildShouldReturnFalse() { - assertThat(new ExactName(NAME).isWild()) - .isFalse(); - } - - @Test - public void getCombinedNameShouldReturnName() { - assertThat(new ExactName(NAME).getCombinedName()) - .isEqualTo(NAME); - } - - @Test - public void isExpressionMatchShouldReturnTrueWhenName() { - assertThat(new ExactName(NAME).isExpressionMatch(NAME)) - .isTrue(); - } - - @Test - public void isExpressionMatchShouldReturnFalseWhenOtherValue() { - assertThat(new ExactName(NAME).isExpressionMatch("other")) - .isFalse(); - } - - @Test - public void isExpressionMatchShouldThrowOnNullValue() { - assertThatThrownBy(() -> new ExactName(NAME).isExpressionMatch(null)) - .isInstanceOf(NullPointerException.class); - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/0cfb3951/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxQueryTest.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxQueryTest.java b/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxQueryTest.java deleted file mode 100644 index 1656bc0..0000000 --- a/mailbox/api/src/test/java/org/apache/james/mailbox/model/MailboxQueryTest.java +++ /dev/null @@ -1,266 +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.james.mailbox.model; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import org.apache.james.mailbox.MailboxSession; -import org.apache.james.mailbox.MailboxSession.User; -import org.apache.james.mailbox.mock.MockMailboxSession; -import org.apache.james.mailbox.model.MailboxQuery.Builder; -import org.junit.Before; -import org.junit.Test; - -public class MailboxQueryTest { - private static final String CURRENT_USER = "user"; - - private MailboxPath mailboxPath; - private MailboxSession mailboxSession; - - @Before - public void setUp() { - mailboxPath = new MailboxPath("namespace", "user", "name"); - mailboxSession = new MockMailboxSession("user"); - } - - @Test - public void buildShouldMatchAllValuesWhenMatchesAll() throws Exception { - //When - MailboxQuery actual = MailboxQuery.builder() - .base(mailboxPath) - .matchesAll() - .mailboxSession(mailboxSession) - .build(); - //Then - assertThat(actual.isExpressionMatch("folder")).isTrue(); - } - - @Test - public void buildShouldConstructMailboxPathWhenPrivateUserMailboxes() throws Exception { - //Given - MailboxPath expected = MailboxPath.forUser("user", ""); - //When - MailboxQuery actual = MailboxQuery.builder() - .username("user") - .privateMailboxes() - .mailboxSession(mailboxSession) - .build(); - //Then - assertThat(actual.getNamespace()).contains(expected.getNamespace()); - assertThat(actual.getUser()).contains(expected.getUser()); - assertThat(actual.getBaseName()).contains(expected.getName()); - } - - @Test - public void buildShouldMatchAllValuesWhenPrivateUserMailboxes() throws Exception { - //Given - Builder testee = MailboxQuery.builder() - .username("user") - .privateMailboxes() - .mailboxSession(mailboxSession); - //When - MailboxQuery actual = testee.build(); - //Then - assertThat(actual.isExpressionMatch("folder")).isTrue(); - } - - @Test - public void builderShouldInitFromSessionWhenGiven() throws Exception { - //Given - MailboxSession mailboxSession = mock(MailboxSession.class); - when(mailboxSession.getPathDelimiter()).thenReturn('#'); - User user = mock(User.class); - when(user.getUserName()).thenReturn("little bobby table"); - when(mailboxSession.getUser()).thenReturn(user); - // When - Builder query = MailboxQuery.privateMailboxesBuilder(mailboxSession); - //Then - assertThat(query.pathDelimiter).contains('#'); - assertThat(query.username).contains("little bobby table"); - } - - @Test - public void builderShouldNotThrowWhenNoBaseDefined() throws Exception { - //Given - Builder testee = MailboxQuery.builder() - .expression("abc") - .mailboxSession(mailboxSession); - //When - testee.build(); - } - - @Test(expected=IllegalStateException.class) - public void builderShouldThrowWhenBaseAndUsernameGiven() throws Exception { - //Given - Builder testee = MailboxQuery.builder() - .base(mailboxPath) - .username("user"); - //When - testee.build(); - } - - @Test(expected=IllegalStateException.class) - public void builderShouldThrowWhenBaseGiven() throws Exception { - //Given - Builder testee = MailboxQuery.builder() - .base(mailboxPath) - .privateMailboxes(); - //When - testee.build(); - } - - @Test(expected=IllegalStateException.class) - public void builderShouldThrowWhenMissingUsername() throws Exception { - //Given - Builder testee = MailboxQuery.builder() - .privateMailboxes(); - //When - testee.build(); - } - - @Test - public void builderShouldUseBaseWhenGiven() throws Exception { - //When - MailboxQuery actual = MailboxQuery.builder() - .base(mailboxPath) - .mailboxSession(mailboxSession) - .build(); - //Then - assertThat(actual.getNamespace()).contains(mailboxPath.getNamespace()); - assertThat(actual.getUser()).contains(mailboxPath.getUser()); - assertThat(actual.getBaseName()).contains(mailboxPath.getName()); - } - - @Test - public void belongsToNamespaceAndUserShouldReturnTrueWithIdenticalMailboxes() { - MailboxQuery mailboxQuery = MailboxQuery.builder() - .base(mailboxPath) - .mailboxSession(mailboxSession) - .build(); - - assertThat(mailboxQuery.belongsToRequestedNamespaceAndUser(mailboxPath)) - .isTrue(); - } - - @Test - public void belongsToNamespaceAndUserShouldReturnTrueWithIdenticalMailboxesWithNullUser() { - MailboxPath base = new MailboxPath("namespace", null, "name"); - - MailboxQuery mailboxQuery = MailboxQuery.builder() - .base(base) - .mailboxSession(mailboxSession) - .build(); - - assertThat(mailboxQuery.belongsToRequestedNamespaceAndUser(mailboxPath)) - .isTrue(); - } - - @Test - public void belongsToNamespaceAndUserShouldReturnTrueWithIdenticalMailboxesWithNullNamespace() { - MailboxPath mailboxPath = new MailboxPath(null, "user", "name"); - - MailboxQuery mailboxQuery = MailboxQuery.builder() - .base(mailboxPath) - .mailboxSession(mailboxSession) - .build(); - - assertThat(mailboxQuery.belongsToRequestedNamespaceAndUser(mailboxPath)) - .isTrue(); - } - - @Test - public void belongsToNamespaceAndUserShouldReturnTrueWithMailboxWithSameNamespaceAndUserWithNullUser() { - MailboxQuery mailboxQuery = MailboxQuery.builder() - .base(new MailboxPath("namespace", null, "name")) - .mailboxSession(mailboxSession) - .build(); - - assertThat(mailboxQuery.belongsToRequestedNamespaceAndUser(new MailboxPath("namespace", null, "name"))) - .isTrue(); - } - - @Test - public void belongsToNamespaceAndUserShouldReturnTrueWithMailboxWithSameNamespaceAndUser() { - MailboxQuery mailboxQuery = MailboxQuery.builder() - .base(new MailboxPath("namespace", CURRENT_USER, "name")) - .mailboxSession(mailboxSession) - .build(); - - assertThat(mailboxQuery.belongsToRequestedNamespaceAndUser(new MailboxPath("namespace", CURRENT_USER, "name2"))) - .isTrue(); - } - - @Test - public void belongsToNamespaceAndUserShouldReturnFalseWithDifferentNamespace() { - MailboxQuery mailboxQuery = MailboxQuery.builder() - .base(new MailboxPath("namespace", CURRENT_USER, "name")) - .mailboxSession(mailboxSession) - .build(); - - assertThat(mailboxQuery.belongsToRequestedNamespaceAndUser(new MailboxPath("namespace2", CURRENT_USER, "name"))) - .isFalse(); - } - - @Test - public void belongsToNamespaceAndUserShouldReturnFalseWithDifferentUser() { - MailboxQuery mailboxQuery = MailboxQuery.builder() - .base(new MailboxPath("namespace", CURRENT_USER, "name")) - .mailboxSession(mailboxSession) - .build(); - - assertThat(mailboxQuery.belongsToRequestedNamespaceAndUser(new MailboxPath("namespace", CURRENT_USER + "2", "name"))) - .isFalse(); - } - @Test - public void belongsToNamespaceAndUserShouldReturnFalseWithOneOfTheUserNull() { - MailboxQuery mailboxQuery = MailboxQuery.builder() - .base(new MailboxPath("namespace", CURRENT_USER, "name")) - .mailboxSession(mailboxSession) - .build(); - - assertThat(mailboxQuery.belongsToRequestedNamespaceAndUser(new MailboxPath("namespace", null, "name"))) - .isFalse(); - } - - @Test - public void belongsToNamespaceAndUserShouldReturnFalseWhenDifferentUser() { - MailboxQuery mailboxQuery = MailboxQuery.builder() - .base(new MailboxPath("namespace", CURRENT_USER, "name")) - .mailboxSession(mailboxSession) - .build(); - - assertThat(mailboxQuery.belongsToRequestedNamespaceAndUser(new MailboxPath("namespace", "other", "name"))) - .isFalse(); - } - - @Test - public void belongsToNamespaceAndUserShouldReturnFalseIfNamespaceAreDifferentWithNullUser() { - MailboxQuery mailboxQuery = MailboxQuery.builder() - .base(new MailboxPath("namespace", null, "name")) - .mailboxSession(mailboxSession) - .build(); - - assertThat(mailboxQuery.belongsToRequestedNamespaceAndUser(new MailboxPath("namespace2", null, "name"))) - .isFalse(); - } -} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org