1 36 package org.columba.ristretto.imap.protocol; 37 38 import java.io.ByteArrayInputStream ; 39 import java.io.IOException ; 40 import java.io.InputStream ; 41 import java.io.SequenceInputStream ; 42 import java.util.Random ; 43 44 import junit.framework.TestCase; 45 46 import org.columba.ristretto.imap.IMAPException; 47 import org.columba.ristretto.imap.IMAPFlags; 48 import org.columba.ristretto.imap.IMAPInputStream; 49 import org.columba.ristretto.imap.IMAPListener; 50 import org.columba.ristretto.imap.IMAPProtocol; 51 import org.columba.ristretto.imap.IMAPResponse; 52 53 public class IMAPInputStreamTest extends TestCase { 54 55 boolean IMAPHandlerCalled; 56 57 public void testLiteralSimple() throws Exception { 58 String test = "* LIST () \"/\" {8}\r\n12345678\r\n"; 59 InputStream in = new ByteArrayInputStream ( test.getBytes() ); 60 IMAPInputStream imap = new IMAPInputStream( in, null ); 61 62 IMAPResponse response = imap.readResponse(); 63 64 assertEquals( "() \"/\" {0}", response.getResponseMessage() ); 65 assertEquals( "12345678", response.getData("{0}").toString()); 66 } 67 68 public void testLiteralMultiple() throws Exception { 69 String test = "* LIST () {8}\r\n12345678 {8}\r\n87654321\r\n"; 70 InputStream in = new ByteArrayInputStream ( test.getBytes() ); 71 IMAPInputStream imap = new IMAPInputStream( in, null ); 72 73 IMAPResponse response = imap.readResponse(); 74 75 assertTrue( response.getResponseMessage().equals("() {0} {1}") ); 76 assertTrue( response.getData("{0}").toString().equals("12345678")); 77 assertTrue( response.getData("{1}").toString().equals("87654321")); 78 } 79 80 public void testReadBodyNonBlock1() throws Exception { 81 byte[] test = new byte[100000]; 82 new Random ().nextBytes(test); 83 String text = "* 12 FETCH (BODY.PEEK {100000}\r\n"; 84 String rest = ")\r\na004 OK FETCH completed\r\n"; 85 InputStream in = new SequenceInputStream (new SequenceInputStream (new ByteArrayInputStream (text.getBytes("US-ASCII")),new ByteArrayInputStream ( test) ),new ByteArrayInputStream ( rest.getBytes("US-ASCII")) ); 86 IMAPInputStream imap = new IMAPInputStream( in, null ); 87 88 InputStream result = imap.readBodyNonBlocking(); 89 90 for( int i=0; i<100000; i++) { 91 assertEquals( test[i], (byte) result.read()); 92 } 93 } 94 95 public void testReadBodyNonBlock2() throws Exception { 96 byte[] test = new byte[1000]; 97 new Random ().nextBytes(test); 98 String text = "* 12 FETCH (BODY.PEEK {1000}\r\n"; 99 String rest = ")\r\na004 OK FETCH completed\r\n"; 100 InputStream in = new SequenceInputStream (new SequenceInputStream (new ByteArrayInputStream (text.getBytes("US-ASCII")),new ByteArrayInputStream ( test) ),new ByteArrayInputStream ( rest.getBytes("US-ASCII")) ); 101 IMAPInputStream imap = new IMAPInputStream( in, null ); 102 103 InputStream result = imap.readBodyNonBlocking(); 104 105 for( int i=0; i<1000; i++) { 106 assertEquals( test[i], (byte) result.read()); 107 } 108 } 109 110 public void testReadBodyNonBlock3() throws Exception { 111 byte[] test = "This is short body".getBytes("US-ASCII"); 112 String text = "* 12 FETCH (BODY \"This is short body\" )\r\n"; 113 String rest = ")\r\na004 OK FETCH completed\r\n"; 114 InputStream in = new SequenceInputStream (new SequenceInputStream (new ByteArrayInputStream (text.getBytes("US-ASCII")),new ByteArrayInputStream ( test) ),new ByteArrayInputStream ( rest.getBytes("US-ASCII")) ); 115 IMAPInputStream imap = new IMAPInputStream( in, null ); 116 117 InputStream result = imap.readBodyNonBlocking(); 118 119 for( int i=0; i<test.length; i++) { 120 assertEquals( test[i], (byte) result.read()); 121 } 122 } 123 124 public void testReadBodyNonBlockUnsolicited() throws Exception { 125 IMAPHandlerCalled = false; 126 byte[] test = "This is short body".getBytes("US-ASCII"); 127 String text = "* 2048 EXISTS\r\n* 12 FETCH (BODY \"This is short body\" )\r\n"; 128 String rest = ")\r\na004 OK FETCH completed\r\n"; 129 InputStream in = new SequenceInputStream (new SequenceInputStream (new ByteArrayInputStream (text.getBytes("US-ASCII")),new ByteArrayInputStream ( test) ),new ByteArrayInputStream ( rest.getBytes("US-ASCII")) ); 130 IMAPProtocol testProtocol = new IMAPProtocol("localhost", 123); 131 testProtocol.addIMAPListener(new IMAPListener() { 132 public void connectionClosed(String message, String responseCode) {} 133 134 public void flagsChanged(String mailbox, IMAPFlags flags) {} 135 136 public void existsChanged(String mailbox, int exists) { 137 assertEquals(2048, exists); 138 139 IMAPHandlerCalled = true; 140 } 141 142 public void recentChanged(String mailbox, int recent) {} 143 144 public void parseError(String message) {} 145 146 public void alertMessage(String message) {} 147 148 public void warningMessage(String message) {} 149 150 }); 151 152 IMAPInputStream imap = new IMAPInputStream( in, testProtocol ); 153 154 155 InputStream result = imap.readBodyNonBlocking(); 156 assertTrue( IMAPHandlerCalled ); 157 158 for( int i=0; i<test.length; i++) { 159 assertEquals( test[i], (byte) result.read()); 160 } 161 } 162 163 public void testReadBodyNonBlockUnsolicitedError() throws Exception { 164 IMAPHandlerCalled = false; 165 String text = "* NO Error 6 processing FETCH command.\r\nR511 NO Error 6 processing FETCH command.\r\n"; 166 IMAPProtocol testProtocol = new IMAPProtocol("localhost", 123); 167 testProtocol.addIMAPListener(new IMAPListener() { 168 public void connectionClosed(String message, String responseCode) {} 169 170 public void flagsChanged(String mailbox, IMAPFlags flags) {} 171 172 public void existsChanged(String mailbox, int exists) { 173 } 174 175 public void recentChanged(String mailbox, int recent) {} 176 177 public void parseError(String message) {} 178 179 public void alertMessage(String message) {} 180 181 public void warningMessage(String message) {} 182 183 }); 184 185 IMAPInputStream imap = new IMAPInputStream( new ByteArrayInputStream (text.getBytes()), testProtocol ); 186 187 188 try { 189 InputStream result = imap.readBodyNonBlocking(); 190 191 assertTrue(false); 193 } catch (IMAPException e) { 194 assertTrue(true); 195 } 196 } 197 198 } 199 | Popular Tags |