1 /* 2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.openjdk.skara.mailinglist; 24 25 import org.openjdk.skara.email.*; 26 import org.openjdk.skara.test.TemporaryDirectory; 27 28 import org.junit.jupiter.api.Test; 29 30 import java.io.IOException; 31 import java.nio.charset.StandardCharsets; 32 import java.nio.file.Files; 33 import java.time.Duration; 34 35 import static org.junit.jupiter.api.Assertions.*; 36 37 class MboxTests { 38 @Test 39 void simple() { 40 try (var folder = new TemporaryDirectory()) { 41 var mbox = MailingListServerFactory.createMboxFileServer(folder.path()); 42 var list = mbox.getList("test"); 43 44 var sender = EmailAddress.from("test", "test@test.mail"); 45 var sentMail = Email.create(sender, "Subject", "Message").build(); 46 list.post(sentMail); 47 var conversations = list.conversations(Duration.ofDays(1)); 48 assertEquals(1, conversations.size()); 49 var conversation = conversations.get(0); 50 assertEquals(sentMail, conversation.first()); 51 } 52 } 53 54 @Test 55 void multiple() { 56 try (var folder = new TemporaryDirectory()) { 57 var mbox = MailingListServerFactory.createMboxFileServer(folder.path()); 58 var list = mbox.getList("test"); 59 60 var sender1 = EmailAddress.from("test1", "test1@test.mail"); 61 var sender2 = EmailAddress.from("test2", "test2@test.mail"); 62 63 var sentParent = Email.create(sender1, "Subject 1", "Message 1").build(); 64 list.post(sentParent); 65 var conversations = list.conversations(Duration.ofDays(1)); 66 assertEquals(1, conversations.size()); 67 68 var sentReply = Email.create(sender2, "Subject 2", "Message 2") 69 .header("In-Reply-To", sentParent.id().toString()) 70 .header("References", sentParent.id().toString()) 71 .build(); 72 list.post(sentReply); 73 conversations = list.conversations(Duration.ofDays(1)); 74 assertEquals(1, conversations.size()); 75 var conversation = conversations.get(0); 76 assertEquals(sentParent, conversation.first()); 77 var replies = conversation.replies(sentParent); 78 assertEquals(1, replies.size()); 79 var reply = replies.get(0); 80 assertEquals(sentReply, reply); 81 } 82 } 83 84 @Test 85 void uninitialized() { 86 try (var folder = new TemporaryDirectory()) { 87 var mbox = MailingListServerFactory.createMboxFileServer(folder.path()); 88 var list = mbox.getList("test"); 89 var conversations = list.conversations(Duration.ofDays(1)); 90 assertEquals(0, conversations.size()); 91 } 92 } 93 94 @Test 95 void nested() { 96 try (var folder = new TemporaryDirectory()) { 97 var mbox = MailingListServerFactory.createMboxFileServer(folder.path()); 98 var list = mbox.getList("this/is/a/nested/path/test"); 99 100 var sender = EmailAddress.from("test", "test@test.mail"); 101 var sentMail = Email.create(sender, "Subject", "Message").build(); 102 list.post(sentMail); 103 var conversations = list.conversations(Duration.ofDays(1)); 104 assertEquals(1, conversations.size()); 105 var conversation = conversations.get(0); 106 assertEquals(sentMail, conversation.first()); 107 } 108 } 109 110 @Test 111 void differentAuthor() { 112 try (var folder = new TemporaryDirectory()) { 113 var mbox = MailingListServerFactory.createMboxFileServer(folder.path()); 114 var list = mbox.getList("test"); 115 116 var sender = EmailAddress.from("test1", "test1@test.mail"); 117 var author = EmailAddress.from("test2", "test2@test.mail"); 118 var sentMail = Email.create(author, "Subject", "Message").sender(sender).build(); 119 list.post(sentMail); 120 var conversations = list.conversations(Duration.ofDays(1)); 121 assertEquals(1, conversations.size()); 122 var conversation = conversations.get(0); 123 assertEquals(sentMail, conversation.first()); 124 } 125 } 126 127 @Test 128 void encodedFrom() { 129 try (var folder = new TemporaryDirectory()) { 130 var mbox = MailingListServerFactory.createMboxFileServer(folder.path()); 131 var list = mbox.getList("test"); 132 133 var sender = EmailAddress.from("test", "test@test.mail"); 134 var sentMail = Email.create(sender, "Subject", "From is an odd way to start\n" + 135 "From may also be the second row\n" + 136 ">>From as a quote\n" + 137 "And From in the middle").build(); 138 list.post(sentMail); 139 var conversations = list.conversations(Duration.ofDays(1)); 140 assertEquals(1, conversations.size()); 141 var conversation = conversations.get(0); 142 assertEquals(sentMail, conversation.first()); 143 } 144 } 145 146 @Test 147 void utf8Encode() { 148 try (var folder = new TemporaryDirectory()) { 149 var mbox = MailingListServerFactory.createMboxFileServer(folder.path()); 150 var list = mbox.getList("test"); 151 152 var sender = EmailAddress.from("têßt", "test@test.mail"); 153 var sentMail = Email.create(sender, "Sübjeçt", "(╯°□°)╯︵ ┻━┻").build(); 154 list.post(sentMail); 155 var conversations = list.conversations(Duration.ofDays(1)); 156 assertEquals(1, conversations.size()); 157 var conversation = conversations.get(0); 158 assertEquals(sentMail, conversation.first()); 159 } 160 } 161 162 @Test 163 void unencodedFrom() throws IOException { 164 try (var folder = new TemporaryDirectory()) { 165 var rawMbox = folder.path().resolve("test.mbox"); 166 Files.writeString(rawMbox, 167 "From test at example.com Wed Aug 21 17:22:50 2019\n" + 168 "From: test at example.com (test at example.com)\n" + 169 "Date: Wed, 21 Aug 2019 17:22:50 +0000\n" + 170 "Subject: this is a test\n" + 171 "Message-ID: <abc123@example.com>\n" + 172 "\n" + 173 "Sometimes there are unencoded from lines as well\n" + 174 "\n" + 175 "From this point onwards, it may be hard to parse this\n" + 176 "\n", StandardCharsets.UTF_8); 177 var mbox = MailingListServerFactory.createMboxFileServer(folder.path()); 178 var list = mbox.getList("test"); 179 var conversations = list.conversations(Duration.ofDays(365 * 100)); 180 assertEquals(1, conversations.size()); 181 var conversation = conversations.get(0); 182 assertEquals(1, conversation.allMessages().size()); 183 assertTrue(conversation.first().body().contains("there are unencoded"), conversation.first().body()); 184 assertTrue(conversation.first().body().contains("this point onwards"), conversation.first().body()); 185 } 186 } 187 }