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 Aleksander V. Budniy
021: * @version $Revision: 1.4 $
022:
023: /**
024: * Created on 15.08.2005
025: */package org.apache.harmony.jpda.tests.jdwp.StackFrame;
026:
027: import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
028: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
029: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
030: import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
031: import org.apache.harmony.jpda.tests.framework.jdwp.Value;
032: import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
033:
034: /**
035: * JDWP Unit test for StackFrame.GetValues command.
036: */
037: public class GetValuesTest extends JDWPStackFrameTestCase {
038:
039: String testedMethodName = "nestledMethod3";
040: String testedThreadName = "";
041:
042: VarInfo[] varInfos;
043:
044: String varSignature[] = {
045: "Lorg/apache/harmony/jpda/tests/jdwp/StackFrame/StackTraceDebuggee;",
046: "Z", "I", "Ljava/lang/String;" };
047:
048: String varNames[] = { "this", "boolLocalVariable",
049: "intLocalVariable", "strLocalVariable" };
050:
051: byte varTags[] = { JDWPConstants.Tag.OBJECT_TAG,
052: JDWPConstants.Tag.BOOLEAN_TAG, JDWPConstants.Tag.INT_TAG,
053: JDWPConstants.Tag.STRING_TAG };
054:
055: private String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/StackFrame/StackTraceDebuggee;";
056:
057: /**
058: * This testcase exercises StackFrame.GetValues command.
059: * <BR>The test starts StackTraceDebuggee, sets breakpoint at the beginning of
060: * the tested method - 'nestledMethod3' and stops at the breakpoint.
061: * <BR> Then the test performs Method.VariableTable command and checks
062: * returned VariableTable.
063: * <BR> At last the test performs StackFrame.GetValues command and checks
064: * returned values of variables.
065: *
066: */
067: public void testGetValues001() {
068: logWriter.println("==> testGetValues001 started...");
069: testedThreadName = synchronizer.receiveMessage();
070: // release on run()
071: synchronizer
072: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
073:
074: // pass nestledMethod1()
075: synchronizer
076: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
077: synchronizer
078: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
079:
080: // enter nestledMethod2()
081: synchronizer
082: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
083:
084: //release debuggee
085: synchronizer
086: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
087: synchronizer
088: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
089:
090: //gets and checks local variables of tested method
091: examineGetValues();
092:
093: // signal to finish debuggee
094: synchronizer
095: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
096: logWriter.println("==> testGetValues001 - OK.");
097:
098: }
099:
100: private void examineGetValues() {
101:
102: long refTypeID = getClassIDBySignature(debuggeeSignature);
103: logWriter.println("=> Debuggee class = "
104: + getDebuggeeClassName());
105: logWriter.println("=> referenceTypeID for Debuggee class = "
106: + refTypeID);
107: long threadID = debuggeeWrapper.vmMirror
108: .getThreadID(testedThreadName);
109:
110: logWriter.println("=> testedThreadID = " + threadID);
111: if (threadID == -1) {
112: printErrorAndFail("testedThread is not found!");
113: }
114:
115: // suspend thread
116: jdwpSuspendThread(threadID);
117:
118: //get number of frames
119: int frameCount = jdwpGetFrameCount(threadID);
120: logWriter.println("=> frames count = " + frameCount);
121:
122: //get frames info
123: FrameInfo[] frameIDs = jdwpGetFrames(threadID, 0, frameCount);
124: if (frameIDs.length != frameCount) {
125: printErrorAndFail("Received number of frames = "
126: + frameIDs.length
127: + " differ from expected number = " + frameCount);
128: }
129:
130: //check and print methods info
131: long methodID = 0;
132: long frameID = 0;
133: String methodName = "";
134: boolean testedMethodChecked = false;
135: for (int i = 0; i < frameCount; i++) {
136: logWriter.println("\n");
137: methodName = getMethodName(frameIDs[i].location.classID,
138: frameIDs[i].location.methodID);
139: logWriter.println("=> method name = " + methodName);
140: logWriter.println("=> methodID = "
141: + frameIDs[i].location.methodID);
142: logWriter.println("=> frameID = " + frameIDs[i].frameID);
143: logWriter.println("\n");
144: if (methodName.equals(testedMethodName)) {
145: methodID = frameIDs[i].location.methodID;
146: frameID = frameIDs[i].frameID;
147: methodName = getMethodName(
148: frameIDs[i].location.classID,
149: frameIDs[i].location.methodID);
150: testedMethodChecked = true;
151: }
152: }
153: if (testedMethodChecked) {
154: logWriter.println("=> Tested method is found");
155: logWriter.println("=> method name = " + testedMethodName);
156: logWriter.println("=> methodID = " + methodID);
157: logWriter.println("=> frameID = " + frameID);
158:
159: } else {
160: printErrorAndFail("Tested method is not found");
161: }
162:
163: //getting Variable Table
164: logWriter.println("");
165: logWriter.println("=> Getting Variable Table...");
166: varInfos = jdwpGetVariableTable(refTypeID, methodID);
167: if (checkVarTable(varInfos)) {
168: logWriter.println("=> Variable table check passed.");
169: } else {
170: printErrorAndFail("Variable table check failed.");
171: }
172:
173: //prepare and perform GetValues command
174: logWriter.println("");
175: logWriter.println("=> Send StackFrame::GetValues command...");
176: CommandPacket packet = new CommandPacket(
177: JDWPCommands.StackFrameCommandSet.CommandSetID,
178: JDWPCommands.StackFrameCommandSet.GetValuesCommand);
179: packet.setNextValueAsThreadID(threadID);
180: packet.setNextValueAsFrameID(frameID);
181:
182: logWriter.println("=> Thread: " + threadID);
183: logWriter.println("=> Frame: " + frameID);
184: packet.setNextValueAsInt(varTags.length);
185: for (int i = 0; i < varTags.length; i++) {
186: logWriter.println("");
187: logWriter.println("=> For variable #" + i + ":");
188: packet.setNextValueAsInt(varInfos[i].getSlot());
189: logWriter.println("=> Slot = " + varInfos[i].getSlot());
190: packet.setNextValueAsByte(varTags[i]);
191: logWriter.println("=> Tag = "
192: + JDWPConstants.Tag.getName(varTags[i]));
193: logWriter.println("");
194: }
195:
196: //check reply for errors
197: ReplyPacket reply = debuggeeWrapper.vmMirror
198: .performCommand(packet);
199: checkReplyPacket(reply, "StackFrame::GetValues command");
200:
201: //check number of retrieved values
202: int numberOfValues = reply.getNextValueAsInt();
203: logWriter.println("=> Number of values = " + numberOfValues);
204: if (numberOfValues != varTags.length) {
205: printErrorAndFail("Unexpected number of values: "
206: + numberOfValues + " instead of " + varTags.length);
207: }
208:
209: boolean success = true;
210: //print and check values of variables
211: logWriter.println("=> Values of variables: ");
212:
213: Value val = reply.getNextValueAsValue();
214: if (val.getTag() == JDWPConstants.Tag.OBJECT_TAG) {
215: logWriter.println("=> Tag is correct");
216: logWriter.println("");
217:
218: } else {
219: logWriter.printError("Unexpected tag of variable: "
220: + JDWPConstants.Tag.getName(val.getTag())
221: + " instead of: CLASS_OBJECT_TAG");
222: logWriter.printError("");
223: success = false;
224: }
225:
226: val = reply.getNextValueAsValue();
227: if (val.getTag() == JDWPConstants.Tag.BOOLEAN_TAG) {
228: logWriter.println("=>Tag is correct");
229: boolean boolValue = val.getBooleanValue();
230: if (boolValue) {
231: logWriter.println("=> " + varInfos[1].getName() + " = "
232: + boolValue);
233: logWriter.println("");
234: } else {
235: logWriter
236: .printError("Unexpected value of boolean variable: "
237: + boolValue + " instead of: true");
238: logWriter.printError("");
239: success = false;
240: }
241: } else {
242: logWriter.printError("Unexpected tag of variable: "
243: + JDWPConstants.Tag.getName(val.getTag())
244: + " instead of: boolean");
245: logWriter.printError("");
246: success = false;
247: }
248:
249: val = reply.getNextValueAsValue();
250: if (val.getTag() == JDWPConstants.Tag.INT_TAG) {
251: logWriter.println("=>Tag is correct");
252: int intValue = val.getIntValue();
253: if (intValue == -512) {
254: logWriter.println("=> " + varInfos[2].getName() + " = "
255: + intValue);
256: logWriter.println("");
257: } else {
258: logWriter
259: .printError("unexpected value of int variable: "
260: + intValue + " instead of: -512");
261: logWriter.printError("");
262: success = false;
263: }
264: } else {
265: logWriter.printError("Unexpected tag of variable: "
266: + JDWPConstants.Tag.getName(val.getTag())
267: + " instead of: integer");
268: logWriter.printError("");
269: success = false;
270: }
271:
272: val = reply.getNextValueAsValue();
273: if (val.getTag() == JDWPConstants.Tag.STRING_TAG) {
274: logWriter.println("=>Tag is correct");
275: long strLocalVariableID = val.getLongValue();
276: String strLocalVariable = getStringValue(strLocalVariableID);
277: if (strLocalVariable.equals("test string")) {
278: logWriter.println("=> " + varInfos[2].getName() + " = "
279: + strLocalVariable);
280: logWriter.println("");
281: } else {
282: logWriter
283: .printError("Unexpected value of string variable: "
284: + strLocalVariable
285: + " instead of: "
286: + "test string");
287: logWriter.printError("");
288: success = false;
289: }
290: } else {
291: logWriter.printError("Unexpected tag of variable: "
292: + JDWPConstants.Tag.getName(val.getTag())
293: + " instead of: string");
294: logWriter.printError("");
295: success = false;
296: }
297: assertTrue(logWriter.getErrorMessage(), success);
298: }
299:
300: //prints variables info, checks signatures
301: boolean checkVarTable(VarInfo[] varInfos) {
302: boolean success = true;
303: logWriter.println("==> Number of variables = "
304: + varInfos.length);
305: if (varInfos.length != varTags.length) {
306:
307: printErrorAndFail("Unexpected number of variables: "
308: + varInfos.length + " instead of " + varTags.length);
309: }
310: for (int i = 0; i < varInfos.length; i++) {
311: logWriter.println("");
312: logWriter.println("=> Name = " + varInfos[i].getName());
313: //## take codeIndex and length and check them
314: logWriter.println("=> Slot = " + varInfos[i].getSlot());
315: logWriter
316: .println("=> Sign = " + varInfos[i].getSignature());
317: if (!(varSignature[i].equals(varInfos[i].getSignature()))) {
318: logWriter
319: .printError("Unexpected signature of variable = "
320: + varInfos[i].getName()
321: + ", on slot = "
322: + varInfos[i].getSlot()
323: + ", with unexpected signature = "
324: + varInfos[i].getSignature()
325: + " instead of signature = "
326: + varSignature[i]);
327: success = false;
328: }
329: if (!(varNames[i].equals(varInfos[i].getName()))) {
330: logWriter.printError("Unexpected name of variable "
331: + varInfos[i].getName() + ", on slot = "
332: + varInfos[i].getSlot() + " instead of name = "
333: + varNames[i]);
334: success = false;
335: }
336:
337: logWriter.println("");
338: }
339: return success;
340: }
341:
342: public static void main(String[] args) {
343: junit.textui.TestRunner.run(GetValuesTest.class);
344: }
345:
346: }
|