harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ghar...@apache.org
Subject svn commit: r408883 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/net/ main/java/org/apache/harmony/luni/internal/net/www/protocol/http/ test/java/tests/api/java/net/
Date Tue, 23 May 2006 12:51:51 GMT
Author: gharley
Date: Tue May 23 05:51:51 2006
New Revision: 408883

URL: http://svn.apache.org/viewvc?rev=408883&view=rev
Log:
HARMONY 462 : Java 5 Enhancement - implements cache-related issue for java.net.URLConnection

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLConnection.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/Header.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/HttpURLConnectionTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLConnection.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLConnection.java?rev=408883&r1=408882&r2=408883&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLConnection.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLConnection.java
Tue May 23 05:51:51 2006
@@ -47,7 +47,7 @@
 
 	private static boolean defaultAllowUserInteraction = false;
 
-	private static boolean defaultUseCaches = false;
+	private static boolean defaultUseCaches = true;
 
 	ContentHandler defaultHandler = new DefaultContentHandler();
 

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/Header.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/Header.java?rev=408883&r1=408882&r2=408883&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/Header.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/Header.java
Tue May 23 05:51:51 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedList;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -52,6 +53,29 @@
     public Header() {
     }
 
+    /**
+     * The alternative constructor which sets the input map as its initial
+     * keyTable.
+     * 
+     * @param map
+     *            the initial keyTable as a map
+     */
+    public Header(Map map) {
+        for (Iterator entries = map.entrySet().iterator(); entries.hasNext();) {
+            Map.Entry next = (Map.Entry) entries.next();
+            String key = (String) next.getKey();
+            props.add(key);
+            List value = (List) next.getValue();
+            LinkedList linkedList = new LinkedList();
+            for (Iterator iter = value.iterator(); iter.hasNext();) {
+                String element = (String) iter.next();
+                linkedList.add(element);
+                props.add(element);
+            }
+            keyTable.put(key, linkedList);
+        }
+    }
+    
     public Object clone() {
         try {
             Header clone = (Header) super.clone();

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java?rev=408883&r1=408882&r2=408883&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java
Tue May 23 05:51:51 2006
@@ -22,12 +22,15 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.Authenticator;
+import java.net.CacheRequest;
+import java.net.CacheResponse;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.PasswordAuthentication;
 import java.net.ProtocolException;
 import java.net.Proxy;
 import java.net.ProxySelector;
+import java.net.ResponseCache;
 import java.net.Socket;
 import java.net.SocketAddress;
 import java.net.SocketPermission;
@@ -70,6 +73,16 @@
     private InputStream uis;
 
     OutputStream socketOut;
+    
+    OutputStream cacheOut;
+    
+    private ResponseCache responseCache;
+    
+    private CacheResponse cacheResponse;
+    
+    private CacheRequest cacheRequest;
+        
+    private boolean hasTriedCache = false;
 
     private HttpOutputStream os;
 
@@ -112,6 +125,11 @@
         public void close() throws IOException {
             bytesRemaining = 0;
             closeSocket();
+            // if user has set useCache to true and cache exists, aborts it when
+            // closing
+            if (useCaches && null != cacheRequest) {
+                cacheRequest.abort();
+            }
         }
 
         public int available() throws IOException {
@@ -125,6 +143,11 @@
             if (bytesRemaining <= 0)
                 return -1;
             int result = is.read();
+            // if user has set useCache to true and cache exists, writes to
+            // cache
+            if (useCaches && null != cacheOut) {
+                cacheOut.write(result);
+            }
             bytesRemaining--;
             return result;
         }
@@ -141,8 +164,14 @@
             if (length > bytesRemaining)
                 length = bytesRemaining;
             int result = is.read(buf, offset, length);
-            if (result > 0)
+            if (result > 0) {
                 bytesRemaining -= result;
+                // if user has set useCache to true and cache exists, writes to
+                // cache
+                if (useCaches && null != cacheOut) {
+                    cacheOut.write(buf, offset, result);
+                }
+            }
             return result;
         }
 
@@ -170,6 +199,11 @@
         public void close() throws IOException {
             atEnd = true;
             closeSocket();
+            // if user has set useCache to true and cache exists, abort when
+            // closing
+            if (useCaches && null != cacheRequest) {
+                cacheRequest.abort();
+            }
         }
 
         public int available() throws IOException {
@@ -201,7 +235,12 @@
             if (atEnd)
                 return -1;
             bytesRemaining--;
-            return is.read();
+            int result = is.read();
+            // if user has set useCache to true and cache exists, write to cache
+            if (useCaches && null != cacheOut) {
+                cacheOut.write(result);
+            }
+            return result;
         }
 
         public int read(byte[] buf, int offset, int length) throws IOException {
@@ -218,8 +257,14 @@
             if (length > bytesRemaining)
                 length = bytesRemaining;
             int result = is.read(buf, offset, length);
-            if (result > 0)
+            if (result > 0) {
                 bytesRemaining -= result;
+                // if user has set useCache to true and cache exists, write to
+                // cache
+                if (useCaches && null != cacheOut) {
+                    cacheOut.write(buf, offset, result);
+                }
+            }
             return result;
         }
 
@@ -434,6 +479,7 @@
         } catch (URISyntaxException e) {
             // do nothing.
         }
