JAMES-1838 Upgrade PathConverter code quality
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/98431877 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/98431877 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/98431877 Branch: refs/heads/master Commit: 984318779e0b0679c6bfbb224445b5ce4f6441d7 Parents: 490bd69 Author: Benoit Tellier <btell...@linagora.com> Authored: Wed Oct 19 17:17:11 2016 +0200 Committer: Benoit Tellier <btell...@linagora.com> Committed: Fri Oct 21 17:01:04 2016 +0200 ---------------------------------------------------------------------- .../apache/james/imap/main/PathConverter.java | 79 +++++++---- .../james/imap/main/PathConverterTest.java | 142 +++++++++++++++++++ 2 files changed, 193 insertions(+), 28 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/98431877/protocols/imap/src/main/java/org/apache/james/imap/main/PathConverter.java ---------------------------------------------------------------------- diff --git a/protocols/imap/src/main/java/org/apache/james/imap/main/PathConverter.java b/protocols/imap/src/main/java/org/apache/james/imap/main/PathConverter.java index 1f68601..5866ee9 100644 --- a/protocols/imap/src/main/java/org/apache/james/imap/main/PathConverter.java +++ b/protocols/imap/src/main/java/org/apache/james/imap/main/PathConverter.java @@ -19,60 +19,83 @@ package org.apache.james.imap.main; +import java.util.List; + import org.apache.james.imap.api.ImapSessionUtils; import org.apache.james.imap.api.process.ImapSession; -import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.model.MailboxConstants; import org.apache.james.mailbox.model.MailboxPath; +import com.google.common.base.Joiner; +import com.google.common.base.Preconditions; +import com.google.common.base.Splitter; +import com.google.common.base.Strings; +import com.google.common.collect.Iterables; + public class PathConverter { + private static final int NAMESPACE = 0; + public static PathConverter forSession(ImapSession session) { return new PathConverter(session); } private final ImapSession session; - public PathConverter(ImapSession session) { + private PathConverter(ImapSession session) { this.session = session; } public MailboxPath buildFullPath(String mailboxName) { - String namespace = null; - String name = null; - final MailboxSession mailboxSession = ImapSessionUtils.getMailboxSession(session); - - if (mailboxName == null || mailboxName.length() == 0) { - return new MailboxPath("", "", ""); + if (Strings.isNullOrEmpty(mailboxName)) { + return buildDefaultPath(); } - if (mailboxName.charAt(0) == MailboxConstants.NAMESPACE_PREFIX_CHAR) { - int namespaceLength = mailboxName.indexOf(mailboxSession.getPathDelimiter()); - if (namespaceLength > -1) { - namespace = mailboxName.substring(0, namespaceLength); - if (mailboxName.length() > namespaceLength) - name = mailboxName.substring(++namespaceLength); - } else { - namespace = mailboxName; - } + if (isAbsolute(mailboxName)) { + return buildAbsolutePath(mailboxName); } else { - namespace = MailboxConstants.USER_NAMESPACE; - name = mailboxName; + return buildRelativePath(mailboxName); } - String user = null; - // we only use the user as part of the MailboxPath if its a private user - // namespace + } + + private MailboxPath buildDefaultPath() { + return new MailboxPath("", "", ""); + } + + private boolean isAbsolute(String mailboxName) { + Preconditions.checkArgument(!Strings.isNullOrEmpty(mailboxName)); + return mailboxName.charAt(0) == MailboxConstants.NAMESPACE_PREFIX_CHAR; + } + + private MailboxPath buildRelativePath(String mailboxName) { + return buildMailboxPath(MailboxConstants.USER_NAMESPACE, ImapSessionUtils.getUserName(session), mailboxName); + } + + private MailboxPath buildAbsolutePath(String absolutePath) { + char pathDelimiter = ImapSessionUtils.getMailboxSession(session).getPathDelimiter(); + List<String> mailboxPathParts = Splitter.on(pathDelimiter).splitToList(absolutePath); + String namespace = mailboxPathParts.get(NAMESPACE); + String mailboxName = Joiner.on(pathDelimiter).join(Iterables.skip(mailboxPathParts, 1)); + return buildMailboxPath(namespace, retrieveUserName(namespace), mailboxName); + } + + private String retrieveUserName(String namespace) { if (namespace.equals(MailboxConstants.USER_NAMESPACE)) { - user = ImapSessionUtils.getUserName(session); + return ImapSessionUtils.getUserName(session); } + return null; + } + + private MailboxPath buildMailboxPath(String namespace, String user, String mailboxName) { + return new MailboxPath(namespace, user, sanitizeMailboxName(mailboxName)); + } + private String sanitizeMailboxName(String mailboxName) { // use uppercase for INBOX - // // See IMAP-349 - if (name.equalsIgnoreCase(MailboxConstants.INBOX)) { - name = MailboxConstants.INBOX; + if (mailboxName.equalsIgnoreCase(MailboxConstants.INBOX)) { + return MailboxConstants.INBOX; } - - return new MailboxPath(namespace, user, name); + return mailboxName; } } http://git-wip-us.apache.org/repos/asf/james-project/blob/98431877/protocols/imap/src/test/java/org/apache/james/imap/main/PathConverterTest.java ---------------------------------------------------------------------- diff --git a/protocols/imap/src/test/java/org/apache/james/imap/main/PathConverterTest.java b/protocols/imap/src/test/java/org/apache/james/imap/main/PathConverterTest.java new file mode 100644 index 0000000..0f6bae1 --- /dev/null +++ b/protocols/imap/src/test/java/org/apache/james/imap/main/PathConverterTest.java @@ -0,0 +1,142 @@ +/**************************************************************** + * 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.imap.main; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.apache.james.imap.api.ImapSessionUtils; +import org.apache.james.imap.api.process.ImapSession; +import org.apache.james.mailbox.MailboxSession; +import org.apache.james.mailbox.model.MailboxConstants; +import org.apache.james.mailbox.model.MailboxPath; +import org.junit.Before; +import org.junit.Test; + +public class PathConverterTest { + + private static final String USERNAME = "username"; + private static final char PATH_DELIMITER = '.'; + + private ImapSession imapSession; + private MailboxSession mailboxSession; + private PathConverter pathConverter; + + @Before + public void setUp() { + imapSession = mock(ImapSession.class); + mailboxSession = mock(MailboxSession.class); + MailboxSession.User user = mock(MailboxSession.User.class); + pathConverter = PathConverter.forSession(imapSession); + when(imapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)).thenReturn(mailboxSession); + when(mailboxSession.getUser()).thenReturn(user); + when(mailboxSession.getPathDelimiter()).thenReturn(PATH_DELIMITER); + when(user.getUserName()).thenReturn(USERNAME); + } + + @Test + public void buildFullPathShouldAcceptNull() { + assertThat(pathConverter.buildFullPath(null)) + .isEqualTo(new MailboxPath("", "", "")); + } + + @Test + public void buildPathShouldAcceptEmpty() { + assertThat(pathConverter.buildFullPath("")) + .isEqualTo(new MailboxPath("", "", "")); + } + + @Test + public void buildPathShouldAcceptRelativeMailboxName() { + String mailboxName = "mailboxName"; + assertThat(pathConverter.buildFullPath(mailboxName)) + .isEqualTo(new MailboxPath(MailboxConstants.USER_NAMESPACE, USERNAME, mailboxName)); + } + + @Test + public void buildFullPathShouldAcceptNamespacePrefix() { + assertThat(pathConverter.buildFullPath("#")) + .isEqualTo(new MailboxPath("#", null, "")); + } + + @Test + public void buildFullPathShouldAcceptUserNamespace() { + assertThat(pathConverter.buildFullPath(MailboxConstants.USER_NAMESPACE)) + .isEqualTo(new MailboxPath(MailboxConstants.USER_NAMESPACE, USERNAME, "")); + } + + @Test + public void buildFullPathShouldAcceptNamespaceAlone() { + String namespace = "#any"; + assertThat(pathConverter.buildFullPath(namespace)) + .isEqualTo(new MailboxPath(namespace, null, "")); + } + + @Test + public void buildFullPathShouldAcceptUserNamespaceAndDelimiter() { + assertThat(pathConverter.buildFullPath(MailboxConstants.USER_NAMESPACE + PATH_DELIMITER)) + .isEqualTo(new MailboxPath(MailboxConstants.USER_NAMESPACE, USERNAME, "")); + } + + @Test + public void buildFullPathShouldAcceptNamespaceAndDelimiter() { + String namespace = "#any"; + assertThat(pathConverter.buildFullPath(namespace + PATH_DELIMITER)) + .isEqualTo(new MailboxPath(namespace, null, "")); + } + + @Test + public void buildFullPathShouldAcceptFullAbsoluteUserPath() { + String mailboxName = "mailboxName"; + assertThat(pathConverter.buildFullPath(MailboxConstants.USER_NAMESPACE + PATH_DELIMITER + mailboxName)) + .isEqualTo(new MailboxPath(MailboxConstants.USER_NAMESPACE, USERNAME, mailboxName)); + } + + @Test + public void buildFullPathShouldAcceptFullAbsolutePath() { + String namespace = "#any"; + String mailboxName = "mailboxName"; + assertThat(pathConverter.buildFullPath(namespace + PATH_DELIMITER + mailboxName)) + .isEqualTo(new MailboxPath(namespace, null, mailboxName)); + } + + @Test + public void buildFullPathShouldAcceptRelativePathWithSubFolder() { + String mailboxName = "mailboxName" + PATH_DELIMITER + "subFolder"; + assertThat(pathConverter.buildFullPath(mailboxName)) + .isEqualTo(new MailboxPath(MailboxConstants.USER_NAMESPACE, USERNAME, mailboxName)); + } + + @Test + public void buildFullPathShouldAcceptAbsoluteUserPathWithSubFolder() { + String mailboxName = "mailboxName.subFolder"; + assertThat(pathConverter.buildFullPath(MailboxConstants.USER_NAMESPACE + PATH_DELIMITER + mailboxName)) + .isEqualTo(new MailboxPath(MailboxConstants.USER_NAMESPACE, USERNAME, mailboxName)); + } + + @Test + public void buildFullPathShouldAcceptAbsolutePathWithSubFolder() { + String namespace = "#any"; + String mailboxName = "mailboxName.subFolder"; + assertThat(pathConverter.buildFullPath(namespace + PATH_DELIMITER + mailboxName)) + .isEqualTo(new MailboxPath(namespace, null, mailboxName)); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org