Java Code Examples for com.icegreen.greenmail.util.GreenMailUtil#random()

The following examples show how to use com.icegreen.greenmail.util.GreenMailUtil#random() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SmtpServerTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendAndWaitForIncomingMailsInBcc() throws Throwable {
    String subject = GreenMailUtil.random();
    String body = GreenMailUtil.random();
    final MimeMessage message = createTextEmail("test@localhost", "from@localhost", subject, body, greenMail.getSmtp().getServerSetup());
    message.addRecipients(Message.RecipientType.BCC, "bcc1@localhost,bcc2@localhost");

    assertEquals(0, greenMail.getReceivedMessages().length);

    GreenMailUtil.sendMimeMessage(message);

    assertTrue(greenMail.waitForIncomingEmail(1500, 3));

    MimeMessage[] emails = greenMail.getReceivedMessages();
    assertEquals(3, emails.length);
}
 
Example 2
Source File: Pop3ServerTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetrieve() throws Exception {
    assertNotNull(greenMail.getPop3());
    final String subject = GreenMailUtil.random();
    final String body = GreenMailUtil.random() + "\r\n" + GreenMailUtil.random() + "\r\n" + GreenMailUtil.random();
    String to = "[email protected]";
    GreenMailUtil.sendTextEmailTest(to, "[email protected]", subject, body);
    greenMail.waitForIncomingEmail(5000, 1);

    try (Retriever retriever = new Retriever(greenMail.getPop3())) {
        Message[] messages = retriever.getMessages(to);
        assertEquals(1, messages.length);
        assertEquals(subject, messages[0].getSubject());
        assertEquals(body, GreenMailUtil.getBody(messages[0]).trim());

        // UID
        POP3Folder f = (POP3Folder) messages[0].getFolder();
        assertNotEquals("UNKNOWN", f.getUID(messages[0]));
    }
}
 
Example 3
Source File: SmtpServerTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testSmtpServerReceiveWithAUTHSuffix() throws Throwable {
    assertEquals(0, greenMail.getReceivedMessages().length);

    String subject = GreenMailUtil.random();

    Properties mailProps = new Properties();
    mailProps.setProperty("mail.smtp.from", "<[email protected]> AUTH <somethingidontknow>");
    Session session = GreenMailUtil.getSession(ServerSetupTest.SMTP, mailProps);

    MimeMessage message = new MimeMessage(session);
    message.setContent("body1", "text/plain");
    message.setFrom("from@localhost");
    message.setRecipients(RecipientType.TO, InternetAddress.parse("to@localhost"));
    message.setSubject(subject);

    GreenMailUtil.sendMimeMessage(message);
    System.setProperty("mail.smtp.from", "<[email protected]> AUTH <somethingidontknow>");
    

    greenMail.waitForIncomingEmail(1500, 1);
    MimeMessage[] emails = greenMail.getReceivedMessages();
    assertEquals(1, emails.length);
    assertEquals(subject, emails[0].getSubject());
}
 
Example 4
Source File: Pop3ServerTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testPop3sReceive() throws Throwable {
    assertNotNull(greenMail.getPop3s());
    final String subject = GreenMailUtil.random();
    final String body = GreenMailUtil.random();
    String to = "[email protected]";
    GreenMailUtil.sendTextEmailSecureTest(to, "[email protected]", subject, body);
    greenMail.waitForIncomingEmail(5000, 1);

    try (Retriever retriever = new Retriever(greenMail.getPop3s())) {
        Message[] messages = retriever.getMessages(to);
        assertEquals(1, messages.length);
        assertEquals(subject, messages[0].getSubject());
        assertEquals(body, GreenMailUtil.getBody(messages[0]).trim());
    }
}
 
Example 5
Source File: ImapServerTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testImapsReceive() throws Throwable {
    assertNotNull(greenMail.getImaps());
    final String subject = GreenMailUtil.random();
    final String body = GreenMailUtil.random();
    String to = "test@localhost";
    GreenMailUtil.sendTextEmailSecureTest(to, "from@localhost", subject, body);
    greenMail.waitForIncomingEmail(5000, 1);

    try (Retriever retriever = new Retriever(greenMail.getImaps())) {
        Message[] messages = retriever.getMessages(to);
        assertEquals(1, messages.length);
        assertEquals(subject, messages[0].getSubject());
        assertEquals(body, ((String) messages[0].getContent()).trim());
    }
}
 
