aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2015-06-11 10:10:57 +0200
committerJulien Lancelot <julien.lancelot@sonarsource.com>2015-06-12 11:13:36 +0200
commit817fdc3955489611f9e544e4c6e0c095378e0070 (patch)
tree4558e4370cb2d7aa2d43bbeae2021b4898b71e96
parent5997e99aa0ff6ef770b4d9a160db0098fd68f299 (diff)
downloadsonarqube-817fdc3955489611f9e544e4c6e0c095378e0070.tar.gz
sonarqube-817fdc3955489611f9e544e4c6e0c095378e0070.zip
SONAR-6636 Remove module checks from batch requiring database
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorValidator.java53
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorValidatorTest.java101
2 files changed, 5 insertions, 149 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorValidator.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorValidator.java
index 208b5766483..af8b1a9b7b6 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorValidator.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorValidator.java
@@ -20,21 +20,16 @@
package org.sonar.batch.scan;
import com.google.common.base.Joiner;
+import java.util.ArrayList;
+import java.util.List;
+import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.config.Settings;
-import org.sonar.api.resources.Qualifiers;
import org.sonar.api.utils.SonarException;
import org.sonar.core.component.ComponentKeys;
-import org.sonar.core.resource.ResourceDao;
-import org.sonar.core.resource.ResourceDto;
-
-import javax.annotation.Nullable;
-
-import java.util.ArrayList;
-import java.util.List;
/**
* This class aims at validating project reactor
@@ -44,20 +39,12 @@ public class ProjectReactorValidator {
private static final String SONAR_PHASE = "sonar.phase";
private final Settings settings;
- private final ResourceDao resourceDao;
-
- public ProjectReactorValidator(Settings settings, @Nullable ResourceDao resourceDao) {
- this.settings = settings;
- this.resourceDao = resourceDao;
- }
public ProjectReactorValidator(Settings settings) {
- this(settings, null);
+ this.settings = settings;
}
public void validate(ProjectReactor reactor) {
- preventAutomaticProjectCreationIfNeeded(reactor);
-
String branch = settings.getString(CoreProperties.PROJECT_BRANCH_PROPERTY);
String rootProjectKey = ComponentKeys.createKey(reactor.getRoot().getKey(), branch);
@@ -75,38 +62,10 @@ public class ProjectReactorValidator {
}
}
- private void preventAutomaticProjectCreationIfNeeded(ProjectReactor reactor) {
- if (resourceDao != null && settings.getBoolean(CoreProperties.CORE_PREVENT_AUTOMATIC_PROJECT_CREATION)) {
- String projectKey = reactor.getRoot().getKeyWithBranch();
- if (resourceDao.findByKey(projectKey) == null) {
- throw new SonarException(String.format("Unable to scan non-existing project \"%s\"", projectKey));
- }
- }
- }
-
private void validateModule(ProjectDefinition moduleDef, List<String> validationMessages, @Nullable String branch, String rootProjectKey) {
if (!ComponentKeys.isValidModuleKey(moduleDef.getKey())) {
validationMessages.add(String.format("\"%s\" is not a valid project or module key. "
+ "Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.", moduleDef.getKey()));
- } else if (resourceDao != null && isSubProject(moduleDef)) {
- // SONAR-4692 Validate root project is the same than previous analysis to avoid module with same key in different projects
- String moduleKey = ComponentKeys.createKey(moduleDef.getKey(), branch);
- ResourceDto rootInDB = resourceDao.getRootProjectByComponentKey(moduleKey);
- if (rootInDB == null || Qualifiers.LIBRARY.equals(rootInDB.getQualifier())) {
- // This is a new module or previously a library so OK
- return;
- }
- if (rootInDB.getKey().equals(moduleKey)) {
- // SONAR-4245 current subproject is actually a root project in SQ DB
- 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'.",
- moduleKey, rootProjectKey, moduleKey, rootProjectKey));
- }
- if (!rootProjectKey.equals(rootInDB.getKey())) {
- // SONAR-4692 current subproject is already a subproject in another project
- throw new SonarException(String.format("Module \"%s\" is already part of project \"%s\"", moduleDef.getKey(), rootInDB.getKey()));
- }
}
}
@@ -116,10 +75,6 @@ public class ProjectReactorValidator {
}
}
- private boolean isSubProject(ProjectDefinition moduleDef) {
- return moduleDef.getParent() != null;
- }
-
private void validateBranch(List<String> validationMessages, @Nullable String branch) {
if (StringUtils.isNotEmpty(branch) && !ComponentKeys.isValidBranch(branch)) {
validationMessages.add(String.format("\"%s\" is not a valid branch name. "
diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorValidatorTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorValidatorTest.java
index 504a734ff7f..acc6fa82c34 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorValidatorTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorValidatorTest.java
@@ -26,14 +26,8 @@ import org.junit.rules.ExpectedException;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.bootstrap.ProjectReactor;
-import org.sonar.api.component.Component;
import org.sonar.api.config.Settings;
import org.sonar.api.utils.SonarException;
-import org.sonar.core.resource.ResourceDao;
-import org.sonar.core.resource.ResourceDto;
-
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public class ProjectReactorValidatorTest {
@@ -42,104 +36,11 @@ public class ProjectReactorValidatorTest {
private ProjectReactorValidator validator;
private Settings settings;
- private ResourceDao resourceDao;
@Before
public void prepare() {
settings = new Settings();
- resourceDao = mock(ResourceDao.class);
- validator = new ProjectReactorValidator(settings, resourceDao);
- }
-
- @Test
- public void not_fail_if_provisioning_enforced_and_project_exists() {
- String key = "project-key";
- settings.setProperty(CoreProperties.CORE_PREVENT_AUTOMATIC_PROJECT_CREATION, true);
- when(resourceDao.findByKey(key)).thenReturn(mock(Component.class));
- ProjectReactor reactor = createProjectReactor(key);
- validator.validate(reactor);
- }
-
- @Test
- public void not_fail_if_provisioning_enforced_and_project_with_branch_exists() {
- String key = "project-key";
- settings.setProperty(CoreProperties.CORE_PREVENT_AUTOMATIC_PROJECT_CREATION, true);
- when(resourceDao.findByKey(key + ":branch")).thenReturn(mock(Component.class));
- ProjectReactor reactor = createProjectReactor(key, "branch");
- validator.validate(reactor);
- }
-
- @Test(expected = SonarException.class)
- public void fail_if_provisioning_enforced_and_project_not_provisioned() {
- String key = "project-key";
- settings.setProperty(CoreProperties.CORE_PREVENT_AUTOMATIC_PROJECT_CREATION, true);
- when(resourceDao.findByKey(key)).thenReturn(null);
- ProjectReactor reactor = createProjectReactor(key);
- validator.validate(reactor);
- }
-
- // SONAR-4692
- @Test(expected = SonarException.class)
- public void fail_if_module_part_of_another_project() {
- String rootProjectKey = "project-key";
- String moduleKey = "module-key";
- ResourceDto rootResource = mock(ResourceDto.class);
-
- when(rootResource.getKey()).thenReturn("another-project-key");
-
- when(resourceDao.getRootProjectByComponentKey(moduleKey)).thenReturn(rootResource);
- ProjectReactor reactor = createProjectReactor(rootProjectKey);
- reactor.getRoot().addSubProject(ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, moduleKey));
- validator.validate(reactor);
- }
-
- // SONAR-4692
- @Test
- public void not_fail_if_module_part_of_same_project() {
- String rootProjectKey = "project-key";
- String moduleKey = "module-key";
- ResourceDto rootResource = mock(ResourceDto.class);
-
- when(rootResource.getKey()).thenReturn(rootProjectKey);
-
- when(resourceDao.getRootProjectByComponentKey(moduleKey)).thenReturn(rootResource);
- ProjectReactor reactor = createProjectReactor(rootProjectKey);
- reactor.getRoot().addSubProject(ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, moduleKey));
- validator.validate(reactor);
- }
-
- // SONAR-4692
- @Test
- public void not_fail_if_new_module() {
- String rootProjectKey = "project-key";
- String moduleKey = "module-key";
-
- when(resourceDao.getRootProjectByComponentKey(moduleKey)).thenReturn(null);
-
- ProjectReactor reactor = createProjectReactor(rootProjectKey);
- reactor.getRoot().addSubProject(ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, moduleKey));
- validator.validate(reactor);
- }
-
- // SONAR-4245
- @Test
- public void shouldFailWhenTryingToConvertProjectIntoModule() {
- String rootProjectKey = "project-key";
- String moduleKey = "module-key";
- ResourceDto rootResource = mock(ResourceDto.class);
-
- when(rootResource.getKey()).thenReturn(moduleKey);
-
- when(resourceDao.getRootProjectByComponentKey(moduleKey)).thenReturn(rootResource);
-
- ProjectReactor reactor = createProjectReactor(rootProjectKey);
- reactor.getRoot().addSubProject(ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, moduleKey));
-
- thrown.expect(SonarException.class);
- thrown.expectMessage("The project 'module-key' is already defined in SonarQube but not as a module of project 'project-key'. "
- + "If you really want to stop directly analysing project 'module-key', please first delete it from SonarQube and then relaunch the analysis of project 'project-key'.");
-
- validator.validate(reactor);
+ validator = new ProjectReactorValidator(settings);
}
@Test