<commit_before>package me.normanmaurer.niosmtp;

import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.ExecutionException;


import me.normanmaurer.niosmtp.impl.UnpooledSMTPClient;
import me.normanmaurer.niosmtp.impl.internal.SMTPClientConfigImpl;

import org.junit.Test;
import org.subethamail.smtp.MessageContext;
import org.subethamail.smtp.MessageHandler;
import org.subethamail.smtp.MessageHandlerFactory;
import org.subethamail.smtp.RejectException;
import org.subethamail.smtp.TooMuchDataException;
import org.subethamail.smtp.server.SMTPServer;

public class SMTPClientTest {
    

    @Test
    public void testRejectMailFrom() throws InterruptedException, ExecutionException {
        int port = 6028;

        SMTPServer smtpServer = new SMTPServer(new TestHandlerFactory() {

            @Override
            public void from(String sender) throws RejectException {
                throw new RejectException("Sender " + sender + " rejected");
            }
            
        });
        smtpServer.setPort(port);
        smtpServer.start();

       
        UnpooledSMTPClient c = new UnpooledSMTPClient();

        try {
            SMTPClientConfigImpl conf = new SMTPClientConfigImpl();
            conf.setConnectionTimeout(4);
            conf.setResponseTimeout(5);
            System.out.println("delivering...");
            SMTPClientFuture future = c.deliver(new InetSocketAddress(port), "<EMAIL>", Arrays.asList(new String[] {"<EMAIL>", "<EMAIL>"}), new ByteArrayInputStream("msg".getBytes()), conf);
            DeliveryResult dr = future.get();
            assertTrue(dr.isSuccess());
            assertNull(dr.getException());
            Iterator<DeliveryRecipientStatus> it = dr.getRecipientStatus();
            DeliveryRecipientStatus status = it.next();
            assertEquals(DeliveryRecipientStatus.Status.PermanentError, status.getStatus());
            assertEquals(554, status.getResponse().getCode());

            status = it.next();
            assertEquals(DeliveryRecipientStatus.Status.PermanentError, status.getStatus());
            assertEquals(554, status.getResponse().getCode());
            
            assertFalse(it.hasNext());
        } finally {
            smtpServer.stop();
            c.destroy();
        }
        
    }


    @Test
    public void testRejectAllRecipients() throws InterruptedException, ExecutionException {
        int port = 6028;

        SMTPServer smtpServer = new SMTPServer(new TestHandlerFactory() {

            @Override
            public void recipient(String rcpt) throws RejectException {
                throw new RejectException();
            }


            
        });
        smtpServer.setPort(port);
        smtpServer.start();

       
        UnpooledSMTPClient c = new UnpooledSMTPClient();

        try {
            SMTPClientConfigImpl conf = new SMTPClientConfigImpl();
            conf.setConnectionTimeout(4);
            conf.setResponseTimeout(5);
            System.out.println("delivering...");
            SMTPClientFuture future = c.deliver(new InetSocketAddress(port), "<EMAIL>", Arrays.asList(new String[] {"<EMAIL>", "<EMAIL>"}), new ByteArrayInputStream("msg".getBytes()), conf);
            DeliveryResult dr = future.get();
            assertTrue(dr.isSuccess());
            assertNull(dr.getException());
            Iterator<DeliveryRecipientStatus> it = dr.getRecipientStatus();
            DeliveryRecipientStatus status = it.next();
            assertEquals(DeliveryRecipientStatus.Status.PermanentError, status.getStatus());
            assertEquals(554, status.getResponse().getCode());

            status = it.next();
            assertEquals(DeliveryRecipientStatus.Status.PermanentError, status.getStatus());
            assertEquals(554, status.getResponse().getCode());
            
            assertFalse(it.hasNext());
        } finally {
            smtpServer.stop();
            c.destroy();
        }
        
    }
    
    
    @Test
    public void testConnectionRefused() throws InterruptedException, ExecutionException {
        UnpooledSMTPClient c = new UnpooledSMTPClient();
        SMTPClientConfigImpl conf = new SMTPClientConfigImpl();
        conf.setConnectionTimeout(4);
        conf.setResponseTimeout(5);
        SMTPClientFuture future = c.deliver(new InetSocketAddress(11111), "<EMAIL>", Arrays.asList(new String[] { "<EMAIL>" }), new ByteArrayInputStream("msg".getBytes()), conf);
        try {
            DeliveryResult dr = future.get();
            assertFalse(dr.isSuccess());
            assertNull(dr.getRecipientStatus());
            assertEquals(SMTPConnectionException.class, dr.getException().getClass());
        } finally {
            c.destroy();
        }
    }
    
