1 /*******************************************************************************
2  * Copyright (c) 2000, 2015 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.debug.jdi.tests;
15 
16 import java.util.LinkedList;
17 import java.util.List;
18 import java.util.ListIterator;
19 
20 import com.sun.jdi.Mirror;
21 import com.sun.jdi.request.AccessWatchpointRequest;
22 import com.sun.jdi.request.BreakpointRequest;
23 import com.sun.jdi.request.ModificationWatchpointRequest;
24 
25 /**
26  * Tests for JDI com.sun.jdi.Mirror.
27  */
28 public class MirrorTest extends AbstractJDITest {
29 	List<Mirror> fMirrors = new LinkedList<>();
30 	/**
31 	 * Creates a new test.
32 	 */
MirrorTest()33 	public MirrorTest() {
34 		super();
35 	}
36 	/**
37 	 * Init the fields that are used by this test only.
38 	 */
39 	@Override
localSetUp()40 	public void localSetUp() {
41 		// Get all kinds of concrete mirror that can be found in the VM
42 		// in alphabetical order.
43 
44 		//TO DO: Add events too
45 
46 		fMirrors = new LinkedList<>();
47 
48 		if (fVM.canWatchFieldAccess())
49 			fMirrors.add(getAccessWatchpointRequest());
50 		// AccessWatchpointRequest
51 
52 		fMirrors.add(getObjectArrayReference()); // ArrayReference
53 		fMirrors.add(getArrayType()); // ArrayType
54 		fMirrors.add(fVM.mirrorOf(true)); // BooleanValue
55 		fMirrors.add(getBreakpointRequest()); // BreakpointRequest
56 		fMirrors.add(fVM.mirrorOf((byte) 1)); // ByteValue
57 		fMirrors.add(fVM.mirrorOf('1')); // CharValue
58 		fMirrors.add(getClassLoaderReference()); // ClassLoaderReference
59 		fMirrors.add(getMainClass()); // ClassType
60 		fMirrors.add(fVM.mirrorOf(12345.6789)); // DoubleValue
61 		fMirrors.add(fVM.eventRequestManager()); // EventRequestManager
62 		fMirrors.add(fVM.eventQueue()); // EventQueue
63 		fMirrors.add(getField()); // Field
64 		fMirrors.add(fVM.mirrorOf(123.45f)); // FieldValue
65 		fMirrors.add(fVM.mirrorOf(12345)); // IntegerValue
66 		fMirrors.add(getInterfaceType()); // InterfaceType
67 		fMirrors.add(getLocalVariable()); // LocalVariable
68 		fMirrors.add(getLocation()); // Location
69 		fMirrors.add(fVM.mirrorOf(123456789l)); // LongValue
70 		fMirrors.add(getMethod()); // Method
71 
72 		if (fVM.canWatchFieldModification())
73 			fMirrors.add(getModificationWatchpointRequest());
74 		// ModificationWatchpointRequest
75 
76 		fMirrors.add(getObjectReference()); // ObjectReference
77 		fMirrors.add(fVM.mirrorOf((short) 12345)); // ShortValue
78 		fMirrors.add(getFrame(RUN_FRAME_OFFSET)); // StackFrame
79 		fMirrors.add(getStringReference()); // StringReference
80 		fMirrors.add(getThread().threadGroup()); // ThreadGroupReference
81 		fMirrors.add(getThread()); // ThreadReference
82 		fMirrors.add(fVM); // VirtualMachine
83 	}
84 	/**
85 	 * Make sure the test leaves the VM in the same state it found it.
86 	 */
87 	@Override
localTearDown()88 	public void localTearDown() {
89 		ListIterator<Mirror> iterator = fMirrors.listIterator();
90 		while (iterator.hasNext()) {
91 			Object mirror = iterator.next();
92 
93 			// Delete the access watchpoint request we created in this test
94 			if (mirror instanceof AccessWatchpointRequest)
95 				fVM.eventRequestManager().deleteEventRequest(
96 					(AccessWatchpointRequest) mirror);
97 
98 			// Delete the breakpoint request we created in this test
99 			if (mirror instanceof BreakpointRequest)
100 				fVM.eventRequestManager().deleteEventRequest(
101 					(BreakpointRequest) mirror);
102 
103 			// Delete the modification watchpoint request we created in this test
104 			if (mirror instanceof ModificationWatchpointRequest)
105 				fVM.eventRequestManager().deleteEventRequest(
106 					(ModificationWatchpointRequest) mirror);
107 		}
108 	}
109 	/**
110 	 * Run all tests and output to standard output.
111 	 * @param args
112 	 */
main(java.lang.String[] args)113 	public static void main(java.lang.String[] args) {
114 		new MirrorTest().runSuite(args);
115 	}
116 	/**
117 	 * Gets the name of the test case.
118 	 * @see junit.framework.TestCase#getName()
119 	 */
120 	@Override
getName()121 	public String getName() {
122 		return "com.sun.jdi.Mirror";
123 	}
124 	/**
125 	 * Test JDI toString().
126 	 */
testJDIToString()127 	public void testJDIToString() {
128 		for (int i = 0; i < fMirrors.size(); i++) {
129 			Mirror mirror = fMirrors.get(i);
130 			assertNotNull(Integer.toString(i), mirror.toString());
131 		}
132 	}
133 	/**
134 	 * Test JDI virtualMachine().
135 	 */
testJDIVirtualMachine()136 	public void testJDIVirtualMachine() {
137 		for (int i = 0; i < fMirrors.size(); i++) {
138 			Mirror mirror = fMirrors.get(i);
139 			assertEquals(Integer.toString(i), fVM, mirror.virtualMachine());
140 		}
141 	}
142 }
143