⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftptestcase.java

📁 java编写的非常详尽的基于ftp协议的上传下载源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        }
        catch (IOException ex) {
            System.out.println("Failed to open " + propsfile);
            System.exit(-1);
        }       
               
        // various test files and dirs
        testdir = props.getProperty("ftptest.testdir");
        localTextFile = props.getProperty("ftptest.file.local.text");
        localUnixTextFile = props.getProperty("ftptest.file.local.text.unix");
        localTestDir = props.getProperty("ftptest.dir.local");
        localDataDir = props.getProperty("ftptest.datadir.local", "data");
        if (!localDataDir.endsWith(File.separator))
            localDataDir += File.separator;
        localBigFile = props.getProperty("ftptest.file.local.big");
        localBigTextFile = props.getProperty("ftptest.file.local.big.text");
        remoteTextFile = props.getProperty("ftptest.file.remote.text");
        localBinaryFile = props.getProperty("ftptest.file.local.binary");
        remoteBinaryFile = props.getProperty("ftptest.file.remote.binary");
        localEmptyFile = props.getProperty("ftptest.file.local.empty");
        remoteEmptyFile = props.getProperty("ftptest.file.remote.empty");
        remoteEmptyDir = props.getProperty("ftptest.dir.remote.empty");
        String bulkCountStr = props.getProperty("ftptest.bulkcount");
        logDir = props.getProperty("ftptest.logdir", "log");
        if (bulkCountStr != null)
            bulkCount = Integer.parseInt(bulkCountStr);
        String lowPortStr = props.getProperty("ftptest.lowport");
        if (lowPortStr != null)
            lowPort = Integer.parseInt(lowPortStr);
        String highPortStr = props.getProperty("ftptest.highport");
        if (highPortStr != null)
            highPort = Integer.parseInt(highPortStr);
        
        String testToolsClass = System.getProperty("ftptest.testtools", props.getProperty("ftptest.testtools"));
        tools = loadTestTools(testToolsClass);
        tools.setProperties(props);
    }

    /**
     * Load the test tools class
     * 
     * @param testToolsClass    full class name
     * @return
     */
    private TestTools loadTestTools(String testToolsClass) {
        try {
            Class clazz = Class.forName(testToolsClass);
            return (TestTools)clazz.newInstance();
        }
        catch (Exception ex) {
            log.error("Failed to instantiate " + testToolsClass, ex);
        }
        return null;
    }

    /**
     *  Setup is called before running each test
     */    
    protected void setUp() throws Exception {
        Logger.addAppender(new FileAppender(logDir + File.separator + getLogName()));
    }
    
    /**
     *  Deallocate resources at close of each test
     */
    protected void tearDown() throws Exception {
        Logger.shutdown();
    }

    /**
     *  Connect to the server and setup log stream
     */
    protected void connect() throws Exception {
    	ftp = tools.connect();
        assertEquals(true, ftp.connected());
        log.debug("connected successfully");
    }
    
    /**
     *  Connect to the server and setup log stream
     */
    protected void connect(int lowest, int highest) throws Exception {
        ftp = tools.connect(lowest, highest);
        assertEquals(true, ftp.connected());
        log.debug("connected successfully");
    }
    
    protected void reconnect(FTPClientInterface ftp) throws Exception {
        tools.reconnect(ftp);
        log.debug("Reconnected successfully");
    }
    
    /**
     *  Generate a random file name for testing
     *
     *  @return  random filename
     */
    protected String generateRandomFilename() {
        Date now = new Date();
        Long ms = new Long(now.getTime());
        return ms.toString();
    }

    /**
     *  Test to see if two buffers are identical, byte for byte
     *
     *  @param buf1   first buffer
     *  @param buf2   second buffer
     */
    protected void assertIdentical(byte[] buf1, byte[] buf2) 
        throws Exception {
        
        assertEquals(buf1.length, buf2.length);
        for (int i = 0; i < buf1.length; i++)
            assertEquals(buf1[i], buf2[i]);
    }

    /**
     *  Test to see if two files are identical, byte for byte
     *
     *  @param file1  name of first file
     *  @param file2  name of second file
     */
    protected void assertIdentical(String file1, String file2) 
        throws Exception {
        File f1 = new File(file1);
        File f2 = new File(file2);
        assertIdentical(f1, f2);
    }

    /**
     *  Test to see if two files are identical, byte for byte
     *
     *  @param file1  first file object
     *  @param file2  second file object
     */
    protected void assertIdentical(File file1, File file2) 
        throws Exception {

        BufferedInputStream is1 = null;
        BufferedInputStream is2 = null;
        try {
            // check lengths first
            assertEquals(file1.length(), file2.length());
            log.debug("Identical size [" + file1.getName() + 
                        "," + file2.getName() + "]"); 

            // now check each byte
            is1 = new BufferedInputStream(new FileInputStream(file1));
            is2 = new BufferedInputStream(new FileInputStream(file2));
            int ch1 = 0;
            int ch2 = 0;
            int count = 0;
            try {
                while ((ch1 = is1.read()) != -1 && 
                       (ch2 = is2.read()) != -1) {
                    count++;
                    assertEquals(ch1, ch2);
                }    
            }
            catch (AssertionFailedError e) {
                log.debug("Comparison failed on char position=" + count);
                throw e;
            }
            log.debug("Contents equal");
        }
        catch (IOException ex) {
            fail("Caught exception: " + ex.getMessage());
        }
        finally {
            if (is1 != null)
                is1.close();
            if (is2 != null)
                is2.close();
        }
    }
    
    
    /**
     * Transfer back and forth multiple times
     */
    protected void bulkTransfer(String localFile) throws Exception {
        // put to a random filename muliple times
        String filename = generateRandomFilename();
        log.debug("Bulk transfer count=" + bulkCount);
        for (int i = 0; i < bulkCount; i++) {
            ftp.put(localDataDir + localFile, filename);

            // get it back
            ftp.get(localDataDir + filename, filename);
            
            // delete remote file
            ftp.delete(filename);
        }

        // check equality of local files
        assertIdentical(localDataDir + localFile, localDataDir + filename);

        // and delete local file
        File local = new File(localDataDir + filename);
        local.delete();
    }
    
    
    /**
     *  Helper method for dumping a listing
     * 
     *  @param list   directory listing to print
     */
    protected void print(String[] list) {
        log.debug("Directory listing:");
        if (list == null) {
            log.debug("Empty");
            return;
        }
        for (int i = 0; i < list.length; i++)
            log.debug(list[i]);
        log.debug("Listing complete");
    }
    
    /**
     *  Helper method for dumping a listing
     * 
     *  @param list   directory listing to print
     */
    protected void print(File[] list) {
        log.debug("Directory listing:");
        if (list == null) {
            log.debug("Empty");
            return;
        }
        for (int i = 0; i < list.length; i++)
            log.debug(list[i].getName());
        log.debug("Listing complete");
    }
    
    /**
     *  Helper method for dumping a listing
     * 
     *  @param list   directory listing to print
     */
    protected void print(FTPFile[] list) {
        log.debug("Directory listing:");
        if (list == null) {
            log.debug("Empty");
            return;
        }
        for (int i = 0; i < list.length; i++)
            log.debug(list[i].toString());
        log.debug("Listing complete");
    }    
        
    /**
     *  Get name of log file
     *
     *  @return name of file to log to
     */
    abstract protected String getLogName();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -