Source Code Cross Referenced for FTPTestCase.java in  » Net » edtftpj » com » enterprisedt » net » ftp » test » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » edtftpj » com.enterprisedt.net.ftp.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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: FTPTestCase.java,v $
029:         *        Revision 1.22  2007-05-29 04:16:16  bruceb
030:         *        connected() test
031:         *
032:         *        Revision 1.21  2007/05/03 04:21:30  bruceb
033:         *        rename logger
034:         *
035:         *        Revision 1.20  2007/04/24 01:35:40  bruceb
036:         *        set to DEBUG
037:         *
038:         *        Revision 1.19  2007/04/21 04:14:20  bruceb
039:         *        *** empty log message ***
040:         *
041:         *        Revision 1.18  2007/02/26 07:14:54  bruceb
042:         *        more info on char pos if mismatch in file comparison
043:         *
044:         *        Revision 1.17  2006/11/14 11:41:02  bruceb
045:         *        added localBigTextFile
046:         *
047:         *        Revision 1.16  2005/07/15 17:30:06  bruceb
048:         *        rework of unit testing structure
049:         *
050:         *        Revision 1.15  2005/07/11 21:15:31  bruceb
051:         *        minor tweak re listings
052:         *
053:         *        Revision 1.14  2005/06/03 11:27:05  bruceb
054:         *        comment update
055:         *
056:         *        Revision 1.14  2005/05/15 19:45:36  bruceb
057:         *        changes for testing setActivePortRange
058:         *
059:         *        Revision 1.13  2005/03/18 11:12:44  bruceb
060:         *        deprecated constructors
061:         *
062:         *        Revision 1.12  2005/01/14 18:07:19  bruceb
063:         *        bulk count added
064:         *
065:         *        Revision 1.11  2004/10/18 15:58:58  bruceb
066:         *        test encoding constructor
067:         *
068:         *        Revision 1.10  2004/08/31 10:44:49  bruceb
069:         *        minor tweaks re compile warnings
070:         *
071:         *        Revision 1.9  2004/07/23 08:33:44  bruceb
072:         *        enable testing for strict replies or not
073:         *
074:         *        Revision 1.8  2004/06/25 12:03:54  bruceb
075:         *        get mode from sys property
076:         *
077:         *        Revision 1.7  2004/05/11 21:58:05  bruceb
078:         *        getVersion() added
079:         *
080:         *        Revision 1.6  2004/05/01 17:05:59  bruceb
081:         *        cleaned up and deprecated
082:         *
083:         *        Revision 1.5  2004/04/17 18:38:38  bruceb
084:         *        tweaks for ssl and new parsing functionality
085:         *
086:         *        Revision 1.4  2004/04/05 20:58:41  bruceb
087:         *        latest hans tweaks to tests
088:         *
089:         *        Revision 1.3  2003/11/02 21:51:44  bruceb
090:         *        fixed bug re transfer mode not being set
091:         *
092:         *        Revision 1.2  2003/05/31 14:54:05  bruceb
093:         *        cleaned up unused imports
094:         *
095:         *        Revision 1.1  2002/11/19 22:00:15  bruceb
096:         *        New JUnit test cases
097:         *
098:         *
099:         */package com.enterprisedt.net.ftp.test;
100:
101:        import java.io.BufferedInputStream;
102:        import java.io.File;
103:        import java.io.FileInputStream;
104:        import java.io.IOException;
105:        import java.util.Date;
106:        import java.util.Properties;
107:
108:        import junit.framework.AssertionFailedError;
109:        import junit.framework.TestCase;
110:
111:        import com.enterprisedt.net.ftp.FTPClientInterface;
112:        import com.enterprisedt.net.ftp.FTPFile;
113:        import com.enterprisedt.util.debug.FileAppender;
114:        import com.enterprisedt.util.debug.Level;
115:        import com.enterprisedt.util.debug.Logger;
116:
117:        /**
118:         *  Generic JUnit test class for FTP, that provides some
119:         *  useful methods for subclasses that implement the actual
120:         *  test cases
121:         *
122:         *  @author         Bruce Blackshaw
123:         *  @version        $Revision: 1.22 $
124:         */
125:        abstract public class FTPTestCase extends TestCase {
126:
127:            /**
128:             *  Revision control id
129:             */
130:            public static String cvsId = "@(#)$Id: FTPTestCase.java,v 1.22 2007-05-29 04:16:16 bruceb Exp $";
131:
132:            /**
133:             *  Log stream
134:             */
135:            protected Logger log = Logger.getLogger("FTPTestCase");
136:
137:            /**
138:             *  Reference to the FTP client
139:             */
140:            protected FTPClientInterface ftp;
141:
142:            /**
143:             * Lowest port
144:             */
145:            protected int lowPort = 10000 + (int) Math.random() * 20000;
146:
147:            /**
148:             * Highest port
149:             */
150:            protected int highPort = lowPort + 15;
151:
152:            /**
153:             *  Remote directory that remote test files/dirs are in
154:             */
155:            protected String testdir;
156:
157:            /**
158:             *  Remote text file
159:             */
160:            protected String remoteTextFile;
161:
162:            /**
163:             *  Local text file
164:             */
165:            protected String localTextFile;
166:
167:            /**
168:             *  Local text file
169:             */
170:            protected String localUnixTextFile;
171:
172:            /**
173:             *  Remote binary file
174:             */
175:            protected String remoteBinaryFile;
176:
177:            /**
178:             *  Local binary file
179:             */
180:            protected String localBinaryFile;
181:
182:            /**
183:             *  Local empty file
184:             */
185:            protected String localEmptyFile;
186:
187:            /**
188:             *  Remote empty file
189:             */
190:            protected String remoteEmptyFile;
191:
192:            /**
193:             *  Remote empty dir
194:             */
195:            protected String remoteEmptyDir;
196:
197:            /**
198:             * Big local file for testing
199:             */
200:            protected String localBigFile;
201:
202:            /**
203:             * Big local text file for testing
204:             */
205:            protected String localBigTextFile;
206:
207:            /**
208:             * Local test dir
209:             */
210:            protected String localTestDir;
211:
212:            /**
213:             * Log directory
214:             */
215:            protected String logDir;
216:
217:            /**
218:             * Local data directory
219:             */
220:            protected String localDataDir;
221:
222:            /**
223:             * Number of operations for stress testing
224:             */
225:            protected int bulkCount = 100;
226:
227:            /**
228:             *  Loaded properties
229:             */
230:            protected Properties props = new Properties();
231:
232:            /**
233:             * Server specific tools 
234:             */
235:            protected TestTools tools = null;
236:
237:            /**
238:             *  Initialize test properties
239:             */
240:            public FTPTestCase() {
241:
242:                Logger.setLevel(Level.DEBUG);
243:
244:                String propsfile = System.getProperty(
245:                        "ftptest.properties.filename", "test.properties");
246:
247:                try {
248:                    props.load(new FileInputStream(propsfile));
249:                } catch (IOException ex) {
250:                    System.out.println("Failed to open " + propsfile);
251:                    System.exit(-1);
252:                }
253:
254:                // various test files and dirs
255:                testdir = props.getProperty("ftptest.testdir");
256:                localTextFile = props.getProperty("ftptest.file.local.text");
257:                localUnixTextFile = props
258:                        .getProperty("ftptest.file.local.text.unix");
259:                localTestDir = props.getProperty("ftptest.dir.local");
260:                localDataDir = props.getProperty("ftptest.datadir.local",
261:                        "data");
262:                if (!localDataDir.endsWith(File.separator))
263:                    localDataDir += File.separator;
264:                localBigFile = props.getProperty("ftptest.file.local.big");
265:                localBigTextFile = props
266:                        .getProperty("ftptest.file.local.big.text");
267:                remoteTextFile = props.getProperty("ftptest.file.remote.text");
268:                localBinaryFile = props
269:                        .getProperty("ftptest.file.local.binary");
270:                remoteBinaryFile = props
271:                        .getProperty("ftptest.file.remote.binary");
272:                localEmptyFile = props.getProperty("ftptest.file.local.empty");
273:                remoteEmptyFile = props
274:                        .getProperty("ftptest.file.remote.empty");
275:                remoteEmptyDir = props.getProperty("ftptest.dir.remote.empty");
276:                String bulkCountStr = props.getProperty("ftptest.bulkcount");
277:                logDir = props.getProperty("ftptest.logdir", "log");
278:                if (bulkCountStr != null)
279:                    bulkCount = Integer.parseInt(bulkCountStr);
280:                String lowPortStr = props.getProperty("ftptest.lowport");
281:                if (lowPortStr != null)
282:                    lowPort = Integer.parseInt(lowPortStr);
283:                String highPortStr = props.getProperty("ftptest.highport");
284:                if (highPortStr != null)
285:                    highPort = Integer.parseInt(highPortStr);
286:
287:                String testToolsClass = props.getProperty("ftptest.testtools");
288:                tools = loadTestTools(testToolsClass);
289:                tools.setProperties(props);
290:            }
291:
292:            /**
293:             * Load the test tools class
294:             * 
295:             * @param testToolsClass    full class name
296:             * @return
297:             */
298:            private TestTools loadTestTools(String testToolsClass) {
299:                try {
300:                    Class clazz = Class.forName(testToolsClass);
301:                    return (TestTools) clazz.newInstance();
302:                } catch (Exception ex) {
303:                    log.error("Failed to instantiate " + testToolsClass, ex);
304:                }
305:                return null;
306:            }
307:
308:            /**
309:             *  Setup is called before running each test
310:             */
311:            protected void setUp() throws Exception {
312:                Logger.addAppender(new FileAppender(logDir + File.separator
313:                        + getLogName()));
314:            }
315:
316:            /**
317:             *  Deallocate resources at close of each test
318:             */
319:            protected void tearDown() throws Exception {
320:                Logger.shutdown();
321:            }
322:
323:            /**
324:             *  Connect to the server and setup log stream
325:             */
326:            protected void connect() throws Exception {
327:                ftp = tools.connect();
328:                assertEquals(true, ftp.connected());
329:                log.debug("connected successfully");
330:            }
331:
332:            /**
333:             *  Generate a random file name for testing
334:             *
335:             *  @return  random filename
336:             */
337:            protected String generateRandomFilename() {
338:                Date now = new Date();
339:                Long ms = new Long(now.getTime());
340:                return ms.toString();
341:            }
342:
343:            /**
344:             *  Test to see if two buffers are identical, byte for byte
345:             *
346:             *  @param buf1   first buffer
347:             *  @param buf2   second buffer
348:             */
349:            protected void assertIdentical(byte[] buf1, byte[] buf2)
350:                    throws Exception {
351:
352:                assertEquals(buf1.length, buf2.length);
353:                for (int i = 0; i < buf1.length; i++)
354:                    assertEquals(buf1[i], buf2[i]);
355:            }
356:
357:            /**
358:             *  Test to see if two files are identical, byte for byte
359:             *
360:             *  @param file1  name of first file
361:             *  @param file2  name of second file
362:             */
363:            protected void assertIdentical(String file1, String file2)
364:                    throws Exception {
365:                File f1 = new File(file1);
366:                File f2 = new File(file2);
367:                assertIdentical(f1, f2);
368:            }
369:
370:            /**
371:             *  Test to see if two files are identical, byte for byte
372:             *
373:             *  @param file1  first file object
374:             *  @param file2  second file object
375:             */
376:            protected void assertIdentical(File file1, File file2)
377:                    throws Exception {
378:
379:                BufferedInputStream is1 = null;
380:                BufferedInputStream is2 = null;
381:                try {
382:                    // check lengths first
383:                    assertEquals(file1.length(), file2.length());
384:                    log.debug("Identical size [" + file1.getName() + ","
385:                            + file2.getName() + "]");
386:
387:                    // now check each byte
388:                    is1 = new BufferedInputStream(new FileInputStream(file1));
389:                    is2 = new BufferedInputStream(new FileInputStream(file2));
390:                    int ch1 = 0;
391:                    int ch2 = 0;
392:                    int count = 0;
393:                    try {
394:                        while ((ch1 = is1.read()) != -1
395:                                && (ch2 = is2.read()) != -1) {
396:                            count++;
397:                            assertEquals(ch1, ch2);
398:                        }
399:                    } catch (AssertionFailedError e) {
400:                        log
401:                                .debug("Comparison failed on char position="
402:                                        + count);
403:                        throw e;
404:                    }
405:                    log.debug("Contents equal");
406:                } catch (IOException ex) {
407:                    fail("Caught exception: " + ex.getMessage());
408:                } finally {
409:                    if (is1 != null)
410:                        is1.close();
411:                    if (is2 != null)
412:                        is2.close();
413:                }
414:            }
415:
416:            /**
417:             * Transfer back and forth multiple times
418:             */
419:            protected void bulkTransfer(String localFile) throws Exception {
420:                // put to a random filename muliple times
421:                String filename = generateRandomFilename();
422:                log.debug("Bulk transfer count=" + bulkCount);
423:                for (int i = 0; i < bulkCount; i++) {
424:                    ftp.put(localDataDir + localFile, filename);
425:
426:                    // get it back
427:                    ftp.get(localDataDir + filename, filename);
428:
429:                    // delete remote file
430:                    ftp.delete(filename);
431:                }
432:
433:                // check equality of local files
434:                assertIdentical(localDataDir + localFile, localDataDir
435:                        + filename);
436:
437:                // and delete local file
438:                File local = new File(localDataDir + filename);
439:                local.delete();
440:            }
441:
442:            /**
443:             *  Helper method for dumping a listing
444:             * 
445:             *  @param list   directory listing to print
446:             */
447:            protected void print(String[] list) {
448:                log.debug("Directory listing:");
449:                for (int i = 0; i < list.length; i++)
450:                    log.debug(list[i]);
451:                log.debug("Listing complete");
452:            }
453:
454:            /**
455:             *  Helper method for dumping a listing
456:             * 
457:             *  @param list   directory listing to print
458:             */
459:            protected void print(File[] list) {
460:                log.debug("Directory listing:");
461:                for (int i = 0; i < list.length; i++)
462:                    log.debug(list[i].getName());
463:                log.debug("Listing complete");
464:            }
465:
466:            /**
467:             *  Helper method for dumping a listing
468:             * 
469:             *  @param list   directory listing to print
470:             */
471:            protected void print(FTPFile[] list) {
472:                log.debug("Directory listing:");
473:                for (int i = 0; i < list.length; i++)
474:                    log.debug(list[i].toString());
475:                log.debug("Listing complete");
476:            }
477:
478:            /**
479:             *  Get name of log file
480:             *
481:             *  @return name of file to log to
482:             */
483:            abstract protected String getLogName();
484:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.