001: /**
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package org.apache.commons.net.pop3;
016:
017: import junit.framework.TestCase;
018: import junit.framework.TestSuite;
019:
020: import java.net.InetAddress;
021: import java.io.IOException;
022: import java.io.Reader;
023:
024: /**
025: * @author <a href="mailto:commons-dev@apache.org">[Net]</a>
026: * @version $Id: POP3ClientCommandsTest.java 165675 2005-05-02 20:09:55Z rwinston $
027: *
028: * The POP3* tests all presume the existence of the following parameters:
029: * mailserver: localhost (running on the default port 110)
030: * account: username=test; password=password
031: * account: username=alwaysempty; password=password.
032: * mail: At least four emails in the test account and zero emails
033: * in the alwaysempty account
034: *
035: * If this won't work for you, you can change these parameters in the
036: * TestSetupParameters class.
037: *
038: * The tests were originally run on a default installation of James.
039: * Your mileage may vary based on the POP3 server you run the tests against.
040: * Some servers are more standards-compliant than others.
041: */
042: public class POP3ClientCommandsTest extends TestCase {
043: POP3Client p = null;
044:
045: String user = TestSetupParameters.user;
046: String emptyUser = TestSetupParameters.emptyuser;
047: String password = TestSetupParameters.password;
048: String mailhost = TestSetupParameters.mailhost;
049:
050: /**
051: *
052: */
053: public POP3ClientCommandsTest(String name) {
054: super (name);
055: }
056:
057: /**
058: * Method suite.
059: * @return TestSuite
060: */
061: public static TestSuite suite() {
062: return (new TestSuite(POP3ClientCommandsTest.class));
063: }
064:
065: private void reset() throws IOException {
066: //Case where this is the first time reset is called
067: if (p == null) {
068: //Do nothing
069: } else if (p.isConnected()) {
070: p.disconnect();
071: }
072: p = null;
073: p = new POP3Client();
074: }
075:
076: private void connect() throws Exception {
077: p.connect(InetAddress.getByName(mailhost));
078: assertTrue(p.isConnected());
079: assertEquals(POP3.AUTHORIZATION_STATE, p.getState());
080: }
081:
082: private void login() throws Exception {
083: assertTrue(p.login(user, password));
084: assertEquals(POP3.TRANSACTION_STATE, p.getState());
085: }
086:
087: /**
088: *
089: *
090: */
091: public void testNoopCommand() throws Exception {
092: reset();
093: connect();
094:
095: //Should fail before authorization
096: assertFalse(p.noop());
097:
098: //Should pass in transaction state
099: login();
100: assertTrue(p.noop());
101:
102: //Should fail in update state
103: p.setState(POP3.UPDATE_STATE);
104: assertFalse(p.noop());
105: }
106:
107: /**
108: *
109: *
110: */
111: public void testStatus() throws Exception {
112: reset();
113: connect();
114:
115: //Should fail in authorization state
116: assertNull(p.status());
117:
118: //Should pass on a mailbox with mail in it
119: login();
120: POP3MessageInfo msg = p.status();
121: assertTrue(msg.number > 0);
122: assertTrue(msg.size > 0);
123: assertNull(msg.identifier);
124: p.logout();
125:
126: //Should also pass on a mailbox with no mail in it
127: reset();
128: connect();
129: assertTrue(p.login(emptyUser, password));
130: POP3MessageInfo msg2 = p.status();
131: assertTrue(msg2.number == 0);
132: assertTrue(msg2.size == 0);
133: assertNull(msg2.identifier);
134: p.logout();
135:
136: //Should fail in the 'update' state
137: reset();
138: connect();
139: login();
140: p.setState(POP3.UPDATE_STATE);
141: assertNull(p.status());
142: }
143:
144: /**
145: *
146: *
147: */
148: public void testListMessagesOnFullMailbox() throws Exception {
149: reset();
150: connect();
151: login();
152:
153: POP3MessageInfo[] msg = p.listMessages();
154: assertTrue(msg.length > 0);
155:
156: for (int i = 0; i < msg.length; i++) {
157: assertNotNull(msg[i]);
158: assertTrue(msg[i].number == i + 1);
159: assertTrue(msg[i].size > 0);
160: assertNull(msg[i].identifier);
161: }
162:
163: //Now test from the update state
164: p.setState(POP3.UPDATE_STATE);
165: msg = p.listMessages();
166: assertNull(msg);
167: }
168:
169: /**
170: *
171: *
172: */
173: public void testListMessageOnFullMailbox() throws Exception {
174: reset();
175: connect();
176: login();
177:
178: //The first message is always at index 1
179: POP3MessageInfo msg = p.listMessage(1);
180: assertNotNull(msg);
181: assertTrue(msg.number == 1);
182: assertTrue(msg.size > 0);
183: assertNull(msg.identifier);
184:
185: //Now retrieve a message from index 0
186: msg = p.listMessage(0);
187: assertNull(msg);
188:
189: //Now retrieve a msg that is not there
190: msg = p.listMessage(100000);
191: assertNull(msg);
192:
193: //Now retrieve a msg with a negative index
194: msg = p.listMessage(-2);
195: assertNull(msg);
196:
197: //Now try to get a valid message from the update state
198: p.setState(POP3.UPDATE_STATE);
199: msg = p.listMessage(1);
200: assertNull(msg);
201: }
202:
203: /**
204: *
205: *
206: */
207: public void testListMessagesOnEmptyMailbox() throws Exception {
208: reset();
209: connect();
210: assertTrue(p.login(emptyUser, password));
211:
212: POP3MessageInfo[] msg = p.listMessages();
213: assertTrue(msg.length == 0);
214:
215: //Now test from the update state
216: p.setState(POP3.UPDATE_STATE);
217: msg = p.listMessages();
218: assertNull(msg);
219: }
220:
221: /**
222: *
223: *
224: */
225: public void testListMessageOnEmptyMailbox() throws Exception {
226: reset();
227: connect();
228: assertTrue(p.login(emptyUser, password));
229:
230: //The first message is always at index 1
231: POP3MessageInfo msg = p.listMessage(1);
232: assertNull(msg);
233: }
234:
235: /**
236: *
237: *
238: */
239: public void testListUniqueIDsOnFullMailbox() throws Exception {
240: reset();
241: connect();
242: login();
243:
244: POP3MessageInfo[] msg = p.listUniqueIdentifiers();
245: assertTrue(msg.length > 0);
246:
247: for (int i = 0; i < msg.length; i++) {
248: assertNotNull(msg[i]);
249: assertTrue(msg[i].number == i + 1);
250: assertNotNull(msg[i].identifier);
251: }
252:
253: //Now test from the update state
254: p.setState(POP3.UPDATE_STATE);
255: msg = p.listUniqueIdentifiers();
256: assertNull(msg);
257: }
258:
259: /**
260: *
261: *
262: */
263: public void testListUniqueIDOnFullMailbox() throws Exception {
264: reset();
265: connect();
266: login();
267:
268: //The first message is always at index 1
269: POP3MessageInfo msg = p.listUniqueIdentifier(1);
270: assertNotNull(msg);
271: assertTrue(msg.number == 1);
272: assertNotNull(msg.identifier);
273:
274: //Now retrieve a message from index 0
275: msg = p.listUniqueIdentifier(0);
276: assertNull(msg);
277:
278: //Now retrieve a msg that is not there
279: msg = p.listUniqueIdentifier(100000);
280: assertNull(msg);
281:
282: //Now retrieve a msg with a negative index
283: msg = p.listUniqueIdentifier(-2);
284: assertNull(msg);
285:
286: //Now try to get a valid message from the update state
287: p.setState(POP3.UPDATE_STATE);
288: msg = p.listUniqueIdentifier(1);
289: assertNull(msg);
290: }
291:
292: /**
293: *
294: *
295: */
296: public void testListUniqueIDsOnEmptyMailbox() throws Exception {
297: reset();
298: connect();
299: assertTrue(p.login(emptyUser, password));
300:
301: POP3MessageInfo[] msg = p.listUniqueIdentifiers();
302: assertTrue(msg.length == 0);
303:
304: //Now test from the update state
305: p.setState(POP3.UPDATE_STATE);
306: msg = p.listUniqueIdentifiers();
307: assertNull(msg);
308: }
309:
310: /**
311: *
312: *
313: */
314: public void testListUniqueIdentifierOnEmptyMailbox()
315: throws Exception {
316: reset();
317: connect();
318: assertTrue(p.login(emptyUser, password));
319:
320: //The first message is always at index 1
321: POP3MessageInfo msg = p.listUniqueIdentifier(1);
322: assertNull(msg);
323: }
324:
325: /**
326: *
327: *
328: */
329: public void testRetrieveMessageOnFullMailbox() throws Exception {
330: reset();
331: connect();
332: login();
333: int reportedSize = 0;
334: int actualSize = 0;
335:
336: POP3MessageInfo[] msg = p.listMessages();
337: assertTrue(msg.length > 0);
338:
339: for (int i = msg.length; i > 0; i--) {
340: reportedSize = msg[i - 1].size;
341: Reader r = p.retrieveMessage(i);
342: assertNotNull(r);
343:
344: int delaycount = 0;
345: if (!r.ready()) {
346: //Give the reader time to get the message
347: //from the server
348: Thread.sleep(500);
349: delaycount++;
350: //but don't wait too long
351: if (delaycount == 4) {
352: break;
353: }
354: }
355: while (r.ready()) {
356: r.read();
357: actualSize++;
358: }
359: //Due to variations in line termination
360: //on different platforms, the actual
361: //size may vary slightly. On Win2KPro, the
362: //actual size is 2 bytes larger than the reported
363: //size.
364: assertTrue(actualSize >= reportedSize);
365: }
366: }
367:
368: /**
369: *
370: *
371: */
372: public void testRetrieveMessageOnEmptyMailbox() throws Exception {
373: reset();
374: connect();
375: assertTrue(p.login(emptyUser, password));
376: assertNull(p.retrieveMessage(1));
377: }
378:
379: /**
380: *
381: *
382: */
383: public void testRetrieveMessageShouldFails() throws Exception {
384: reset();
385: connect();
386: login();
387:
388: //Try to get message 0
389: assertNull(p.retrieveMessage(0));
390:
391: //Try to get a negative message
392: assertNull(p.retrieveMessage(-2));
393:
394: //Try to get a message that is not there
395: assertNull(p.retrieveMessage(100000));
396:
397: //Change states and try to get a valid message
398: p.setState(POP3.UPDATE_STATE);
399: assertNull(p.retrieveMessage(1));
400: }
401:
402: /**
403: *
404: *
405: */
406: public void testRetrieveMessageTopOnFullMailbox() throws Exception {
407: reset();
408: connect();
409: login();
410: int numLines = 10;
411:
412: POP3MessageInfo[] msg = p.listMessages();
413: assertTrue(msg.length > 0);
414:
415: for (int i = 0; i < msg.length; i++) {
416: Reader r = p.retrieveMessageTop(i + 1, numLines);
417: assertNotNull(r);
418: r.close();
419: r = null;
420: }
421: }
422:
423: /**
424: *
425: *
426: */
427: public void testRetrieveOverSizedMessageTopOnFullMailbox()
428: throws Exception {
429: reset();
430: connect();
431: login();
432: int reportedSize = 0;
433: int actualSize = 0;
434:
435: POP3MessageInfo msg = p.listMessage(1);
436: reportedSize = msg.size;
437:
438: //Now try to retrieve more lines than exist in the message
439: Reader r = p.retrieveMessageTop(1, 100000);
440: assertNotNull(r);
441:
442: int delaycount = 0;
443: while (!r.ready()) {
444: //Give the reader time to get the message
445: //from the server
446: Thread.sleep(500);
447: delaycount++;
448: //but don't wait too long
449: if (delaycount == 4) {
450: break;
451: }
452: }
453: while (r.ready()) {
454: r.read();
455: actualSize++;
456: }
457: //Due to variations in line termination
458: //on different platforms, the actual
459: //size may vary slightly. On Win2KPro, the
460: //actual size is 2 bytes larger than the reported
461: //size.
462: assertTrue(actualSize >= reportedSize);
463: }
464:
465: /**
466: *
467: *
468: */
469: public void testRetrieveMessageTopOnEmptyMailbox() throws Exception {
470: reset();
471: connect();
472: assertTrue(p.login(emptyUser, password));
473: assertNull(p.retrieveMessageTop(1, 10));
474: }
475:
476: /**
477: *
478: *
479: */
480: public void testRetrieveMessageTopShouldFails() throws Exception {
481: reset();
482: connect();
483: login();
484:
485: //Try to get message 0
486: assertNull(p.retrieveMessageTop(0, 10));
487:
488: //Try to get a negative message
489: assertNull(p.retrieveMessageTop(-2, 10));
490:
491: //Try to get a message that is not there
492: assertNull(p.retrieveMessageTop(100000, 10));
493:
494: //Change states and try to get a valid message
495: p.setState(POP3.UPDATE_STATE);
496: assertNull(p.retrieveMessageTop(1, 10));
497: }
498:
499: public void testDeleteWithReset() throws Exception {
500: reset();
501: connect();
502: login();
503: //Get the original number of messages
504: POP3MessageInfo[] msg = p.listMessages();
505: int numMessages = msg.length;
506: int numDeleted = 0;
507:
508: //Now delete some and logout
509: for (int i = 0; i < numMessages - 1; i++) {
510: p.deleteMessage(i + 1);
511: numDeleted++;
512: }
513: //Check to see that they are marked as deleted
514: assertEquals(numMessages, (numDeleted + 1));
515:
516: //Now reset to unmark the messages as deleted
517: p.reset();
518:
519: //Logout and come back in
520: p.logout();
521: reset();
522: connect();
523: login();
524:
525: //Get the new number of messages, because of
526: //reset, new number should match old number
527: msg = p.listMessages();
528: assertEquals(numMessages, msg.length);
529: }
530:
531: public void testDelete() throws Exception {
532: reset();
533: connect();
534: login();
535: //Get the original number of messages
536: POP3MessageInfo[] msg = p.listMessages();
537: int numMessages = msg.length;
538: int numDeleted = 0;
539:
540: //Now delete some and logout
541: for (int i = 0; i < numMessages - 3; i++) {
542: p.deleteMessage(i + 1);
543: numDeleted++;
544: }
545: //Check to see that they are marked as deleted
546: assertEquals(numMessages, (numDeleted + 3));
547:
548: //Logout and come back in
549: p.logout();
550: reset();
551: connect();
552: login();
553:
554: //Get the new number of messages, because of
555: //reset, new number should match old number
556: msg = p.listMessages();
557: assertEquals(numMessages - numDeleted, msg.length);
558: }
559:
560: public void testResetAndDeleteShouldFails() throws Exception {
561: reset();
562: connect();
563: login();
564:
565: p.setState(POP3.UPDATE_STATE);
566: assertFalse(p.reset());
567:
568: assertFalse(p.deleteMessage(1));
569: }
570: }
|