瀏覽代碼

SONAR-4188 Validate project/module key with a regexp

tags/3.6
Julien HENRY 11 年之前
父節點
當前提交
8b6cbd78f1

+ 9
- 3
sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorReady.java 查看文件

@@ -20,6 +20,7 @@
package org.sonar.batch.scan;

import org.sonar.api.batch.bootstrap.ProjectBuilder;
import org.sonar.api.batch.bootstrap.ProjectReactor;

/**
* Barrier to control the project lifecycle :
@@ -34,13 +35,18 @@ import org.sonar.api.batch.bootstrap.ProjectBuilder;
*/
public class ProjectReactorReady {

public ProjectReactorReady(ProjectExclusions exclusions, ProjectBuilder[] projectBuilders) {
private ProjectReactor reactor;

public ProjectReactorReady(ProjectExclusions exclusions, ProjectReactor reactor, ProjectBuilder[] projectBuilders) {
this.reactor = reactor;
}

public ProjectReactorReady(ProjectExclusions exclusions) {
public ProjectReactorReady(ProjectExclusions exclusions, ProjectReactor reactor) {
this.reactor = reactor;
}

public void start() {

ProjectReactorValidator validator = new ProjectReactorValidator();
validator.validate(reactor);
}
}

+ 55
- 0
sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorValidator.java 查看文件

@@ -0,0 +1,55 @@
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2008-2012 SonarSource
* 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.scan;

import com.google.common.base.Joiner;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.utils.SonarException;

import java.util.ArrayList;
import java.util.List;

/**
* This class aims at validating project reactor
* @since 3.6
*/
public class ProjectReactorValidator {

private static final String VALID_MODULE_KEY_REGEXP = "[0-9a-zA-Z:-_\\.]+";

public void validate(ProjectReactor reactor) {
List<String> validationMessages = new ArrayList<String>();
for (ProjectDefinition def : reactor.getProjects()) {
validate(def, 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) {
if (!def.getKey().matches(VALID_MODULE_KEY_REGEXP)) {
validationMessages.add(String.format("%s is not a valid project or module key", def.getKey()));
}
}

}

+ 3
- 2
sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorReadyTest.java 查看文件

@@ -21,6 +21,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 static org.mockito.Mockito.mock;

@@ -28,13 +29,13 @@ public class ProjectReactorReadyTest {
@Test
public void should_do_nothing() {
// it's only a barrier
ProjectReactorReady barrier = new ProjectReactorReady(mock(ProjectExclusions.class), new ProjectBuilder[]{mock(ProjectBuilder.class)});
ProjectReactorReady barrier = new ProjectReactorReady(mock(ProjectExclusions.class), mock(ProjectReactor.class), new ProjectBuilder[] {mock(ProjectBuilder.class)});
barrier.start();
}

@Test
public void project_builders_should_be_optional() {
ProjectReactorReady barrier = new ProjectReactorReady(mock(ProjectExclusions.class));
ProjectReactorReady barrier = new ProjectReactorReady(mock(ProjectExclusions.class), mock(ProjectReactor.class));
barrier.start();
}
}

+ 64
- 0
sonar-batch/src/test/java/org/sonar/batch/scan/ProjectReactorValidatorTest.java 查看文件

@@ -0,0 +1,64 @@
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2008-2012 SonarSource
* 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.scan;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
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.utils.SonarException;

public class ProjectReactorValidatorTest {

@Rule
public ExpectedException thrown = ExpectedException.none();
private ProjectReactorValidator validator;

@Before
public void prepare() {
validator = new ProjectReactorValidator();
}

@Test
public void should_not_fail_with_valid_data() {
ProjectReactor reactor = createProjectReactor("foo");
validator.validate(reactor);
}

@Test
public void should_fail_with_invalid_key() {
String projectKey = "foo$bar";
ProjectReactor reactor = createProjectReactor(projectKey);

thrown.expect(SonarException.class);
thrown.expectMessage("foo$bar is not a valid project or module key");
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;
}

}

Loading…
取消
儲存