Fix URLConnectionTest#test_getAllowUserInteraction.

Also improve the documentation, make it possible to run these tests
individually outside of CTS with vogar, and remove a few more URLs of
external web servers.

We should clean up all tests to remove all reliance on external web servers.

Bug: http://code.google.com/p/android/issues/detail?id=35400

(cherry-picked from 3827b65b1937acfbf3abbc449f8ba0ffc60f3cf3.)

Conflicts:

	luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java

Change-Id: I4959fefa130290236533be72cce7c57b9ea1e296
diff --git a/luni/src/main/java/java/net/URLConnection.java b/luni/src/main/java/java/net/URLConnection.java
index 7cf71d5..c832bfb 100644
--- a/luni/src/main/java/java/net/URLConnection.java
+++ b/luni/src/main/java/java/net/URLConnection.java
@@ -123,8 +123,8 @@
     protected boolean doInput = true;
 
     /**
-     * Specifies whether this {@code URLConnection} allows user interaction as
-     * it is needed for authentication purposes.
+     * Unused by Android. This field can be accessed via {@link #getAllowUserInteraction}
+     * and {@link #setAllowUserInteraction}.
      */
     protected boolean allowUserInteraction = defaultAllowUserInteraction;
 
@@ -167,11 +167,7 @@
     public abstract void connect() throws IOException;
 
     /**
-     * Returns the option value which indicates whether user interaction is allowed
-     * on this {@code URLConnection}.
-     *
-     * @return the value of the option {@code allowUserInteraction}.
-     * @see #allowUserInteraction
+     * Returns {@code allowUserInteraction}. Unused by Android.
      */
     public boolean getAllowUserInteraction() {
         return allowUserInteraction;
@@ -342,11 +338,7 @@
     }
 
     /**
-     * Returns the default setting whether this connection allows user interaction.
-     *
-     * @return the value of the default setting {@code
-     *         defaultAllowUserInteraction}.
-     * @see #allowUserInteraction
+     * Returns the default value of {@code allowUserInteraction}. Unused by Android.
      */
     public static boolean getDefaultAllowUserInteraction() {
         return defaultAllowUserInteraction;
@@ -804,16 +796,7 @@
     }
 
     /**
-     * Sets the flag indicating whether this connection allows user interaction
-     * or not. This method can only be called prior to the connection
-     * establishment.
-     *
-     * @param newValue
-     *            the value of the flag to be set.
-     * @throws IllegalStateException
-     *             if this method attempts to change the flag after the
-     *             connection has been established.
-     * @see #allowUserInteraction
+     * Sets {@code allowUserInteraction}. Unused by Android.
      */
     public void setAllowUserInteraction(boolean newValue) {
         checkNotConnected();
@@ -837,14 +820,7 @@
     }
 
     /**
-     * Sets the default value for the flag indicating whether this connection
-     * allows user interaction or not. Existing {@code URLConnection}s are
-     * unaffected.
-     *
-     * @param allows
-     *            the default value of the flag to be used for new connections.
-     * @see #defaultAllowUserInteraction
-     * @see #allowUserInteraction
+     * Sets the default value for {@code allowUserInteraction}. Unused by Android.
      */
     public static void setDefaultAllowUserInteraction(boolean allows) {
         defaultAllowUserInteraction = allows;
diff --git a/luni/src/main/java/libcore/net/url/FileURLConnection.java b/luni/src/main/java/libcore/net/url/FileURLConnection.java
index bc41169..b4654cd 100644
--- a/luni/src/main/java/libcore/net/url/FileURLConnection.java
+++ b/luni/src/main/java/libcore/net/url/FileURLConnection.java
@@ -38,7 +38,7 @@
  */
 public class FileURLConnection extends URLConnection {
 
-    String fileName;
+    private String filename;
 
     private InputStream is;
 
@@ -56,11 +56,11 @@
      */
     public FileURLConnection(URL url) {
         super(url);
-        fileName = url.getFile();
-        if (fileName == null) {
-            fileName = "";
+        filename = url.getFile();
+        if (filename == null) {
+            filename = "";
         }
-        fileName = UriCodec.decode(fileName);
+        filename = UriCodec.decode(filename);
     }
 
     /**
@@ -73,7 +73,7 @@
      */
     @Override
     public void connect() throws IOException {
-        File f = new File(fileName);
+        File f = new File(filename);
         if (f.isDirectory()) {
             isDir = true;
             is = getDirectoryListing(f);
@@ -196,7 +196,7 @@
     @Override
     public java.security.Permission getPermission() throws IOException {
         if (permission == null) {
-            String path = fileName;
+            String path = filename;
             if (File.separatorChar != '/') {
                 path = path.replace('/', File.separatorChar);
             }
diff --git a/luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java b/luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java
index 9f414c3..3ca11f7 100644
--- a/luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java
+++ b/luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java
@@ -220,8 +220,6 @@
     public void setUp() throws Exception {
         super.setUp();
 
-//        ftpURL = new URL(Support_Configuration.testFTPURL);
-
         port = Support_PortManager.getNextPort();
         server = new Support_TestWebServer();
         server.initServer(port, false);
@@ -243,9 +241,6 @@
         server.close();
         ((HttpURLConnection) uc).disconnect();
         ((HttpURLConnection) uc2).disconnect();
-//        if (((FtpURLConnection) ftpURLCon).getInputStream() !=  null) {
-//        ((FtpURLConnection) ftpURLCon).getInputStream().close();
-//        }
     }
 
     /**
@@ -312,66 +307,21 @@
         // TODO: test User-Agent?
     }
 
-    /**
-     * @throws IOException
-     * {@link java.net.URLConnection#getAllowUserInteraction()}
-     */
-    public void test_getAllowUserInteraction() throws IOException {
+    public void test_getAllowUserInteraction() throws Exception {
         uc.setAllowUserInteraction(false);
-        assertFalse("getAllowUserInteraction should have returned false", uc
-                .getAllowUserInteraction());
+        assertFalse(uc.getAllowUserInteraction());
 
         uc.setAllowUserInteraction(true);
-        assertTrue("getAllowUserInteraction should have returned true", uc
-                .getAllowUserInteraction());
+        assertTrue(uc.getAllowUserInteraction());
 
         uc.connect();
 
+        // Can't call the setter after connecting.
         try {
             uc.setAllowUserInteraction(false);
-            fail("Exception expected");
-        } catch (IllegalStateException e) {
-            //ok
+            fail();
+        } catch (IllegalStateException expected) {
         }
-
-        // test if setAllowUserInteraction works
-        URL serverURL = new URL("http://onearth.jpl.nasa.gov/landsat.cgi");
-
-        // connect to server
-        URLConnection uc2 = serverURL.openConnection();
-        HttpURLConnection conn = (HttpURLConnection) uc2;
-        uc2.setAllowUserInteraction(true);
-
-        uc2.setDoInput(true);
-        uc2.setDoOutput(true);
-
-        // get reference to stream to post to
-        OutputStream os = uc2.getOutputStream();
-
-        InputStream in = uc2.getInputStream();
-
-
-        int contentLength = uc2.getContentLength();
-        String contentType = uc2.getContentType();
-        int numBytesRead = 0;
-        int allBytesRead = 0;
-
-        byte[] buffer = new byte[4096];
-
-        do {
-
-        numBytesRead = in.read(buffer);
-        allBytesRead += allBytesRead + numBytesRead;
-
-        } while (numBytesRead > 0);
-
-        assertTrue(allBytesRead > 0);
-
-        uc2.connect();
-
-        numBytesRead = in.read(buffer);
-
-        assertEquals(-1, numBytesRead);
     }
 
     /**
@@ -409,8 +359,9 @@
             buf = r.readLine();
             assertTrue("Incorrect content returned from fileURL: "+buf,
                     testString.equals(buf.trim()));
+            i.close();
         } else {
-            fail("Some unkown type is returned "+obj.toString());
+            fail("Some unknown type is returned "+obj.toString());
         }
 
         //Exception test
@@ -478,6 +429,8 @@
         } catch (NullPointerException e) {
             // expected
         }
+
+        fileURLCon.getInputStream().close();
     }
 
     /**
@@ -516,43 +469,28 @@
     /**
      * {@link java.net.URLConnection#getContentLength()}
      */
-    public void test_getContentLength() {
-        assertEquals(testString.getBytes().length,
-                fileURLCon.getContentLength());
+    public void test_getContentLength() throws Exception {
+        assertEquals(testString.getBytes().length, fileURLCon.getContentLength());
         assertEquals(Support_TestWebData.test1.length, uc.getContentLength());
         assertEquals(Support_TestWebData.test2.length, uc2.getContentLength());
 
         assertNotNull(jarURLCon.getContentLength());
         assertNotNull(gifURLCon.getContentLength());
+
+        fileURLCon.getInputStream().close();
     }
 
-    /**
-     * {@link java.net.URLConnection#getContentType()}
-     */
-    public void test_getContentType() throws IOException, MalformedURLException {
-
+    public void test_getContentType() throws Exception {
         assertTrue("getContentType failed: " + fileURLCon.getContentType(),
                 fileURLCon.getContentType().contains("text/plain"));
 
+        fileURLCon.getInputStream().close();
+
         URLConnection htmlFileCon = openHTMLFile();
         String contentType = htmlFileCon.getContentType();
         if (contentType != null) {
             assertTrue(contentType.equalsIgnoreCase("text/html"));
         }
-
-
-        /*
-        contentType = uc.getContentType();
-        if (contentType != null) {
-        assertTrue(contentType.equalsIgnoreCase("text/html"));
-        }
-
-        contentType = gifURLCon.getContentType();
-        if (contentType != null) {
-        assertTrue(contentType.equalsIgnoreCase("image/gif"));
-        }
-        */
-
     }
 
     /**
@@ -616,15 +554,13 @@
         assertFalse("Should have been set to false", uc2.getDoInput());
 
         fileURLCon.connect();
-        fileURLCon.getInputStream();
+        fileURLCon.getInputStream().close();
 
         uc2.connect();
         try {
             uc2.getInputStream();
-        } catch (Throwable e) {
-            // ok
+        } catch (Throwable expected) {
         }
-
     }
 
     /**
@@ -919,8 +855,7 @@
 
     public void test_getOutputStream() throws IOException {
         String posted = "this is a test";
-        URLConnection uc3 = new URL(Support_Configuration.hTTPURLgoogle)
-                .openConnection();
+        URLConnection uc3 = new URL("http://www.google.com/ie").openConnection();
         uc3.setDoOutput(true);
         uc3.connect();
 
@@ -933,18 +868,16 @@
 
         int code = ((HttpURLConnection) uc3).getResponseCode();
 
-
         // writing to url not allowed
         assertEquals("Got different responseCode ", 405, code);
 
-
         // try exception testing
         try {
             fileURLCon.setDoOutput(true);
             fileURLCon.connect();
+            fileURLCon.getInputStream().close();
             fileURLCon.getOutputStream();
-        } catch (UnknownServiceException e) {
-            // ok cannot write to fileURL
+        } catch (UnknownServiceException expected) {
         }
 
         ((HttpURLConnection) uc2).disconnect();
diff --git a/support/src/test/java/tests/support/Support_Configuration.java b/support/src/test/java/tests/support/Support_Configuration.java
index fed0bc8..9cb617d 100644
--- a/support/src/test/java/tests/support/Support_Configuration.java
+++ b/support/src/test/java/tests/support/Support_Configuration.java
@@ -91,18 +91,8 @@
     // than one addresses returned for this host name as needed by a test
     // END android-changed
 
-    public static String testURL = "harmony.apache.org";
-
-    public static String hTTPURLwExpiration = "http://phpwiki.sourceforge.net/phpwiki-1.2/";
-
     public static String hTTPURLwLastModified = "http://www.php.net/manual/en/function.explode.php";
 
-    public static String hTTPURLyahoo = "http://news.yahoo.com/";
-
-    public static String hTTPURLgoogle = "http://www.google.com/ie";
-
-    public static String testContentEncoding = "http://www.amazon.com/";
-
     public static int SpecialInetTestAddressNumber = 4;
 
     /**
diff --git a/support/src/test/java/tests/support/resource/Support_Resources.java b/support/src/test/java/tests/support/resource/Support_Resources.java
index 80a53fb..ddcb881 100644
--- a/support/src/test/java/tests/support/resource/Support_Resources.java
+++ b/support/src/test/java/tests/support/resource/Support_Resources.java
@@ -18,6 +18,7 @@
 package tests.support.resource;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
@@ -34,12 +35,25 @@
     public static final String RESOURCE_PACKAGE_NAME = "tests.resources";
 
     public static InputStream getStream(String name) {
+        // If we have the resources packaged up in our jar file, get them that way.
         String path = RESOURCE_PACKAGE + name;
         InputStream result = Support_Resources.class.getResourceAsStream(path);
-        if (result == null) {
-            throw new IllegalArgumentException("No such resource: " + path);
+        if (result != null) {
+            return result;
         }
-        return result;
+        // Otherwise, if we're in an Android build tree, get the files directly.
+        String ANDROID_BUILD_TOP = System.getenv("ANDROID_BUILD_TOP");
+        if (ANDROID_BUILD_TOP != null) {
+            File resource = new File(ANDROID_BUILD_TOP + "/libcore/support/src/test/java" + path);
+            if (resource.exists()) {
+                try {
+                    return new FileInputStream(resource);
+                } catch (IOException ex) {
+                    throw new IllegalArgumentException("Couldn't open: " + resource, ex);
+                }
+            }
+        }
+        throw new IllegalArgumentException("No such resource: " + path);
     }
 
     public static String getURL(String name) {