Search in sources :

Example 6 with Location

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

the class RuleSetsCacheTest method secondGetLoadsWhenLaterAddedProjectRuleSetsWereChanged.

/**
 * Verifies that the second cache access loads the rule sets if the project model's rule sets have been changed
 * after the first access. In this case, the project model was added after the rule sets cache was created.
 */
@Test
public void secondGetLoadsWhenLaterAddedProjectRuleSetsWereChanged() throws Exception {
    final WorkspaceModel workspaceModel = getWorkspaceModel();
    final RuleSetsCache cache = new RuleSetsCache(getCacheLoaderMock(), workspaceModel);
    workspaceModel.add(new ProjectModel(PROJECT_NAME_2));
    cache.getRuleSets(PROJECT_NAME_2);
    final RuleSetModel ruleSetModel = new RuleSetModel("abc", new Location("path", LocationContext.WORKSPACE));
    workspaceModel.getOrCreateProject(PROJECT_NAME_2).setRuleSets(Arrays.asList(ruleSetModel));
    final RuleSets actualRuleSets = cache.getRuleSets(PROJECT_NAME_2);
    assertSame("Second cache access should reload rule sets", RULE_SETS_BAR_2, actualRuleSets);
}
Also used : WorkspaceModel(ch.acanda.eclipse.pmd.domain.WorkspaceModel) RuleSetModel(ch.acanda.eclipse.pmd.domain.RuleSetModel) RuleSets(net.sourceforge.pmd.RuleSets) ProjectModel(ch.acanda.eclipse.pmd.domain.ProjectModel) Location(ch.acanda.eclipse.pmd.domain.Location) Test(org.junit.Test)

Example 7 with Location

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

the class LocationResolverTest method resolveIfExistsRemoteLocationWithMissingFile.

/**
 * Verifies that {@link LocationResolver#resolveIfExists(Location, IProject)} does not resolve the location in a
 * remote context when the file does not exist.
 */
@Test
public void resolveIfExistsRemoteLocationWithMissingFile() {
    final Location location = new Location("http://example.org/pmd.xml", LocationContext.REMOTE);
    final IProject project = mock(IProject.class);
    final Optional<String> result = LocationResolver.resolveIfExists(location, project);
    assertFalse("The location should not resolve", result.isPresent());
}
Also used : IProject(org.eclipse.core.resources.IProject) Location(ch.acanda.eclipse.pmd.domain.Location) Test(org.junit.Test)

Example 8 with Location

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

the class LocationResolverTest method resolveIfExistsProjectLocationWithInvalidPath.

/**
 * Verifies that {@link LocationResolver#resolveIfExists(Location, IProject)} does not throw an exception in a
 * project context if the path is invalid.
 */
@Test
public void resolveIfExistsProjectLocationWithInvalidPath() throws URISyntaxException {
    final Location location = new Location("\u0000:", LocationContext.PROJECT);
    final IProject project = mock(IProject.class);
    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 : URI(java.net.URI) IProject(org.eclipse.core.resources.IProject) Location(ch.acanda.eclipse.pmd.domain.Location) Test(org.junit.Test)

Example 9 with Location

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

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

the class LocationResolverTest method resolveIfExistsProjectLocation.

/**
 * Verifies that {@link LocationResolver#resolveIfExists(Location, IProject)} resolves the location in a project
 * context correctly.
 */
@Test
public void resolveIfExistsProjectLocation() throws URISyntaxException, IOException {
    final Path ruleSetFile = Files.createTempFile(LocationResolverTest.class.getSimpleName(), ".xml");
    try {
        final Location location = new Location(ruleSetFile.getFileName().toString(), LocationContext.PROJECT);
        final IProject project = mock(IProject.class);
        when(project.getLocationURI()).thenReturn(ruleSetFile.getParent().toUri());
        final Optional<String> result = LocationResolver.resolveIfExists(location, project);
        assertTrue("A valid project location should resolve", result.isPresent());
        assertEquals("The resolved location in a project context should be the provided location appended to the project location", ruleSetFile.toString(), result.get());
    } finally {
        Files.deleteIfExists(ruleSetFile);
    }
}
Also used : Path(java.nio.file.Path) IProject(org.eclipse.core.resources.IProject) Location(ch.acanda.eclipse.pmd.domain.Location) Test(org.junit.Test)

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