diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2013-04-18 16:10:54 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2013-04-18 16:11:30 +0200 |
commit | 4e318cf3a6d736ab44583559bef9b9e73a19a2b0 (patch) | |
tree | bdbbfcbfa103400ea308f69dec0a8425c4485122 /sonar-batch | |
parent | d095aa375d815ddff883fbd5a451594422b38539 (diff) | |
download | sonarqube-4e318cf3a6d736ab44583559bef9b9e73a19a2b0.tar.gz sonarqube-4e318cf3a6d736ab44583559bef9b9e73a19a2b0.zip |
SONAR-4188 Validate branch too
Diffstat (limited to 'sonar-batch')
4 files changed, 75 insertions, 22 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorReady.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorReady.java index 621c5556c33..5f9979e57f4 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorReady.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorReady.java @@ -21,6 +21,7 @@ package org.sonar.batch.scan; import org.sonar.api.batch.bootstrap.ProjectBuilder; import org.sonar.api.batch.bootstrap.ProjectReactor; +import org.sonar.api.config.Settings; /** * Barrier to control the project lifecycle : @@ -35,18 +36,20 @@ import org.sonar.api.batch.bootstrap.ProjectReactor; */ public class ProjectReactorReady { - private ProjectReactor reactor; + private final ProjectReactor reactor; + private final Settings settings; - public ProjectReactorReady(ProjectExclusions exclusions, ProjectReactor reactor, ProjectBuilder[] projectBuilders) { + public ProjectReactorReady(ProjectExclusions exclusions, ProjectReactor reactor, Settings settings, ProjectBuilder[] projectBuilders) { this.reactor = reactor; + this.settings = settings; } - public ProjectReactorReady(ProjectExclusions exclusions, ProjectReactor reactor) { - this.reactor = reactor; + public ProjectReactorReady(ProjectExclusions exclusions, ProjectReactor reactor, Settings settings) { + this(exclusions, reactor, settings, null); } public void start() { - ProjectReactorValidator validator = new ProjectReactorValidator(); + ProjectReactorValidator validator = new ProjectReactorValidator(settings); validator.validate(reactor); } } 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 86a5191b40f..cc4b24c6464 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,8 +20,11 @@ package org.sonar.batch.scan; import com.google.common.base.Joiner; +import org.codehaus.plexus.util.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.utils.SonarException; import java.util.ArrayList; @@ -34,22 +37,40 @@ import java.util.List; public class ProjectReactorValidator { private static final String VALID_MODULE_KEY_REGEXP = "[0-9a-zA-Z\\-_\\.:]+"; + private final Settings settings; + + public ProjectReactorValidator(Settings settings) { + this.settings = settings; + } public void validate(ProjectReactor reactor) { List<String> validationMessages = new ArrayList<String>(); - for (ProjectDefinition def : reactor.getProjects()) { - validate(def, validationMessages); + for (ProjectDefinition moduleDef : reactor.getProjects()) { + validateModule(moduleDef, validationMessages); } + validateBranch(validationMessages); + if (!validationMessages.isEmpty()) { throw new SonarException("Validation of project reactor failed:\n o " + Joiner.on("\n o ").join(validationMessages)); } } - private void validate(ProjectDefinition def, List<String> validationMessages) { + private void validateModule(ProjectDefinition moduleDef, List<String> validationMessages) { + validateKey(moduleDef, validationMessages); + } + + private void validateKey(ProjectDefinition def, List<String> validationMessages) { if (!def.getKey().matches(VALID_MODULE_KEY_REGEXP)) { validationMessages.add(String.format("%s is not a valid project or module key", def.getKey())); } } + private void validateBranch(List<String> validationMessages) { + String branch = settings.getString(CoreProperties.PROJECT_BRANCH_PROPERTY); + if (StringUtils.isNotEmpty(branch) && !branch.matches(VALID_MODULE_KEY_REGEXP)) { + validationMessages.add(String.format("%s is not a valid branch name", branch)); + } + } + } diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorReadyTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorReadyTest.java index 4903aa71986..91dbead92ad 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorReadyTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorReadyTest.java @@ -22,6 +22,7 @@ package org.sonar.batch.scan; import org.junit.Test; import org.sonar.api.batch.bootstrap.ProjectBuilder; import org.sonar.api.batch.bootstrap.ProjectReactor; +import org.sonar.api.config.Settings; import static org.mockito.Mockito.mock; @@ -29,13 +30,14 @@ public class ProjectReactorReadyTest { @Test public void should_do_nothing() { // it's only a barrier - ProjectReactorReady barrier = new ProjectReactorReady(mock(ProjectExclusions.class), mock(ProjectReactor.class), new ProjectBuilder[] {mock(ProjectBuilder.class)}); + ProjectReactorReady barrier = new ProjectReactorReady(mock(ProjectExclusions.class), mock(ProjectReactor.class), new Settings(), + new ProjectBuilder[] {mock(ProjectBuilder.class)}); barrier.start(); } @Test public void project_builders_should_be_optional() { - ProjectReactorReady barrier = new ProjectReactorReady(mock(ProjectExclusions.class), mock(ProjectReactor.class)); + ProjectReactorReady barrier = new ProjectReactorReady(mock(ProjectExclusions.class), mock(ProjectReactor.class), new Settings()); barrier.start(); } } 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 c146ce7f6a9..7b6f4e7bdb1 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,59 +26,62 @@ 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.config.Settings; import org.sonar.api.utils.SonarException; public class ProjectReactorValidatorTest { @Rule public ExpectedException thrown = ExpectedException.none(); + private ProjectReactorValidator validator; + private Settings settings; @Before public void prepare() { - validator = new ProjectReactorValidator(); + settings = new Settings(); + validator = new ProjectReactorValidator(settings); } @Test - public void should_not_fail_with_valid_data() { + public void should_not_fail_with_valid_key() { ProjectReactor reactor = createProjectReactor("foo"); validator.validate(reactor); } @Test - public void should_not_fail_with_alphanumeric() { + public void should_not_fail_with_alphanumeric_key() { ProjectReactor reactor = createProjectReactor("Foobar2"); validator.validate(reactor); } @Test - public void should_not_fail_with_dot() { + public void should_not_fail_with_dot_key() { ProjectReactor reactor = createProjectReactor("foo.bar"); validator.validate(reactor); } @Test - public void should_not_fail_with_dash() { + public void should_not_fail_with_dash_key() { ProjectReactor reactor = createProjectReactor("foo-bar"); validator.validate(reactor); } @Test - public void should_not_fail_with_colon() { + public void should_not_fail_with_colon_key() { ProjectReactor reactor = createProjectReactor("foo:bar"); validator.validate(reactor); } @Test - public void should_not_fail_with_underscore() { + public void should_not_fail_with_underscore_key() { ProjectReactor reactor = createProjectReactor("foo_bar"); validator.validate(reactor); } @Test public void should_fail_with_invalid_key() { - String projectKey = "foo$bar"; - ProjectReactor reactor = createProjectReactor(projectKey); + ProjectReactor reactor = createProjectReactor("foo$bar"); thrown.expect(SonarException.class); thrown.expectMessage("foo$bar is not a valid project or module key"); @@ -86,19 +89,43 @@ public class ProjectReactorValidatorTest { } @Test - public void should_fail_with_backslash() { - String projectKey = "foo\\bar"; - ProjectReactor reactor = createProjectReactor(projectKey); + public void should_fail_with_backslash_in_key() { + ProjectReactor reactor = createProjectReactor("foo\\bar"); thrown.expect(SonarException.class); thrown.expectMessage("foo\\bar is not a valid project or module key"); validator.validate(reactor); } + @Test + public void should_not_fail_with_valid_branch() { + validator.validate(createProjectReactor("foo", "branch")); + validator.validate(createProjectReactor("foo", "Branch2")); + validator.validate(createProjectReactor("foo", "bra.nch")); + validator.validate(createProjectReactor("foo", "bra-nch")); + validator.validate(createProjectReactor("foo", "bra:nch")); + validator.validate(createProjectReactor("foo", "bra_nch")); + } + + @Test + public void should_fail_with_invalid_branch() { + ProjectReactor reactor = createProjectReactor("foo", "bran#ch"); + thrown.expect(SonarException.class); + thrown.expectMessage("bran#ch is not a valid branch name"); + validator.validate(reactor); + } + private ProjectReactor createProjectReactor(String projectKey) { ProjectDefinition def = ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, projectKey); ProjectReactor reactor = new ProjectReactor(def); return reactor; } + private ProjectReactor createProjectReactor(String projectKey, String branch) { + ProjectDefinition def = ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, projectKey); + ProjectReactor reactor = new ProjectReactor(def); + settings.setProperty(CoreProperties.PROJECT_BRANCH_PROPERTY, branch); + return reactor; + } + } |