Search in sources :

Example 61 with IWorkspace

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;
}
Also used : IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) IWorkspace(org.eclipse.core.resources.IWorkspace) IOException(java.io.IOException) ProjectModel(ch.acanda.eclipse.pmd.domain.ProjectModel) IProject(org.eclipse.core.resources.IProject)

Example 62 with IWorkspace

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);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) IWorkspace(org.eclipse.core.resources.IWorkspace) IProject(org.eclipse.core.resources.IProject)

Example 63 with IWorkspace

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());
    }
}
Also used : Path(java.nio.file.Path) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IWorkspace(org.eclipse.core.resources.IWorkspace) Matchers.anyString(org.mockito.Matchers.anyString) ValidationResult(ch.acanda.eclipse.pmd.ui.model.ValidationResult) IProject(org.eclipse.core.resources.IProject) Test(org.junit.Test)

Example 64 with IWorkspace

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());
}
Also used : IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IWorkspace(org.eclipse.core.resources.IWorkspace) URI(java.net.URI) IProject(org.eclipse.core.resources.IProject) Location(ch.acanda.eclipse.pmd.domain.Location) Test(org.junit.Test)

Example 65 with IWorkspace

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));
}
Also used : IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IWorkspace(org.eclipse.core.resources.IWorkspace) URI(java.net.URI) IProject(org.eclipse.core.resources.IProject) Location(ch.acanda.eclipse.pmd.domain.Location) Test(org.junit.Test)

Aggregations

IWorkspace (org.eclipse.core.resources.IWorkspace)118 IProject (org.eclipse.core.resources.IProject)55 CoreException (org.eclipse.core.runtime.CoreException)54 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)37 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)36 IPath (org.eclipse.core.runtime.IPath)35 IWorkspaceRunnable (org.eclipse.core.resources.IWorkspaceRunnable)29 IStatus (org.eclipse.core.runtime.IStatus)27 IFile (org.eclipse.core.resources.IFile)24 PersistenceException (org.talend.commons.exception.PersistenceException)23 IProjectDescription (org.eclipse.core.resources.IProjectDescription)21 File (java.io.File)19 InvocationTargetException (java.lang.reflect.InvocationTargetException)19 Path (org.eclipse.core.runtime.Path)19 ISchedulingRule (org.eclipse.core.runtime.jobs.ISchedulingRule)19 IResource (org.eclipse.core.resources.IResource)18 Status (org.eclipse.core.runtime.Status)14 IOException (java.io.IOException)13 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)13 ArrayList (java.util.ArrayList)12