Fix NewInstanceString test to not time out getting string value.

Checks the string value while the debuggee is still suspended on a
breakpoint.

Bug: 30951794
Cherry-pick of two changes:
ea4486cd693527acfcc5aa85a2faebf969142242
93c6e5c49ebaa5e4c6630b21dac49df52143f54c

Change-Id: I9218cec067e6a7ad39fd83680f82aeae0374e3e0
diff --git a/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/ClassType/AbstractNewInstanceTestCase.java b/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/ClassType/AbstractNewInstanceTestCase.java
index 4120863..881100e 100644
--- a/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/ClassType/AbstractNewInstanceTestCase.java
+++ b/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/ClassType/AbstractNewInstanceTestCase.java
@@ -77,11 +77,10 @@
      * @param typeSignature the type signature of the created object
      * @param constructorSignature the constructor signature
      * @param provider the arguments provider
-     * @param expectedTag the expected JDWP tag
-     * @return the tagged object returned by the constructor
+     * @param checker the checker that verifies the construction of the object.
      */
-    protected TaggedObject checkNewInstanceTag(String typeSignature, String constructorSignature,
-            ConstructorArgumentsProvider provider, byte expectedTag) {
+    protected void checkNewInstanceTag(String typeSignature, String constructorSignature,
+            ConstructorArgumentsProvider provider, Checker checker) {
         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
 
         long debuggeeClassId = getClassIDBySignature(getDebuggeeClassSignature());
@@ -135,17 +134,29 @@
         TaggedObject objectResult = reply.getNextValueAsTaggedObject();
         TaggedObject exceptionResult = reply.getNextValueAsTaggedObject();
         assertAllDataRead(reply);
-
-        assertNotNull("objectResult is null", objectResult);
-        assertNotNull("exceptionResult is null", exceptionResult);
-        assertTrue(objectResult.objectID != JDWPTestConstants.NULL_OBJECT_ID);
-        assertTrue(exceptionResult.tag == JDWPConstants.Tag.OBJECT_TAG);
-        assertEquals(exceptionResult.objectID, JDWPTestConstants.NULL_OBJECT_ID);
-        assertTagEquals("Invalid tag", expectedTag, objectResult.tag);
+        checker.check(objectResult, exceptionResult);
 
         // Debuggee is suspended on the breakpoint: resume it now.
         resumeDebuggee();
+    }
 
-        return objectResult;
+    /**
+     * Checks that the constructed object has the right tag.
+     */
+    protected class Checker {
+        private final byte expectedTag;
+
+        public Checker(byte expectedTag) {
+            this.expectedTag = expectedTag;
+        }
+
+        public void check(TaggedObject objectResult, TaggedObject exceptionResult) {
+            assertNotNull("objectResult is null", objectResult);
+            assertNotNull("exceptionResult is null", exceptionResult);
+            assertTrue(objectResult.objectID != JDWPTestConstants.NULL_OBJECT_ID);
+            assertTrue(exceptionResult.tag == JDWPConstants.Tag.OBJECT_TAG);
+            assertEquals(exceptionResult.objectID, JDWPTestConstants.NULL_OBJECT_ID);
+            assertTagEquals("Invalid tag", expectedTag, objectResult.tag);
+        }
     }
 }
diff --git a/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/ClassType/NewInstanceStringTest.java b/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/ClassType/NewInstanceStringTest.java
index ec618f0..ef2e3c3 100644
--- a/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/ClassType/NewInstanceStringTest.java
+++ b/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/ClassType/NewInstanceStringTest.java
@@ -271,13 +271,31 @@
      */
     private void runTestNewInstanceString(String constructorSignature, String expectedString,
             ConstructorArgumentsProvider provider) {
-        TaggedObject result = checkNewInstanceTag("Ljava/lang/String;", constructorSignature,
-                provider, JDWPConstants.Tag.STRING_TAG);
+        checkNewInstanceTag("Ljava/lang/String;", constructorSignature, provider,
+                new StringChecker(expectedString));
+    }
 
