KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > CommandParserTest


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.imapserver;
19
20 import org.apache.james.imapserver.commands.CommandParser;
21 import org.apache.james.imapserver.commands.IdSet;
22
23 import junit.framework.TestCase;
24
25 import java.io.BufferedReader JavaDoc;
26 import java.io.StringReader JavaDoc;
27 import java.io.StringWriter JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.ByteArrayInputStream JavaDoc;
30 import java.io.ByteArrayOutputStream JavaDoc;
31 import java.io.UnsupportedEncodingException JavaDoc;
32 import java.util.Date JavaDoc;
33 import java.util.Calendar JavaDoc;
34 import java.util.SimpleTimeZone JavaDoc;
35 import java.util.TimeZone JavaDoc;
36 import java.text.SimpleDateFormat JavaDoc;
37 import java.text.DateFormat JavaDoc;
38
39 /**
40  * Tests for the {@link ImapRequestLineReader}.
41  * TODO: atom, literal, other (not yet implemented) arguments
42  *
43  * @version $Revision: 1.5.2.3 $
44  */

45 public class CommandParserTest
46         extends TestCase
47 {
48     private CommandParser parser = new CommandParser();
49     private ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
50
51     public CommandParserTest( String JavaDoc s )
52     {
53         super( s );
54     }
55
56     /**
57      * Tests handling of the "atom" argument type.
58      * TODO: check special characters.
59      * @throws Exception
60      */

61     public void testAtom() throws Exception JavaDoc
62     {
63         String JavaDoc testRequest = "a01 a.not.her one\ntwo ";
64         ImapRequestLineReader request = getRequest( testRequest );
65
66         assertEquals( "a01", parser.atom( request ) );
67         assertEquals( "a.not.her", parser.atom( request ) );
68         assertEquals( "one", parser.atom( request ) );
69         request.eol();
70         request.consumeLine();
71         assertEquals( "two", parser.atom( request ) );
72
73         // Must have \n or " " after atom.
74
testRequest = "a01";
75         request = getRequest( testRequest );
76         try {
77             String JavaDoc test = parser.atom( request );
78             fail( "shouldn't get here" );
79         }
80         catch ( ProtocolException e ) {}
81     }
82
83     /**
84      * Test handling of the "tag" argument type.
85      */

86     public void testTag() throws Exception JavaDoc
87     {
88         String JavaDoc testRequest = "this-is-ok this+is+not+ok";
89         ImapRequestLineReader request = getRequest( testRequest );
90
91         assertEquals( "this-is-ok", parser.tag( request ));
92         try {
93             String JavaDoc test = parser.tag( request );
94             fail( "Tags may not contain the '+' character." );
95         }
96         catch ( ProtocolException e ) {}
97     }
98
99     /**
100      * Tests handling of quoted strings.
101      * TODO: illegal characters. illegal escapes
102      */

103     public void testQuoted() throws Exception JavaDoc
104     {
105         String JavaDoc testRequest = "\"word\" \"words with spaces\" \"\" " +
106                 "\"esca\\\\ped \\\" chars\"";
107         ImapRequestLineReader request = getRequest( testRequest );
108
109         assertEquals( "word", parser.astring( request) );
110         assertEquals( "words with spaces", parser.astring( request) );
111         assertEquals( "", parser.astring( request) );
112         assertEquals( "esca\\ped \" chars", parser.astring( request ) );
113     }
114
115     /**
116      * Tests handling of "literal" arguments.
117      * TODO: test this thoroughly
118      */

119     public void testLiteral() throws Exception JavaDoc
120     {
121         // Synchronized literal.
122
String JavaDoc test = "{24+}\r\nA \tsynchronized \nliteral {27}\r\nThe \tunsynchronized\nliteral";
123         ImapRequestLineReader request = getRequest(test );
124
125         assertEquals( "A \tsynchronized \nliteral", parser.astring( request ) );
126         // Make sure we didn't get a command continuation response
127
assertEquals( "", getServerResponse() );
128
129         assertEquals( "The \tunsynchronized\nliteral", parser.astring( request ) );
130         // Make sure we got a command continuation response
131
assertEquals( "+\r\n", getServerResponse() );
132
133     }
134
135     /**
136      * Test handling of astring arguments. More detailed tests for atom,
137      * quoted and literal should be in specific argument tests.
138      */

139     public void testAstring() throws Exception JavaDoc
140     {
141         String JavaDoc testRequest = "atom at.om \"quoted\" \"\" {6+}\r\n\"here\"";
142         ImapRequestLineReader request = getRequest( testRequest );
143
144         assertEquals( "atom", parser.astring( request ) );
145         assertEquals( "at.om", parser.astring( request ));
146         assertEquals( "quoted", parser.astring( request ));
147         assertEquals( "", parser.astring( request ));
148         assertEquals( "\"here\"", parser.astring( request ));
149     }
150
151     /**
152      * Tests for reading "mailbox" arguments. This is simply an "astring", where the
153      * special name "INBOX" is treated case insensitive.
154      */

155     public void testMailbox() throws Exception JavaDoc
156     {
157         String JavaDoc testRequest = "mailboxName \"quotedName\" {11+}\nliteralName iNbOx ";
158         ImapRequestLineReader request = getRequest( testRequest );
159
160         assertEquals( "mailboxName", parser.mailbox( request ) );
161         assertEquals( "quotedName", parser.mailbox( request ));
162         assertEquals( "literalName", parser.mailbox( request ));
163         assertEquals( "INBOX", parser.mailbox( request ));
164     }
165
166     /**
167      * Tests for reading "date-time" arguments.
168      * TODO this test fails, as timezones aren't handled properly - need to investigate.
169      */

170     public void xtestDateTime() throws Exception JavaDoc
171     {
172         String JavaDoc testRequest = "\"20-Mar-1971 00:23:02 +0000\"";
173         ImapRequestLineReader request = getRequest( testRequest );
174
175         SimpleDateFormat JavaDoc formatter
176             = new SimpleDateFormat JavaDoc ("yyyyMMdd hh:mm:ss");
177         formatter.setTimeZone( TimeZone.getTimeZone( "UTC" ));
178         String JavaDoc actual = formatter.format( parser.dateTime( request ) );
179         assertEquals( "19710320 00:23:02", actual );
180     }
181
182     /**
183      * Tests parsing of "set" arguments.
184      */

185     public void testIdSet() throws Exception JavaDoc
186     {
187         String JavaDoc testRequest = "8 25 1:4 33:* 2,3,4 1,4:6,8:* ";
188         ImapRequestLineReader request = getRequest( testRequest );
189
190         IdSet idSet;
191         idSet = parser.set( request );
192         checkSet( idSet, new long[]{8}, new long[]{0, 2, 7, 9, 20, Long.MAX_VALUE } );
193
194         idSet = parser.set( request );
195         checkSet( idSet, new long[]{ 25 }, new long[]{ 0, 5, 20, 30, Long.MAX_VALUE } );
196
197         idSet = parser.set( request );
198         checkSet( idSet, new long[]{ 1, 2, 3, 4 }, new long[]{0, 5, 10 } );
199
200         idSet = parser.set( request );
201         checkSet( idSet, new long[]{ 33, 35, 100, 1000, Long.MAX_VALUE}, new long[]{0, 1, 32});
202
203         idSet = parser.set( request );
204         checkSet( idSet, new long[]{ 2,3,4}, new long[]{0, 1, 5,8 });
205
206         idSet = parser.set( request );
207         checkSet( idSet, new long[]{ 1,4,5,6,8,100,1000,Long.MAX_VALUE}, new long[]{0,2,3,7});
208
209     }
210
211     private void checkSet( IdSet idSet, long[] includes, long[] excludes )
212     {
213         for ( int i = 0; i < includes.length; i++ ) {
214             assertTrue( idSet.includes( includes[i] ));
215         }
216         for ( int i = 0; i < excludes.length; i++ ) {
217             assertTrue( ! idSet.includes( excludes[i] ));
218         }
219     }
220
221     /**
222      * Builds and ImapRequestLineReader with the specified string, using {@link #output}
223      * as the server writer for command continuation requests
224      * @param testRequest A string containing client requests.
225      * @return An initialised ImapRequestLineReader
226      */

227     private ImapRequestLineReader getRequest( String JavaDoc testRequest ) throws Exception JavaDoc
228     {
229         InputStream JavaDoc input = new ByteArrayInputStream JavaDoc( testRequest.getBytes( "US-ASCII" ) );
230         // Clear the writer.
231
output.reset();
232         ImapRequestLineReader request = new ImapRequestLineReader( input, output );
233         return request;
234     }
235
236     private String JavaDoc getServerResponse() throws UnsupportedEncodingException JavaDoc
237     {
238         byte[] bytesOut = output.toByteArray();
239         output.reset();
240         return new String JavaDoc( bytesOut, "US-ASCII" );
241     }
242
243
244 }
245
Popular Tags