1 16 17 package org.apache.commons.net.pop3; 18 19 import junit.framework.TestCase; 20 import junit.framework.TestSuite; 21 22 import java.net.InetAddress ; 23 import java.io.IOException ; 24 import java.io.Reader ; 25 26 44 public class POP3ClientCommandsTest extends TestCase 45 { 46 POP3Client p = null; 47 48 String user = TestSetupParameters.user; 49 String emptyUser = TestSetupParameters.emptyuser; 50 String password = TestSetupParameters.password; 51 String mailhost = TestSetupParameters.mailhost; 52 53 56 public POP3ClientCommandsTest(String name) 57 { 58 super(name); 59 } 60 61 65 public static TestSuite suite() 66 { 67 return (new TestSuite(POP3ClientCommandsTest.class)); 68 } 69 70 private void reset() throws IOException 71 { 72 if (p == null) 74 { 75 } 77 else if (p.isConnected()) 78 { 79 p.disconnect(); 80 } 81 p = null; 82 p = new POP3Client(); 83 } 84 85 private void connect() throws Exception 86 { 87 p.connect(InetAddress.getByName(mailhost)); 88 assertTrue(p.isConnected()); 89 assertEquals(POP3.AUTHORIZATION_STATE, p.getState()); 90 } 91 92 private void login() throws Exception 93 { 94 assertTrue(p.login(user, password)); 95 assertEquals(POP3.TRANSACTION_STATE, p.getState()); 96 } 97 98 102 public void testNoopCommand() throws Exception 103 { 104 reset(); 105 connect(); 106 107 assertFalse(p.noop()); 109 110 login(); 112 assertTrue(p.noop()); 113 114 p.setState(POP3.UPDATE_STATE); 116 assertFalse(p.noop()); 117 } 118 119 123 public void testStatus() throws Exception 124 { 125 reset(); 126 connect(); 127 128 assertNull(p.status()); 130 131 login(); 133 POP3MessageInfo msg = p.status(); 134 assertTrue(msg.number > 0); 135 assertTrue(msg.size > 0); 136 assertNull(msg.identifier); 137 p.logout(); 138 139 reset(); 141 connect(); 142 assertTrue(p.login(emptyUser, password)); 143 POP3MessageInfo msg2 = p.status(); 144 assertTrue(msg2.number == 0); 145 assertTrue(msg2.size == 0); 146 assertNull(msg2.identifier); 147 p.logout(); 148 149 reset(); 151 connect(); 152 login(); 153 p.setState(POP3.UPDATE_STATE); 154 assertNull(p.status()); 155 } 156 157 161 public void testListMessagesOnFullMailbox() throws Exception 162 { 163 reset(); 164 connect(); 165 login(); 166 167 POP3MessageInfo[] msg = p.listMessages(); 168 assertTrue(msg.length > 0); 169 170 for(int i = 0; i < msg.length; i++) 171 { 172 assertNotNull(msg[i]); 173 assertTrue(msg[i].number == i + 1); 174 assertTrue(msg[i].size > 0); 175 assertNull(msg[i].identifier); 176 } 177 178 p.setState(POP3.UPDATE_STATE); 180 msg = p.listMessages(); 181 assertNull(msg); 182 } 183 184 188 public void testListMessageOnFullMailbox() throws Exception 189 { 190 reset(); 191 connect(); 192 login(); 193 194 POP3MessageInfo msg = p.listMessage(1); 196 assertNotNull(msg); 197 assertTrue(msg.number == 1); 198 assertTrue(msg.size > 0); 199 assertNull(msg.identifier); 200 201 msg = p.listMessage(0); 203 assertNull(msg); 204 205 msg = p.listMessage(100000); 207 assertNull(msg); 208 209 msg = p.listMessage(-2); 211 assertNull(msg); 212 213 p.setState(POP3.UPDATE_STATE); 215 msg = p.listMessage(1); 216 assertNull(msg); 217 } 218 219 223 public void testListMessagesOnEmptyMailbox() throws Exception 224 { 225 reset(); 226 connect(); 227 assertTrue(p.login(emptyUser, password)); 228 229 POP3MessageInfo[] msg = p.listMessages(); 230 assertTrue(msg.length == 0); 231 232 p.setState(POP3.UPDATE_STATE); 234 msg = p.listMessages(); 235 assertNull(msg); 236 } 237 238 242 public void testListMessageOnEmptyMailbox() throws Exception 243 { 244 reset(); 245 connect(); 246 assertTrue(p.login(emptyUser, password)); 247 248 POP3MessageInfo msg = p.listMessage(1); 250 assertNull(msg); 251 } 252 253 257 public void testListUniqueIDsOnFullMailbox() throws Exception 258 { 259 reset(); 260 connect(); 261 login(); 262 263 POP3MessageInfo[] msg = p.listUniqueIdentifiers(); 264 assertTrue(msg.length > 0); 265 266 for(int i = 0; i < msg.length; i++) 267 { 268 assertNotNull(msg[i]); 269 assertTrue(msg[i].number == i + 1); 270 assertNotNull(msg[i].identifier); 271 } 272 273 p.setState(POP3.UPDATE_STATE); 275 msg = p.listUniqueIdentifiers(); 276 assertNull(msg); 277 } 278 279 283 public void testListUniqueIDOnFullMailbox() throws Exception 284 { 285 reset(); 286 connect(); 287 login(); 288 289 POP3MessageInfo msg = p.listUniqueIdentifier(1); 291 assertNotNull(msg); 292 assertTrue(msg.number == 1); 293 assertNotNull(msg.identifier); 294 295 msg = p.listUniqueIdentifier(0); 297 assertNull(msg); 298 299 msg = p.listUniqueIdentifier(100000); 301 assertNull(msg); 302 303 msg = p.listUniqueIdentifier(-2); 305 assertNull(msg); 306 307 p.setState(POP3.UPDATE_STATE); 309 msg = p.listUniqueIdentifier(1); 310 assertNull(msg); 311 } 312 313 317 public void testListUniqueIDsOnEmptyMailbox() throws Exception 318 { 319 reset(); 320 connect(); 321 assertTrue(p.login(emptyUser, password)); 322 323 POP3MessageInfo[] msg = p.listUniqueIdentifiers(); 324 assertTrue(msg.length == 0); 325 326 p.setState(POP3.UPDATE_STATE); 328 msg = p.listUniqueIdentifiers(); 329 assertNull(msg); 330 } 331 332 336 public void testListUniqueIdentifierOnEmptyMailbox() throws Exception 337 { 338 reset(); 339 connect(); 340 assertTrue(p.login(emptyUser, password)); 341 342 POP3MessageInfo msg = p.listUniqueIdentifier(1); 344 assertNull(msg); 345 } 346 347 351 public void testRetrieveMessageOnFullMailbox() throws Exception 352 { 353 reset(); 354 connect(); 355 login(); 356 int reportedSize = 0; 357 int actualSize = 0; 358 359 POP3MessageInfo[] msg = p.listMessages(); 360 assertTrue(msg.length > 0); 361 362 for (int i = msg.length; i > 0; i--) 363 { 364 reportedSize = msg[i - 1].size; 365 Reader r = p.retrieveMessage(i); 366 assertNotNull(r); 367 368 int delaycount = 0; 369 if (!r.ready()) 370 { 371 Thread.sleep(500); 374 delaycount++; 375 if (delaycount == 4) 377 { 378 break; 379 } 380 } 381 while(r.ready()) 382 { 383 r.read(); 384 actualSize++; 385 } 386 assertTrue(actualSize >= reportedSize); 392 } 393 } 394 395 399 public void testRetrieveMessageOnEmptyMailbox() throws Exception 400 { 401 reset(); 402 connect(); 403 assertTrue(p.login(emptyUser, password)); 404 assertNull(p.retrieveMessage(1)); 405 } 406 407 411 public void testRetrieveMessageShouldFails() throws Exception 412 { 413 reset(); 414 connect(); 415 login(); 416 417 assertNull(p.retrieveMessage(0)); 419 420 assertNull(p.retrieveMessage(-2)); 422 423 assertNull(p.retrieveMessage(100000)); 425 426 p.setState(POP3.UPDATE_STATE); 428 assertNull(p.retrieveMessage(1)); 429 } 430 431 435 public void testRetrieveMessageTopOnFullMailbox() throws Exception 436 { 437 reset(); 438 connect(); 439 login(); 440 int numLines = 10; 441 442 POP3MessageInfo[] msg = p.listMessages(); 443 assertTrue(msg.length > 0); 444 445 for (int i = 0; i < msg.length; i++) 446 { 447 Reader r = p.retrieveMessageTop(i + 1, numLines); 448 assertNotNull(r); 449 r.close(); 450 r = null; 451 } 452 } 453 454 458 public void testRetrieveOverSizedMessageTopOnFullMailbox() throws Exception 459 { 460 reset(); 461 connect(); 462 login(); 463 int reportedSize = 0; 464 int actualSize = 0; 465 466 POP3MessageInfo msg = p.listMessage(1); 467 reportedSize = msg.size; 468 469 Reader r = p.retrieveMessageTop(1, 100000); 471 assertNotNull(r); 472 473 int delaycount = 0; 474 while(!r.ready()) 475 { 476 Thread.sleep(500); 479 delaycount++; 480 if (delaycount == 4) 482 { 483 break; 484 } 485 } 486 while(r.ready()) 487 { 488 r.read(); 489 actualSize++; 490 } 491 assertTrue(actualSize >= reportedSize); 497 } 498 499 503 public void testRetrieveMessageTopOnEmptyMailbox() throws Exception 504 { 505 reset(); 506 connect(); 507 assertTrue(p.login(emptyUser, password)); 508 assertNull(p.retrieveMessageTop(1, 10)); 509 } 510 511 515 public void testRetrieveMessageTopShouldFails() throws Exception 516 { 517 reset(); 518 connect(); 519 login(); 520 521 assertNull(p.retrieveMessageTop(0, 10)); 523 524 assertNull(p.retrieveMessageTop(-2, 10)); 526 527 assertNull(p.retrieveMessageTop(100000, 10)); 529 530 p.setState(POP3.UPDATE_STATE); 532 assertNull(p.retrieveMessageTop(1, 10)); 533 } 534 535 public void testDeleteWithReset() throws Exception 536 { 537 reset(); 538 connect(); 539 login(); 540 POP3MessageInfo[] msg = p.listMessages(); 542 int numMessages = msg.length; 543 int numDeleted = 0; 544 545 for (int i = 0; i < numMessages - 1; i ++) 547 { 548 p.deleteMessage(i + 1); 549 numDeleted++; 550 } 551 assertEquals(numMessages, (numDeleted + 1)); 553 554 p.reset(); 556 557 p.logout(); 559 reset(); 560 connect(); 561 login(); 562 563 msg = p.listMessages(); 566 assertEquals(numMessages, msg.length); 567 } 568 569 public void testDelete() throws Exception 570 { 571 reset(); 572 connect(); 573 login(); 574 POP3MessageInfo[] msg = p.listMessages(); 576 int numMessages = msg.length; 577 int numDeleted = 0; 578 579 for (int i = 0; i < numMessages - 3; i ++) 581 { 582 p.deleteMessage(i + 1); 583 numDeleted++; 584 } 585 assertEquals(numMessages, (numDeleted + 3)); 587 588 p.logout(); 590 reset(); 591 connect(); 592 login(); 593 594 msg = p.listMessages(); 597 assertEquals(numMessages - numDeleted, msg.length); 598 } 599 600 public void testResetAndDeleteShouldFails() throws Exception 601 { 602 reset(); 603 connect(); 604 login(); 605 606 p.setState(POP3.UPDATE_STATE); 607 assertFalse(p.reset()); 608 609 assertFalse(p.deleteMessage(1)); 610 } 611 } 612 | Popular Tags |