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