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 09.03.2005
25  */
26 package org.apache.harmony.jpda.tests.jdwp.ArrayReference;
27 
28 import java.io.UnsupportedEncodingException;
29 
30 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
31 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
32 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
33 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
34 import org.apache.harmony.jpda.tests.framework.jdwp.Value;
35 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
36 
37 
38 
39 /**
40  * JDWP unit test for ArrayReference.Length command.
41  *
42  */
43 
44 public class LengthTest extends JDWPArrayReferenceTestCase {
45     /**
46      * This testcase exercises ArrayReference.Length command.
47      * <BR>Starts <A HREF="ArrayReferenceDebuggee.html">ArrayReferenceDebuggee</A>.
48      * <BR>Receives fields with ReferenceType.fields command,
49      * checks length with ArrayReference.Length command.
50      */
testLength001()51     public void testLength001() throws UnsupportedEncodingException {
52         logWriter.println("testLength001 started");
53         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
54 
55         // obtain classID
56         long classID = getClassIDBySignature("Lorg/apache/harmony/jpda/tests/jdwp/ArrayReference/ArrayReferenceDebuggee;");
57 
58         // obtain fields
59         CommandPacket packet = new CommandPacket(
60                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
61                 JDWPCommands.ReferenceTypeCommandSet.FieldsCommand);
62         packet.setNextValueAsReferenceTypeID(classID);
63         ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
64         checkReplyPacket(reply, "ReferenceType::Fields command");
65 
66         int declared = reply.getNextValueAsInt();
67         for (int i = 0; i < declared; i++) {
68             long fieldID = reply.getNextValueAsFieldID();
69             String fieldName = reply.getNextValueAsString();
70             reply.getNextValueAsString();
71             reply.getNextValueAsInt();
72 
73             if (fieldName.equals("intArray")) {
74                 // int[] intArray = new int[10]
75                 checkArrayLength(classID, fieldID, JDWPConstants.Error.NONE, 10);
76             } else if (fieldName.equals("strArray")) {
77                 // String[] strArray = new String[8]
78                 checkArrayLength(classID, fieldID, JDWPConstants.Error.NONE, 8);
79             } else if (fieldName.equals("intField")) {
80                 // Integer intField = new Integer(-1)
81                 checkArrayLength(classID, fieldID, JDWPConstants.Error.INVALID_ARRAY, 0);
82             }
83         }
84 
85         logWriter.println("test PASSED!");
86         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
87     }
88 
checkArrayLength(long classID, long fieldID, int error, int length)89     private void checkArrayLength(long classID, long fieldID, int error, int length) {
90         //System.err.println("classID="+classID);
91         //System.err.println("fieldID="+fieldID);
92         CommandPacket packet = new CommandPacket(
93                 JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
94                 JDWPCommands.ReferenceTypeCommandSet.GetValuesCommand);
95         packet.setNextValueAsReferenceTypeID(classID);
96         packet.setNextValueAsInt(1);
97         packet.setNextValueAsFieldID(fieldID);
98         ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
99         checkReplyPacket(reply, "ReferenceType::GetValues command");
100 
101         int values = reply.getNextValueAsInt();
102         assertEquals("ReferenceType::GetValues returned invalid number of values,", 1, values);
103 
104         Value value = reply.getNextValueAsValue();
105         //System.err.println("value="+value);
106         long arrayID = value.getLongValue();
107         logWriter.println("arrayID = " + arrayID);
108 
109         packet = new CommandPacket(
110                 JDWPCommands.ArrayReferenceCommandSet.CommandSetID,
111                 JDWPCommands.ArrayReferenceCommandSet.LengthCommand);
112         packet.setNextValueAsArrayID(arrayID);
113         reply = debuggeeWrapper.vmMirror.performCommand(packet);
114         checkReplyPacket(reply, "ArrayReference::Length command", error);
115 
116         if (reply.getErrorCode() == JDWPConstants.Error.NONE) {
117             // do not check length for non-array fields
118             int returnedLength = reply.getNextValueAsInt();
119             assertEquals("ArrayReference::Length returned invalid length,",
120                     length, returnedLength);
121         }
122     }
123 }
124