Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2008 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.pde.ui.tests.target; |
12 |
|
13 |
import org.eclipse.core.runtime.CoreException; |
14 |
|
15 |
import org.eclipse.pde.internal.core.target.provisional.ITargetHandle; |
16 |
|
17 |
import org.eclipse.core.resources.IFile; |
18 |
|
19 |
import org.eclipse.core.resources.ResourcesPlugin; |
20 |
|
21 |
import org.eclipse.pde.internal.core.target.provisional.ITargetPlatformService; |
22 |
|
23 |
import java.io.*; |
24 |
import java.net.URL; |
25 |
import java.util.*; |
26 |
import java.util.zip.ZipEntry; |
27 |
import java.util.zip.ZipFile; |
28 |
import junit.framework.TestCase; |
29 |
import org.eclipse.core.runtime.*; |
30 |
import org.eclipse.equinox.internal.provisional.frameworkadmin.BundleInfo; |
31 |
import org.eclipse.pde.core.plugin.TargetPlatform; |
32 |
import org.eclipse.pde.internal.core.*; |
33 |
import org.eclipse.pde.internal.core.target.provisional.*; |
34 |
import org.eclipse.pde.internal.ui.tests.macro.MacroPlugin; |
35 |
import org.osgi.framework.ServiceReference; |
36 |
|
37 |
/** |
38 |
* Tests for target definitions. |
39 |
* |
40 |
* @since 3.5 |
41 |
*/ |
42 |
public class TargetDefinitionTests extends TestCase { |
43 |
|
44 |
/** |
45 |
* Retrieves all bundles (source and code) in the given target definition |
46 |
* returning them as a set of URLs. |
47 |
* |
48 |
* @param target target definition |
49 |
* @return all bundle URLs |
50 |
*/ |
51 |
protected Set getAllBundleURLs(ITargetDefinition target) throws Exception { |
52 |
BundleInfo[] code = target.resolveBundles(null); |
53 |
BundleInfo[] source = target.resolveSourceBundles(null); |
54 |
Set urls = new HashSet(code.length + source.length); |
55 |
for (int i = 0; i < code.length; i++) { |
56 |
urls.add(code[i].getLocation().toURL()); |
57 |
} |
58 |
for (int i = 0; i < source.length; i++) { |
59 |
urls.add(source[i].getLocation().toURL()); |
60 |
} |
61 |
return urls; |
62 |
} |
63 |
|
64 |
/** |
65 |
* Extracts the classic plug-ins archive, if not already done, and returns a path to the |
66 |
* root directory containing the plug-ins. |
67 |
* |
68 |
* @return path to the plug-ins directory |
69 |
* @throws Exception |
70 |
*/ |
71 |
protected IPath extracClassicPlugins() throws Exception { |
72 |
// extract the 3.0.2 skeleton |
73 |
IPath stateLocation = MacroPlugin.getDefault().getStateLocation(); |
74 |
IPath location = stateLocation.append("classic-plugins"); |
75 |
if (location.toFile().exists()) { |
76 |
return location; |
77 |
} |
78 |
URL zipURL = MacroPlugin.getBundleContext().getBundle().getEntry("/tests/targets/classic-plugins.zip"); |
79 |
Path zipPath = new Path(new File(FileLocator.toFileURL(zipURL).getFile()).getAbsolutePath()); |
80 |
ZipFile zipFile = new ZipFile(zipPath.toFile()); |
81 |
Enumeration entries = zipFile.entries(); |
82 |
while (entries.hasMoreElements()) { |
83 |
ZipEntry entry = (ZipEntry) entries.nextElement(); |
84 |
if (!entry.isDirectory()) { |
85 |
IPath entryPath = stateLocation.append(entry.getName()); |
86 |
File dir = entryPath.removeLastSegments(1).toFile(); |
87 |
dir.mkdirs(); |
88 |
File file = entryPath.toFile(); |
89 |
file.createNewFile(); |
90 |
InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry)); |
91 |
byte[] bytes = getInputStreamAsByteArray(inputStream, -1); |
92 |
inputStream.close(); |
93 |
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file)); |
94 |
outputStream.write(bytes); |
95 |
outputStream.close(); |
96 |
} |
97 |
} |
98 |
zipFile.close(); |
99 |
return location; |
100 |
} |
101 |
|
102 |
/** |
103 |
* Returns the target platform service or <code>null</code> if none |
104 |
* |
105 |
* @return target platform service |
106 |
*/ |
107 |
protected ITargetPlatformService getTargetService() { |
108 |
ServiceReference reference = MacroPlugin.getBundleContext().getServiceReference(ITargetPlatformService.class.getName()); |
109 |
assertNotNull("Missing target platform service", reference); |
110 |
if (reference == null) |
111 |
return null; |
112 |
return (ITargetPlatformService) MacroPlugin.getBundleContext().getService(reference); |
113 |
} |
114 |
|
115 |
/** |
116 |
* Tests that a target definition equivalent to the default target platform |
117 |
* contains the same bundles as the default target platform (this is an |
118 |
* explicit location with no target weaving). |
119 |
* |
120 |
* @throws Exception |
121 |
*/ |
122 |
public void testDefaultTargetPlatform() throws Exception { |
123 |
// the new way |
124 |
ITargetDefinition definition = getTargetService().newTarget(); |
125 |
IBundleContainer container = getTargetService().newProfileContainer(TargetPlatform.getDefaultLocation()); |
126 |
definition.setBundleContainers(new IBundleContainer[]{container}); |
127 |
Set urls = getAllBundleURLs(definition); |
128 |
|
129 |
// the old way |
130 |
IPath location = new Path(TargetPlatform.getDefaultLocation()); |
131 |
URL[] pluginPaths = P2Utils.readBundlesTxt(location.toOSString(), location.append("configuration").toFile().toURL()); |
132 |
assertEquals("Should have same number of bundles", pluginPaths.length, urls.size()); |
133 |
for (int i = 0; i < pluginPaths.length; i++) { |
134 |
URL url = pluginPaths[i]; |
135 |
assertTrue("Missing plug-in " + url.toString(), urls.contains(url)); |
136 |
} |
137 |
|
138 |
} |
139 |
|
140 |
/** |
141 |
* Tests that a target definition equivalent to the default target platform |
142 |
* contains the same bundles as the default target platform using the |
143 |
* platform's configuration location (which will do target weaving). This |
144 |
* is really only tested when run as a JUnit plug-in test suite from |
145 |
* within Eclipse. |
146 |
* |
147 |
* @throws Exception |
148 |
*/ |
149 |
public void testWovenTargetPlatform() throws Exception { |
150 |
// the new way |
151 |
ITargetDefinition definition = getTargetService().newTarget(); |
152 |
IBundleContainer container = getTargetService().newProfileContainer(TargetPlatform.getDefaultLocation(), |
153 |
new File(Platform.getConfigurationLocation().getURL().getFile()).getAbsolutePath()); |
154 |
definition.setBundleContainers(new IBundleContainer[]{container}); |
155 |
Set urls = getAllBundleURLs(definition); |
156 |
|
157 |
// the old way |
158 |
URL[] pluginPaths = PluginPathFinder.getPluginPaths(TargetPlatform.getDefaultLocation()); |
159 |
assertEquals("Should have same number of bundles", pluginPaths.length, urls.size()); |
160 |
for (int i = 0; i < pluginPaths.length; i++) { |
161 |
URL url = pluginPaths[i]; |
162 |
assertTrue("Missing plug-in " + url.toString(), urls.contains(url)); |
163 |
} |
164 |
|
165 |
} |
166 |
|
167 |
/** |
168 |
* Tests that a bundle directory container is equivalent to scanning locations. |
169 |
* |
170 |
* @throws Exception |
171 |
*/ |
172 |
public void testDirectoryBundleContainer() throws Exception { |
173 |
// the new way |
174 |
ITargetDefinition definition = getTargetService().newTarget(); |
175 |
IBundleContainer container = getTargetService().newDirectoryContainer(TargetPlatform.getDefaultLocation() + "/plugins"); |
176 |
definition.setBundleContainers(new IBundleContainer[]{container}); |
177 |
Set urls = getAllBundleURLs(definition); |
178 |
|
179 |
Preferences store = PDECore.getDefault().getPluginPreferences(); |
180 |
boolean restore = store.getBoolean(ICoreConstants.TARGET_PLATFORM_REALIZATION); |
181 |
try { |
182 |
store.setValue(ICoreConstants.TARGET_PLATFORM_REALIZATION, false); |
183 |
// the old way |
184 |
URL[] pluginPaths = PluginPathFinder.getPluginPaths(TargetPlatform.getDefaultLocation()); |
185 |
assertEquals("Should have same number of bundles", pluginPaths.length, urls.size()); |
186 |
for (int i = 0; i < pluginPaths.length; i++) { |
187 |
URL url = pluginPaths[i]; |
188 |
assertTrue("Missing plug-in " + url.toString(), urls.contains(url)); |
189 |
} |
190 |
} |
191 |
finally { |
192 |
store.setValue(ICoreConstants.TARGET_PLATFORM_REALIZATION, restore); |
193 |
} |
194 |
} |
195 |
|
196 |
/** |
197 |
* Tests reading a 3.0.2 install with a mix of classic and OSGi plug-ins. |
198 |
* |
199 |
* @throws Exception |
200 |
*/ |
201 |
public void testClassicPlugins() throws Exception { |
202 |
// extract the 3.0.2 skeleton |
203 |
IPath location = extracClassicPlugins(); |
204 |
|
205 |
// the new way |
206 |
ITargetDefinition definition = getTargetService().newTarget(); |
207 |
IBundleContainer container = getTargetService().newDirectoryContainer(location.toOSString()); |
208 |
definition.setBundleContainers(new IBundleContainer[]{container}); |
209 |
Set urls = getAllBundleURLs(definition); |
210 |
|
211 |
Preferences store = PDECore.getDefault().getPluginPreferences(); |
212 |
boolean restore = store.getBoolean(ICoreConstants.TARGET_PLATFORM_REALIZATION); |
213 |
try { |
214 |
store.setValue(ICoreConstants.TARGET_PLATFORM_REALIZATION, false); |
215 |
// the old way |
216 |
URL[] pluginPaths = PluginPathFinder.getPluginPaths(location.toOSString()); |
217 |
for (int i = 0; i < pluginPaths.length; i++) { |
218 |
URL url = pluginPaths[i]; |
219 |
if (!urls.contains(url)) { |
220 |
System.err.println(url.toString()); |
221 |
} |
222 |
} |
223 |
assertEquals("Wrong number of bundles", pluginPaths.length, urls.size()); |
224 |
} |
225 |
finally { |
226 |
store.setValue(ICoreConstants.TARGET_PLATFORM_REALIZATION, restore); |
227 |
} |
228 |
} |
229 |
|
230 |
/** |
231 |
* Tests identification of source bundles in a 3.0.2 install. |
232 |
* |
233 |
* @throws Exception |
234 |
*/ |
235 |
public void testClassicSourcePlugins() throws Exception { |
236 |
// extract the 3.0.2 skeleton |
237 |
IPath location = extracClassicPlugins(); |
238 |
|
239 |
// the new way |
240 |
ITargetDefinition definition = getTargetService().newTarget(); |
241 |
IBundleContainer container = getTargetService().newDirectoryContainer(location.toOSString()); |
242 |
definition.setBundleContainers(new IBundleContainer[]{container}); |
243 |
BundleInfo[] bundles = definition.resolveSourceBundles(null); |
244 |
assertEquals("Wrong number of source bundles", 3, bundles.length); |
245 |
Set names = new HashSet(); |
246 |
for (int i = 0; i < bundles.length; i++) { |
247 |
names.add(bundles[i].getSymbolicName()); |
248 |
} |
249 |
String[] expected = new String[]{"org.eclipse.platform.source", "org.eclipse.jdt.source", "org.eclipse.pde.source"}; |
250 |
for (int i = 0; i < expected.length; i++) { |
251 |
assertTrue("Missing source for " + expected[i], names.contains(expected[i])); |
252 |
} |
253 |
} |
254 |
|
255 |
/** |
256 |
* Returns the given input stream as a byte array |
257 |
* @param stream the stream to get as a byte array |
258 |
* @param length the length to read from the stream or -1 for unknown |
259 |
* @return the given input stream as a byte array |
260 |
* @throws IOException |
261 |
*/ |
262 |
public static byte[] getInputStreamAsByteArray(InputStream stream, int length) throws IOException { |
263 |
byte[] contents; |
264 |
if (length == -1) { |
265 |
contents = new byte[0]; |
266 |
int contentsLength = 0; |
267 |
int amountRead = -1; |
268 |
do { |
269 |
// read at least 8K |
270 |
int amountRequested = Math.max(stream.available(), 8192); |
271 |
// resize contents if needed |
272 |
if (contentsLength + amountRequested > contents.length) { |
273 |
System.arraycopy(contents, |
274 |
0, |
275 |
contents = new byte[contentsLength + amountRequested], |
276 |
0, |
277 |
contentsLength); |
278 |
} |
279 |
// read as many bytes as possible |
280 |
amountRead = stream.read(contents, contentsLength, amountRequested); |
281 |
if (amountRead > 0) { |
282 |
// remember length of contents |
283 |
contentsLength += amountRead; |
284 |
} |
285 |
} while (amountRead != -1); |
286 |
// resize contents if necessary |
287 |
if (contentsLength < contents.length) { |
288 |
System.arraycopy(contents, 0, contents = new byte[contentsLength], 0, contentsLength); |
289 |
} |
290 |
} else { |
291 |
contents = new byte[length]; |
292 |
int len = 0; |
293 |
int readSize = 0; |
294 |
while ((readSize != -1) && (len != length)) { |
295 |
// See PR 1FMS89U |
296 |
// We record first the read size. In this case length is the actual |
297 |
// read size. |
298 |
len += readSize; |
299 |
readSize = stream.read(contents, len, length - len); |
300 |
} |
301 |
} |
302 |
return contents; |
303 |
} |
304 |
|
305 |
/** |
306 |
* Tests restoration of a handle to target definition in an IFile |
307 |
* @throws CoreException |
308 |
*/ |
309 |
public void testFileTargetHandleMemento() throws CoreException { |
310 |
ITargetPlatformService service = getTargetService(); |
311 |
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path("does/not/exist")); |
312 |
ITargetHandle handle = service.getTarget(file); |
313 |
assertFalse("Target should not exist", handle.exists()); |
314 |
String memento = handle.getMemento(); |
315 |
assertNotNull("Missing memento", memento); |
316 |
ITargetHandle handle2 = service.getTargetHandle(memento); |
317 |
assertEquals("Restore failed", handle, handle2); |
318 |
IFile file2 = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path("does/not/exist/either")); |
319 |
ITargetHandle handle3 = service.getTarget(file2); |
320 |
assertFalse("Should be different targets", handle.equals(handle3)); |
321 |
} |
322 |
|
323 |
/** |
324 |
* Tests restoration of a handle to target definition in local metadata |
325 |
* |
326 |
* @throws CoreException |
327 |
* @throws InterruptedException |
328 |
*/ |
329 |
public void testLocalTargetHandleMemento() throws CoreException, InterruptedException { |
330 |
ITargetPlatformService service = getTargetService(); |
331 |
ITargetHandle handle = service.newTarget().getHandle(); |
332 |
assertFalse("Target should not exist", handle.exists()); |
333 |
String memento = handle.getMemento(); |
334 |
assertNotNull("Missing memento", memento); |
335 |
ITargetHandle handle2 = service.getTargetHandle(memento); |
336 |
assertEquals("Restore failed", handle, handle2); |
337 |
ITargetHandle handle3 = service.newTarget().getHandle(); |
338 |
assertFalse("Should be different targets", handle.equals(handle3)); |
339 |
} |
340 |
} |