001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.net;
019:
020: import java.io.BufferedOutputStream;
021: import java.io.File;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.net.JarURLConnection;
026: import java.net.MalformedURLException;
027: import java.net.URL;
028: import java.net.URLConnection;
029: import java.util.jar.JarEntry;
030: import java.util.jar.JarFile;
031: import java.util.jar.JarOutputStream;
032: import java.util.jar.Manifest;
033: import java.util.zip.ZipEntry;
034: import java.util.zip.ZipFile;
035:
036: import tests.support.resource.Support_Resources;
037:
038: public class JarURLConnectionTest extends junit.framework.TestCase {
039:
040: JarURLConnection juc;
041:
042: URLConnection uc;
043:
044: private static final URL BASE = JarURLConnectionTest.class
045: .getClassLoader().getResource(
046: ".."
047: + File.separatorChar
048: + JarURLConnectionTest.class.getPackage()
049: .getName().replace('.',
050: File.separatorChar));
051:
052: /**
053: * @tests java.net.JarURLConnection#getAttributes()
054: */
055: public void test_getAttributes() throws Exception {
056: URL u = new URL("jar:" + BASE.toString() + "/lf.jar!/swt.dll");
057: juc = (JarURLConnection) u.openConnection();
058: java.util.jar.Attributes a = juc.getJarEntry().getAttributes();
059: assertEquals("Returned incorrect Attributes", "SHA MD5", a
060: .get(new java.util.jar.Attributes.Name(
061: "Digest-Algorithms")));
062: }
063:
064: /**
065: * @throws Exception
066: * @tests java.net.JarURLConnection#getEntryName()
067: */
068: public void test_getEntryName() throws Exception {
069: URL u = new URL("jar:" + BASE.toString() + "/lf.jar!/plus.bmp");
070: juc = (JarURLConnection) u.openConnection();
071: assertEquals("Returned incorrect entryName", "plus.bmp", juc
072: .getEntryName());
073: u = new URL("jar:" + BASE.toString() + "/lf.jar!/");
074: juc = (JarURLConnection) u.openConnection();
075: assertNull("Returned incorrect entryName", juc.getEntryName());
076: // Regression test for harmony-3053
077: URL url = new URL(
078: "jar:file:///bar.jar!/foo.jar!/Bugs/HelloWorld.class");
079: assertEquals("foo.jar!/Bugs/HelloWorld.class",
080: ((JarURLConnection) url.openConnection())
081: .getEntryName());
082: }
083:
084: /**
085: * @tests java.net.JarURLConnection#getJarEntry()
086: */
087: public void test_getJarEntry() throws Exception {
088: URL u = new URL("jar:" + BASE.toString() + "/lf.jar!/plus.bmp");
089: juc = (JarURLConnection) u.openConnection();
090: assertEquals("Returned incorrect JarEntry", "plus.bmp", juc
091: .getJarEntry().getName());
092: u = new URL("jar:" + BASE.toString() + "/lf.jar!/");
093: juc = (JarURLConnection) u.openConnection();
094: assertNull("Returned incorrect JarEntry", juc.getJarEntry());
095: }
096:
097: /**
098: * @tests java.net.JarURLConnection#getJarFile()
099: */
100: public void test_getJarFile() throws MalformedURLException,
101: IOException {
102: URL url = null;
103: url = new URL("jar:" + BASE.toString() + "/lf.jar!/missing");
104:
105: JarURLConnection connection = null;
106: connection = (JarURLConnection) url.openConnection();
107: try {
108: connection.connect();
109: fail("Did not throw exception on connect");
110: } catch (IOException e) {
111: // expected
112: }
113:
114: try {
115: connection.getJarFile();
116: fail("Did not throw exception after connect");
117: } catch (IOException e) {
118: // expected
119: }
120:
121: File resources = Support_Resources.createTempFolder();
122:
123: Support_Resources.copyFile(resources, null, "hyts_att.jar");
124: File file = new File(resources.toString() + "/hyts_att.jar");
125: URL fUrl1 = new URL("jar:file:" + file.getPath() + "!/");
126: JarURLConnection con1 = (JarURLConnection) fUrl1
127: .openConnection();
128: ZipFile jf1 = con1.getJarFile();
129: JarURLConnection con2 = (JarURLConnection) fUrl1
130: .openConnection();
131: ZipFile jf2 = con2.getJarFile();
132: assertTrue("file: JarFiles not the same", jf1 == jf2);
133: jf1.close();
134: assertTrue("File should exist", file.exists());
135: new URL("jar:" + BASE.toString() + "/lf.jar!/");
136: con1 = (JarURLConnection) fUrl1.openConnection();
137: jf1 = con1.getJarFile();
138: con2 = (JarURLConnection) fUrl1.openConnection();
139: jf2 = con2.getJarFile();
140: assertTrue("http: JarFiles not the same", jf1 == jf2);
141: jf1.close();
142: }
143:
144: /**
145: * @tests java.net.JarURLConnection.getJarFile()
146: *
147: * Regression test for HARMONY-29
148: */
149: public void test_getJarFile29() throws Exception {
150: File jarFile = File.createTempFile("1+2 3", "test.jar");
151: jarFile.deleteOnExit();
152: JarOutputStream out = new JarOutputStream(new FileOutputStream(
153: jarFile));
154: out.putNextEntry(new ZipEntry("test"));
155: out.closeEntry();
156: out.close();
157:
158: JarURLConnection conn = (JarURLConnection) new URL("jar:file:"
159: + jarFile.getAbsolutePath().replaceAll(" ", "%20")
160: + "!/").openConnection();
161: conn.getJarFile().entries();
162: }
163:
164: //Regression for HARMONY-3436
165: public void test_setUseCaches() throws Exception {
166: File resources = Support_Resources.createTempFolder();
167: Support_Resources.copyFile(resources, null, "hyts_att.jar");
168: File file = new File(resources.toString() + "/hyts_att.jar");
169: URL url = new URL("jar:file:" + file.getPath()
170: + "!/HasAttributes.txt");
171:
172: JarURLConnection connection = (JarURLConnection) url
173: .openConnection();
174: connection.setUseCaches(false);
175: InputStream in = connection.getInputStream();
176: JarFile jarFile1 = connection.getJarFile();
177: JarEntry jarEntry1 = connection.getJarEntry();
178: byte[] data = new byte[1024];
179: while (in.read(data) >= 0)
180: ;
181: in.close();
182: JarFile jarFile2 = connection.getJarFile();
183: JarEntry jarEntry2 = connection.getJarEntry();
184: assertSame(jarFile1, jarFile2);
185: assertSame(jarEntry1, jarEntry2);
186:
187: try {
188: connection.getInputStream();
189: fail("should throw IllegalStateException");
190: } catch (IllegalStateException e) {
191: // expected
192: }
193: }
194:
195: /**
196: * @tests java.net.JarURLConnection#getJarFileURL()
197: */
198: public void test_getJarFileURL() throws Exception {
199: URL fileURL = new URL(BASE.toString() + "/lf.jar");
200: URL u = new URL("jar:" + BASE.toString() + "/lf.jar!/plus.bmp");
201: juc = (JarURLConnection) u.openConnection();
202: assertTrue("Returned incorrect file URL", juc.getJarFileURL()
203: .equals(fileURL));
204: // Regression test for harmony-3053
205: URL url = new URL(
206: "jar:file:///bar.jar!/foo.jar!/Bugs/HelloWorld.class");
207: assertEquals("file:/bar.jar", ((JarURLConnection) url
208: .openConnection()).getJarFileURL().toString());
209: }
210:
211: /**
212: * @tests java.net.JarURLConnection#getMainAttributes()
213: */
214: public void test_getMainAttributes() throws Exception {
215: URL u = new URL("jar:" + BASE.toString() + "/lf.jar!/swt.dll");
216: juc = (JarURLConnection) u.openConnection();
217: java.util.jar.Attributes a = juc.getMainAttributes();
218: assertEquals("Returned incorrect Attributes", "1.0", a
219: .get(java.util.jar.Attributes.Name.MANIFEST_VERSION));
220: }
221:
222: /**
223: * @tests java.net.JarURLConnection#getInputStream()
224: */
225: public void test_getInputStream_DeleteJarFileUsingURLConnection()
226: throws Exception {
227: String jarFileName = "file.jar";
228: String entry = "text.txt";
229: File file = new File(jarFileName);
230: FileOutputStream jarFile = new FileOutputStream(jarFileName);
231: JarOutputStream out = new JarOutputStream(
232: new BufferedOutputStream(jarFile));
233: JarEntry jarEntry = new JarEntry(entry);
234: out.putNextEntry(jarEntry);
235: out.write(new byte[] { 'a', 'b', 'c' });
236: out.close();
237:
238: URL url = new URL("jar:file:" + jarFileName + "!/" + entry);
239: URLConnection conn = url.openConnection();
240: conn.setUseCaches(false);
241: InputStream is = conn.getInputStream();
242: is.close();
243: assertTrue(file.delete());
244: }
245:
246: /**
247: * @tests java.net.JarURLConnection#getManifest()
248: */
249: public void test_getManifest() throws Exception {
250: URL u = new URL("jar:" + BASE.toString() + "/lf.jar!/plus.bmp");
251: juc = (JarURLConnection) u.openConnection();
252: Manifest mf = juc.getManifest();
253: assertNotNull(mf);
254: // equal but not same manifest
255: assertEquals(mf, juc.getManifest());
256: assertNotSame(mf, juc.getManifest());
257: // same main attributes
258: assertEquals(juc.getMainAttributes(), mf.getMainAttributes());
259: }
260:
261: /**
262: * @tests java.net.JarURLConnection#getCertificates()
263: */
264: public void test_getCertificates() throws Exception {
265: URL u = new URL("jar:" + BASE.toString() + "/lf.jar!/plus.bmp");
266: juc = (JarURLConnection) u.openConnection();
267: // read incomplete, shall return null
268: assertNull(juc.getCertificates());
269: assertEquals("Returned incorrect JarEntry", "plus.bmp", juc
270: .getJarEntry().getName());
271: // read them all
272: InputStream is = juc.getInputStream();
273: byte[] buf = new byte[80];
274: while (is.read(buf) > 0)
275: ;
276: // still return null for this type of file
277: assertNull(juc.getCertificates());
278:
279: URL fileURL = new URL("jar:" + BASE.toString() + "/lf.jar!/");
280: juc = (JarURLConnection) fileURL.openConnection();
281: is = juc.getJarFileURL().openStream();
282: while (is.read(buf) > 0)
283: ;
284: // null for this jar file
285: assertNull(juc.getCertificates());
286: }
287:
288: /**
289: * @tests java.net.JarURLConnection#getContentLength()
290: * Regression test for HARMONY-3665
291: */
292: public void test_getContentLength() throws Exception {
293: // check length for jar file itself
294: URL u = new URL("jar:" + BASE.toString() + "/lf.jar!/");
295: assertEquals("Returned incorrect size for jar file", 33095, u
296: .openConnection().getContentLength());
297:
298: // check length for jar entry
299: u = new URL("jar:" + BASE.toString() + "/lf.jar!/plus.bmp");
300: assertEquals("Returned incorrect size for the entry", 190, u
301: .openConnection().getContentLength());
302: }
303:
304: /**
305: * @tests java.net.JarURLConnection#getContentType()
306: * Regression test for HARMONY-3665
307: */
308: public void test_getContentType() throws Exception {
309: // check type for jar file itself
310: URL u = new URL("jar:" + BASE.toString() + "/lf.jar!/");
311: assertEquals("Returned incorrect type for jar file",
312: "x-java/jar", u.openConnection().getContentType());
313:
314: // check type for jar entry with known type
315: u = new URL("jar:" + BASE.toString() + "/lf.jar!/plus.bmp");
316: assertEquals(
317: "Returned incorrect type for the entry with known type",
318: "image/bmp", u.openConnection().getContentType());
319:
320: // check type for jar entry with unknown type
321: u = new URL("jar:" + BASE.toString() + "/lf.jar!/Manifest.mf");
322: assertEquals(
323: "Returned incorrect type for the entry with known type",
324: "content/unknown", u.openConnection().getContentType());
325: }
326:
327: protected void setUp() {
328: }
329:
330: protected void tearDown() {
331: }
332: }
|