317N/A/*
317N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
317N/A *
317N/A * Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
317N/A *
317N/A * The contents of this file are subject to the terms of either the GNU
317N/A * General Public License Version 2 only ("GPL") or the Common Development
317N/A * and Distribution License("CDDL") (collectively, the "License"). You
317N/A * may not use this file except in compliance with the License. You can
317N/A * obtain a copy of the License at
317N/A * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
317N/A * or packager/legal/LICENSE.txt. See the License for the specific
317N/A * language governing permissions and limitations under the License.
317N/A *
317N/A * When distributing the software, include this License Header Notice in each
317N/A * file and include the License file at packager/legal/LICENSE.txt.
317N/A *
317N/A * GPL Classpath Exception:
317N/A * Oracle designates this particular file as subject to the "Classpath"
317N/A * exception as provided by Oracle in the GPL Version 2 section of the License
317N/A * file that accompanied this code.
317N/A *
317N/A * Modifications:
317N/A * If applicable, add the following below the License Header, with the fields
317N/A * enclosed by brackets [] replaced by your own identifying information:
317N/A * "Portions Copyright [year] [name of copyright owner]"
317N/A *
317N/A * Contributor(s):
317N/A * If you wish your version of this file to be governed by only the CDDL or
317N/A * only the GPL Version 2, indicate your decision by adding "[Contributor]
317N/A * elects to include this software in this distribution under the [CDDL or GPL
317N/A * Version 2] license." If you don't indicate a single choice of license, a
317N/A * recipient has the option to distribute your version of this file under
317N/A * either the CDDL, the GPL Version 2 or to extend the choice of license to
317N/A * its licensees as provided above. However, if you add GPL Version 2 code
317N/A * and therefore, elected the GPL Version 2 license, then the option applies
317N/A * only if the new code is made subject to such option by the copyright
317N/A * holder.
317N/A */
317N/A
317N/Apackage javax.mail.internet;
317N/A
317N/Aimport java.io.ByteArrayInputStream;
317N/Aimport java.util.Properties;
317N/A
317N/Aimport javax.mail.BodyPart;
317N/Aimport javax.mail.Session;
317N/Aimport javax.mail.internet.MimeMessage;
317N/Aimport javax.mail.internet.MimeMultipart;
317N/A
317N/Aimport org.junit.Test;
317N/A
317N/Aimport static org.junit.Assert.assertArrayEquals;
317N/Aimport static org.junit.Assert.assertEquals;
317N/A
317N/A/**
317N/A * Test the MIME multipart messages are parsed correctly and the
317N/A * correct preamble is returned no matter what line terminators
317N/A * are used. This test found some bugs in the way LineInputStream
317N/A * handled different line terminators.
317N/A *
317N/A * @author trejkaz@kenai.com
317N/A */
317N/Apublic class MimeMultipartPreambleTest {
317N/A @SuppressWarnings({"SingleCharacterStringConcatenation"})
317N/A private String THREE_PART_MAIL =
317N/A "From: user1@example.com\n" +
317N/A "To: user2@example.com\n" +
317N/A "Subject: Receipts\n" +
317N/A "Date: Wed, 14 Jul 2010 19:25:30 +1000\n" +
317N/A "MIME-Version: 1.0\n" +
317N/A "Content-Type: multipart/mixed;boundary=\"----=_NextPart_000_001C_01CB238A.52E35400\"\n" +
317N/A "\n" +
317N/A "This is a multi-part message in MIME format.\n" +
317N/A "\n" +
317N/A "------=_NextPart_000_001C_01CB238A.52E35400\n" +
317N/A "Content-Type: text/plain;charset=\"us-ascii\"\n" +
317N/A "Content-Transfer-Encoding: 7bit\n" +
317N/A "\n" +
317N/A "Hi.\n" +
317N/A "\n" +
317N/A "\n" +
317N/A "------=_NextPart_000_001C_01CB238A.52E35400\n" +
317N/A "Content-Type: application/pdf;name=\"Receipt 1.pdf\"\n" +
317N/A "Content-Transfer-Encoding: base64\n" +
317N/A "Content-Disposition: attachment;filename=\"Receipt 1.pdf\"\n" +
317N/A "\n" +
317N/A "JVBERi0xLjQKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVyIC9GbGF0ZURlY29k\n" +
317N/A "\n" +
317N/A "------=_NextPart_000_001C_01CB238A.52E35400\n" +
317N/A "Content-Type: application/pdf;name=\"Receipt 2.pdf\"\n" +
317N/A "Content-Transfer-Encoding: base64\n" +
317N/A "Content-Disposition: attachment;filename=\"Receipt 2.pdf\"\n" +
317N/A "\n" +
317N/A "JVBERi0xLjQKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVyIC9GbGF0ZURlY29k\n" +
317N/A "\n" +
317N/A "------=_NextPart_000_001C_01CB238A.52E35400--\n" +
317N/A "\n" +
317N/A "\n";
317N/A
317N/A @Test
317N/A public void testUnixLines() throws Exception {
317N/A doThreePartMailTest(THREE_PART_MAIL);
317N/A }
317N/A
317N/A @Test
317N/A public void testWindowsLines() throws Exception {
317N/A doThreePartMailTest(THREE_PART_MAIL.replace("\n", "\r\n"));
317N/A }
317N/A
317N/A @Test
317N/A public void testMacLines() throws Exception {
317N/A doThreePartMailTest(THREE_PART_MAIL.replace('\n', '\r'));
317N/A }
317N/A
317N/A /**
317N/A * Performs a check that the multipart of the email was handled correctly.
317N/A *
317N/A * @param text the email text.
317N/A * @throws Exception if an error occurs.
317N/A */
317N/A private void doThreePartMailTest(String text) throws Exception {
317N/A Session session = Session.getDefaultInstance(new Properties());
317N/A MimeMessage mimeMessage = new MimeMessage(session,
317N/A new ByteArrayInputStream(text.getBytes("US-ASCII")));
317N/A
317N/A MimeMultipart topMultipart = (MimeMultipart) mimeMessage.getContent();
317N/A assertEquals("Wrong preamble",
317N/A "This is a multi-part message in MIME format.",
317N/A topMultipart.getPreamble().trim());
317N/A assertEquals("Wrong number of parts", 3, topMultipart.getCount());
317N/A
317N/A BodyPart part1 = topMultipart.getBodyPart(0);
317N/A assertEquals("Wrong content type for part 1",
317N/A "text/plain;charset=\"us-ascii\"", part1.getContentType());
317N/A
317N/A BodyPart part2 = topMultipart.getBodyPart(1);
317N/A assertEquals("Wrong content type for part 2",
317N/A "application/pdf;name=\"Receipt 1.pdf\"", part2.getContentType());
317N/A
317N/A BodyPart part3 = topMultipart.getBodyPart(2);
317N/A assertEquals("Wrong content type for part 3",
317N/A "application/pdf;name=\"Receipt 2.pdf\"", part3.getContentType());
317N/A }
317N/A}