- Timestamp:
- 08/19/08 08:34:29 PM (5 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/LogicMailTests/src/org/logicprobe/LogicMail/mail/imap/ImapProtocolTest.java
r271 r276 8 8 * 9 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer. 11 11 * 2. Redistributions in binary form must reproduce the above copyright 12 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 13 * documentation and/or other materials provided with the distribution. 14 14 * 3. Neither the name of the project nor the names of its 15 15 * contributors may be used to endorse or promote products derived … … 29 29 * OF THE POSSIBILITY OF SUCH DAMAGE. 30 30 */ 31 32 31 package org.logicprobe.LogicMail.mail.imap; 33 32 … … 36 35 import j2meunit.framework.TestMethod; 37 36 import j2meunit.framework.TestSuite; 38 import java.io.IOException; 37 38 import org.logicprobe.LogicMail.message.MessageEnvelope; 39 import org.logicprobe.LogicMail.util.StringParser; 40 39 41 import java.util.Calendar; 40 42 import java.util.Hashtable; 41 43 import java.util.TimeZone; 42 44 import java.util.Vector; 43 import org.logicprobe.LogicMail.mail.MailException; 44 import org.logicprobe.LogicMail.message.MessageEnvelope; 45 import org.logicprobe.LogicMail.util.StringParser; 45 46 46 47 47 /** … … 49 49 */ 50 50 public class ImapProtocolTest extends TestCase { 51 /**52 * Variation on ImapProtocol that overrides the "execute" method53 * to facilitate testing54 */55 class TestImapProtocol extends ImapProtocol {56 private class ExecuteExpectation {57 public String command;58 public String arguments;59 public String[] result;60 }61 62 private Vector executeExpectations;63 64 public TestImapProtocol() {65 super(null);66 executeExpectations = new Vector();67 }68 69 public void addExecuteExpectation(String command, String arguments, String[] result) {70 ExecuteExpectation expect = new ExecuteExpectation();71 expect.command = command;72 expect.arguments = arguments;73 expect.result = result;74 executeExpectations.addElement(expect);75 }76 77 public void clearExpectations() {78 executeExpectations.removeAllElements();79 }80 81 public void verifyExpectations() {82 assertTrue("Expectations failed", executeExpectations.isEmpty());83 }84 85 protected String[] execute(String command, String arguments) {86 assertTrue("No expectations", !executeExpectations.isEmpty());87 ExecuteExpectation expect = (ExecuteExpectation)executeExpectations.lastElement();88 assertEquals("Bad command", expect.command, command);89 assertEquals("Bad arguments", expect.arguments, arguments);90 executeExpectations.removeElement(expect);91 return expect.result;92 }93 }94 95 51 private TestImapProtocol instance; 96 52 97 53 public ImapProtocolTest() { 98 54 } 99 55 100 56 public ImapProtocolTest(String testName, TestMethod testMethod) { 101 57 super(testName, testMethod); 102 58 } 103 59 104 60 public void setUp() { 105 61 instance = new TestImapProtocol(); 106 62 } 107 63 108 64 public void tearDown() { 109 65 instance.verifyExpectations(); … … 111 67 instance = null; 112 68 } 113 69 114 70 public void testExecuteCapability() { 115 71 try { 116 instance.addExecuteExpectation( 117 "CAPABILITY", null, 118 new String[] { "* CAPABILITY ALPHA BRAVO CHARLIE DELTA" }); 72 instance.addExecuteExpectation("CAPABILITY", null, 73 new String[] { "* CAPABILITY ALPHA BRAVO CHARLIE DELTA" }); 119 74 120 75 Hashtable result = instance.executeCapability(); … … 127 82 assertEquals(Boolean.TRUE, result.get("DELTA")); 128 83 } catch (Throwable t) { 129 fail("Exception thrown during test: " +t.toString());130 t.printStackTrace(); 131 } 132 } 133 134 public void testExecuteNamespace () {84 fail("Exception thrown during test: " + t.toString()); 85 t.printStackTrace(); 86 } 87 } 88 89 public void testExecuteNamespace1() { 135 90 try { 136 91 // Normal namespace: 137 92 // NAMESPACE (("" "/")) (("Other Users/" "/")) NIL 138 instance.addExecuteExpectation( 139 "NAMESPACE", null, 140 new String[] { "* NAMESPACE ((\"\" \"/\")) ((\"Other Users/\" \"/\")) NIL" }); 93 instance.addExecuteExpectation("NAMESPACE", null, 94 new String[] { 95 "* NAMESPACE ((\"\" \"/\")) ((\"Other Users/\" \"/\")) NIL" 96 }); 97 141 98 ImapProtocol.NamespaceResponse result = instance.executeNamespace(); 142 assertNotNull( result);143 144 assertNotNull( result.personal);99 assertNotNull("Result", result); 100 101 assertNotNull("Personal", result.personal); 145 102 assertEquals(1, result.personal.length); 146 103 assertEquals("", result.personal[0].prefix); 147 104 assertEquals("/", result.personal[0].delimiter); 148 149 assertNotNull( result.other);105 106 assertNotNull("Other", result.other); 150 107 assertEquals(1, result.other.length); 151 108 assertEquals("Other Users/", result.other[0].prefix); 152 109 assertEquals("/", result.other[0].delimiter); 153 110 154 assertNull(result.shared); 155 111 assertNull("Shared", result.shared); 112 } catch (Throwable t) { 113 fail("Exception thrown during test: " + t.toString()); 114 t.printStackTrace(); 115 } 116 } 117 118 public void testExecuteNamespace2() { 119 try { 156 120 // Escaped delimiters: 157 121 // NAMESPACE (("" "\\")) (("Other Users\\" "\\")) (("Public Folders\\" "\\")) 158 instance.addExecuteExpectation( 159 "NAMESPACE", null, 160 new String[] { "* NAMESPACE ((\"\" \"\\\\\")) ((\"Other Users\\\\\" \"\\\\\")) ((\"Public Folders\\\\\" \"\\\\\"))" }); 161 result = instance.executeNamespace(); 162 assertNotNull(result); 163 164 assertNotNull(result.personal); 122 instance.addExecuteExpectation("NAMESPACE", null, 123 new String[] { 124 "* NAMESPACE ((\"\" \"\\\\\")) ((\"Other Users\\\\\" \"\\\\\")) ((\"Public Folders\\\\\" \"\\\\\"))" 125 }); 126 127 ImapProtocol.NamespaceResponse result = instance.executeNamespace(); 128 assertNotNull("Result", result); 129 130 assertNotNull("Personal", result.personal); 165 131 assertEquals(1, result.personal.length); 166 132 assertEquals("", result.personal[0].prefix); 167 133 assertEquals("\\", result.personal[0].delimiter); 168 169 assertNotNull( result.other);134 135 assertNotNull("Other", result.other); 170 136 assertEquals(1, result.other.length); 171 137 assertEquals("Other Users\\", result.other[0].prefix); 172 138 assertEquals("\\", result.other[0].delimiter); 173 174 assertNotNull( result.shared);139 140 assertNotNull("Shared ", result.shared); 175 141 assertEquals(1, result.shared.length); 176 142 assertEquals("Public Folders\\", result.shared[0].prefix); 177 143 assertEquals("\\", result.shared[0].delimiter); 178 144 } catch (Throwable t) { 179 fail("Exception thrown during test: " +t.toString());180 t.printStackTrace(); 181 } 182 } 183 145 fail("Exception thrown during test: " + t.toString()); 146 t.printStackTrace(); 147 } 148 } 149 184 150 public void testExecuteList1() { 185 151 try { 186 152 // Test top-level inbox 187 instance.addExecuteExpectation( 188 "LIST", "\"\" \"%\"",189 new String[] { "* LIST (\\HasChildren) \".\" \"INBOX\"" }); 153 instance.addExecuteExpectation("LIST", "\"\" \"%\"", 154 new String[] { "* LIST (\\HasChildren) \".\" \"INBOX\"" }); 155 190 156 Vector result = instance.executeList("", "%"); 191 157 assertNotNull(result); 192 158 assertEquals(1, result.size()); 193 159 assertTrue(result.elementAt(0) instanceof ImapProtocol.ListResponse); 194 ImapProtocol.ListResponse result1 = (ImapProtocol.ListResponse)result.elementAt(0); 195 160 161 ImapProtocol.ListResponse result1 = (ImapProtocol.ListResponse) result.elementAt(0); 162 196 163 assertTrue(result1.hasChildren); 197 164 assertTrue(result1.canSelect); … … 200 167 assertEquals("INBOX", result1.name); 201 168 } catch (Throwable t) { 202 fail("Exception thrown during test: " +t.toString());203 t.printStackTrace(); 204 } 205 } 206 169 fail("Exception thrown during test: " + t.toString()); 170 t.printStackTrace(); 171 } 172 } 173 207 174 public void testExecuteList2() { 208 175 try { 209 176 // Test subfolders of inbox 210 instance.addExecuteExpectation( 211 "LIST", "\"INBOX.\" \"%\"", 212 new String[] { 213 "* LIST (\\HasNoChildren) \".\" \"INBOX.Saved\"", 214 "* LIST (\\HasNoChildren) \".\" \"INBOX.Sent\"", 215 }); 216 177 instance.addExecuteExpectation("LIST", "\"INBOX.\" \"%\"", 178 new String[] { 179 "* LIST (\\HasNoChildren) \".\" \"INBOX.Saved\"", 180 "* LIST (\\HasNoChildren) \".\" \"INBOX.Sent\"", 181 }); 182 217 183 Vector result = instance.executeList("INBOX.", "%"); 218 184 assertNotNull(result); … … 220 186 assertTrue(result.elementAt(0) instanceof ImapProtocol.ListResponse); 221 187 assertTrue(result.elementAt(1) instanceof ImapProtocol.ListResponse); 222 ImapProtocol.ListResponse result1 = (ImapProtocol.ListResponse)result.elementAt(0); 223 ImapProtocol.ListResponse result2 = (ImapProtocol.ListResponse)result.elementAt(1); 188 189 ImapProtocol.ListResponse result1 = (ImapProtocol.ListResponse) result.elementAt(0); 190 ImapProtocol.ListResponse result2 = (ImapProtocol.ListResponse) result.elementAt(1); 224 191 225 192 assertTrue(!result1.hasChildren); … … 228 195 assertEquals(".", result1.delim); 229 196 assertEquals("INBOX.Saved", result1.name); 230 197 231 198 assertTrue(!result2.hasChildren); 232 199 assertTrue(result2.canSelect); … … 235 202 assertEquals("INBOX.Sent", result2.name); 236 203 } catch (Throwable t) { 237 fail("Exception thrown during test: " +t.toString());238 t.printStackTrace(); 239 } 240 } 241 204 fail("Exception thrown during test: " + t.toString()); 205 t.printStackTrace(); 206 } 207 } 208 242 209 public void testExecuteList3() { 243 210 try { 244 211 // Test inbox with a NIL delimiter 245 instance.addExecuteExpectation( 246 "LIST", "\"\" \"%\"",247 new String[] {248 "* LIST (\\HasNoChildren) NIL INBOX",249 "* LIST (\\HasNoChildren) \"/\" Sent"250 }); 212 instance.addExecuteExpectation("LIST", "\"\" \"%\"", 213 new String[] { 214 "* LIST (\\HasNoChildren) NIL INBOX", 215 "* LIST (\\HasNoChildren) \"/\" Sent" 216 }); 217 251 218 Vector result = instance.executeList("", "%"); 252 219 assertNotNull(result); … … 254 221 assertTrue(result.elementAt(0) instanceof ImapProtocol.ListResponse); 255 222 assertTrue(result.elementAt(1) instanceof ImapProtocol.ListResponse); 256 ImapProtocol.ListResponse result1 = (ImapProtocol.ListResponse)result.elementAt(0); 257 ImapProtocol.ListResponse result2 = (ImapProtocol.ListResponse)result.elementAt(1); 223 224 ImapProtocol.ListResponse result1 = (ImapProtocol.ListResponse) result.elementAt(0); 225 ImapProtocol.ListResponse result2 = (ImapProtocol.ListResponse) result.elementAt(1); 258 226 259 227 assertTrue(!result1.hasChildren); … … 262 230 assertEquals("", result1.delim); 263 231 assertEquals("INBOX", result1.name); 264 232 265 233 assertTrue(!result2.hasChildren); 266 234 assertTrue(result2.canSelect); … … 269 237 assertEquals("Sent", result2.name); 270 238 } catch (Throwable t) { 271 fail("Exception thrown during test: " +t.toString());272 t.printStackTrace(); 273 } 274 } 275 239 fail("Exception thrown during test: " + t.toString()); 240 t.printStackTrace(); 241 } 242 } 243 276 244 public void testExecuteList4() { 277 245 try { 278 246 // Test parameter in response 279 instance.addExecuteExpectation( 280 "LIST", "\"INBOX.\" \"%\"", 281 new String[] { 282 "* LIST (\\HasChildren) \".\" \"INBOX\"", 283 "* LIST (\\HasNoChildren) \".\" \"INBOX.Saved\"", 284 "* LIST (\\HasNoChildren) \".\" \"INBOX.Sent\"", 285 }); 286 247 instance.addExecuteExpectation("LIST", "\"INBOX.\" \"%\"", 248 new String[] { 249 "* LIST (\\HasChildren) \".\" \"INBOX\"", 250 "* LIST (\\HasNoChildren) \".\" \"INBOX.Saved\"", 251 "* LIST (\\HasNoChildren) \".\" \"INBOX.Sent\"", 252 }); 253 287 254 Vector result = instance.executeList("INBOX.", "%"); 288 255 assertNotNull(result); … … 290 257 assertTrue(result.elementAt(0) instanceof ImapProtocol.ListResponse); 291 258 assertTrue(result.elementAt(1) instanceof ImapProtocol.ListResponse); 292 ImapProtocol.ListResponse result1 = (ImapProtocol.ListResponse)result.elementAt(0); 293 ImapProtocol.ListResponse result2 = (ImapProtocol.ListResponse)result.elementAt(1); 259 260 ImapProtocol.ListResponse result1 = (ImapProtocol.ListResponse) result.elementAt(0); 261 ImapProtocol.ListResponse result2 = (ImapProtocol.ListResponse) result.elementAt(1); 294 262 295 263 assertTrue(!result1.hasChildren); … … 298 266 assertEquals(".", result1.delim); 299 267 assertEquals("INBOX.Saved", result1.name); 300 268 301 269 assertTrue(!result2.hasChildren); 302 270 assertTrue(result2.canSelect); … … 305 273 assertEquals("INBOX.Sent", result2.name); 306 274 } catch (Throwable t) { 307 fail("Exception thrown during test: " +t.toString());308 t.printStackTrace(); 309 } 310 } 311 275 fail("Exception thrown during test: " + t.toString()); 276 t.printStackTrace(); 277 } 278 } 279 312 280 public void testExecuteList5() { 313 281 try { 314 282 // Test escaped delimiter 315 instance.addExecuteExpectation( 316 "LIST", "\"INBOX\\\\\" \"%\"", 317 new String[] { 318 "* LIST (\\HasNoChildren) \"\\\\\" \"INBOX\\Saved\\Stuff\"", 319 "* LIST (\\HasNoChildren) \"\\\\\" \"INBOX\\Sent\"", 320 }); 321 283 instance.addExecuteExpectation("LIST", "\"INBOX\\\\\" \"%\"", 284 new String[] { 285 "* LIST (\\HasNoChildren) \"\\\\\" \"INBOX\\Saved\\Stuff\"", 286 "* LIST (\\HasNoChildren) \"\\\\\" \"INBOX\\Sent\"", 287 }); 288 322 289 Vector result = instance.executeList("INBOX\\", "%"); 323 290 assertNotNull(result); … … 325 292 assertTrue(result.elementAt(0) instanceof ImapProtocol.ListResponse); 326 293 assertTrue(result.elementAt(1) instanceof ImapProtocol.ListResponse); 327 ImapProtocol.ListResponse result1 = (ImapProtocol.ListResponse)result.elementAt(0); 328 ImapProtocol.ListResponse result2 = (ImapProtocol.ListResponse)result.elementAt(1); 294 295 ImapProtocol.ListResponse result1 = (ImapProtocol.ListResponse) result.elementAt(0); 296 ImapProtocol.ListResponse result2 = (ImapProtocol.ListResponse) result.elementAt(1); 329 297 330 298 assertTrue(!result1.hasChildren); … … 333 301 assertEquals("\\", result1.delim); 334 302 assertEquals("INBOX\\Saved\\Stuff", result1.name); 335 303 336 304 assertTrue(!result2.hasChildren); 337 305 assertTrue(result2.canSelect); … … 340 308 assertEquals("INBOX\\Sent", result2.name); 341 309 } catch (Throwable t) { 342 fail("Exception thrown during test: " +t.toString());343 t.printStackTrace(); 344 } 345 } 346 310 fail("Exception thrown during test: " + t.toString()); 311 t.printStackTrace(); 312 } 313 } 314 347 315 public void testExecuteList6() { 348 316 try { 349 317 // Test specified-length encoding for path name 350 instance.addExecuteExpectation( 351 "LIST", "\"2007\\\\\" \"%\"", 352 new String[] { 353 "* LIST (\\HasNoChildren) \"\\\\\" {12}", 354 "2007\\Q3-2007", 355 "* LIST (\\HasNoChildren) \"\\\\\" {12}", 356 "2007\\Q4-2007" 357 }); 318 instance.addExecuteExpectation("LIST", "\"2007\\\\\" \"%\"", 319 new String[] { 320 "* LIST (\\HasNoChildren) \"\\\\\" {12}", "2007\\Q3-2007", 321 "* LIST (\\HasNoChildren) \"\\\\\" {12}", "2007\\Q4-2007" 322 }); 358 323 359 324 Vector result = instance.executeList("2007\\", "%"); … … 362 327 assertTrue(result.elementAt(0) instanceof ImapProtocol.ListResponse); 363 328 assertTrue(result.elementAt(1) instanceof ImapProtocol.ListResponse); 364 ImapProtocol.ListResponse result1 = (ImapProtocol.ListResponse)result.elementAt(0); 365 ImapProtocol.ListResponse result2 = (ImapProtocol.ListResponse)result.elementAt(1); 329 330 ImapProtocol.ListResponse result1 = (ImapProtocol.ListResponse) result.elementAt(0); 331 ImapProtocol.ListResponse result2 = (ImapProtocol.ListResponse) result.elementAt(1); 366 332 367 333 assertTrue(!result1.hasChildren); … … 377 343 assertEquals("2007\\Q4-2007", result2.name); 378 344 } catch (Throwable t) { 379 fail("Exception thrown during test: " +t.toString());380 t.printStackTrace(); 381 } 382 } 383 345 fail("Exception thrown during test: " + t.toString()); 346 t.printStackTrace(); 347 } 348 } 349 384 350 public void testExecuteFetchEnvelope1() { 385 351 try { 386 instance.addExecuteExpectation( 387 "FETCH", "1:1 (FLAGS UID ENVELOPE)", 352 instance.addExecuteExpectation("FETCH", "1:1 (FLAGS UID ENVELOPE)", 388 353 new String[] { 389 354 "* 1 FETCH (FLAGS (\\Answered \\Seen) UID 10 " + … … 393 358 "((\"jim smith\" NIL \"jsmith\" \"scratch.test\")) " + 394 359 "((\"John Doe\" NIL \"jdoe\" \"generic.test\")) " + 395 "NIL NIL " + 396 "\"<200703121933.25327.jdoe@generic.test>\" " + 360 "NIL NIL " + "\"<200703121933.25327.jdoe@generic.test>\" " + 397 361 "\"<7b02460f0703121938sff23a05xd3c2a37dc6b9eb7d@mail.scratch.test>\"))" 398 362 }); 399 ImapProtocol.FetchEnvelopeResponse[] result = instance.executeFetchEnvelope(1, 1); 363 364 ImapProtocol.FetchEnvelopeResponse[] result = instance.executeFetchEnvelope(1, 365 1); 400 366 assertNotNull(result); 401 367 assertEquals(1, result.length); 402 368 assertNotNull(result[0]); 403 369 404 370 assertEquals(1, result[0].index); 405 371 assertEquals(10, result[0].uid); … … 411 377 assertTrue(!result[0].flags.flagged); 412 378 assertTrue(!result[0].flags.recent); 413 379 414 380 assertNotNull(result[0].envelope); 381 415 382 MessageEnvelope env = result[0].envelope; 416 383 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT-7")); … … 421 388 cal.set(Calendar.MINUTE, 38); 422 389 cal.set(Calendar.SECOND, 31); 423 assertEquals(StringParser.createDateString(cal.getTime()), StringParser.createDateString(env.date)); 424 390 assertEquals(StringParser.createDateString(cal.getTime()), 391 StringParser.createDateString(env.date)); 392 425 393 assertEquals("Re: Calm down! :-)", env.subject); 426 394 … … 429 397 assertNotNull(env.from[0]); 430 398 assertEquals("jim smith <jsmith@scratch.test>", env.from[0]); 431 399 432 400 assertNotNull(env.sender); 433 401 assertEquals(1, env.sender.length); … … 439 407 assertNotNull(env.replyTo[0]); 440 408 assertEquals("jim smith <jsmith@scratch.test>", env.replyTo[0]); 441 409 442 410 assertNotNull(env.to); 443 411 assertEquals(1, env.to.length); 444 412 assertNotNull(env.to[0]); 445 413 assertEquals("John Doe <jdoe@generic.test>", env.to[0]); 446 414 447 415 assertNull(env.cc); 448 416 assertNull(env.bcc); 449 417 450 418 assertEquals("<200703121933.25327.jdoe@generic.test>", env.inReplyTo); 451 assertEquals("<7b02460f0703121938sff23a05xd3c2a37dc6b9eb7d@mail.scratch.test>", env.messageId); 452 453 } catch (MailException e) { 454 fail("MailException thrown during test: "+e.toString()); 455 e.printStackTrace(); 456 } catch (IOException e) { 457 fail("IOException thrown during test: "+e.toString()); 458 e.printStackTrace(); 459 } 460 } 461 419 assertEquals("<7b02460f0703121938sff23a05xd3c2a37dc6b9eb7d@mail.scratch.test>", 420 env.messageId); 421 } catch (Throwable t) { 422 fail("Exception thrown during test: " + t.toString()); 423 t.printStackTrace(); 424 } 425 } 426 462 427 public void testExecuteFetchEnvelope2() { 463 428 try { 464 instance.addExecuteExpectation( 465 "FETCH", "1:1 (FLAGS UID ENVELOPE)", 429 instance.addExecuteExpectation("FETCH", "1:1 (FLAGS UID ENVELOPE)", 466 430 new String[] { 467 431  
