diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-06-05 07:47:15 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-06-05 07:47:15 +0200 |
commit | ea06bab235def20efc3963a94ee2630bde15b116 (patch) | |
tree | c4413878e5b2e6382470813de77f88553b152463 /sonar-batch | |
parent | 0a9ca248ec060224202a69427483d93c901b53f4 (diff) | |
download | sonarqube-ea06bab235def20efc3963a94ee2630bde15b116.tar.gz sonarqube-ea06bab235def20efc3963a94ee2630bde15b116.zip |
SONAR-4310
Diffstat (limited to 'sonar-batch')
7 files changed, 30 insertions, 27 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java index 015d0799fb0..650462ca6ba 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java @@ -54,8 +54,6 @@ public class DefaultIndex extends SonarIndex { private DefaultResourceCreationLock lock; private MetricFinder metricFinder; private final ScanGraph graph; - private final SnapshotCache snapshotCache; - private final ResourceCache resourceCache; // filters private ResourceFilters resourceFilters; @@ -71,14 +69,12 @@ public class DefaultIndex extends SonarIndex { private ScanIssues scanIssues; public DefaultIndex(PersistenceManager persistence, DefaultResourceCreationLock lock, ProjectTree projectTree, MetricFinder metricFinder, - ScanGraph graph, SnapshotCache snapshotCache, ResourceCache resourceCache, DeprecatedViolations deprecatedViolations) { + ScanGraph graph, DeprecatedViolations deprecatedViolations) { this.persistence = persistence; this.lock = lock; this.projectTree = projectTree; this.metricFinder = metricFinder; this.graph = graph; - this.snapshotCache = snapshotCache; - this.resourceCache = resourceCache; this.deprecatedViolations = deprecatedViolations; } @@ -542,8 +538,6 @@ 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); - resourceCache.add(resource); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java index d4e4ac5c740..625d6480aa2 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java @@ -44,14 +44,17 @@ import java.util.Map; public final class DefaultResourcePersister implements ResourcePersister { - private DatabaseSession session; + private final DatabaseSession session; + private final Map<Resource, Snapshot> snapshotsByResource = Maps.newHashMap(); + private final ResourcePermissions permissions; + private final SnapshotCache snapshotCache; + private final ResourceCache resourceCache; - private Map<Resource, Snapshot> snapshotsByResource = Maps.newHashMap(); - private ResourcePermissions permissions; - - public DefaultResourcePersister(DatabaseSession session, ResourcePermissions permissions) { + public DefaultResourcePersister(DatabaseSession session, ResourcePermissions permissions, SnapshotCache snapshotCache, ResourceCache resourceCache) { this.session = session; this.permissions = permissions; + this.snapshotCache = snapshotCache; + this.resourceCache = resourceCache; } public Snapshot saveProject(Project project, Project parent) { @@ -66,6 +69,8 @@ public final class DefaultResourcePersister implements ResourcePersister { private void addToCache(Resource resource, Snapshot snapshot) { if (snapshot != null) { snapshotsByResource.put(resource, snapshot); + resourceCache.add(resource); + snapshotCache.put(resource.getEffectiveKey(), snapshot); } } 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 72adc7c24b2..aef8a0a98c5 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 @@ -62,7 +62,7 @@ public class ScanIssueStorage extends IssueStorage implements BatchComponent { if (snapshot != null) { return snapshot.getRootProjectId(); } - throw new IllegalStateException("Unknown component: " + issue.componentKey()); + throw new IllegalStateException("Project id not found for: " + issue.componentKey()); } private Snapshot getSnapshot(DefaultIssue issue) { diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java index 598b73750ae..cef290c6f00 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultIndexTest.java @@ -63,7 +63,7 @@ public class DefaultIndexTest { ruleFinder = mock(RuleFinder.class); index = new DefaultIndex(mock(PersistenceManager.class), lock, mock(ProjectTree.class), metricFinder, mock(ScanGraph.class), - mock(SnapshotCache.class), mock(ResourceCache.class), mock(DeprecatedViolations.class)); + mock(DeprecatedViolations.class)); Project project = new Project("project"); ResourceFilter filter = new ResourceFilter() { diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java index 460b79d94fd..a7a0273e756 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java @@ -44,7 +44,9 @@ import static org.mockito.Mockito.when; public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { - private Project singleProject, singleCopyProject, multiModuleProject, moduleA, moduleB, moduleB1; + Project singleProject, singleCopyProject, multiModuleProject, moduleA, moduleB, moduleB1; + SnapshotCache snapshotCache = mock(SnapshotCache.class); + ResourceCache resourceCache = mock(ResourceCache.class); @Before public void before() throws ParseException { @@ -75,7 +77,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { public void shouldSaveNewProject() { setupData("shared"); - ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class)); + ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class), snapshotCache, resourceCache); persister.saveProject(singleProject, null); checkTables("shouldSaveNewProject", new String[] {"build_date", "created_at"}, "projects", "snapshots"); @@ -89,7 +91,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { public void shouldSaveCopyProject() { setupData("shared"); - ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class)); + ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class), snapshotCache, resourceCache); persister.saveProject(singleCopyProject, null); checkTables("shouldSaveCopyProject", new String[] {"build_date", "created_at"}, "projects", "snapshots"); @@ -99,7 +101,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { public void shouldSaveNewMultiModulesProject() { setupData("shared"); - ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class)); + ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class), snapshotCache, resourceCache); persister.saveProject(multiModuleProject, null); persister.saveProject(moduleA, multiModuleProject); persister.saveProject(moduleB, multiModuleProject); @@ -112,7 +114,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { public void shouldSaveNewDirectory() { setupData("shared"); - ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class)); + ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class), snapshotCache, resourceCache); persister.saveProject(singleProject, null); persister.saveResource(singleProject, new JavaPackage("org.foo").setEffectiveKey("foo:org.foo")); @@ -124,7 +126,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { public void shouldSaveNewLibrary() { setupData("shared"); - ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class)); + ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class), snapshotCache, resourceCache); persister.saveProject(singleProject, null); persister.saveResource(singleProject, new Library("junit:junit", "4.8.2").setEffectiveKey("junit:junit")); persister.saveResource(singleProject, new Library("junit:junit", "4.8.2").setEffectiveKey("junit:junit"));// do nothing, already saved @@ -137,7 +139,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { public void shouldClearResourcesExceptProjects() { setupData("shared"); - DefaultResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class)); + DefaultResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class), snapshotCache, resourceCache); persister.saveProject(multiModuleProject, null); persister.saveProject(moduleA, multiModuleProject); persister.saveResource(moduleA, new JavaPackage("org.foo").setEffectiveKey("a:org.foo")); @@ -153,7 +155,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { public void shouldUpdateExistingResource() { setupData("shouldUpdateExistingResource"); - ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class)); + ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class), snapshotCache, resourceCache); singleProject.setName("new name"); singleProject.setDescription("new description"); persister.saveProject(singleProject, null); @@ -166,7 +168,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { public void shouldRemoveRootIndexIfResourceIsProject() { setupData("shouldRemoveRootIndexIfResourceIsProject"); - ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class)); + ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class), snapshotCache, resourceCache); persister.saveProject(singleProject, null); checkTables("shouldRemoveRootIndexIfResourceIsProject", new String[] {"build_date", "created_at"}, "projects", "snapshots"); @@ -179,7 +181,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { ResourcePermissions permissions = mock(ResourcePermissions.class); when(permissions.hasRoles(singleProject)).thenReturn(false); - ResourcePersister persister = new DefaultResourcePersister(getSession(), permissions); + ResourcePersister persister = new DefaultResourcePersister(getSession(), permissions, snapshotCache, resourceCache); persister.saveProject(singleProject, null); verify(permissions).grantDefaultRoles(singleProject); @@ -192,7 +194,7 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase { ResourcePermissions permissions = mock(ResourcePermissions.class); when(permissions.hasRoles(singleProject)).thenReturn(true); - ResourcePersister persister = new DefaultResourcePersister(getSession(), permissions); + ResourcePersister persister = new DefaultResourcePersister(getSession(), permissions, snapshotCache, resourceCache); persister.saveProject(singleProject, null); verify(permissions, never()).grantDefaultRoles(singleProject); 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 485fd846e57..e29aa563075 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 @@ -97,7 +97,7 @@ public class ScanIssueStorageTest extends AbstractDaoTestCase { storage.projectId(new DefaultIssue().setComponentKey("struts:Action.java")); fail(); } catch (Exception e) { - assertThat(e).hasMessage("Unknown component: struts:Action.java"); + assertThat(e).hasMessage("Project id not found for: struts:Action.java"); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/phases/UpdateStatusJobTest.java b/sonar-batch/src/test/java/org/sonar/batch/phases/UpdateStatusJobTest.java index be60b91d406..3102e3cc36d 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/phases/UpdateStatusJobTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/phases/UpdateStatusJobTest.java @@ -28,6 +28,8 @@ import org.sonar.api.resources.Project; import org.sonar.api.security.ResourcePermissions; import org.sonar.batch.bootstrap.ServerClient; import org.sonar.batch.index.DefaultResourcePersister; +import org.sonar.batch.index.ResourceCache; +import org.sonar.batch.index.SnapshotCache; import org.sonar.jpa.test.AbstractDbUnitTestCase; import javax.persistence.Query; @@ -56,7 +58,7 @@ public class UpdateStatusJobTest extends AbstractDbUnitTestCase { DatabaseSession session = getSession(); UpdateStatusJob sensor = new UpdateStatusJob(new Settings().appendProperty(CoreProperties.SERVER_BASE_URL, "http://myserver/"), mock(ServerClient.class), session, - new DefaultResourcePersister(session, mock(ResourcePermissions.class)), + new DefaultResourcePersister(session, mock(ResourcePermissions.class), mock(SnapshotCache.class), mock(ResourceCache.class)), mock(Project.class), loadSnapshot(snapshotId)); sensor.execute(); |