1 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 ; 26 import java.io.StringReader ; 27 import java.io.StringWriter ; 28 import java.io.InputStream ; 29 import java.io.ByteArrayInputStream ; 30 import java.io.ByteArrayOutputStream ; 31 import java.io.UnsupportedEncodingException ; 32 import java.util.Date ; 33 import java.util.Calendar ; 34 import java.util.SimpleTimeZone ; 35 import java.util.TimeZone ; 36 import java.text.SimpleDateFormat ; 37 import java.text.DateFormat ; 38 39 45 public class CommandParserTest 46 extends TestCase 47 { 48 private CommandParser parser = new CommandParser(); 49 private ByteArrayOutputStream output = new ByteArrayOutputStream (); 50 51 public CommandParserTest( String s ) 52 { 53 super( s ); 54 } 55 56 61 public void testAtom() throws Exception 62 { 63 String 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 testRequest = "a01"; 75 request = getRequest( testRequest ); 76 try { 77 String test = parser.atom( request ); 78 fail( "shouldn't get here" ); 79 } 80 catch ( ProtocolException e ) {} 81 } 82 83 86 public void testTag() throws Exception 87 { 88 String 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 test = parser.tag( request ); 94 fail( "Tags may not contain the '+' character." ); 95 } 96 catch ( ProtocolException e ) {} 97 } 98 99 103 public void testQuoted() throws Exception 104 { 105 String 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 119 public void testLiteral() throws Exception 120 { 121 String 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 assertEquals( "", getServerResponse() ); 128 129 assertEquals( "The \tunsynchronized\nliteral", parser.astring( request ) ); 130 assertEquals( "+\r\n", getServerResponse() ); 132 133 } 134 135 139 public void testAstring() throws Exception 140 { 141 String 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 155 public void testMailbox() throws Exception 156 { 157 String 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 170 public void xtestDateTime() throws Exception 171 { 172 String testRequest = "\"20-Mar-1971 00:23:02 +0000\""; 173 ImapRequestLineReader request = getRequest( testRequest ); 174 175 SimpleDateFormat formatter 176 = new SimpleDateFormat ("yyyyMMdd hh:mm:ss"); 177 formatter.setTimeZone( TimeZone.getTimeZone( "UTC" )); 178 String actual = formatter.format( parser.dateTime( request ) ); 179 assertEquals( "19710320 00:23:02", actual ); 180 } 181 182 185 public void testIdSet() throws Exception 186 { 187 String 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 227 private ImapRequestLineReader getRequest( String testRequest ) throws Exception 228 { 229 InputStream input = new ByteArrayInputStream ( testRequest.getBytes( "US-ASCII" ) ); 230 output.reset(); 232 ImapRequestLineReader request = new ImapRequestLineReader( input, output ); 233 return request; 234 } 235 236 private String getServerResponse() throws UnsupportedEncodingException 237 { 238 byte[] bytesOut = output.toByteArray(); 239 output.reset(); 240 return new String ( bytesOut, "US-ASCII" ); 241 } 242 243 244 } 245 | Popular Tags |