Example 6
Source File: ImapServerTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetreiveSimpleWithNonDefaultPassword() throws Exception {
    assertNotNull(greenMail.getImap());
    final String to = "[email protected]";
    final String password = "donotharmanddontrecipricateharm";
    greenMail.setUser(to, password);
    final String subject = GreenMailUtil.random();
    final String body = GreenMailUtil.random();
    GreenMailUtil.sendTextEmailTest(to, "from@localhost", subject, body);
    greenMail.waitForIncomingEmail(5000, 1);

    try (Retriever retriever = new Retriever(greenMail.getImap())) {
        try {
            retriever.getMessages(to, "wrongpassword");
            fail("Expected failed login");
        } catch (Throwable e) {
            // ok
        }

        Message[] messages = retriever.getMessages(to, password);
        assertEquals(1, messages.length);
        assertEquals(subject, messages[0].getSubject());
        assertEquals(body, ((String) messages[0].getContent()).trim());
    }
}
 
Example 7
Source File: Pop3ServerTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetrieveWithNonDefaultPassword() throws Exception {
    assertNotNull(greenMail.getPop3());
    final String to = "[email protected]";
    final String password = "donotharmanddontrecipricateharm";
    greenMail.setUser(to, password);
    final String subject = GreenMailUtil.random();
    final String body = GreenMailUtil.random();
    GreenMailUtil.sendTextEmailTest(to, "[email protected]", subject, body);
    greenMail.waitForIncomingEmail(5000, 1);

    try (Retriever retriever = new Retriever(greenMail.getPop3())) {
        try {
            retriever.getMessages(to, "wrongpassword");
            fail("Expected authentication failure");
        } catch (Throwable e) {
            // ok
        }

        Message[] messages = retriever.getMessages(to, password);
        assertEquals(1, messages.length);
        assertEquals(subject, messages[0].getSubject());
        assertEquals(body, GreenMailUtil.getBody(messages[0]).trim());
    }
}
 
Example 8
Source File: EMailTestServer.java    From syndesis with Apache License 2.0 5 votes vote down vote up
public void generateFolder(String user, String password, String folderName) throws Exception {
    if (server instanceof SmtpServer) {
        throw new Exception("SMTP not applicable for generating folders");
    }

    Store store = server.createStore();
    store.connect(user, password);

    Folder newFolder = store.getFolder(folderName);
    if (! newFolder.exists()) {
        newFolder.create(Folder.HOLDS_MESSAGES);
        assertTrue(newFolder.exists());
    }

    newFolder.open(Folder.READ_WRITE);
    assertTrue(newFolder.isOpen());

    List<MimeMessage> msgs = new ArrayList<>();
    for (int i = 1; i <= 5; ++i) {
        // Use random content to avoid potential residual lingering problems
        String subject = folderName + SPACE + HYPHEN + SPACE + GreenMailUtil.random();
        String body = folderName + NEW_LINE + GreenMailUtil.random();
        GreenMailUser greenUser = greenMail.setUser(user, password);
        msgs.add(createTextMessage(greenUser.getEmail(), "Ben" + i + "@test.com", subject, body)); // Construct message
    }

    newFolder.appendMessages(msgs.toArray(new MimeMessage[0]));
    assertEquals(msgs.size(), newFolder.getMessageCount());
}
 
