1 36 package org.columba.ristretto.parser; 37 38 import java.io.IOException ; 39 40 import junit.framework.TestCase; 41 42 import org.columba.ristretto.io.CharSequenceSource; 43 import org.columba.ristretto.io.Source; 44 import org.columba.ristretto.message.LocalMimePart; 45 import org.columba.ristretto.message.MimeHeader; 46 import org.columba.ristretto.message.MimePart; 47 48 public class BodyParserTest extends TestCase { 49 50 54 public BodyParserTest(String arg0) { 55 super(arg0); 56 } 57 58 public void testSimpleBody() { 59 MimeHeader mimeHeader = new MimeHeader(); 60 String testmail = "This is a test\r\n"; 61 try { 62 LocalMimePart message = BodyParser.parseMimePart(mimeHeader, new CharSequenceSource(testmail)); 63 Source source = message.getBody(); 64 assertTrue( source.length() == testmail.length() ); 65 } catch (IOException e) { 66 e.printStackTrace(); 67 } catch (ParserException e) { 68 e.printStackTrace(); 69 } 70 } 71 72 public void testMultipartBody() { 73 MimeHeader mimeHeader = new MimeHeader("multipart","mixed"); 74 mimeHeader.putContentParameter("boundary","+?*"); 75 String testmail = "This is a test\r\n--+?*\r\nContent-Type: text/plain\r\n\r\n1\r\n--+?*\r\nContent-Type: text/plain\r\n\r\n2\r\n--+?*--\r\n"; 76 try { 77 LocalMimePart message = BodyParser.parseMimePart(mimeHeader, new CharSequenceSource(testmail)); 78 Source source = message.getBody(); 79 assertTrue( source.length() == testmail.length() ); 80 assertTrue( message.countChilds() == 2); 81 Source body1 = ((LocalMimePart)message.getChild(0)).getBody(); 82 assertEquals( "1", body1.toString()); 83 Source body2 = ((LocalMimePart)message.getChild(1)).getBody(); 84 assertTrue( body2.toString().equals("2")); 85 86 } catch (IOException e) { 87 e.printStackTrace(); 88 } catch (ParserException e) { 89 e.printStackTrace(); 90 } 91 } 92 93 public void testMultipartBody2() { 94 MimeHeader mimeHeader = new MimeHeader("multipart","mixed"); 95 mimeHeader.putContentParameter("boundary","=_alternative 0047EBBC85256D9F_="); 96 String testmail = "This is a test\r\n--=_alternative 0047EBBC85256D9F_=\r\nContent-Type: text/plain\r\n\r\n1\r\n--=_alternative 0047EBBC85256D9F_=\r\nContent-Type: text/plain\r\n\r\n2\r\n--=_alternative 0047EBBC85256D9F_=--\r\n"; 97 try { 98 LocalMimePart message = BodyParser.parseMimePart(mimeHeader, new CharSequenceSource(testmail)); 99 Source source = message.getBody(); 100 assertTrue( source.length() == testmail.length() ); 101 assertTrue( message.countChilds() == 2); 102 Source body1 = ((LocalMimePart)message.getChild(0)).getBody(); 103 assertTrue( body1.toString().equals("1")); 104 Source body2 = ((LocalMimePart)message.getChild(1)).getBody(); 105 assertTrue( body2.toString().equals("2")); 106 107 } catch (IOException e) { 108 e.printStackTrace(); 109 } catch (ParserException e) { 110 e.printStackTrace(); 111 } 112 } 113 114 public void testMultipartMultipartBody() { 115 MimeHeader mimeHeader = new MimeHeader("multipart","mixed"); 116 mimeHeader.putContentParameter("boundary","boundary"); 117 String testmail = "This is a test\r\n--boundary\r\nContent-Type: text/plain\r\n\r\n1\r\n--boundary\r\nContent-Type: multipart/mixed; boundary=\"bound2\"\r\n\r\nblablbala\r\n--bound2\r\nContent-Type: text/plain\r\n\r\n21\r\n--bound2\r\nContent-Type: text/plain\r\n\r\n22\r\n--bound2--\r\n\r\n--boundary--\r\n"; 118 try { 119 LocalMimePart message = BodyParser.parseMimePart(mimeHeader, new CharSequenceSource(testmail)); 120 Source source = message.getBody(); 121 assertTrue( source.length() == testmail.length() ); 122 assertTrue( message.countChilds() == 2); 123 Source body1 = ((LocalMimePart)message.getChild(0)).getBody(); 124 assertTrue( body1.toString().equals("1")); 125 MimePart nestedMultipart = (MimePart)message.getChild(1); 126 Source body21 = ((LocalMimePart)nestedMultipart.getChild(0)).getBody(); 127 assertTrue( body21.toString().equals("21")); 128 Source body22 = ((LocalMimePart)nestedMultipart.getChild(1)).getBody(); 129 assertTrue( body22.toString().equals("22")); 130 131 } catch (IOException e) { 132 e.printStackTrace(); 133 } catch (ParserException e) { 134 e.printStackTrace(); 135 } 136 } 137 138 public void testMultipartBodyNoStartBoundary() { 139 MimeHeader mimeHeader = new MimeHeader("multipart","mixed"); 140 mimeHeader.putContentParameter("boundary","willNotFind"); 141 String testmail = "This is a test\r\n--+?*\r\nContent-Type: text/plain\r\n\r\n1\r\n--+?*\r\nContent-Type: text/plain\r\n\r\n2\r\n--+?*--\r\n"; 142 try { 143 LocalMimePart message = BodyParser.parseMimePart(mimeHeader, new CharSequenceSource(testmail)); 144 } catch (IOException e) { 145 e.printStackTrace(); 146 } catch (ParserException e) { 147 assertTrue(true); 148 System.err.println( e.getSource() ); 149 return; 150 } 151 152 assertTrue( false ); 153 } 154 155 public void testMultipartBodyMissingEndBoundary() { 156 MimeHeader mimeHeader = new MimeHeader("multipart","mixed"); 157 mimeHeader.putContentParameter("boundary","+?*"); 158 String testmail = "This is a test\r\n--+?*\r\nContent-Type: text/plain\r\n\r\n1\r\n--+?*\r\nContent-Type: text/plain\r\n\r\n2"; 159 try { 160 LocalMimePart message = BodyParser.parseMimePart(mimeHeader, new CharSequenceSource(testmail)); 161 Source source = message.getBody(); 162 assertTrue( source.length() == testmail.length() ); 163 assertTrue( message.countChilds() == 2); 164 Source body1 = ((LocalMimePart)message.getChild(0)).getBody(); 165 assertEquals( "1", body1.toString()); 166 Source body2 = ((LocalMimePart)message.getChild(1)).getBody(); 167 assertEquals( "2", body2.toString()); 168 169 } catch (IOException e) { 170 e.printStackTrace(); 171 } catch (ParserException e) { 172 e.printStackTrace(); 173 } 174 } 175 176 177 } 178
| Popular Tags
|