1cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath/*
2cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  Licensed to the Apache Software Foundation (ASF) under one or more
3cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  contributor license agreements.  See the NOTICE file distributed with
4cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  this work for additional information regarding copyright ownership.
5cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  The ASF licenses this file to You under the Apache License, Version 2.0
6cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  (the "License"); you may not use this file except in compliance with
7cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  the License.  You may obtain a copy of the License at
8cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *
9cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *     http://www.apache.org/licenses/LICENSE-2.0
10cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *
11cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  Unless required by applicable law or agreed to in writing, software
12cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  distributed under the License is distributed on an "AS IS" BASIS,
13cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  See the License for the specific language governing permissions and
15cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *  limitations under the License.
16cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */
17cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
18ab762bb740405d0fefcccf4a0899a234f995be13Narayan Kamathpackage org.apache.harmony.tests.java.net;
19cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
20e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamathimport tests.support.resource.Support_Resources;
21cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.io.BufferedOutputStream;
22cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.io.File;
23cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.io.FileOutputStream;
24cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.io.IOException;
25cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.io.InputStream;
26cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.net.JarURLConnection;
27cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.net.MalformedURLException;
28cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.net.URL;
29cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.net.URLConnection;
30cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.util.jar.JarEntry;
31cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.util.jar.JarFile;
32cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.util.jar.JarOutputStream;
33cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.util.jar.Manifest;
34cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.util.zip.ZipEntry;
35cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.util.zip.ZipFile;
36cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
37cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathpublic class JarURLConnectionTest extends junit.framework.TestCase {
38cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
392b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath    private JarURLConnection juc;
40cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
41e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath    private URL copyAndOpenResourceStream(String jarFile, String inFile)
42e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath            throws MalformedURLException {
43e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath
44e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        File resources = Support_Resources.createTempFolder();
45e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath
46e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        Support_Resources.copyFile(resources, "net", jarFile);
47e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        File file = new File(resources.toString() + "/net/" + jarFile);
48e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath
49e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        return new URL("jar:file:" + file.getPath() + "!/" + inFile);
50e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath    }
51e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath
52cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
53cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
54cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.net.JarURLConnection#getAttributes()
55cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
56cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_getAttributes() throws Exception {
57e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        URL u = copyAndOpenResourceStream("lf.jar", "swt.dll");
58cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
59cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        juc = (JarURLConnection) u.openConnection();
60cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        java.util.jar.Attributes a = juc.getJarEntry().getAttributes();
61cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals("Returned incorrect Attributes", "SHA MD5", a
62cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                .get(new java.util.jar.Attributes.Name("Digest-Algorithms")));
63cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
64cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
65cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
66cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * @throws Exception
67cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.net.JarURLConnection#getEntryName()
68cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
69cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_getEntryName() throws Exception {
70e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        URL u = copyAndOpenResourceStream("lf.jar", "plus.bmp");
71cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        juc = (JarURLConnection) u.openConnection();
72cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals("Returned incorrect entryName", "plus.bmp", juc
73cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                .getEntryName());
74e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        u = copyAndOpenResourceStream("lf.jar", "");
75cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        juc = (JarURLConnection) u.openConnection();
76cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertNull("Returned incorrect entryName", juc.getEntryName());
77cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath//      Regression test for harmony-3053
78e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        URL url = copyAndOpenResourceStream("lf.jar", "foo.jar!/Bugs/HelloWorld.class");
79cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals("foo.jar!/Bugs/HelloWorld.class", ((JarURLConnection) url.openConnection()).getEntryName());
80cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
81cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
82cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
83cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.net.JarURLConnection#getJarEntry()
84cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
85cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_getJarEntry() throws Exception {
86e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        URL u = copyAndOpenResourceStream("lf.jar", "plus.bmp");
87cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        juc = (JarURLConnection) u.openConnection();
88cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals("Returned incorrect JarEntry", "plus.bmp", juc
89cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                .getJarEntry().getName());
90e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        u = copyAndOpenResourceStream("lf.jar", "");
91cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        juc = (JarURLConnection) u.openConnection();
92cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertNull("Returned incorrect JarEntry", juc.getJarEntry());
93cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
94cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
95cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
96cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.net.JarURLConnection#getJarFile()
97cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
98cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_getJarFile() throws MalformedURLException, IOException {
99e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        URL url = copyAndOpenResourceStream("lf.jar", "missing");
100cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
101cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        JarURLConnection connection = null;
102cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        connection = (JarURLConnection) url.openConnection();
103cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        try {
104cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            connection.connect();
105cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            fail("Did not throw exception on connect");
106cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        } catch (IOException e) {
107cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            // expected
108cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        }
109cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
110cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        try {
111cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            connection.getJarFile();
112cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            fail("Did not throw exception after connect");
113cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        } catch (IOException e) {
114cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            // expected
115cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        }
116cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
117cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        File resources = Support_Resources.createTempFolder();
118cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
119cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Support_Resources.copyFile(resources, null, "hyts_att.jar");
120cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        File file = new File(resources.toString() + "/hyts_att.jar");
121cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        URL fUrl1 = new URL("jar:file:" + file.getPath() + "!/");
122cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        JarURLConnection con1 = (JarURLConnection) fUrl1.openConnection();
123cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        ZipFile jf1 = con1.getJarFile();
124cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        JarURLConnection con2 = (JarURLConnection) fUrl1.openConnection();
125cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        ZipFile jf2 = con2.getJarFile();
126cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("file: JarFiles not the same", jf1 == jf2);
127cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        jf1.close();
128cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("File should exist", file.exists());
129cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        con1 = (JarURLConnection) fUrl1.openConnection();
130cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        jf1 = con1.getJarFile();
131cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        con2 = (JarURLConnection) fUrl1.openConnection();
132cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        jf2 = con2.getJarFile();
133cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue("http: JarFiles not the same", jf1 == jf2);
134cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        jf1.close();
135cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
136cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
137cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
138cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.net.JarURLConnection.getJarFile()
139cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * <p/>
140cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * Regression test for HARMONY-29
141cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
142cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_getJarFile29() throws Exception {
143cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        File jarFile = File.createTempFile("1+2 3", "test.jar");
144cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        jarFile.deleteOnExit();
145cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        JarOutputStream out = new JarOutputStream(new FileOutputStream(jarFile));
146cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        out.putNextEntry(new ZipEntry("test"));
147cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        out.closeEntry();
148cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        out.close();
149cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
150cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        JarURLConnection conn = (JarURLConnection) new URL("jar:file:"
151cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                + jarFile.getAbsolutePath().replaceAll(" ", "%20") + "!/")
152cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                .openConnection();
153cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        conn.getJarFile().entries();
154cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
155cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
156cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    //Regression for HARMONY-3436
157cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_setUseCaches() throws Exception {
158cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        File resources = Support_Resources.createTempFolder();
159cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Support_Resources.copyFile(resources, null, "hyts_att.jar");
160cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        File file = new File(resources.toString() + "/hyts_att.jar");
161cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        URL url = new URL("jar:file:" + file.getPath() + "!/HasAttributes.txt");
162cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
163cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        JarURLConnection connection = (JarURLConnection) url.openConnection();
164cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        connection.setUseCaches(false);
165cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        InputStream in = connection.getInputStream();
166cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        JarFile jarFile1 = connection.getJarFile();
167cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        JarEntry jarEntry1 = connection.getJarEntry();
168cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        byte[] data = new byte[1024];
169cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        while (in.read(data) >= 0)
170cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            ;
171cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        in.close();
172cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        JarFile jarFile2 = connection.getJarFile();
173cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        JarEntry jarEntry2 = connection.getJarEntry();
174cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertSame(jarFile1, jarFile2);
175cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertSame(jarEntry1, jarEntry2);
176cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
177cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        try {
178cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            connection.getInputStream();
179cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            fail("should throw IllegalStateException");
180cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        } catch (IllegalStateException e) {
181cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            // expected
182cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        }
183cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
184cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
185cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
186cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.net.JarURLConnection#getJarFileURL()
187cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
188cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_getJarFileURL() throws Exception {
189cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Regression test for harmony-3053
190cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        URL url = new URL("jar:file:///bar.jar!/foo.jar!/Bugs/HelloWorld.class");
191e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        assertEquals("file:///bar.jar", ((JarURLConnection) url.openConnection()).getJarFileURL().toString());
192cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
193cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
194cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
195cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.net.JarURLConnection#getMainAttributes()
196cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
197cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_getMainAttributes() throws Exception {
198e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        URL u = copyAndOpenResourceStream("lf.jar", "swt.dll");
199cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        juc = (JarURLConnection) u.openConnection();
200cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        java.util.jar.Attributes a = juc.getMainAttributes();
201cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals("Returned incorrect Attributes", "1.0", a
202cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                .get(java.util.jar.Attributes.Name.MANIFEST_VERSION));
203cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
204cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
205cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
206cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.net.JarURLConnection#getInputStream()
207cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
208cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_getInputStream_DeleteJarFileUsingURLConnection()
209cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            throws Exception {
2102b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath        File file = File.createTempFile("JarUrlConnectionTest", "jar");
2112b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath        FileOutputStream jarFile = new FileOutputStream(file);
212cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        JarOutputStream out = new JarOutputStream(new BufferedOutputStream(
213cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                jarFile));
2142b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath        JarEntry jarEntry = new JarEntry("entry.txt");
215cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        out.putNextEntry(jarEntry);
216cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        out.write(new byte[] { 'a', 'b', 'c' });
217cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        out.close();
218cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
2192b7c83942a5e28c53698232182193d5118028e6cNarayan Kamath        URL url = new URL("jar:file:" + file.getAbsolutePath() + "!/entry.txt");
220cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        URLConnection conn = url.openConnection();
221cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        conn.setUseCaches(false);
222cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        InputStream is = conn.getInputStream();
223cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        is.close();
224cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue(file.delete());
225cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
226cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
227cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
228cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.net.JarURLConnection#getManifest()
229cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
230cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_getManifest() throws Exception {
231e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        URL u = copyAndOpenResourceStream("lf.jar", "plus.bmp");
232cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        juc = (JarURLConnection) u.openConnection();
233cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        Manifest mf = juc.getManifest();
234cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertNotNull(mf);
235cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // equal but not same manifest
236cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals(mf, juc.getManifest());
237cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertNotSame(mf, juc.getManifest());
238cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // same main attributes
239cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals(juc.getMainAttributes(), mf.getMainAttributes());
240cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
241cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
242cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
243cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.net.JarURLConnection#getCertificates()
244cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
245cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_getCertificates() throws Exception {
246e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        URL u = copyAndOpenResourceStream("lf.jar", "plus.bmp");
247cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        juc = (JarURLConnection) u.openConnection();
248cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // read incomplete, shall return null
249cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertNull(juc.getCertificates());
250cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals("Returned incorrect JarEntry", "plus.bmp", juc
251cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                .getJarEntry().getName());
252cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // read them all
253cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        InputStream is = juc.getInputStream();
254cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        byte[] buf = new byte[80];
255cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        while (is.read(buf) > 0) ;
256cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // still return null for this type of file
257cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertNull(juc.getCertificates());
258cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
259e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        URL fileURL = copyAndOpenResourceStream("lf.jar", "");
260cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        juc = (JarURLConnection) fileURL.openConnection();
261cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        is = juc.getJarFileURL().openStream();
262cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        while (is.read(buf) > 0) ;
263cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // null for this jar file
264cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertNull(juc.getCertificates());
265cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
266cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
267cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
268cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.net.JarURLConnection#getContentLength()
269cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * Regression test for HARMONY-3665
270cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
271cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_getContentLength() throws Exception {
272cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // check length for jar file itself
273e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        URL u = copyAndOpenResourceStream("lf.jar", "");
274cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals("Returned incorrect size for jar file", 33095,
275cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                u.openConnection().getContentLength());
276cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
277cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // check length for jar entry
278e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        u = copyAndOpenResourceStream("lf.jar", "plus.bmp");
279cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals("Returned incorrect size for the entry", 190,
280cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                u.openConnection().getContentLength());
281cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
282cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
283cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
284cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.net.JarURLConnection#getContentType()
285cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * Regression test for HARMONY-3665
286cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
287cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_getContentType() throws Exception {
288cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // check type for jar file itself
289e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        URL u = copyAndOpenResourceStream("lf.jar", "");
290cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals("Returned incorrect type for jar file", "x-java/jar",
291cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                u.openConnection().getContentType());
292cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
293cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // check type for jar entry with known type
294e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        u = copyAndOpenResourceStream("lf.jar", "plus.bmp");
295cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals("Returned incorrect type for the entry with known type",
296e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath                "image/x-ms-bmp", u.openConnection().getContentType());
297cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
298cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // check type for jar entry with unknown type
299e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        u = copyAndOpenResourceStream("lf.jar", "Manifest.mf");
300cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertEquals("Returned incorrect type for the entry with known type",
301cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                "content/unknown", u.openConnection().getContentType());
302cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
303cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
304cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_getURLEncodedEntry() throws IOException {
305e19fe36340c4df01c0841090f53e4789044c9fe0Narayan Kamath        URL url = copyAndOpenResourceStream("url-test.jar", "test%20folder%20for%20url%20test/test");
306cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        if (url != null) {
307cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            // Force existence check
308cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            InputStream is = url.openStream();
309cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            is.close();
310cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        }
311cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
312cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath}
313