001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Anton V. Karnachuk
021: * @version $Revision: 1.3 $
022: */
023:
024: /**
025: * Created on 09.03.2005
026: */package org.apache.harmony.jpda.tests.jdwp.ArrayReference;
027:
028: import java.io.UnsupportedEncodingException;
029:
030: import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
031: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
032: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
033: import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
034: import org.apache.harmony.jpda.tests.framework.jdwp.Value;
035: import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
036:
037: /**
038: * JDWP unit test for ArrayReference.Length command.
039: *
040: */
041:
042: public class LengthTest extends JDWPArrayReferenceTestCase {
043:
044: /**
045: * Starts this test by junit.textui.TestRunner.run() method.
046: */
047: public static void main(String[] args) {
048: junit.textui.TestRunner.run(LengthTest.class);
049: }
050:
051: /**
052: * This testcase exercises ArrayReference.Length command.
053: * <BR>Starts <A HREF="ArrayReferenceDebuggee.html">ArrayReferenceDebuggee</A>.
054: * <BR>Receives fields with ReferenceType.fields command,
055: * checks length with ArrayReference.Length command.
056: */
057: public void testLength001() throws UnsupportedEncodingException {
058: logWriter.println("testLength001 started");
059: synchronizer
060: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
061:
062: // obtain classID
063: long classID = getClassIDBySignature("Lorg/apache/harmony/jpda/tests/jdwp/ArrayReference/ArrayReferenceDebuggee;");
064:
065: // obtain fields
066: CommandPacket packet = new CommandPacket(
067: JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
068: JDWPCommands.ReferenceTypeCommandSet.FieldsCommand);
069: packet.setNextValueAsReferenceTypeID(classID);
070: ReplyPacket reply = debuggeeWrapper.vmMirror
071: .performCommand(packet);
072: checkReplyPacket(reply, "ReferenceType::Fields command");
073:
074: int declared = reply.getNextValueAsInt();
075: for (int i = 0; i < declared; i++) {
076: long fieldID = reply.getNextValueAsFieldID();
077: reply.getNextValueAsString();
078: reply.getNextValueAsString();
079: reply.getNextValueAsInt();
080:
081: switch (i) {
082: case 0:
083: // int[] intArray = new int[10]
084: checkArrayLength(classID, fieldID,
085: JDWPConstants.Error.NONE, 10);
086: break;
087: case 1:
088: // String[] strArray = new String[8]
089: checkArrayLength(classID, fieldID,
090: JDWPConstants.Error.NONE, 8);
091: break;
092: case 2:
093: // Integer intField = new Integer(-1)
094: checkArrayLength(classID, fieldID,
095: JDWPConstants.Error.INVALID_ARRAY, 0);
096: break;
097: }
098: }
099:
100: logWriter.println("test PASSED!");
101: synchronizer
102: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
103: }
104:
105: private void checkArrayLength(long classID, long fieldID,
106: int error, int length) {
107: //System.err.println("classID="+classID);
108: //System.err.println("fieldID="+fieldID);
109: CommandPacket packet = new CommandPacket(
110: JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
111: JDWPCommands.ReferenceTypeCommandSet.GetValuesCommand);
112: packet.setNextValueAsReferenceTypeID(classID);
113: packet.setNextValueAsInt(1);
114: packet.setNextValueAsFieldID(fieldID);
115: ReplyPacket reply = debuggeeWrapper.vmMirror
116: .performCommand(packet);
117: checkReplyPacket(reply, "ReferenceType::GetValues command");
118:
119: int values = reply.getNextValueAsInt();
120: assertEquals(
121: "ReferenceType::GetValues returned invalid number of values,",
122: 1, values);
123:
124: Value value = reply.getNextValueAsValue();
125: //System.err.println("value="+value);
126: long arrayID = value.getLongValue();
127: logWriter.println("arrayID = " + arrayID);
128:
129: packet = new CommandPacket(
130: JDWPCommands.ArrayReferenceCommandSet.CommandSetID,
131: JDWPCommands.ArrayReferenceCommandSet.LengthCommand);
132: packet.setNextValueAsArrayID(arrayID);
133: reply = debuggeeWrapper.vmMirror.performCommand(packet);
134: checkReplyPacket(reply, "ArrayReference::Length command", error);
135:
136: if (reply.getErrorCode() == JDWPConstants.Error.NONE) {
137: // do not check length for non-array fields
138: int returnedLength = reply.getNextValueAsInt();
139: assertEquals(
140: "ArrayReference::Length returned invalid length,",
141: length, returnedLength);
142: }
143: }
144: }
|