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: FTPClientTest.java,v $
029: * Revision 1.7 2007-08-09 00:10:53 hans
030: * Removed unused imports.
031: *
032: * Revision 1.6 2005/06/03 11:27:05 bruceb
033: * comment update
034: *
035: * Revision 1.5 2004/08/31 10:45:08 bruceb
036: * assign args correctly
037: *
038: * Revision 1.4 2004/05/01 16:56:14 bruceb
039: * cleaned up and deprecated
040: *
041: * Revision 1.3 2002/11/19 22:00:41 bruceb
042: * simple test case
043: *
044: *
045: */package com.enterprisedt.net.ftp.test;
046:
047: import com.enterprisedt.net.ftp.FTPClient;
048: import com.enterprisedt.net.ftp.FTPConnectMode;
049: import com.enterprisedt.net.ftp.FTPException;
050: import com.enterprisedt.net.ftp.FTPTransferType;
051: import com.enterprisedt.util.debug.Level;
052: import com.enterprisedt.util.debug.Logger;
053:
054: /**
055: * Basic test harness. No longer in use or maintained - see the
056: * JUnit tests and the demo program for current tests.
057: *
058: *
059: * @author Bruce Blackshaw
060: * @version $Revision: 1.7 $
061: *
062: */
063: public class FTPClientTest {
064:
065: /**
066: * Revision control id
067: */
068: public static final String cvsId = "$Id: FTPClientTest.java,v 1.7 2007-08-09 00:10:53 hans Exp $";
069:
070: /**
071: * Test harness
072: */
073: public static void main(String args[]) {
074:
075: Logger.setLevel(Level.ALL);
076:
077: // we want remote host, user name and password
078: if (args.length < 7) {
079: System.out.println(args.length);
080: usage();
081: System.exit(1);
082: }
083: try {
084:
085: // assign args to make it clear
086: String host = args[0];
087: String user = args[1];
088: String password = args[2];
089: String filename = args[3];
090: String directory = args[4];
091: String mode = args[5];
092: String connMode = args[6];
093:
094: // connect and test supplying port no.
095: FTPClient ftp = new FTPClient();
096: ftp.setControlEncoding("UTF-8");
097: ftp.setRemoteHost(host);
098: ftp.setRemotePort(21);
099: ftp.connect();
100: ftp.login(user, password);
101: ftp.quote("LANG da-DK", new String[] { "200" });
102: // ftp.dirDetails(".");
103: ftp.chdir("test");
104: ftp.quit();
105: if (true)
106: return;
107:
108: // connect again
109: ftp = new FTPClient();
110: ftp.setRemoteHost(host);
111:
112: ftp.login(user, password);
113:
114: ftp.dir(".", false);
115:
116: // binary transfer
117: if (mode.equalsIgnoreCase("BINARY")) {
118: ftp.setType(FTPTransferType.BINARY);
119: } else if (mode.equalsIgnoreCase("ASCII")) {
120: ftp.setType(FTPTransferType.ASCII);
121: } else {
122: System.out.println("Unknown transfer type: " + args[5]);
123: System.exit(-1);
124: }
125:
126: // PASV or active?
127: if (connMode.equalsIgnoreCase("PASV")) {
128: ftp.setConnectMode(FTPConnectMode.PASV);
129: } else if (connMode.equalsIgnoreCase("ACTIVE")) {
130: ftp.setConnectMode(FTPConnectMode.ACTIVE);
131: } else {
132: System.out.println("Unknown connect mode: " + args[6]);
133: System.exit(-1);
134: }
135:
136: // change dir
137: ftp.chdir(directory);
138:
139: // put a local file to remote host
140: ftp.put(filename, filename);
141:
142: // get bytes
143: byte[] buf = ftp.get(filename);
144: System.out.println("Got " + buf.length + " bytes");
145:
146: // append local file
147: try {
148: ftp.put(filename, filename, true);
149: } catch (FTPException ex) {
150: System.out.println("Append failed: " + ex.getMessage());
151: }
152:
153: // get bytes again - should be 2 x
154: buf = ftp.get(filename);
155: System.out.println("Got " + buf.length + " bytes");
156:
157: // rename
158: ftp.rename(filename, filename + ".new");
159:
160: // get a remote file - the renamed one
161: ftp.get(filename + ".tst", filename + ".new");
162:
163: // ASCII transfer
164: ftp.setType(FTPTransferType.ASCII);
165:
166: // test that list() works
167: String[] listing = ftp.dir(".");
168: for (int i = 0; i < listing.length; i++)
169: System.out.println(listing[i]);
170:
171: // test that dir() works in full mode
172: String[] listings = ftp.dir(".", true);
173: for (int i = 0; i < listings.length; i++)
174: System.out.println(listings[i]);
175:
176: // try system()
177: System.out.println(ftp.system());
178:
179: // try pwd()
180: System.out.println(ftp.pwd());
181:
182: ftp.quit();
183: } catch (Exception ex) {
184: System.out.println("Caught exception: " + ex.getMessage());
185: }
186: }
187:
188: /**
189: * Basic usage statement
190: */
191: public static void usage() {
192:
193: System.out.println("Usage: ");
194: System.out.println("com.enterprisedt.net.ftp.FTPClientTest "
195: + "remotehost user password filename directory "
196: + "(ascii|binary) (active|pasv)");
197: }
198:
199: }
|