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: TestResume.java,v $
029: * Revision 1.10 2007-12-18 07:55:50 bruceb
030: * add finally
031: *
032: * Revision 1.9 2007-08-09 00:10:52 hans
033: * Removed unused imports.
034: *
035: * Revision 1.8 2007-05-03 04:21:06 bruceb
036: * extra debug
037: *
038: * Revision 1.7 2007/02/07 23:04:34 bruceb
039: * fix testing for cancellation
040: *
041: * Revision 1.6 2005/11/15 21:07:30 bruceb
042: * disconnect when cancelled and reconnect
043: *
044: * Revision 1.5 2005/11/10 19:46:45 bruceb
045: * uses FTPTransferCancelledException
046: *
047: * Revision 1.4 2005/07/15 17:30:06 bruceb
048: * rework of unit testing structure
049: *
050: * Revision 1.3 2005/06/03 11:27:05 bruceb
051: * comment update
052: *
053: * Revision 1.2 2004/08/31 13:49:01 bruceb
054: * test cancelResume()
055: *
056: * Revision 1.1 2004/08/31 10:44:17 bruceb
057: * test code
058: *
059: */package com.enterprisedt.net.ftp.test;
060:
061: import java.io.File;
062:
063: import junit.framework.Test;
064: import junit.framework.TestSuite;
065:
066: import com.enterprisedt.net.ftp.FTPClientInterface;
067: import com.enterprisedt.net.ftp.FTPProgressMonitor;
068: import com.enterprisedt.net.ftp.FTPTransferCancelledException;
069: import com.enterprisedt.net.ftp.FTPTransferType;
070:
071: /**
072: * Test get'ing and put'ing of remote files in various ways
073: *
074: * @author Bruce Blackshaw
075: * @version $Revision: 1.10 $
076: */
077: public class TestResume extends FTPTestCase {
078:
079: /**
080: * Revision control id
081: */
082: public static String cvsId = "@(#)$Id: TestResume.java,v 1.10 2007-12-18 07:55:50 bruceb Exp $";
083:
084: /**
085: * Get name of log file
086: *
087: * @return name of file to log to
088: */
089: protected String getLogName() {
090: return "TestResume.log";
091: }
092:
093: /**
094: * Test resuming when putting a binary file
095: */
096: public void testResumePut() throws Exception {
097:
098: log.debug("testResumePut()");
099:
100: try {
101:
102: setup();
103:
104: // put to a random filename
105: String filename = generateRandomFilename();
106:
107: // use monitor to abort
108: ftp.setProgressMonitor(new CancelProgressMonitor(ftp),
109: 50000);
110:
111: // initiate the put
112: boolean cancelled = false;
113: try {
114: ftp.put(localDataDir + localBinaryFile, filename);
115: } catch (FTPTransferCancelledException ex) {
116: log.debug("Caught expected cancellation exception", ex);
117: cancelled = true;
118: }
119:
120: ftp.quit();
121:
122: assertTrue(cancelled);
123:
124: // reconnect
125: setup();
126:
127: // should be cancelled - now resume
128: ftp.resume();
129:
130: // put again - should append
131: ftp.put(localDataDir + localBinaryFile, filename);
132:
133: // get it back
134: ftp.get(localDataDir + filename, filename);
135:
136: // check equality of local files
137: assertIdentical(localDataDir + localBinaryFile,
138: localDataDir + filename);
139:
140: // finally, just check cancelResume works
141: ftp.cancelResume();
142:
143: // get it back
144: ftp.get(localDataDir + filename, filename);
145:
146: // delete remote file
147: ftp.delete(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 resuming when putting a binary file
163: */
164: public void testResumeGet() throws Exception {
165:
166: log.debug("testResumeGet()");
167:
168: try {
169:
170: setup();
171:
172: // put to a random filename
173: String filename = generateRandomFilename();
174:
175: // use monitor to abort
176: ftp.setProgressMonitor(new CancelProgressMonitor(ftp),
177: 131072);
178:
179: // initiate the put
180: boolean cancelled = false;
181: try {
182: ftp.get(localDataDir + filename, remoteBinaryFile);
183: } catch (FTPTransferCancelledException ex) {
184: log.debug("Caught expected cancellation exception", ex);
185: cancelled = true;
186: }
187:
188: ftp.quit();
189:
190: assertTrue(cancelled);
191:
192: // reconnect
193: setup();
194:
195: // should be cancelled - now resume
196: ftp.resume();
197:
198: // get again - should append
199: ftp.get(localDataDir + filename, remoteBinaryFile);
200:
201: String filename2 = generateRandomFilename();
202: ;
203:
204: // do another get - complete this time
205: ftp.get(localDataDir + filename2, remoteBinaryFile);
206:
207: // check equality of local files
208: assertIdentical(localDataDir + filename, localDataDir
209: + filename2);
210:
211: // and delete local file
212: File local = new File(localDataDir + filename);
213: local.delete();
214: local = new File(localDataDir + filename2);
215: local.delete();
216:
217: ftp.quit();
218: } finally {
219: if (ftp.connected()) {
220: ftp.quitImmediately();
221: }
222: }
223:
224: }
225:
226: /**
227: * Connect and navigate to test dir, binary mode
228: *
229: * @throws Exception
230: */
231: private void setup() throws Exception {
232: connect();
233:
234: // move to test directory
235: ftp.chdir(testdir);
236: ftp.setType(FTPTransferType.BINARY);
237: }
238:
239: /**
240: * Automatic test suite construction
241: *
242: * @return suite of tests for this class
243: */
244: public static Test suite() {
245: return new TestSuite(TestResume.class);
246: }
247:
248: /**
249: * Enable our class to be run, doing the
250: * tests
251: */
252: public static void main(String[] args) {
253: junit.textui.TestRunner.run(suite());
254: }
255:
256: /**
257: * As soon it receives notification of bytes transferred, it
258: * cancels the transfer
259: */
260: class CancelProgressMonitor implements FTPProgressMonitor {
261:
262: /**
263: * True if cancelled
264: */
265: private boolean cancelled = false;
266:
267: /**
268: * FTPClient reference
269: */
270: private FTPClientInterface ftpClient;
271:
272: /**
273: * Constructor
274: *
275: * @param ftp FTP client reference
276: */
277: public CancelProgressMonitor(FTPClientInterface ftp) {
278: this .ftpClient = ftp;
279: }
280:
281: /* (non-Javadoc)
282: * @see com.enterprisedt.net.ftp.FTPProgressMonitor#bytesTransferred(long)
283: */
284: public void bytesTransferred(long bytes) {
285: log.debug("bytesTransferred(" + bytes + ") called");
286: if (!cancelled) {
287: log.debug("Cancelling transfer");
288: ftpClient.cancelTransfer();
289: cancelled = true;
290: }
291: }
292: }
293: }
|