AI 147687: Several fixes for failing tests in the cts host. also tests that timeout
  are now marked with BrokenTest to exclude them for the time being until
  we have a way to execute them.
  BUG=1285921

Automated import of CL 147687
diff --git a/libcore/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/SocketHandlerTest.java b/libcore/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/SocketHandlerTest.java
index 3e17ff6..cc3b165 100644
--- a/libcore/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/SocketHandlerTest.java
+++ b/libcore/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/SocketHandlerTest.java
@@ -970,6 +970,26 @@
     }
 
     /*
+     * A mock stream handler, expose setOutputStream.
+     */
+    public static class MockHandler extends Handler {
+        public MockHandler() throws Exception {
+        }
+
+        @Override
+        public void close() {
+        }
+
+        @Override
+        public void flush() {
+        }
+
+        @Override
+        public void publish(LogRecord record) {
+        }
+    }
+
+    /*
      * A server thread that accepts an incoming connection request and reads any
      * incoming data into an byte array.
      */
diff --git a/libcore/luni/src/test/java/org/apache/harmony/luni/tests/internal/net/www/protocol/https/HttpsURLConnectionTest.java b/libcore/luni/src/test/java/org/apache/harmony/luni/tests/internal/net/www/protocol/https/HttpsURLConnectionTest.java
index 012cecc..c99a0c1 100644
--- a/libcore/luni/src/test/java/org/apache/harmony/luni/tests/internal/net/www/protocol/https/HttpsURLConnectionTest.java
+++ b/libcore/luni/src/test/java/org/apache/harmony/luni/tests/internal/net/www/protocol/https/HttpsURLConnectionTest.java
@@ -18,6 +18,7 @@
 package org.apache.harmony.luni.tests.internal.net.www.protocol.https;
 
 import dalvik.annotation.AndroidOnly;
+import dalvik.annotation.BrokenTest;
 import dalvik.annotation.KnownFailure;
 import dalvik.annotation.TestTargetClass; 
 import dalvik.annotation.TestTargets;
@@ -159,6 +160,8 @@
         method = "setDefaultHostnameVerifier",
         args = {javax.net.ssl.HostnameVerifier.class}
     )
+    @KnownFailure("Handshake fails.")
+    @BrokenTest("Different behavior between cts host and run-core-test")
     @AndroidOnly("we only have a .bks key store in the test resources")
     public void testHttpsConnection() throws Throwable {
         // set up the properties defining the default values needed by SSL stuff
@@ -217,6 +220,8 @@
             args = {int.class}
         )
     })
+    @KnownFailure("Handshake fails.")
+    @BrokenTest("Different behavior between cts host and run-core-test")
     @AndroidOnly("we only have a .bks key store in the test resources")
     public void testHttpsConnection_Not_Found_Response() throws Throwable {
         // set up the properties defining the default values needed by SSL stuff
@@ -442,6 +447,8 @@
         method = "setHostnameVerifier",
         args = {javax.net.ssl.HostnameVerifier.class}
     )
+    @KnownFailure("Handshake fails.")
+    @BrokenTest("Different behavior between cts host and run-core-test")
     @AndroidOnly("we only have a .bks key store in the test resources")
     public void testSetHostnameVerifier() throws Throwable {
         // setting up the properties pointing to the key/trust stores
@@ -491,6 +498,8 @@
         method = "setDoOutput",
         args = {boolean.class}
     )
+    @KnownFailure("Handshake fails.")
+    @BrokenTest("Different behavior between cts host and run-core-test")
     @AndroidOnly("we only have a .bks key store in the test resources")
     public void test_doOutput() throws Throwable {
         // setting up the properties pointing to the key/trust stores
diff --git a/libcore/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/ClassTest.java b/libcore/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/ClassTest.java
index e827111..14ca1ad 100644
--- a/libcore/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/ClassTest.java
+++ b/libcore/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/ClassTest.java
@@ -560,6 +560,7 @@
         args = {}
     )
     @AndroidOnly("Uses dalvik.system.PathClassLoader.")
