aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2010-11-26 08:10:37 +0000
committersimonbrandhof <simon.brandhof@gmail.com>2010-11-26 08:10:37 +0000
commit55480d19e772c86f65982a76a22f3b76aabf07aa (patch)
treed57247472acd8b1cb9a691a07cc3c297112a310e /sonar-batch
parentc64d328afa303bf48724bab92c80fff31114ef24 (diff)
downloadsonarqube-55480d19e772c86f65982a76a22f3b76aabf07aa.tar.gz
sonarqube-55480d19e772c86f65982a76a22f3b76aabf07aa.zip
SONAR-249 add unit tests
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/Batch.java2
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/ProjectBatch.java4
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java247
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/index/ResourcePersister.java223
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java (renamed from sonar-batch/src/test/java/org/sonar/batch/index/ResourcePersisterTest.java)14
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java61
-rw-r--r--sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shared.xml (renamed from sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shared.xml)0
-rw-r--r--sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewDirectory-result.xml (renamed from sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewDirectory-result.xml)0
-rw-r--r--sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewLibrary-result.xml (renamed from sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewLibrary-result.xml)0
-rw-r--r--sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewMultiModulesProject-result.xml (renamed from sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewMultiModulesProject-result.xml)0
-rw-r--r--sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewProject-result.xml (renamed from sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewProject-result.xml)0
-rw-r--r--sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldUpdateExistingResource-result.xml (renamed from sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldUpdateExistingResource-result.xml)0
-rw-r--r--sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldUpdateExistingResource.xml (renamed from sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldUpdateExistingResource.xml)0
-rw-r--r--sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/shared.xml11
-rw-r--r--sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/shouldSaveSource-result.xml12
15 files changed, 346 insertions, 228 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/Batch.java b/sonar-batch/src/main/java/org/sonar/batch/Batch.java
index 847bd08cdeb..303d4d099b5 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/Batch.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/Batch.java
@@ -78,7 +78,7 @@ public class Batch {
batchContainer.as(Characteristics.CACHE).addComponent(EventPersister.class);
batchContainer.as(Characteristics.CACHE).addComponent(LinkPersister.class);
batchContainer.as(Characteristics.CACHE).addComponent(MeasurePersister.class);
- batchContainer.as(Characteristics.CACHE).addComponent(ResourcePersister.class);
+ batchContainer.as(Characteristics.CACHE).addComponent(DefaultResourcePersister.class);
batchContainer.as(Characteristics.CACHE).addComponent(SourcePersister.class);
batchContainer.as(Characteristics.CACHE).addComponent(ViolationPersister.class);
batchContainer.as(Characteristics.CACHE).addComponent(JpaPluginDao.class);
diff --git a/sonar-batch/src/main/java/org/sonar/batch/ProjectBatch.java b/sonar-batch/src/main/java/org/sonar/batch/ProjectBatch.java
index 83b21a6dd39..d6843c900dd 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/ProjectBatch.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/ProjectBatch.java
@@ -36,7 +36,7 @@ import org.sonar.api.rules.DefaultRulesManager;
import org.sonar.api.utils.IocContainer;
import org.sonar.api.utils.SonarException;
import org.sonar.batch.index.DefaultIndex;
-import org.sonar.batch.index.ResourcePersister;
+import org.sonar.batch.index.DefaultResourcePersister;
import org.sonar.batch.phases.Phases;
import org.sonar.core.qualitymodel.DefaultModelFinder;
import org.sonar.core.rule.DefaultRuleFinder;
@@ -78,7 +78,7 @@ public class ProjectBatch {
batchContainer.as(Characteristics.CACHE).addComponent(RulesDao.class);
// the Snapshot component will be removed when asynchronous measures are improved (required for AsynchronousMeasureSensor)
- batchContainer.as(Characteristics.CACHE).addComponent(globalContainer.getComponent(ResourcePersister.class).getSnapshot(project));
+ batchContainer.as(Characteristics.CACHE).addComponent(globalContainer.getComponent(DefaultResourcePersister.class).getSnapshot(project));
batchContainer.as(Characteristics.CACHE).addComponent(org.sonar.api.database.daos.RulesDao.class);
batchContainer.as(Characteristics.CACHE).addComponent(org.sonar.api.database.daos.MeasuresDao.class);
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
new file mode 100644
index 00000000000..82a3dc0ffe4
--- /dev/null
+++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java
@@ -0,0 +1,247 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * 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.collect.Maps;
+import org.apache.commons.lang.ObjectUtils;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.database.DatabaseSession;
+import org.sonar.api.database.model.ResourceModel;
+import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.resources.Library;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Resource;
+import org.sonar.api.resources.ResourceUtils;
+import org.sonar.api.utils.SonarException;
+
+import javax.persistence.NonUniqueResultException;
+import javax.persistence.Query;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+public final class DefaultResourcePersister implements ResourcePersister {
+
+ private DatabaseSession session;
+
+ private Map<Resource, Snapshot> snapshotsByResource = Maps.newHashMap();
+
+ public DefaultResourcePersister(DatabaseSession session) {
+ this.session = session;
+ }
+
+ public Snapshot saveProject(Project project) {
+ Snapshot snapshot = snapshotsByResource.get(project);
+ if (snapshot == null) {
+ snapshot = doSaveProject(project);
+ }
+ return snapshot;
+ }
+
+ public Snapshot getSnapshot(Resource resource) {
+ if (resource != null) {
+ return snapshotsByResource.get(resource);
+ }
+ return null;
+ }
+
+ /**
+ * just for unit tests
+ */
+ Map<Resource, Snapshot> getSnapshotsByResource() {
+ return snapshotsByResource;
+ }
+
+ private Snapshot doSaveProject(Project project) {
+ ResourceModel model = findOrCreateModel(project, project.getKey());
+ model.setLanguageKey(project.getLanguageKey());// ugly, only for projects
+
+ Snapshot parentSnapshot = null;
+ if (project.getParent() != null) {
+ // assume that the parent project has already been saved
+ parentSnapshot = snapshotsByResource.get(project.getParent());
+ model.setRootId((Integer) ObjectUtils.defaultIfNull(parentSnapshot.getRootProjectId(), parentSnapshot.getResourceId()));
+ }
+ model = session.save(model);
+ project.setId(model.getId()); // TODO to be removed
+
+ Snapshot snapshot = new Snapshot(model, parentSnapshot);
+ snapshot.setVersion(project.getAnalysisVersion());
+ snapshot.setCreatedAt(project.getAnalysisDate());
+ snapshot = session.save(snapshot);
+ session.commit();
+ snapshotsByResource.put(project, snapshot);
+ return snapshot;
+ }
+
+ public Snapshot saveResource(Project project, Resource resource) {
+ if (resource == null) {
+ return null;
+ }
+ Snapshot snapshot = snapshotsByResource.get(resource);
+ if (snapshot == null) {
+ if (resource instanceof Project) {
+ snapshot = doSaveProject((Project) resource);
+
+ } else if (resource instanceof Library) {
+ snapshot = doSaveLibrary(project, (Library) resource);
+
+ } else {
+ snapshot = doSaveResource(project, resource);
+ }
+ }
+ return snapshot;
+ }
+
+ private Snapshot doSaveLibrary(Project project, Library library) {
+ ResourceModel model = findOrCreateModel(library, library.getKey());
+ model = session.save(model);
+ library.setId(model.getId()); // TODO to be removed
+ library.setEffectiveKey(library.getKey());
+
+ Snapshot snapshot = findLibrarySnapshot(model.getId(), library.getVersion());
+ if (snapshot == null) {
+ snapshot = new Snapshot(model, null);
+ snapshot.setCreatedAt(project.getAnalysisDate());
+ snapshot.setVersion(library.getVersion());
+ snapshot.setStatus(Snapshot.STATUS_PROCESSED);
+
+ // see http://jira.codehaus.org/browse/SONAR-1850
+ // The qualifier must be LIB, even if the resource is TRK, because this snapshot has no measures.
+ snapshot.setQualifier(Resource.QUALIFIER_LIB);
+ snapshot = session.save(snapshot);
+ }
+ session.commit();
+ return snapshot;
+ }
+
+ private Snapshot findLibrarySnapshot(Integer resourceId, String version) {
+ Query query = session.createQuery("from " + Snapshot.class.getSimpleName() + " s WHERE s.resourceId=:resourceId AND s.version=:version AND s.scope=:scope AND s.qualifier<>:qualifier AND s.last=:last");
+ query.setParameter("resourceId", resourceId);
+ query.setParameter("version", version);
+ query.setParameter("scope", Resource.SCOPE_SET);
+ query.setParameter("qualifier", Resource.QUALIFIER_LIB);
+ query.setParameter("last", Boolean.TRUE);
+ List<Snapshot> snapshots = query.getResultList();
+ if (snapshots.isEmpty()) {
+ snapshots = session.getResults(Snapshot.class, "resourceId", resourceId, "version", version, "scope", Resource.SCOPE_SET, "qualifier", Resource.QUALIFIER_LIB);
+ }
+ return (snapshots.isEmpty() ? null : snapshots.get(0));
+ }
+
+ /**
+ * Everything except project and library
+ */
+ private Snapshot doSaveResource(Project project, Resource resource) {
+ String databaseKey = getDatabaseKey(project, resource);
+ ResourceModel model = findOrCreateModel(resource, databaseKey);
+ Snapshot projectSnapshot = snapshotsByResource.get(project);
+ model.setRootId(projectSnapshot.getResourceId());
+ model = session.save(model);
+ resource.setId(model.getId()); // TODO to be removed
+ resource.setEffectiveKey(databaseKey);
+
+ Snapshot parentSnapshot = (Snapshot)ObjectUtils.defaultIfNull(getSnapshot(resource.getParent()), projectSnapshot);
+ Snapshot snapshot = new Snapshot(model, parentSnapshot);
+ snapshot = session.save(snapshot);
+ session.commit();
+ snapshotsByResource.put(resource, snapshot);
+ return snapshot;
+ }
+
+ public void clear() {
+ // we keep cache of projects
+ for (Iterator<Map.Entry<Resource, Snapshot>> it = snapshotsByResource.entrySet().iterator(); it.hasNext();) {
+ Map.Entry<Resource, Snapshot> entry = it.next();
+ if (!ResourceUtils.isSet(entry.getKey())) {
+ it.remove();
+ }
+ }
+ }
+
+
+ private ResourceModel findOrCreateModel(Resource resource, String databaseKey) {
+ ResourceModel model;
+ try {
+ model = session.getSingleResult(ResourceModel.class, "key", databaseKey);
+ if (model == null) {
+ model = createModel(resource, databaseKey);
+
+ } else {
+ mergeModel(model, resource);
+ }
+ return model;
+
+ } catch (NonUniqueResultException e) {
+ throw new SonarException("The resource '" + databaseKey + "' is duplicated in database.");
+ }
+ }
+
+ static ResourceModel createModel(Resource resource, String databaseKey) {
+ ResourceModel model = new ResourceModel();
+ model.setEnabled(Boolean.TRUE);
+ model.setDescription(resource.getDescription());
+ model.setKey(databaseKey);
+ if (resource.getLanguage() != null) {
+ model.setLanguageKey(resource.getLanguage().getKey());
+ }
+ if (StringUtils.isNotBlank(resource.getName())) {
+ model.setName(resource.getName());
+ } else {
+ model.setName(resource.getKey());
+ }
+ model.setLongName(resource.getLongName());
+ model.setScope(resource.getScope());
+ model.setQualifier(resource.getQualifier());
+ return model;
+ }
+
+ static void mergeModel(ResourceModel model, Resource resource) {
+ model.setEnabled(true);
+ if (StringUtils.isNotBlank(resource.getName())) {
+ model.setName(resource.getName());
+ }
+ if (StringUtils.isNotBlank(resource.getLongName())) {
+ model.setLongName(resource.getLongName());
+ }
+ if (StringUtils.isNotBlank(resource.getDescription())) {
+ model.setDescription(resource.getDescription());
+ }
+ if (!ResourceUtils.isLibrary(resource)) {
+ model.setScope(resource.getScope());
+ model.setQualifier(resource.getQualifier());
+ }
+ if (resource.getLanguage() != null) {
+ model.setLanguageKey(resource.getLanguage().getKey());
+ }
+ }
+
+ static String getDatabaseKey(Project project, Resource resource) {
+ if (StringUtils.equals(Resource.SCOPE_SET, resource.getScope())) {
+ // projects + libraries
+ return resource.getKey();
+ }
+ return new StringBuilder(ResourceModel.KEY_SIZE)
+ .append(project.getKey())
+ .append(':')
+ .append(resource.getKey())
+ .toString();
+ }
+}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/ResourcePersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/ResourcePersister.java
index f9f77b5ae2d..94660c451c4 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/index/ResourcePersister.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/index/ResourcePersister.java
@@ -19,229 +19,16 @@
*/
package org.sonar.batch.index;
-import com.google.common.collect.Maps;
-import org.apache.commons.lang.ObjectUtils;
-import org.apache.commons.lang.StringUtils;
-import org.sonar.api.database.DatabaseSession;
-import org.sonar.api.database.model.ResourceModel;
import org.sonar.api.database.model.Snapshot;
-import org.sonar.api.resources.Library;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
-import org.sonar.api.resources.ResourceUtils;
-import org.sonar.api.utils.SonarException;
-import javax.persistence.NonUniqueResultException;
-import javax.persistence.Query;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
+public interface ResourcePersister {
+ Snapshot saveProject(Project project);
-public class ResourcePersister {
+ Snapshot getSnapshot(Resource resource);
- private DatabaseSession session;
+ Snapshot saveResource(Project project, Resource resource);
- private Map<Resource, Snapshot> snapshotsByResource = Maps.newHashMap();
-
- public ResourcePersister(DatabaseSession session) {
- this.session = session;
- }
-
- public Snapshot saveProject(Project project) {
- Snapshot snapshot = snapshotsByResource.get(project);
- if (snapshot == null) {
- snapshot = doSaveProject(project);
- }
- return snapshot;
- }
-
- public Snapshot getSnapshot(Resource resource) {
- if (resource != null) {
- return snapshotsByResource.get(resource);
- }
- return null;
- }
-
- /**
- * just for unit tests
- */
- Map<Resource, Snapshot> getSnapshotsByResource() {
- return snapshotsByResource;
- }
-
- private Snapshot doSaveProject(Project project) {
- ResourceModel model = findOrCreateModel(project, project.getKey());
- model.setLanguageKey(project.getLanguageKey());// ugly, only for projects
-
- Snapshot parentSnapshot = null;
- if (project.getParent() != null) {
- // assume that the parent project has already been saved
- parentSnapshot = snapshotsByResource.get(project.getParent());
- model.setRootId((Integer) ObjectUtils.defaultIfNull(parentSnapshot.getRootProjectId(), parentSnapshot.getResourceId()));
- }
- model = session.save(model);
- project.setId(model.getId()); // TODO to be removed
-
- Snapshot snapshot = new Snapshot(model, parentSnapshot);
- snapshot.setVersion(project.getAnalysisVersion());
- snapshot.setCreatedAt(project.getAnalysisDate());
- snapshot = session.save(snapshot);
- session.commit();
- snapshotsByResource.put(project, snapshot);
- return snapshot;
- }
-
- public Snapshot saveResource(Project project, Resource resource) {
- if (resource == null) {
- return null;
- }
- Snapshot snapshot = snapshotsByResource.get(resource);
- if (snapshot == null) {
- if (resource instanceof Project) {
- snapshot = doSaveProject((Project) resource);
-
- } else if (resource instanceof Library) {
- snapshot = doSaveLibrary(project, (Library) resource);
-
- } else {
- snapshot = doSaveResource(project, resource);
- }
- }
- return snapshot;
- }
-
- private Snapshot doSaveLibrary(Project project, Library library) {
- ResourceModel model = findOrCreateModel(library, library.getKey());
- model = session.save(model);
- library.setId(model.getId()); // TODO to be removed
- library.setEffectiveKey(library.getKey());
-
- Snapshot snapshot = findLibrarySnapshot(model.getId(), library.getVersion());
- if (snapshot == null) {
- snapshot = new Snapshot(model, null);
- snapshot.setCreatedAt(project.getAnalysisDate());
- snapshot.setVersion(library.getVersion());
- snapshot.setStatus(Snapshot.STATUS_PROCESSED);
-
- // see http://jira.codehaus.org/browse/SONAR-1850
- // The qualifier must be LIB, even if the resource is TRK, because this snapshot has no measures.
- snapshot.setQualifier(Resource.QUALIFIER_LIB);
- snapshot = session.save(snapshot);
- }
- session.commit();
- return snapshot;
- }
-
- private Snapshot findLibrarySnapshot(Integer resourceId, String version) {
- Query query = session.createQuery("from " + Snapshot.class.getSimpleName() + " s WHERE s.resourceId=:resourceId AND s.version=:version AND s.scope=:scope AND s.qualifier<>:qualifier AND s.last=:last");
- query.setParameter("resourceId", resourceId);
- query.setParameter("version", version);
- query.setParameter("scope", Resource.SCOPE_SET);
- query.setParameter("qualifier", Resource.QUALIFIER_LIB);
- query.setParameter("last", Boolean.TRUE);
- List<Snapshot> snapshots = query.getResultList();
- if (snapshots.isEmpty()) {
- snapshots = session.getResults(Snapshot.class, "resourceId", resourceId, "version", version, "scope", Resource.SCOPE_SET, "qualifier", Resource.QUALIFIER_LIB);
- }
- return (snapshots.isEmpty() ? null : snapshots.get(0));
- }
-
- /**
- * Everything except project and library
- */
- private Snapshot doSaveResource(Project project, Resource resource) {
- String databaseKey = getDatabaseKey(project, resource);
- ResourceModel model = findOrCreateModel(resource, databaseKey);
- Snapshot projectSnapshot = snapshotsByResource.get(project);
- model.setRootId(projectSnapshot.getResourceId());
- model = session.save(model);
- resource.setId(model.getId()); // TODO to be removed
- resource.setEffectiveKey(databaseKey);
-
- Snapshot parentSnapshot = (Snapshot)ObjectUtils.defaultIfNull(getSnapshot(resource.getParent()), projectSnapshot);
- Snapshot snapshot = new Snapshot(model, parentSnapshot);
- snapshot = session.save(snapshot);
- session.commit();
- snapshotsByResource.put(resource, snapshot);
- return snapshot;
- }
-
- public void clear() {
- // we keep cache of projects
- for (Iterator<Map.Entry<Resource, Snapshot>> it = snapshotsByResource.entrySet().iterator(); it.hasNext();) {
- Map.Entry<Resource, Snapshot> entry = it.next();
- if (!ResourceUtils.isSet(entry.getKey())) {
- it.remove();
- }
- }
- }
-
-
- private ResourceModel findOrCreateModel(Resource resource, String databaseKey) {
- ResourceModel model;
- try {
- model = session.getSingleResult(ResourceModel.class, "key", databaseKey);
- if (model == null) {
- model = createModel(resource, databaseKey);
-
- } else {
- mergeModel(model, resource);
- }
- return model;
-
- } catch (NonUniqueResultException e) {
- throw new SonarException("The resource '" + databaseKey + "' is duplicated in database.");
- }
- }
-
- static ResourceModel createModel(Resource resource, String databaseKey) {
- ResourceModel model = new ResourceModel();
- model.setEnabled(Boolean.TRUE);
- model.setDescription(resource.getDescription());
- model.setKey(databaseKey);
- if (resource.getLanguage() != null) {
- model.setLanguageKey(resource.getLanguage().getKey());
- }
- if (StringUtils.isNotBlank(resource.getName())) {
- model.setName(resource.getName());
- } else {
- model.setName(resource.getKey());
- }
- model.setLongName(resource.getLongName());
- model.setScope(resource.getScope());
- model.setQualifier(resource.getQualifier());
- return model;
- }
-
- static void mergeModel(ResourceModel model, Resource resource) {
- model.setEnabled(true);
- if (StringUtils.isNotBlank(resource.getName())) {
- model.setName(resource.getName());
- }
- if (StringUtils.isNotBlank(resource.getLongName())) {
- model.setLongName(resource.getLongName());
- }
- if (StringUtils.isNotBlank(resource.getDescription())) {
- model.setDescription(resource.getDescription());
- }
- if (!ResourceUtils.isLibrary(resource)) {
- model.setScope(resource.getScope());
- model.setQualifier(resource.getQualifier());
- }
- if (resource.getLanguage() != null) {
- model.setLanguageKey(resource.getLanguage().getKey());
- }
- }
-
- static String getDatabaseKey(Project project, Resource resource) {
- if (StringUtils.equals(Resource.SCOPE_SET, resource.getScope())) {
- // projects + libraries
- return resource.getKey();
- }
- return new StringBuilder(ResourceModel.KEY_SIZE)
- .append(project.getKey())
- .append(':')
- .append(resource.getKey())
- .toString();
- }
+ void clear();
}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/ResourcePersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java
index 88fbb209afd..8134742fc15 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/index/ResourcePersisterTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java
@@ -34,7 +34,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;
-public class ResourcePersisterTest extends AbstractDbUnitTestCase {
+public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase {
private Project singleProject, multiModuleProject, moduleA, moduleB, moduleB1;
@@ -65,7 +65,7 @@ public class ResourcePersisterTest extends AbstractDbUnitTestCase {
public void shouldSaveNewProject() {
setupData("shared");
- ResourcePersister persister = new ResourcePersister(getSession());
+ ResourcePersister persister = new DefaultResourcePersister(getSession());
persister.saveProject(singleProject);
checkTables("shouldSaveNewProject", "projects", "snapshots");
@@ -75,7 +75,7 @@ public class ResourcePersisterTest extends AbstractDbUnitTestCase {
public void shouldSaveNewMultiModulesProject() throws ParseException {
setupData("shared");
- ResourcePersister persister = new ResourcePersister(getSession());
+ ResourcePersister persister = new DefaultResourcePersister(getSession());
persister.saveProject(multiModuleProject);
persister.saveProject(moduleA);
persister.saveProject(moduleB);
@@ -88,7 +88,7 @@ public class ResourcePersisterTest extends AbstractDbUnitTestCase {
public void shouldSaveNewDirectory() {
setupData("shared");
- ResourcePersister persister = new ResourcePersister(getSession());
+ ResourcePersister persister = new DefaultResourcePersister(getSession());
persister.saveProject(singleProject);
persister.saveResource(singleProject, new JavaPackage("org.foo"));
@@ -100,7 +100,7 @@ public class ResourcePersisterTest extends AbstractDbUnitTestCase {
public void shouldSaveNewLibrary() {
setupData("shared");
- ResourcePersister persister = new ResourcePersister(getSession());
+ ResourcePersister persister = new DefaultResourcePersister(getSession());
persister.saveProject(singleProject);
persister.saveResource(singleProject, new Library("junit:junit", "4.8.2"));
persister.saveResource(singleProject, new Library("junit:junit", "4.8.2"));// do nothing, already saved
@@ -113,7 +113,7 @@ public class ResourcePersisterTest extends AbstractDbUnitTestCase {
public void shouldClearResourcesExceptProjects() {
setupData("shared");
- ResourcePersister persister = new ResourcePersister(getSession());
+ DefaultResourcePersister persister = new DefaultResourcePersister(getSession());
persister.saveProject(multiModuleProject);
persister.saveProject(moduleA);
persister.saveResource(moduleA, new JavaPackage("org.foo"));
@@ -129,7 +129,7 @@ public class ResourcePersisterTest extends AbstractDbUnitTestCase {
public void shouldUpdateExistingResource() {
setupData("shouldUpdateExistingResource");
- ResourcePersister persister = new ResourcePersister(getSession());
+ ResourcePersister persister = new DefaultResourcePersister(getSession());
singleProject.setName("new name");
singleProject.setDescription("new description");
persister.saveProject(singleProject);
diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java
new file mode 100644
index 00000000000..d92fdb8adf7
--- /dev/null
+++ b/sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java
@@ -0,0 +1,61 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * 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.Before;
+import org.junit.Test;
+import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.resources.JavaFile;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Resource;
+import org.sonar.api.utils.SonarException;
+import org.sonar.jpa.test.AbstractDbUnitTestCase;
+
+import static org.mockito.Matchers.anyObject;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class SourcePersisterTest extends AbstractDbUnitTestCase {
+
+ private SourcePersister sourcePersister;
+
+ @Before
+ public void before() {
+ setupData("shared");
+ Snapshot snapshot = getSession().getSingleResult(Snapshot.class, "id", 1000);
+ ResourcePersister resourcePersister = mock(ResourcePersister.class);
+ when(resourcePersister.saveResource((Project) anyObject(), (Resource) anyObject())).thenReturn(snapshot);
+ sourcePersister = new SourcePersister(getSession(), resourcePersister);
+ }
+
+ @Test
+ public void shouldSaveSource() {
+ sourcePersister.saveSource(new Project(""), new JavaFile("org.foo.Bar"), "this is the file content");
+ checkTables("shouldSaveSource", "snapshot_sources");
+ }
+
+ @Test(expected = SonarException.class)
+ public void shouldFailIfSourceSavedSeveralTimes() {
+ Project project = new Project("project");
+ JavaFile file = new JavaFile("org.foo.Bar");
+ sourcePersister.saveSource(project, file, "this is the file content");
+ sourcePersister.saveSource(project, file, "new content"); // fail
+ }
+}
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shared.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shared.xml
index 0c4e4aed8a2..0c4e4aed8a2 100644
--- a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shared.xml
+++ b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shared.xml
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewDirectory-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewDirectory-result.xml
index 7e82d5b090d..7e82d5b090d 100644
--- a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewDirectory-result.xml
+++ b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewDirectory-result.xml
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewLibrary-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewLibrary-result.xml
index f53d7421a8f..f53d7421a8f 100644
--- a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewLibrary-result.xml
+++ b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewLibrary-result.xml
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewMultiModulesProject-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewMultiModulesProject-result.xml
index a83a24b4f88..a83a24b4f88 100644
--- a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewMultiModulesProject-result.xml
+++ b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewMultiModulesProject-result.xml
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewProject-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewProject-result.xml
index e69ab9a12b2..e69ab9a12b2 100644
--- a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldSaveNewProject-result.xml
+++ b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldSaveNewProject-result.xml
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldUpdateExistingResource-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldUpdateExistingResource-result.xml
index 3006cd6f826..3006cd6f826 100644
--- a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldUpdateExistingResource-result.xml
+++ b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldUpdateExistingResource-result.xml
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldUpdateExistingResource.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldUpdateExistingResource.xml
index 1a6997f37fe..1a6997f37fe 100644
--- a/sonar-batch/src/test/resources/org/sonar/batch/index/ResourcePersisterTest/shouldUpdateExistingResource.xml
+++ b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shouldUpdateExistingResource.xml
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/shared.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/shared.xml
new file mode 100644
index 00000000000..8290d2ec5c5
--- /dev/null
+++ b/sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/shared.xml
@@ -0,0 +1,11 @@
+<dataset>
+
+ <projects id="200" scope="FIL" qualifier="CLA" kee="project:org.foo.Bar" root_id="[null]"
+ name="Bar" long_name="org.foo.Bar" description="[null]"
+ enabled="true" language="java" copy_resource_id="[null]" profile_id="[null]"/>
+
+ <snapshots id="1000" project_id="200" parent_snapshot_id="[null]" root_project_id="100" root_snapshot_id="[null]"
+ scope="FIL" qualifier="CLA" created_at="2008-11-01 13:58:00.00" version="[null]" path=""
+ status="U" islast="false" depth="3" />
+
+</dataset> \ No newline at end of file
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/shouldSaveSource-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/shouldSaveSource-result.xml
new file mode 100644
index 00000000000..535d2131485
--- /dev/null
+++ b/sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/shouldSaveSource-result.xml
@@ -0,0 +1,12 @@
+<dataset>
+
+ <projects id="200" scope="FIL" qualifier="CLA" kee="project:org.foo.Bar" root_id="[null]"
+ name="Bar" long_name="org.foo.Bar" description="[null]"
+ enabled="true" language="java" copy_resource_id="[null]" profile_id="[null]"/>
+
+ <snapshots id="1000" project_id="200" parent_snapshot_id="[null]" root_project_id="100" root_snapshot_id="[null]"
+ scope="FIL" qualifier="CLA" created_at="2008-11-01 13:58:00.00" version="[null]" path=""
+ status="U" islast="false" depth="3" />
+
+ <SNAPSHOT_SOURCES ID="1" SNAPSHOT_ID="1000" DATA="this is the file content"/>
+</dataset> \ No newline at end of file