Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9519e6069bdde253a8fe958054e34ad91d753f99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*******************************************************************************
 * Copyright (c) 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.debug.tests.variables;

import org.eclipse.debug.core.DebugEvent;
import org.eclipse.debug.core.model.IValue;
import org.eclipse.jdt.debug.core.IJavaDebugTarget;
import org.eclipse.jdt.debug.core.IJavaStackFrame;
import org.eclipse.jdt.debug.core.IJavaThread;
import org.eclipse.jdt.debug.eval.IEvaluationListener;
import org.eclipse.jdt.debug.eval.IEvaluationResult;
import org.eclipse.jdt.debug.tests.AbstractDebugTest;
import org.eclipse.jdt.internal.debug.eval.ast.engine.ASTEvaluationEngine;

/**
 * Tests launching / suspending and evaluating within anonymous 
 * types
 */
public class TestAnonymousInspect extends AbstractDebugTest {

	static final String TYPE_NAME = "InspectTests"; 
	static final String SNIPPET = "getchar()";
	
	class Listener implements IEvaluationListener {
		IEvaluationResult fResult;
		
		public void evaluationComplete(IEvaluationResult result) {
			fResult= result;
		}
		
		public IEvaluationResult getResult() {
			return fResult;
		}
	}
	Listener listener= new Listener();
	
	/**
	 * Constructor
	 */
	public TestAnonymousInspect() {
		super("TestAnonymousInspect");
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jdt.debug.tests.AbstractDebugTest#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
		createLaunchConfiguration(get14Project(), TYPE_NAME);
	}
	
	/**
	 * Perform the actual evaluation (inspect)
	 * @param thread
	 * @return the result of the evaluation
	 * @throws Exception
	 */
	IValue doEval(IJavaThread thread) throws Exception{
		IJavaStackFrame frame = (IJavaStackFrame) thread.getTopStackFrame();
		assertNotNull("There should be a stackframe", frame);
		ASTEvaluationEngine engine = new ASTEvaluationEngine(get14Project(), (IJavaDebugTarget) thread.getDebugTarget());
		try {
			engine.evaluate(SNIPPET, frame, listener, DebugEvent.EVALUATION_IMPLICIT, false);
			long timeout = System.currentTimeMillis()+5000;
			while(listener.fResult == null && System.currentTimeMillis() < timeout) {
				Thread.sleep(100);
			}
			assertFalse("The evaluation should not have errors", listener.fResult.hasErrors());
			return listener.fResult.getValue();
		}
		finally {
			engine.dispose();
		}
	}
	
	/**
	 * Tests that we can successfully inspect a method call from an anonymous type declaration that is assigned
	 * to a field
	 * 
	 * @throws Exception
	 */
	public void testInspectInAnonField() throws Exception {
		IJavaThread thread = null;
		try {
			createLineBreakpoint(26, TYPE_NAME);
			thread = launchToBreakpoint(TYPE_NAME);
			assertNotNull("The application should have suspended - we cannot have a null thread", thread);
			IValue value = doEval(thread);
			assertNotNull("The evaluation result cannot be null", value);
			assertEquals("The type must be char", "char", value.getReferenceTypeName());
			assertEquals("The value must be 'a'", "a", value.getValueString());
		}
		finally {
			terminateAndRemove(thread);
			removeAllBreakpoints();
			listener.fResult = null;
		}
	}
	
	/**
	 * Tests that we can successfully inspect a method call from an anonymous type declaration within a method
	 * declaration
	 * 
	 * @throws Exception
	 */
	public void testInspectInAnonMethod() throws Exception {
		IJavaThread thread = null;
		try {
			createLineBreakpoint(34, TYPE_NAME);
			thread = launchToBreakpoint(TYPE_NAME);
			assertNotNull("The application should have suspended - we cannot have a null thread", thread);
			IValue value = doEval(thread);
			assertNotNull("The evaluation result cannot be null", value);
			assertEquals("The type must be char", "char", value.getReferenceTypeName());
			assertEquals("The value must be 'b'", "b", value.getValueString());
		}
		finally {
			terminateAndRemove(thread);
			removeAllBreakpoints();
			listener.fResult = null;
		}
	}
	
	/**
	 * Tests that we can successfully inspect a method call from an anonymous type declaration within a static
	 * initializer 
	 * 
	 * @throws Exception
	 */
	public void testInspectInAnonInitializer() throws Exception {
		IJavaThread thread = null;
		try {
			createLineBreakpoint(17, TYPE_NAME);
			thread = launchToBreakpoint(TYPE_NAME);
			assertNotNull("The application should have suspended - we cannot have a null thread", thread);
			IValue value = doEval(thread);
			assertNotNull("The evaluation result cannot be null", value);
			assertEquals("The type must be char", "char", value.getReferenceTypeName());
			assertEquals("The value must be 's'", "s", value.getValueString());
		}
		finally {
			terminateAndRemove(thread);
			removeAllBreakpoints();
			listener.fResult = null;
		}
	}
}

Back to the top