Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a819dd86f91267a50ace18696ef064bfbf05b81e (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
/*******************************************************************************
 *  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 java.io.File;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.debug.tests.AbstractDebugTest;
import org.eclipse.jdt.debug.ui.JavaUISourceLocator;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.sourcelookup.ArchiveSourceLocation;
import org.eclipse.jdt.launching.sourcelookup.DirectorySourceLocation;
import org.eclipse.jdt.launching.sourcelookup.IJavaSourceLocation;
import org.eclipse.jdt.launching.sourcelookup.JavaProjectSourceLocation;
import org.eclipse.jdt.launching.sourcelookup.JavaSourceLocator;
import org.eclipse.jdt.launching.sourcelookup.PackageFragmentRootSourceLocation;

/**
 * Tests source location creation/restoration.
 */
public class SourceLocationTests extends AbstractDebugTest {
	
	public static final String JRE_CONTAINER_1_4_CPE_NAME = "org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.4";

	public SourceLocationTests(String name) {
		super(name);
	}

	public void testProjectLocationMemento() throws Exception {
		IJavaSourceLocation location = new JavaProjectSourceLocation(get14Project());
		String memento = location.getMemento();
		IJavaSourceLocation restored = new JavaProjectSourceLocation();
		restored.initializeFrom(memento);
		assertEquals("project locations should be equal", location, restored);
	}
	
	public void testDirectoryLocationMemento() throws Exception {
		File dir = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile();
		IJavaSourceLocation location = new DirectorySourceLocation(dir);
		String memento = location.getMemento();
		IJavaSourceLocation restored = new DirectorySourceLocation();
		restored.initializeFrom(memento);
		assertEquals("directory locations should be equal", location, restored);
	}	
	
	public void testArchiveLocationMemento() throws Exception {
		IVMInstall vm = JavaRuntime.getDefaultVMInstall();
		IJavaSourceLocation location = new ArchiveSourceLocation(JavaRuntime.getLibraryLocations(vm)[0].getSystemLibraryPath().toOSString(), null);
		String memento = location.getMemento();
		IJavaSourceLocation restored = new ArchiveSourceLocation();
		restored.initializeFrom(memento);
		assertEquals("archive locations should be equal", location, restored);
	}	
	
	public void testJavaSourceLocatorMemento() throws Exception {
		IJavaSourceLocation location1 = new JavaProjectSourceLocation(get14Project());
		File dir = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile();
		IJavaSourceLocation location2 = new DirectorySourceLocation(dir);
		IVMInstall vm = JavaRuntime.getDefaultVMInstall();
		IJavaSourceLocation location3 = new ArchiveSourceLocation(JavaRuntime.getLibraryLocations(vm)[0].getSystemLibraryPath().toOSString(), null);
		
		JavaSourceLocator locator = new JavaSourceLocator(new IJavaSourceLocation[] {location1, location2, location3});
		String memento = locator.getMemento();
		JavaSourceLocator restored = new JavaSourceLocator();
		restored.initializeFromMemento(memento);
		IJavaSourceLocation[] locations = restored.getSourceLocations();
		
		assertEquals("wrong number of source locations", 3, locations.length);
		assertEquals("1st locations not equal", location1, locations[0]);
		assertEquals("2nd locations not equal", location2, locations[1]);
		assertEquals("3rd locations not equal", location3, locations[2]);
	}
	
	public void testJavaUISourceLocatorMemento() throws Exception {
		IJavaSourceLocation location1 = new JavaProjectSourceLocation(get14Project());
		File dir = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile();
		IJavaSourceLocation location2 = new DirectorySourceLocation(dir);
		IVMInstall vm = JavaRuntime.getDefaultVMInstall();
		IJavaSourceLocation location3 = new ArchiveSourceLocation(JavaRuntime.getLibraryLocations(vm)[0].getSystemLibraryPath().toOSString(), null);
		
		JavaUISourceLocator locator = new JavaUISourceLocator(get14Project());
		locator.setSourceLocations(new IJavaSourceLocation[] {location1, location2, location3});
		locator.setFindAllSourceElement(true);
		
		String memento = locator.getMemento();
		JavaUISourceLocator restored = new JavaUISourceLocator();
		restored.initializeFromMemento(memento);
		IJavaSourceLocation[] locations = restored.getSourceLocations();
		
		assertEquals("wrong number of source locations", 3, locations.length);
		assertEquals("1st locations not equal", location1, locations[0]);
		assertEquals("2nd locations not equal", location2, locations[1]);
		assertEquals("3rd locations not equal", location3, locations[2]);
		assertTrue("Should find all source locations", locator.isFindAllSourceElements());		
	}
	
	public void testPackageFragmentRootLocationMemento() throws Exception {
		IResource res = get14Project().getProject().getFolder("src");
		IPackageFragmentRoot root = get14Project().getPackageFragmentRoot(res);
		IJavaSourceLocation location = new PackageFragmentRootSourceLocation(root);
		String memento = location.getMemento();
		IJavaSourceLocation restored = new PackageFragmentRootSourceLocation();
		restored.initializeFrom(memento);
		assertEquals("root locations should be equal", location, restored);
	}
	
	public void testEmptyPackageFragmentRootLocationMemento() throws Exception {
		IJavaSourceLocation location = new PackageFragmentRootSourceLocation();
		String memento = location.getMemento();
		IJavaSourceLocation restored = new PackageFragmentRootSourceLocation();
		restored.initializeFrom(memento);
		assertEquals("root locations should be equal", location, restored);
	}	
		
	public void testPositiveSourceFolderSourceLocation() throws Exception {
		IResource res = get14Project().getProject().getFolder("src");
		IPackageFragmentRoot root = get14Project().getPackageFragmentRoot(res);
		IJavaSourceLocation location = new PackageFragmentRootSourceLocation(root);
		
		Object source = location.findSourceElement("Breakpoints");
		assertTrue("Did not find source for 'Breakpoints'", source instanceof ICompilationUnit);
		ICompilationUnit cu = (ICompilationUnit)source;
		assertEquals("Did not find source for 'Breakpoints'", cu.getElementName(), "Breakpoints.java");
		
		source = location.findSourceElement("org.eclipse.debug.tests.targets.InfiniteLoop");
		assertTrue("Did not find source for 'InfiniteLoop'", source instanceof ICompilationUnit);
		cu = (ICompilationUnit)source;
		assertEquals("Did not find source for 'Breakpoints'", cu.getElementName(), "InfiniteLoop.java");
	}
	
	public void testNegativeSourceFolderSourceLocation() throws Exception {
		IResource res = get14Project().getProject().getFolder("src");
		IPackageFragmentRoot root = get14Project().getPackageFragmentRoot(res);
		IJavaSourceLocation location = new PackageFragmentRootSourceLocation(root);
		
		Object source = location.findSourceElement("DoesNotExist");
		assertNull("Should not have found source", source);
		
		source = location.findSourceElement("org.eclipse.DoesNotExist");
		assertNull("Should not have found source", source);
	}	
	
	public void testPositiveSystemLibrarySourceLocation() throws Exception {
		IClasspathEntry[] cpes = get14Project().getRawClasspath();
		IClasspathEntry lib = null;
		for (int i = 0; i < cpes.length; i++) {
			if (cpes[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
				if (cpes[i].getPath().equals(new Path(JRE_CONTAINER_1_4_CPE_NAME))) {
					lib = cpes[i];
					break;
				}
			}
		}
		assertNotNull("Could not find JRE_CONTAINER entry", lib);
		
		IPackageFragmentRoot[] roots = get14Project().findPackageFragmentRoots(lib);
		Object source = null;
		for (int i = 0; i < roots.length; i++) {
			IPackageFragmentRoot root = roots[i];
			IJavaSourceLocation location = new PackageFragmentRootSourceLocation(root);
			source = location.findSourceElement("java.lang.Object");
			if (source != null) {
				break;
			}
		}
		
		assertTrue("Did not find source for 'Object'", source instanceof IClassFile);
		IClassFile cf = (IClassFile)source;
		assertEquals("Did not find source for 'Object'", "Object.class", cf.getElementName());
		
		for (int i = 0; i < roots.length; i++) {
			IPackageFragmentRoot root = roots[i];
			IJavaSourceLocation location = new PackageFragmentRootSourceLocation(root);
			source = location.findSourceElement("java.util.Vector$1");
			if (source != null) {
				break;
			}
		}		
		assertTrue("Did not find source for 'Vector$1'", source instanceof IClassFile);
		cf = (IClassFile)source;
		assertEquals("Did not find source for 'Vector$1'", "Vector$1.class", cf.getElementName());
	}
	
	public void testNegativeSystemLibrarySourceLocation() throws Exception {
		IClasspathEntry[] cpes = get14Project().getRawClasspath();
		IClasspathEntry lib = null;
		for (int i = 0; i < cpes.length; i++) {
			if (cpes[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
				if (cpes[i].getPath().equals(new Path(JRE_CONTAINER_1_4_CPE_NAME))) {
					lib = cpes[i];
					break;
				}
			}
		}
		assertNotNull("Could not find JRE_CONTAINER entry", lib);
		
		IPackageFragmentRoot[] roots = get14Project().findPackageFragmentRoots(lib);
		Object source = null;
		for (int i = 0; i < roots.length; i++) {
			IPackageFragmentRoot root = roots[i];
			IJavaSourceLocation location = new PackageFragmentRootSourceLocation(root);
			source = location.findSourceElement("xyz.abc.Object");
			if (source != null) {
				break;
			}
		}
		
		assertNull("Should not find source", source);

	}	
}

Back to the top