jdk/test/sun/net/www/protocol/jar/B4957695.java
author wetmore
Thu, 06 Aug 2009 17:56:59 -0700
changeset 3433 3b2490b7a752
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6867657: Many JSN tests do not run under cygwin Reviewed-by: ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4957695
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @library ../../httptest/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @build HttpCallback HttpServer ClosedChannelList HttpTransaction AbstractCallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @summary URLJarFile.retrieve does not delete tmpFile on IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class B4957695 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    static int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static boolean error = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static void read (InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        int c,len=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        while ((c=is.read()) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            len += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        System.out.println ("read " + len + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static class CallBack extends AbstractCallback {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        public void request (HttpTransaction req, int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                System.out.println ("Request received");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                req.setResponseEntityBody (new FileInputStream ("foo1.jar"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                System.out.println ("content length " + req.getResponseHeader (
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                        "Content-length"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                req.sendPartialResponse (200, "Ok");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                req.abortiveClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static HttpServer server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        String tmpdir = System.getProperty("java.io.tmpdir");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        String[] list1 = listTmpFiles(tmpdir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        //server = new HttpServer (new CallBack(), 10, 1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        server = new HttpServer (new CallBack(), 1, 5, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        int port = server.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        System.out.println ("Server: listening on port: " + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        URL url = new URL ("jar:http://localhost:"+port+"!/COPYRIGHT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            URLConnection urlc = url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            read (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            System.out.println ("Received IOException as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        String[] list2 = listTmpFiles(tmpdir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (!sameList (list1, list2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new RuntimeException ("some jar_cache files left behind");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    static String[] listTmpFiles (String d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        File dir = new File (d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return dir.list (new FilenameFilter () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            public boolean accept (File dr, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                return (name.startsWith ("jar_cache"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static boolean sameList (String[] list1, String[] list2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (list1.length != list2.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        for (int i=0; i<list1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            String s1 = list1[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            String s2 = list2[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if ((s1 == null && s2 != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            } else if ((s2 == null && s1 != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            } else if (s1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            } else if (!s1.equals(s2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
}