001: /**
002: *
003: * Java FTP client library.
004: *
005: * Copyright (C) 2000-2003 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: TestFileOperations.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:53 hans
033: * Removed unused imports.
034: *
035: * Revision 1.8 2005/07/15 17:30:06 bruceb
036: * rework of unit testing structure
037: *
038: * Revision 1.7 2005/06/03 11:27:05 bruceb
039: * comment update
040: *
041: * Revision 1.6 2004/08/31 10:44:49 bruceb
042: * minor tweaks re compile warnings
043: *
044: * Revision 1.5 2004/05/01 17:05:43 bruceb
045: * Logger stuff added
046: *
047: * Revision 1.4 2004/04/17 18:38:38 bruceb
048: * tweaks for ssl and new parsing functionality
049: *
050: * Revision 1.3 2003/11/02 21:51:32 bruceb
051: * test for size()
052: *
053: * Revision 1.2 2003/05/31 14:54:05 bruceb
054: * cleaned up unused imports
055: *
056: * Revision 1.1 2002/11/19 22:00:15 bruceb
057: * New JUnit test cases
058: *
059: *
060: */package com.enterprisedt.net.ftp.test;
061:
062: import java.io.File;
063: import java.text.SimpleDateFormat;
064: import java.util.Date;
065:
066: import junit.framework.Test;
067: import junit.framework.TestSuite;
068:
069: import com.enterprisedt.net.ftp.FTPException;
070: import com.enterprisedt.net.ftp.FTPTransferType;
071:
072: /**
073: * Tests various file operations
074: *
075: * @author Bruce Blackshaw
076: * @version $Revision: 1.10 $
077: */
078: public class TestFileOperations extends FTPTestCase {
079:
080: /**
081: * Revision control id
082: */
083: public static String cvsId = "@(#)$Id: TestFileOperations.java,v 1.10 2007-12-18 07:55:50 bruceb Exp $";
084:
085: /**
086: * Formatter for modtime
087: */
088: private SimpleDateFormat modFormatter = new SimpleDateFormat(
089: "yyyy/MM/dd @ HH:mm:ss");
090:
091: /**
092: * Get name of log file
093: *
094: * @return name of file to log to
095: */
096: protected String getLogName() {
097: return "TestFileOperations.log";
098: }
099:
100: /**
101: * Test remote deletion
102: */
103: public void testDelete() throws Exception {
104: // test existent & non-existent file
105: log.debug("testDelete() - ENTRY");
106: try {
107: connect();
108:
109: // move to test directory
110: ftp.chdir(testdir);
111:
112: try {
113: // try to delete a non-existent file
114: String file = generateRandomFilename();
115: log.debug("Deleting a non-existent file");
116: ftp.delete(file);
117: } catch (FTPException ex) {
118: log.debug("Expected exception: " + ex.getMessage());
119: }
120:
121: // how to delete a file and make it repeatable?
122: ftp.quit();
123: } finally {
124: log.debug("testDelete() - EXIT");
125: if (ftp.connected()) {
126: ftp.quitImmediately();
127: }
128: }
129: }
130:
131: /**
132: * Test renaming of a file
133: */
134: public void testRename() throws Exception {
135: log.debug("testRename() - ENTRY");
136: try {
137: // test existent & non-existent file
138: connect();
139:
140: // move to test directory
141: ftp.chdir(testdir);
142:
143: // rename file
144: String rename = remoteTextFile + ".renamed";
145: ftp.rename(remoteTextFile, rename);
146:
147: // get its mod time
148: Date modTime = ftp.modtime(rename);
149: String mod = modFormatter.format(modTime);
150: log.debug(rename + ": " + mod);
151:
152: // modtime on original file should fail
153: try {
154: ftp.modtime(remoteTextFile);
155: } catch (FTPException ex) {
156: log.debug("Expected exception: " + ex.getMessage());
157: }
158:
159: // and rename file back again
160: ftp.rename(rename, remoteTextFile);
161:
162: // and modtime should succeed
163: modTime = ftp.modtime(remoteTextFile);
164: mod = modFormatter.format(modTime);
165: log.debug(remoteTextFile + ": " + mod);
166:
167: // modtime on renamed (first time) file should
168: // now fail (as we've just renamed it to original)
169: try {
170: ftp.modtime(rename);
171: } catch (FTPException ex) {
172: log.debug("Expected exception: " + ex.getMessage());
173: }
174:
175: ftp.quit();
176: } finally {
177: log.debug("testRename() - EXIT");
178: if (ftp.connected()) {
179: ftp.quitImmediately();
180: }
181: }
182: }
183:
184: /**
185: * Test finding the modification time
186: * of a file
187: */
188: public void testModtime() throws Exception {
189: log.debug("testModtime() - ENTRY");
190: try {
191: connect();
192:
193: // move to test directory
194: ftp.chdir(testdir);
195:
196: // get modtime
197: log.debug("Modtime on existing file: " + remoteTextFile);
198: Date modTime = ftp.modtime(remoteTextFile);
199: String mod = modFormatter.format(modTime);
200: log.debug(remoteTextFile + ": " + mod);
201:
202: try {
203: // get modtime on non-existent file
204: String file = generateRandomFilename();
205: log.debug("Modtime on non-existent file");
206: modTime = ftp.modtime(file);
207: mod = modFormatter.format(modTime);
208: log.debug(remoteTextFile + ": " + mod);
209: } catch (FTPException ex) {
210: log.debug("Expected exception: " + ex.getMessage());
211: }
212:
213: ftp.quit();
214: } finally {
215: log.debug("testModtime() - EXIT");
216: if (ftp.connected()) {
217: ftp.quitImmediately();
218: }
219: }
220: }
221:
222: /**
223: * Test the size() method
224: */
225: public void testSize() throws Exception {
226: log.debug("testSize() - ENTRY");
227: try {
228: connect();
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 + localTextFile, filename);
237:
238: // find size of local file
239: File local = new File(localDataDir + localTextFile);
240: long sizeLocal = local.length();
241:
242: // find size of remote
243: long sizeRemote = ftp.size(filename);
244:
245: // delete remote file
246: ftp.delete(filename);
247:
248: if (sizeLocal != sizeRemote) {
249: String msg = "Local size(" + sizeLocal
250: + ") != remote size(" + sizeRemote + ")";
251: log.debug(msg);
252: throw new Exception(msg);
253: }
254:
255: ftp.quit();
256: } finally {
257: log.debug("testSize() - EXIT");
258: if (ftp.connected()) {
259: ftp.quitImmediately();
260: }
261: }
262: }
263:
264: /**
265: * Automatic test suite construction
266: *
267: * @return suite of tests for this class
268: */
269: public static Test suite() {
270: return new TestSuite(TestFileOperations.class);
271: }
272:
273: /**
274: * Enable our class to be run, doing the
275: * tests
276: */
277: public static void main(String[] args) {
278: junit.textui.TestRunner.run(suite());
279: }
280: }
|