diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-04-18 12:15:13 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-04-18 14:09:18 +0200 |
commit | bfeca720d3a702b77664b0164aa02ce89b353cec (patch) | |
tree | e68bad3f6151ed92c89667fa7e92e55cc8487401 /sonar-batch | |
parent | 5857333fb71af4090a6b3dcb3e972d465873f98d (diff) | |
download | sonarqube-bfeca720d3a702b77664b0164aa02ce89b353cec.tar.gz sonarqube-bfeca720d3a702b77664b0164aa02ce89b353cec.zip |
SONAR-5218 Once a module has been turned into a project, its issues are no more visible in the UI
Diffstat (limited to 'sonar-batch')
8 files changed, 49 insertions, 47 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssuable.java b/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssuable.java index 62b44e7d69e..578bb2bb49e 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssuable.java +++ b/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssuable.java @@ -24,6 +24,7 @@ import org.sonar.api.component.Component; import org.sonar.api.issue.Issuable; import org.sonar.api.issue.Issue; import org.sonar.api.issue.internal.DefaultIssue; +import org.sonar.api.resources.Project; import org.sonar.core.issue.DefaultIssueBuilder; import java.util.List; @@ -36,16 +37,18 @@ public class DefaultIssuable implements Issuable { private final ModuleIssues moduleIssues; private final IssueCache cache; private final Component component; + private final Project project; - DefaultIssuable(Component component, ModuleIssues moduleIssues, IssueCache cache) { + DefaultIssuable(Component component, Project project, ModuleIssues moduleIssues, IssueCache cache) { this.component = component; + this.project = project; this.moduleIssues = moduleIssues; this.cache = cache; } @Override public IssueBuilder newIssueBuilder() { - return new DefaultIssueBuilder().componentKey(component.key()); + return new DefaultIssueBuilder().componentKey(component.key()).projectKey(project.getKey()); } @Override diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/IssuableFactory.java b/sonar-batch/src/main/java/org/sonar/batch/issue/IssuableFactory.java index 147ab7c7c15..7491bbb7814 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/IssuableFactory.java +++ b/sonar-batch/src/main/java/org/sonar/batch/issue/IssuableFactory.java @@ -22,6 +22,7 @@ package org.sonar.batch.issue; import org.sonar.api.component.Component; import org.sonar.api.issue.Issuable; import org.sonar.api.resources.Scopes; +import org.sonar.batch.ProjectTree; import org.sonar.core.component.PerspectiveBuilder; import org.sonar.core.component.ResourceComponent; @@ -35,11 +36,13 @@ public class IssuableFactory extends PerspectiveBuilder<Issuable> { private final ModuleIssues moduleIssues; private final IssueCache cache; + private final ProjectTree projectTree; - public IssuableFactory(ModuleIssues moduleIssues, IssueCache cache) { + public IssuableFactory(ModuleIssues moduleIssues, IssueCache cache, ProjectTree projectTree) { super(Issuable.class); this.moduleIssues = moduleIssues; this.cache = cache; + this.projectTree = projectTree; } @CheckForNull @@ -49,6 +52,6 @@ public class IssuableFactory extends PerspectiveBuilder<Issuable> { if (component instanceof ResourceComponent) { supported = Scopes.isHigherThanOrEquals(((ResourceComponent) component).scope(), Scopes.FILE); } - return supported ? new DefaultIssuable(component, moduleIssues, cache) : null; + return supported ? new DefaultIssuable(component, projectTree.getRootProject(), moduleIssues, cache) : null; } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/ModuleIssues.java b/sonar-batch/src/main/java/org/sonar/batch/issue/ModuleIssues.java index c0a49d68671..6f46e2cc7a9 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/ModuleIssues.java +++ b/sonar-batch/src/main/java/org/sonar/batch/issue/ModuleIssues.java @@ -67,6 +67,7 @@ public class ModuleIssues { private DefaultIssue newIssue(Violation violation) { return (DefaultIssue) new DefaultIssueBuilder() .componentKey(violation.getResource().getEffectiveKey()) + .projectKey(project.getRoot().getEffectiveKey()) .ruleKey(RuleKey.of(violation.getRule().getRepositoryKey(), violation.getRule().getKey())) .effortToFix(violation.getCost()) .line(violation.getLineId()) diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/ScanIssueStorage.java b/sonar-batch/src/main/java/org/sonar/batch/issue/ScanIssueStorage.java index 23dab80cde1..723082eb335 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/ScanIssueStorage.java +++ b/sonar-batch/src/main/java/org/sonar/batch/issue/ScanIssueStorage.java @@ -23,6 +23,7 @@ import org.sonar.api.BatchComponent; import org.sonar.api.database.model.Snapshot; import org.sonar.api.issue.internal.DefaultIssue; import org.sonar.api.rules.RuleFinder; +import org.sonar.batch.ProjectTree; import org.sonar.batch.index.SnapshotCache; import org.sonar.core.issue.db.IssueStorage; import org.sonar.core.persistence.MyBatis; @@ -34,16 +35,18 @@ public class ScanIssueStorage extends IssueStorage implements BatchComponent { private final SnapshotCache snapshotCache; private final ResourceDao resourceDao; + private final ProjectTree projectTree; - public ScanIssueStorage(MyBatis mybatis, RuleFinder ruleFinder, SnapshotCache snapshotCache, ResourceDao resourceDao) { + public ScanIssueStorage(MyBatis mybatis, RuleFinder ruleFinder, SnapshotCache snapshotCache, ResourceDao resourceDao, ProjectTree projectTree) { super(mybatis, ruleFinder); this.snapshotCache = snapshotCache; this.resourceDao = resourceDao; + this.projectTree = projectTree; } @Override protected long componentId(DefaultIssue issue) { - Snapshot snapshot = getSnapshot(issue); + Snapshot snapshot = snapshotCache.get(issue.componentKey()); if (snapshot != null) { return snapshot.getResourceId(); } @@ -58,19 +61,7 @@ public class ScanIssueStorage extends IssueStorage implements BatchComponent { @Override protected long projectId(DefaultIssue issue) { - Snapshot snapshot = getSnapshot(issue); - if (snapshot != null) { - return snapshot.getRootProjectId(); - } - throw new IllegalStateException("Project id not found for: " + issue.componentKey()); - } - - private Snapshot getSnapshot(DefaultIssue issue) { - Snapshot snapshot = snapshotCache.get(issue.componentKey()); - if (snapshot != null) { - return snapshot; - } - return null; + return projectTree.getRootProject().getId(); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/DefaultIssuableTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/DefaultIssuableTest.java index 1f557c64ad1..90721058222 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/DefaultIssuableTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/DefaultIssuableTest.java @@ -23,6 +23,7 @@ import org.junit.Test; import org.sonar.api.component.Component; import org.sonar.api.issue.Issue; import org.sonar.api.issue.internal.DefaultIssue; +import org.sonar.api.resources.Project; import java.util.Arrays; import java.util.List; @@ -35,6 +36,7 @@ public class DefaultIssuableTest { ModuleIssues moduleIssues = mock(ModuleIssues.class); IssueCache cache = mock(IssueCache.class); + Project project = mock(Project.class); Component component = mock(Component.class); @Test @@ -44,7 +46,7 @@ public class DefaultIssuableTest { DefaultIssue unresolved = new DefaultIssue(); when(cache.byComponent("struts:org.apache.Action")).thenReturn(Arrays.asList(resolved, unresolved)); - DefaultIssuable perspective = new DefaultIssuable(component, moduleIssues, cache); + DefaultIssuable perspective = new DefaultIssuable(component, project, moduleIssues, cache); List<Issue> issues = perspective.issues(); assertThat(issues).containsOnly(unresolved); @@ -57,7 +59,7 @@ public class DefaultIssuableTest { DefaultIssue unresolved = new DefaultIssue(); when(cache.byComponent("struts:org.apache.Action")).thenReturn(Arrays.asList(resolved, unresolved)); - DefaultIssuable perspective = new DefaultIssuable(component, moduleIssues, cache); + DefaultIssuable perspective = new DefaultIssuable(component, project, moduleIssues, cache); List<Issue> issues = perspective.resolvedIssues(); assertThat(issues).containsOnly(resolved); diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/IssuableFactoryTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/IssuableFactoryTest.java index d11d0a72111..4753023ccbc 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/IssuableFactoryTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/IssuableFactoryTest.java @@ -25,6 +25,7 @@ import org.sonar.api.component.Component; import org.sonar.api.issue.Issuable; import org.sonar.api.resources.File; import org.sonar.api.resources.Project; +import org.sonar.batch.ProjectTree; import org.sonar.core.component.ResourceComponent; import static org.fest.assertions.Assertions.assertThat; @@ -34,10 +35,11 @@ public class IssuableFactoryTest { ModuleIssues moduleIssues = mock(ModuleIssues.class); IssueCache cache = mock(IssueCache.class, Mockito.RETURNS_MOCKS); + ProjectTree projectTree = mock(ProjectTree.class); @Test public void file_should_be_issuable() throws Exception { - IssuableFactory factory = new IssuableFactory(moduleIssues, cache); + IssuableFactory factory = new IssuableFactory(moduleIssues, cache, projectTree); Component component = new ResourceComponent(new File("foo/bar.c").setEffectiveKey("foo/bar.c")); Issuable issuable = factory.loadPerspective(Issuable.class, component); @@ -48,7 +50,7 @@ public class IssuableFactoryTest { @Test public void project_should_be_issuable() throws Exception { - IssuableFactory factory = new IssuableFactory(moduleIssues, cache); + IssuableFactory factory = new IssuableFactory(moduleIssues, cache, projectTree); Component component = new ResourceComponent(new Project("Foo").setEffectiveKey("foo")); Issuable issuable = factory.loadPerspective(Issuable.class, component); diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/ModuleIssuesTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/ModuleIssuesTest.java index 3df87485b74..bacfe55be16 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/ModuleIssuesTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/ModuleIssuesTest.java @@ -73,6 +73,7 @@ public class ModuleIssuesTest { public void setUp() { when(project.getAnalysisDate()).thenReturn(new Date()); when(project.getEffectiveKey()).thenReturn("org.apache:struts-core"); + when(project.getRoot()).thenReturn(project); } @Test @@ -225,6 +226,7 @@ public class ModuleIssuesTest { assertThat(issue.key()).isNotEmpty(); assertThat(issue.ruleKey().toString()).isEqualTo("squid:AvoidCycle"); assertThat(issue.componentKey()).isEqualTo("struts:src/org/struts/Action.java"); + assertThat(issue.projectKey()).isEqualTo("org.apache:struts-core"); } @Test diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/ScanIssueStorageTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/ScanIssueStorageTest.java index 7875ae97f25..8866851b3da 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/ScanIssueStorageTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/ScanIssueStorageTest.java @@ -19,13 +19,19 @@ */ package org.sonar.batch.issue; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; import org.sonar.api.database.model.Snapshot; import org.sonar.api.issue.internal.DefaultIssue; +import org.sonar.api.resources.Project; import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.Rule; import org.sonar.api.rules.RuleFinder; import org.sonar.api.rules.RuleQuery; +import org.sonar.batch.ProjectTree; import org.sonar.batch.index.SnapshotCache; import org.sonar.core.persistence.AbstractDaoTestCase; import org.sonar.core.resource.ResourceDao; @@ -34,16 +40,28 @@ import java.util.Collection; import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Fail.fail; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +@RunWith(MockitoJUnitRunner.class) public class ScanIssueStorageTest extends AbstractDaoTestCase { + + @Mock + SnapshotCache snapshotCache; + + @Mock + ProjectTree projectTree; + + ScanIssueStorage storage; + + @Before + public void setUp() throws Exception { + storage = new ScanIssueStorage(getMyBatis(), new FakeRuleFinder(), snapshotCache, new ResourceDao(getMyBatis()), projectTree); + } + @Test public void should_load_component_id_from_cache() throws Exception { - SnapshotCache snapshotCache = mock(SnapshotCache.class); when(snapshotCache.get("struts:Action.java")).thenReturn(new Snapshot().setResourceId(123)); - ScanIssueStorage storage = new ScanIssueStorage(getMyBatis(), new FakeRuleFinder(), snapshotCache, new ResourceDao(getMyBatis())); long componentId = storage.componentId(new DefaultIssue().setComponentKey("struts:Action.java")); assertThat(componentId).isEqualTo(123); @@ -52,10 +70,8 @@ public class ScanIssueStorageTest extends AbstractDaoTestCase { @Test public void should_load_component_id_from_db() throws Exception { setupData("should_load_component_id_from_db"); - SnapshotCache snapshotCache = mock(SnapshotCache.class); when(snapshotCache.get("struts:Action.java")).thenReturn(null); - ScanIssueStorage storage = new ScanIssueStorage(getMyBatis(), new FakeRuleFinder(), snapshotCache, new ResourceDao(getMyBatis())); long componentId = storage.componentId(new DefaultIssue().setComponentKey("struts:Action.java")); assertThat(componentId).isEqualTo(123); @@ -64,10 +80,8 @@ public class ScanIssueStorageTest extends AbstractDaoTestCase { @Test public void should_fail_to_load_component_id_if_unknown_component() throws Exception { setupData("should_fail_to_load_component_id_if_unknown_component"); - SnapshotCache snapshotCache = mock(SnapshotCache.class); when(snapshotCache.get("struts:Action.java")).thenReturn(null); - ScanIssueStorage storage = new ScanIssueStorage(getMyBatis(), new FakeRuleFinder(), snapshotCache, new ResourceDao(getMyBatis())); try { storage.componentId(new DefaultIssue().setComponentKey("struts:Action.java")); fail(); @@ -78,29 +92,13 @@ public class ScanIssueStorageTest extends AbstractDaoTestCase { @Test public void should_load_project_id() throws Exception { - SnapshotCache snapshotCache = mock(SnapshotCache.class); - when(snapshotCache.get("struts:Action.java")).thenReturn(new Snapshot().setResourceId(123).setRootProjectId(100)); + when(projectTree.getRootProject()).thenReturn((Project) new Project("struts").setId(100)); - ScanIssueStorage storage = new ScanIssueStorage(getMyBatis(), new FakeRuleFinder(), snapshotCache, new ResourceDao(getMyBatis())); long projectId = storage.projectId(new DefaultIssue().setComponentKey("struts:Action.java")); assertThat(projectId).isEqualTo(100); } - @Test - public void should_fail_to_load_project_id_if_unknown_component() throws Exception { - SnapshotCache snapshotCache = mock(SnapshotCache.class); - when(snapshotCache.get("struts:Action.java")).thenReturn(null); - - ScanIssueStorage storage = new ScanIssueStorage(getMyBatis(), new FakeRuleFinder(), snapshotCache, new ResourceDao(getMyBatis())); - try { - storage.projectId(new DefaultIssue().setComponentKey("struts:Action.java")); - fail(); - } catch (Exception e) { - assertThat(e).hasMessage("Project id not found for: struts:Action.java"); - } - } - static class FakeRuleFinder implements RuleFinder { @Override |