0N/A/*
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0N/A *
292N/A * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
0N/A *
0N/A * The contents of this file are subject to the terms of either the GNU
0N/A * General Public License Version 2 only ("GPL") or the Common Development
0N/A * and Distribution License("CDDL") (collectively, the "License"). You
292N/A * may not use this file except in compliance with the License. You can
292N/A * obtain a copy of the License at
292N/A * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
292N/A * or packager/legal/LICENSE.txt. See the License for the specific
0N/A * language governing permissions and limitations under the License.
0N/A *
0N/A * When distributing the software, include this License Header Notice in each
292N/A * file and include the License file at packager/legal/LICENSE.txt.
292N/A *
292N/A * GPL Classpath Exception:
292N/A * Oracle designates this particular file as subject to the "Classpath"
292N/A * exception as provided by Oracle in the GPL Version 2 section of the License
292N/A * file that accompanied this code.
292N/A *
292N/A * Modifications:
292N/A * If applicable, add the following below the License Header, with the fields
292N/A * enclosed by brackets [] replaced by your own identifying information:
292N/A * "Portions Copyright [year] [name of copyright owner]"
0N/A *
0N/A * Contributor(s):
0N/A * If you wish your version of this file to be governed by only the CDDL or
0N/A * only the GPL Version 2, indicate your decision by adding "[Contributor]
0N/A * elects to include this software in this distribution under the [CDDL or GPL
0N/A * Version 2] license." If you don't indicate a single choice of license, a
0N/A * recipient has the option to distribute your version of this file under
0N/A * either the CDDL, the GPL Version 2 or to extend the choice of license to
0N/A * its licensees as provided above. However, if you add GPL Version 2 code
0N/A * and therefore, elected the GPL Version 2 license, then the option applies
0N/A * only if the new code is made subject to such option by the copyright
0N/A * holder.
0N/A */
0N/A
0N/Aimport java.util.*;
0N/Aimport java.io.*;
0N/Aimport javax.mail.*;
0N/Aimport javax.mail.event.*;
0N/Aimport javax.mail.internet.*;
0N/Aimport javax.mail.util.*;
0N/Aimport javax.activation.*;
0N/A
234N/Aimport org.junit.*;
234N/Aimport static org.junit.Assert.assertEquals;
234N/Aimport static org.junit.Assert.assertTrue;
234N/A
0N/A/*
0N/A * Test multipart parsing.
0N/A *
0N/A * @author Bill Shannon
0N/A */
0N/A
234N/Apublic class MimeMultipartParseTest {
234N/A private static Session session =
234N/A Session.getInstance(new Properties(), null);
0N/A
234N/A private static final int maxsize = 10000;
234N/A private static final String data =
0N/A "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
0N/A
234N/A @Test
234N/A public void testParse() throws Exception {
234N/A test(false);
234N/A }
0N/A
234N/A @Test
234N/A public void testParseShared() throws Exception {
234N/A test(true);
234N/A }
0N/A
234N/A /*
234N/A * Test a few potential boundary cases, then test a range.
234N/A * This is a compromise to make the run time of this test reasonable,
234N/A * although it still takes about 30 seconds, which is on the long side
234N/A * for a unit test.
234N/A */
234N/A public void test(boolean shared) throws Exception {
234N/A testMessage(1, shared);
234N/A testMessage(2, shared);
234N/A testMessage(62, shared);
234N/A testMessage(63, shared);
234N/A testMessage(64, shared);
234N/A testMessage(65, shared);
234N/A testMessage(1023, shared);
234N/A testMessage(1024, shared);
234N/A testMessage(1025, shared);
234N/A for (int size = 8100; size <= maxsize; size++)
234N/A testMessage(size, shared);
0N/A }
0N/A
234N/A public void testMessage(int size, boolean shared) throws Exception {
234N/A //System.out.println("SIZE: " + size);
0N/A /*
234N/A * Construct a multipart message with a part of the
234N/A * given size.
0N/A */
234N/A MimeMessage msg = new MimeMessage(session);
234N/A msg.setFrom(new InternetAddress("me@example.com"));
234N/A msg.setSubject("test multipart parsing");
234N/A msg.setSentDate(new Date(0));
234N/A MimeBodyPart mbp1 = new MimeBodyPart();
234N/A mbp1.setText("main text\n");
234N/A MimeBodyPart mbp3 = new MimeBodyPart();
234N/A mbp3.setText("end text\n");
234N/A MimeBodyPart mbp2 = new MimeBodyPart();
234N/A byte[] part = new byte[size];
234N/A for (int i = 0; i < size; i++) {
234N/A int j = i % 64;
234N/A if (j == 62)
234N/A part[i] = (byte)'\r';
234N/A else if (j == 63)
234N/A part[i] = (byte)'\n';
234N/A else
234N/A part[i] = (byte)data.charAt((j + i / 64) % 62);
0N/A }
234N/A mbp2.setDataHandler(new DataHandler(
234N/A new ByteArrayDataSource(part, "text/plain")));
0N/A
234N/A MimeMultipart mp = new MimeMultipart();
234N/A mp.addBodyPart(mbp1);
234N/A mp.addBodyPart(mbp2);
234N/A mp.addBodyPart(mbp3);
234N/A msg.setContent(mp);
234N/A msg.saveChanges();
0N/A
0N/A /*
234N/A * Write the message out to a byte array.
0N/A */
234N/A ByteArrayOutputStream bos = new ByteArrayOutputStream();
234N/A msg.writeTo(bos);
234N/A bos.close();
234N/A byte[] buf = bos.toByteArray();
0N/A
234N/A /*
234N/A * Construct a new message to parse the bytes.
234N/A */
234N/A msg = new MimeMessage(session, shared ?
234N/A new SharedByteArrayInputStream(buf) :
234N/A new ByteArrayInputStream(buf));
0N/A
234N/A // verify that the part content is correct
234N/A mp = (MimeMultipart)msg.getContent();
234N/A mbp2 = (MimeBodyPart)mp.getBodyPart(1);
234N/A InputStream is = mbp2.getInputStream();
234N/A int k = 0;
234N/A int c;
234N/A while ((c = is.read()) >= 0) {
234N/A int j = k % 64;
234N/A byte e;
234N/A if (j == 62)
234N/A e = (byte)'\r';
234N/A else if (j == 63)
234N/A e = (byte)'\n';
0N/A else
234N/A e = (byte)data.charAt((j + k / 64) % 62);
234N/A Assert.assertEquals("Size " + size + " at byte " + k, e, c);
234N/A k++;
0N/A }
234N/A Assert.assertEquals("Expected size", size, k);
0N/A }
0N/A}