harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From odea...@apache.org
Subject svn commit: r964029 - in /harmony/enhanced/java/trunk/classlib/modules/luni/src: main/java/java/net/URLConnection.java test/api/common/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java
Date Wed, 14 Jul 2010 12:31:32 GMT
Author: odeakin
Date: Wed Jul 14 12:31:32 2010
New Revision: 964029

URL: http://svn.apache.org/viewvc?rev=964029&view=rev
Log:
Add implementation of add/get/setRequestProperty() and getRequestProperties() methods which
are currently stubs. Add tests for their behaviour.

Modified:
    harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/net/URLConnection.java
    harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java

Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/net/URLConnection.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/net/URLConnection.java?rev=964029&r1=964028&r2=964029&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/net/URLConnection.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/net/URLConnection.java
Wed Jul 14 12:31:32 2010
@@ -22,8 +22,10 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
@@ -103,6 +105,11 @@ public abstract class URLConnection {
     static Hashtable<String, Object> contentHandlers = new Hashtable<String, Object>();
 
     /**
+     * HashMap for storing request property key-valuelist pairs
+     */
+    private HashMap<String, List<String>> requestProperties = new HashMap<String,
List<String>>();
+
+    /**
      * A hashtable that maps the filename extension (key) to a MIME-type
      * (element)
      */
@@ -441,7 +448,12 @@ public abstract class URLConnection {
         if (connected) {
             throw new IllegalStateException(Messages.getString("luni.5E")); //$NON-NLS-1$
         }
-        return Collections.emptyMap();
+
+        HashMap<String, List<String>> map = new HashMap<String, List<String>>();
+        for (String key : requestProperties.keySet()) {
+            map.put(key, Collections.unmodifiableList(requestProperties.get(key)));
+        }
+        return Collections.unmodifiableMap(map);
     }
 
     /**
@@ -465,6 +477,15 @@ public abstract class URLConnection {
         if (field == null) {
             throw new NullPointerException(Messages.getString("luni.95")); //$NON-NLS-1$
         }
+
+        List<String> valuesList = requestProperties.get(field);
+        if (valuesList == null) {
+            valuesList = new ArrayList<String>();
+            valuesList.add(0, newValue);
+            requestProperties.put(field, valuesList);
+        } else {
+            valuesList.add(0, newValue);
+        }
     }
 
     /**
@@ -607,8 +628,7 @@ public abstract class URLConnection {
 
     /**
      * Gets the value of the request header property specified by {code field}
-     * or {@code null} if there is no field with this name. The current
-     * implementation of this method returns always {@code null}.
+     * or {@code null} if there is no field with this name.
      * 
      * @param field
      *            the name of the request header property.
@@ -620,7 +640,11 @@ public abstract class URLConnection {
         if (connected) {
             throw new IllegalStateException(Messages.getString("luni.5E")); //$NON-NLS-1$
         }
-        return null;
+        List<String> valuesList = requestProperties.get(field);
+        if (valuesList == null) {
+            return null;
+        }
+        return valuesList.get(0);
     }
 
     /**
@@ -955,6 +979,10 @@ public abstract class URLConnection {
         if (field == null) {
             throw new NullPointerException(Messages.getString("luni.95")); //$NON-NLS-1$
         }
+
+        List<String> valuesList = new ArrayList<String>();
+        valuesList.add(newValue);
+        requestProperties.put(field, valuesList);
     }
 
     /**

Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java?rev=964029&r1=964028&r2=964029&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java
Wed Jul 14 12:31:32 2010
@@ -38,7 +38,9 @@ import java.net.URLConnection;
 import java.net.URLStreamHandler;
 import java.security.Permission;
 import java.util.Arrays;
+import java.util.ArrayList;
 import java.util.Calendar;
+import java.util.Collections;
 import java.util.GregorianCalendar;
 import java.util.List;
 import java.util.Map;
@@ -129,6 +131,32 @@ public class URLConnectionTest extends j
             // expected
         }
 
+        u.addRequestProperty("key", "value");
+        u.addRequestProperty("key", "value2");
+        assertEquals("value2", u.getRequestProperty("key"));
+        ArrayList list = new ArrayList();
+        list.add("value2");
+        list.add("value");
+
+        Map<String,List<String>> propertyMap = u.getRequestProperties();
+        // Check this map is unmodifiable
+        try {
+            propertyMap.put("test", null);
+            fail("Map returned by URLConnection.getRequestProperties() should be unmodifiable");
+        } catch (UnsupportedOperationException e) {
+            // Expected
+        }
+
+        List<String> valuesList = propertyMap.get("key");
+        // Check this list is also unmodifiable
+        try {
+            valuesList.add("test");
+            fail("List entries in the map returned by URLConnection.getRequestProperties()
should be unmodifiable");
+        } catch (UnsupportedOperationException e) {
+            // Expected
+        }
+        assertEquals(list, valuesList);
+
         u.connect();
         try {
             // state of connection is checked first
@@ -1020,7 +1048,7 @@ public class URLConnectionTest extends j
         URL url = new URL("http", "test", 80, "index.html", new NewHandler());
         URLConnection urlCon = url.openConnection();
         urlCon.setRequestProperty("test", "testProperty");
-        assertNull(urlCon.getRequestProperty("test"));
+        assertEquals("testProperty", urlCon.getRequestProperty("test"));
 
         urlCon.connect();
         try {
@@ -1293,8 +1321,16 @@ public class URLConnectionTest extends j
      * @tests java.net.URLConnection#setRequestProperty(java.lang.String,
      *        java.lang.String)
      */
-    public void test_setRequestPropertyLjava_lang_StringLjava_lang_String() {
-        assertTrue("Used to test", true);
+    public void test_setRequestPropertyLjava_lang_StringLjava_lang_String() 
+                throws MalformedURLException{
+        MockURLConnection u = new MockURLConnection(new URL(
+                "http://www.apache.org"));
+
+        u.setRequestProperty("", "");
+        assertEquals("", u.getRequestProperty(""));
+
+        u.setRequestProperty("key", "value");
+        assertEquals("value", u.getRequestProperty("key"));
     }
 
     /**



Mime
View raw message