This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 6fc956be61100c0b4fab4dab9b4d885d10a66089
Author: Benoit TELLIER <btell...@linagora.com>
AuthorDate: Wed Oct 9 14:39:27 2024 +0200

    JAMES-2182 PathConverter: handle virtual hosting
    
    As we can only access resources within a single domain the
    domain is implicit and thus do not need to be repeated in the
    IMAP wording.
---
 .../org/apache/james/imap/main/PathConverter.java  |  12 +--
 .../apache/james/imap/main/PathConverterTest.java  | 114 +++++++++++++++++++++
 2 files changed, 120 insertions(+), 6 deletions(-)

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 151e503f39..3bbcbf469f 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
@@ -113,7 +113,7 @@ public interface PathConverter {
          */
         private String joinMailboxPath(MailboxPath mailboxPath, MailboxSession 
session) {
             StringBuilder sb = new StringBuilder();
-            if (mailboxPath.getNamespace() != null && 
!mailboxPath.getNamespace().equals("")) {
+            if (mailboxPath.getNamespace() != null && 
!mailboxPath.getNamespace().isEmpty()) {
                 if 
(mailboxPath.getNamespace().equalsIgnoreCase(MailboxConstants.USER_NAMESPACE)
                     && !mailboxPath.belongsTo(session)) {
                     sb.append("#user");
@@ -121,16 +121,16 @@ public interface PathConverter {
                     sb.append(mailboxPath.getNamespace());
                 }
             }
-            if (mailboxPath.getUser() != null && 
!mailboxPath.getUser().equals("")) {
+            if (mailboxPath.getUser() != null) {
                 if (!mailboxPath.belongsTo(session)) {
-                    if (sb.length() > 0) {
+                    if (!sb.isEmpty()) {
                         sb.append(session.getPathDelimiter());
                     }
-                    sb.append(mailboxPath.getUser().asString());
+                    sb.append(mailboxPath.getUser().getLocalPart());
                 }
             }
-            if (mailboxPath.getName() != null && 
!mailboxPath.getName().equals("")) {
-                if (sb.length() > 0) {
+            if (mailboxPath.getName() != null && 
!mailboxPath.getName().isEmpty()) {
+                if (!sb.isEmpty()) {
                     sb.append(session.getPathDelimiter());
                 }
                 sb.append(mailboxPath.getName());
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
index eedc447b97..f314f32949 100644
--- 
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
@@ -29,12 +29,16 @@ import org.apache.james.mailbox.MailboxSessionUtil;
 import org.apache.james.mailbox.model.MailboxConstants;
 import org.apache.james.mailbox.model.MailboxPath;
 import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
 
 class PathConverterTest {
 
     private static final Username USERNAME = Username.of("username");
     private static final Username USERNAME2 = Username.of("username2");
+
+    private static final Username USERNAME_WITH_MAIL = 
Username.of("usern...@apache.org");
+    private static final Username USERNAME2_WITH_MAIL = 
Username.of("userna...@apache.org");
     private static final char PATH_DELIMITER = '.';
     public static final boolean RELATIVE = true;
 
@@ -155,4 +159,114 @@ class PathConverterTest {
         assertThat(pathConverter.mailboxName(!RELATIVE, new 
MailboxPath("#Shared", Username.of("marketing"), "abc"), mailboxSession))
             .isEqualTo("#Shared.marketing.abc");
     }
+    @Nested
+    class WithEmail{
+        @BeforeEach
+        void setUp() {
+            imapSession = new FakeImapSession();
+            mailboxSession = MailboxSessionUtil.create(USERNAME_WITH_MAIL);
+            pathConverter = 
PathConverter.Factory.DEFAULT.forSession(imapSession);
+            imapSession.setMailboxSession(mailboxSession);
+        }
+
+        @Test
+        void buildFullPathShouldAcceptNull() {
+            assertThat(pathConverter.buildFullPath(null))
+                .isEqualTo(new MailboxPath("", USERNAME_WITH_MAIL, ""));
+        }
+
+        @Test
+        void buildPathShouldAcceptEmpty() {
+            assertThat(pathConverter.buildFullPath(""))
+                .isEqualTo(new MailboxPath("", USERNAME_WITH_MAIL, ""));
+        }
+
+        @Test
+        void buildPathShouldAcceptRelativeMailboxName() {
+            String mailboxName = "mailboxName";
+            assertThat(pathConverter.buildFullPath(mailboxName))
+                .isEqualTo(MailboxPath.forUser(USERNAME_WITH_MAIL, 
mailboxName));
+        }
+
+        @Test
+        void buildFullPathShouldAcceptUserNamespace() {
+            
assertThat(pathConverter.buildFullPath(MailboxConstants.USER_NAMESPACE))
+                .isEqualTo(MailboxPath.forUser(USERNAME_WITH_MAIL, ""));
+        }
+
+        @Test
+        void buildFullPathShouldAcceptUserNamespaceAndDelimiter() {
+            
assertThat(pathConverter.buildFullPath(MailboxConstants.USER_NAMESPACE + 
PATH_DELIMITER))
+                .isEqualTo(MailboxPath.forUser(USERNAME_WITH_MAIL, ""));
+        }
+
+        @Test
+        void buildFullPathShouldAcceptFullAbsoluteUserPath() {
+            String mailboxName = "mailboxName";
+            
assertThat(pathConverter.buildFullPath(MailboxConstants.USER_NAMESPACE + 
PATH_DELIMITER + mailboxName))
+                .isEqualTo(MailboxPath.forUser(USERNAME_WITH_MAIL, 
mailboxName));
+        }
+
+        @Test
+        void buildFullPathShouldAcceptRelativePathWithSubFolder() {
+            String mailboxName = "mailboxName" + PATH_DELIMITER + "subFolder";
+            assertThat(pathConverter.buildFullPath(mailboxName))
+                .isEqualTo(MailboxPath.forUser(USERNAME_WITH_MAIL, 
mailboxName));
+        }
+
+        @Test
+        void buildFullPathShouldAcceptAbsoluteUserPathWithSubFolder() {
+            String mailboxName = "mailboxName.subFolder";
+            
assertThat(pathConverter.buildFullPath(MailboxConstants.USER_NAMESPACE + 
PATH_DELIMITER + mailboxName))
+                .isEqualTo(MailboxPath.forUser(USERNAME_WITH_MAIL, 
mailboxName));
+        }
+
+        @Test
+        void buildFullPathShouldAcceptAbsoluteOtherUserPath() {
+            assertThat(pathConverter.buildFullPath("#user.username2.abc"))
+                .isEqualTo(MailboxPath.forUser(USERNAME2_WITH_MAIL, "abc"));
+        }
+
+        @Test
+        void buildFullPathShouldAcceptAbsoluteOtherUserPathWithSubfolder() {
+            assertThat(pathConverter.buildFullPath("#user.username2.abc.def"))
+                .isEqualTo(MailboxPath.forUser(USERNAME2_WITH_MAIL, 
"abc.def"));
+        }
+
+        @Test
+        void mailboxNameShouldReturnNameOnlyWhenRelativeAndUserMailbox() {
+            assertThat(pathConverter.mailboxName(RELATIVE, 
MailboxPath.forUser(USERNAME_WITH_MAIL, "abc"), mailboxSession))
+                .isEqualTo("abc");
+        }
+
+        @Test
+        void mailboxNameShouldReturnFQDNWhenRelativeAndOtherUserMailbox() {
+            assertThat(pathConverter.mailboxName(RELATIVE, 
MailboxPath.forUser(USERNAME2_WITH_MAIL, "abc"), mailboxSession))
+                .isEqualTo("#user.username2.abc");
+        }
+
+        @Test
+        void mailboxNameShouldReturnFQDNWhenRelativeAndSharedMailbox() {
+            assertThat(pathConverter.mailboxName(RELATIVE, new 
MailboxPath("#Shared", Username.of("market...@apache.org"), "abc"), 
mailboxSession))
+                .isEqualTo("#Shared.marketing.abc");
+        }
+
+        @Test
+        void mailboxNameShouldReturnFQDNWhenNotRelativeAndUserMailbox() {
+            assertThat(pathConverter.mailboxName(!RELATIVE, 
MailboxPath.forUser(USERNAME_WITH_MAIL, "abc"), mailboxSession))
+                .isEqualTo("#private.abc");
+        }
+
+        @Test
+        void mailboxNameShouldReturnFQDNWhenNotRelativeAndOtherUserMailbox() {
+            assertThat(pathConverter.mailboxName(!RELATIVE, 
MailboxPath.forUser(USERNAME2_WITH_MAIL, "abc"), mailboxSession))
+                .isEqualTo("#user.username2.abc");
+        }
+
+        @Test
+        void mailboxNameShouldReturnFQDNWhenNotRelativeAndSharedMailbox() {
+            assertThat(pathConverter.mailboxName(!RELATIVE, new 
MailboxPath("#Shared", Username.of("market...@apache.org"), "abc"), 
mailboxSession))
+                .isEqualTo("#Shared.marketing.abc");
+        }
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org
For additional commands, e-mail: notifications-h...@james.apache.org

Reply via email to