harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject svn commit: r531591 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/org/apache/harmony/luni/internal/net/www/protocol/jar/JarURLConnection.java test/java/tests/api/java/net/JarURLConnectionTest.java
Date Mon, 23 Apr 2007 20:37:32 GMT
Author: ayza
Date: Mon Apr 23 13:37:31 2007
New Revision: 531591

URL: http://svn.apache.org/viewvc?view=rev&rev=531591
Log:
The patch from HARMONY-3665 ([classlib][archive][netbeans] JarURLConnection.getContentLength()
returns -1 instead of file size) was applied

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/jar/JarURLConnection.java
    harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/JarURLConnectionTest.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/jar/JarURLConnection.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/jar/JarURLConnection.java?view=diff&rev=531591&r1=531590&r2=531591
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/jar/JarURLConnection.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/jar/JarURLConnection.java
Mon Apr 23 13:37:31 2007
@@ -354,39 +354,63 @@
     }
 
     /**
-     * Answers the content type of the resource. Test cases reveal that only if
-     * the URL is refering to a Jar file, that this method answers a non-null
-     * value - x-java/jar.
+     * Answers the content type of the resource.
+     * For jar file itself "x-java/jar" should be returned,
+     * for jar entries the content type of the entry should be returned.
+     * Returns non-null results ("content/unknown" for unknown types).
      * 
      * @return the content type
      */
     @Override
     public String getContentType() {
-        // it could also return "x-java/jar" which jdk returns but here, we get
-        // it from the URLConnection
-        try {
-            if (url.getFile().endsWith("!/")) { //$NON-NLS-1$
-                return getJarFileURL().openConnection().getContentType();
+        if (url.getFile().endsWith("!/")) { //$NON-NLS-1$
+            // the type for jar file itself is always "x-java/jar"
+            return "x-java/jar"; //$NON-NLS-1$
+        } else {
+            String cType = null;
+            String entryName = getEntryName();
+
+            if (entryName != null) {
+                // if there is an Jar Entry, get the content type from the name
+                cType = guessContentTypeFromName(entryName);
+            } else {
+                try {
+                    if (!connected) {
+                        connect();
+                    }
+
+                    cType = jarFileURLConnection.getContentType();
+                } catch (IOException ioe) {
+                    // Ignore
+                }
             }
-        } catch (IOException ioe) {
-            // Ignore
+
+            if (cType == null) {
+                cType = "content/unknown"; //$NON-NLS-1$
+            }
+            return cType;
         }
-        // if there is an Jar Entry, get the content type from the name
-        return guessContentTypeFromName(url.getFile());
     }
 
     /**
      * Answers the content length of the resource. Test cases reveal that if the
      * URL is refering to a Jar file, this method answers a content-length
-     * returned by URLConnection. If not, it will return -1.
+     * returned by URLConnection. For jar entry it should return it's size.
+     * Otherwise, it will return -1.
      * 
      * @return the content length
      */
     @Override
     public int getContentLength() {
         try {
-            if (url.getFile().endsWith("!/")) { //$NON-NLS-1$
-                return getJarFileURL().openConnection().getContentLength();
+            if (!connected) {
+                connect();
+            }
+
+            if (jarEntry == null) {
+                return jarFileURLConnection.getContentLength();
+            } else {
+                return (int) getJarEntry().getSize();
             }
         } catch (IOException e) {
             //Ignored

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/JarURLConnectionTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/JarURLConnectionTest.java?view=diff&rev=531591&r1=531590&r2=531591
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/JarURLConnectionTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/JarURLConnectionTest.java
Mon Apr 23 13:37:31 2007
@@ -272,6 +272,48 @@
         assertNull(juc.getCertificates());
     }
 
+    /**
+     * @tests java.net.JarURLConnection#getContentLength()
+     * Regression test for HARMONY-3665
+     */
+    public void test_getContentLength() throws Exception {
+        // check length for jar file itself
+        URL u = new URL("jar:"
+                + BASE.toString()+"/lf.jar!/");
+        assertEquals("Returned incorrect size for jar file", 33095,
+                u.openConnection().getContentLength());
+
+        // check length for jar entry
+        u = new URL("jar:"
+                + BASE.toString()+"/lf.jar!/plus.bmp");
+        assertEquals("Returned incorrect size for the entry", 190,
+                u.openConnection().getContentLength());
+    }
+
+    /**
+     * @tests java.net.JarURLConnection#getContentType()
+     * Regression test for HARMONY-3665
+     */
+    public void test_getContentType() throws Exception {
+        // check type for jar file itself
+        URL u = new URL("jar:"
+                + BASE.toString()+"/lf.jar!/");
+        assertEquals("Returned incorrect type for jar file", "x-java/jar",
+                u.openConnection().getContentType());
+
+        // check type for jar entry with known type
+        u = new URL("jar:"
+                + BASE.toString()+"/lf.jar!/plus.bmp");
+        assertEquals("Returned incorrect type for the entry with known type",
+                "image/bmp", u.openConnection().getContentType());
+
+        // check type for jar entry with unknown type
+        u = new URL("jar:"
+                + BASE.toString()+"/lf.jar!/Manifest.mf");
+        assertEquals("Returned incorrect type for the entry with known type",
+                "content/unknown", u.openConnection().getContentType());
+    }
+
 	protected void setUp() {
 	}
 



Mime
View raw message