Author: sebb
Date: Wed Apr  6 17:02:41 2011
New Revision: 1089535

URL: http://svn.apache.org/viewvc?rev=1089535&view=rev
Log:
Add test utilities (need some work)

Added:
    
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/DownloadListings.java
   (with props)
    
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/MLSDComparison.java
   (with props)

Added: 
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/DownloadListings.java
URL: 
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/DownloadListings.java?rev=1089535&view=auto
==============================================================================
--- 
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/DownloadListings.java
 (added)
+++ 
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/DownloadListings.java
 Wed Apr  6 17:02:41 2011
@@ -0,0 +1,131 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.commons.net.ftp.parser;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.io.Reader;
+import java.net.Socket;
+
+import org.apache.commons.net.PrintCommandListener;
+import org.apache.commons.net.ftp.FTPClient;
+import org.apache.commons.net.ftp.FTPCommand;
+import org.apache.commons.net.io.Util;
+
+/**
+ * Sample class to download LIST and MLSD listings from list of ftp sites.
+ */
+public class DownloadListings extends FTPClient {
+    
+    // Also used by MLDSComparison
+    static final String DOWNLOAD_DIR = "target/ftptest";
+
+    private PrintCommandListener listener;
+    private PrintWriter out;
+
+    private boolean open(String host, int port) throws Exception{
+        System.out.println("Connecting to "+host);
+        out = new PrintWriter(new FileWriter(new File(DOWNLOAD_DIR, 
host+"_info.txt")));
+        listener = new PrintCommandListener(out);
+        addProtocolCommandListener(listener);
+        setConnectTimeout(30000);
+        try {
+            connect(host, port);
+        } catch (Exception e) {
+            System.out.println(e);
+            return false;
+        }
+        enterLocalPassiveMode(); // this is reset by connect
+        System.out.println("Logging in to "+host);
+        return login("anonymous", "user@localhost");
+    }
+
+    private void info() throws IOException {
+        syst();
+        help();
+        feat();
+        removeProtocolCommandListener(listener);
+    }
+
+    private void download(String path, int command, File filename) throws 
Exception {
+        Socket socket;
+        if ((socket = _openDataConnection_(command, getListArguments(path))) 
== null) {
+            System.out.println(getReplyString());
+            return;
+        }
+        InputStream inputStream = socket.getInputStream();
+        OutputStream outputStream = new FileOutputStream(filename);
+        Util.copyStream(inputStream, outputStream );
+        inputStream.close();
+        socket.close();
+        outputStream.close();
+
+        if (!completePendingCommand())
+        {
+            System.out.println(getReplyString());
+        }
+    }
+
+    public static void main(String[] args) throws Exception {
+        String host;// = "ftp.funet.fi";
+        int port = 21;
+        String path;// = "/";
+
+        new File(DOWNLOAD_DIR).mkdirs();
+        DownloadListings self = new DownloadListings();
+        OutputStream os = new FileOutputStream(new File(DOWNLOAD_DIR, 
"session.log"));
+        self.addProtocolCommandListener(new PrintCommandListener(new 
PrintWriter(os), true));
+
+        Reader is = new FileReader("mirrors.list");
+        BufferedReader rdr = new BufferedReader(is);
+        String line;
+        while((line=rdr.readLine()) != null){
+            if (line.startsWith("ftp")){
+                String []parts = line.split("\\s+");
+                String target = parts[2];
+                host = target.substring("ftp://".length());
+                int slash = host.indexOf('/');
+                path = host.substring(slash);
+                host = host.substring(0,slash);
+                System.out.println(host+ " "+path);
+                if (self.open(host, port)) {
+                    try {
+                        self.info();
+                        self.download(path, FTPCommand.LIST, new 
File(DOWNLOAD_DIR, host+"_list.txt"));
+                        self.download(path, FTPCommand.MLSD, new 
File(DOWNLOAD_DIR, host+"_mlsd.txt"));
+                    } catch (Exception e) {
+                        e.printStackTrace();
+                    } finally {
+                        self.disconnect();
+                    }
+                    self.removeProtocolCommandListener(self.listener);
+                    self.out.close();
+                }
+            }
+        }
+        os.close();
+    }
+}

