aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2013-07-12 16:03:17 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2013-07-12 16:03:51 +0200
commit9f58e25cb331d9619e0cf059367196b3816eecb2 (patch)
tree32be40fc11a47685a665098d3a573964b69dbb83 /sonar-batch
parent1103e34e0b7bd96ac258dd4ccdb6c308f738f281 (diff)
downloadsonarqube-9f58e25cb331d9619e0cf059367196b3816eecb2.tar.gz
sonarqube-9f58e25cb331d9619e0cf059367196b3816eecb2.zip
SONAR-4245 Don't allow to convert a Sonar Maven project into a Sonar Maven module
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java9
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/index/DefaultResourcePersisterTest.java27
-rw-r--r--sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shared.xml2
3 files changed, 36 insertions, 2 deletions
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 625d6480aa2..0d754d215d6 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
@@ -287,6 +287,15 @@ public final class DefaultResourcePersister implements ResourcePersister {
model.setDescription(resource.getDescription());
}
if (!ResourceUtils.isLibrary(resource)) {
+ // SONAR-4245
+ if (Scopes.PROJECT.equals(resource.getScope()) &&
+ Qualifiers.MODULE.equals(resource.getQualifier())
+ && Qualifiers.PROJECT.equals(model.getQualifier())) {
+ throw new SonarException(
+ String.format("The project '%s' is already defined in SonarQube but not as a module of project '%s'. "
+ + "If you really want to stop directly analysing project '%s', please first delete it from SonarQube and then relaunch the analysis of project '%s'.",
+ resource.getKey(), resource.getParent().getKey(), resource.getKey(), resource.getParent().getKey()));
+ }
model.setScope(resource.getScope());
model.setQualifier(resource.getQualifier());
}
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 a7a0273e756..68cba62d50b 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
@@ -21,13 +21,16 @@ package org.sonar.batch.index;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.sonar.api.database.model.ResourceModel;
import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.JavaPackage;
import org.sonar.api.resources.Library;
import org.sonar.api.resources.Project;
import org.sonar.api.security.ResourcePermissions;
+import org.sonar.api.utils.SonarException;
import org.sonar.jpa.test.AbstractDbUnitTestCase;
import java.text.ParseException;
@@ -44,7 +47,10 @@ import static org.mockito.Mockito.when;
public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase {
- Project singleProject, singleCopyProject, multiModuleProject, moduleA, moduleB, moduleB1;
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ Project singleProject, singleCopyProject, multiModuleProject, moduleA, moduleB, moduleB1, existingProject;
SnapshotCache snapshotCache = mock(SnapshotCache.class);
ResourceCache resourceCache = mock(ResourceCache.class);
@@ -54,6 +60,9 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase {
singleProject = newProject("foo", "java");
singleProject.setName("Foo").setDescription("some description").setAnalysisDate(format.parse("25/12/2010"));
+ existingProject = newProject("my:key", "java");
+ existingProject.setName("Other project").setDescription("some description").setAnalysisDate(format.parse("25/12/2010"));
+
singleCopyProject = newCopyProject("foo", "java", 10);
singleCopyProject.setName("Foo").setDescription("some description").setAnalysisDate(format.parse("25/12/2010"));
@@ -110,6 +119,22 @@ public class DefaultResourcePersisterTest extends AbstractDbUnitTestCase {
checkTables("shouldSaveNewMultiModulesProject", new String[] {"build_date", "created_at"}, "projects", "snapshots");
}
+ // SONAR-4245
+ @Test
+ public void shouldFailWhenTryingToConvertProjectIntoModule() {
+ setupData("shared");
+
+ ResourcePersister persister = new DefaultResourcePersister(getSession(), mock(ResourcePermissions.class), snapshotCache, resourceCache);
+ existingProject.setParent(multiModuleProject);
+ persister.saveProject(multiModuleProject, null);
+
+ thrown.expect(SonarException.class);
+ thrown.expectMessage("The project 'my:key' is already defined in SonarQube but not as a module of project 'root'. "
+ + "If you really want to stop directly analysing project 'my:key', please first delete it from SonarQube and then relaunch the analysis of project 'root'.");
+
+ persister.saveProject(existingProject, multiModuleProject);
+ }
+
@Test
public void shouldSaveNewDirectory() {
setupData("shared");
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shared.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shared.xml
index 4eaddace0e1..813a604bf6e 100644
--- a/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shared.xml
+++ b/sonar-batch/src/test/resources/org/sonar/batch/index/DefaultResourcePersisterTest/shared.xml
@@ -9,4 +9,4 @@
scope="PRJ" qualifier="TRK" created_at="2008-11-01 13:58:00.00" build_date="2008-11-01 13:58:00.00" version="[null]" path=""
status="P" islast="false" depth="0" />
-</dataset> \ No newline at end of file
+</dataset>