Example 9
Source File: SmtpServerTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testSmtpServerReceiveMultipart() throws Exception {
    assertEquals(0, greenMail.getReceivedMessages().length);

    String subject = GreenMailUtil.random();
    String body = GreenMailUtil.random();
    GreenMailUtil.sendAttachmentEmail("[email protected]", "[email protected]", subject, body, new byte[]{0, 1, 2}, "image/gif", "testimage_filename", "testimage_description", ServerSetupTest.SMTP);
    greenMail.waitForIncomingEmail(1500, 1);
    Message[] emails = greenMail.getReceivedMessages();
    assertEquals(1, emails.length);
    assertEquals(subject, emails[0].getSubject());

    Object o = emails[0].getContent();
    assertTrue(o instanceof MimeMultipart);
    MimeMultipart mp = (MimeMultipart) o;
    assertEquals(2, mp.getCount());
    BodyPart bp;
    bp = mp.getBodyPart(0);
    assertEquals(body, GreenMailUtil.getBody(bp).trim());

    bp = mp.getBodyPart(1);
    assertEquals("AAEC", GreenMailUtil.getBody(bp).trim());

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    GreenMailUtil.copyStream(bp.getInputStream(), bout);
    byte[] gif = bout.toByteArray();
    for (int i = 0; i < gif.length; i++) {
        assertEquals(i, gif[i]);
    }
}
 
Example 10
Source File: SmtpServerTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testSmtpServerReceiveWithSetup() throws Throwable {
    assertEquals(0, greenMail.getReceivedMessages().length);

    String subject = GreenMailUtil.random();
    String body = GreenMailUtil.random();
    GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject, body);
    greenMail.waitForIncomingEmail(1500, 1);
    MimeMessage[] emails = greenMail.getReceivedMessages();
    assertEquals(1, emails.length);
    assertEquals(subject, emails[0].getSubject());
    assertEquals(body, GreenMailUtil.getBody(emails[0]).trim());
}
 
Example 11
Source File: ExampleSendNoRuleAdvTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testSend() throws MessagingException, IOException {
    GreenMail greenMail = new GreenMail(ServerSetupTest.SMTP_IMAP);
    try {
        greenMail.start();

        //Use random content to avoid potential residual lingering problems
        final String subject = GreenMailUtil.random();
        final String body = GreenMailUtil.random();

        sendTestMails(subject, body); // Place your sending code here

        //wait for max 5s for 1 email to arrive
        //waitForIncomingEmail() is useful if you're sending stuff asynchronously in a separate thread
        assertTrue(greenMail.waitForIncomingEmail(5000, 2));

        //Retrieve using GreenMail API
        Message[] messages = greenMail.getReceivedMessages();
        assertEquals(2, messages.length);

        // Simple message
        assertEquals(subject, messages[0].getSubject());
        assertEquals(body, GreenMailUtil.getBody(messages[0]).trim());

        //if you send content as a 2 part multipart...
        assertTrue(messages[1].getContent() instanceof MimeMultipart);
        MimeMultipart mp = (MimeMultipart) messages[1].getContent();
        assertEquals(2, mp.getCount());
        assertEquals("body1", GreenMailUtil.getBody(mp.getBodyPart(0)).trim());
        assertEquals("body2", GreenMailUtil.getBody(mp.getBodyPart(1)).trim());
    } finally {
        greenMail.stop();
    }
}
 
Example 12
Source File: EmailReaderTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testPopLongWait() throws Exception {
  String subject = GreenMailUtil.random();
  String body = GreenMailUtil.random();

  String subject2 = GreenMailUtil.random();
  String body2 = GreenMailUtil.random();

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject, body);
  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject2, body2);

  BaleenCollectionReader bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL, "pop3",
          EmailReader.PARAM_WAIT, 15,
          EmailReader.PARAM_SERVER, greenMail.getPop3().getBindTo(),
          EmailReader.PARAM_PORT, greenMail.getPop3().getPort(),
          EmailReader.PARAM_USER, "[email protected]",
          EmailReader.PARAM_PASS, "password",
          EmailReader.PARAM_PROCESS, "content");

  bcr.initialize();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  jCas.reset();

  Thread.sleep(20000);

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  jCas.reset();

  assertFalse(bcr.doHasNext());

  bcr.close();
}
 
