<commit_before>package com.dumbster.pop;

import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import java.util.Properties;

import com.dumbster.smtp.FixedSizeMailStore;
import com.dumbster.smtp.MailMessage;
import com.dumbster.smtp.MailMessageImpl;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * Unit tests for the POP API to dumbster.
 */
public class POPServerTest {
    private static final int PORT = 1082;

    private POPServer _server;
    
    @Before
    public void setup() {
        _server = POPServerFactory.startServer(PORT);
        _server.setMailStore(new FixedSizeMailStore(10));
    }

    @After
    public void shutdown() {
        _server.stop();
    }

    @Test
    public void loginAndQuit() throws Exception {
        Store store = connect();
    
        Folder inbox = store.getFolder("INBOX");
        if (inbox == null) {
          Assert.fail("POP should have an inbox");
        }
        inbox.open(Folder.READ_ONLY);
    
        inbox.close(false);
        store.close();

    }

    @Test
    public void retrieveAndDelete() throws Exception {
        _server.getMailstore().addMessage(createMessage("<EMAIL>",
                                                        "<EMAIL>",
                                                        "test message",
                                                        "Message body with multiple\r\nlines of text.\r\n.6 percent\r\n"));
        Store store = connect();
        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_WRITE);
        int messageCount = inbox.getMessageCount();
        Message[] messages = inbox.getMessages();
        Assert.assertEquals(messageCount, messages.length);
        Assert.assertEquals(1, messageCount);
        
        messages[0].getContent();
        messages[0].setFlag(Flags.Flag.DELETED, true);
        inbox.close(true);
        store.close();
        Assert.assertEquals(0, _server.getMailstore().getEmailCount());
    }
    
    @Test
    public void validateByteStuffing() throws Exception {
        _server.getMailstore().addMessage(createMessage("<EMAIL>",
                                                        "<EMAIL>",
                                                        "test message",
                                                        "Message body with multiple\r\nlines of text.\r\n.6 percent\r\n"));
        Store store = connect();
        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_WRITE);
        int messageCount = inbox.getMessageCount();
        Message[] messages = inbox.getMessages();
        Assert.assertEquals(messageCount, messages.length);
        Assert.assertEquals(1, messageCount);
        
        Object body = messages[0].getContent();
        String msgStr = body.toString();
        int index = msgStr.indexOf("\r\n.6 percent");
        if (index < 0) {
            Assert.fail("incorrect stuffing of a period at the beginning of a line");
        }
        inbox.close(true);
        store.close();
        _server.getMailstore().clearMessages();
    }

    @Test
    public void testUIDL() throws Exception {
        Assert.fail("Not implemented");
    }

    @Test
    public void testList() throws Exception {
        Assert.fail("Not implemented");
    }
    
    private Store connect() throws MessagingException {
        Properties props = new Properties();

        String host = "localhost";
        String username = "userName";
        String password = "<PASSWORD>";
        String provider = "pop3";<commit_msg>Add unit tests for LIST and UIDL<commit_after>package com.dumbster.pop;

import javax.mail.*;
import java.util.Properties;

import com.dumbster.smtp.FixedSizeMailStore;
import com.dumbster.smtp.MailMessage;
import com.dumbster.smtp.MailMessageImpl;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * Unit tests for the POP API to dumbster.
 */
public class POPServerTest {
    private static final int PORT = 1082;

    private POPServer _server;
    
    @Before
    public void setup() {
        _server = POPServerFactory.startServer(PORT);
        _server.setMailStore(new FixedSizeMailStore(10));
    }

    @After
    public void shutdown() {
        _server.stop();
    }

    @Test
    public void loginAndQuit() throws Exception {
        Store store = connect();
    
        Folder inbox = store.getFolder("INBOX");
        if (inbox == null) {
          Assert.fail("POP should have an inbox");
        }
        inbox.open(Folder.READ_ONLY);
    
        inbox.close(false);
        store.close();

    }

    @Test
    public void retrieveAndDelete() throws Exception {
        _server.getMailstore().addMessage(createMessage("<EMAIL>",
                                                        "<EMAIL>",
                                                        "test message",
                                                        "Message body with multiple\r\nlines of text.\r\n.6 percent\r\n"));
        Store store = connect();
        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_WRITE);
        int messageCount = inbox.getMessageCount();
        Message[] messages = inbox.getMessages();
        Assert.assertEquals(messageCount, messages.length);
        Assert.assertEquals(1, messageCount);
        
        messages[0].getContent();
        messages[0].setFlag(Flags.Flag.DELETED, true);
        inbox.close(true);
        store.close();
        Assert.assertEquals(0, _server.getMailstore().getEmailCount());
    }
    
    @Test
    public void validateByteStuffing() throws Exception {
        _server.getMailstore().addMessage(createMessage("<EMAIL>",
                                                        "<EMAIL>",
                                                        "test message",
                                                        "Message body with multiple\r\nlines of text.\r\n.6 percent\r\n"));
        Store store = connect();
        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_WRITE);
        int messageCount = inbox.getMessageCount();
        Message[] messages = inbox.getMessages();
        Assert.assertEquals(messageCount, messages.length);
        Assert.assertEquals(1, messageCount);
        
        Object body = messages[0].getContent();
        String msgStr = body.toString();
        int index = msgStr.indexOf("\r\n.6 percent");
        if (index < 0) {
            Assert.fail("incorrect stuffing of a period at the beginning of a line");
        }
        inbox.close(true);
        store.close();
        _server.getMailstore().clearMessages();
    }

    @Test
    public void testUIDLSingle() throws Exception {
        _server.getMailstore().addMessage(createMessage("<EMAIL>",
                "<EMAIL>",
                "test message",
                "Message body with multiple\r\nlines of text.\r\n.6 percent\r\n"));
        _server.getMailstore().addMessage(createMessage("<EMAIL>",
                "<EMAIL>",
                "second test message",
                "More email!\r\n"));
        Store store = connect();
        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_WRITE);

        Message[] messages = inbox.getMessages();
        com.sun.mail.pop3.POP3Folder pf =
                    (com.sun.mail.pop3.POP3Folder)inbox;
        pf.getUID(messages[0]);

