Search in sources :

Example 11 with Location

use of ch.acanda.eclipse.pmd.domain.Location 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());
}
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 12 with Location

use of ch.acanda.eclipse.pmd.domain.Location in project eclipse-pmd by acanda.

the class LocationResolverTest method resolveRemoteLocation.

/**
 * Verifies that {@link LocationResolver#resolve(Location, IProject)} resolves the location in a remote context
 * correctly.
 */
@Test
public void resolveRemoteLocation() {
    final Location location = new Location("http://example.org/pmd.xml", LocationContext.FILE_SYSTEM);
    final IProject project = mock(IProject.class);
    final String result = LocationResolver.resolve(location, project);
    assertEquals("The resolved location should be the URL", "http://example.org/pmd.xml", result);
}
Also used : IProject(org.eclipse.core.resources.IProject) Location(ch.acanda.eclipse.pmd.domain.Location) Test(org.junit.Test)

Example 13 with Location

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

Example 14 with Location

use of ch.acanda.eclipse.pmd.domain.Location in project eclipse-pmd by acanda.

the class V07ToV08Converter method moveProjectSettings.

private static void moveProjectSettings(final IProject project, final ImmutableList<RuleSetConfiguration> configs, final ProjectModelRepository repository) {
    final PMDProjectSettings pmdProjectSettings = new PMDProjectSettings(project);
    final Set<RuleSetConfiguration> activeConfigs = pmdProjectSettings.getActiveRuleSetConfigurations(configs);
    if (!activeConfigs.isEmpty()) {
        final ProjectModel projectModel = new ProjectModel(project.getName());
        projectModel.setPMDEnabled(pmdProjectSettings.isPMDEnabled());
        final List<RuleSetModel> ruleSets = new ArrayList<>(activeConfigs.size());
        for (final RuleSetConfiguration config : activeConfigs) {
            final Location location = getLocation(config, project.getWorkspace().getRoot());
            final RuleSetModel ruleSetModel = new RuleSetModel(config.getName(), location);
            ruleSets.add(ruleSetModel);
        }
        projectModel.setRuleSets(ruleSets);
        repository.save(projectModel);
    }
    if (repository.load(project.getName()).isPresent()) {
        pmdProjectSettings.deleteSettings();
    }
}
Also used : RuleSetModel(ch.acanda.eclipse.pmd.domain.RuleSetModel) ArrayList(java.util.ArrayList) ProjectModel(ch.acanda.eclipse.pmd.domain.ProjectModel) Location(ch.acanda.eclipse.pmd.domain.Location)

Example 15 with Location

use of ch.acanda.eclipse.pmd.domain.Location in project eclipse-pmd by acanda.

the class ProjectConfigurationContentHandler method createRuleSet.

private RuleSetModel createRuleSet(final Attributes attributes) {
    final LocationContext context = getContext(attributes.getValue(ATTRIBUTE_NAME_REFCONTEXT));
    final Location location = new Location(attributes.getValue(ATTRIBUTE_NAME_REF), context);
    return new RuleSetModel(attributes.getValue(ATTRIBUTE_NAME_NAME), location);
}
Also used : RuleSetModel(ch.acanda.eclipse.pmd.domain.RuleSetModel) LocationContext(ch.acanda.eclipse.pmd.domain.LocationContext) Location(ch.acanda.eclipse.pmd.domain.Location)

Aggregations

Location (ch.acanda.eclipse.pmd.domain.Location)28 Test (org.junit.Test)24 IProject (org.eclipse.core.resources.IProject)19 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)10 URI (java.net.URI)8 IWorkspace (org.eclipse.core.resources.IWorkspace)7 RuleSetModel (ch.acanda.eclipse.pmd.domain.RuleSetModel)5 Path (java.nio.file.Path)4 LocationContext (ch.acanda.eclipse.pmd.domain.LocationContext)3 ProjectModel (ch.acanda.eclipse.pmd.domain.ProjectModel)2 WorkspaceModel (ch.acanda.eclipse.pmd.domain.WorkspaceModel)2 RuleSets (net.sourceforge.pmd.RuleSets)2 ArrayList (java.util.ArrayList)1