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