//        FetchProfile fp = new FetchProfile();
//        fp.add(UIDFolder.FetchProfileItem.UID);
//        pf.fetch(pf.getMessages(), fp);

        inbox.close(true);
        store.close();
        _server.getMailstore().clearMessages();
    }

    @Test
    public void testUIDLAll() throws Exception {
        _server.getMailstore().addMessage(createMessage("<EMAIL>",
                "<EMAIL>",
                "test message",
                "Message body with multiple\r\nlines of text.\r\n.6 percent\r\n"));
        _server.getMailstore().addMessage(createMessage("<EMAIL>",
                "<EMAIL>",
                "second test message",
                "More email!\r\n"));
        Store store = connect();
        Folder inbox = store.getFolder("INBOX");

        com.sun.mail.pop3.POP3Folder pf =
                (com.sun.mail.pop3.POP3Folder)inbox;

        pf.open(Folder.READ_WRITE);
        FetchProfile fp = new FetchProfile();
        fp.add(UIDFolder.FetchProfileItem.UID);
        pf.fetch(pf.getMessages(), fp);

        inbox.close(true);
        store.close();
        _server.getMailstore().clearMessages();
    }

    @Test
    public void testList() throws Exception {
        _server.getMailstore().addMessage(createMessage("<EMAIL>",
                "<EMAIL>",
                "test message",
                "Message body with multiple\r\nlines of text.\r\n.6 percent\r\n"));
        _server.getMailstore().addMessage(createMessage("<EMAIL>",
                "<EMAIL>",
                "second test message",
                "More email!\r\n"));
        Store store = connect();
        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_WRITE);
        com.sun.mail.pop3.POP3Folder pf = (com.sun.mail.pop3.POP3Folder)inbox;

        int [] sizes = pf.getSizes();

        inbox.close(true);
        store.close();
        _server.getMailstore().clearMessages();
    }
    
    private Store connect() throws MessagingException {
        Properties props = new Properties();

        String host = "localhost";
        String username = "userName";
        String password = "<PASSWORD>";
        String provider = "pop3";
{'fraction_non_alphanumeric': 0.11923037560126906, 'fraction_numerical': 0.005117183502200389, 'mean_word_length': 1.925748502994012, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7140784', 'n_tokens_mistral': 2654, 'n_tokens_neox': 2556, 'n_words': 561}