Home | History | Annotate | Download | only in StringReference
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *
     15  *  See the License for the specific language governing permissions and
     16  *  limitations under the License.
     17  */
     18 
     19 /**
     20  * @author Anton V. Karnachuk
     21  */
     22 
     23 /**
     24  * Created on 07.02.2005
     25  */
     26 
     27 package org.apache.harmony.jpda.tests.jdwp.StringReference;
     28 
     29 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
     30 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
     31 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
     32 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
     33 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
     34 import org.apache.harmony.jpda.tests.jdwp.share.JDWPTestConstants;
     35 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
     36 
     37 
     38 /**
     39  * JDWP Unit test for StringReference.Value command.
     40  */
     41 public class ValueTest extends JDWPSyncTestCase {
     42 
     43     @Override
     44     protected String getDebuggeeClassName() {
     45         return "org.apache.harmony.jpda.tests.jdwp.share.debuggee.HelloWorld";
     46     }
     47 
     48     protected void checkString(String testString) {
     49         logWriter.println("=> Test string: \"" + testString + "\"");
     50 
     51         // create new string
     52         long stringID = createString(testString);
     53 
     54         // get string from StringID
     55         String returnedTestString = getStringValue(stringID);
     56         logWriter.println("=> Returned string: \"" + returnedTestString + "\"");
     57 
     58         assertString("StringReference::Value command returned invalid string,",
     59                 testString, returnedTestString);
     60     }
     61 
     62     /**
     63      * This testcase exercises StringReference.Value command.
     64      * <BR>The test starts HelloWorld debuggee, create some strings
     65      * with VirtualMachine.CreateString command.
     66      * <BR> Then the test checks that for each created string
     67      * StringReference.Value command returns string which is
     68      * equal to string used for CreateString command.
     69      */
     70     public void testStringReferenceValueTest001() {
     71         logWriter.println("StringReferenceValueTest001 started");
     72         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
     73 
     74         checkString("");
     75         checkString("1234567890");
     76         checkString("Some test string with various ASCII symbols "
     77             + "~!@#$%^&*()_+|}{\"'?><,./");
     78         checkString("Some test string with various national symbols "
     79             + "\u00a9\u0436\u0433\u0404\u0490\u00ad\u0408\u0438\u0439\u00a7\u0435"
     80             + "\u043a\u043d\u00a6\u00a4\u00ab\u00ae\u0430\u0407\u00a0\u045e\u043b"
     81             + "\u0434\u043f\u0437\u0431\u00ac\u0401\u0432\u043c\u040e\u043e.");
     82 
     83         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
     84     }
     85 
     86     /**
     87      * This testcase exercises StringReference.Value command.
     88      * <BR>The test starts HelloWorld debuggee then checks INVALID_OBJECT error
     89      * is returned if we pass a null id.
     90      */
     91     public void testStringReferenceValueTest001_NullString() {
     92         logWriter.println("testStringReferenceValueTest001_NullString started");
     93         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
     94 
     95         checkCommandError(JDWPTestConstants.NULL_OBJECT_ID,
     96                           JDWPConstants.Error.INVALID_OBJECT);
     97 
     98         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
     99     }
    100 
    101     /**
    102      * This testcase exercises StringReference.Value command.
    103      * <BR>The test starts HelloWorld debuggee then checks INVALID_OBJECT error
    104      * is returned if we pass an invalid object id.
    105      */
    106     public void testStringReferenceValueTest001_InvalidObject() {
    107         logWriter.println("testStringReferenceValueTest001_InvalidObject started");
    108         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
    109 
    110         checkCommandError(JDWPTestConstants.INVALID_OBJECT_ID,
    111                           JDWPConstants.Error.INVALID_OBJECT);
    112 
    113         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
    114     }
    115 
    116     /**
    117      * This testcase exercises StringReference.Value command.
    118      * <BR>The test starts HelloWorld debuggee then checks INVALID_STRING error
    119      * is returned if we pass a valid object id but the corresponding object
    120      * is not a java.lang.String.
    121      */
    122     public void testStringReferenceValueTest001_InvalidString() {
    123         logWriter.println("testStringReferenceValueTest001_InvalidString started");
    124         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
    125 
    126         String signature = "Lorg/apache/harmony/jpda/tests/jdwp/share/debuggee/HelloWorld;";
    127         long debuggeeClassID = getClassIDBySignature(signature);
    128         checkCommandError(debuggeeClassID, JDWPConstants.Error.INVALID_STRING);
    129 
    130         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
    131     }
    132 
    133     private void checkCommandError(long stringID, int expectedError) {
    134         logWriter.println("Send StringReference.Value command with id " + stringID);
    135 
    136         CommandPacket packet = new CommandPacket(
    137                 JDWPCommands.StringReferenceCommandSet.CommandSetID,
    138                 JDWPCommands.StringReferenceCommandSet.ValueCommand);
    139         packet.setNextValueAsObjectID(stringID);
    140         ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
    141 
    142         checkReplyPacket(reply, "StringReference::Value command", expectedError);
    143     }
    144 }
    145