### Eclipse Workspace Patch 1.0 #P org.eclipse.core.tests.resources Index: src/org/eclipse/core/tests/resources/LinkedResourceTest.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/LinkedResourceTest.java,v retrieving revision 1.50 diff -u -r1.50 LinkedResourceTest.java --- src/org/eclipse/core/tests/resources/LinkedResourceTest.java 1 Feb 2008 23:02:15 -0000 1.50 +++ src/org/eclipse/core/tests/resources/LinkedResourceTest.java 20 Oct 2008 13:22:06 -0000 @@ -10,6 +10,10 @@ *******************************************************************************/ package org.eclipse.core.tests.resources; +import java.nio.CharBuffer; + +import org.eclipse.core.runtime.jobs.Job; + import java.io.*; import java.net.URI; import java.util.HashMap; @@ -136,6 +140,143 @@ Workspace.clear(resolve(nonExistingLocation).toFile()); } + /** + * Bug251370 + * Updating a .project file within a nested Project causes problems when the project is closed + * and then reopened (having moved one of the linked resources) + * + * This test creates a linked resource between the inner project and outer project. + * The first linked resource is relative to a path variable "ROOT" + * The second is absolute + * + * It then closes and opens the inner project replacing the .project file between each open. + * For me the test fails sometime between the first close and open and the ~20th + */ + public void testChangingLinksWithNestedProjects() { + // /ExistingProject/foo + final String sOverlappingProject = "foo"; + final IFolder overlappingFolder = existingProject.getFolder(sOverlappingProject); + ensureExistsInFileSystem(overlappingFolder); + + // /foo -> /ExistingProject/foo + final IProject overlappingProject = getWorkspace().getRoot().getProject(sOverlappingProject); + IProjectDescription desc = getWorkspace().newProjectDescription(sOverlappingProject); + desc.setLocation(overlappingFolder.getLocation()); + try { + overlappingProject.create(desc, getMonitor()); + overlappingProject.open(getMonitor()); + assertTrue("0.9",overlappingProject.exists()); + } catch (CoreException e) { + fail("1.0"); + } + + // Create a path variable for the workspace root + try { + IPath ROOT = getWorkspace().getRoot().getLocation(); + getWorkspace().getPathVariableManager().setValue("ROOT", ROOT); + } catch (CoreException e) { + fail("1.5"); + } + + // Two linked resource paths: + // 1) Uses path variable + IPath l1 = new Path("ROOT").append(existingFolderInExistingProject.getFullPath()); + // 2) uses absolute path + IPath l2 = existingFolderInExistingProject.getLocation(); + + // Refresh Workspace + try { + getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, getMonitor()); + } catch (CoreException e) { + fail ("2.0"); + } + + // Create a link to the folder in the existing project + // /foo/linkedFolder -> /ExistingProject/existingFolderInExistingProject + IFolder linkedFolder = overlappingProject.getFolder("linkedFolder"); + try { + linkedFolder.createLink(l1, IResource.REPLACE, getMonitor()); + assertTrue("1.9",linkedFolder.exists()); + } catch (CoreException e) { + fail("3.0"); + } + + // Create a backup of the .project file + try { + overlappingProject.getFile(".project").copy(new Path(".project1"), true, getMonitor()); + } catch (CoreException e) { + fail("4.0"); + } + + // Change the variable to be relative to the ROOT variable + try { + linkedFolder.delete(true, getMonitor()); +// linkedFolder.setLinkLocation(l2, IResource.NONE, getMonitor()); + linkedFolder.createLink(l2, IResource.REPLACE, getMonitor()); + assertTrue(linkedFolder.exists()); + } catch (CoreException e) { + fail("5.0"); + } + + // Create a new .project file + try { + overlappingProject.getFile(".project").copy(new Path(".project2"), true, getMonitor()); + } catch (CoreException e) { + fail("6.0"); + } + + for (int i = 0; i < 100; i++) { + final String tdesc = i % 2 == 0 ? ".project1" : ".project2"; + + try { + getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, getMonitor()); + overlappingProject.close(getMonitor()); + } catch (CoreException e) { + fail("7.0"); + } + + System.out.println("Round " + i); + + Job j = new Job ("") { + protected IStatus run(IProgressMonitor monitor) { + try { + getWorkspace().run(new IWorkspaceRunnable() { + + public void run(IProgressMonitor monitor) throws CoreException { + try { + java.io.File projFile = overlappingFolder.getFile(".project").getLocation().toFile(); + java.io.File tempFile = overlappingFolder.getFile(tdesc).getLocation().toFile(); + CharBuffer cb = CharBuffer.allocate(100000); + int len = new FileReader(tempFile).read(cb); + Writer w = new FileWriter(projFile); + w.write(cb.array(), 0, len); + w.flush(); + w.close(); + } catch (Exception e) { + fail("8.0"); + } + } + }, getWorkspace().getRoot(), IWorkspace.AVOID_UPDATE, getMonitor()); + } catch (Exception e) { + fail("8.0"); + } + return Status.OK_STATUS; + } + }; + j.schedule(); + try { + j.join(); + } catch (InterruptedException e) {fail();} + + try { + overlappingProject.open(getMonitor()); + getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, getMonitor()); + } catch (CoreException e) { + fail ("10.0"); + } + } + } + /** * Tests creation of a linked resource whose corresponding file system * path does not exist. This should succeed but no operations will be