|
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.api.tools.builder.tests.compatibility; |
| 12 |
|
| 13 |
import java.io.File; |
| 14 |
import java.io.FileInputStream; |
| 15 |
import java.io.FileOutputStream; |
| 16 |
import java.io.InputStream; |
| 17 |
import java.util.HashSet; |
| 18 |
import java.util.Set; |
| 19 |
import java.util.jar.JarFile; |
| 20 |
|
| 21 |
import junit.framework.Test; |
| 22 |
|
| 23 |
import org.eclipse.core.resources.IFile; |
| 24 |
import org.eclipse.core.resources.IFolder; |
| 25 |
import org.eclipse.core.resources.IMarker; |
| 26 |
import org.eclipse.core.resources.IProject; |
| 27 |
import org.eclipse.core.resources.IResource; |
| 28 |
import org.eclipse.core.resources.ResourcesPlugin; |
| 29 |
import org.eclipse.core.runtime.IPath; |
| 30 |
import org.eclipse.core.runtime.Path; |
| 31 |
import org.eclipse.pde.api.tools.builder.tests.ApiBuilderTest; |
| 32 |
import org.eclipse.pde.api.tools.builder.tests.ApiProblem; |
| 33 |
import org.eclipse.pde.api.tools.builder.tests.ApiTestingEnvironment; |
| 34 |
import org.eclipse.pde.api.tools.internal.ApiSettingsXmlVisitor; |
| 35 |
import org.eclipse.pde.api.tools.internal.model.ApiModelFactory; |
| 36 |
import org.eclipse.pde.api.tools.internal.problems.ApiProblemFactory; |
| 37 |
import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; |
| 38 |
import org.eclipse.pde.api.tools.internal.provisional.IApiBaselineManager; |
| 39 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiBaseline; |
| 40 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent; |
| 41 |
import org.eclipse.pde.api.tools.internal.util.Util; |
| 42 |
import org.eclipse.pde.api.tools.model.tests.TestSuiteHelper; |
| 43 |
import org.eclipse.pde.api.tools.tests.ApiTestsPlugin; |
| 44 |
|
| 45 |
/** |
| 46 |
* Tests for splitting plug-ins across releases |
| 47 |
* |
| 48 |
* @since 1.0 |
| 49 |
*/ |
| 50 |
public class BundleSplitTest extends ApiBuilderTest { |
| 51 |
|
| 52 |
/** |
| 53 |
* |
| 54 |
*/ |
| 55 |
public static final String WORKSPACE_PROFILE = "split/post-split"; |
| 56 |
|
| 57 |
public static final String BASELINE = "split/pre-split"; |
| 58 |
|
| 59 |
/** |
| 60 |
* Constructor |
| 61 |
* @param name |
| 62 |
*/ |
| 63 |
public BundleSplitTest(String name) { |
| 64 |
super(name); |
| 65 |
} |
| 66 |
|
| 67 |
/* (non-Javadoc) |
| 68 |
* @see org.eclipse.pde.api.tools.builder.tests.ApiBuilderTests#setBuilderOptions() |
| 69 |
*/ |
| 70 |
protected void setBuilderOptions() { |
| 71 |
enableUnsupportedTagOptions(false); |
| 72 |
enableBaselineOptions(true); |
| 73 |
enableCompatibilityOptions(true); |
| 74 |
enableLeakOptions(false); |
| 75 |
enableSinceTagOptions(false); |
| 76 |
enableUsageOptions(false); |
| 77 |
enableVersionNumberOptions(true); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @return the tests for this class |
| 82 |
*/ |
| 83 |
public static Test suite() { |
| 84 |
return buildTestSuite(BundleSplitTest.class); |
| 85 |
} |
| 86 |
|
| 87 |
/* (non-Javadoc) |
| 88 |
* |
| 89 |
* Ensure a baseline has been created to compare against. |
| 90 |
* |
| 91 |
* @see org.eclipse.pde.api.tools.builder.tests.ApiBuilderTest#setUp() |
| 92 |
*/ |
| 93 |
@Override |
| 94 |
protected void setUp() throws Exception { |
| 95 |
ApiTestingEnvironment env = getEnv(); |
| 96 |
if (env != null) { |
| 97 |
env.setRevert(true); |
| 98 |
} |
| 99 |
super.setUp(); |
| 100 |
// build the baseline if not present |
| 101 |
IApiBaselineManager manager = ApiPlugin.getDefault().getApiBaselineManager(); |
| 102 |
IApiBaseline baseline = manager.getDefaultApiBaseline(); |
| 103 |
if (baseline == null) { |
| 104 |
// import baseline projects |
| 105 |
createExistingProjects(BASELINE, true); |
| 106 |
// create the API baseline |
| 107 |
IApiBaseline profile = manager.getWorkspaceBaseline(); |
| 108 |
IProject[] projects = getEnv().getWorkspace().getRoot().getProjects(); |
| 109 |
IPath baselineLocation = ApiTestsPlugin.getDefault().getStateLocation().append(BASELINE); |
| 110 |
for (int i = 0; i < projects.length; i++) { |
| 111 |
exportApiComponent( |
| 112 |
projects[i], |
| 113 |
profile.getApiComponent(projects[i].getName()), |
| 114 |
baselineLocation); |
| 115 |
} |
| 116 |
baseline = ApiModelFactory.newApiBaseline("API-baseline"); |
| 117 |
IApiComponent[] components = new IApiComponent[projects.length]; |
| 118 |
for (int i = 0; i < projects.length; i++) { |
| 119 |
IProject project = projects[i]; |
| 120 |
IPath location = baselineLocation.append(project.getName()); |
| 121 |
components[i] = ApiModelFactory.newApiComponent(baseline, location.toOSString()); |
| 122 |
} |
| 123 |
baseline.addApiComponents(components); |
| 124 |
manager.addApiBaseline(baseline); |
| 125 |
manager.setDefaultApiBaseline(baseline.getName()); |
| 126 |
// delete the projects |
| 127 |
for (int i = 0; i < projects.length; i++) { |
| 128 |
getEnv().removeProject(projects[i].getFullPath()); |
| 129 |
} |
| 130 |
} |
| 131 |
// for the first test create workspace projects |
| 132 |
IProject[] projects = getEnv().getWorkspace().getRoot().getProjects(); |
| 133 |
if (projects.length == 0) { |
| 134 |
// populate the workspace with initial plug-ins/projects |
| 135 |
createExistingProjects(WORKSPACE_PROFILE, true); |
| 136 |
} else { |
| 137 |
// build after revert |
| 138 |
incrementalBuild(); |
| 139 |
} |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
protected void tearDown() throws Exception { |
| 145 |
super.tearDown(); |
| 146 |
getEnv().setRevert(false); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Exports the project as an API component to be used in an API baseline. |
| 151 |
* |
| 152 |
* @param project project to export |
| 153 |
* @param apiComponent associated API component from the workspace profile |
| 154 |
* @param baselineLocation local file system directory to host exported component |
| 155 |
*/ |
| 156 |
private void exportApiComponent(IProject project, IApiComponent apiComponent, IPath baselineLocation) throws Exception { |
| 157 |
File root = baselineLocation.toFile(); |
| 158 |
File componentDir = new File(root, project.getName()); |
| 159 |
componentDir.mkdirs(); |
| 160 |
IResource[] members = project.members(); |
| 161 |
// copy root files and manifest |
| 162 |
for (int i = 0; i < members.length; i++) { |
| 163 |
IResource res = members[i]; |
| 164 |
if (res.getType() == IResource.FILE) { |
| 165 |
copyFile(componentDir, (IFile)res); |
| 166 |
} else if (res.getType() == IResource.FOLDER) { |
| 167 |
if (res.getName().equals("META-INF")) { |
| 168 |
File manDir = new File(componentDir, "META-INF"); |
| 169 |
manDir.mkdirs(); |
| 170 |
copyFile(manDir, ((IFolder)res).getFile("MANIFEST.MF")); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
// copy over .class files |
| 175 |
IFolder output = project.getFolder("bin"); |
| 176 |
copyFolder(output, componentDir); |
| 177 |
// API Description |
| 178 |
ApiSettingsXmlVisitor visitor = new ApiSettingsXmlVisitor(apiComponent); |
| 179 |
apiComponent.getApiDescription().accept(visitor); |
| 180 |
String xml = visitor.getXML(); |
| 181 |
File desc = new File(componentDir, ".api_description"); |
| 182 |
desc.createNewFile(); |
| 183 |
FileOutputStream stream = new FileOutputStream(desc); |
| 184 |
stream.write(xml.getBytes("UTF-8")); |
| 185 |
stream.close(); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Copy the folder contents to the local file system. |
| 190 |
* |
| 191 |
* @param folder workspace folder |
| 192 |
* @param dir local directory |
| 193 |
*/ |
| 194 |
private void copyFolder(IFolder folder, File dir) throws Exception { |
| 195 |
IResource[] members = folder.members(); |
| 196 |
for (int i = 0; i < members.length; i++) { |
| 197 |
IResource res = members[i]; |
| 198 |
if (res.getType() == IResource.FILE) { |
| 199 |
IFile file = (IFile) res; |
| 200 |
copyFile(dir, file); |
| 201 |
} else { |
| 202 |
IFolder nested = (IFolder) res; |
| 203 |
File next = new File(dir, nested.getName()); |
| 204 |
next.mkdirs(); |
| 205 |
copyFolder(nested, next); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Copies the given file to the given directory. |
| 212 |
* |
| 213 |
* @param dir |
| 214 |
* @param file |
| 215 |
*/ |
| 216 |
private void copyFile(File dir, IFile file) throws Exception { |
| 217 |
File local = new File(dir, file.getName()); |
| 218 |
local.createNewFile(); |
| 219 |
FileOutputStream stream = new FileOutputStream(local); |
| 220 |
InputStream contents = file.getContents(); |
| 221 |
byte[] bytes = Util.getInputStreamAsByteArray(contents, -1); |
| 222 |
stream.write(bytes); |
| 223 |
contents.close(); |
| 224 |
stream.close(); |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Updates the contents of a workspace file at the specified location (full path), |
| 229 |
* with the contents of a local file at the given replacement location (absolute path). |
| 230 |
* |
| 231 |
* @param workspaceLocation |
| 232 |
* @param replacementLocation |
| 233 |
*/ |
| 234 |
protected void updateWorkspaceFile(IPath workspaceLocation, IPath replacementLocation) throws Exception { |
| 235 |
IFile file = getEnv().getWorkspace().getRoot().getFile(workspaceLocation); |
| 236 |
assertTrue("Workspace file does not exist: " + workspaceLocation.toString(), file.exists()); |
| 237 |
File replacement = replacementLocation.toFile(); |
| 238 |
assertTrue("Replacement file does not exist: " + replacementLocation.toOSString(), replacement.exists()); |
| 239 |
FileInputStream stream = new FileInputStream(replacement); |
| 240 |
file.setContents(stream, false, true, null); |
| 241 |
stream.close(); |
| 242 |
getEnv().changed(workspaceLocation); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Updates the contents of a workspace file at the specified location (full path), |
| 247 |
* with the contents of a local file at the given replacement location (absolute path). |
| 248 |
* |
| 249 |
* @param workspaceLocation |
| 250 |
* @param replacementLocation |
| 251 |
*/ |
| 252 |
protected void createWorkspaceFile(IPath workspaceLocation, IPath replacementLocation) throws Exception { |
| 253 |
IFile file = getEnv().getWorkspace().getRoot().getFile(workspaceLocation); |
| 254 |
assertFalse("Workspace file should not exist: " + workspaceLocation.toString(), file.exists()); |
| 255 |
File replacement = replacementLocation.toFile(); |
| 256 |
assertTrue("Replacement file does not exist: " + replacementLocation.toOSString(), replacement.exists()); |
| 257 |
FileInputStream stream = new FileInputStream(replacement); |
| 258 |
file.create(stream, false, null); |
| 259 |
stream.close(); |
| 260 |
getEnv().added(workspaceLocation); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Deletes the workspace file at the specified location (full path). |
| 265 |
* |
| 266 |
* @param workspaceLocation |
| 267 |
*/ |
| 268 |
protected void deleteWorkspaceFile(IPath workspaceLocation) throws Exception { |
| 269 |
IFile file = getEnv().getWorkspace().getRoot().getFile(workspaceLocation); |
| 270 |
assertTrue("Workspace file does not exist: " + workspaceLocation.toString(), file.exists()); |
| 271 |
file.delete(false, null); |
| 272 |
getEnv().removed(workspaceLocation); |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Returns a path in the local file system to an updated file based on this tests source path |
| 277 |
* and filename. |
| 278 |
* |
| 279 |
* @param filename name of file to update |
| 280 |
* @return path to the file in the local file system |
| 281 |
*/ |
| 282 |
protected IPath getUpdateFilePath(String filename) { |
| 283 |
return TestSuiteHelper.getPluginDirectoryPath().append(TEST_SOURCE_ROOT).append(getTestSourcePath()).append(filename); |
| 284 |
} |
| 285 |
/** |
| 286 |
* Performs a compatibility test. The workspace file at the specified (full workspace path) |
| 287 |
* location is updated with a corresponding file from test data. A build is performed |
| 288 |
* and problems are compared against the expected problems for the associated resource. |
| 289 |
* |
| 290 |
* @param workspaceFile file to update |
| 291 |
* @param incremental whether to perform an incremental (<code>true</code>) or |
| 292 |
* full (<code>false</code>) build |
| 293 |
* @throws Exception |
| 294 |
*/ |
| 295 |
protected void performCompatibilityTest(IPath workspaceFile, boolean incremental) throws Exception { |
| 296 |
updateWorkspaceFile( |
| 297 |
workspaceFile, |
| 298 |
getUpdateFilePath(workspaceFile.lastSegment())); |
| 299 |
if (incremental) { |
| 300 |
incrementalBuild(); |
| 301 |
} else { |
| 302 |
fullBuild(); |
| 303 |
} |
| 304 |
IMarker[] jdtMarkers = getEnv().getAllJDTMarkers(workspaceFile); |
| 305 |
int length = jdtMarkers.length; |
| 306 |
if (length != 0) { |
| 307 |
for (int i = 0; i < length; i++) { |
| 308 |
boolean condition = jdtMarkers[i].getAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING) == IMarker.SEVERITY_ERROR; |
| 309 |
if (condition) { |
| 310 |
System.err.println("workspace file : " + workspaceFile.toOSString()); |
| 311 |
System.err.println(jdtMarkers[i].getAttribute(IMarker.MESSAGE)); |
| 312 |
} |
| 313 |
assertFalse("Should not be a JDT error", condition); |
| 314 |
} |
| 315 |
} |
| 316 |
ApiProblem[] problems = getEnv().getProblemsFor(workspaceFile, null); |
| 317 |
assertProblems(problems); |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Performs a compatibility test. The workspace file at the specified (full workspace path) |
| 322 |
* location is updated with a corresponding file from test data. A build is performed |
| 323 |
* and problems are compared against the expected problems for the associated resource. |
| 324 |
* |
| 325 |
* @param workspaceFile file to update |
| 326 |
* @param incremental whether to perform an incremental (<code>true</code>) or |
| 327 |
* full (<code>false</code>) build |
| 328 |
* @throws Exception |
| 329 |
*/ |
| 330 |
protected void performVersionTest(IPath workspaceFile, boolean incremental) throws Exception { |
| 331 |
updateWorkspaceFile( |
| 332 |
workspaceFile, |
| 333 |
getUpdateFilePath(workspaceFile.lastSegment())); |
| 334 |
if (incremental) { |
| 335 |
incrementalBuild(); |
| 336 |
} else { |
| 337 |
fullBuild(); |
| 338 |
} |
| 339 |
ApiProblem[] problems = getEnv().getProblemsFor(new Path(workspaceFile.segment(0)).append(JarFile.MANIFEST_NAME), null); |
| 340 |
assertProblems(problems); |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Performs a compatibility test. The workspace file at the specified (full workspace path) |
| 345 |
* location is deleted. A build is performed and problems are compared against the expected |
| 346 |
* problems for the associated resource. |
| 347 |
* |
| 348 |
* @param workspaceFile file to update |
| 349 |
* @param incremental whether to perform an incremental (<code>true</code>) or |
| 350 |
* full (<code>false</code>) build |
| 351 |
* @throws Exception |
| 352 |
*/ |
| 353 |
protected void performDeletionCompatibilityTest(IPath workspaceFile, boolean incremental) throws Exception { |
| 354 |
deleteWorkspaceFile(workspaceFile); |
| 355 |
if (incremental) { |
| 356 |
incrementalBuild(); |
| 357 |
} else { |
| 358 |
fullBuild(); |
| 359 |
} |
| 360 |
ApiProblem[] problems = getEnv().getProblems(); |
| 361 |
assertProblems(problems); |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Performs a compatibility test. The workspace file at the specified (full workspace path) |
| 366 |
* location is created. A build is performed and problems are compared against the expected |
| 367 |
* problems for the associated resource. |
| 368 |
* |
| 369 |
* @param workspaceFile file to update |
| 370 |
* @param incremental whether to perform an incremental (<code>true</code>) or |
| 371 |
* full (<code>false</code>) build |
| 372 |
* @throws Exception |
| 373 |
*/ |
| 374 |
protected void performCreationCompatibilityTest(IPath workspaceFile, boolean incremental) throws Exception { |
| 375 |
createWorkspaceFile( |
| 376 |
workspaceFile, |
| 377 |
getUpdateFilePath(workspaceFile.lastSegment())); |
| 378 |
if (incremental) { |
| 379 |
incrementalBuild(); |
| 380 |
} else { |
| 381 |
fullBuild(); |
| 382 |
} |
| 383 |
ApiProblem[] problems = getEnv().getProblems(); |
| 384 |
assertProblems(problems); |
| 385 |
} |
| 386 |
|
| 387 |
/* (non-Javadoc) |
| 388 |
* @see org.eclipse.pde.api.tools.builder.tests.ApiBuilderTest#assertProblems(org.eclipse.pde.api.tools.builder.tests.ApiProblem[]) |
| 389 |
*/ |
| 390 |
@Override |
| 391 |
protected void assertProblems(ApiProblem[] problems) { |
| 392 |
super.assertProblems(problems); |
| 393 |
int[] expectedProblemIds = getExpectedProblemIds(); |
| 394 |
int length = problems.length; |
| 395 |
if (expectedProblemIds.length != length) { |
| 396 |
for (int i = 0; i < length; i++) { |
| 397 |
System.err.println(problems[i]); |
| 398 |
} |
| 399 |
} |
| 400 |
assertEquals("Wrong number of problems", expectedProblemIds.length, length); |
| 401 |
String[][] args = getExpectedMessageArgs(); |
| 402 |
if (args != null) { |
| 403 |
// compare messages |
| 404 |
Set<String> set = new HashSet<String>(); |
| 405 |
for (int i = 0; i < length; i++) { |
| 406 |
set.add(problems[i].getMessage()); |
| 407 |
} |
| 408 |
for (int i = 0; i < expectedProblemIds.length; i++) { |
| 409 |
String[] messageArgs = args[i]; |
| 410 |
int messageId = ApiProblemFactory.getProblemMessageId(expectedProblemIds[i]); |
| 411 |
String message = ApiProblemFactory.getLocalizedMessage(messageId, messageArgs); |
| 412 |
assertTrue("Missing expected problem: " + message, set.remove(message)); |
| 413 |
} |
| 414 |
} else { |
| 415 |
// compare id's |
| 416 |
Set<Integer> set = new HashSet<Integer>(); |
| 417 |
for (int i = 0; i < length; i++) { |
| 418 |
set.add(new Integer(problems[i].getProblemId())); |
| 419 |
} |
| 420 |
for (int i = 0; i < expectedProblemIds.length; i++) { |
| 421 |
assertTrue("Missing expected problem: " + expectedProblemIds[i], set.remove(new Integer(expectedProblemIds[i]))); |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
/** |
| 427 |
* Tests that splitting a plug-in and re-exporting the base is compatible with |
| 428 |
* previous release. |
| 429 |
* |
| 430 |
* @throws Exception |
| 431 |
*/ |
| 432 |
public void testPluginSplit() throws Exception { |
| 433 |
fullBuild(); |
| 434 |
IMarker[] jdtMarkers = getEnv().getAllJDTMarkers(ResourcesPlugin.getWorkspace().getRoot().getLocation()); |
| 435 |
int length = jdtMarkers.length; |
| 436 |
if (length != 0) { |
| 437 |
for (int i = 0; i < length; i++) { |
| 438 |
boolean condition = jdtMarkers[i].getAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING) == IMarker.SEVERITY_ERROR; |
| 439 |
if (condition) { |
| 440 |
System.err.println(jdtMarkers[i].getAttribute(IMarker.MESSAGE)); |
| 441 |
} |
| 442 |
assertFalse("Should not be a JDT error", condition); |
| 443 |
} |
| 444 |
} |
| 445 |
ApiProblem[] problems = getEnv().getProblems(); |
| 446 |
assertProblems(problems); |
| 447 |
} |
| 448 |
|
| 449 |
/* (non-Javadoc) |
| 450 |
* @see org.eclipse.pde.api.tools.builder.tests.ApiBuilderTest#getDefaultProblemId() |
| 451 |
*/ |
| 452 |
@Override |
| 453 |
protected int getDefaultProblemId() { |
| 454 |
// TODO Auto-generated method stub |
| 455 |
return 0; |
| 456 |
} |
| 457 |
|
| 458 |
/* (non-Javadoc) |
| 459 |
* @see org.eclipse.pde.api.tools.builder.tests.ApiBuilderTest#getTestSourcePath() |
| 460 |
*/ |
| 461 |
@Override |
| 462 |
protected IPath getTestSourcePath() { |
| 463 |
// TODO Auto-generated method stub |
| 464 |
return null; |
| 465 |
} |
| 466 |
|
| 467 |
/* (non-Javadoc) |
| 468 |
* @see org.eclipse.pde.api.tools.builder.tests.ApiBuilderTest#getTestingProjectName() |
| 469 |
*/ |
| 470 |
@Override |
| 471 |
protected String getTestingProjectName() { |
| 472 |
// TODO Auto-generated method stub |
| 473 |
return null; |
| 474 |
} |
| 475 |
|
| 476 |
} |