    protected class TestHandlerFactory implements MessageHandlerFactory, MessageHandler {
<commit_msg>More unit tests<commit_after>package me.normanmaurer.niosmtp;

import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.ExecutionException;


import me.normanmaurer.niosmtp.impl.UnpooledSMTPClient;
import me.normanmaurer.niosmtp.impl.internal.SMTPClientConfigImpl;

import org.junit.Test;
import org.subethamail.smtp.MessageContext;
import org.subethamail.smtp.MessageHandler;
import org.subethamail.smtp.MessageHandlerFactory;
import org.subethamail.smtp.RejectException;
import org.subethamail.smtp.TooMuchDataException;
import org.subethamail.smtp.server.SMTPServer;

public class SMTPClientTest {
    

    @Test
    public void testRejectMailFrom() throws InterruptedException, ExecutionException {
        int port = 6028;

        SMTPServer smtpServer = new SMTPServer(new TestHandlerFactory() {

            @Override
            public void from(String sender) throws RejectException {
                throw new RejectException("Sender " + sender + " rejected");
            }
            
        });
        smtpServer.setPort(port);
        smtpServer.start();

       
        UnpooledSMTPClient c = new UnpooledSMTPClient();

        try {
            SMTPClientConfigImpl conf = new SMTPClientConfigImpl();
            conf.setConnectionTimeout(4);
            conf.setResponseTimeout(5);
            System.out.println("delivering...");
            SMTPClientFuture future = c.deliver(new InetSocketAddress(port), "<EMAIL>", Arrays.asList(new String[] {"<EMAIL>", "<EMAIL>"}), new ByteArrayInputStream("msg".getBytes()), conf);
            DeliveryResult dr = future.get();
            assertTrue(dr.isSuccess());
            assertNull(dr.getException());
            Iterator<DeliveryRecipientStatus> it = dr.getRecipientStatus();
            DeliveryRecipientStatus status = it.next();
            assertEquals(DeliveryRecipientStatus.Status.PermanentError, status.getStatus());
            assertEquals(554, status.getResponse().getCode());

            status = it.next();
            assertEquals(DeliveryRecipientStatus.Status.PermanentError, status.getStatus());
            assertEquals(554, status.getResponse().getCode());
            
            assertFalse(it.hasNext());
        } finally {
            smtpServer.stop();
            c.destroy();
        }
        
    }


