001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.debug.jdi.tests;
011:
012: import java.util.LinkedList;
013: import java.util.List;
014: import java.util.Map;
015:
016: import com.sun.jdi.AbsentInformationException;
017: import com.sun.jdi.ClassNotLoadedException;
018: import com.sun.jdi.InvalidTypeException;
019: import com.sun.jdi.LocalVariable;
020: import com.sun.jdi.ObjectReference;
021: import com.sun.jdi.ReferenceType;
022: import com.sun.jdi.StackFrame;
023: import com.sun.jdi.ThreadReference;
024:
025: /**
026: * Tests for JDI com.sun.jdi.StackFrame.
027: */
028: public class StackFrameTest extends AbstractJDITest {
029:
030: private StackFrame fFrame;
031:
032: /**
033: * Creates a new test.
034: */
035: public StackFrameTest() {
036: super ();
037: }
038:
039: /**
040: * Init the fields that are used by this test only.
041: */
042: public void localSetUp() {
043: // Get the frame running MainClass.run()
044: fFrame = getFrame(RUN_FRAME_OFFSET);
045:
046: }
047:
048: /**
049: * Run all tests and output to standard output.
050: * @param args
051: */
052: public static void main(java.lang.String[] args) {
053: new StackFrameTest().runSuite(args);
054: }
055:
056: /**
057: * Gets the name of the test case.
058: * @see junit.framework.TestCase#getName()
059: */
060: public String getName() {
061: return "com.sun.jdi.StackFrame";
062: }
063:
064: /**
065: * Test JDI equals() and hashCode().
066: */
067: public void testJDIEquality() {
068: StackFrame sameFrame = getFrame(RUN_FRAME_OFFSET);
069: StackFrame otherFrame = getFrame(0);
070:
071: // Not identical
072: assertTrue("1", fFrame != sameFrame);
073: // But equal
074: assertTrue("2", fFrame.equals(sameFrame));
075: assertTrue("3", fFrame.hashCode() == sameFrame.hashCode());
076:
077: assertTrue("4", fFrame.equals(fFrame));
078: assertTrue("5", fFrame.hashCode() == fFrame.hashCode());
079:
080: assertTrue("6", !fFrame.equals(otherFrame));
081:
082: assertTrue("7", !fFrame.equals(new Object()));
083: assertTrue("8", !fFrame.equals(null));
084: }
085:
086: /**
087: * Test JDI location().
088: */
089: public void testJDILocation() {
090: assertNotNull("1", fFrame.location());
091: }
092:
093: /**
094: * Test JDI setValue(LocalVariable, Value), getValue(LocalVariable) and
095: * getValues(List).
096: */
097: public void testJDISetGetValue() {
098: // setValue
099: ThreadReference thread = (ThreadReference) fVM.allThreads()
100: .get(0);
101: LocalVariable var = getLocalVariable();
102: try {
103: fFrame.setValue(var, thread);
104: } catch (ClassNotLoadedException e) {
105: assertTrue("1.1", false);
106: } catch (InvalidTypeException e) {
107: assertTrue("1.2", false);
108: }
109:
110: // getValue(LocalVariable)
111: ThreadReference value = (ThreadReference) fFrame
112: .getValue(getLocalVariable());
113: assertEquals("2", thread, value);
114:
115: // getValues(List)
116: List vars = new LinkedList();
117: vars.add(var);
118: Map values = fFrame.getValues(vars);
119: value = (ThreadReference) values.get(var);
120: assertEquals("3", thread, value);
121:
122: // test null value
123: var = getLocalVariable();
124: try {
125: fFrame.setValue(var, null);
126: } catch (ClassNotLoadedException e) {
127: assertTrue("4.1", false);
128: } catch (InvalidTypeException e) {
129: assertTrue("4.2", false);
130: }
131:
132: value = (ThreadReference) fFrame.getValue(getLocalVariable());
133: assertEquals("5", null, value);
134:
135: }
136:
137: /**
138: * Test JDI thisObject().
139: */
140: public void testJDIThisObject() {
141: ObjectReference object = fFrame.this Object();
142: ReferenceType expected = getMainClass();
143: ReferenceType referenceType = object.referenceType();
144: assertEquals("1", expected, referenceType);
145: }
146:
147: /**
148: * Test JDI thread().
149: */
150: public void testJDIThread() {
151: assertEquals("1", getThread(), fFrame.thread());
152: }
153:
154: /**
155: * Test JDI visibleVariableByName(String).
156: */
157: public void testJDIVisibleVariableByName() {
158: LocalVariable var = null;
159: try {
160: var = fFrame.visibleVariableByName("t");
161: } catch (AbsentInformationException e) {
162: assertTrue("1", false);
163: }
164: assertEquals("2", getLocalVariable(), var);
165: try {
166: var = fFrame.visibleVariableByName("bogus");
167: } catch (AbsentInformationException e) {
168: assertTrue("3", false);
169: }
170: assertTrue("4", null == var);
171: }
172:
173: /**
174: * Test JDI visibleVariables().
175: */
176: public void testJDIVisibleVariables() {
177: List vars = null;
178: try {
179: vars = fFrame.visibleVariables();
180: } catch (AbsentInformationException e) {
181: assertTrue("1", false);
182: }
183: assertEquals("2", 2, vars.size());
184:
185: LocalVariable var;
186: int i = 0;
187: do {
188: var = (LocalVariable) vars.get(i++);
189: } while (!var.name().equals("t"));
190: assertEquals("3", getLocalVariable(), var);
191: }
192: }
|