use of org.eclipse.core.resources.IWorkspace in project eclipse-pmd by acanda.
the class LocationResolverTest method resolveIfExistsWorkspaceLocationWithMissingFile.
/**
* Verifies that {@link LocationResolver#resolveIfExists(Location, IProject)} does not throw an exception in a
* workspace context if the rule set file does not exist.
*/
@Test
public void resolveIfExistsWorkspaceLocationWithMissingFile() throws URISyntaxException {
final Location location = new Location("project/pmd.xml", LocationContext.WORKSPACE);
final IProject project = mock(IProject.class);
final IWorkspace workspace = mock(IWorkspace.class);
final IWorkspaceRoot workspaceRoot = mock(IWorkspaceRoot.class);
when(project.getWorkspace()).thenReturn(workspace);
when(workspace.getRoot()).thenReturn(workspaceRoot);
when(workspaceRoot.getProject("project")).thenReturn(project);
when(project.getLocationURI()).thenReturn(new URI("file:///workspace/project"));
final Optional<String> result = LocationResolver.resolveIfExists(location, project);
assertFalse("The location should not resolve", result.isPresent());
}
use of org.eclipse.core.resources.IWorkspace in project eclipse-pmd by acanda.
the class LocationResolverTest method resolveIfExistsWorkspaceLocationWithProjectNameOnly.
/**
* Verifies that {@link LocationResolver#resolveIfExists(Location, IProject)} does not throw an exception in a
* workspace context if the path consists only of the project name.
*/
@Test
public void resolveIfExistsWorkspaceLocationWithProjectNameOnly() throws URISyntaxException {
final Location location = new Location("project", LocationContext.WORKSPACE);
final IProject project = mock(IProject.class);
final IWorkspace workspace = mock(IWorkspace.class);
final IWorkspaceRoot workspaceRoot = mock(IWorkspaceRoot.class);
when(project.getWorkspace()).thenReturn(workspace);
when(workspace.getRoot()).thenReturn(workspaceRoot);
when(workspaceRoot.getProject("project")).thenReturn(project);
when(project.getLocationURI()).thenReturn(new URI("file:///workspace/project/"));
final Optional<String> result = LocationResolver.resolveIfExists(location, project);
assertFalse("The location should not resolve", result.isPresent());
}
use of org.eclipse.core.resources.IWorkspace in project eclipse-pmd by acanda.
the class LocationResolverTest method resolveIfExistsWorkspaceLocation.
/**
* Verifies that {@link LocationResolver#resolveIfExists(Location, IProject)} resolves the location in a workspace
* context correctly.
*/
@Test
public void resolveIfExistsWorkspaceLocation() throws URISyntaxException, IOException {
final Path ruleSetFile = Files.createTempFile(LocationResolverTest.class.getSimpleName(), ".xml");
try {
final Location location = new Location("project/" + ruleSetFile.getFileName().toString(), LocationContext.WORKSPACE);
final IProject project = mock(IProject.class);
final IWorkspace workspace = mock(IWorkspace.class);
final IWorkspaceRoot workspaceRoot = mock(IWorkspaceRoot.class);
when(project.getWorkspace()).thenReturn(workspace);
when(workspace.getRoot()).thenReturn(workspaceRoot);
when(workspaceRoot.getProject("project")).thenReturn(project);
when(project.getLocationURI()).thenReturn(ruleSetFile.getParent().toUri());
final Optional<String> result = LocationResolver.resolveIfExists(location, project);
assertTrue("A valid workspace location should resolve", result.isPresent());
assertEquals("The resolved location in a workspace context should be the provided location appended to the workspace location", ruleSetFile.toString(), result.get());
} finally {
Files.deleteIfExists(ruleSetFile);
}
}
use of org.eclipse.core.resources.IWorkspace in project yamcs-studio by yamcs.
the class FileUtil method writeTextFile.
/**
* Write a text file.
*
* @param filePath
* path of the file. It can be an absolute path or a relative path to the OPI that contains the specified
* widget. If it is an absolute path, it can be either<br>
* a workspace path such as <code>/BOY Examples/Scripts/myfile.xml</code><br>
* a local file system path such as <code>C:\myfile.xml</code><br>
* or an URL path such as <code>http://mysite.com/myfile.xml</code>.
* @param inWorkspace
* true if the file path is a workspace file path. Otherwise, it will be recognized as a local file
* system file.
* @param widget
* a widget in the OPI, which is used to provide relative path reference. It can be null if the path is
* an absolute path.
* @param text
* the text to be written to the file.
* @param append
* true if the text should be appended to the end of the file.
* @throws Exception
* if error happens.
*/
public static void writeTextFile(String filePath, boolean inWorkspace, AbstractBaseEditPart widget, String text, boolean append) throws Exception {
IPath path = FileUtil.buildAbsolutePath(filePath, widget);
if (inWorkspace) {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
String projectName = path.segment(0);
IProject project = root.getProject(projectName);
if (!(project.exists())) {
project.create(new NullProgressMonitor());
}
project.open(new NullProgressMonitor());
IFolder folder = null;
for (int i = 1; i < path.segmentCount() - 1; i++) {
if (i == 1)
folder = project.getFolder(path.segment(i));
else
folder = folder.getFolder(path.segment(i));
if (!(folder.exists())) {
folder.create(true, true, null);
}
}
IContainer container;
if (folder == null)
container = project;
else
container = folder;
IFile file = container.getFile(ResourceUtil.getPathFromString(path.lastSegment()));
if (file.exists()) {
StringBuilder sb = new StringBuilder();
if (append) {
sb.append(FileUtil.readTextFile(filePath, widget));
}
sb.append(text);
file.setContents(new ByteArrayInputStream(sb.toString().getBytes("UTF-8")), true, false, // $NON-NLS-1$
null);
} else {
File sysFile = file.getLocation().toFile();
BufferedWriter writer = new BufferedWriter(// $NON-NLS-1$
new OutputStreamWriter(new FileOutputStream(sysFile, append), "UTF-8"));
writer.write(text);
writer.flush();
writer.close();
}
} else {
BufferedWriter writer = new BufferedWriter(// $NON-NLS-1$
new OutputStreamWriter(new FileOutputStream(path.toString(), append), "UTF-8"));
writer.write(text);
writer.flush();
writer.close();
}
}
use of org.eclipse.core.resources.IWorkspace in project yamcs-studio by yamcs.
the class ResourceAndContainerGroup method validateContainer.
/**
* Returns a <code>boolean</code> indicating whether a container name
* represents a valid container resource in the workbench. An error message
* is stored for future reference if the name does not represent a valid
* container.
*
* @return <code>boolean</code> indicating validity of the container name
*/
private boolean validateContainer() {
IPath path = _containerGroup.getFullPath();
if (path == null) {
_problemType = PROBLEM_CONTAINER_EMPTY;
_problemMessage = Messages.ResourceAndContainerGroup_PROBLEM_EMPTY;
return false;
}
IWorkspace workspace = ResourcesPlugin.getWorkspace();
String projectName = path.segment(0);
if (projectName == null || !workspace.getRoot().getProject(projectName).exists()) {
_problemType = PROBLEM_PROJECT_DOES_NOT_EXIST;
_problemMessage = Messages.ResourceAndContainerGroup_PROBLEM_DOES_NOT_EXIST;
return false;
}
// path is invalid if any prefix is occupied by a file
IWorkspaceRoot root = workspace.getRoot();
while (path.segmentCount() > 1) {
if (root.getFile(path).exists()) {
_problemType = PROBLEM_PATH_OCCUPIED;
_problemMessage = NLS.bind(Messages.ResourceAndContainerGroup_PROBLEM_FILE_ALREADY_EXISTS_AT_LOCATION, path.makeRelative());
return false;
}
path = path.removeLastSegments(1);
}
return true;
}
Aggregations