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.List;
013:
014: import com.sun.jdi.AbsentInformationException;
015: import com.sun.jdi.ClassNotLoadedException;
016: import com.sun.jdi.LocalVariable;
017: import com.sun.jdi.Location;
018: import com.sun.jdi.Method;
019: import com.sun.jdi.NativeMethodException;
020: import com.sun.jdi.VoidType;
021:
022: /**
023: * Tests for JDI com.sun.jdi.Method
024: * and JDWP Method command set.
025: */
026: public class MethodTest extends AbstractJDITest {
027:
028: private Method fMethod1;
029:
030: /**
031: * Creates a new test.
032: */
033: public MethodTest() {
034: super ();
035: }
036:
037: /**
038: * Init the fields that are used by this test only.
039: */
040: public void localSetUp() {
041: // Get method useLocalVars(Thread, MainClass)
042: fMethod1 = getMethod("useLocalVars",
043: "(Ljava/lang/Thread;Lorg/eclipse/debug/jdi/tests/program/MainClass;)V");
044: }
045:
046: /**
047: * Run all tests and output to standard output.
048: * @param args
049: */
050: public static void main(java.lang.String[] args) {
051: new MethodTest().runSuite(args);
052: }
053:
054: /**
055: * Gets the name of the test case.
056: * @see junit.framework.TestCase#getName()
057: */
058: public String getName() {
059: return "com.sun.jdi.Method";
060: }
061:
062: /**
063: * Test JDI arguments() and JDWP 'Method - Get variable table'.
064: */
065: public void testJDIArguments() {
066: List arguments = null;
067: try {
068: arguments = fMethod1.arguments();
069: } catch (AbsentInformationException e) {
070: assertTrue("1", false);
071: }
072: assertEquals("2", 2, arguments.size());
073: assertEquals("3", "t", ((LocalVariable) arguments.get(0))
074: .name());
075: assertEquals("4", "o", ((LocalVariable) arguments.get(1))
076: .name());
077: }
078:
079: /**
080: * Test JDI argumentTypeNames().
081: */
082: public void testJDIArgumentTypeNames() {
083: List names = fMethod1.argumentTypeNames();
084: assertEquals("1", 2, names.size());
085: assertEquals("2", "java.lang.Thread", names.get(0));
086: assertEquals("3",
087: "org.eclipse.debug.jdi.tests.program.MainClass", names
088: .get(1));
089: }
090:
091: /**
092: * Test JDI argumentTypes().
093: */
094: public void testJDIArgumentTypes() {
095: List types = null;
096: try {
097: types = fMethod1.argumentTypes();
098: } catch (ClassNotLoadedException e) {
099: assertTrue("1", false);
100: }
101: assertEquals("2", 2, types.size());
102: assertEquals("3", fVM.classesByName("java.lang.Thread").get(0),
103: types.get(0));
104: assertEquals(
105: "4",
106: fVM
107: .classesByName(
108: "org.eclipse.debug.jdi.tests.program.MainClass")
109: .get(0), types.get(1));
110: }
111:
112: /**
113: * Test JDI bytecodes().
114: */
115: public void testJDIBytecodes() {
116: if (!fVM.canGetBytecodes())
117: return;
118:
119: byte[] bytecodes = fMethod1.bytecodes();
120: assertEquals("1", 27, bytecodes.length);
121: }
122:
123: /**
124: * Test JDI equals() and hashCode().
125: */
126: public void testJDIEquality() {
127: assertTrue("1", fMethod1.equals(fMethod1));
128: Method other = getMethod("run", "()V");
129: assertTrue("2", !fMethod1.equals(other));
130: assertTrue("3", !fMethod1.equals(new Object()));
131: assertTrue("4", !fMethod1.equals(null));
132: assertTrue("5", fMethod1.hashCode() != other.hashCode());
133: }
134:
135: /**
136: * Test JDI isAbstract().
137: */
138: public void testJDIIsAbstract() {
139: assertTrue("1", !fMethod1.isAbstract());
140: }
141:
142: /**
143: * Test JDI isConstructor().
144: */
145: public void testJDIIsConstructor() {
146: assertTrue("1", !fMethod1.isConstructor());
147: }
148:
149: /**
150: * Test JDI isNative().
151: */
152: public void testJDIIsNative() {
153: assertTrue("1", !fMethod1.isNative());
154: }
155:
156: /**
157: * Test JDI isStaticInitializer().
158: */
159: public void testJDIIsStaticInitializer() {
160: assertTrue("1", !fMethod1.isStaticInitializer());
161: }
162:
163: /**
164: * Test JDI isSynchronized().
165: */
166: public void testJDIIsSynchronized() {
167: assertTrue("1", !fMethod1.isSynchronized());
168: }
169:
170: /**
171: * Test JDI locationOfCodeIndex(long).
172: */
173: public void testJDILocationOfCodeIndex() {
174: Location expected = fMethod1.location();
175: Location result = fMethod1.locationOfCodeIndex(expected
176: .codeIndex());
177: assertEquals("1", expected, result);
178: }
179:
180: /**
181: * Test JDI locationsOfLine(int) and JDWP 'Method - Get line table'.
182: */
183: public void testJDILocationsOfLine() {
184: int expected = fMethod1.location().lineNumber();
185: List locations = null;
186: try {
187: locations = fMethod1.locationsOfLine(expected);
188: } catch (AbsentInformationException e) {
189: assertTrue("1", false);
190: }
191: assertEquals("2", 1, locations.size());
192: assertEquals("3", expected, ((Location) locations.get(0))
193: .lineNumber());
194: }
195:
196: /**
197: * Test JDI returnType().
198: */
199: public void testJDIReturnType() {
200: try {
201: assertTrue("1", fMethod1.returnType() instanceof VoidType);
202: } catch (ClassNotLoadedException e) {
203: assertTrue("2", false);
204: }
205: }
206:
207: /**
208: * Test JDI returnTypeName().
209: */
210: public void testJDIReturnTypeName() {
211: assertEquals("1", "void", fMethod1.returnTypeName());
212: }
213:
214: /**
215: * Test JDI variables() and JDWP 'Method - Get variable table'.
216: */
217: public void testJDIVariables() {
218: List variables = null;
219: try {
220: variables = fMethod1.variables();
221: } catch (AbsentInformationException e) {
222: assertTrue("1", false);
223: }
224: assertEquals("2", 2, variables.size());
225: assertEquals("3", "t", ((LocalVariable) variables.get(0))
226: .name());
227: assertEquals("4", "o", ((LocalVariable) variables.get(1))
228: .name());
229: }
230:
231: /**
232: * Test JDI variables() and JDWP 'Method - Get variable table'
233: * for native method.
234: */
235: public void testJDINativeMethodVariables() {
236: Method method = getMethod("java.lang.Thread", "sleep", "(J)V");
237:
238: try {
239: method.variables();
240: } catch (AbsentInformationException e) {
241: // since 1.4, returns an AbsentInformationException for native methods
242: assertFalse("1", "1.3".equals(fVM.version()));
243: return;
244: } catch (NativeMethodException nme) {
245: assertTrue("1", "1.3".equals(fVM.version()));
246: return;
247: }
248: assertTrue("Should have thrown native method exception", false);
249: }
250:
251: /**
252: * Test JDI variables() and JDWP 'Method - Get variable table'
253: * for non-native method with a long argument.
254: */
255: public void testJDIMethodVariablesWithLong() {
256: List variables = null;
257: Method method = getMethod("variablesTest", "(J)V");
258:
259: try {
260: variables = method.variables();
261: } catch (AbsentInformationException e) {
262: assertTrue("1", false);
263: }
264: assertEquals("1", 1, variables.size());
265: }
266:
267: /**
268: * Test JDI variablesByName(String) and JDWP 'Method - Get variable table'.
269: */
270: public void testJDIVariablesByName() {
271: String varName = "t";
272: List variables = null;
273: try {
274: variables = fMethod1.variablesByName(varName);
275: } catch (AbsentInformationException e) {
276: assertTrue("1", false);
277: }
278: assertEquals("2", 1, variables.size());
279: assertEquals("3", varName, ((LocalVariable) variables.get(0))
280: .name());
281: }
282: }
|