use of org.eclipse.core.resources.IWorkspace in project eclipse-pmd by acanda.
the class ProjectModelRepository method load.
public Optional<ProjectModel> load(final String projectName) {
checkState(!Strings.isNullOrEmpty(projectName), "The argument 'projectName' must be a valid project name.");
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
final IProject project = workspace.getRoot().getProject(projectName);
final IFile configFile = project.getFile(PMD_CONFIG_FILENAME);
Optional<ProjectModel> result = Optional.absent();
if (configFile.exists()) {
try {
result = Optional.of(new ProjectModelSerializer().deserialize(configFile.getContents(true), projectName));
} catch (IOException | CoreException e) {
PMDPlugin.getDefault().error("Cannot load " + PMD_CONFIG_FILENAME + " in project " + projectName, e);
}
}
return result;
}
use of org.eclipse.core.resources.IWorkspace in project eclipse-pmd by acanda.
the class ProjectModelRepository method save.
public void save(final ProjectModel model) {
checkNotNull(model, "The argument 'model' must not be null.");
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
try {
final IProject project = workspace.getRoot().getProject(model.getProjectName());
final IFile configFile = project.getFile(PMD_CONFIG_FILENAME);
final String config = new ProjectModelSerializer().serialize(model);
final ByteArrayInputStream source = new ByteArrayInputStream(config.getBytes(ProjectModelSerializer.ENCODING));
if (configFile.exists()) {
configFile.setContents(source, true, true, null);
} else {
configFile.create(source, true, null);
}
} catch (final CoreException e) {
PMDPlugin.getDefault().error("Cannot save " + PMD_CONFIG_FILENAME + " in project " + model.getProjectName(), e);
}
}
use of org.eclipse.core.resources.IWorkspace in project eclipse-pmd by acanda.
the class AddRuleSetConfigurationModelTest method validateWorkspaceConfigurationWithProjectOutsideWorkspace.
/**
* Verifies that a rule set configuration in a project outside of the workspace does not produce an error in the
* "Add Rule Set Configuration" wizard.
*/
@Test
public void validateWorkspaceConfigurationWithProjectOutsideWorkspace() throws IOException {
final Path ruleSetFile = createRuleSetFile();
final IProject project = mock(IProject.class);
final IWorkspace workspace = mock(IWorkspace.class);
final IWorkspaceRoot root = mock(IWorkspaceRoot.class);
when(project.getWorkspace()).thenReturn(workspace);
when(workspace.getRoot()).thenReturn(root);
when(root.getProject(anyString())).thenReturn(project);
when(project.getLocationURI()).thenReturn(ruleSetFile.getParent().toUri());
final AddRuleSetConfigurationModel model = new AddRuleSetConfigurationModel(project);
model.setWorkspaceTypeSelected(true);
model.setName("X");
model.setLocation("ProjectX/" + ruleSetFile.getName(ruleSetFile.getNameCount() - 1));
final ValidationResult validationResult = new ValidationResult();
model.validate(AddRuleSetConfigurationModel.LOCATION, validationResult);
if (validationResult.hasErrors()) {
final String msg = "The validation should not result in any errors " + "if the project is located outside the workspace. First error: ";
fail(msg + validationResult.getFirstErrorMessage());
}
}
use of org.eclipse.core.resources.IWorkspace in project eclipse-pmd by acanda.
the class LocationResolverTest method resolveIfExistsWorkspaceLocationWithInvalidPath.
/**
* Verifies that {@link LocationResolver#resolveIfExists(Location, IProject)} does not throw an exception in a
* workspace context if the path contains invalid characters.
*/
@Test
public void resolveIfExistsWorkspaceLocationWithInvalidPath() throws URISyntaxException {
final Location location = new Location("project/\u0000:", 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 resolveWorkspaceLocation.
/**
* Verifies that {@link LocationResolver#resolve(Location, IProject)} resolves the location in a project context
* correctly.
*/
@Test
public void resolveWorkspaceLocation() throws URISyntaxException {
final Location location = new Location("project/path/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 String result = LocationResolver.resolve(location, project);
assertEquals("The resolved location should be the project's path with the location's path appended", Paths.get("/workspace", "project", "path", "pmd.xml"), Paths.get(result));
}
Aggregations