Search in sources :

Example 1 with ProjectModel

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

the class ProjectConfigurationContentHandler method getProjectModel.

public ProjectModel getProjectModel(final String projectName) {
    final ProjectModel model = new ProjectModel(projectName);
    model.setPMDEnabled(isPMDEnabled);
    model.setRuleSets(builder.build());
    return model;
}
Also used : ProjectModel(ch.acanda.eclipse.pmd.domain.ProjectModel)

Example 2 with ProjectModel

use of ch.acanda.eclipse.pmd.domain.ProjectModel 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 3 with ProjectModel

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

the class PMDPlugin method initWorkspaceModel.

private void initWorkspaceModel() {
    workspaceModel = new WorkspaceModel();
    final IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
    final ProjectModelRepository projectModelRepository = new ProjectModelRepository();
    for (final IProject project : projects) {
        final Optional<ProjectModel> model = projectModelRepository.load(project.getName());
        if (model.isPresent()) {
            workspaceModel.add(model.get());
        } else {
            workspaceModel.add(new ProjectModel(project.getName()));
        }
    }
    final IResourceChangeListener workspaceChangeListener = new WorkspaceChangeListener(workspaceModel, projectModelRepository);
    ResourcesPlugin.getWorkspace().addResourceChangeListener(workspaceChangeListener, IResourceChangeEvent.POST_CHANGE);
}
Also used : WorkspaceModel(ch.acanda.eclipse.pmd.domain.WorkspaceModel) ProjectModelRepository(ch.acanda.eclipse.pmd.repository.ProjectModelRepository) IResourceChangeListener(org.eclipse.core.resources.IResourceChangeListener) ProjectModel(ch.acanda.eclipse.pmd.domain.ProjectModel) IProject(org.eclipse.core.resources.IProject)

Example 4 with ProjectModel

use of ch.acanda.eclipse.pmd.domain.ProjectModel 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 5 with ProjectModel

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

the class ProjectModelSerializerTest method serialize.

/**
 * Verifies that {@link ProjectModelSerializer#serialize(ProjectModel)} serializes a {@link ProjectModel} correctly.
 */
@Test
public void serialize() throws SAXException, IOException {
    final ProjectModel projectModel = new ProjectModel("TestProjectName");
    projectModel.setPMDEnabled(true);
    projectModel.setRuleSets(createRuleSets());
    final String actual = new ProjectModelSerializer().serialize(projectModel);
    final String expected = createXmlConfiguration();
    assertEquals("Serialized project model", expected, actual);
    assertValid(actual);
}
Also used : ProjectModel(ch.acanda.eclipse.pmd.domain.ProjectModel) Test(org.junit.Test)

Aggregations

ProjectModel (ch.acanda.eclipse.pmd.domain.ProjectModel)17 Test (org.junit.Test)10 ByteArrayInputStream (java.io.ByteArrayInputStream)6 WorkspaceModel (ch.acanda.eclipse.pmd.domain.WorkspaceModel)5 RuleSetModel (ch.acanda.eclipse.pmd.domain.RuleSetModel)4 RuleSets (net.sourceforge.pmd.RuleSets)3 Location (ch.acanda.eclipse.pmd.domain.Location)2 IProject (org.eclipse.core.resources.IProject)2 ProjectModelRepository (ch.acanda.eclipse.pmd.repository.ProjectModelRepository)1 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)1 RuleSetNotFoundException (net.sourceforge.pmd.RuleSetNotFoundException)1 RuleSetReferenceId (net.sourceforge.pmd.RuleSetReferenceId)1 IFile (org.eclipse.core.resources.IFile)1 IResourceChangeListener (org.eclipse.core.resources.IResourceChangeListener)1 IWorkspace (org.eclipse.core.resources.IWorkspace)1 CoreException (org.eclipse.core.runtime.CoreException)1