<commit_before>/***********************************************************************
 * Copyright (c) 1999-2006 The Apache Software Foundation.             *
 * All rights reserved.                                                *
 * ------------------------------------------------------------------- *
 * Licensed under the Apache License, Version 2.0 (the "License"); you *
 * may not use this file except in compliance with the License. You    *
 * may obtain a copy of the License at:                                *
 *                                                                     *
 *     http://www.apache.org/licenses/LICENSE-2.0                      *
 *                                                                     *
 * Unless required by applicable law or agreed to in writing, software *
 * distributed under the License is distributed on an "AS IS" BASIS,   *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
 * implied.  See the License for the specific language governing       *
 * permissions and limitations under the License.                      *
 ***********************************************************************/
package org.apache.james.core;

import org.apache.avalon.framework.container.ContainerUtil;
import org.apache.mailet.RFC2822Headers;

import javax.mail.BodyPart;
import javax.mail.Session;
import javax.mail.internet.InternetHeaders;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.Properties;

import junit.framework.TestCase;

/**
 * Test the subject folding issue.
 */
public class MimeMessageTest extends TestCase {

    protected MimeMessage getSimpleMessage() throws Exception {
        MimeMessage mmCreated = new MimeMessage(Session.getDefaultInstance(new Properties()));
        mmCreated.setSubject("test");
        mmCreated.setText("test body");
        mmCreated.saveChanges();
        return mmCreated;
    }
    
    protected String getSimpleMessageCleanedSource() throws Exception {
        return "Subject: test\r\n"
            +"MIME-Version: 1.0\r\n"
            +"Content-Type: text/plain; charset=us-ascii\r\n"
            +"Content-Transfer-Encoding: 7bit\r\n"
            +"\r\n"
            +"test body";
    }
    
    /*
     * Class under test for String getSubject()
     */
    public void testSimpleMessage() throws Exception {
        assertEquals(getSimpleMessageCleanedSource(), getCleanedMessageSource(getSimpleMessage()));
    }
    
    
    protected MimeMessage getMultipartMessage() throws Exception {
        MimeMessage mmCreated = new MimeMessage(Session.getDefaultInstance(new Properties()));
        mmCreated.setSubject("test");
        MimeMultipart mm = new MimeMultipart("alternative");
        mm.addBodyPart(new MimeBodyPart(new InternetHeaders(new ByteArrayInputStream("X-header: test1\r\nContent-Type: text/plain; charset=Cp1252\r\n".getBytes())),"first part ���".getBytes()));
        mm.addBodyPart(new MimeBodyPart(new InternetHeaders(new ByteArrayInputStream("X-header: test2\r\nContent-Type: text/plain; charset=Cp1252\r\nContent-Transfer-Encoding: quoted-printable\r\n".getBytes())),"second part =E8=E8".getBytes()));
        mmCreated.setContent(mm);
        mmCreated.saveChanges();
        return mmCreated;
    }
    
    protected String getMultipartMessageSource() {
        return "Subject: test\r\n"
            +"MIME-Version: 1.0\r\n"
            +"Content-Type: multipart/alternative; \r\n" 
            +"\tboundary=\"----=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\"\r\n"
            +"\r\n"
            +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\r\n"
            +"X-header: test1\r\n"
            +"Content-Type: text/plain; charset=Cp1252\r\n"
            +"Content-Transfer-Encoding: quoted-printable\r\n"<commit_msg>Undo change that caused character set corruption<commit_after>/***********************************************************************
 * Copyright (c) 1999-2006 The Apache Software Foundation.             *
 * All rights reserved.                                                *
 * ------------------------------------------------------------------- *
 * Licensed under the Apache License, Version 2.0 (the "License"); you *
 * may not use this file except in compliance with the License. You    *
 * may obtain a copy of the License at:                                *
 *                                                                     *
 *     http://www.apache.org/licenses/LICENSE-2.0                      *
 *                                                                     *
 * Unless required by applicable law or agreed to in writing, software *
 * distributed under the License is distributed on an "AS IS" BASIS,   *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
 * implied.  See the License for the specific language governing       *
 * permissions and limitations under the License.                      *
 ***********************************************************************/
package org.apache.james.core;

import org.apache.avalon.framework.container.ContainerUtil;
import org.apache.mailet.RFC2822Headers;

import javax.mail.BodyPart;
import javax.mail.Session;
import javax.mail.internet.InternetHeaders;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.Properties;

import junit.framework.TestCase;

/**
 * Test the subject folding issue.
 */
public class MimeMessageTest extends TestCase {

    protected MimeMessage getSimpleMessage() throws Exception {
        MimeMessage mmCreated = new MimeMessage(Session.getDefaultInstance(new Properties()));
        mmCreated.setSubject("test");
        mmCreated.setText("test body");
        mmCreated.saveChanges();
        return mmCreated;
    }
    
    protected String getSimpleMessageCleanedSource() throws Exception {
        return "Subject: test\r\n"
            +"MIME-Version: 1.0\r\n"
            +"Content-Type: text/plain; charset=us-ascii\r\n"
            +"Content-Transfer-Encoding: 7bit\r\n"
            +"\r\n"
            +"test body";
    }
    
    /*
     * Class under test for String getSubject()
     */
    public void testSimpleMessage() throws Exception {
        assertEquals(getSimpleMessageCleanedSource(), getCleanedMessageSource(getSimpleMessage()));
    }
    
    
    protected MimeMessage getMultipartMessage() throws Exception {
        MimeMessage mmCreated = new MimeMessage(Session.getDefaultInstance(new Properties()));
        mmCreated.setSubject("test");
        MimeMultipart mm = new MimeMultipart("alternative");
        mm.addBodyPart(new MimeBodyPart(new InternetHeaders(new ByteArrayInputStream("X-header: test1\r\nContent-Type: text/plain; charset=Cp1252\r\n".getBytes())),"first part ".getBytes()));
        mm.addBodyPart(new MimeBodyPart(new InternetHeaders(new ByteArrayInputStream("X-header: test2\r\nContent-Type: text/plain; charset=Cp1252\r\nContent-Transfer-Encoding: quoted-printable\r\n".getBytes())),"second part =E8=E8".getBytes()));
        mmCreated.setContent(mm);
        mmCreated.saveChanges();
        return mmCreated;
    }
    
    protected String getMultipartMessageSource() {
        return "Subject: test\r\n"
            +"MIME-Version: 1.0\r\n"
            +"Content-Type: multipart/alternative; \r\n" 
            +"\tboundary=\"----=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\"\r\n"
            +"\r\n"
            +"------=_Part_0_XXXXXXXXXXX.XXXXXXXXXXX\r\n"
            +"X-header: test1\r\n"
            +"Content-Type: text/plain; charset=Cp1252\r\n"
            +"Content-Transfer-Encoding: quoted-printable\r\n"
{'fraction_non_alphanumeric': 0.15617920966524038, 'fraction_numerical': 0.010067958721369242, 'mean_word_length': 2.3831417624521074, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, 'word_list_counts': {'cursed_substrings.json': 19, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '825153', 'n_tokens_mistral': 2134, 'n_tokens_neox': 2038, 'n_words': 471}