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