    @Test
    public void testRejectAllRecipients() throws InterruptedException, ExecutionException {
        int port = 6028;

        SMTPServer smtpServer = new SMTPServer(new TestHandlerFactory() {

            @Override
            public void recipient(String rcpt) throws RejectException {
                throw new RejectException();
            }


            
        });
        smtpServer.setPort(port);
        smtpServer.start();

       
        UnpooledSMTPClient c = new UnpooledSMTPClient();

        try {
            SMTPClientConfigImpl conf = new SMTPClientConfigImpl();
            conf.setConnectionTimeout(4);
            conf.setResponseTimeout(5);
            System.out.println("delivering...");
            SMTPClientFuture future = c.deliver(new InetSocketAddress(port), "<EMAIL>", Arrays.asList(new String[] {"<EMAIL>", "<EMAIL>"}), new ByteArrayInputStream("msg".getBytes()), conf);
            DeliveryResult dr = future.get();
            assertTrue(dr.isSuccess());
            assertNull(dr.getException());
            Iterator<DeliveryRecipientStatus> it = dr.getRecipientStatus();
            DeliveryRecipientStatus status = it.next();
            assertEquals(DeliveryRecipientStatus.Status.PermanentError, status.getStatus());
            assertEquals(554, status.getResponse().getCode());

            status = it.next();
            assertEquals(DeliveryRecipientStatus.Status.PermanentError, status.getStatus());
            assertEquals(554, status.getResponse().getCode());
            
            assertFalse(it.hasNext());
        } finally {
            smtpServer.stop();
            c.destroy();
        }
        
    }
    
    
    @Test
    public void testRejectData() throws InterruptedException, ExecutionException {
        int port = 6028;

        SMTPServer smtpServer = new SMTPServer(new TestHandlerFactory() {

            @Override
            public void data(InputStream arg0) throws RejectException, TooMuchDataException, IOException {
                throw new RejectException();
            }


            
        });
        smtpServer.setPort(port);
        smtpServer.start();

       
        UnpooledSMTPClient c = new UnpooledSMTPClient();

        try {
            SMTPClientConfigImpl conf = new SMTPClientConfigImpl();
            conf.setConnectionTimeout(4);
            conf.setResponseTimeout(5);
            System.out.println("delivering...");
            SMTPClientFuture future = c.deliver(new InetSocketAddress(port), "<EMAIL>", Arrays.asList(new String[] {"<EMAIL>", "<EMAIL>"}), new ByteArrayInputStream("msg".getBytes()), conf);
            DeliveryResult dr = future.get();
            assertTrue(dr.isSuccess());
            assertNull(dr.getException());
            Iterator<DeliveryRecipientStatus> it = dr.getRecipientStatus();
            DeliveryRecipientStatus status = it.next();
            assertEquals(DeliveryRecipientStatus.Status.PermanentError, status.getStatus());
            assertEquals(554, status.getResponse().getCode());

            status = it.next();
            assertEquals(DeliveryRecipientStatus.Status.PermanentError, status.getStatus());
            assertEquals(554, status.getResponse().getCode());
            
            assertFalse(it.hasNext());
        } finally {
            smtpServer.stop();
            c.destroy();
        }
        
    }
    
    
    @Test
    public void testRejectOneRecipient() throws InterruptedException, ExecutionException {
        int port = 6028;

        SMTPServer smtpServer = new SMTPServer(new TestHandlerFactory() {

            @Override
            public void recipient(String rcpt) throws RejectException {
                if (rcpt.equals("<EMAIL>"))  {
                    throw new RejectException();
                }
            }


            
        });
        smtpServer.setPort(port);
        smtpServer.start();

       
        UnpooledSMTPClient c = new UnpooledSMTPClient();

        try {
            SMTPClientConfigImpl conf = new SMTPClientConfigImpl();
            conf.setConnectionTimeout(4);
            conf.setResponseTimeout(5);
            System.out.println("delivering...");
            SMTPClientFuture future = c.deliver(new InetSocketAddress(port), "<EMAIL>", Arrays.asList(new String[] {"<EMAIL>", "<EMAIL>", "<EMAIL>"}), new ByteArrayInputStream("msg".getBytes()), conf);
            DeliveryResult dr = future.get();
            assertTrue(dr.isSuccess());
            assertNull(dr.getException());
            Iterator<DeliveryRecipientStatus> it = dr.getRecipientStatus();
            DeliveryRecipientStatus status = it.next();
            assertEquals(DeliveryRecipientStatus.Status.Ok, status.getStatus());
            assertEquals(250, status.getResponse().getCode());
            assertEquals("<EMAIL>", status.getAddress());
            
            status = it.next();
            assertEquals(DeliveryRecipientStatus.Status.PermanentError, status.getStatus());
            assertEquals(554, status.getResponse().getCode());
            assertEquals("<EMAIL>", status.getAddress());

            status = it.next();
            assertEquals(DeliveryRecipientStatus.Status.Ok, status.getStatus());
            assertEquals(250, status.getResponse().getCode());
            assertEquals("<EMAIL>", status.getAddress());
            
            assertFalse(it.hasNext());
        } finally {
            smtpServer.stop();
            c.destroy();
        }
        
    }
    
    
    @Test
    public void testConnectionRefused() throws InterruptedException, ExecutionException {
        UnpooledSMTPClient c = new UnpooledSMTPClient();
        SMTPClientConfigImpl conf = new SMTPClientConfigImpl();
        conf.setConnectionTimeout(4);
        conf.setResponseTimeout(5);
        SMTPClientFuture future = c.deliver(new InetSocketAddress(11111), "<EMAIL>", Arrays.asList(new String[] { "<EMAIL>" }), new ByteArrayInputStream("msg".getBytes()), conf);
        try {
            DeliveryResult dr = future.get();
            assertFalse(dr.isSuccess());
            assertNull(dr.getRecipientStatus());
            assertEquals(SMTPConnectionException.class, dr.getException().getClass());
        } finally {
            c.destroy();
        }
    }
    
    protected class TestHandlerFactory implements MessageHandlerFactory, MessageHandler {

{'fraction_non_alphanumeric': 0.11033640023001726, 'fraction_numerical': 0.006469235192639448, 'mean_word_length': 2.4704415066101273, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3016650', 'n_tokens_mistral': 3751, 'n_tokens_neox': 3598, 'n_words': 647}