Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0bc25f8211329f1f4556163a67ae24abd9e2ab17 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*******************************************************************************
 * Copyright (c) 2000, 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.sourcelookup;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.debug.core.model.ISourceLocator;
import org.eclipse.debug.core.model.IStackFrame;
import org.eclipse.debug.internal.core.IInternalDebugCoreConstants;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.debug.core.IJavaThread;
import org.eclipse.jdt.debug.tests.AbstractDebugTest;
import org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation;
import org.eclipse.jdt.launching.sourcelookup.PackageFragmentRootSourceLocation;

/**
 * Tests source lookup in source folders
 */
@SuppressWarnings("deprecation")
public class SourceLookupTests extends AbstractDebugTest {
	
	public SourceLookupTests(String name) {
		super(name);
	}
	
	/**
	 * See Bug 53646
	 */
	public void testPackageFragmentRoots() throws Exception {
		IJavaProject javaProject = get14Project();
		IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
		IResource resource = javaProject.getProject().getFile("src/A.jar");
		IPackageFragmentRoot packageFragmentRoot = javaProject.getPackageFragmentRoot(resource);
		assertTrue("package fragment root should exist", packageFragmentRoot.exists());
		for (int i = 0; i < roots.length; i++) {
			IPackageFragmentRoot root = roots[i];
			if (packageFragmentRoot.equals(root)) {
				return;
			}
		}
		assertTrue("Failed to locate package fragment root", false);
	}

	/**
	 * see Bug 37545
	 */
	public void testStackFrameReuse() throws Exception {
		String typeName = "org.eclipse.debug.tests.targets.CallStack";
		createLineBreakpoint(28, "org.eclipse.debug.tests.targets.ClassOne");
		createLineBreakpoint(28, "org.eclipse.debug.tests.targets.ClassTwo");
		
		IJavaThread thread= null;
		try {
			thread= launchToBreakpoint(typeName);
			assertNotNull("Breakpoint not hit within timeout period", thread);
			
			// get source element for first breakpoint
			IStackFrame[] frames = thread.getStackFrames();
			IStackFrame frame = frames[2]; 
			ISourceLocator sourceLocator = thread.getLaunch().getSourceLocator();
			Object source1 = sourceLocator.getSourceElement(frame);
			source1 = ((IAdaptable)source1).getAdapter(IJavaElement.class);
			
			IPackageFragment[] fragments = get14Project().getPackageFragments();
			IPackageFragment fragment = null;
			for (int i = 0; i < fragments.length; i++) {
				if (fragments[i].getElementName().equals("org.eclipse.debug.tests.targets")) {
					fragment = fragments[i];
					break;
				}
			}
			assertNotNull("Did not locate package framgment 'org.eclipse.debug.tests.targets'", fragment);
			ICompilationUnit unit1 = fragment.getCompilationUnit("ClassOne.java");
			assertEquals("Source lookup failed for frame1", unit1, source1);
			
			// resume to second breakpoint
			thread = resume(thread);
			assertNotNull("Breakpoint not hit within timeout period", thread);
			IStackFrame frame2 = thread.getStackFrames()[2];
			Object source2 = sourceLocator.getSourceElement(frame2);
			source2 = ((IAdaptable)source2).getAdapter(IJavaElement.class);
			ICompilationUnit unit2 = fragment.getCompilationUnit("ClassTwo.java");
			assertEquals("Source lookup failed for frame2", unit2, source2);
						
			// the source elements should not be equal
		} finally {
			terminateAndRemove(thread);
			removeAllBreakpoints();
		}		
	}
	
	/**
	 * Tests source lookup in a top level type, in the default package.
	 */
	public void testDefTopLevelType() throws Exception {
		IPackageFragmentRoot root = getPackageFragmentRoot(get14Project(), "src");
		IJavaSourceLocation location = new PackageFragmentRootSourceLocation(root);
		
		ICompilationUnit expectedSource = getCompilationUnit(get14Project(), "src", IInternalDebugCoreConstants.EMPTY_STRING, "Breakpoints.java");
		assertTrue("did not find compilation unit for Breakpoints.java", expectedSource.exists());
				
		assertEquals("Source lookup failed", expectedSource, location.findSourceElement("Breakpoints"));
	}
	
	/**
	 * Tests source lookup in an inner type, the default package.
	 */
	public void testDefInnerType() throws Exception {
		IPackageFragmentRoot root = getPackageFragmentRoot(get14Project(), "src");
		IJavaSourceLocation location = new PackageFragmentRootSourceLocation(root);
		
		ICompilationUnit expectedSource = getCompilationUnit(get14Project(), "src", IInternalDebugCoreConstants.EMPTY_STRING, "Breakpoints.java");
		assertTrue("did not find compilation unit for Breakpoints.java", expectedSource.exists());
				
		assertEquals("Source lookup failed", expectedSource, location.findSourceElement("Breakpoints$InnerRunnable"));
	}
	
	/**
	 * Tests source lookup in an anonymous inner type, in the default package.
	 * Must debug since we do not know name of type.
	 */
	public void testDefAnonInnerType() throws Exception {
		String typeName = "Breakpoints";
		createLineBreakpoint(43, typeName);
		ICompilationUnit expectedSource = getCompilationUnit(get14Project(), "src", IInternalDebugCoreConstants.EMPTY_STRING, "Breakpoints.java");
		assertTrue("did not find compilation unit for Breakpoints.java", expectedSource.exists());
		
		IJavaThread thread= null;
		try {
			thread= launchToBreakpoint(typeName);
			assertNotNull("Breakpoint not hit within timeout period", thread);
			
			// get source element for top frame
			IStackFrame frame = thread.getTopStackFrame();
			ISourceLocator sourceLocator = thread.getLaunch().getSourceLocator();
			Object source = sourceLocator.getSourceElement(frame);
			assertEquals("Source lookup failed", expectedSource.getResource(), source);
		} finally {
			terminateAndRemove(thread);
			removeAllBreakpoints();			
		}
	}	
	
