001: /**
002: *
003: * edtFTPj
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: VMSTests.java,v $
029: * Revision 1.3 2007/03/02 07:10:18 bruceb
030: * tweak to try cd into a dir that is returned from listing.
031: *
032: * Revision 1.2 2005/07/15 17:30:06 bruceb
033: * rework of unit testing structure
034: *
035: * Revision 1.1 2005/06/03 11:26:49 bruceb
036: * new tests
037: *
038: *
039: */package com.enterprisedt.net.ftp.test;
040:
041: import java.io.File;
042: import java.io.IOException;
043: import java.text.ParseException;
044:
045: import junit.framework.Test;
046: import junit.framework.TestSuite;
047:
048: import com.enterprisedt.net.ftp.FTPException;
049: import com.enterprisedt.net.ftp.FTPFile;
050: import com.enterprisedt.net.ftp.FTPTransferType;
051:
052: /**
053: * Tests against an external (public) VMS FTP server - so we
054: * can't do put's.
055: *
056: * @author Bruce Blackshaw
057: * @version $Revision: 1.3 $
058: */
059: public class VMSTests extends FTPTestCase {
060:
061: /**
062: * Revision control id
063: */
064: public static String cvsId = "@(#)$Id: VMSTests.java,v 1.3 2007/03/02 07:10:18 bruceb Exp $";
065:
066: /**
067: * Get name of log file
068: *
069: * @return name of file to log to
070: */
071: protected String getLogName() {
072: return "TestVMS.log";
073: }
074:
075: /**
076: * Test directory listings
077: */
078: public void testDir() throws Exception {
079:
080: connect();
081:
082: // move to test directory
083: ftp.chdir(testdir);
084:
085: // list current dir
086: String[] list = ftp.dir();
087: print(list);
088:
089: // non-existent file
090: String randomName = generateRandomFilename();
091: try {
092: list = ftp.dir(randomName);
093: print(list);
094: } catch (FTPException ex) {
095: if (ex.getReplyCode() != 550 && ex.getReplyCode() != 552)
096: fail("dir(" + randomName
097: + ") should throw 550/552 for non-existent dir");
098: }
099:
100: ftp.quit();
101: }
102:
103: /**
104: * Test full directory listings
105: */
106: public void testDirFull() throws Exception {
107:
108: connect();
109:
110: // move to test directory
111: ftp.chdir(testdir);
112:
113: // list current dir by name
114: String[] list = ftp.dir("", true);
115: print(list);
116:
117: log.debug("******* dirDetails *******");
118: FTPFile[] files = ftp.dirDetails("");
119: print(files);
120: log.debug("******* end dirDetails *******");
121:
122: // non-existent file. Some FTP servers don't send
123: // a 450/450, but IIS does - so we catch it and
124: // confirm it is a 550
125: String randomName = generateRandomFilename();
126: try {
127: list = ftp.dir(randomName, true);
128: print(list);
129: } catch (FTPException ex) {
130: if (ex.getReplyCode() != 550 && ex.getReplyCode() != 552)
131: fail("dir(" + randomName
132: + ") should throw 550/552 for non-existent dir");
133: }
134:
135: ftp.quit();
136: }
137:
138: /**
139: * Test transfering a text file
140: */
141: public void testChangeDir() throws Exception {
142:
143: log.debug("testChangeDir");
144:
145: connect();
146:
147: // move to test directory
148: ftp.chdir(testdir);
149:
150: String dir = getRemoteDirName();
151:
152: ftp.chdir(dir);
153:
154: ftp.cdup();
155:
156: ftp.quit();
157: }
158:
159: /**
160: * Test transfering a text file
161: */
162: public void testTransfer() throws Exception {
163:
164: log.debug("testTransfer");
165:
166: connect();
167:
168: // move to test directory
169: ftp.chdir(testdir);
170: ftp.setType(FTPTransferType.ASCII);
171:
172: // random filename
173: String filename = generateRandomFilename();
174:
175: String remoteFile = getRemoteFileName();
176:
177: if (remoteFile == null)
178: remoteFile = remoteTextFile;
179:
180: // get it
181: ftp.get(localDataDir + filename, remoteFile);
182:
183: ftp.setType(FTPTransferType.BINARY);
184:
185: ftp.get(localDataDir + filename, remoteFile);
186:
187: // and delete local file
188: File local = new File(localDataDir + filename);
189: local.delete();
190:
191: ftp.quit();
192: }
193:
194: private String getRemoteFileName() throws IOException,
195: FTPException, ParseException {
196: FTPFile[] files = ftp.dirDetails("");
197: String remoteFile = null;
198: for (int i = 0; i < files.length; i++) {
199: if (files[i].getName().toUpperCase().endsWith(".TXT")) {
200: remoteFile = files[i].getName();
201: break;
202: }
203: }
204: return remoteFile;
205: }
206:
207: private String getRemoteDirName() throws IOException, FTPException,
208: ParseException {
209: FTPFile[] files = ftp.dirDetails("");
210: String dir = null;
211: for (int i = 0; i < files.length; i++) {
212: if (files[i].isDir()) {
213: dir = files[i].getName();
214: break;
215: }
216: }
217: return dir;
218: }
219:
220: /**
221: * Automatic test suite construction
222: *
223: * @return suite of tests for this class
224: */
225: public static Test suite() {
226: return new TestSuite(VMSTests.class);
227: }
228:
229: /**
230: * Enable our class to be run, doing the
231: * tests
232: */
233: public static void main(String[] args) {
234: junit.textui.TestRunner.run(suite());
235: }
236: }
|