Example 13
Source File: Pop3ServerTest.java    From greenmail with Apache License 2.0 5 votes vote down vote up
@Test
public void testRetrieveMultipart() throws Exception {
    assertNotNull(greenMail.getPop3());

    String subject = GreenMailUtil.random();
    String body = GreenMailUtil.random();
    String to = "[email protected]";
    GreenMailUtil.sendAttachmentEmail(to, "[email protected]", subject, body, new byte[]{0, 1, 2}, "image/gif", "testimage_filename", "testimage_description", ServerSetupTest.SMTP);
    greenMail.waitForIncomingEmail(5000, 1);

    try (Retriever retriever = new Retriever(greenMail.getPop3())) {
        Message[] messages = retriever.getMessages(to);

        Object o = messages[0].getContent();
        assertTrue(o instanceof MimeMultipart);
        MimeMultipart mp = (MimeMultipart) o;
        assertEquals(2, mp.getCount());
        BodyPart bp;
        bp = mp.getBodyPart(0);
        assertEquals(body, GreenMailUtil.getBody(bp).trim());

        bp = mp.getBodyPart(1);
        assertEquals("AAEC", GreenMailUtil.getBody(bp).trim());

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        GreenMailUtil.copyStream(bp.getInputStream(), bout);
        byte[] gif = bout.toByteArray();
        for (int i = 0; i < gif.length; i++) {
            assertEquals(i, gif[i]);
        }
    }
}
 
Example 14
Source File: EmailReaderTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testImapBoth() throws Exception {
  File folder = Files.createTempDir();

  String subject1 = GreenMailUtil.random();
  String body1 = GreenMailUtil.random();

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject1, body1);

  String subject2 = GreenMailUtil.random();
  String body2 = GreenMailUtil.random();

  GreenMailUtil.sendAttachmentEmail(
      "[email protected]",
      "[email protected]",
      subject2,
      body2,
      IOUtils.toByteArray(getClass().getResourceAsStream("lineReader.txt")),
      "text/plain",
      "lineReader.txt",
      "Test File",
      ServerSetupTest.SMTP);

  BaleenCollectionReader bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL,
          "imap",
          EmailReader.PARAM_WAIT,
          5,
          EmailReader.PARAM_SERVER,
          greenMail.getImap().getBindTo(),
          EmailReader.PARAM_PORT,
          greenMail.getImap().getPort(),
          EmailReader.PARAM_USER,
          "[email protected]",
          EmailReader.PARAM_PASS,
          "password",
          EmailReader.PARAM_PROCESS,
          "both",
          EmailReader.PARAM_FOLDER,
          folder.getPath());

  bcr.initialize();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body1));

  jCas.reset();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body2));

  jCas.reset();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(FIRST_LINE));
  assertEquals(1, folder.list().length);

  jCas.reset();

  assertFalse(bcr.doHasNext());

  bcr.close();

  FileUtils.deleteDirectory(folder);
}
 
Example 15
Source File: EmailReaderTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testImapDeleteMessages() throws Exception {
  String subject1 = GreenMailUtil.random();
  String body1 = GreenMailUtil.random();

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject1, body1);

  String subject2 = GreenMailUtil.random();
  String body2 = GreenMailUtil.random();

  GreenMailUtil.sendAttachmentEmail(
      "[email protected]",
      "[email protected]",
      subject2,
      body2,
      IOUtils.toByteArray(getClass().getResourceAsStream("lineReader.txt")),
      "text/plain",
      "lineReader.txt",
      "Test File",
      ServerSetupTest.SMTP);

  BaleenCollectionReader bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL,
          "imap",
          EmailReader.PARAM_WAIT,
          5,
          EmailReader.PARAM_SERVER,
          greenMail.getImap().getBindTo(),
          EmailReader.PARAM_PORT,
          greenMail.getImap().getPort(),
          EmailReader.PARAM_USER,
          "[email protected]",
          EmailReader.PARAM_PASS,
          "password",
          EmailReader.PARAM_PROCESS,
          "content",
          EmailReader.PARAM_DELETE_EMAIL,
          true);

  bcr.initialize();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body1));

  jCas.reset();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body2));

  jCas.reset();

  assertFalse(bcr.doHasNext());

  bcr.close();

  // Check that there are no messages on the server
  bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL,
          "imap",
          EmailReader.PARAM_WAIT,
          5,
          EmailReader.PARAM_SERVER,
          greenMail.getImap().getBindTo(),
          EmailReader.PARAM_PORT,
          greenMail.getImap().getPort(),
          EmailReader.PARAM_USER,
          "[email protected]",
          EmailReader.PARAM_PASS,
          "password");

  bcr.initialize();
  assertFalse(bcr.doHasNext());
  bcr.close();
}
 
