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.5 $
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.ArrayRegion;
031: import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
032: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
033: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
034: import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
035: import org.apache.harmony.jpda.tests.framework.jdwp.Value;
036: import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
037:
038: /**
039: * JDWP unit test for ArrayReference.GetValues command.
040: */
041:
042: public class GetValuesTest 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(GetValuesTest.class);
049: }
050:
051: /**
052: * This testcase exercises ArrayReference.GetValues command.
053: * <BR>Starts <A HREF="ArrayReferenceDebuggee.html">ArrayReferenceDebuggee</A>.
054: * <BR>Receives fields with ReferenceType.fields command,
055: * receives values with ArrayReference.GetValues then checks them.
056: */
057: public void testGetValues001() throws UnsupportedEncodingException {
058: logWriter
059: .println("==> GetValuesTest.testGetValues001 started...");
060: synchronizer
061: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
062:
063: String debuggeeSig = "Lorg/apache/harmony/jpda/tests/jdwp/ArrayReference/ArrayReferenceDebuggee;";
064:
065: // obtain classID
066: long classID = getClassIDBySignature(debuggeeSig);
067:
068: // obtain fields
069: CommandPacket packet = new CommandPacket(
070: JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
071: JDWPCommands.ReferenceTypeCommandSet.FieldsCommand);
072: packet.setNextValueAsReferenceTypeID(classID);
073: ReplyPacket reply = debuggeeWrapper.vmMirror
074: .performCommand(packet);
075: checkReplyPacket(reply, "ReferenceType::Fields command");
076:
077: int declared = reply.getNextValueAsInt();
078: for (int i = 0; i < declared; i++) {
079: long fieldID = reply.getNextValueAsFieldID();
080: String name = reply.getNextValueAsString();
081: reply.getNextValueAsString();
082: reply.getNextValueAsInt();
083:
084: if (name.equals("threadArray")) {
085: logWriter
086: .println("\n==> testGetValues001: check for array field: 'threadArray'...");
087: checkArrayValues(classID, fieldID,
088: JDWPConstants.Error.NONE, 1, 1,
089: JDWPConstants.Tag.OBJECT_TAG,
090: JDWPConstants.Tag.THREAD_TAG, false);
091: }
092: if (name.equals("threadGroupArray")) {
093: logWriter
094: .println("\n==> testGetValues001: check for array field: 'threadGroupArray...");
095: checkArrayValues(classID, fieldID,
096: JDWPConstants.Error.NONE, 1, 1,
097: JDWPConstants.Tag.OBJECT_TAG,
098: JDWPConstants.Tag.THREAD_GROUP_TAG, false);
099: }
100: if (name.equals("classArray")) {
101: logWriter
102: .println("\n==> testGetValues001: check for array field: 'classArray'...");
103: checkArrayValues(classID, fieldID,
104: JDWPConstants.Error.NONE, 1, 1,
105: JDWPConstants.Tag.OBJECT_TAG,
106: JDWPConstants.Tag.CLASS_OBJECT_TAG, false);
107: }
108: if (name.equals("ClassLoaderArray")) {
109: logWriter
110: .println("\n==> testGetValues001: check for array field: 'ClassLoaderArray'...");
111: checkArrayValues(classID, fieldID,
112: JDWPConstants.Error.NONE, 1, 1,
113: JDWPConstants.Tag.OBJECT_TAG,
114: JDWPConstants.Tag.CLASS_LOADER_TAG, false);
115: }
116: if (name.equals("myThreadArray")) {
117: logWriter
118: .println("\n==> testGetValues001: check for array field: 'myThreadArray'...");
119: checkArrayValues(classID, fieldID,
120: JDWPConstants.Error.NONE, 1, 1,
121: JDWPConstants.Tag.OBJECT_TAG,
122: JDWPConstants.Tag.THREAD_TAG, false);
123: }
124: if (name.equals("objectArrayArray")) {
125: logWriter
126: .println("\n==> testGetValues001: check for array field: 'objectArrayArray'...");
127: checkArrayValues(classID, fieldID,
128: JDWPConstants.Error.NONE, 1, 1,
129: JDWPConstants.Tag.ARRAY_TAG,
130: JDWPConstants.Tag.ARRAY_TAG, false);
131: }
132: if (name.equals("intArray")) {
133: // int[] intArray = new int[10]
134: logWriter
135: .println("\n==> testGetValues001: check for array field: 'int[] intArray'...");
136: checkArrayValues(classID, fieldID,
137: JDWPConstants.Error.NONE, 10, 4,
138: JDWPConstants.Tag.INT_TAG,
139: JDWPConstants.Tag.INT_TAG, true);
140: }
141: if (name.equals("strArray")) {
142: // String[] strArray = new String[8]
143: logWriter
144: .println("\n==> testGetValues001: check for array field: 'String[] strArray'...");
145: checkArrayValues(classID, fieldID,
146: JDWPConstants.Error.NONE, 8, 4,
147: JDWPConstants.Tag.OBJECT_TAG,
148: JDWPConstants.Tag.STRING_TAG, true);
149: }
150: if (name.equals("intField")) {
151: // Integer intField = new Integer(-1)
152: logWriter
153: .println("\n==> testGetValues001: check for non-array field: 'Integer intField = new Integer(-1)'...");
154: checkArrayValues(classID, fieldID,
155: JDWPConstants.Error.INVALID_ARRAY, 0, 4,
156: (byte) 0, (byte) 0, false);
157: }
158: }
159:
160: synchronizer
161: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
162: }
163:
164: private void checkArrayValues(long classID, long fieldID,
165: int error, int length, int checksNumber,
166: byte expectedArrayTag, byte expectedElementTag,
167: boolean checkValues) throws UnsupportedEncodingException {
168: CommandPacket packet = new CommandPacket(
169: JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
170: JDWPCommands.ReferenceTypeCommandSet.GetValuesCommand);
171: // set referenceTypeID
172: packet.setNextValueAsReferenceTypeID(classID);
173: // repeat 1 time
174: packet.setNextValueAsInt(1);
175: // set fieldID
176: packet.setNextValueAsFieldID(fieldID);
177: ReplyPacket reply = debuggeeWrapper.vmMirror
178: .performCommand(packet);
179: checkReplyPacket(reply, "ReferenceType::GetValues command");
180:
181: assertEquals(
182: "ReferenceType::GetValues command returned invalid int value,",
183: reply.getNextValueAsInt(), 1);
184: Value value = reply.getNextValueAsValue();
185: long arrayID = value.getLongValue();
186: logWriter.println("==> testGetValues001: checked arrayID = "
187: + arrayID);
188:
189: logWriter
190: .println("==> testGetValues001: checkArrayRegion: arrayID = "
191: + arrayID
192: + "; Expected error = "
193: + error
194: + "("
195: + JDWPConstants.Error.getName(error)
196: + ")"
197: + "; firstIndex = 0; length = " + length);
198: checkArrayRegion(arrayID, error, 0, length, expectedArrayTag,
199: expectedElementTag, checkValues);
200: logWriter.println("==> PASSED!");
201:
202: if (checksNumber > 1) {
203: logWriter
204: .println("==> testGetValues001: checkArrayRegion: arrayID = "
205: + arrayID
206: + "; Expected error = "
207: + error
208: + "("
209: + JDWPConstants.Error.getName(error)
210: + ")"
211: + "; firstIndex = 1; length = "
212: + (length - 1));
213: checkArrayRegion(arrayID, error, 1, length - 1,
214: expectedArrayTag, expectedElementTag, checkValues);
215: logWriter.println("==> PASSED!");
216:
217: logWriter
218: .println("==> testGetValues001: checkArrayRegion: arrayID = "
219: + arrayID
220: + "; Expected error = "
221: + error
222: + "("
223: + JDWPConstants.Error.getName(error)
224: + ")"
225: + "; firstIndex = 0; length = "
226: + (length - 1));
227: checkArrayRegion(arrayID, error, 0, length - 1,
228: expectedArrayTag, expectedElementTag, checkValues);
229: logWriter.println("==> PASSED!");
230:
231: logWriter
232: .println("==> testGetValues001: checkArrayRegion: arrayID = "
233: + arrayID
234: + "; Expected error = "
235: + error
236: + "("
237: + JDWPConstants.Error.getName(error)
238: + ")"
239: + "; firstIndex = "
240: + (length - 1)
241: + " length = 1");
242: checkArrayRegion(arrayID, error, length - 1, 1,
243: expectedArrayTag, expectedElementTag, checkValues);
244: logWriter.println("==> PASSED!");
245: }
246: }
247:
248: private void checkArrayRegion(long arrayID, int error,
249: int firstIndex, int length, byte expectedArrayTag,
250: byte expectedElementTag, boolean checkValues)
251: throws UnsupportedEncodingException {
252:
253: CommandPacket packet = new CommandPacket(
254: JDWPCommands.ArrayReferenceCommandSet.CommandSetID,
255: JDWPCommands.ArrayReferenceCommandSet.GetValuesCommand);
256: packet.setNextValueAsArrayID(arrayID);
257: packet.setNextValueAsInt(firstIndex);
258: packet.setNextValueAsInt(length);
259:
260: ReplyPacket reply = debuggeeWrapper.vmMirror
261: .performCommand(packet);
262: checkReplyPacket(reply, "ArrayReference::GetValues command",
263: error);
264:
265: if (reply.getErrorCode() == JDWPConstants.Error.NONE) {
266: // do not check values for non-array fields
267: ArrayRegion region = reply.getNextValueAsArrayRegion();
268: //System.err.println("length="+region.length);
269: byte arrayTag = region.getTag();
270: logWriter.println("==> arrayTag = " + arrayTag + "("
271: + JDWPConstants.Tag.getName(arrayTag) + ")");
272: logWriter.println("==> arrayLength = "
273: + region.getLength());
274: Value value_0 = region.getValue(0);
275: byte elementTag = value_0.getTag();
276: logWriter.println("==> elementTag = " + elementTag + "("
277: + JDWPConstants.Tag.getName(elementTag) + ")");
278:
279: assertEquals(
280: "ArrayReference::GetValues returned invalid array tag,",
281: expectedArrayTag, arrayTag, JDWPConstants.Tag
282: .getName(expectedArrayTag),
283: JDWPConstants.Tag.getName(arrayTag));
284: assertEquals(
285: "ArrayReference::GetValues returned invalid array length,",
286: length, region.getLength());
287: assertEquals(
288: "ArrayReference::GetValues returned invalid element tag",
289: expectedElementTag, elementTag, JDWPConstants.Tag
290: .getName(expectedElementTag),
291: JDWPConstants.Tag.getName(elementTag));
292:
293: if (checkValues) {
294: for (int i = 0; i < region.getLength(); i++) {
295: Value value = region.getValue(i);
296: if (value.getTag() == JDWPConstants.Tag.INT_TAG) {
297: assertEquals(
298: "ArrayReference::GetValues returned invalid value on index:<"
299: + i + ">,",
300: value.getIntValue(), i + firstIndex);
301: } else if (value.getTag() == JDWPConstants.Tag.STRING_TAG) {
302: long stringID = value.getLongValue();
303: String s = getStringValue(stringID);
304: assertEquals(
305: "ArrayReference::GetValues returned invalid value on index:<"
306: + i + ">,",
307: Integer.parseInt(s), i + firstIndex);
308: }
309: }
310: }
311: }
312: }
313: }
|