Some of our JUnits suites require that projects or other resources be
deleted during the test, for various reasons, and I think some of the
"random errors" we see are because it's hard to delete a project, or file,
in a multi-threaded Eclipse. If there is a file from the project open in
another thread, for example, perhaps in a thread that's running
validation, hence reading files, the delete will fail ("randomly") and
then that might cause a JUnit test to fail, directly or indirectly (by
having an unexpected state).
I had to solve this, just this evening, and took me a while, reading
platform test examples, and googling around, to find a solution that I
think is fairly good ... for JUnit tests, at least.
I've pasted the code below ... just in case it helps anyone. Hopefully the
constants are obvious, but if anyone wants to see the whole class, it is
org.eclipse.jst.jsp.core.tests.taglibindex.TestIndex
And ... as always the case in open development ... if anyone has other
tips/tricks, or better ways, feel free to let us know.
Thanks,
/**
* It's not easy to delete projects. If any of it's files are open
by another thread,
* the operation will fail. So, this method will make several
attempts before giving up.
* @param project
* @throws CoreException
* @throws InterruptedException
*/
private void deleteProject(IProject project) throws CoreException,
InterruptedException {
int nTrys = 0;
while (project != null && project.exists() && nTrys <
MAX_RETRYS) {
try {
nTrys++;
project.delete(true, true, null);
}
catch (ResourceException e) {
if (DEBUG) {
System.out.println();
System.out.println("Could not
delete project on attempt number: "+ nTrys);
IStatus eStatus = e.getStatus();
// should always be MultiStatus,
but we'll check
if (eStatus instanceof
MultiStatus) {
MultiStatus mStatus =
(MultiStatus) eStatus;
IStatus[] iStatus =
mStatus.getChildren();
for (int j = 0; j <
iStatus.length; j++) {
System.out
.println("Status: " + j + " " + iStatus[j]);
}
}
else {
System.out.println(
"Status: " + eStatus);
}
}
/*
* If we could not delete the first time,
wait a bit and
* re-try. If we could not delete, it is
likely because
* another thread has a file open, or
similar (such as the
* validation thread).
*/
Thread.sleep(PAUSE_TIME);
}
}
if (project != null && project.exists()) {
fail("Error in test infrastructure. Could not
delete project " + project + " after " + MAX_RETRYS + "attempts.");
}
}_______________________________________________
wtp-dev mailing list
[email protected]
https://dev.eclipse.org/mailman/listinfo/wtp-dev