Browse Source

ScanPersister implementations can load snapshots through SnapshotCache

tags/3.6
Simon Brandhof 11 years ago
parent
commit
619d71c598

+ 13
- 26
sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java View File

@@ -31,18 +31,9 @@ import org.sonar.api.batch.SonarIndex;
import org.sonar.api.database.model.ResourceModel;
import org.sonar.api.database.model.Snapshot;
import org.sonar.api.design.Dependency;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.MeasuresFilter;
import org.sonar.api.measures.MeasuresFilters;
import org.sonar.api.measures.Metric;
import org.sonar.api.measures.MetricFinder;
import org.sonar.api.measures.*;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.ProjectLink;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Resource;
import org.sonar.api.resources.ResourceUtils;
import org.sonar.api.resources.Scopes;
import org.sonar.api.resources.*;
import org.sonar.api.rules.ActiveRule;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
@@ -56,14 +47,7 @@ import org.sonar.batch.ViolationFilters;
import org.sonar.batch.issue.DeprecatedViolations;
import org.sonar.core.component.ScanGraph;

import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

public class DefaultIndex extends SonarIndex {

@@ -75,6 +59,7 @@ public class DefaultIndex extends SonarIndex {
private MetricFinder metricFinder;
private RuleFinder ruleFinder;
private ScanGraph graph;
private SnapshotCache snapshotCache;

// filters
private ViolationFilters violationFilters;
@@ -90,7 +75,7 @@ public class DefaultIndex extends SonarIndex {
private final DeprecatedViolations deprecatedViolations;

public DefaultIndex(PersistenceManager persistence, DefaultResourceCreationLock lock, ProjectTree projectTree, MetricFinder metricFinder,
RuleFinder ruleFinder, ScanGraph graph, DeprecatedViolations deprecatedViolations) {
RuleFinder ruleFinder, ScanGraph graph, DeprecatedViolations deprecatedViolations, SnapshotCache snapshotCache) {
this.persistence = persistence;
this.lock = lock;
this.projectTree = projectTree;
@@ -98,6 +83,7 @@ public class DefaultIndex extends SonarIndex {
this.ruleFinder = ruleFinder;
this.graph = graph;
this.deprecatedViolations = deprecatedViolations;
this.snapshotCache = snapshotCache;
}

public void start() {
@@ -366,7 +352,7 @@ public class DefaultIndex extends SonarIndex {
return;
}

if (rule.getId()==null) {
if (rule.getId() == null) {
Rule persistedRule = ruleFinder.findByKey(rule.getRepositoryKey(), rule.getKey());
if (persistedRule == null) {
LOG.warn("Rule does not exist. Ignoring violation {}", violation);
@@ -380,6 +366,7 @@ public class DefaultIndex extends SonarIndex {
return;
}

deprecatedViolations.add(violation);
addViolation(violation, bucket, force);
}

@@ -389,7 +376,6 @@ public class DefaultIndex extends SonarIndex {
return;
}

deprecatedViolations.add(violation);
// TODO this code is not the responsibility of this index. It should be moved somewhere else.
if (!violation.isManual()) {
ActiveRule activeRule = profile.getActiveRule(violation.getRule());
@@ -489,10 +475,10 @@ public class DefaultIndex extends SonarIndex {
if (!StringUtils.equals(Scopes.PROJECT, resource.getScope())) {
// not a project nor a library
uid = new StringBuilder(ResourceModel.KEY_SIZE)
.append(project.getKey())
.append(':')
.append(resource.getKey())
.toString();
.append(project.getKey())
.append(':')
.append(resource.getKey())
.toString();
}
return uid;
}
@@ -578,6 +564,7 @@ public class DefaultIndex extends SonarIndex {
Snapshot snapshot = persistence.saveResource(currentProject, resource, (parentBucket != null ? parentBucket.getResource() : null));
if (ResourceUtils.isPersistable(resource) && !Qualifiers.LIBRARY.equals(resource.getQualifier())) {
graph.addComponent(resource, snapshot);
snapshotCache.put(resource.getEffectiveKey(), snapshot);
}
}


sonar-batch/src/main/java/org/sonar/batch/phases/ScanPersister.java → sonar-batch/src/main/java/org/sonar/batch/index/ScanPersister.java View File

@@ -17,7 +17,7 @@
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.batch.phases;
package org.sonar.batch.index;

import org.sonar.api.BatchComponent;


+ 42
- 0
sonar-batch/src/main/java/org/sonar/batch/index/SnapshotCache.java View File

@@ -0,0 +1,42 @@
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2008-2012 SonarSource
* mailto:contact AT sonarsource DOT com
*
* Sonar is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.batch.index;

import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import org.sonar.api.BatchComponent;
import org.sonar.api.database.model.Snapshot;

import java.util.Map;

public class SnapshotCache implements BatchComponent {
// snapshots by component key
private final Map<String, Snapshot> snapshots = Maps.newHashMap();

public Snapshot get(String componentKey) {
return snapshots.get(componentKey);
}

public SnapshotCache put(String componentKey, Snapshot snapshot) {
Preconditions.checkState(!snapshots.containsKey(componentKey), "Component is already registered: " + componentKey);
snapshots.put(componentKey, snapshot);
return this;
}
}

+ 1
- 1
sonar-batch/src/main/java/org/sonar/batch/issue/IssuePersister.java View File

@@ -20,7 +20,7 @@
package org.sonar.batch.issue;

import org.sonar.api.issue.Issue;
import org.sonar.batch.phases.ScanPersister;
import org.sonar.batch.index.ScanPersister;
import org.sonar.core.issue.IssueDao;

public class IssuePersister implements ScanPersister {

+ 0
- 7
sonar-batch/src/main/java/org/sonar/batch/issue/ScanIssuable.java View File

@@ -22,7 +22,6 @@ package org.sonar.batch.issue;
import org.sonar.api.component.Component;
import org.sonar.api.issue.Issuable;
import org.sonar.api.issue.Issue;
import org.sonar.api.issue.IssueAction;
import org.sonar.core.issue.DefaultIssueBuilder;

import java.util.Collection;
@@ -35,7 +34,6 @@ public class ScanIssuable implements Issuable {
private final ModuleIssues moduleIssues;
private final Component component;


ScanIssuable(Component component, ModuleIssues moduleIssues) {
this.component = component;
this.moduleIssues = moduleIssues;
@@ -46,11 +44,6 @@ public class ScanIssuable implements Issuable {
return new DefaultIssueBuilder(moduleIssues, component.key());
}

@Override
public Issue apply(Issue issue, IssueAction action) {
return null;
}

@Override
public Collection<Issue> issues() {
return moduleIssues.issues(component.key());

+ 1
- 0
sonar-batch/src/main/java/org/sonar/batch/phases/GraphPersister.java View File

@@ -23,6 +23,7 @@ import com.tinkerpop.blueprints.Graph;
import org.apache.commons.io.IOUtils;
import org.slf4j.LoggerFactory;
import org.sonar.api.component.Perspective;
import org.sonar.batch.index.ScanPersister;
import org.sonar.core.component.ComponentVertex;
import org.sonar.core.component.GraphPerspectiveBuilder;
import org.sonar.core.component.PerspectiveBuilder;

+ 1
- 0
sonar-batch/src/main/java/org/sonar/batch/phases/PhaseExecutor.java View File

@@ -25,6 +25,7 @@ import org.sonar.api.resources.Project;
import org.sonar.batch.events.EventBus;
import org.sonar.batch.index.DefaultIndex;
import org.sonar.batch.index.PersistenceManager;
import org.sonar.batch.index.ScanPersister;
import org.sonar.batch.scan.filesystem.FileSystemLogger;
import org.sonar.batch.scan.maven.MavenPhaseExecutor;
import org.sonar.batch.scan.maven.MavenPluginsConfigurator;

+ 1
- 0
sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java View File

@@ -75,6 +75,7 @@ public class ProjectScanContainer extends ComponentContainer {
DefaultFileLinesContextFactory.class,
ProjectLock.class,
LastSnapshots.class,
SnapshotCache.class,

DeprecatedViolations.class,
IssueCache.class,

+ 1
- 1
sonar-batch/src/main/java/org/sonar/batch/scan/source/SyntaxHighlightingPersister.java View File

@@ -20,7 +20,7 @@

package org.sonar.batch.scan.source;

import org.sonar.batch.phases.ScanPersister;
import org.sonar.batch.index.ScanPersister;
import org.sonar.core.source.jdbc.SnapshotDataDao;

public class SyntaxHighlightingPersister implements ScanPersister {

+ 36
- 0
sonar-batch/src/main/java/org/sonar/batch/source/SyntaxPersister.java View File

@@ -0,0 +1,36 @@
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2008-2012 SonarSource
* mailto:contact AT sonarsource DOT com
*
* Sonar is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.batch.source;

import org.sonar.batch.index.ScanPersister;
import org.sonar.batch.index.SnapshotCache;

public class SyntaxPersister implements ScanPersister {
private final SnapshotCache snapshots;

public SyntaxPersister(SnapshotCache snapshots) {
this.snapshots = snapshots;
}

@Override
public void persist() {

}
}

+ 1
- 1
sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java View File

@@ -73,7 +73,7 @@ public class DefaultIndexTest {
when(metricFinder.findByKey("ncloc")).thenReturn(CoreMetrics.NCLOC);
ruleFinder = mock(RuleFinder.class);

index = new DefaultIndex(mock(PersistenceManager.class), lock, mock(ProjectTree.class), metricFinder, ruleFinder, mock(ScanGraph.class), mock(DeprecatedViolations.class));
index = new DefaultIndex(mock(PersistenceManager.class), lock, mock(ProjectTree.class), metricFinder, ruleFinder, mock(ScanGraph.class), mock(DeprecatedViolations.class), mock(SnapshotCache.class));
Project project = new Project("project");

ResourceFilter filter = new ResourceFilter() {

+ 54
- 0
sonar-batch/src/test/java/org/sonar/batch/index/SnapshotCacheTest.java View File

@@ -0,0 +1,54 @@
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2008-2012 SonarSource
* mailto:contact AT sonarsource DOT com
*
* Sonar is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.batch.index;

import org.junit.Test;
import org.sonar.api.database.model.Snapshot;

import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.Fail.fail;
import static org.mockito.Mockito.mock;

public class SnapshotCacheTest {

private Snapshot snapshot = mock(Snapshot.class);

@Test
public void should_cache_snapshots() throws Exception {
SnapshotCache cache = new SnapshotCache();
String componentKey = "org.apache.struts:struts-core";
cache.put(componentKey, snapshot);
assertThat(cache.get(componentKey)).isSameAs(snapshot);
assertThat(cache.get("other")).isNull();
}

@Test
public void should_fail_if_put_twice() throws Exception {
SnapshotCache cache = new SnapshotCache();
String componentKey = "org.apache.struts:struts-core";
cache.put(componentKey, snapshot);
try {
cache.put(componentKey, mock(Snapshot.class));
fail();
} catch (IllegalStateException e) {
// success
}
}
}

+ 37
- 0
sonar-batch/src/test/java/org/sonar/batch/source/SyntaxPersisterTest.java View File

@@ -0,0 +1,37 @@
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2008-2012 SonarSource
* mailto:contact AT sonarsource DOT com
*
* Sonar is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.batch.source;

import org.junit.Test;
import org.sonar.batch.index.SnapshotCache;

import static org.mockito.Mockito.mock;

public class SyntaxPersisterTest {

SnapshotCache snapshots = mock(SnapshotCache.class);

@Test
public void should_persist() throws Exception {
SyntaxPersister persister = new SyntaxPersister(snapshots);
persister.persist();
// TODO
}
}

+ 0
- 2
sonar-plugin-api/src/main/java/org/sonar/api/issue/Issuable.java View File

@@ -50,8 +50,6 @@ public interface Issuable extends Perspective {

IssueBuilder newIssue();

Issue apply(Issue issue, IssueAction action);

Collection<Issue> issues();

@Override

+ 37
- 0
sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueActions.java View File

@@ -0,0 +1,37 @@
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2008-2012 SonarSource
* mailto:contact AT sonarsource DOT com
*
* Sonar is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.api.issue;

import org.sonar.api.BatchComponent;
import org.sonar.api.ServerComponent;

import javax.annotation.Nullable;

public interface IssueActions extends BatchComponent, ServerComponent {

IssueActions comment(Issue issue, String userLogin, String comment);
IssueActions setSeverity(Issue issue, String severity);
IssueActions setMessage(Issue issue, String message);
IssueActions setCost(Issue issue, @Nullable Double cost);
IssueActions setResolution(Issue issue, String resolution);
IssueActions assign(Issue issue, String userLogin);
IssueActions setAttribute(Issue issue, String key, @Nullable String value);

}

sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueAction.java → sonar-plugin-api/src/main/java/org/sonar/api/issue/NewIssueHandler.java View File

@@ -17,12 +17,20 @@
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/

package org.sonar.api.issue;

import org.sonar.api.BatchExtension;

/**
* @since 3.6
* Observe issues considered as new during project scan. Note that it does not observe manual
* issues created by end-users
*/
public interface IssueAction {
// TO BE DEFINED
}
public interface NewIssueHandler extends BatchExtension {

interface NewIssueEvent {
Issue issue();
}

void onNewIssue(NewIssueEvent event);

}

Loading…
Cancel
Save