001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.ArrayList;
013: import java.util.List;
014:
015: import com.sun.jdi.ObjectReference;
016: import com.sun.jdi.ReferenceType;
017:
018: /**
019: * Test cases for the implementation of heap walking in the new java 1.6 VM
020: *
021: * @since 3.3
022: */
023: public class HeapWalkingTests extends AbstractJDITest {
024:
025: private ReferenceType fClass, fClass1;
026: private ObjectReference fObject;
027:
028: /** setup our tests */
029: public void localSetUp() {
030: }
031:
032: /** tear down our tests */
033: public void localTearDown() {
034: super .localTearDown();
035: fClass = null;
036: fClass1 = null;
037: fObject = null;
038: }
039:
040: /**
041: * test to make sure that the vm supports getting instance info for heap walking if it is a 1.6 VM
042: */
043: public void testCanGetInstanceInfo() {
044: if (fVM.version().indexOf("1.6") > -1) {
045: assertTrue("Should have instance info", fVM
046: .canGetInstanceInfo());
047: } else {
048: assertTrue("Should not have instance info", !fVM
049: .canGetInstanceInfo());
050: }
051: }
052:
053: /**
054: * tests the new method instanceCounts, to make sure it throws an NPE when required.
055: * test is not applicable to non 1.6 VMs
056: */
057: public void testGetInstanceCountsNullAttrib() {
058: if (!fVM.canGetInstanceInfo()) {
059: return;
060: }
061: try {
062: fVM.instanceCounts(null);
063: assertTrue("No excpetion thrown", false);
064: } catch (NullPointerException npe) {
065: }
066: }
067:
068: /**
069: * tests to make sure the instanceCounts method throws a not supported
070: */
071: public void testGetInstanceCountsUnsupported() {
072: if (fVM.version().indexOf("1.6") > -1) {
073: try {
074: fVM.instanceCounts(new ArrayList());
075: } catch (UnsupportedOperationException uoe) {
076: assertTrue("Threw unsupported exception in 1.6 VM",
077: false);
078: }
079: } else {
080: try {
081: fVM.instanceCounts(new ArrayList());
082: assertTrue("No exception for non 1.6 VM", false);
083: } catch (UnsupportedOperationException uoe) {
084: }
085: }
086: }
087:
088: /**
089: * test to collect any referring instances can be collected for the specified class.
090: * test is not applicable to non 1.6 VMs.
091: */
092: public void testGetInstanceCounts() {
093: if (!fVM.canGetInstanceInfo()) {
094: return;
095: }
096: triggerAndWait(fVM.eventRequestManager()
097: .createClassPrepareRequest(), "refclass3load", true);
098: triggerAndWait(fVM.eventRequestManager()
099: .createClassPrepareRequest(), "refclassload", true);
100: ArrayList list = new ArrayList(2);
101: fClass = getClass("org.eclipse.debug.jdi.tests.program.RefClass1");
102: assertNotNull("RefClass should not be null", fClass);
103: list.add(fClass);
104: fClass1 = getClass("org.eclipse.debug.jdi.tests.program.RefClass2");
105: list.add(fClass1);
106: long[] counts = fVM.instanceCounts(list);
107: assertNotNull("counts should not be null", counts);
108: assertTrue("counts should have two entires", counts.length == 2);
109: assertTrue("count for RefClass1 should be 2", counts[0] == 2);
110: assertTrue("count for RefClass2 should be 1", counts[1] == 1);
111: }
112:
113: /**
114: * test to make sure instances throws an unsupported exception for non 1.6 VMs
115: */
116: public void testGetInstancesUnsupported() {
117: fClass = getClass("java.io.PrintStream");
118: assertNotNull("classs should not be null", fClass);
119: if (fVM.version().indexOf("1.6") > -1) {
120: try {
121: fClass.instances(20);
122: } catch (UnsupportedOperationException uoe) {
123: assertTrue("Threw unsupported exception in 1.6 VM",
124: false);
125: }
126: } else {
127: try {
128: fClass.instances(20);
129: assertTrue("No exception for non 1.6 VM", false);
130: } catch (UnsupportedOperationException uoe) {
131: }
132: }
133: }
134:
135: /**
136: * test to make sure instances throws and IllegalArgument exception for negative long arguments.
137: * test is not applicable to non 1.6 VMs
138: */
139: public void testGetInstancesNegativeMax() {
140: if (!fVM.canGetInstanceInfo()) {
141: return;
142: }
143: fClass = getClass("java.io.PrintStream");
144: assertNotNull("classs should not be null", fClass);
145: try {
146: fClass.instances(-1);
147: assertTrue("No excpetion thrown", false);
148: } catch (IllegalArgumentException iae) {
149: }
150: }
151:
152: /**
153: * test to collect a list of instances.
154: * test is not applicable to non 1.6 VMs.
155: */
156: public void testGetInstances() {
157: if (!fVM.canGetInstanceInfo()) {
158: return;
159: }
160: triggerAndWait(fVM.eventRequestManager()
161: .createClassPrepareRequest(), "refclass3load", true);
162: triggerAndWait(fVM.eventRequestManager()
163: .createClassPrepareRequest(), "refclassload", true);
164: fClass = getClass("org.eclipse.debug.jdi.tests.program.RefClass3");
165: assertNotNull("RefClass3 should not be null", fClass);
166: fClass1 = getClass("org.eclipse.debug.jdi.tests.program.RefClass1");
167: assertNotNull("RefClass1 should not be null", fClass1);
168: List list = fClass1.instances(10);
169: assertNotNull("list should not be null", list);
170: assertTrue("list should have two enrtries", list.size() == 2);
171: }
172:
173: /**
174: * test to make sure referringObjects throws an unsupported exception for non-1.6 VMs
175: */
176: public void testGetReferringObjectsUnsupported() {
177: fClass = getMainClass();
178: assertNotNull("main class ref should not be null", fClass);
179: fObject = getObjectReference();
180: assertNotNull("String obj ref should not be null", fObject);
181: if (fVM.version().indexOf("1.6") > -1) {
182: try {
183: fObject.referringObjects(100);
184: } catch (UnsupportedOperationException uoe) {
185: assertTrue("Threw unsupported exception in 1.6 VM",
186: false);
187: }
188: } else {
189: try {
190: fObject.referringObjects(10);
191: assertTrue("No exception for non 1.6 VM", false);
192: } catch (UnsupportedOperationException uoe) {
193: }
194: }
195: }
196:
197: /**
198: * test to make sure referringObjects throws an IllegalArgument exception for bad values of max
199: */
200: public void testGetreferringObjectsNegativeMax() {
201: if (!fVM.canGetInstanceInfo()) {
202: return;
203: }
204: fClass = getMainClass();
205: assertNotNull("main class ref should not be null", fClass);
206: fObject = getStringReference();
207: assertNotNull("String obj ref should not be null", fObject);
208: try {
209: fObject.referringObjects(-1);
210: assertTrue("No excpetion thrown", false);
211: } catch (IllegalArgumentException iae) {
212: assertTrue("Threw exception", true);
213: }
214: }
215:
216: /**
217: * tests the method referring objects to ensure working to spec.
218: * test is not applicable to non 1.6 VMs
219: */
220: public void testGetReferringObjects() {
221: if (!fVM.canGetInstanceInfo()) {
222: return;
223: }
224: fClass = getMainClass();
225: assertNotNull("main class ref should not be null", fClass);
226: triggerAndWait(fVM.eventRequestManager()
227: .createClassPrepareRequest(), "refclassload", true);
228: fClass1 = getClass("org.eclipse.debug.jdi.tests.program.RefClass");
229: assertNotNull("RefClass should not be null", fClass1);
230: fObject = getObjectReference();
231: assertNotNull("String obj ref should not be null", fObject);
232: List list = fObject.referringObjects(100);
233: assertNotNull("referring objects list should not be null", list);
234: assertTrue("list size should be 4", list.size() == 4);
235: assertTrue("list should contain the main class", list
236: .contains(fClass.classObject()));
237: assertTrue("list should contain the main class thread", list
238: .contains(getThread()));
239: }
240: }
|