1 36 package org.columba.ristretto.composer; 37 38 import java.io.InputStream ; 39 import java.nio.charset.Charset ; 40 import java.util.Date ; 41 42 import junit.framework.TestCase; 43 44 import org.columba.ristretto.io.CharSequenceSource; 45 import org.columba.ristretto.message.Address; 46 import org.columba.ristretto.message.BasicHeader; 47 import org.columba.ristretto.message.Header; 48 import org.columba.ristretto.message.LocalMimePart; 49 import org.columba.ristretto.message.MimeHeader; 50 import org.columba.ristretto.message.MimeType; 51 import org.columba.ristretto.message.StreamableMimePart; 52 53 public class MimeTreeRendererTest extends TestCase { 54 55 public void testComposeSimple() { 56 Header header = new Header(); 57 String body = "Body"; 58 BasicHeader basicHeader = new BasicHeader( header ); 59 basicHeader.setFrom( new Address( "Timo Stich", "tstich@users.sourceforge.net")); 60 basicHeader.setTo( new Address[] { new Address( "Timo Stich", "tstich@users.sourceforge.net" )}); 61 basicHeader.setSubject( "This is a test mail", Charset.forName("US-ASCII")); 62 basicHeader.setDate(new Date (0)); 63 64 MimeHeader mimeHeader = new MimeHeader(header); 65 mimeHeader.setMimeType(new MimeType("text", "plain")); 66 mimeHeader.setContentTransferEncoding("7bit"); 67 mimeHeader.putContentParameter("charset", "us-ascii"); 68 69 StreamableMimePart mimepart = new LocalMimePart( mimeHeader, new CharSequenceSource(body)); 70 71 72 try { 74 StringBuffer result = new StringBuffer (); 75 InputStream messageStream = MimeTreeRenderer.getInstance().renderMimePart(mimepart); 76 int next = messageStream.read(); 77 while( next != -1 ) { 78 result.append((char) next); 79 next = messageStream.read(); 80 } 81 String message = result.toString(); 82 System.out.println( message ); 83 84 assertEquals( "Date: Thu, 1 Jan 1970 01:00:00 +0100\r\nSubject: This is a test mail\r\nTo: \"Timo Stich\" <tstich@users.sourceforge.net>\r\nContent-Transfer-Encoding: 7bit\r\nFrom: \"Timo Stich\" <tstich@users.sourceforge.net>\r\nContent-Type: text/plain; charset=\"us-ascii\"\r\n\r\nBody" , message); 85 } catch (Exception e) { 86 e.printStackTrace(); 87 } 88 } 89 90 public void testComposeMultipartMixed() { 91 Header header = new Header(); 92 String body = "Body"; 93 BasicHeader basicHeader = new BasicHeader( header ); 94 basicHeader.setFrom( new Address( "Timo Stich", "tstich@users.sourceforge.net")); 95 basicHeader.setTo( new Address[] { new Address( "Timo Stich", "tstich@users.sourceforge.net" )}); 96 basicHeader.setSubject( "This is a test mail", Charset.forName("US-ASCII")); 97 basicHeader.setDate(new Date (0)); 98 99 MimeHeader mimeHeader = new MimeHeader(header); 100 mimeHeader.setMimeType(new MimeType("multipart", "mixed")); 101 102 StreamableMimePart mimepart = new LocalMimePart( mimeHeader, new CharSequenceSource(body)); 103 104 mimeHeader = new MimeHeader(); 105 mimeHeader.setMimeType(new MimeType("text", "plain")); 106 mimeHeader.setContentTransferEncoding("7bit"); 107 mimeHeader.putContentParameter("charset", "us-ascii"); 108 109 StreamableMimePart subpart = new LocalMimePart( mimeHeader, new CharSequenceSource(body)); 110 mimepart.addChild( subpart ); 111 mimepart.addChild( subpart ); 112 113 114 try { 116 StringBuffer result = new StringBuffer (); 117 InputStream messageStream = MimeTreeRenderer.getInstance().renderMimePart(mimepart); 118 int next = messageStream.read(); 119 while( next != -1 ) { 120 result.append((char) next); 121 next = messageStream.read(); 122 } 123 String message = result.toString(); 124 System.out.println( message ); 125 126 } catch (Exception e) { 127 e.printStackTrace(); 128 } 129 } 130 131 public void testComposeQPEncoded() { 132 Header header = new Header(); 133 String body = "This is a ?est"; 134 BasicHeader basicHeader = new BasicHeader( header ); 135 basicHeader.setFrom( new Address( "Timo Stich", "tstich@users.sourceforge.net")); 136 basicHeader.setTo( new Address[] { new Address( "Timo Stich", "tstich@users.sourceforge.net" )}); 137 basicHeader.setSubject( "This is a test mail", Charset.forName("US-ASCII")); 138 basicHeader.setDate(new Date (0)); 139 140 MimeHeader mimeHeader = new MimeHeader(header); 141 mimeHeader.setMimeType(new MimeType("text", "plain")); 142 mimeHeader.setContentTransferEncoding("7bit"); 143 mimeHeader.putContentParameter("charset", "iso-8859-1"); 144 mimeHeader.setContentTransferEncoding("quoted-printable"); 145 146 StreamableMimePart mimepart = new LocalMimePart( mimeHeader, new CharSequenceSource(body)); 147 148 149 try { 151 StringBuffer result = new StringBuffer (); 152 InputStream messageStream = MimeTreeRenderer.getInstance().renderMimePart(mimepart); 153 int next = messageStream.read(); 154 while( next != -1 ) { 155 result.append((char) next); 156 next = messageStream.read(); 157 } 158 String message = result.toString(); 159 System.out.println( message ); 160 161 } catch (Exception e) { 162 e.printStackTrace(); 163 } 164 } 165 166 167 } 168 | Popular Tags |