/*
 * Copyright (c) 2009, 2023 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0, which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the
 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
 * version 2 with the GNU Classpath Exception, which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 */

package org.xbib.net.mail.test.smtp;

import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.Session;
import jakarta.mail.Transport;
import jakarta.mail.event.ConnectionAdapter;
import jakarta.mail.event.ConnectionEvent;
import jakarta.mail.internet.MimeMessage;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.xbib.net.mail.test.test.TestServer;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import static org.junit.jupiter.api.Assertions.fail;

/**
 * Test that the connection is closed when an IOException is detected.
 */
@Timeout(5)
public final class SMTPIOExceptionTest {

    private boolean closed = false;

    private static final int TIMEOUT = 200;    // I/O timeout, in millis

    @Test
    public void test() throws Exception {
        TestServer server = null;
        final CountDownLatch closedLatch = new CountDownLatch(1);
        try {
            SMTPHandler handler = new SMTPHandler() {
                @Override
                public void rcpt(String line) throws IOException {
                    try {
                        // delay long enough to cause timeout
                        Thread.sleep(2 * TIMEOUT);
                    } catch (Exception ex) {
                    }
                    super.rcpt(line);
                }
            };
            server = new TestServer(handler);
            server.start();

            final Properties properties = new Properties();
            properties.setProperty("mail.smtp.host", "localhost");
            properties.setProperty("mail.smtp.port", String.valueOf(server.getPort()));
            properties.setProperty("mail.smtp.timeout", "" + TIMEOUT);
            final Session session = Session.getInstance(properties);
            //session.setDebug(true);

            final Transport t = session.getTransport("smtp");
            /*
             * Use a listener to detect the connection being closed
             * because if we called isConnected() and the connection
             * wasn't already closed, it will issue a command that
             * might detect that the connection was closed, even
             * though it wasn't closed already.
             */
            t.addConnectionListener(new ConnectionAdapter() {
                @Override
                public void closed(ConnectionEvent e) {
                    closedLatch.countDown();
                }
            });
            try {
                MimeMessage msg = new MimeMessage(session);
                msg.setRecipients(Message.RecipientType.TO, "joe@example.com");
                msg.setSubject("test");
                msg.setText("test");
                t.connect();
                t.sendMessage(msg, msg.getAllRecipients());
            } catch (MessagingException ex) {
                // expect an exception from sendMessage
                closedLatch.await();    // wait for the listener to run
                // if we get here, the listener was called - SUCCESS
            } finally {
                t.close();
            }
        } catch (final Exception e) {
            e.printStackTrace();
            fail(e.getMessage());
        } finally {
            if (server != null) {
                server.quit();
                server.interrupt();
                // wait for handler to exit
                server.join();
            }
        }
    }
}