+        responseCache = ResponseCache.getDefault();
     }
 
     /**
@@ -466,6 +512,9 @@
     public void connect() throws IOException {
         if (connected)
             return;
+        if (getFromCache()) {
+            return;
+        }
         Socket socket;
         int connectTimeout = getConnectTimeout();
         InetAddress host = getHostAddress();
@@ -482,6 +531,43 @@
         socketOut = socket.getOutputStream();
         is = new BufferedInputStream(socket.getInputStream());
     }
+    
+    // Tries to get head and body from cache, return true if has got this time or
+    // already got before
+    private boolean getFromCache() throws IOException {
+        if (useCaches && null != responseCache && !hasTriedCache) {
+            hasTriedCache = true;
+            if (null == resHeader) {
+                resHeader = new Header();
+            }
+            cacheResponse = responseCache.get(uri, method, resHeader.getFieldMap());
+            if (null != cacheResponse) {
+                Map headMap = cacheResponse.getHeaders();
+                if (null!=headMap){
+                    resHeader = new Header(headMap);
+                }
+                is = cacheResponse.getBody();
+                if (null != is) {
+                    return true;
+                }
+            }
+        }
+        if (hasTriedCache && null != is) {
+            return true;
+        }
+        return false;
+    }
+
+    // if user sets useCache to true, tries to put response to cache if cache
+    // exists
+    private void putToCache() throws IOException {
+        if (useCaches && null != responseCache) {
+            cacheRequest = responseCache.put(uri, this);
+            if (null != cacheRequest) {
+                cacheOut = cacheRequest.getBody();
+            }
+        }
+    }
 
     /**
      * Closes the connection with the HTTP server
@@ -536,11 +622,11 @@
      * Answers <code>null</code> if there is fewer than <code>pos</code>
fields
      * in the response header.
      *
-     * @return java.lang.String		The value of the field
-     * @param pos int				the position of the field from the top
+     * @return java.lang.String     The value of the field
+     * @param pos int               the position of the field from the top
      *
-     * @see 		#getHeaderField(String)
-     * @see 		#getHeaderFieldKey
+     * @see         #getHeaderField(String)
+     * @see         #getHeaderFieldKey
      */
     public String getHeaderField(int pos) {
         try {
@@ -605,7 +691,13 @@
      * @since 1.4
      */
     public Map getHeaderFields() {
-        return resHeader.getFieldMap();
+        try {
+            // ensure that resHeader exists
+            getInputStream();
+            return resHeader.getFieldMap();
+        } catch (IOException e) {
+            return null;
+        }        
     }
 
     /**
@@ -735,6 +827,10 @@
             doRequest();
             return os;
         }
+        if(!connected){
+            // connect and see if there is cache available.
+            connect();
+        }
         return os = new HttpOutputStream();
 
     }
@@ -820,6 +916,10 @@
         // make sure we have a connection
         if (!connected)
             connect();
+        if (null != cacheResponse) {
+            // does not send if already has a response cache
+            return true;
+        }
         // send out the HTTP request
         socketOut.write(request);
         sentRequest = true;
@@ -854,6 +954,7 @@
             closeSocket();
             uis = new LimitedInputStream(0);
         }
+        putToCache();
     }
 
     /**

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/HttpURLConnectionTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/HttpURLConnectionTest.java?rev=408883&r1=408882&r2=408883&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/HttpURLConnectionTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/HttpURLConnectionTest.java
Tue May 23 05:51:51 2006
@@ -18,12 +18,19 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.net.CacheRequest;
+import java.net.CacheResponse;
 import java.net.ConnectException;
 import java.net.HttpURLConnection;
+import java.net.ResponseCache;
+import java.net.URI;
 import java.net.URL;
 import java.net.URLConnection;
 import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.Hashtable;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 
 import tests.support.Support_Configuration;
@@ -34,6 +41,18 @@
 	URL url;
 
 	HttpURLConnection uc;
+    
+    private boolean isGetCalled;
+
+    private boolean isPutCalled;
+
+    private boolean isCacheWriteCalled;
+
+    private boolean isAbortCalled;
+
+    private Map<String, List<String>> mockHeaderMap;
+    
+    private InputStream mockIs = new MockInputStream();
 
 	/**
 	 * @tests java.net.HttpURLConnection#getResponseCode()
@@ -313,34 +332,6 @@
         assertEquals(1, mock.getChunkLength());
     }
 
-    class MockHttpConnection extends HttpURLConnection {
-
-        protected MockHttpConnection(URL url) {
-            super(url);
-        }
-
-        public void disconnect() {
-            // do nothing
-        }
-
-        public boolean usingProxy() {
-            return false;
-        }
-
-        public void connect() throws IOException {
-            // do nothing
-        }
-
-        public int getChunkLength() {
-            return super.chunkLength;
-        }
-
-        public int getFixedLength() {
-            return super.fixedContentLength;
-        }
-
-    }
-
     /**
      * @tests java.net.HttpURLConnection#setFixedLengthStreamingMode_I()
      */
@@ -402,23 +393,385 @@
     }
     
     public void test_getOutputStream_afterConnection() throws Exception {
-        URLConnection uc = new URL("http://www.apache.org").openConnection();
+        URLConnection uc = new URL("http://"
+                + Support_Configuration.InetTestAddress).openConnection();
         uc.setDoOutput(true);
         uc.connect();
         assertNotNull(uc.getOutputStream());
     }
     
-	protected void setUp() {
-		try {
-			url = new URL(Support_Resources
-					.getResourceURL("/URLConnectionTest/Harmony.html"));
-			uc = (HttpURLConnection) url.openConnection();
-		} catch (Exception e) {
-			fail("Exception during setup : " + e.getMessage());
-		}
-	}
 
-	protected void tearDown() {
-		uc.disconnect();
-	}
+    /**
+     * @tests java.net.URLConnection#setUseCaches() and its real implementation
+     *        in HttpURLConnection using GetInputStream() and Connect()
+     */
+    public void test_UseCache_HttpURLConnection_Connect_GetInputStream()
+            throws Exception {
+        // set cache before URLConnection created, or it does not take effect
+        ResponseCache rc = new MockNonCachedResponseCache();
+        ResponseCache.setDefault(rc);
+        URLConnection uc = new URL("http://"
+                + Support_Configuration.InetTestAddress).openConnection();
+        assertFalse(isGetCalled);
+        uc.setUseCaches(true);
+        uc.setDoOutput(true);
+        uc.connect();
+        assertTrue(isGetCalled);
+        assertFalse(isPutCalled);
+        InputStream is = uc.getInputStream();
+        assertTrue(isPutCalled);
+        is.close();
+        ((HttpURLConnection)uc).disconnect();
+    }
+
+    /**
+     * @tests java.net.URLConnection#setUseCaches() and its real implementation
+     *        in HttpURLConnection using GetOutputStream() and Connect()
+     */
+    public void test_UseCache_HttpURLConnection_Connect_GetOutputStream()
+            throws Exception {
+        // set cache before URLConnection created, or it does not take effect
+        ResponseCache rc = new MockNonCachedResponseCache();
+        ResponseCache.setDefault(rc);
+        uc.setUseCaches(true);
+        URLConnection uc = new URL("http://"
+                + Support_Configuration.InetTestAddress).openConnection();
+        uc.setDoOutput(true);
+        assertFalse(isGetCalled);
+        uc.connect();
+        assertTrue(isGetCalled);
+        assertFalse(isPutCalled);
+        OutputStream os = uc.getOutputStream();
+        assertFalse(isPutCalled);
+        os.close();
+        ((HttpURLConnection)uc).disconnect();
+    }
+
+    /**
+     * @tests java.net.URLConnection#setUseCaches() and its real implementation
+     *        in HttpURLConnection using GetOutputStream()
+     */
+    public void test_UseCache_HttpURLConnection_GetOutputStream()
+            throws Exception {
+        // set cache before URLConnection created, or it does not take effect
+        ResponseCache rc = new MockNonCachedResponseCache();
+        ResponseCache.setDefault(rc);
+        URLConnection uc = new URL("http://"
+                + Support_Configuration.InetTestAddress).openConnection();
+        assertFalse(isGetCalled);
+        uc.setDoOutput(true);
+        uc.setUseCaches(true);
+        OutputStream os = uc.getOutputStream();
+        assertTrue(isGetCalled);
+        assertFalse(isPutCalled);
+        os.write(1);
+        os.flush();
+        os.close();
+        ((HttpURLConnection) uc).getResponseCode();
+        assertTrue(isGetCalled);
+        assertTrue(isPutCalled);
+        isGetCalled = false;
+        isPutCalled = false;
+        InputStream is = uc.getInputStream();
+        assertFalse(isGetCalled);
+        assertFalse(isPutCalled);
+        is.close();
+        ((HttpURLConnection)uc).disconnect();
+    }
+
+    /**
+     * @tests java.net.URLConnection#setUseCaches() and its real implementation
+     *        in HttpURLConnection using GetInputStream()
+     */
+    public void test_UseCache_HttpURLConnection_GetInputStream()
+            throws Exception {
+        // set cache before URLConnection created, or it does not take effect
+        ResponseCache rc = new MockNonCachedResponseCache();
+        ResponseCache.setDefault(rc);
+        URLConnection uc = new URL("http://"
+                + Support_Configuration.InetTestAddress).openConnection();
+        assertFalse(isGetCalled);
+        uc.setDoOutput(true);
+        uc.setUseCaches(true);
+        InputStream is = uc.getInputStream();
+        assertTrue(isGetCalled);
+        assertTrue(isPutCalled);
+        ((HttpURLConnection) uc).getResponseCode();
+        is.close();
+        ((HttpURLConnection)uc).disconnect();
+    }
+
+    /**
+     * @tests java.net.URLConnection#setUseCaches() and its real implementation
+     *        in HttpURLConnection using a MockResponseCache returns cache of
+     *        null
+     */
+    public void test_UseCache_HttpURLConnection_NonCached() throws IOException {
+        ResponseCache.setDefault(new MockNonCachedResponseCache());
+        URL u = new URL("http://"
+                + Support_Configuration.InetTestAddress);
+        HttpURLConnection uc = (HttpURLConnection) u.openConnection();
+
+        // default useCaches is true
+        assertTrue(uc.getUseCaches());
+
+        // make sure ResponseCache.get/put is called
+        isGetCalled = false;
+        isPutCalled = false;
+        InputStream is = uc.getInputStream();
+        assertFalse(is instanceof MockInputStream);
+        assertTrue(isGetCalled);
+        assertTrue(isPutCalled);
+
+        // make sure protocol handler has tried to write to cache.
+        isCacheWriteCalled = false;
+        is.read();
+        assertTrue(isCacheWriteCalled);
+
+        // make sure protocol handler has tried to write to cache.
+        isCacheWriteCalled = false;
+        byte[] buf = new byte[1];
+        is.read(buf);
+        assertTrue(isCacheWriteCalled);
+
+        // make sure protocol handler has tried to write to cache.
+        isCacheWriteCalled = false;
+        buf = new byte[1];
+        is.read(buf, 0, 1);
+        assertTrue(isCacheWriteCalled);
+
+        // make sure protocol handler has tried to call abort.
+        isAbortCalled = false;
+        is.close();
+        assertTrue(isAbortCalled);
+        uc.disconnect();        
+    }
+
+    /**
+     * @tests java.net.URLConnection#setUseCaches() and its real implementation
+     *        in HttpURLConnection using a MockResponseCache returns a mock
+     *        cache
+     */
+    public void test_UseCache_HttpURLConnection_Cached() throws IOException {
+        ResponseCache.setDefault(new MockCachedResponseCache());
+        URL u = new URL("http://"
+                + Support_Configuration.InetTestAddress);
+        HttpURLConnection uc = (HttpURLConnection) u.openConnection();
+
+        // default useCaches is true
+        assertTrue(uc.getUseCaches());
+
+        // make sure ResponseCache.get/put is called
+        isGetCalled = false;
+        isPutCalled = false;
+        InputStream is = uc.getInputStream();
+        assertTrue(is instanceof MockInputStream);
+        assertTrue(isGetCalled);
+
+        // make sure protocol handler doesn't try to write to cache, since
+        // it has been in cache already.
+        isCacheWriteCalled = false;
+        is.read();
+        assertFalse(isCacheWriteCalled);
+
+        // make sure protocol handler doesn't try to write to cache, since
+        // it has been in cache already.
+        isCacheWriteCalled = false;
+        byte[] buf = new byte[1];
+        is.read(buf);
+        assertFalse(isCacheWriteCalled);
+
+        // make sure protocol handler doesn't try to write to cache, since
+        // it has been in cache already.
+        isCacheWriteCalled = false;
+        buf = new byte[1];
+        is.read(buf, 0, 1);
+        assertFalse(isCacheWriteCalled);
+
+        // make sure abort is not called since no write is performed
+        isAbortCalled = false;
+        is.close();
+        assertFalse(isAbortCalled);
+        uc.disconnect();
+    }
+
+    /**
+     * @tests java.net.URLConnection#setUseCaches() and its real implementation
+     *        in HttpURLConnection using getHeaderFields()
+     */
+    public void test_UseCache_HttpURLConnection_getHeaderFields()
+            throws IOException {
+        ResponseCache.setDefault(new MockCachedResponseCache());
+        URL u = new URL("http://"
+                + Support_Configuration.InetTestAddress);
+        HttpURLConnection uc = (HttpURLConnection) u.openConnection();
+        Map<String, List<String>> headerMap = uc.getHeaderFields();
+        assertTrue(isGetCalled);
+        assertFalse(isPutCalled);
+        assertEquals(mockHeaderMap, headerMap);
+        assertEquals(uc.getInputStream(),mockIs);
+        uc.disconnect();
+    }
+
+    /**
+     * @tests java.net.URLConnection#setUseCaches() and its real implementation
+     *        in HttpURLConnection using GetOutputStream()
+     */
+    public void test_UseCache_HttpURLConnection_NoCached_GetOutputStream()
+            throws Exception {
+        ResponseCache.setDefault(new MockNonCachedResponseCache());
+        URL u = new URL("http://"
+                + Support_Configuration.InetTestAddress);
+        HttpURLConnection uc = (HttpURLConnection) u.openConnection();
+        uc.setChunkedStreamingMode(10);
+        uc.setDoOutput(true);        
+        uc.getOutputStream();
+        assertTrue(isGetCalled);
+        assertFalse(isPutCalled);
+        assertFalse(isAbortCalled);
+        uc.disconnect();
+    }
+
+    class MockNonCachedResponseCache extends ResponseCache {
+
+        public CacheResponse get(URI arg0, String arg1, Map arg2)
+                throws IOException {
+            isGetCalled = true;
+            return null;
+        }
+
+        public CacheRequest put(URI arg0, URLConnection arg1)
+                throws IOException {
+            isPutCalled = true;
+            return new MockCacheRequest();
+        }
+    }
+
+    class MockCachedResponseCache extends ResponseCache {
+
+        public CacheResponse get(URI arg0, String arg1, Map arg2)
+                throws IOException {
+            if (null == arg0 || null == arg1 || null == arg2) {
+                throw new NullPointerException();
+            }
+            isGetCalled = true;
+            return new MockCacheResponse();
+        }
+
+        public CacheRequest put(URI arg0, URLConnection arg1)
+                throws IOException {
+            if (null == arg0 || null == arg1) {
+                throw new NullPointerException();
+            }
+            isPutCalled = true;
+            return new MockCacheRequest();
+        }
+    }
+
+    class MockCacheRequest extends CacheRequest {
+
+        public OutputStream getBody() throws IOException {
+            isCacheWriteCalled = true;
+            return new MockOutputStream();
+        }
+
+        public void abort() {
+            isAbortCalled = true;
+        }
+
+    }
+
+    class MockCacheResponse extends CacheResponse {
+
+        public Map<String, List<String>> getHeaders() throws IOException {
+            return mockHeaderMap;
+        }
+
+        public InputStream getBody() throws IOException {
+            return mockIs;
+        }
+    }
+
+    class MockInputStream extends InputStream {
+
+        public int read() throws IOException {
+            return 1;
+        }
+
+        public int read(byte[] arg0, int arg1, int arg2) throws IOException {
+            return 1;
+        }
+
+        public int read(byte[] arg0) throws IOException {
+            return 1;
+        }
+
+    }
+
+    class MockOutputStream extends OutputStream {
+
+        public void write(int b) throws IOException {
+            isCacheWriteCalled = true;
+        }
+
+        public void write(byte[] b, int off, int len) throws IOException {
+            isCacheWriteCalled = true;
+        }
+
+        public void write(byte[] b) throws IOException {
+            isCacheWriteCalled = true;
+        }
+    }
+    
+
+    class MockHttpConnection extends HttpURLConnection {
+
+        protected MockHttpConnection(URL url) {
+            super(url);
+        }
+
+        public void disconnect() {
+            // do nothing
+        }
+
+        public boolean usingProxy() {
+            return false;
+        }
+
+        public void connect() throws IOException {
+            // do nothing
+        }
+
+        public int getChunkLength() {
+            return super.chunkLength;
+        }
+
+        public int getFixedLength() {
+            return super.fixedContentLength;
+        }
+
+    }
+
+    protected void setUp() {
+        try {
+            url = new URL(Support_Resources
+                    .getResourceURL("/URLConnectionTest/Harmony.html"));
+            uc = (HttpURLConnection) url.openConnection();
+        } catch (Exception e) {
+            fail("Exception during setup : " + e.getMessage());
+        }
+        mockHeaderMap = new Hashtable<String, List<String>>();
+        List<String> valueList = new ArrayList<String>();
+        valueList.add("value1");
+        mockHeaderMap.put("field1", valueList);
+        mockHeaderMap.put("field2", valueList);
+        isGetCalled = false;
+        isPutCalled = false;
+        isCacheWriteCalled = false;
+    }
+	
+    protected void tearDown() {
+        uc.disconnect();
+        ResponseCache.setDefault(null);
+    }
 }



Mime
View raw message