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 java.io.*; |
14 |
import java.net.URL; |
15 |
import java.util.*; |
16 |
import java.util.zip.ZipEntry; |
17 |
import java.util.zip.ZipFile; |
18 |
import junit.framework.TestCase; |
19 |
import org.eclipse.core.runtime.*; |
20 |
import org.eclipse.equinox.internal.provisional.frameworkadmin.BundleInfo; |
21 |
import org.eclipse.pde.core.plugin.TargetPlatform; |
22 |
import org.eclipse.pde.internal.core.*; |
23 |
import org.eclipse.pde.internal.core.target.provisional.*; |
24 |
import org.eclipse.pde.internal.ui.tests.macro.MacroPlugin; |
25 |
|
26 |
/** |
27 |
* Tests for target definitions. |
28 |
* |
29 |
* @since 3.5 |
30 |
*/ |
31 |
public class TargetDefinitionTests extends TestCase { |
32 |
|
33 |
/** |
34 |
* Retrieves all bundles (source and code) in the given target definition |
35 |
* returning them as a set of URLs. |
36 |
* |
37 |
* @param target target definition |
38 |
* @return all bundle URLs |
39 |
*/ |
40 |
protected Set getAllBundleURLs(ITargetDefinition target) throws Exception { |
41 |
BundleInfo[] code = target.resolveBundles(null); |
42 |
BundleInfo[] source = target.resolveSourceBundles(null); |
43 |
Set urls = new HashSet(code.length + source.length); |
44 |
for (int i = 0; i < code.length; i++) { |
45 |
urls.add(code[i].getLocation().toURL()); |
46 |
} |
47 |
for (int i = 0; i < source.length; i++) { |
48 |
urls.add(source[i].getLocation().toURL()); |
49 |
} |
50 |
return urls; |
51 |
} |
52 |
|
53 |
/** |
54 |
* Extracts the classic plug-ins archive, if not already done, and returns a path to the |
55 |
* root directory containing the plug-ins. |
56 |
* |
57 |
* @return path to the plug-ins directory |
58 |
* @throws Exception |
59 |
*/ |
60 |
protected IPath extracClassicPlugins() throws Exception { |
61 |
// extract the 3.0.2 skeleton |
62 |
IPath stateLocation = MacroPlugin.getDefault().getStateLocation(); |
63 |
IPath location = stateLocation.append("classic-plugins"); |
64 |
if (location.toFile().exists()) { |
65 |
return location; |
66 |
} |
67 |
URL zipURL = MacroPlugin.getBundleContext().getBundle().getEntry("/tests/targets/classic-plugins.zip"); |
68 |
Path zipPath = new Path(new File(FileLocator.toFileURL(zipURL).getFile()).getAbsolutePath()); |
69 |
ZipFile zipFile = new ZipFile(zipPath.toFile()); |
70 |
Enumeration entries = zipFile.entries(); |
71 |
while (entries.hasMoreElements()) { |
72 |
ZipEntry entry = (ZipEntry) entries.nextElement(); |
73 |
if (!entry.isDirectory()) { |
74 |
IPath entryPath = stateLocation.append(entry.getName()); |
75 |
File dir = entryPath.removeLastSegments(1).toFile(); |
76 |
dir.mkdirs(); |
77 |
File file = entryPath.toFile(); |
78 |
file.createNewFile(); |
79 |
InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry)); |
80 |
byte[] bytes = getInputStreamAsByteArray(inputStream, -1); |
81 |
inputStream.close(); |
82 |
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file)); |
83 |
outputStream.write(bytes); |
84 |
outputStream.close(); |
85 |
} |
86 |
} |
87 |
zipFile.close(); |
88 |
return location; |
89 |
} |
90 |
|
91 |
/** |
92 |
* Tests that a target definition equivalent to the default target platform |
93 |
* contains the same bundles as the default target platform (this is an |
94 |
* explicit location with no target weaving). |
95 |
* |
96 |
* @throws Exception |
97 |
*/ |
98 |
public void testDefaultTargetPlatform() throws Exception { |
99 |
// the new way |
100 |
ITargetDefinition definition = TargetFactory.newTargetDefinition(); |
101 |
IBundleContainer container = TargetFactory.newProfileContainer(TargetPlatform.getDefaultLocation()); |
102 |
definition.setBundleContainers(new IBundleContainer[]{container}); |
103 |
Set urls = getAllBundleURLs(definition); |
104 |
|
105 |
// the old way |
106 |
IPath location = new Path(TargetPlatform.getDefaultLocation()); |
107 |
URL[] pluginPaths = P2Utils.readBundlesTxt(location.toOSString(), location.append("configuration").toFile().toURL()); |
108 |
assertEquals("Should have same number of bundles", pluginPaths.length, urls.size()); |
109 |
for (int i = 0; i < pluginPaths.length; i++) { |
110 |
URL url = pluginPaths[i]; |
111 |
assertTrue("Missing plug-in " + url.toString(), urls.contains(url)); |
112 |
} |
113 |
|
114 |
} |
115 |
|
116 |
/** |
117 |
* Tests that a target definition equivalent to the default target platform |
118 |
* contains the same bundles as the default target platform using the |
119 |
* platform's configuration location (which will do target weaving). This |
120 |
* is really only tested when run as a JUnit plug-in test suite from |
121 |
* within Eclipse. |
122 |
* |
123 |
* @throws Exception |
124 |
*/ |
125 |
public void testWovenTargetPlatform() throws Exception { |
126 |
// the new way |
127 |
ITargetDefinition definition = TargetFactory.newTargetDefinition(); |
128 |
IBundleContainer container = TargetFactory.newProfileContainer(TargetPlatform.getDefaultLocation(), |
129 |
new File(Platform.getConfigurationLocation().getURL().getFile()).getAbsolutePath()); |
130 |
definition.setBundleContainers(new IBundleContainer[]{container}); |
131 |
Set urls = getAllBundleURLs(definition); |
132 |
|
133 |
// the old way |
134 |
URL[] pluginPaths = PluginPathFinder.getPluginPaths(TargetPlatform.getDefaultLocation()); |
135 |
assertEquals("Should have same number of bundles", pluginPaths.length, urls.size()); |
136 |
for (int i = 0; i < pluginPaths.length; i++) { |
137 |
URL url = pluginPaths[i]; |
138 |
assertTrue("Missing plug-in " + url.toString(), urls.contains(url)); |
139 |
} |
140 |
|
141 |
} |
142 |
|
143 |
/** |
144 |
* Tests that a bundle directory container is equivalent to scanning locations. |
145 |
* |
146 |
* @throws Exception |
147 |
*/ |
148 |
public void testDirectoryBundleContainer() throws Exception { |
149 |
// the new way |
150 |
ITargetDefinition definition = TargetFactory.newTargetDefinition(); |
151 |
IBundleContainer container = TargetFactory.newDirectoryContainer(TargetPlatform.getDefaultLocation() + "/plugins"); |
152 |
definition.setBundleContainers(new IBundleContainer[]{container}); |
153 |
Set urls = getAllBundleURLs(definition); |
154 |
|
155 |
Preferences store = PDECore.getDefault().getPluginPreferences(); |
156 |
boolean restore = store.getBoolean(ICoreConstants.TARGET_PLATFORM_REALIZATION); |
157 |
try { |
158 |
store.setValue(ICoreConstants.TARGET_PLATFORM_REALIZATION, false); |
159 |
// the old way |
160 |
URL[] pluginPaths = PluginPathFinder.getPluginPaths(TargetPlatform.getDefaultLocation()); |
161 |
assertEquals("Should have same number of bundles", pluginPaths.length, urls.size()); |
162 |
for (int i = 0; i < pluginPaths.length; i++) { |
163 |
URL url = pluginPaths[i]; |
164 |
assertTrue("Missing plug-in " + url.toString(), urls.contains(url)); |
165 |
} |
166 |
} |
167 |
finally { |
168 |
store.setValue(ICoreConstants.TARGET_PLATFORM_REALIZATION, restore); |
169 |
} |
170 |
} |
171 |
|
172 |
/** |
173 |
* Tests reading a 3.0.2 install with a mix of classic and OSGi plug-ins. |
174 |
* |
175 |
* @throws Exception |
176 |
*/ |
177 |
public void testClassicPlugins() throws Exception { |
178 |
// extract the 3.0.2 skeleton |
179 |
IPath location = extracClassicPlugins(); |
180 |
|
181 |
// the new way |
182 |
ITargetDefinition definition = TargetFactory.newTargetDefinition(); |
183 |
IBundleContainer container = TargetFactory.newDirectoryContainer(location.toOSString()); |
184 |
definition.setBundleContainers(new IBundleContainer[]{container}); |
185 |
Set urls = getAllBundleURLs(definition); |
186 |
|
187 |
Preferences store = PDECore.getDefault().getPluginPreferences(); |
188 |
boolean restore = store.getBoolean(ICoreConstants.TARGET_PLATFORM_REALIZATION); |
189 |
try { |
190 |
store.setValue(ICoreConstants.TARGET_PLATFORM_REALIZATION, false); |
191 |
// the old way |
192 |
URL[] pluginPaths = PluginPathFinder.getPluginPaths(location.toOSString()); |
193 |
for (int i = 0; i < pluginPaths.length; i++) { |
194 |
URL url = pluginPaths[i]; |
195 |
if (!urls.contains(url)) { |
196 |
System.err.println(url.toString()); |
197 |
} |
198 |
} |
199 |
assertEquals("Wrong number of bundles", pluginPaths.length, urls.size()); |
200 |
} |
201 |
finally { |
202 |
store.setValue(ICoreConstants.TARGET_PLATFORM_REALIZATION, restore); |
203 |
} |
204 |
} |
205 |
|
206 |
/** |
207 |
* Tests identification of source bundles in a 3.0.2 install. |
208 |
* |
209 |
* @throws Exception |
210 |
*/ |
211 |
public void testClassicSourcePlugins() throws Exception { |
212 |
// extract the 3.0.2 skeleton |
213 |
IPath location = extracClassicPlugins(); |
214 |
|
215 |
// the new way |
216 |
ITargetDefinition definition = TargetFactory.newTargetDefinition(); |
217 |
IBundleContainer container = TargetFactory.newDirectoryContainer(location.toOSString()); |
218 |
definition.setBundleContainers(new IBundleContainer[]{container}); |
219 |
BundleInfo[] bundles = definition.resolveSourceBundles(null); |
220 |
assertEquals("Wrong number of source bundles", 3, bundles.length); |
221 |
Set names = new HashSet(); |
222 |
for (int i = 0; i < bundles.length; i++) { |
223 |
names.add(bundles[i].getSymbolicName()); |
224 |
} |
225 |
String[] expected = new String[]{"org.eclipse.platform.source", "org.eclipse.jdt.source", "org.eclipse.pde.source"}; |
226 |
for (int i = 0; i < expected.length; i++) { |
227 |
assertTrue("Missing source for " + expected[i], names.contains(expected[i])); |
228 |
} |
229 |
} |
230 |
|
231 |
/** |
232 |
* Returns the given input stream as a byte array |
233 |
* @param stream the stream to get as a byte array |
234 |
* @param length the length to read from the stream or -1 for unknown |
235 |
* @return the given input stream as a byte array |
236 |
* @throws IOException |
237 |
*/ |
238 |
public static byte[] getInputStreamAsByteArray(InputStream stream, int length) throws IOException { |
239 |
byte[] contents; |
240 |
if (length == -1) { |
241 |
contents = new byte[0]; |
242 |
int contentsLength = 0; |
243 |
int amountRead = -1; |
244 |
do { |
245 |
// read at least 8K |
246 |
int amountRequested = Math.max(stream.available(), 8192); |
247 |
// resize contents if needed |
248 |
if (contentsLength + amountRequested > contents.length) { |
249 |
System.arraycopy(contents, |
250 |
0, |
251 |
contents = new byte[contentsLength + amountRequested], |
252 |
0, |
253 |
contentsLength); |
254 |
} |
255 |
// read as many bytes as possible |
256 |
amountRead = stream.read(contents, contentsLength, amountRequested); |
257 |
if (amountRead > 0) { |
258 |
// remember length of contents |
259 |
contentsLength += amountRead; |
260 |
} |
261 |
} while (amountRead != -1); |
262 |
// resize contents if necessary |
263 |
if (contentsLength < contents.length) { |
264 |
System.arraycopy(contents, 0, contents = new byte[contentsLength], 0, contentsLength); |
265 |
} |
266 |
} else { |
267 |
contents = new byte[length]; |
268 |
int len = 0; |
269 |
int readSize = 0; |
270 |
while ((readSize != -1) && (len != length)) { |
271 |
// See PR 1FMS89U |
272 |
// We record first the read size. In this case length is the actual |
273 |
// read size. |
274 |
len += readSize; |
275 |
readSize = stream.read(contents, len, length - len); |
276 |
} |
277 |
} |
278 |
return contents; |
279 |
} |
280 |
} |