blob: 44878628b9604ba947bf71f964340602e26c5fca [file] [log] [blame]
/**********************************************************************
* Copyright (c) 2002 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM - Initial API and implementation
**********************************************************************/
package org.eclipse.core.tests.resources;
import java.io.IOException;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.core.boot.BootLoader;
import org.eclipse.core.internal.resources.Workspace;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.core.tests.harness.*;
/**
* Tests the following API methods:
* IFile#createLink
* IFolder#createLink
*
* This test supports both variable-based and non-variable-based locations.
* Although the method used for creating random locations
* <code>EclipseWorkspaceTest#getRandomLocation()</code> never returns variable-
* based paths, this method is overriden in the derived class
* <code>LinkedResourceWithPathVariable</code> to always return variable-based
* paths.
*
* To support variable-based paths wherever a file system location is used, it
* is mandatory first to resolve it and only then using it, except in calls to
* <code>IFile#createLink</code> and <code>IFolder#createLink</code> and when
* the location is obtained using <code>IResource#getLocation()</code>.
*/
public class LinkedResourceTest extends EclipseWorkspaceTest {
protected IProject existingProject;
protected IProject otherExistingProject;
protected IProject closedProject;
protected IProject nonExistingProject;
protected IFolder existingFolderInExistingProject;
protected IFolder existingFolderInExistingFolder;
protected IFolder nonExistingFolderInExistingProject;
protected IFolder nonExistingFolderInOtherExistingProject;
protected IFolder nonExistingFolderInNonExistingProject;
protected IFolder nonExistingFolderInExistingFolder;
protected IFolder nonExistingFolderInNonExistingFolder;
protected IFile existingFileInExistingProject;
protected IFile nonExistingFileInExistingProject;
protected IFile nonExistingFileInOtherExistingProject;
protected IFile nonExistingFileInExistingFolder;
protected IPath existingLocation;
protected IPath nonExistingLocation;
protected IPath localFile;
protected String childName = "File.txt";
public LinkedResourceTest() {
super();
}
public LinkedResourceTest(String name) {
super(name);
}
public static Test suite() {
return new TestSuite(LinkedResourceTest.class);
}
protected void setUp() throws Exception {
super.setUp();
existingProject = getWorkspace().getRoot().getProject("ExistingProject");
otherExistingProject = getWorkspace().getRoot().getProject("OtherExistingProject");
closedProject = getWorkspace().getRoot().getProject("ClosedProject");
existingFolderInExistingProject = existingProject.getFolder("existingFolderInExistingProject");
existingFolderInExistingFolder = existingFolderInExistingProject.getFolder("existingFolderInExistingFolder");
nonExistingFolderInExistingProject = existingProject.getFolder("nonExistingFolderInExistingProject");
nonExistingFolderInOtherExistingProject = otherExistingProject.getFolder("nonExistingFolderInOtherExistingProject");
nonExistingFolderInNonExistingFolder = nonExistingFolderInExistingProject.getFolder("nonExistingFolderInNonExistingFolder");
nonExistingFolderInExistingFolder = existingFolderInExistingProject.getFolder("nonExistingFolderInExistingFolder");
nonExistingProject = getWorkspace().getRoot().getProject("NonProject");
nonExistingFolderInNonExistingProject = nonExistingProject.getFolder("nonExistingFolderInNonExistingProject");
existingFileInExistingProject = existingProject.getFile("existingFileInExistingProject");
nonExistingFileInExistingProject = existingProject.getFile("nonExistingFileInExistingProject");
nonExistingFileInOtherExistingProject = otherExistingProject.getFile("nonExistingFileInOtherExistingProject");
nonExistingFileInExistingFolder = existingFolderInExistingProject.getFile("nonExistingFileInExistingFolder");
existingLocation = getRandomLocation();
nonExistingLocation = getRandomLocation();
localFile = existingLocation.append(childName);
doCleanup();
}
protected void doCleanup() throws Exception {
ensureExistsInWorkspace(new IResource[] {
existingProject,
otherExistingProject,
closedProject,
existingFolderInExistingProject,
existingFolderInExistingFolder,
existingFileInExistingProject}, true);
closedProject.close(getMonitor());
ensureDoesNotExistInWorkspace(new IResource[] {
nonExistingProject,
nonExistingFolderInExistingProject,
nonExistingFolderInExistingFolder,
nonExistingFolderInOtherExistingProject,
nonExistingFolderInNonExistingProject,
nonExistingFolderInNonExistingFolder,
nonExistingFileInExistingProject,
nonExistingFileInOtherExistingProject,
nonExistingFileInExistingFolder});
ensureDoesNotExistInFileSystem(resolvePath(nonExistingLocation).toFile());
resolvePath(existingLocation).toFile().mkdirs();
createFileInFileSystem(resolvePath(localFile), getRandomContents());
}
/**
* Maybe overridden in subclasses that use path variables.
*/
protected IPath resolvePath(IPath path) {
return path;
}
protected void tearDown() throws Exception {
super.tearDown();
Workspace.clear(resolvePath(existingLocation).toFile());
Workspace.clear(resolvePath(nonExistingLocation).toFile());
}
/**
* Tests creation of a linked resource whose corresponding file system
* path does not exist. This should suceed but no operations will be
* available on the resulting resource.
*/
public void testAllowMissingLocal() {
//get a non-existing location
IPath location = getRandomLocation();
IFolder folder = nonExistingFolderInExistingProject;
//try to create without the flag (should fail)
try {
folder.createLink(location, IResource.NONE, getMonitor());
fail("1.0");
} catch (CoreException e) {
//should fail
}
//now try to create with the flag (should suceed)
try {
folder.createLink(location, IResource.ALLOW_MISSING_LOCAL, getMonitor());
} catch (CoreException e) {
fail("1.1", e);
}
assertEquals("1.2", resolvePath(location), folder.getLocation());
assertTrue("1.3", !resolvePath(location).toFile().exists());
//getting children should suceed (and be empty)
try {
assertEquals("1.4", 0, folder.members().length);
} catch (CoreException e) {
fail("1.5", e);
}
//delete should suceed
try {
folder.delete(IResource.NONE, getMonitor());
} catch (CoreException e) {
fail("1.6", e);
}
//try to create with local path that can never exist
if (BootLoader.getOS().equals(BootLoader.OS_WIN32))
location = new Path("b:\\does\\not\\exist");
else
location = new Path("/dev/null/does/not/exist");
try {
folder.createLink(location, IResource.NONE, getMonitor());
fail("2.1");
} catch (CoreException e) {
//should fail
}
try {
folder.createLink(location, IResource.ALLOW_MISSING_LOCAL, getMonitor());
} catch (CoreException e) {
fail("2.2", e);
}
assertEquals("2.3", location, folder.getLocation());
assertTrue("2.4", !location.toFile().exists());
// creating child should fail
try {
folder.getFile("abc.txt").create(getRandomContents(), IResource.NONE, getMonitor());
fail("2.5");
} catch (CoreException e) {
//should fail
}
}
/**
* Tests case where a resource in the file system cannot be added to the workspace
* because it is blocked by a linked resource of the same name.
*/
public void testBlockedFolder() {
//create local folder that will be blocked
ensureExistsInFileSystem(nonExistingFolderInExistingProject);
IFile blockedFile = nonExistingFolderInExistingProject.getFile("BlockedFile");
try {
createFileInFileSystem(blockedFile.getLocation(), getRandomContents());
} catch (IOException e) {
fail("1.0", e);
}
try {
//link the folder elsewhere
nonExistingFolderInExistingProject.createLink(existingLocation, IResource.NONE, getMonitor());
//refresh the project
existingProject.refreshLocal(IResource.DEPTH_INFINITE, getMonitor());
} catch (CoreException e) {
fail("1.1", e);
}
//the blocked file should not exist in the workspace
assertTrue("1.2", !blockedFile.exists());
assertTrue("1.3", nonExistingFolderInExistingProject.exists());
assertTrue("1.4", nonExistingFolderInExistingProject.getFile(childName).exists());
assertEquals("1.5", nonExistingFolderInExistingProject.getLocation(), resolvePath(existingLocation));
//now delete the link
try {
nonExistingFolderInExistingProject.delete(IResource.NONE, getMonitor());
} catch (CoreException e) {
fail("1.99", e);
}
//the blocked file and the linked folder should not exist in the workspace
assertTrue("2.0", !blockedFile.exists());
assertTrue("2.1", !nonExistingFolderInExistingProject.exists());
assertTrue("2.2", !nonExistingFolderInExistingProject.getFile(childName).exists());
assertEquals("2.3", nonExistingFolderInExistingProject.getLocation(), existingProject.getLocation().append(nonExistingFolderInExistingProject.getName()));
//now refresh again to discover the blocked resource
try {
existingProject.refreshLocal(IResource.DEPTH_INFINITE, getMonitor());
} catch (CoreException e) {
fail("2.99", e);
}
//the blocked file should now exist
assertTrue("3.0", blockedFile.exists());
assertTrue("3.1", nonExistingFolderInExistingProject.exists());
assertTrue("3.2", !nonExistingFolderInExistingProject.getFile(childName).exists());
assertEquals("3.3", nonExistingFolderInExistingProject.getLocation(), existingProject.getLocation().append(nonExistingFolderInExistingProject.getName()));
//attempting to link again will fail because the folder exists in the workspace
try {
nonExistingFolderInExistingProject.createLink(existingLocation, IResource.NONE, getMonitor());
fail("3.4");
} catch (CoreException e) {
//expected
}
}
public void testCopyFile() {
final IFile source = nonExistingFileInExistingProject;
IResource[] destinationResources = new IResource[] {existingProject, closedProject, nonExistingFileInOtherExistingProject, nonExistingFileInExistingFolder};
Boolean[] deepCopy = new Boolean[] {Boolean.TRUE, Boolean.FALSE};
IProgressMonitor[] monitors = new IProgressMonitor[] {new FussyProgressMonitor(), new CancelingProgressMonitor(), null};
Object[][] inputs = new Object[][] {destinationResources, deepCopy, monitors};
new TestPerformer("LinkedResourceTest.testCopyFile") {
protected static final String CANCELLED = "cancelled";
public boolean shouldFail(Object[] args, int count) {
IResource destination = (IResource) args[0];
boolean isDeep = ((Boolean)args[1]).booleanValue();
IResource parent = destination.getParent();
if (!isDeep && (parent == null || parent.getType() != IResource.PROJECT))
return true;
if (!parent.isAccessible())
return true;
if (destination.exists())
return true;
//passed all failure cases so it should suceed
return false;
}
public Object invokeMethod(Object[] args, int count) throws Exception {
IResource destination = (IResource) args[0];
boolean isDeep = ((Boolean)args[1]).booleanValue();
IProgressMonitor monitor = (IProgressMonitor) args[2];
if (monitor instanceof FussyProgressMonitor)
((FussyProgressMonitor) monitor).prepare();
try {
source.createLink(localFile, IResource.NONE, null);
source.copy(destination.getFullPath(), isDeep ? IResource.NONE : IResource.SHALLOW, monitor);
} catch (OperationCanceledException e) {
return CANCELLED;
}
if (monitor instanceof FussyProgressMonitor)
((FussyProgressMonitor) monitor).sanityCheck();
return null;
}
public boolean wasSuccess(Object[] args, Object result, Object[] oldState) throws Exception {
if (result == CANCELLED)
return true;
IResource destination = (IResource) args[0];
boolean isDeep = ((Boolean)args[1]).booleanValue();
if (!destination.exists())
return false;
//destination should only be linked for a shallow copy
if (isDeep) {
if (destination.isLinked())
return false;
if (source.getLocation().equals(destination.getLocation()))
return false;
if (!destination.getProject().getLocation().isPrefixOf(destination.getLocation()))
return false;
} else {
if (!destination.isLinked())
return false;
if (!source.getLocation().equals(destination.getLocation()))
return false;
}
return true;
}
public void cleanUp(Object[] args, int count) {
super.cleanUp(args, count);
try {
doCleanup();
} catch (Exception e) {
fail("invocation " + count + " failed to cleanup", e);
}
}
}
.performTest(inputs);
}
public void testCopyFolder() {
final IFolder source = nonExistingFolderInExistingProject;
IResource[] destinationResources = new IResource[] {existingProject, closedProject, nonExistingProject, existingFolderInExistingProject, nonExistingFolderInOtherExistingProject, nonExistingFolderInExistingFolder};
Boolean[] deepCopy = new Boolean[] {Boolean.TRUE, Boolean.FALSE};
IProgressMonitor[] monitors = new IProgressMonitor[] {new FussyProgressMonitor(), new CancelingProgressMonitor(), null};
Object[][] inputs = new Object[][] {destinationResources, deepCopy, monitors};
new TestPerformer("LinkedResourceTest.testCopyFolder") {
protected static final String CANCELLED = "cancelled";
public boolean shouldFail(Object[] args, int count) {
IResource destination = (IResource) args[0];
boolean isDeep = ((Boolean)args[1]).booleanValue();
IResource parent = destination.getParent();
if (destination.getType() == IResource.PROJECT)
return true;
if (!isDeep && (parent == null || parent.getType() != IResource.PROJECT))
return true;
if (!parent.isAccessible())
return true;
if (destination.exists())
return true;
//passed all failure case so it should suceed
return false;
}
public Object invokeMethod(Object[] args, int count) throws Exception {
IResource destination = (IResource) args[0];
boolean isDeep = ((Boolean)args[1]).booleanValue();
IProgressMonitor monitor = (IProgressMonitor) args[2];
if (monitor instanceof FussyProgressMonitor)
((FussyProgressMonitor) monitor).prepare();
try {
source.createLink(existingLocation, IResource.NONE, null);
source.copy(destination.getFullPath(), isDeep ? IResource.NONE : IResource.SHALLOW, monitor);
} catch (OperationCanceledException e) {
return CANCELLED;
}
if (monitor instanceof FussyProgressMonitor)
((FussyProgressMonitor) monitor).sanityCheck();
return null;
}
public boolean wasSuccess(Object[] args, Object result, Object[] oldState) throws Exception {
if (result == CANCELLED)
return true;
IResource destination = (IResource) args[0];
boolean isDeep = ((Boolean)args[1]).booleanValue();
if (!destination.exists())
return false;
//destination should only be linked for a shallow copy
if (isDeep) {
if (destination.isLinked())
return false;
if (source.getLocation().equals(destination.getLocation()))
return false;
if (!destination.getProject().getLocation().isPrefixOf(destination.getLocation()))
return false;
} else {
if (!destination.isLinked())
return false;
if (!source.getLocation().equals(destination.getLocation()))
return false;
}
return true;
}
public void cleanUp(Object[] args, int count) {
super.cleanUp(args, count);
try {
doCleanup();
} catch (Exception e) {
fail("invocation " + count + " failed to cleanup", e);
}
}
}
.performTest(inputs);
}
public void testCopyProjectWithLinks() {
IPath fileLocation = getRandomLocation();
IFile file = nonExistingFileInExistingProject;
IFolder folder = nonExistingFolderInExistingProject;
try {
try {
createFileInFileSystem(resolvePath(fileLocation));
folder.createLink(existingLocation, IResource.NONE, getMonitor());
file.createLink(fileLocation, IResource.NONE, getMonitor());
} catch (CoreException e) {
fail("1.0", e);
}
//copy the project
IProject destination = getWorkspace().getRoot().getProject("CopyTargetProject");
try {
existingProject.copy(destination.getFullPath(), IResource.SHALLOW, getMonitor());
} catch (CoreException e) {
fail("2.0", e);
}
IFile newFile = destination.getFile(file.getProjectRelativePath());
assertTrue("3.0", newFile.isLinked());
assertEquals("3.1", file.getLocation(), newFile.getLocation());
IFolder newFolder = destination.getFolder(folder.getProjectRelativePath());
assertTrue("4.0", newFolder.isLinked());
assertEquals("4.1", folder.getLocation(), newFolder.getLocation());
//test project deep copy
try {
destination.delete(IResource.NONE, getMonitor());
existingProject.copy(destination.getFullPath(), IResource.NONE, getMonitor());
} catch (CoreException e) {
fail("5.0", e);
}
assertTrue("5.1", !newFile.isLinked());
assertEquals("5.2", destination.getLocation().append(newFile.getProjectRelativePath()), newFile.getLocation());
assertTrue("5.3", !newFolder.isLinked());
assertEquals("5.4", destination.getLocation().append(newFolder.getProjectRelativePath()), newFolder.getLocation());
} finally {
Workspace.clear(resolvePath(fileLocation).toFile());
}
}
public void testMoveFolder() {
IResource[] sourceResources = new IResource[] {nonExistingFolderInExistingProject};
IResource[] destinationResources = new IResource[] {existingProject, closedProject, nonExistingProject, existingFolderInExistingProject, nonExistingFolderInOtherExistingProject, nonExistingFolderInExistingFolder};
IProgressMonitor[] monitors = new IProgressMonitor[] {new FussyProgressMonitor(), new CancelingProgressMonitor(), null};
Object[][] inputs = new Object[][] {sourceResources, destinationResources, monitors};
new TestPerformer("LinkedResourceTest.testMoveFolder") {
protected static final String CANCELLED = "cancelled";
public boolean shouldFail(Object[] args, int count) {
IResource destination = (IResource) args[1];
IResource parent = destination.getParent();
if (parent == null || parent.getType() != IResource.PROJECT)
return true;
if (!parent.isAccessible())
return true;
if (destination.exists())
return true;
//passed all failure case so it should suceed
return false;
}
public Object invokeMethod(Object[] args, int count) throws Exception {
IFolder source = (IFolder) args[0];
IResource destination = (IResource) args[1];
IProgressMonitor monitor = (IProgressMonitor) args[2];
if (monitor instanceof FussyProgressMonitor)
((FussyProgressMonitor) monitor).prepare();
try {
source.createLink(existingLocation, IResource.NONE, null);
source.move(destination.getFullPath(), IResource.SHALLOW, monitor);
} catch (OperationCanceledException e) {
return CANCELLED;
}
if (monitor instanceof FussyProgressMonitor)
((FussyProgressMonitor) monitor).sanityCheck();
return null;
}
public boolean wasSuccess(Object[] args, Object result, Object[] oldState) throws Exception {
if (result == CANCELLED)
return true;
IResource destination = (IResource) args[1];
if (!destination.exists())
return false;
if (!destination.isLinked())
return false;
if (!resolvePath(existingLocation).equals(destination.getLocation()))
return false;
return true;
}
public void cleanUp(Object[] args, int count) {
super.cleanUp(args, count);
try {
doCleanup();
} catch (Exception e) {
fail("invocation " + count + " failed to cleanup", e);
}
}
}
.performTest(inputs);
}
public void testMoveProjectWithLinks() {
IPath fileLocation = getRandomLocation();
IFile file = nonExistingFileInExistingProject;
IFolder folder = nonExistingFolderInExistingProject;
IResource[] oldResources = new IResource[] {file, folder, existingProject};
try {
try {
createFileInFileSystem(resolvePath(fileLocation));
folder.createLink(existingLocation, IResource.NONE, getMonitor());
file.createLink(fileLocation, IResource.NONE, getMonitor());
} catch (CoreException e) {
fail("1.0", e);
}
//move the project
IProject destination = getWorkspace().getRoot().getProject("MoveTargetProject");
IFile newFile = destination.getFile(file.getProjectRelativePath());
IFolder newFolder = destination.getFolder(folder.getProjectRelativePath());
IResource[] newResources = new IResource[] {destination, newFile, newFolder};
assertDoesNotExistInWorkspace("2.0", destination);
try {
existingProject.move(destination.getFullPath(), IResource.SHALLOW, getMonitor());
} catch (CoreException e) {
fail("2.1", e);
}
assertExistsInWorkspace("3.0", newResources);
assertDoesNotExistInWorkspace("3.1", oldResources);
assertTrue("3.2", newFile.isLinked());
assertEquals("3.3", resolvePath(fileLocation), newFile.getLocation());
assertTrue("3.4", newFolder.isLinked());
assertEquals("3.5", resolvePath(existingLocation), newFolder.getLocation());
//now do a deep move back to the original project
try {
destination.move(existingProject.getFullPath(), IResource.NONE, getMonitor());
} catch (CoreException e) {
fail("5.0", e);
}
assertExistsInWorkspace("5.1", oldResources);
assertDoesNotExistInWorkspace("5.2", newResources);
assertTrue("5.3", !file.isLinked());
assertTrue("5.4", !folder.isLinked());
assertEquals("5.5", existingProject.getLocation().append(file.getProjectRelativePath()), file.getLocation());
assertEquals("5.6", existingProject.getLocation().append(folder.getProjectRelativePath()), folder.getLocation());
} finally {
Workspace.clear(resolvePath(fileLocation).toFile());
}
}
public void testNatureVeto() {
//note: simpleNature has the link veto turned on.
//test create link on project with nature veto
try {
IProjectDescription description = existingProject.getDescription();
description.setNatureIds(new String[] {NATURE_SIMPLE});
existingProject.setDescription(description, IResource.NONE, getMonitor());
} catch (CoreException e) {
fail("1.0", e);
}
try {
nonExistingFolderInExistingProject.createLink(existingLocation, IResource.NONE, getMonitor());
fail("1.1");
} catch (CoreException e) {
//should fail
}
try {
nonExistingFileInExistingProject.createLink(localFile, IResource.NONE, getMonitor());
fail("1.2");
} catch (CoreException e) {
//should fail
}
//test add nature with veto to project that already has link
try {
existingProject.delete(IResource.FORCE, getMonitor());
existingProject.create(getMonitor());
existingProject.open(getMonitor());
nonExistingFolderInExistingProject.createLink(existingLocation, IResource.NONE, getMonitor());
} catch (CoreException e) {
fail("2.0", e);
}
try {
IProjectDescription description = existingProject.getDescription();
description.setNatureIds(new String[] {NATURE_SIMPLE});
existingProject.setDescription(description, IResource.NONE, getMonitor());
fail("3.0");
} catch (CoreException e) {
//should fail
}
}
/**
* Automated test of IFile#createLink
*/
public void testLinkFile() {
IResource[] interestingResources = new IResource[] {existingFileInExistingProject, nonExistingFileInExistingProject, nonExistingFileInExistingFolder};
IPath[] interestingLocations = new IPath[] {existingLocation, nonExistingLocation};
IProgressMonitor[] monitors = new IProgressMonitor[] {new FussyProgressMonitor(), new CancelingProgressMonitor(), null};
Object[][] inputs = new Object[][] {interestingResources, interestingLocations, monitors};
new TestPerformer("LinkedResourceTest.testLinkFile") {
protected static final String CANCELLED = "cancelled";
public boolean shouldFail(Object[] args, int count) {
IResource resource = (IResource) args[0];
IPath location = (IPath)args[1];
//This resource already exists in the workspace
if (resource.exists())
return true;
IPath resolvedLocation = resolvePath(location);
//The corresponding location in the local file system does not exist.
if (!resolvedLocation.toFile().exists())
return true;
//The workspace contains a resource of a different type at the same path as this resource
if (getWorkspace().getRoot().findMember(resource.getFullPath()) != null)
return true;
//The parent of this resource does not exist.
if (!resource.getParent().isAccessible())
return true;
//The parent of this resource is not an open project
if (resource.getParent().getType() != IResource.PROJECT)
return true;
//The name of this resource is not valid (according to IWorkspace.validateName)
if (!getWorkspace().validateName(resource.getName(), IResource.FOLDER).isOK())
return true;
//The corresponding location in the local file system is occupied by a directory (as opposed to a file)
if (resolvedLocation.toFile().isDirectory())
return true;
//passed all failure case so it should suceed
return false;
}
public Object invokeMethod(Object[] args, int count) throws Exception {
IFile file = (IFile) args[0];
IPath location = (IPath)args[1];
IProgressMonitor monitor = (IProgressMonitor) args[2];
if (monitor instanceof FussyProgressMonitor)
((FussyProgressMonitor) monitor).prepare();
try {
file.createLink(location, IResource.NONE, monitor);
} catch (OperationCanceledException e) {
return CANCELLED;
}
if (monitor instanceof FussyProgressMonitor)
((FussyProgressMonitor) monitor).sanityCheck();
return null;
}
public boolean wasSuccess(Object[] args, Object result, Object[] oldState) throws Exception {
if (result == CANCELLED)
return true;
IFile resource = (IFile) args[0];
IPath location = (IPath)args[1];
IPath resolvedLocation = resolvePath(location);
if (!resource.exists() || !resolvedLocation.toFile().exists())
return false;
if (!resource.getLocation().equals(resolvedLocation))
return false;
if (!resource.isSynchronized(IResource.DEPTH_INFINITE))
return false;
return true;
}
public void cleanUp(Object[] args, int count) {
super.cleanUp(args, count);
try {
doCleanup();
} catch (Exception e) {
fail("invocation " + count + " failed to cleanup", e);
}
}
}
.performTest(inputs);
}/**
* Automated test of IFolder#createLink
*/
public void testLinkFolder() {
IResource[] interestingResources = new IResource[] {existingFolderInExistingProject, existingFolderInExistingFolder,
nonExistingFolderInExistingProject, nonExistingFolderInNonExistingProject, nonExistingFolderInNonExistingFolder};
IPath[] interestingLocations = new IPath[] {existingLocation, nonExistingLocation};
IProgressMonitor[] monitors = new IProgressMonitor[] {new FussyProgressMonitor(), new CancelingProgressMonitor(), null};
Object[][] inputs = new Object[][] {interestingResources, interestingLocations, monitors};
new TestPerformer("LinkedResourceTest.testLinkFolder") {
protected static final String CANCELLED = "cancelled";
public boolean shouldFail(Object[] args, int count) {
IResource resource = (IResource) args[0];
IPath location = (IPath)args[1];
//This resource already exists in the workspace
if (resource.exists())
return true;
//The corresponding location in the local file system does not exist.
if (!resolvePath(location).toFile().exists())
return true;
//The workspace contains a resource of a different type at the same path as this resource
if (getWorkspace().getRoot().findMember(resource.getFullPath()) != null)
return true;
//The parent of this resource does not exist.
if (!resource.getParent().isAccessible())
return true;
//The parent of this resource is not an open project
if (resource.getParent().getType() != IResource.PROJECT)
return true;
//The name of this resource is not valid (according to IWorkspace.validateName)
if (!getWorkspace().validateName(resource.getName(), IResource.FOLDER).isOK())
return true;
//The corresponding location in the local file system is occupied by a file (as opposed to a directory)
if (resolvePath(location).toFile().isFile())
return true;
//passed all failure case so it should suceed
return false;
}
public Object invokeMethod(Object[] args, int count) throws Exception {
IFolder folder = (IFolder) args[0];
IPath location = (IPath)args[1];
IProgressMonitor monitor = (IProgressMonitor) args[2];
if (monitor instanceof FussyProgressMonitor)
((FussyProgressMonitor) monitor).prepare();
try {
folder.createLink(location, IResource.NONE, monitor);
} catch (OperationCanceledException e) {
return CANCELLED;
}
if (monitor instanceof FussyProgressMonitor)
((FussyProgressMonitor) monitor).sanityCheck();
return null;
}
public boolean wasSuccess(Object[] args, Object result, Object[] oldState) throws Exception {
if (result == CANCELLED)
return true;
IFolder resource = (IFolder) args[0];
IPath location = (IPath)args[1];
IPath resolvedLocation = resolvePath(location);
if (!resource.exists() || !resolvedLocation.toFile().exists())
return false;
if (!resource.getLocation().equals(resolvedLocation))
return false;
//ensure child exists
if (!resource.getFile(childName).exists())
return false;
return true;
}
public void cleanUp(Object[] args, int count) {
super.cleanUp(args, count);
try {
doCleanup();
} catch (Exception e) {
fail("invocation " + count + " failed to cleanup", e);
}
}
}
.performTest(inputs);
}
public void testMoveFile() {
final IFile source = nonExistingFileInExistingProject;
IResource[] destinationResources = new IResource[] {existingProject, closedProject, nonExistingFileInOtherExistingProject, nonExistingFileInExistingFolder};
Boolean[] deepCopy = new Boolean[] {Boolean.TRUE, Boolean.FALSE};
IProgressMonitor[] monitors = new IProgressMonitor[] {new FussyProgressMonitor(), new CancelingProgressMonitor(), null};
Object[][] inputs = new Object[][] {destinationResources, deepCopy, monitors};
new TestPerformer("LinkedResourceTest.testMoveFile") {
protected static final String CANCELLED = "cancelled";
public boolean shouldFail(Object[] args, int count) {
IResource destination = (IResource) args[0];
boolean isDeep = ((Boolean)args[1]).booleanValue();
IResource parent = destination.getParent();
if (!isDeep && (parent == null || parent.getType() != IResource.PROJECT))
return true;
if (!parent.isAccessible())
return true;
if (destination.exists())
return true;
//passed all failure case so it should suceed
return false;
}
public Object invokeMethod(Object[] args, int count) throws Exception {
IResource destination = (IResource) args[0];
boolean isDeep = ((Boolean)args[1]).booleanValue();
IProgressMonitor monitor = (IProgressMonitor) args[2];
if (monitor instanceof FussyProgressMonitor)
((FussyProgressMonitor) monitor).prepare();
try {
source.createLink(localFile, IResource.NONE, null);
source.move(destination.getFullPath(), isDeep ? IResource.NONE : IResource.SHALLOW, monitor);
} catch (OperationCanceledException e) {
return CANCELLED;
}
if (monitor instanceof FussyProgressMonitor)
((FussyProgressMonitor) monitor).sanityCheck();
return null;
}
public boolean wasSuccess(Object[] args, Object result, Object[] oldState) throws Exception {
if (result == CANCELLED)
return true;
IResource destination = (IResource) args[0];
boolean isDeep = ((Boolean)args[1]).booleanValue();
if (!destination.exists())
return false;
//destination should only be linked for a shallow move
if (isDeep) {
if (destination.isLinked())
return false;
if (resolvePath(localFile).equals(destination.getLocation()))
return false;
if (!destination.getProject().getLocation().isPrefixOf(destination.getLocation()))
return false;
} else {
if (!destination.isLinked())
return false;
if (!resolvePath(localFile).equals(destination.getLocation()))
return false;
}
return true;
}
public void cleanUp(Object[] args, int count) {
super.cleanUp(args, count);
try {
doCleanup();
} catch (Exception e) {
fail("invocation " + count + " failed to cleanup", e);
}
}
}
.performTest(inputs);
}
}