Propchange: 
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/DownloadListings.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/DownloadListings.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: 
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/MLSDComparison.java
URL: 
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/MLSDComparison.java?rev=1089535&view=auto
==============================================================================
--- 
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/MLSDComparison.java
 (added)
+++ 
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/MLSDComparison.java
 Wed Apr  6 17:02:41 2011
@@ -0,0 +1,225 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.commons.net.ftp.parser;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FilenameFilter;
+import java.io.InputStream;
+import java.util.Arrays;
+import java.util.Calendar;
+import java.util.Comparator;
+import java.util.TimeZone;
+
+import org.apache.commons.net.ftp.FTP;
+import org.apache.commons.net.ftp.FTPClientConfig;
+import org.apache.commons.net.ftp.FTPFile;
+import org.apache.commons.net.ftp.FTPFileFilters;
+import org.apache.commons.net.ftp.FTPListParseEngine;
+import org.apache.commons.net.ftp.parser.MLSxEntryParser;
+import org.apache.commons.net.ftp.parser.UnixFTPEntryParser;
+import org.junit.Test;
+
+/**
+ * Attempt comparison of LIST and MLSD listings
+ * 
+ * TODO - needs some work.
+ */
+public class MLSDComparison {
+
+    private Comparator<FTPFile> cmp = new Comparator<FTPFile>() {
+        public int compare(FTPFile o1, FTPFile o2) {
+                String n1 = o1.getName();
+                String n2 = o2.getName();
+                return n1.compareTo(n2);
+            }
+    };
+
+    @Test
+    public void testFile() throws Exception{
+        File path = new File(DownloadListings.DOWNLOAD_DIR);
+        FilenameFilter filter = new FilenameFilter(){
+            public boolean accept(File dir, String name) {
+                return name.endsWith("_mlsd.txt");
+            }
+
+        };
+        for (File mlsd : path.listFiles(filter)){
+            System.out.println(mlsd);
+            InputStream is = new FileInputStream(mlsd);
+            FTPListParseEngine engine = new 
FTPListParseEngine(MLSxEntryParser.getInstance());
+            engine.readServerList(is, FTP.DEFAULT_CONTROL_ENCODING);
+            FTPFile [] mlsds = engine.getFiles(FTPFileFilters.ALL);
+            is.close();
+            File list = new 
File(mlsd.getParentFile(),mlsd.getName().replace("_mlsd", "_list"));
+
+            System.out.println(list);
+            is = new FileInputStream(list);
+            FTPClientConfig cfg = new FTPClientConfig();
+            cfg.setServerTimeZoneId("GMT");
+            UnixFTPEntryParser parser = new UnixFTPEntryParser(cfg);
+            engine = new FTPListParseEngine(parser);
+            engine.readServerList(is, FTP.DEFAULT_CONTROL_ENCODING);
+            FTPFile [] lists = engine.getFiles(FTPFileFilters.ALL);
+            is.close();
+            compareSortedLists(mlsds, lists);
+        }
+    }
+
+    private void compareSortedLists(FTPFile[] lst, FTPFile[] mlst){
+        Arrays.sort(lst, cmp );
+        Arrays.sort(mlst, cmp );
+        FTPFile first, second;
+        int firstl=lst.length;
+        int secondl=mlst.length;
+        int one=0, two=0;
+        first = lst[one++];
+        second = mlst[two++];
+        int cmp;
+        while (one < firstl || two < secondl) {
+//            String fs1 = first.toFormattedString();
+//            String fs2 = second.toFormattedString();
+            String rl1 = first.getRawListing();
+            String rl2 = second.getRawListing();
+            cmp = first.getName().compareTo(second.getName());
+            if (cmp == 0) {
+                if (first.getName().endsWith("HEADER.html")){
+                    cmp = 0;
+                }
+                if (!areEquivalent(first, second)){
+//                    System.out.println(rl1);
+//                    System.out.println(fs1);
+                    long tdiff = 
first.getTimestamp().getTimeInMillis()-second.getTimestamp().getTimeInMillis();
+                    System.out.println("Minutes diff "+tdiff/(1000*60));
+//                    System.out.println(fs2);
+//                    System.out.println(rl2);
+//                    System.out.println();
+//                    fail();
+                }
+                if (one < firstl) {
+                    first = lst[one++];
+                }
+                if (two < secondl) {
+                    second = mlst[two++];
+                }
+            } else if (cmp < 0) {
+                if (!first.getName().startsWith(".")) { // skip hidden files
+                    System.out.println("1: "+rl1);
+                }
+                if (one < firstl) {
+                    first = lst[one++];
+                }
+            } else {
+                System.out.println("2: "+rl2);
+                if (two < secondl) {
+                    second = mlst[two++];
+                }
+            }
+        }
+    }
+    /**
+     * Compare two instances to see if they are the same,
+     * ignoring any uninitialised fields.
+     *
+     * @param b
+     * @return true if the initialised fields are the same
+     * @since 3.0
+     */
+    public boolean areEquivalent(FTPFile a, FTPFile b) {
+        return
+            a.getName().equals(b.getName()) &&
+            areSame(a.getSize(), b.getSize(), -1L) &&
+//            areSame(a.getUser(), b.getUser()) &&
+//            areSame(a.getGroup(), b.getGroup()) &&
+            areSame(a.getTimestamp(), b.getTimestamp()) &&
+//            areSame(a.getType(), b.getType(), UNKNOWN_TYPE) &&
+//            areSame(a.getHardLinkCount(), b.getHardLinkCount(), 0) &&
+//            areSame(a._permissions, b._permissions)
+            true
+            ;
+    }
+
+    // compare permissions: default is all false, but that is also a possible
+    // state, so this may miss some differences
+//    private boolean areSame(boolean[][] a, boolean[][] b) {
+//        return isDefault(a) || isDefault(b) || Arrays.deepEquals(a, b);
+//    }
+
+    // Is the array in its default state?
+//    private boolean isDefault(boolean[][] a) {
+//        for(boolean[] r : a){
+//            for(boolean rc : r){
+//                if (rc) { // not default
+//                    return false;
+//                }
+//            }
+//        }
+//        return true;
+//    }
+
+
+    private boolean areSame(Calendar a, Calendar b) {
+        return a == null || b == null || areSameDateTime(a, b);
+    }
+
+    private boolean areSameDateTime(Calendar a, Calendar b) {
+        TimeZone UTC = TimeZone.getTimeZone("UTC");
+        Calendar ac = Calendar.getInstance(UTC);
+        ac.setTime(a.getTime());
+        Calendar bc = Calendar.getInstance(UTC);
+        bc.setTime(b.getTime());
+        return isSameDay(ac, bc) && isSameTime(ac, bc);
+    }
+
+    private boolean isSameDay(Calendar a, Calendar b) {
+        int ad = a.get(Calendar.DAY_OF_MONTH);
+        int bd = b.get(Calendar.DAY_OF_MONTH);
+        return
+            a.get(Calendar.YEAR) == b.get(Calendar.YEAR) &&
+            a.get(Calendar.MONTH) == b.get(Calendar.MONTH) &&
+            ad == bd
+            ;
+    }
+    private boolean isSameTime(Calendar a, Calendar b) {
+        int ah = a.get(Calendar.HOUR_OF_DAY);
+        int bh = b.get(Calendar.HOUR_OF_DAY);
+        int am = a.get(Calendar.MINUTE);
+        int bm = b.get(Calendar.MINUTE);
+        int as = a.get(Calendar.SECOND);
+        int bs = b.get(Calendar.SECOND);
+        return
+            (ah == 0 && am == 0 && as ==0) ||
+            (bh == 0 && bm == 0 && bs ==0) ||
+            (ah == bh && am == bm) // ignore seconds
+            ;
+    }
+
+
+    private boolean areSame(long a, long b, long d) {
+        return a == d || b == d || a == b;
+    }
+
+//    private boolean areSame(int a, int b, int d) {
+//        return a == d || b == d || a == b;
+//    }
+//
+//    private boolean areSame(String a, String b) {
+//        return a.length() == 0 || b.length() == 0 || a.equals(b);
+//    }
+}

Propchange: 
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/MLSDComparison.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
commons/proper/net/trunk/src/test/java/org/apache/commons/net/ftp/parser/MLSDComparison.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision


Reply via email to