KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > imap > protocol > IMAPInputStreamTest


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.imap.protocol;
37
38 import java.io.ByteArrayInputStream JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.InputStream JavaDoc;
41 import java.io.SequenceInputStream JavaDoc;
42 import java.util.Random JavaDoc;
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 JavaDoc {
58         String JavaDoc test = "* LIST () \"/\" {8}\r\n12345678\r\n";
59         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc( 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 JavaDoc {
69         String JavaDoc test = "* LIST () {8}\r\n12345678 {8}\r\n87654321\r\n";
70         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc( 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 JavaDoc {
81         byte[] test = new byte[100000];
82         new Random JavaDoc().nextBytes(test);
83         String JavaDoc text = "* 12 FETCH (BODY.PEEK {100000}\r\n";
84         String JavaDoc rest = ")\r\na004 OK FETCH completed\r\n";
85         InputStream JavaDoc in = new SequenceInputStream JavaDoc(new SequenceInputStream JavaDoc(new ByteArrayInputStream JavaDoc(text.getBytes("US-ASCII")),new ByteArrayInputStream JavaDoc( test) ),new ByteArrayInputStream JavaDoc( rest.getBytes("US-ASCII")) );
86         IMAPInputStream imap = new IMAPInputStream( in, null );
87         
88         InputStream JavaDoc 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 JavaDoc {
96         byte[] test = new byte[1000];
97         new Random JavaDoc().nextBytes(test);
98         String JavaDoc text = "* 12 FETCH (BODY.PEEK {1000}\r\n";
99         String JavaDoc rest = ")\r\na004 OK FETCH completed\r\n";
100         InputStream JavaDoc in = new SequenceInputStream JavaDoc(new SequenceInputStream JavaDoc(new ByteArrayInputStream JavaDoc(text.getBytes("US-ASCII")),new ByteArrayInputStream JavaDoc( test) ),new ByteArrayInputStream JavaDoc( rest.getBytes("US-ASCII")) );
101         IMAPInputStream imap = new IMAPInputStream( in, null );
102         
103         InputStream JavaDoc 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 JavaDoc {
111         byte[] test = "This is short body".getBytes("US-ASCII");
112         String JavaDoc text = "* 12 FETCH (BODY \"This is short body\" )\r\n";
113         String JavaDoc rest = ")\r\na004 OK FETCH completed\r\n";
114         InputStream JavaDoc in = new SequenceInputStream JavaDoc(new SequenceInputStream JavaDoc(new ByteArrayInputStream JavaDoc(text.getBytes("US-ASCII")),new ByteArrayInputStream JavaDoc( test) ),new ByteArrayInputStream JavaDoc( rest.getBytes("US-ASCII")) );
115         IMAPInputStream imap = new IMAPInputStream( in, null );
116         
117         InputStream JavaDoc 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 JavaDoc {
125         IMAPHandlerCalled = false;
126         byte[] test = "This is short body".getBytes("US-ASCII");
127         String JavaDoc text = "* 2048 EXISTS\r\n* 12 FETCH (BODY \"This is short body\" )\r\n";
128         String JavaDoc rest = ")\r\na004 OK FETCH completed\r\n";
129         InputStream JavaDoc in = new SequenceInputStream JavaDoc(new SequenceInputStream JavaDoc(new ByteArrayInputStream JavaDoc(text.getBytes("US-ASCII")),new ByteArrayInputStream JavaDoc( test) ),new ByteArrayInputStream JavaDoc( rest.getBytes("US-ASCII")) );
130         IMAPProtocol testProtocol = new IMAPProtocol("localhost", 123);
131         testProtocol.addIMAPListener(new IMAPListener() {
132             public void connectionClosed(String JavaDoc message, String JavaDoc responseCode) {}
133             
134             public void flagsChanged(String JavaDoc mailbox, IMAPFlags flags) {}
135             
136             public void existsChanged(String JavaDoc mailbox, int exists) {
137                 assertEquals(2048, exists);
138                 
139                 IMAPHandlerCalled = true;
140             }
141             
142             public void recentChanged(String JavaDoc mailbox, int recent) {}
143             
144             public void parseError(String JavaDoc message) {}
145             
146             public void alertMessage(String JavaDoc message) {}
147             
148             public void warningMessage(String JavaDoc message) {}
149
150             });
151         
152         IMAPInputStream imap = new IMAPInputStream( in, testProtocol );
153         
154         
155         InputStream JavaDoc 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 JavaDoc {
164         IMAPHandlerCalled = false;
165         String JavaDoc 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 JavaDoc message, String JavaDoc responseCode) {}
169             
170             public void flagsChanged(String JavaDoc mailbox, IMAPFlags flags) {}
171             
172             public void existsChanged(String JavaDoc mailbox, int exists) {
173             }
174             
175             public void recentChanged(String JavaDoc mailbox, int recent) {}
176             
177             public void parseError(String JavaDoc message) {}
178             
179             public void alertMessage(String JavaDoc message) {}
180             
181             public void warningMessage(String JavaDoc message) {}
182
183             });
184         
185         IMAPInputStream imap = new IMAPInputStream( new ByteArrayInputStream JavaDoc(text.getBytes()), testProtocol );
186         
187         
188         try {
189             InputStream JavaDoc result = imap.readBodyNonBlocking();
190             
191             // An Exception should have been thrown!
192
assertTrue(false);
193         } catch (IMAPException e) {
194             assertTrue(true);
195         }
196     }
197
198 }
199
Popular Tags