	/**
	 * Tests source lookup in a named local type, in the default package.
	 * Must debug to test this, since we do no know name of local type.
	 */
	public void testDefLocalType() throws Exception {
		String typeName = "Breakpoints";
		createLineBreakpoint(30, typeName);
		ICompilationUnit expectedSource = getCompilationUnit(get14Project(), "src", IInternalDebugCoreConstants.EMPTY_STRING, "Breakpoints.java");
		assertTrue("did not find compilation unit for Breakpoints.java", expectedSource.exists());
		
		IJavaThread thread= null;
		try {
			thread= launchToBreakpoint(typeName);
			assertNotNull("Breakpoint not hit within timeout period", thread);
			
			// get source element for top frame
			IStackFrame frame = thread.getTopStackFrame();
			ISourceLocator sourceLocator = thread.getLaunch().getSourceLocator();
			Object source = sourceLocator.getSourceElement(frame);
			assertEquals("Source lookup failed", expectedSource.getResource(), source);
		} finally {
			terminateAndRemove(thread);
			removeAllBreakpoints();			
		}
	}	
	
	/**
	 * Tests source lookup in a top level type.
	 */
	public void testTopLevelType() throws Exception {
		IPackageFragmentRoot root = getPackageFragmentRoot(get14Project(), "src");
		IJavaSourceLocation location = new PackageFragmentRootSourceLocation(root);
		
		ICompilationUnit expectedSource = getCompilationUnit(get14Project(), "src", "org.eclipse.debug.tests.targets", "SourceLookup.java");
		assertTrue("did not find compilation unit for SourceLookup.java", expectedSource.exists());
				
		assertEquals("Source lookup failed", expectedSource, location.findSourceElement("org.eclipse.debug.tests.targets.SourceLookup"));
	}
	
	/**
	 * Tests source lookup in an inner type.
	 */
	public void testInnerType() throws Exception {
		IPackageFragmentRoot root = getPackageFragmentRoot(get14Project(), "src");
		IJavaSourceLocation location = new PackageFragmentRootSourceLocation(root);
		
		ICompilationUnit expectedSource = getCompilationUnit(get14Project(), "src", "org.eclipse.debug.tests.targets", "SourceLookup.java");
		assertTrue("did not find compilation unit for SourceLookup.java", expectedSource.exists());
				
		assertEquals("Source lookup failed", expectedSource, location.findSourceElement("org.eclipse.debug.tests.targets.SourceLookup$Inner"));
	}
	
	/**
	 * Tests source lookup in an inner inner type.
	 */
	public void testInnerNestedType() throws Exception {
		IPackageFragmentRoot root = getPackageFragmentRoot(get14Project(), "src");
		IJavaSourceLocation location = new PackageFragmentRootSourceLocation(root);
		
		ICompilationUnit expectedSource = getCompilationUnit(get14Project(), "src", "org.eclipse.debug.tests.targets", "SourceLookup.java");
		assertTrue("did not find compilation unit for SourceLookup.java", expectedSource.exists());
				
		assertEquals("Source lookup failed", expectedSource, location.findSourceElement("org.eclipse.debug.tests.targets.SourceLookup$Inner$Nested"));
	}	
	
	/**
	 * Tests source lookup in a top level type, with a $ named class
	 */
	public void testTopLevel$Type() throws Exception {
		IPackageFragmentRoot root = getPackageFragmentRoot(get14Project(), "src");
		IJavaSourceLocation location = new PackageFragmentRootSourceLocation(root);
		
		ICompilationUnit expectedSource = getCompilationUnit(get14Project(), "src", "org.eclipse.debug.tests.targets", "Source_$_Lookup.java");
		assertTrue("did not find compilation unit for Source_$_Lookup.java", expectedSource.exists());
				
		assertEquals("Source lookup failed", expectedSource, location.findSourceElement("org.eclipse.debug.tests.targets.Source_$_Lookup"));
	}
	
	/**
	 * Tests source lookup in an inner type in a $ named class.
	 */
	public void testInner$Type() throws Exception {
		IPackageFragmentRoot root = getPackageFragmentRoot(get14Project(), "src");
		IJavaSourceLocation location = new PackageFragmentRootSourceLocation(root);
		
		ICompilationUnit expectedSource = getCompilationUnit(get14Project(), "src", "org.eclipse.debug.tests.targets", "Source_$_Lookup.java");
		assertTrue("did not find compilation unit for Source_$_Lookup.java", expectedSource.exists());
				
		assertEquals("Source lookup failed", expectedSource, location.findSourceElement("org.eclipse.debug.tests.targets.Source_$_Lookup$Inner"));
	}
	
	/**
	 * Tests source lookup in an inner inner type in a $ named class.
	 */
	public void testInnerNested$Type() throws Exception {
		IPackageFragmentRoot root = getPackageFragmentRoot(get14Project(), "src");
		IJavaSourceLocation location = new PackageFragmentRootSourceLocation(root);
		
		ICompilationUnit expectedSource = getCompilationUnit(get14Project(), "src", "org.eclipse.debug.tests.targets", "Source_$_Lookup.java");
		assertTrue("did not find compilation unit for Source_$_Lookup.java", expectedSource.exists());
				
		assertEquals("Source lookup failed", expectedSource, location.findSourceElement("org.eclipse.debug.tests.targets.Source_$_Lookup$Inner$Nested"));
	}	
}

Back to the top