/*
 * Copyright (c) 2015, 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.util;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeBodyPart;
import jakarta.mail.internet.MimeUtility;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * Test "mail.mime.encodefilename" System property set.
 */
public class EncodeFileNameTest {

    private static final Logger logger = Logger.getLogger(EncodeFileNameTest.class.getName());

    @Test
    public void test() throws Exception {
        System.setProperty("mail.mime.charset", "utf-8");
        System.setProperty("mail.mime.encodefilename", "true");
        System.clearProperty("mail.mime.encodeparameters");
        String encodedFileName = "=?utf-8?B?w4DDgcOFw4bDgMOBw4XDhsOHw4jDicOKw4vDjMONw47Dj8OQw4DD" +
                "gcOFw4bDh8OIw4nDisOLw4zDjcOOw4/DkMORw5LDk8OUw5XDlsOYw5nDmsObw5" +
                "zDncOew5/DoMOhw6LDo8Okw6XDpsOnw6jDqcOqw6vDrMOtw67Dr8Oww7HDssOz" +
                "w7TDtcO2w7jDucO6w7vDvMO9w77Dv8OAw4HDhcOGw4cuZG9j?=";
        String fileName = MimeUtility.decodeText(encodedFileName);
        MimeBodyPartPublicUpdateHeaders mbp = new MimeBodyPartPublicUpdateHeaders();
        mbp.setText("test");
        mbp.setFileName(fileName);
        mbp.updateHeaders();
        String h = mbp.getHeader("Content-Type", "");
        logger.log(Level.INFO, "h = " + h);
        assertTrue(h.contains("name="));
        // depends on exactly how MimeUtility.encodeText splits long words
        String expected1 = "=?UTF-8?B?w4DDgcOFw4bDgMOBw4XDhsOHw4jDicOKw4vDjMONw47Dj8OQw4DDgcOF?=";
        assertTrue(h.contains(expected1));
        String expected2 = "=?UTF-8?B?w4bDh8OIw4nDisOLw4zDjcOOw4/DkMORw5LDk8OUw5XDlsOYw5nDmsObw5w=?=";
        assertTrue(h.contains(expected2));
        String expected3 = "=?UTF-8?B?w53DnsOfw6DDocOiw6PDpMOlw6bDp8Oow6nDqsOrw6zDrcOuw6/DsMOx?=";
        assertTrue(h.contains(expected3));
        String expected4 = "=?UTF-8?B?w7LDs8O0w7XDtsO4w7nDusO7w7zDvcO+w7/DgMOBw4XDhsOHLmRvYw==?=";
        assertTrue(h.contains(expected4));
        h = mbp.getHeader("Content-Disposition", "");
        assertTrue(h.contains("filename="));
        assertTrue(h.contains(expected1));
        assertTrue(h.contains(expected2));
        assertTrue(h.contains(expected3));
        assertTrue(h.contains(expected4));
        System.clearProperty("mail.mime.charset");
        System.clearProperty("mail.mime.encodefilename");
        System.clearProperty("mail.mime.encodeparameters");
    }

    private static class MimeBodyPartPublicUpdateHeaders extends MimeBodyPart {
        @Override
        public void updateHeaders() throws MessagingException {
            super.updateHeaders();
        }
    }
}