+    @BrokenTest("Different behavior between cts host and run-core-test")
     public void test_getPackage() {
 
       Package thisPackage = getClass().getPackage();
diff --git a/libcore/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/PackageTest.java b/libcore/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/PackageTest.java
index 19cc341..cb35324 100644
--- a/libcore/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/PackageTest.java
+++ b/libcore/luni/src/test/java/org/apache/harmony/luni/tests/java/lang/PackageTest.java
@@ -16,6 +16,7 @@
  */
 package org.apache.harmony.luni.tests.java.lang;
 
+import dalvik.annotation.BrokenTest;
 import dalvik.annotation.KnownFailure;
 import dalvik.annotation.TestTargets;
 import dalvik.annotation.TestLevel;
@@ -236,6 +237,7 @@
         method = "getName",
         args = {}
     )
+    @BrokenTest("Different behavior between cts host and run-core-test")
     public void test_getName() throws Exception {
         Package p = getTestPackage("hyts_pq.jar", "p.q.C");
         assertEquals("Package getName returns a wrong string", "p.q", p
@@ -403,6 +405,7 @@
         method = "toString",
         args = {}
     )
+    @BrokenTest("Different behavior between cts host and run-core-test")
     public void test_toString() throws Exception {
         Package p = getTestPackage("hyts_c.jar", "p.C");
         assertTrue("Package toString returns wrong string", p.toString()
diff --git a/libcore/luni/src/test/java/tests/api/java/io/SerializationStressTest2.java b/libcore/luni/src/test/java/tests/api/java/io/SerializationStressTest2.java
index d721cd7..d3bbc29 100644
--- a/libcore/luni/src/test/java/tests/api/java/io/SerializationStressTest2.java
+++ b/libcore/luni/src/test/java/tests/api/java/io/SerializationStressTest2.java
@@ -1897,36 +1897,50 @@
         method = "!Serialization",
         args = {}
     )
-    public void test_DeepNestingWithWriteObject() {
+    public void test_DeepNestingWithWriteObject() throws Throwable {
         // Test for method void
         // java.io.ObjectOutputStream.writeObject(java.lang.Object)
 
-        Object objToSave = null;
-        Object objLoaded;
+        // this test was wrapped in a Thread so we can increase the max stack
+        // size to more than the default 8K. In this case we assign 256K
+        Thread t = new Thread(null, null, "deep nested", 256*1024) {
 
-        try {
-            DeepNestingWithWriteObject test = new DeepNestingWithWriteObject(50);
-            objToSave = test;
-            if (DEBUG)
-                System.out.println("Obj = " + objToSave);
-            objLoaded = dumpAndReload(objToSave);
+            @Override
+            public void run() {
+                try {
+                    deepNestingHelper();
+                } catch (Throwable e) {
+                    error = e;
+                }
+            }
+        };
+        t.start();
 
-            // Has to have worked
-            assertTrue(MSG_TEST_FAILED + objToSave, (test.equals(objLoaded)));
+        t.join();
 
-        } catch (IOException e) {
-            fail("IOException serializing " + objToSave + " : "
-                    + e.getMessage());
-        } catch (ClassNotFoundException e) {
-            fail("ClassNotFoundException reading Object type : "
-                    + e.getMessage());
-        } catch (Error err) {
-            // err.printStackTrace();
-            System.out.println("Error " + err + " when obj = " + objToSave);
-            throw err;
+        if (error != null) {
+            throw error;
         }
     }
 
+    static Throwable error;
+
+    public void deepNestingHelper() throws IOException,
+            ClassNotFoundException {
+        // Test for method void
+        // java.io.ObjectOutputStream.writeObject(java.lang.Object)
+
+        DeepNestingWithWriteObject test = new DeepNestingWithWriteObject(50);
+        if (DEBUG) {
+            System.out.println("Obj = " + test);
+        }
+
+        Object objLoaded = dumpAndReload(test);
+
+        // Has to have worked
+        assertTrue(MSG_TEST_FAILED + test, (test.equals(objLoaded)));
+    }
+
     @TestTargetNew(
         level = TestLevel.COMPLETE,
         notes = "Verifies serialization.",
diff --git a/libcore/luni/src/test/java/tests/api/java/util/CalendarTest.java b/libcore/luni/src/test/java/tests/api/java/util/CalendarTest.java
index c414f64..a3c003f 100644
--- a/libcore/luni/src/test/java/tests/api/java/util/CalendarTest.java
+++ b/libcore/luni/src/test/java/tests/api/java/util/CalendarTest.java
@@ -1283,6 +1283,7 @@
         args = {int.class}
     )
     public void test_EdgeCases() {
+        Locale.setDefault(Locale.US);
         Calendar c = Calendar.getInstance();
         
         c.setTimeInMillis(Long.MAX_VALUE);
diff --git a/libcore/sql/src/test/java/org/apache/harmony/sql/tests/java/sql/TimestampTest.java b/libcore/sql/src/test/java/org/apache/harmony/sql/tests/java/sql/TimestampTest.java
index c2e0f26..bc1a9d5 100644
--- a/libcore/sql/src/test/java/org/apache/harmony/sql/tests/java/sql/TimestampTest.java
+++ b/libcore/sql/src/test/java/org/apache/harmony/sql/tests/java/sql/TimestampTest.java
@@ -409,7 +409,7 @@
                 theReturn.getNanos());
 
         theReturn = Timestamp.valueOf("1969-12-31 13:14:39.309");
-        assertEquals("Wrong result for time test", 38720691,
+        assertEquals("Wrong result for time test", -38720691,
                 theReturn.getTime());
         assertEquals("Wrong result for nanos test", 309000000,
                 theReturn.getNanos());
diff --git a/libcore/text/src/test/java/org/apache/harmony/text/tests/java/text/DecimalFormatTest.java b/libcore/text/src/test/java/org/apache/harmony/text/tests/java/text/DecimalFormatTest.java
index a5252dc..a802ad8 100644
--- a/libcore/text/src/test/java/org/apache/harmony/text/tests/java/text/DecimalFormatTest.java
+++ b/libcore/text/src/test/java/org/apache/harmony/text/tests/java/text/DecimalFormatTest.java
@@ -985,6 +985,7 @@
         method = "equals",
         args = {java.lang.Object.class}
     )
+    @BrokenTest("Behaves differently between cts host and run-core-tests")
     public void test_equalsLjava_lang_Object() {
         DecimalFormat format = (DecimalFormat) DecimalFormat
                 .getInstance(Locale.US);
diff --git a/libcore/text/src/test/java/org/apache/harmony/text/tests/java/text/NumberFormatTest.java b/libcore/text/src/test/java/org/apache/harmony/text/tests/java/text/NumberFormatTest.java
index c1a582e..c047d3e 100644
--- a/libcore/text/src/test/java/org/apache/harmony/text/tests/java/text/NumberFormatTest.java
+++ b/libcore/text/src/test/java/org/apache/harmony/text/tests/java/text/NumberFormatTest.java
@@ -560,6 +560,7 @@
     )
     public void test_getCurrencyInstance() {
 
+        Locale.setDefault(Locale.US);
         NumberFormat format = NumberFormat.getCurrencyInstance();
 
         assertNotSame("Instance is null", null, format);
diff --git a/libcore/text/src/test/java/org/apache/harmony/text/tests/java/text/SimpleDateFormatTest.java b/libcore/text/src/test/java/org/apache/harmony/text/tests/java/text/SimpleDateFormatTest.java
index 286f080..f96410f 100644
--- a/libcore/text/src/test/java/org/apache/harmony/text/tests/java/text/SimpleDateFormatTest.java
+++ b/libcore/text/src/test/java/org/apache/harmony/text/tests/java/text/SimpleDateFormatTest.java
@@ -17,6 +17,7 @@
 package org.apache.harmony.text.tests.java.text;
 
 import dalvik.annotation.AndroidOnly;
+import dalvik.annotation.BrokenTest;
 import dalvik.annotation.KnownFailure;
 import dalvik.annotation.TestLevel;
 import dalvik.annotation.TestTargetClass;
@@ -757,10 +758,12 @@
         args = {java.lang.String.class, java.text.ParsePosition.class}
     )
     @AndroidOnly("ICU specific...")
+    @BrokenTest("Different behavior between cts host and run-core-test")
     public void test_parseLjava_lang_StringLjava_text_ParsePosition() {
         // Test for method java.util.Date
         // java.text.SimpleDateFormat.parse(java.lang.String,
         // java.text.ParsePosition)
+
         TestFormat test = new TestFormat(
                 "test_formatLjava_util_DateLjava_lang_StringBufferLjava_text_FieldPosition");
 
@@ -887,7 +890,19 @@
 
         test.parse("yyyyMMddHHmmss", "19990913171901", new GregorianCalendar(
                 1999, Calendar.SEPTEMBER, 13, 17, 19, 01).getTime(), 0, 14);
+    }
 
+    /**
+     * @tests java.text.SimpleDateFormat#parse(java.lang.String,
+     *        java.text.ParsePosition)
+     */
+    @TestTargetNew(
+        level = TestLevel.COMPLETE,
+        method = "parse",
+        args = {java.lang.String.class, java.text.ParsePosition.class}
+    )
+    @AndroidOnly("ICU specific...")
+    public void test_parseLjava_lang_StringLjava_text_ParsePosition_2() {
         Date d = new Date(1015822800000L);
         SimpleDateFormat df = new SimpleDateFormat("", new Locale("en", "US"));
         df.setTimeZone(TimeZone.getTimeZone("EST"));
diff --git a/libcore/xml/src/test/java/tests/api/javax/xml/parsers/SAXParserTest.java b/libcore/xml/src/test/java/tests/api/javax/xml/parsers/SAXParserTest.java
index 4ca0f1f..2b8bb5c 100644
--- a/libcore/xml/src/test/java/tests/api/javax/xml/parsers/SAXParserTest.java
+++ b/libcore/xml/src/test/java/tests/api/javax/xml/parsers/SAXParserTest.java
@@ -46,6 +46,7 @@
 import tests.api.org.xml.sax.support.BrokenInputStream;
 import tests.api.org.xml.sax.support.MethodLogger;
 import tests.api.org.xml.sax.support.MockHandler;
+import tests.support.resource.Support_Resources;
 import dalvik.annotation.KnownFailure;
 import dalvik.annotation.TestLevel;
 import dalvik.annotation.TestTargetClass;
@@ -144,7 +145,7 @@
         return this.getClass().getResourceAsStream(name);        
     }
     
-    public SAXParserTest() throws Exception{
+    public void initFiles() throws Exception {
         // we differntiate between a validating and a non validating parser
         try {
             SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
@@ -153,45 +154,40 @@
             fail("could not obtain a SAXParser");
         }
 
-        // nwf = non well formed, wf = well formed 
-        list_wf = new File[] {File.createTempFile(
-                SAXParserTestSupport.XML_WF + "staff","xml")};
-        list_nwf = new File[] {File.createTempFile(
-                SAXParserTestSupport.XML_NWF + "staff","xml")};
+        String tmpPath = System.getProperty("java.io.tmpdir");
 
-        copyFile(getResource(SAXParserTestSupport.XML_WF + "staff.xml"),
-                list_wf[0].getAbsolutePath());
-        copyFile(getResource(SAXParserTestSupport.XML_WF + "staff.dtd"),
-                File.createTempFile(SAXParserTestSupport.XML_WF + "staff",
-                        "dtd").getAbsolutePath());
-        copyFile(getResource(SAXParserTestSupport.XML_NWF + "staff.xml"),
-                list_nwf[0].getAbsolutePath());
-        copyFile(getResource(SAXParserTestSupport.XML_NWF + "staff.dtd"),
-                File.createTempFile(SAXParserTestSupport.XML_NWF + "staff",
-                        "dtd").getAbsolutePath());
+        // nwf = not well formed, wf = well formed 
+        list_wf = new File[] {new File(tmpPath + "/" + 
+                SAXParserTestSupport.XML_WF + "staff.xml")};
+        list_nwf = new File[] {new File(tmpPath + "/" +
+                SAXParserTestSupport.XML_NWF + "staff.xml")};
+        list_out_dh = new File[] {new File(tmpPath + "/" +
+                SAXParserTestSupport.XML_WF_OUT_DH + "staff.out")};
+        list_out_hb = new File[] {new File(tmpPath + "/" +
+                SAXParserTestSupport.XML_WF_OUT_HB + "staff.out")};
 
-        list_out_dh = new File[] {File.createTempFile(
-                SAXParserTestSupport.XML_WF_OUT_DH + "staff", "out")};
-        list_out_hb = new File[] {File.createTempFile(
-                SAXParserTestSupport.XML_WF_OUT_HB + "staff", "out")};
-        copyFile(getResource(SAXParserTestSupport.XML_WF_OUT_HB + "staff.out"),
-                list_out_hb[0].getAbsolutePath());
-        copyFile(getResource(SAXParserTestSupport.XML_WF_OUT_DH + "staff.out"),
-                list_out_dh[0].getAbsolutePath());
-    }
+        list_wf[0].deleteOnExit();
+        list_nwf[0].deleteOnExit();
+        list_out_hb[0].deleteOnExit();
+        list_out_dh[0].deleteOnExit();
 
-    private void copyFile(InputStream toCopy, String target) throws Exception {
-        new File(target).getParentFile().mkdirs();
-        OutputStream writer = new FileOutputStream(target);
-        byte[] buffer = new byte[512];
-        int i = toCopy.read(buffer);
-        while (i >= 0) {
-            writer.write(buffer,0,i);
-            i = toCopy.read(buffer);
-        }
-        writer.flush();
-        writer.close();
-        toCopy.close();
+
+        Support_Resources.copyLocalFileto(list_wf[0],
+                getResource(SAXParserTestSupport.XML_WF + "staff.xml"));
+        Support_Resources.copyLocalFileto(new File(
+                tmpPath + "/" + SAXParserTestSupport.XML_WF + "staff.dtd"),
+                getResource(SAXParserTestSupport.XML_WF + "staff.dtd"));
+
+        Support_Resources.copyLocalFileto(list_nwf[0],
+                getResource(SAXParserTestSupport.XML_NWF + "staff.xml"));
+        Support_Resources.copyLocalFileto(new File(
+                tmpPath + "/" + SAXParserTestSupport.XML_NWF + "staff.dtd"),
+                getResource(SAXParserTestSupport.XML_NWF + "staff.dtd"));
+
+        Support_Resources.copyLocalFileto(list_out_dh[0],
+                getResource(SAXParserTestSupport.XML_WF_OUT_DH + "staff.out"));
+        Support_Resources.copyLocalFileto(list_out_hb[0],
+                getResource(SAXParserTestSupport.XML_WF_OUT_HB + "staff.out"));
     }
     
     @Override
@@ -203,6 +199,7 @@
         ns = new HashMap<String, String>();
         attr = new HashMap<String, String>();
         el = new Vector<String>();
+        initFiles();
     }
 
     @Override
@@ -350,7 +347,9 @@
         method = "parse",
         args = {java.io.File.class, org.xml.sax.helpers.DefaultHandler.class}
     )
-    public void _test_parseLjava_io_FileLorg_xml_sax_helpers_DefaultHandler()
+    @KnownFailure("The default handler doesn't get the qName value supplied. " +
+            "We either need to change the test, or fix the parser.")
+    public void test_parseLjava_io_FileLorg_xml_sax_helpers_DefaultHandler()
     throws Exception {
 
         for(int i = 0; i < list_wf.length; i++) {
@@ -457,8 +456,9 @@
         method = "parse",
         args = {org.xml.sax.InputSource.class, org.xml.sax.helpers.DefaultHandler.class}
     )
-    
-    public void _test_parseLorg_xml_sax_InputSourceLorg_xml_sax_helpers_DefaultHandler()
+    @KnownFailure("The default handler doesn't get the qName value supplied. " +
+            "We either need to change the test, or fix the parser.")
+    public void test_parseLorg_xml_sax_InputSourceLorg_xml_sax_helpers_DefaultHandler()
     throws Exception {
 
         for(int i = 0; i < list_wf.length; i++) {
@@ -623,7 +623,9 @@
         method = "parse",
         args = {java.io.InputStream.class, org.xml.sax.helpers.DefaultHandler.class}
     )
-    public void _test_parseLjava_io_InputStreamLorg_xml_sax_helpers_DefaultHandler()
+    @KnownFailure("The default handler doesn't get the qName value supplied. " +
+            "We either need to change the test, or fix the parser.")
+    public void test_parseLjava_io_InputStreamLorg_xml_sax_helpers_DefaultHandler()
     throws Exception {
 
         for(int i = 0; i < list_wf.length; i++) {
@@ -673,7 +675,9 @@
         method = "parse",
         args = {java.io.InputStream.class, org.xml.sax.helpers.DefaultHandler.class, java.lang.String.class}
     )
-    public void _test_parseLjava_io_InputStreamLorg_xml_sax_helpers_DefaultHandlerLjava_lang_String() {
+    @KnownFailure("The default handler doesn't get the qName value supplied. " +
+            "We either need to change the test, or fix the parser.")
+    public void test_parseLjava_io_InputStreamLorg_xml_sax_helpers_DefaultHandlerLjava_lang_String() {
         for(int i = 0; i < list_wf.length; i++) {
             try {
                 HashMap<String, String> hm = sp.readFile(
@@ -948,7 +952,9 @@
         method = "parse",
         args = {java.lang.String.class, org.xml.sax.helpers.DefaultHandler.class}
     )
-    public void _test_parseLjava_lang_StringLorg_xml_sax_helpers_DefaultHandler()
+    @KnownFailure("The default handler doesn't get the qName value supplied. " +
+            "We either need to change the test, or fix the parser.")
+    public void test_parseLjava_lang_StringLorg_xml_sax_helpers_DefaultHandler()
     throws Exception {
 
         for(int i = 0; i < list_wf.length; i++) {
@@ -956,14 +962,14 @@
             HashMap<String, String> hm = new SAXParserTestSupport().readFile(
                     list_out_dh[i].getPath());
             MyDefaultHandler dh = new MyDefaultHandler();
-            parser.parse(list_wf[i].getPath(), dh);
+            parser.parse(list_wf[i].toURI().toString(), dh);
             assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData()));
         }
 
         for(int i = 0; i < list_nwf.length; i++) {
             try {
                 MyDefaultHandler dh = new MyDefaultHandler();
-                parser.parse(list_nwf[i].getPath(), dh);
+                parser.parse(list_nwf[i].toURI().toString(), dh);
                 fail("SAXException is not thrown");
             } catch(org.xml.sax.SAXException se) {
                 //expected
@@ -979,7 +985,7 @@
         }
 
         try {
-            parser.parse(list_wf[0].getPath(), (DefaultHandler) null);
+            parser.parse(list_wf[0].toURI().toString(), (DefaultHandler) null);
         } catch(java.lang.IllegalArgumentException iae) {
             fail("java.lang.IllegalArgumentException is thrown");
         }
diff --git a/libcore/xml/src/test/java/tests/api/org/xml/sax/helpers/ParserFactoryTest.java b/libcore/xml/src/test/java/tests/api/org/xml/sax/helpers/ParserFactoryTest.java
index 1ef0ee4..dd4b1c3 100644
--- a/libcore/xml/src/test/java/tests/api/org/xml/sax/helpers/ParserFactoryTest.java
+++ b/libcore/xml/src/test/java/tests/api/org/xml/sax/helpers/ParserFactoryTest.java
@@ -24,6 +24,10 @@
 
 import org.xml.sax.helpers.ParserFactory;
 
+import java.util.Iterator;
+import java.util.Properties;
+import java.util.Map.Entry;
+
 @SuppressWarnings("deprecation")
 @TestTargetClass(ParserFactory.class)
 public class ParserFactoryTest extends TestCase {
@@ -36,6 +40,9 @@
     )
     public void testMakeParser() throws ClassNotFoundException,
             IllegalAccessException, InstantiationException {
+
+        System.clearProperty("org.xml.sax.parser");
+
         // Property not set at all
         try {
             ParserFactory.makeParser();