001: /**
002: *
003: * Java FTP client library.
004: *
005: * Copyright (C) 2000 Enterprise Distributed Technologies Ltd
006: *
007: * www.enterprisedt.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: *
023: * Bug fixes, suggestions and comments should be should posted on
024: * http://www.enterprisedt.com/forums/index.php
025: *
026: * Change Log:
027: *
028: * $Log: TestTransfer.java,v $
029: * Revision 1.14 2007-12-18 07:55:50 bruceb
030: * add finally
031: *
032: * Revision 1.13 2007/04/21 04:14:12 bruceb
033: * test unix text files transferred
034: *
035: * Revision 1.12 2007/02/26 07:14:34 bruceb
036: * make progress monitor public
037: *
038: * Revision 1.11 2007/02/06 07:23:40 bruceb
039: * testNoLocalFilename()
040: *
041: * Revision 1.10 2007/01/10 02:39:11 bruceb
042: * added testTransferUnique
043: *
044: * Revision 1.9 2006/10/11 08:59:54 hans
045: * Organised imports.
046: *
047: * Revision 1.8 2005/10/10 20:43:39 bruceb
048: * append now in FTPClientInterface
049: *
050: * Revision 1.7 2005/07/15 17:30:06 bruceb
051: * rework of unit testing structure
052: *
053: * Revision 1.6 2005/06/03 11:27:05 bruceb
054: * comment update
055: *
056: * Revision 1.5 2004/08/31 10:44:49 bruceb
057: * minor tweaks re compile warnings
058: *
059: * Revision 1.4 2004/05/01 17:05:43 bruceb
060: * Logger stuff added
061: *
062: * Revision 1.3 2003/11/03 21:18:51 bruceb
063: * added test of progress callback
064: *
065: * Revision 1.2 2003/05/31 14:54:05 bruceb
066: * cleaned up unused imports
067: *
068: * Revision 1.1 2002/11/19 22:00:15 bruceb
069: * New JUnit test cases
070: *
071: *
072: */package com.enterprisedt.net.ftp.test;
073:
074: import java.io.ByteArrayInputStream;
075: import java.io.ByteArrayOutputStream;
076: import java.io.File;
077: import java.io.FileNotFoundException;
078: import java.io.IOException;
079:
080: import junit.framework.Test;
081: import junit.framework.TestSuite;
082:
083: import com.enterprisedt.net.ftp.FTPException;
084: import com.enterprisedt.net.ftp.FTPProgressMonitor;
085: import com.enterprisedt.net.ftp.FTPTransferType;
086:
087: /**
088: * Test get'ing and put'ing of remote files in various ways
089: *
090: * @author Bruce Blackshaw
091: * @version $Revision: 1.14 $
092: */
093: public class TestTransfer extends FTPTestCase {
094:
095: /**
096: * Revision control id
097: */
098: public static String cvsId = "@(#)$Id: TestTransfer.java,v 1.14 2007-12-18 07:55:50 bruceb Exp $";
099:
100: /**
101: * Get name of log file
102: *
103: * @return name of file to log to
104: */
105: protected String getLogName() {
106: return "TestTransfer.log";
107: }
108:
109: /**
110: * Test transfering a binary file
111: */
112: public void testTransferBinary() throws Exception {
113:
114: log.info("testTransferBinary()");
115:
116: try {
117:
118: connect();
119:
120: // monitor transfer progress
121: ftp.setProgressMonitor(new TestProgressMonitor(), 250000);
122:
123: // move to test directory
124: ftp.chdir(testdir);
125: ftp.setType(FTPTransferType.BINARY);
126:
127: // put to a random filename
128: String filename = generateRandomFilename();
129: ftp.put(localDataDir + localBinaryFile, filename);
130:
131: // get it back
132: ftp.get(localDataDir + filename, filename);
133:
134: // delete remote file
135: ftp.delete(filename);
136: try {
137: ftp.modtime(filename);
138: fail(filename + " should not be found");
139: } catch (IOException ex) {
140: log.info("Expected exception: " + ex.getMessage());
141: } catch (FTPException ex) {
142: log.info("Expected exception: " + ex.getMessage());
143: }
144:
145: // check equality of local files
146: assertIdentical(localDataDir + localBinaryFile,
147: localDataDir + filename);
148:
149: // and delete local file
150: File local = new File(localDataDir + filename);
151: local.delete();
152:
153: ftp.quit();
154: } finally {
155: if (ftp.connected()) {
156: ftp.quitImmediately();
157: }
158: }
159: }
160:
161: /**
162: * Test transfering using the server to generate a unique
163: * file name
164: */
165: public void testTransferUnique() throws Exception {
166:
167: log.info("testTransferUnique()");
168:
169: try {
170:
171: connect();
172:
173: // monitor transfer progress
174: ftp.setProgressMonitor(new TestProgressMonitor(), 250000);
175:
176: // move to test directory
177: ftp.chdir(testdir);
178: ftp.setType(FTPTransferType.BINARY);
179:
180: // put to a random filename
181: String filename = ftp.put(localDataDir + localBinaryFile,
182: null);
183: log.info("Put file to '" + filename + "'");
184:
185: // get it back
186: ftp.get(localDataDir + filename, filename);
187:
188: // delete remote file
189: ftp.delete(filename);
190: try {
191: ftp.modtime(filename);
192: fail(filename + " should not be found");
193: } catch (IOException ex) {
194: log.info("Expected exception: " + ex.getMessage());
195: } catch (FTPException ex) {
196: log.info("Expected exception: " + ex.getMessage());
197: }
198:
199: // check equality of local files
200: assertIdentical(localDataDir + localBinaryFile,
201: localDataDir + filename);
202:
203: // and delete local file
204: File local = new File(localDataDir + filename);
205: local.delete();
206:
207: ftp.quit();
208: } finally {
209: if (ftp.connected()) {
210: ftp.quitImmediately();
211: }
212: }
213: }
214:
215: /**
216: * Test transfering by only supplying a local dir, not a
217: * full pathname
218: */
219: public void testNoLocalFilename() throws Exception {
220:
221: log.info("testNoLocalFilename()");
222:
223: try {
224:
225: connect();
226:
227: // monitor transfer progress
228: ftp.setProgressMonitor(new TestProgressMonitor(), 250000);
229:
230: // move to test directory
231: ftp.chdir(testdir);
232: ftp.setType(FTPTransferType.BINARY);
233:
234: // put to a random filename
235: String filename = generateRandomFilename();
236: ftp.put(localDataDir + localBinaryFile, filename);
237: log.info("Put file to '" + filename + "'");
238:
239: // get it back
240: ftp.get(localDataDir, filename);
241:
242: // delete remote file
243: ftp.delete(filename);
244: try {
245: ftp.modtime(filename);
246: fail(filename + " should not be found");
247: } catch (IOException ex) {
248: log.info("Expected exception: " + ex.getMessage());
249: } catch (FTPException ex) {
250: log.info("Expected exception: " + ex.getMessage());
251: }
252:
253: // check equality of local files
254: assertIdentical(localDataDir + localBinaryFile,
255: localDataDir + filename);
256:
257: // and delete local file
258: File local = new File(localDataDir + filename);
259: local.delete();
260:
261: ftp.quit();
262: } finally {
263: if (ftp.connected()) {
264: ftp.quitImmediately();
265: }
266: }
267: }
268:
269: /**
270: * Test transfering a text file
271: */
272: public void testTransferText() throws Exception {
273:
274: log.info("testTransferText()");
275:
276: try {
277:
278: connect();
279:
280: // monitor transfer progress
281: ftp.setProgressMonitor(new TestProgressMonitor(), 250000);
282:
283: // move to test directory
284: ftp.chdir(testdir);
285: ftp.setType(FTPTransferType.ASCII);
286:
287: // put to a random filename
288: String filename = generateRandomFilename();
289: ftp.put(localDataDir + localTextFile, filename);
290:
291: // get it back
292: ftp.get(localDataDir + filename, filename);
293:
294: // delete remote file
295: ftp.delete(filename);
296: try {
297: ftp.modtime(filename);
298: fail(filename + " should not be found");
299: } catch (IOException ex) {
300: log.info("Expected exception: " + ex.getMessage());
301: } catch (FTPException ex) {
302: log.info("Expected exception: " + ex.getMessage());
303: }
304:
305: // check equality of local files
306: assertIdentical(localDataDir + localTextFile, localDataDir
307: + filename);
308:
309: // and delete local file
310: File local = new File(localDataDir + filename);
311: local.delete();
312:
313: ftp.quit();
314: } finally {
315: if (ftp.connected()) {
316: ftp.quitImmediately();
317: }
318: }
319: }
320:
321: /**
322: * Test transfering a text file
323: */
324: public void testTransferUnixText() throws Exception {
325:
326: log.info("testTransferUnixText()");
327:
328: try {
329:
330: connect();
331:
332: // monitor transfer progress
333: ftp.setProgressMonitor(new TestProgressMonitor(), 250000);
334:
335: // move to test directory
336: ftp.chdir(testdir);
337: ftp.setType(FTPTransferType.ASCII);
338:
339: // put to a random filename
340: String filename = generateRandomFilename();
341: ftp.put(localDataDir + localUnixTextFile, filename);
342:
343: // get it back
344: ftp.get(localDataDir + filename, filename);
345:
346: // check equality of local files - against the equivalent local text file
347: // not the transferred unix one
348: assertIdentical(localDataDir + localTextFile, localDataDir
349: + filename);
350:
351: // delete remote file
352: ftp.delete(filename);
353: try {
354: ftp.modtime(filename);
355: fail(filename + " should not be found");
356: } catch (IOException ex) {
357: log.info("Expected exception: " + ex.getMessage());
358: } catch (FTPException ex) {
359: log.info("Expected exception: " + ex.getMessage());
360: }
361:
362: // and delete local file
363: File local = new File(localDataDir + filename);
364: local.delete();
365:
366: ftp.quit();
367: } finally {
368: if (ftp.connected()) {
369: ftp.quitImmediately();
370: }
371: }
372: }
373:
374: /**
375: * Test getting a byte array
376: */
377: public void testGetBytes() throws Exception {
378:
379: log.info("testGetBytes()");
380:
381: try {
382:
383: connect();
384:
385: // monitor transfer progress
386: ftp.setProgressMonitor(new TestProgressMonitor(), 250000);
387:
388: // move to test directory
389: ftp.chdir(testdir);
390: ftp.setType(FTPTransferType.BINARY);
391:
392: // get the file and work out its size
393: String filename1 = generateRandomFilename();
394: ftp.get(localDataDir + filename1, remoteBinaryFile);
395: File file1 = new File(localDataDir + filename1);
396: long len = file1.length();
397:
398: // now get to a buffer and check the length
399: byte[] result = ftp.get(remoteBinaryFile);
400: assertTrue(result.length == len);
401:
402: // put the buffer
403: String filename2 = generateRandomFilename();
404: ftp.put(result, filename2);
405:
406: // get it back as a file
407: ftp.get(localDataDir + filename2, filename2);
408:
409: // remove it remotely
410: ftp.delete(filename2);
411:
412: // and now check files are identical
413: File file2 = new File(localDataDir + filename2);
414: assertIdentical(file1, file2);
415:
416: // and finally delete them
417: file1.delete();
418: file2.delete();
419:
420: ftp.quit();
421: } finally {
422: if (ftp.connected()) {
423: ftp.quitImmediately();
424: }
425: }
426: }
427:
428: /**
429: * Test the stream functionality
430: */
431: public void testTransferStream() throws Exception {
432:
433: log.info("testTransferStream()");
434:
435: try {
436:
437: connect();
438:
439: // monitor transfer progress
440: ftp.setProgressMonitor(new TestProgressMonitor(), 250000);
441:
442: // move to test directory
443: ftp.chdir(testdir);
444: ftp.setType(FTPTransferType.BINARY);
445:
446: // get file as output stream
447: ByteArrayOutputStream out = new ByteArrayOutputStream();
448: ftp.get(out, remoteBinaryFile);
449:
450: // convert to byte array
451: byte[] result1 = out.toByteArray();
452:
453: // put this
454: String filename = generateRandomFilename();
455: ftp.put(new ByteArrayInputStream(result1), filename);
456:
457: // get it back
458: byte[] result2 = ftp.get(filename);
459:
460: // delete remote file
461: ftp.delete(filename);
462:
463: // and compare the buffers
464: assertIdentical(result1, result2);
465:
466: ftp.quit();
467: } finally {
468: if (ftp.connected()) {
469: ftp.quitImmediately();
470: }
471: }
472: }
473:
474: /**
475: * Test the append functionality in put()
476: */
477: public void testPutAppend() throws Exception {
478:
479: log.info("testPutAppend()");
480:
481: try {
482:
483: connect();
484:
485: // monitor transfer progress
486: ftp.setProgressMonitor(new TestProgressMonitor(), 250000);
487:
488: // move to test directory
489: ftp.chdir(testdir);
490: ftp.setType(FTPTransferType.BINARY);
491:
492: // put to a random filename
493: String filename = generateRandomFilename();
494: ftp.put(localDataDir + localBinaryFile, filename);
495:
496: // second time, append if possible
497: int count = 1;
498: ftp.put(localDataDir + localBinaryFile, filename, true);
499: count++;
500:
501: // get it back & delete remotely
502: ftp.get(localDataDir + filename, filename);
503: ftp.delete(filename);
504:
505: // check it is the right size
506:
507: File file1 = new File(localDataDir + localBinaryFile);
508: File file2 = new File(localDataDir + filename);
509: assertTrue(file1.length() * count == file2.length());
510: log.info(localBinaryFile + " length=" + file1.length()
511: + ", " + filename + " length=" + file2.length());
512:
513: // and finally delete it
514: file2.delete();
515:
516: ftp.quit();
517: } finally {
518: if (ftp.connected()) {
519: ftp.quitImmediately();
520: }
521: }
522: }
523:
524: /**
525: * Test transferring empty files
526: */
527: public void testTransferEmpty() throws Exception {
528:
529: log.info("testTransferEmpty()");
530:
531: try {
532:
533: connect();
534:
535: // move to test directory
536: ftp.chdir(testdir);
537:
538: // get an empty file
539: ftp.get(localDataDir + remoteEmptyFile, remoteEmptyFile);
540: File empty = new File(localDataDir + remoteEmptyFile);
541: assertTrue(empty.exists());
542: assertTrue(empty.length() == 0);
543:
544: // delete it
545: empty.delete();
546:
547: // put an empty file
548: ftp.put(localDataDir + localEmptyFile, localEmptyFile);
549:
550: // get it back as a different filename
551: String filename = generateRandomFilename();
552: ftp.get(localDataDir + filename, localEmptyFile);
553: empty = new File(localDataDir + filename);
554: assertTrue(empty.exists());
555: assertTrue(empty.length() == 0);
556:
557: // delete file we got back (copy of our local empty file)
558: empty.delete();
559:
560: // and delete the remote empty file we
561: // put there
562: ftp.delete(localEmptyFile);
563:
564: ftp.quit();
565: } finally {
566: if (ftp.connected()) {
567: ftp.quitImmediately();
568: }
569: }
570: }
571:
572: /**
573: * Test transferring non-existent files
574: */
575: public void testTransferNonExistent() throws Exception {
576:
577: log.info("testTransferNonExistent()");
578:
579: try {
580:
581: connect();
582:
583: // move to test directory
584: ftp.chdir(testdir);
585:
586: // generate a name & try to get it
587: String filename = generateRandomFilename();
588: log.info("Getting non-existent file: " + filename);
589: try {
590: ftp.get(localDataDir + filename, filename);
591: fail(filename + " should not be found");
592: } catch (IOException ex) {
593: log.info("Expected exception: " + ex.getMessage());
594: } catch (FTPException ex) {
595: log.info("Expected exception: " + ex.getMessage());
596:
597: }
598:
599: // ensure we don't have a local file of that name produced
600: File file = new File(localDataDir + filename);
601: assertFalse(file.exists());
602:
603: // generate name & try to put
604: filename = generateRandomFilename();
605: try {
606: ftp.put(localDataDir + filename, filename);
607: fail(filename + " should not be found");
608: } catch (FileNotFoundException ex) {
609: log.info("Expected exception: " + ex.getMessage());
610: }
611:
612: ftp.quit();
613: } finally {
614: if (ftp.connected()) {
615: ftp.quitImmediately();
616: }
617: }
618: }
619:
620: /**
621: * Test of progress monitor functionality
622: */
623: public class TestProgressMonitor implements FTPProgressMonitor {
624:
625: /* (non-Javadoc)
626: * @see com.enterprisedt.net.ftp.FTPProgressMonitor#bytesTransferred(long)
627: */
628: public void bytesTransferred(long count) {
629: log.info(count + " bytes transferred");
630: }
631: }
632:
633: /**
634: * Automatic test suite construction
635: *
636: * @return suite of tests for this class
637: */
638: public static Test suite() {
639: return new TestSuite(TestTransfer.class);
640: }
641:
642: /**
643: * Enable our class to be run, doing the
644: * tests
645: */
646: public static void main(String[] args) {
647: junit.textui.TestRunner.run(suite());
648: }
649: }
|