Example 16
Source File: EmailReaderTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testImapDeleteAttachments() throws Exception {
  File folder = Files.createTempDir();

  String subject1 = GreenMailUtil.random();
  String body1 = GreenMailUtil.random();

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject1, body1);

  String subject2 = GreenMailUtil.random();
  String body2 = GreenMailUtil.random();

  GreenMailUtil.sendAttachmentEmail(
      "[email protected]",
      "[email protected]",
      subject2,
      body2,
      IOUtils.toByteArray(getClass().getResourceAsStream("lineReader.txt")),
      "text/plain",
      "lineReader.txt",
      "Test File",
      ServerSetupTest.SMTP);

  BaleenCollectionReader bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL,
          "imap",
          EmailReader.PARAM_WAIT,
          5,
          EmailReader.PARAM_SERVER,
          greenMail.getImap().getBindTo(),
          EmailReader.PARAM_PORT,
          greenMail.getImap().getPort(),
          EmailReader.PARAM_USER,
          "[email protected]",
          EmailReader.PARAM_PASS,
          "password",
          EmailReader.PARAM_PROCESS,
          "attachments",
          EmailReader.PARAM_FOLDER,
          folder.getPath(),
          EmailReader.PARAM_DELETE_ATTACHMENT,
          true);

  bcr.initialize();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(FIRST_LINE));
  assertEquals(0, folder.list().length);

  jCas.reset();

  assertFalse(bcr.doHasNext());

  bcr.close();

  FileUtils.deleteDirectory(folder);
}
 
Example 17
Source File: EmailReaderTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testImapAttachments() throws Exception {
  File folder = Files.createTempDir();

  String subject1 = GreenMailUtil.random();
  String body1 = GreenMailUtil.random();

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject1, body1);

  String subject2 = GreenMailUtil.random();
  String body2 = GreenMailUtil.random();

  GreenMailUtil.sendAttachmentEmail(
      "[email protected]",
      "[email protected]",
      subject2,
      body2,
      IOUtils.toByteArray(getClass().getResourceAsStream("lineReader.txt")),
      "text/plain",
      "lineReader.txt",
      "Test File",
      ServerSetupTest.SMTP);

  BaleenCollectionReader bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL,
          "imap",
          EmailReader.PARAM_WAIT,
          5,
          EmailReader.PARAM_SERVER,
          greenMail.getImap().getBindTo(),
          EmailReader.PARAM_PORT,
          greenMail.getImap().getPort(),
          EmailReader.PARAM_USER,
          "[email protected]",
          EmailReader.PARAM_PASS,
          "password",
          EmailReader.PARAM_PROCESS,
          "attachments",
          EmailReader.PARAM_FOLDER,
          folder.getPath());

  bcr.initialize();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(FIRST_LINE));
  assertEquals(1, folder.list().length);

  jCas.reset();

  assertFalse(bcr.doHasNext());

  bcr.close();

  FileUtils.deleteDirectory(folder);
}
 
Example 18
Source File: EmailReaderTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testPopBadProcessConfig() throws Exception {
  File folder = Files.createTempDir();

  String subject1 = GreenMailUtil.random();
  String body1 = GreenMailUtil.random();

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject1, body1);

  String subject2 = GreenMailUtil.random();
  String body2 = GreenMailUtil.random();

  GreenMailUtil.sendAttachmentEmail(
      "[email protected]",
      "[email protected]",
      subject2,
      body2,
      IOUtils.toByteArray(getClass().getResourceAsStream("lineReader.txt")),
      "text/plain",
      "lineReader.txt",
      "Test File",
      ServerSetupTest.SMTP);

  BaleenCollectionReader bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL,
          "pop3",
          EmailReader.PARAM_WAIT,
          5,
          EmailReader.PARAM_SERVER,
          greenMail.getPop3().getBindTo(),
          EmailReader.PARAM_PORT,
          greenMail.getPop3().getPort(),
          EmailReader.PARAM_USER,
          "[email protected]",
          EmailReader.PARAM_PASS,
          "password",
          EmailReader.PARAM_PROCESS,
          "NotARealProcess",
          EmailReader.PARAM_FOLDER,
          folder.getPath());

  bcr.initialize();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body1));

  jCas.reset();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body2));

  jCas.reset();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(FIRST_LINE));
  assertEquals(1, folder.list().length);

  jCas.reset();

  assertFalse(bcr.doHasNext());

  bcr.close();

  FileUtils.deleteDirectory(folder);
}
 