-        // Get the character data out of the new String and check that it's expected.
-        String resultString = getStringValue(result.objectID);
-        assertString("ClassType::NewInstance command returned invalid string,",
-                expectedString, resultString);
+    /**
+     * A specialization for String that also checks the string's value.
+     */
+    private final class StringChecker extends Checker {
+        private final String expectedString;
+
+        private StringChecker(String expectedString) {
+            super(JDWPConstants.Tag.STRING_TAG);
+            this.expectedString = expectedString;
+        }
+
+        @Override
+        public void check(TaggedObject objectResult, TaggedObject exceptionResult) {
+            // Check tag first.
+            super.check(objectResult, exceptionResult);
+
+            // Get the character data out of the new String and check that it's expected.
+            String resultString = getStringValue(objectResult.objectID);
+            assertString("ClassType::NewInstance command returned invalid string,",
+                    expectedString, resultString);
+        }
     }
 
     private Value getStaticFieldValue(long classId, String fieldName) {
diff --git a/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/ClassType/NewInstanceTagTest.java b/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/ClassType/NewInstanceTagTest.java
index 9167fb2..57b2bf3 100644
--- a/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/ClassType/NewInstanceTagTest.java
+++ b/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/ClassType/NewInstanceTagTest.java
@@ -45,7 +45,7 @@
      */
     public void testNewInstance_Object() {
         checkNewInstanceTag("Ljava/lang/Object;", "()V", new NoConstructorArgumentProvider(),
-                JDWPConstants.Tag.OBJECT_TAG);
+                new Checker(JDWPConstants.Tag.OBJECT_TAG));
     }
 
     /**
@@ -54,7 +54,7 @@
     public void testNewInstance_MyObject() {
         String subclassSig = getClassSignature(NewInstanceTagDebuggee.MyObject.class);
         checkNewInstanceTag(subclassSig, "()V", new NoConstructorArgumentProvider(),
-                JDWPConstants.Tag.OBJECT_TAG);
+                new Checker(JDWPConstants.Tag.OBJECT_TAG));
     }
 
     /**
@@ -62,7 +62,7 @@
      */
     public void testNewInstance_String() {
         checkNewInstanceTag("Ljava/lang/String;", "()V", new NoConstructorArgumentProvider(),
-                JDWPConstants.Tag.STRING_TAG);
+                new Checker(JDWPConstants.Tag.STRING_TAG));
     }
 
     /**
@@ -75,7 +75,7 @@
     public void testNewInstance_ClassLoader() {
         String subclassSig = getClassSignature(NewInstanceTagDebuggee.MyClassLoader.class);
         checkNewInstanceTag(subclassSig, "()V", new NoConstructorArgumentProvider(),
-                JDWPConstants.Tag.CLASS_LOADER_TAG);
+                new Checker(JDWPConstants.Tag.CLASS_LOADER_TAG));
     }
 
     /**
@@ -83,7 +83,7 @@
      */
     public void testNewInstance_Thread() {
         checkNewInstanceTag("Ljava/lang/Thread;", "()V", new NoConstructorArgumentProvider(),
-                JDWPConstants.Tag.THREAD_TAG);
+                new Checker(JDWPConstants.Tag.THREAD_TAG));
     }
 
     /**
@@ -93,7 +93,7 @@
     public void testNewInstance_MyThread() {
         String subclassSig = getClassSignature(NewInstanceTagDebuggee.MyThread.class);
         checkNewInstanceTag(subclassSig, "()V", new NoConstructorArgumentProvider(),
-                JDWPConstants.Tag.THREAD_TAG);
+                new Checker(JDWPConstants.Tag.THREAD_TAG));
     }
 
     /**
@@ -112,7 +112,7 @@
                         // Pass created string to constructor.
                         constructorArguments.add(new Value(JDWPConstants.Tag.STRING_TAG, stringId));
                     }
-                }, JDWPConstants.Tag.THREAD_GROUP_TAG);
+                }, new Checker(JDWPConstants.Tag.THREAD_GROUP_TAG));
     }
 
     /**
@@ -122,6 +122,6 @@
     public void testNewInstance_MyThreadGroup() {
         String subclassSig = getClassSignature(NewInstanceTagDebuggee.MyThreadGroup.class);
         checkNewInstanceTag(subclassSig, "()V", new NoConstructorArgumentProvider(),
-                JDWPConstants.Tag.THREAD_GROUP_TAG);
+                new Checker(JDWPConstants.Tag.THREAD_GROUP_TAG));
     }
 }