001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.net.pop3; 018 019 import junit.framework.TestCase; 020 import junit.framework.TestSuite; 021 022 import java.net.InetAddress; 023 import java.io.IOException; 024 import java.io.Reader; 025 026 /** 027 * @author <a href="mailto:commons-dev@apache.org">[Net]</a> 028 * @version $Id: POP3ClientCommandsTest.java 631313 2008-02-26 17:41:09Z niallp $ 029 * 030 * The POP3* tests all presume the existence of the following parameters: 031 * mailserver: localhost (running on the default port 110) 032 * account: username=test; password=password 033 * account: username=alwaysempty; password=password. 034 * mail: At least four emails in the test account and zero emails 035 * in the alwaysempty account 036 * 037 * If this won't work for you, you can change these parameters in the 038 * TestSetupParameters class. 039 * 040 * The tests were originally run on a default installation of James. 041 * Your mileage may vary based on the POP3 server you run the tests against. 042 * Some servers are more standards-compliant than others. 043 */ 044 public class POP3ClientCommandsTest extends TestCase 045 { 046 POP3Client p = null; 047 048 String user = TestSetupParameters.user; 049 String emptyUser = TestSetupParameters.emptyuser; 050 String password = TestSetupParameters.password; 051 String mailhost = TestSetupParameters.mailhost; 052 053 /** 054 * 055 */ 056 public POP3ClientCommandsTest(String name) 057 { 058 super(name); 059 } 060 061 /** 062 * Method suite. 063 * @return TestSuite 064 */ 065 public static TestSuite suite() 066 { 067 return (new TestSuite(POP3ClientCommandsTest.class)); 068 } 069 070 private void reset() throws IOException 071 { 072 //Case where this is the first time reset is called 073 if (p == null) 074 { 075 //Do nothing 076 } 077 else if (p.isConnected()) 078 { 079 p.disconnect(); 080 } 081 p = null; 082 p = new POP3Client(); 083 } 084 085 private void connect() throws Exception 086 { 087 p.connect(InetAddress.getByName(mailhost)); 088 assertTrue(p.isConnected()); 089 assertEquals(POP3.AUTHORIZATION_STATE, p.getState()); 090 } 091 092 private void login() throws Exception 093 { 094 assertTrue(p.login(user, password)); 095 assertEquals(POP3.TRANSACTION_STATE, p.getState()); 096 } 097 098 /** 099 * 100 * 101 */ 102 public void testNoopCommand() throws Exception 103 { 104 reset(); 105 connect(); 106 107 //Should fail before authorization 108 assertFalse(p.noop()); 109 110 //Should pass in transaction state 111 login(); 112 assertTrue(p.noop()); 113 114 //Should fail in update state 115 p.setState(POP3.UPDATE_STATE); 116 assertFalse(p.noop()); 117 } 118 119 /** 120 * 121 * 122 */ 123 public void testStatus() throws Exception 124 { 125 reset(); 126 connect(); 127 128 //Should fail in authorization state 129 assertNull(p.status()); 130 131 //Should pass on a mailbox with mail in it 132 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 //Should also pass on a mailbox with no mail in it 140 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 //Should fail in the 'update' state 150 reset(); 151 connect(); 152 login(); 153 p.setState(POP3.UPDATE_STATE); 154 assertNull(p.status()); 155 } 156 157 /** 158 * 159 * 160 */ 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 //Now test from the update state 179 p.setState(POP3.UPDATE_STATE); 180 msg = p.listMessages(); 181 assertNull(msg); 182 } 183 184 /** 185 * 186 * 187 */ 188 public void testListMessageOnFullMailbox() throws Exception 189 { 190 reset(); 191 connect(); 192 login(); 193 194 //The first message is always at index 1 195 POP3MessageInfo msg = p.listMessage(1); 196 assertNotNull(msg); 197 assertTrue(msg.number == 1); 198 assertTrue(msg.size > 0); 199 assertNull(msg.identifier); 200 201 //Now retrieve a message from index 0 202 msg = p.listMessage(0); 203 assertNull(msg); 204 205 //Now retrieve a msg that is not there 206 msg = p.listMessage(100000); 207 assertNull(msg); 208 209 //Now retrieve a msg with a negative index 210 msg = p.listMessage(-2); 211 assertNull(msg); 212 213 //Now try to get a valid message from the update state 214 p.setState(POP3.UPDATE_STATE); 215 msg = p.listMessage(1); 216 assertNull(msg); 217 } 218 219 /** 220 * 221 * 222 */ 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 //Now test from the update state 233 p.setState(POP3.UPDATE_STATE); 234 msg = p.listMessages(); 235 assertNull(msg); 236 } 237 238 /** 239 * 240 * 241 */ 242 public void testListMessageOnEmptyMailbox() throws Exception 243 { 244 reset(); 245 connect(); 246 assertTrue(p.login(emptyUser, password)); 247 248 //The first message is always at index 1 249 POP3MessageInfo msg = p.listMessage(1); 250 assertNull(msg); 251 } 252 253 /** 254 * 255 * 256 */ 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 //Now test from the update state 274 p.setState(POP3.UPDATE_STATE); 275 msg = p.listUniqueIdentifiers(); 276 assertNull(msg); 277 } 278 279 /** 280 * 281 * 282 */ 283 public void testListUniqueIDOnFullMailbox() throws Exception 284 { 285 reset(); 286 connect(); 287 login(); 288 289 //The first message is always at index 1 290 POP3MessageInfo msg = p.listUniqueIdentifier(1); 291 assertNotNull(msg); 292 assertTrue(msg.number == 1); 293 assertNotNull(msg.identifier); 294 295 //Now retrieve a message from index 0 296 msg = p.listUniqueIdentifier(0); 297 assertNull(msg); 298 299 //Now retrieve a msg that is not there 300 msg = p.listUniqueIdentifier(100000); 301 assertNull(msg); 302 303 //Now retrieve a msg with a negative index 304 msg = p.listUniqueIdentifier(-2); 305 assertNull(msg); 306 307 //Now try to get a valid message from the update state 308 p.setState(POP3.UPDATE_STATE); 309 msg = p.listUniqueIdentifier(1); 310 assertNull(msg); 311 } 312 313 /** 314 * 315 * 316 */ 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 //Now test from the update state 327 p.setState(POP3.UPDATE_STATE); 328 msg = p.listUniqueIdentifiers(); 329 assertNull(msg); 330 } 331 332 /** 333 * 334 * 335 */ 336 public void testListUniqueIdentifierOnEmptyMailbox() throws Exception 337 { 338 reset(); 339 connect(); 340 assertTrue(p.login(emptyUser, password)); 341 342 //The first message is always at index 1 343 POP3MessageInfo msg = p.listUniqueIdentifier(1); 344 assertNull(msg); 345 } 346 347 /** 348 * 349 * 350 */ 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 //Give the reader time to get the message 372 //from the server 373 Thread.sleep(500); 374 delaycount++; 375 //but don't wait too long 376 if (delaycount == 4) 377 { 378 break; 379 } 380 } 381 while(r.ready()) 382 { 383 r.read(); 384 actualSize++; 385 } 386 //Due to variations in line termination 387 //on different platforms, the actual 388 //size may vary slightly. On Win2KPro, the 389 //actual size is 2 bytes larger than the reported 390 //size. 391 assertTrue(actualSize >= reportedSize); 392 } 393 } 394 395 /** 396 * 397 * 398 */ 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 /** 408 * 409 * 410 */ 411 public void testRetrieveMessageShouldFails() throws Exception 412 { 413 reset(); 414 connect(); 415 login(); 416 417 //Try to get message 0 418 assertNull(p.retrieveMessage(0)); 419 420 //Try to get a negative message 421 assertNull(p.retrieveMessage(-2)); 422 423 //Try to get a message that is not there 424 assertNull(p.retrieveMessage(100000)); 425 426 //Change states and try to get a valid message 427 p.setState(POP3.UPDATE_STATE); 428 assertNull(p.retrieveMessage(1)); 429 } 430 431 /** 432 * 433 * 434 */ 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 /** 455 * 456 * 457 */ 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 //Now try to retrieve more lines than exist in the message 470 Reader r = p.retrieveMessageTop(1, 100000); 471 assertNotNull(r); 472 473 int delaycount = 0; 474 while(!r.ready()) 475 { 476 //Give the reader time to get the message 477 //from the server 478 Thread.sleep(500); 479 delaycount++; 480 //but don't wait too long 481 if (delaycount == 4) 482 { 483 break; 484 } 485 } 486 while(r.ready()) 487 { 488 r.read(); 489 actualSize++; 490 } 491 //Due to variations in line termination 492 //on different platforms, the actual 493 //size may vary slightly. On Win2KPro, the 494 //actual size is 2 bytes larger than the reported 495 //size. 496 assertTrue(actualSize >= reportedSize); 497 } 498 499 /** 500 * 501 * 502 */ 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 /** 512 * 513 * 514 */ 515 public void testRetrieveMessageTopShouldFails() throws Exception 516 { 517 reset(); 518 connect(); 519 login(); 520 521 //Try to get message 0 522 assertNull(p.retrieveMessageTop(0, 10)); 523 524 //Try to get a negative message 525 assertNull(p.retrieveMessageTop(-2, 10)); 526 527 //Try to get a message that is not there 528 assertNull(p.retrieveMessageTop(100000, 10)); 529 530 //Change states and try to get a valid message 531 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 //Get the original number of messages 541 POP3MessageInfo[] msg = p.listMessages(); 542 int numMessages = msg.length; 543 int numDeleted = 0; 544 545 //Now delete some and logout 546 for (int i = 0; i < numMessages - 1; i ++) 547 { 548 p.deleteMessage(i + 1); 549 numDeleted++; 550 } 551 //Check to see that they are marked as deleted 552 assertEquals(numMessages, (numDeleted + 1)); 553 554 //Now reset to unmark the messages as deleted 555 p.reset(); 556 557 //Logout and come back in 558 p.logout(); 559 reset(); 560 connect(); 561 login(); 562 563 //Get the new number of messages, because of 564 //reset, new number should match old number 565 msg = p.listMessages(); 566 assertEquals(numMessages, msg.length); 567 } 568 569 public void testDelete() throws Exception 570 { 571 reset(); 572 connect(); 573 login(); 574 //Get the original number of messages 575 POP3MessageInfo[] msg = p.listMessages(); 576 int numMessages = msg.length; 577 int numDeleted = 0; 578 579 //Now delete some and logout 580 for (int i = 0; i < numMessages - 3; i ++) 581 { 582 p.deleteMessage(i + 1); 583 numDeleted++; 584 } 585 //Check to see that they are marked as deleted 586 assertEquals(numMessages, (numDeleted + 3)); 587 588 //Logout and come back in 589 p.logout(); 590 reset(); 591 connect(); 592 login(); 593 594 //Get the new number of messages, because of 595 //reset, new number should match old number 596 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 }