Example 19
Source File: EmailReaderTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testPopDeleteMessages() throws Exception {
  String subject1 = GreenMailUtil.random();
  String body1 = GreenMailUtil.random();

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject1, body1);

  String subject2 = GreenMailUtil.random();
  String body2 = GreenMailUtil.random();

  GreenMailUtil.sendAttachmentEmail(
      "[email protected]",
      "[email protected]",
      subject2,
      body2,
      IOUtils.toByteArray(getClass().getResourceAsStream("lineReader.txt")),
      "text/plain",
      "lineReader.txt",
      "Test File",
      ServerSetupTest.SMTP);

  BaleenCollectionReader bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL,
          "pop3",
          EmailReader.PARAM_WAIT,
          5,
          EmailReader.PARAM_SERVER,
          greenMail.getPop3().getBindTo(),
          EmailReader.PARAM_PORT,
          greenMail.getPop3().getPort(),
          EmailReader.PARAM_USER,
          "[email protected]",
          EmailReader.PARAM_PASS,
          "password",
          EmailReader.PARAM_PROCESS,
          "content",
          EmailReader.PARAM_DELETE_EMAIL,
          true);

  bcr.initialize();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body1));

  jCas.reset();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body2));

  jCas.reset();

  assertFalse(bcr.doHasNext());

  bcr.close();

  // Check that there are no messages on the server
  bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL,
          "pop3",
          EmailReader.PARAM_WAIT,
          5,
          EmailReader.PARAM_SERVER,
          greenMail.getPop3().getBindTo(),
          EmailReader.PARAM_PORT,
          greenMail.getPop3().getPort(),
          EmailReader.PARAM_USER,
          "[email protected]",
          EmailReader.PARAM_PASS,
          "password");

  bcr.initialize();
  assertFalse(bcr.doHasNext());
  bcr.close();
}
 
Example 20
Source File: EmailReaderTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testPopBoth() throws Exception {
  File folder = Files.createTempDir();

  String subject1 = GreenMailUtil.random();
  String body1 = GreenMailUtil.random();

  GreenMailUtil.sendTextEmailTest("[email protected]", "[email protected]", subject1, body1);

  String subject2 = GreenMailUtil.random();
  String body2 = GreenMailUtil.random();

  GreenMailUtil.sendAttachmentEmail(
      "[email protected]",
      "[email protected]",
      subject2,
      body2,
      IOUtils.toByteArray(getClass().getResourceAsStream("lineReader.txt")),
      "text/plain",
      "lineReader.txt",
      "Test File",
      ServerSetupTest.SMTP);

  BaleenCollectionReader bcr =
      getCollectionReader(
          EmailReader.PARAM_PROTOCOL,
          "pop3",
          EmailReader.PARAM_WAIT,
          5,
          EmailReader.PARAM_SERVER,
          greenMail.getPop3().getBindTo(),
          EmailReader.PARAM_PORT,
          greenMail.getPop3().getPort(),
          EmailReader.PARAM_USER,
          "[email protected]",
          EmailReader.PARAM_PASS,
          "password",
          EmailReader.PARAM_PROCESS,
          "both",
          EmailReader.PARAM_FOLDER,
          folder.getPath());

  bcr.initialize();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body1));

  jCas.reset();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(body2));

  jCas.reset();

  assertTrue(bcr.doHasNext());
  bcr.getNext(jCas);

  assertTrue(jCas.getDocumentText().startsWith(FIRST_LINE));
  assertEquals(1, folder.list().length);

  jCas.reset();

  assertFalse(bcr.doHasNext());

  bcr.close();

  FileUtils.deleteDirectory(folder);
}