0N/A/*
3909N/A * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A @bug 4166799
0N/A @summary Make sure URL-downloaded jar files (jar_cache files)
0N/A will be deleted when VM exits.
0N/A
0N/A @build DeleteTempJar
0N/A @run shell deletetempjar.sh
0N/A */
0N/A
0N/Aimport java.io.File;
0N/Aimport java.io.FileInputStream;
0N/Aimport java.io.FileOutputStream;
0N/Aimport java.io.OutputStream;
0N/A
0N/Aimport java.net.InetAddress;
0N/Aimport java.net.InetSocketAddress;
0N/Aimport java.net.JarURLConnection;
0N/Aimport java.net.URL;
0N/A
0N/Aimport java.util.jar.JarEntry;
0N/Aimport java.util.jar.JarFile;
0N/Aimport java.util.jar.JarOutputStream;
0N/A
0N/Aimport com.sun.net.httpserver.*;
0N/A
0N/A
0N/Apublic class DeleteTempJar
0N/A{
0N/A public static void realMain(String args[]) throws Exception
0N/A {
0N/A final File zf = File.createTempFile("deletetemp", ".jar");
0N/A zf.deleteOnExit();
3656N/A try (FileOutputStream fos = new FileOutputStream(zf);
3656N/A JarOutputStream jos = new JarOutputStream(fos))
3656N/A {
3656N/A JarEntry je = new JarEntry("entry");
3656N/A jos.putNextEntry(je);
3656N/A jos.write("hello, world".getBytes("ASCII"));
3656N/A }
0N/A
0N/A HttpServer server = HttpServer.create(
0N/A new InetSocketAddress((InetAddress) null, 0), 0);
0N/A HttpContext context = server.createContext("/",
0N/A new HttpHandler() {
0N/A public void handle(HttpExchange e) {
3656N/A try (FileInputStream fis = new FileInputStream(zf)) {
3656N/A e.sendResponseHeaders(200, zf.length());
3656N/A OutputStream os = e.getResponseBody();
3656N/A byte[] buf = new byte[1024];
3656N/A int count = 0;
3656N/A while ((count = fis.read(buf)) != -1) {
3656N/A os.write(buf, 0, count);
3656N/A }
3656N/A } catch (Exception ex) {
3656N/A unexpected(ex);
3656N/A } finally {
3656N/A e.close();
0N/A }
0N/A }
3656N/A });
0N/A server.start();
0N/A
0N/A URL url = new URL("jar:http://localhost:"
0N/A + new Integer(server.getAddress().getPort()).toString()
0N/A + "/deletetemp.jar!/");
0N/A JarURLConnection c = (JarURLConnection)url.openConnection();
0N/A JarFile f = c.getJarFile();
0N/A check(f.getEntry("entry") != null);
0N/A System.out.println(f.getName());
0N/A server.stop(0);
0N/A }
0N/A
0N/A //--------------------- Infrastructure ---------------------------
0N/A static volatile int passed = 0, failed = 0;
0N/A static boolean pass() {passed++; return true;}
0N/A static boolean fail() {failed++; Thread.dumpStack(); return false;}
0N/A static boolean fail(String msg) {System.out.println(msg); return fail();}
0N/A static void unexpected(Throwable t) {failed++; t.printStackTrace();}
0N/A static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
0N/A static boolean equal(Object x, Object y) {
0N/A if (x == null ? y == null : x.equals(y)) return pass();
0N/A else return fail(x + " not equal to " + y);}
0N/A public static void main(String[] args) throws Throwable {
0N/A try {realMain(args);} catch (Throwable t) {unexpected(t);}
0N/A System.out.println("\nPassed = " + passed + " failed = " + failed);
0N/A if (failed > 0) throw new AssertionError("Some tests failed");}
0N/A}