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: import java.util.ListIterator;
014:
015: import com.sun.jdi.ArrayReference;
016: import com.sun.jdi.ClassNotLoadedException;
017: import com.sun.jdi.DoubleValue;
018: import com.sun.jdi.InvalidTypeException;
019: import com.sun.jdi.StringReference;
020:
021: /**
022: * Tests for JDI com.sun.jdi.ArrayReference
023: * and JDWP Array command set.
024: */
025: public class ArrayReferenceTest extends AbstractJDITest {
026:
027: private ArrayReference fArray;
028: private ArrayReference fDoubleArray;
029:
030: /**
031: * Creates a new test.
032: */
033: public ArrayReferenceTest() {
034: super ();
035: }
036:
037: /**
038: * Init the fields that are used by this test only.
039: */
040: public void localSetUp() {
041: // Get array references
042: fArray = getObjectArrayReference();
043: fDoubleArray = getNonEmptyDoubleArrayReference();
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 ArrayReferenceTest().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.ArrayReference";
060: }
061:
062: /**
063: * Test JDI getValue(int), getValues(), getValues(int,int)
064: * setValue(Value,int), setValues(List) and setValues(int,List,int,int),
065: * and JDWP 'Array - Get values' and 'Array - Set values'.
066: */
067: public void testJDIGetSetDoubleValue() {
068: double one = 1.0;
069: double pi = 3.1415926535;
070: double twos = 2.2;
071: double threes = 3.33;
072: double cnt = 12345;
073: double zero = 0.0;
074: double delta = 0.0;
075:
076: DoubleValue dbl = (DoubleValue) fDoubleArray.getValue(0);
077: assertEquals("testJDIGetSetDoubleValue.1", one, dbl.value(),
078: delta);
079:
080: DoubleValue piValue = fVM.mirrorOf(pi);
081: DoubleValue cntValue = fVM.mirrorOf(cnt);
082: DoubleValue zeroValue = fVM.mirrorOf(zero);
083: try {
084: fDoubleArray.setValue(0, piValue);
085: } catch (ClassNotLoadedException e) {
086: assertTrue("testJDIGetSetDoubleValue.3.1", false);
087: } catch (InvalidTypeException e) {
088: assertTrue("testJDIGetSetDoubleValue.3.2", false);
089: }
090: DoubleValue value = (DoubleValue) fDoubleArray.getValue(0);
091: assertEquals("testJDIGetSetDoubleValue.4.1", value, piValue);
092: assertEquals("testJDIGetSetDoubleValue.4.2", pi, value.value(),
093: delta);
094:
095: // getValues()
096: List values = fDoubleArray.getValues();
097: double[] expected = new double[] { pi, twos, threes };
098: ListIterator iterator = values.listIterator();
099: while (iterator.hasNext()) {
100: DoubleValue dv = (DoubleValue) iterator.next();
101: boolean included = false;
102: for (int i = 0; i < expected.length; i++) {
103: if (dv.value() == expected[i]) {
104: included = true;
105: break;
106: }
107: }
108: assertTrue("testJDIGetSetDoubleValue.5." + dv.value(),
109: included);
110: }
111:
112: // setValues(List)
113: List newValues = values;
114: newValues.set(1, cntValue);
115: try {
116: fDoubleArray.setValues(newValues);
117: } catch (ClassNotLoadedException e) {
118: assertTrue("testJDIGetSetDoubleValue.7.1", false);
119: } catch (InvalidTypeException e) {
120: assertTrue("testJDIGetSetDoubleValue.7.2", false);
121: }
122: values = fDoubleArray.getValues();
123: assertEquals("testJDIGetSetDoubleValue.8", values, newValues);
124:
125: // getValues(int,int)
126: values = fDoubleArray.getValues(1, 2);
127: expected = new double[] { cnt, threes };
128: iterator = values.listIterator();
129: while (iterator.hasNext()) {
130: DoubleValue dv = (DoubleValue) iterator.next();
131: boolean included = false;
132: for (int i = 0; i < expected.length; i++) {
133: if (dv.value() == expected[i]) {
134: included = true;
135: break;
136: }
137: }
138: assertTrue("testJDIGetSetDoubleValue.9." + dv.value(),
139: included);
140: }
141:
142: // setValues(int,List,int,int)
143: newValues = fDoubleArray.getValues(0, 2);
144: newValues.set(0, zeroValue);
145: try {
146: fDoubleArray.setValues(0, newValues, 0, 2);
147: } catch (ClassNotLoadedException e) {
148: assertTrue("testJDIGetSetDoubleValue.11.1", false);
149: } catch (InvalidTypeException e) {
150: assertTrue("testJDIGetSetDoubleValue.11.2", false);
151: }
152: values = fDoubleArray.getValues(0, 2);
153: assertEquals("testJDIGetSetDoubleValue.12", values, newValues);
154: }
155:
156: /**
157: * Test JDI getValue(int), getValues(), getValues(int,int)
158: * setValue(Value,int), setValues(List) and setValues(int,List,int,int),
159: * and JDWP 'Array - Get values' and 'Array - Set values'.
160: */
161: public void testJDIGetSetValue() {
162: // getValue(int)
163: StringReference string = (StringReference) fArray.getValue(0);
164: assertEquals("1", "foo", string.value());
165:
166: // setValue(int,Value)
167: StringReference newValue = null;
168: newValue = fVM.mirrorOf("biz");
169: try {
170: fArray.setValue(0, newValue);
171: } catch (ClassNotLoadedException e) {
172: assertTrue("2.1", false);
173: } catch (InvalidTypeException e) {
174: assertTrue("2.2", false);
175: }
176: StringReference value = (StringReference) fArray.getValue(0);
177: assertEquals("3", value, newValue);
178:
179: // getValues()
180: List values = fArray.getValues();
181: String[] expected = new String[] { "biz", "bar", "hop" };
182: ListIterator iterator = values.listIterator();
183: while (iterator.hasNext()) {
184: StringReference ref = (StringReference) iterator.next();
185: boolean included = false;
186: for (int i = 0; i < expected.length; i++) {
187: if (ref.value().equals(expected[i])) {
188: included = true;
189: break;
190: }
191: }
192: assertTrue("4." + ref.value(), included);
193: }
194:
195: // setValues(List)
196: List newValues = values;
197: newValue = fVM.mirrorOf("hip");
198: newValues.set(1, newValue);
199: try {
200: fArray.setValues(newValues);
201: } catch (ClassNotLoadedException e) {
202: assertTrue("5.1", false);
203: } catch (InvalidTypeException e) {
204: assertTrue("6.2", false);
205: }
206: values = fArray.getValues();
207: assertEquals("7", values, newValues);
208:
209: // getValues(int,int)
210: values = fArray.getValues(1, 2);
211: expected = new String[] { "hip", "hop" };
212: iterator = values.listIterator();
213: while (iterator.hasNext()) {
214: StringReference ref = (StringReference) iterator.next();
215: boolean included = false;
216: for (int i = 0; i < expected.length; i++) {
217: if (ref.value().equals(expected[i])) {
218: included = true;
219: break;
220: }
221: }
222: assertTrue("8." + ref.value(), included);
223: }
224:
225: // setValues(int,List,int,int)
226: newValues = fArray.getValues(0, 2);
227: newValue = fVM.mirrorOf("rap");
228: newValues.set(0, newValue);
229: try {
230: fArray.setValues(0, newValues, 0, 2);
231: } catch (ClassNotLoadedException e) {
232: assertTrue("9.1", false);
233: } catch (InvalidTypeException e) {
234: assertTrue("9.2", false);
235: }
236: values = fArray.getValues(0, 2);
237: assertEquals("10", values, newValues);
238:
239: // test null value
240: newValues.set(0, null);
241: try {
242: fArray.setValues(0, newValues, 0, 2);
243: } catch (ClassNotLoadedException e) {
244: assertTrue("11.1", false);
245: } catch (InvalidTypeException e) {
246: assertTrue("11.2", false);
247: }
248: values = fArray.getValues(0, 2);
249: assertEquals("12", values.get(0), null);
250: }
251:
252: /**
253: * Test JDI length() and JDWP 'Array - Get length'.
254: */
255: public void testJDILength() {
256: int length = fArray.length();
257: assertEquals("1", 3, length);
258: }
259: }
|