jdk/test/sun/net/ftp/MarkResetTest.java
changeset 45465 75b876952ec1
parent 23010 6dadb192ad81
equal deleted inserted replaced
45464:7d02d9bb39d1 45465:75b876952ec1
     1 /*
     1 /*
     2  * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2002, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  *
    25  * @test
    26  * run from MarkResetTest.sh
    26  * @bug 4673103
       
    27  * @run main/othervm/timeout=140 MarkResetTest
       
    28  * @summary URLConnection.getContent() hangs over FTP for DOC, PPT, XLS files
    27  */
    29  */
    28 
    30 
    29 import java.io.*;
    31 import java.io.BufferedReader;
    30 import java.net.*;
    32 import java.io.File;
    31 import java.util.regex.*;
    33 import java.io.FileInputStream;
       
    34 import java.io.IOException;
       
    35 import java.io.InputStream;
       
    36 import java.io.InputStreamReader;
       
    37 import java.io.OutputStream;
       
    38 import java.io.PrintWriter;
       
    39 import java.net.InetAddress;
       
    40 import java.net.ServerSocket;
       
    41 import java.net.Socket;
       
    42 import java.net.URL;
       
    43 import java.net.URLConnection;
       
    44 import java.nio.file.Files;
       
    45 import java.nio.file.Paths;
    32 
    46 
    33 public class MarkResetTest {
    47 public class MarkResetTest {
       
    48     private static final String FILE_NAME = "EncDec.doc";
    34 
    49 
    35     /**
    50     /**
    36      * A class that simulates, on a separate, an FTP server.
    51      * A class that simulates, on a separate, an FTP server.
    37      */
    52      */
    38     private class FtpServer extends Thread {
    53     private class FtpServer extends Thread {
   386             }
   401             }
   387         }
   402         }
   388     }
   403     }
   389 
   404 
   390     public static void main(String[] args) throws Exception {
   405     public static void main(String[] args) throws Exception {
   391         MarkResetTest test = new MarkResetTest();
   406         Files.copy(Paths.get(System.getProperty("test.src"), FILE_NAME),
       
   407                 Paths.get(".", FILE_NAME));
       
   408         new MarkResetTest();
   392     }
   409     }
   393 
   410 
   394     public MarkResetTest() {
   411     public MarkResetTest() {
   395         FtpServer server = null;
   412         FtpServer server = null;
   396         try {
   413         try {
   400             while (port == 0) {
   417             while (port == 0) {
   401                 Thread.sleep(500);
   418                 Thread.sleep(500);
   402                 port = server.getPort();
   419                 port = server.getPort();
   403             }
   420             }
   404 
   421 
   405             String filename = "EncDec.doc";
   422 
   406             URL url = new URL("ftp://localhost:" + port + "/" +
   423             URL url = new URL("ftp://localhost:" + port + "/" + FILE_NAME);
   407                                 filename);
       
   408 
   424 
   409             URLConnection con = url.openConnection();
   425             URLConnection con = url.openConnection();
   410             System.out.println("getContent: " + con.getContent());
   426             System.out.println("getContent: " + con.getContent());
   411             System.out.println("getContent-length: " + con.getContentLength());
   427             System.out.println("getContent-length: " + con.getContentLength());
   412 
   428 
   436             // We're done!
   452             // We're done!
   437             server.terminate();
   453             server.terminate();
   438             server.interrupt();
   454             server.interrupt();
   439 
   455 
   440             // Did we pass ?
   456             // Did we pass ?
   441             if (len != (new File(filename)).length()) {
   457             if (len != (new File(FILE_NAME)).length()) {
   442                 throw new Exception("Failed to read the file correctly");
   458                 throw new Exception("Failed to read the file correctly");
   443             }
   459             }
   444             System.out.println("PASSED: File read correctly");
   460             System.out.println("PASSED: File read correctly");
   445         } catch (Exception e) {
   461         } catch (Exception e) {
   446             e.printStackTrace();
   462             e.printStackTrace();