}
@Test
- public void skipByLanguage() {
- PropertiesConfiguration conf = new PropertiesConfiguration();
- conf.setProperty("sonar.cpd.skip", "false");
- conf.setProperty("sonar.cpd.php.skip", "true");
-
- Project phpProject = createPhpProject().setConfiguration(conf);
- Project javaProject = createJavaProject().setConfiguration(conf);
+ public void shouldSkipByLanguage() {
+ Project phpProject = createPhpProject();
+ phpProject.getConfiguration().setProperty("sonar.cpd.skip", "false");
+ phpProject.getConfiguration().setProperty("sonar.cpd.php.skip", "true");
assertTrue(sensor.isSkipped(phpProject));
+
+ Project javaProject = createJavaProject();
+ javaProject.getConfiguration().setProperty("sonar.cpd.skip", "false");
+ javaProject.getConfiguration().setProperty("sonar.cpd.php.skip", "true");
assertFalse(sensor.isSkipped(javaProject));
+
}
@Test
}
private Project createJavaProject() {
- return new Project("java_project").setLanguageKey("java").setLanguage(Java.INSTANCE);
+ PropertiesConfiguration conf = new PropertiesConfiguration();
+ conf.setProperty("sonar.language", "java");
+ return new Project("java_project").setConfiguration(conf).setLanguage(Java.INSTANCE);
}
private Project createPhpProject() {
+ PropertiesConfiguration conf = new PropertiesConfiguration();
+ conf.setProperty("sonar.language", "php");
Language phpLanguage = mock(Language.class);
- return new Project("php_project").setLanguageKey("php").setLanguage(phpLanguage);
+ return new Project("php_project").setConfiguration(conf).setLanguage(phpLanguage);
}
}
@Test
public void defaultMinimumTokens() {
- Project project = new Project("foo").setConfiguration(new PropertiesConfiguration());
+ Project project = newProject("foo", "java");
assertThat(SonarBridgeEngine.getMinimumTokens(project), is(CoreProperties.CPD_MINIMUM_TOKENS_DEFAULT_VALUE));
}
@Test
public void generalMinimumTokens() {
- PropertiesConfiguration conf = new PropertiesConfiguration();
- conf.setProperty("sonar.cpd.minimumTokens", "33");
- Project project = new Project("foo").setConfiguration(conf);
+ Project project = newProject("foo", "java");
+ project.getConfiguration().setProperty("sonar.cpd.minimumTokens", "33");
assertThat(SonarBridgeEngine.getMinimumTokens(project), is(33));
}
@Test
public void minimumTokensByLanguage() {
- PropertiesConfiguration conf = new PropertiesConfiguration();
- conf.setProperty("sonar.cpd.java.minimumTokens", "42");
- conf.setProperty("sonar.cpd.php.minimumTokens", "33");
-
- Project javaProject = new Project("foo").setLanguageKey("java").setConfiguration(conf);
- Project phpProject = new Project("foo").setLanguageKey("php").setConfiguration(conf);
-
+ Project javaProject = newProject("foo", "java");
+ javaProject.getConfiguration().setProperty("sonar.cpd.java.minimumTokens", "42");
+ javaProject.getConfiguration().setProperty("sonar.cpd.php.minimumTokens", "33");
assertThat(SonarBridgeEngine.getMinimumTokens(javaProject), is(42));
+
+ Project phpProject = newProject("foo", "php");
+ phpProject.getConfiguration().setProperty("sonar.cpd.java.minimumTokens", "42");
+ phpProject.getConfiguration().setProperty("sonar.cpd.php.minimumTokens", "33");
assertThat(SonarBridgeEngine.getMinimumTokens(phpProject), is(33));
}
+ private static Project newProject(String key, String language) {
+ PropertiesConfiguration conf = new PropertiesConfiguration();
+ conf.setProperty("sonar.language", language);
+ return new Project(key).setConfiguration(conf).setAnalysisType(Project.AnalysisType.DYNAMIC);
+ }
}
import org.sonar.api.Plugins;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric;
-import org.sonar.api.resources.Languages;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.ResourceTypes;
import org.sonar.batch.DefaultFileLinesContextFactory;
addCoreSingleton(DefaultNotificationManager.class);
addCoreSingleton(DefaultUserFinder.class);
addCoreSingleton(ResourceTypes.class);
- addCoreSingleton(Languages.class);
addCoreMetrics();
addBatchExtensions();
}
+++ /dev/null
-/*
- * 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.bootstrap;
-
-import org.apache.commons.lang.StringUtils;
-import org.sonar.api.BatchComponent;
-import org.sonar.api.resources.*;
-import org.sonar.api.utils.SonarException;
-import org.sonar.batch.config.ProjectSettings;
-import org.sonar.core.resource.ResourceDao;
-import org.sonar.core.resource.ResourceDto;
-
-/**
- * Should be dropped when org.sonar.api.resources.Project is fully refactored.
- */
-public class ProjectInitializer implements BatchComponent {
-
- private ResourceDao resourceDao;
- private DryRun dryRun;
- private Languages languages;
-
- public ProjectInitializer(ResourceDao resourceDao, DryRun dryRun, Languages languages) {
- this.resourceDao = resourceDao;
- this.dryRun = dryRun;
- this.languages = languages;
- }
-
- public void execute(Project project, ProjectSettings settings) {
- initLanguage(project, settings);
- }
-
- private void initLanguage(Project project, ProjectSettings settings) {
- project.setLanguageKey(StringUtils.defaultIfBlank(settings.getString("sonar.language"), Java.KEY));
- Language language = languages.get(project.getLanguageKey());
- if (language == null) {
- throw new SonarException("Language with key '" + project.getLanguageKey() + "' not found");
- }
- project.setLanguage(language);
- if (!dryRun.isEnabled() && project.getId() != null) {
- ResourceDto dto = resourceDao.getResource(project.getId());
- dto.setLanguage(project.getLanguageKey());
- resourceDao.insertOrUpdate(dto);
- }
-
- }
-}
import org.sonar.api.batch.BatchExtensionDictionnary;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.profiles.RulesProfile;
+import org.sonar.api.resources.Languages;
import org.sonar.api.resources.Project;
import org.sonar.api.utils.IocContainer;
import org.sonar.batch.*;
addCoreSingleton(projectDefinition);
addCoreSingleton(project);
addCoreSingleton(project.getConfiguration());
- addCoreSingleton(ProjectInitializer.class);
addCoreSingleton(ProjectSettings.class);
addCoreSingleton(UnsupportedProperties.class);
addCoreSingleton(IocContainer.class);
for (Object component : projectDefinition.getContainerExtensions()) {
addCoreSingleton(component);
}
+ addCoreSingleton(Languages.class);
addCoreSingleton(DefaultProjectClasspath.class);
addCoreSingleton(DefaultProjectFileSystem2.class);
addCoreSingleton(RulesDao.class);
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;
-import org.sonar.batch.bootstrap.ProjectInitializer;
import org.sonar.core.config.ConfigurationUtils;
import org.sonar.core.properties.PropertiesDao;
import org.sonar.core.properties.PropertyDto;
private ProjectDefinition projectDefinition;
private PropertiesDao propertiesDao;
- public ProjectSettings(PropertyDefinitions definitions, ProjectDefinition projectDefinition, PropertiesDao propertiesDao, Project project, ProjectInitializer initializer) {
+ public ProjectSettings(PropertyDefinitions definitions, ProjectDefinition projectDefinition, PropertiesDao propertiesDao, Project project) {
super(definitions);
this.deprecatedCommonsConf = project.getConfiguration(); // Configuration is not a parameter to be sure that the project conf is used, not the global one
this.projectDefinition = projectDefinition;
this.propertiesDao = propertiesDao;
load();
-
- // TODO should be refactored in a clean way
- initializer.execute(project, this);
}
public ProjectSettings load() {
import com.google.common.collect.Lists;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.resources.Project;
+import org.sonar.batch.phases.ProjectInitializer;
import org.sonar.batch.events.EventBus;
import org.sonar.batch.index.DefaultIndex;
import org.sonar.batch.index.PersistenceManager;
public static Collection<Class> getPhaseClasses(boolean dryRun) {
List<Class> classes = Lists.<Class>newArrayList(DecoratorsExecutor.class, MavenPhaseExecutor.class, MavenPluginsConfigurator.class,
PostJobsExecutor.class, SensorsExecutor.class,
- InitializersExecutor.class);
+ InitializersExecutor.class, ProjectInitializer.class);
if (!dryRun) {
classes.add(UpdateStatusJob.class);
}
private PersistenceManager persistenceManager;
private SensorContext sensorContext;
private DefaultIndex index;
+ private ProjectInitializer pi;
public Phases(DecoratorsExecutor decoratorsExecutor, MavenPhaseExecutor mavenPhaseExecutor,
MavenPluginsConfigurator mavenPluginsConfigurator, InitializersExecutor initializersExecutor,
PostJobsExecutor postJobsExecutor, SensorsExecutor sensorsExecutor,
PersistenceManager persistenceManager, SensorContext sensorContext, DefaultIndex index,
- EventBus eventBus, UpdateStatusJob updateStatusJob) {
+ EventBus eventBus, UpdateStatusJob updateStatusJob, ProjectInitializer pi) {
this.decoratorsExecutor = decoratorsExecutor;
this.mavenPhaseExecutor = mavenPhaseExecutor;
this.mavenPluginsConfigurator = mavenPluginsConfigurator;
this.index = index;
this.eventBus = eventBus;
this.updateStatusJob = updateStatusJob;
+ this.pi = pi;
}
public Phases(DecoratorsExecutor decoratorsExecutor, MavenPhaseExecutor mavenPhaseExecutor,
MavenPluginsConfigurator mavenPluginsConfigurator, InitializersExecutor initializersExecutor,
PostJobsExecutor postJobsExecutor, SensorsExecutor sensorsExecutor,
PersistenceManager persistenceManager, SensorContext sensorContext, DefaultIndex index,
- EventBus eventBus) {
+ EventBus eventBus, ProjectInitializer pi) {
this(decoratorsExecutor, mavenPhaseExecutor, mavenPluginsConfigurator, initializersExecutor, postJobsExecutor,
- sensorsExecutor, persistenceManager, sensorContext, index, eventBus, null);
+ sensorsExecutor, persistenceManager, sensorContext, index, eventBus, null, pi);
}
/**
* Executed on each module
*/
public void execute(Project project) {
+ pi.execute(project);
eventBus.fireEvent(new ProjectAnalysisEvent(project, true));
mavenPluginsConfigurator.execute(project);
mavenPhaseExecutor.execute(project);
--- /dev/null
+/*
+ * 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.phases;
+
+import org.sonar.api.BatchComponent;
+import org.sonar.api.resources.Language;
+import org.sonar.api.resources.Languages;
+import org.sonar.api.resources.Project;
+import org.sonar.api.utils.SonarException;
+import org.sonar.batch.bootstrap.DryRun;
+import org.sonar.core.resource.ResourceDao;
+import org.sonar.core.resource.ResourceDto;
+
+/**
+ * Should be dropped when org.sonar.api.resources.Project is fully refactored.
+ */
+public class ProjectInitializer implements BatchComponent {
+
+ private ResourceDao resourceDao;
+ private DryRun dryRun;
+ private Languages languages;
+
+ public ProjectInitializer(ResourceDao resourceDao, DryRun dryRun, Languages languages) {
+ this.resourceDao = resourceDao;
+ this.dryRun = dryRun;
+ this.languages = languages;
+ }
+
+ public void execute(Project project) {
+ initLanguage(project);
+ }
+
+ private void initLanguage(Project project) {
+ Language language = languages.get(project.getLanguageKey());
+ if (language == null) {
+ throw new SonarException("Language with key '" + project.getLanguageKey() + "' not found");
+ }
+ project.setLanguage(language);
+ if (!dryRun.isEnabled() && project.getId() != null) {
+ ResourceDto dto = resourceDao.getResource(project.getId());
+ dto.setLanguage(project.getLanguageKey());
+ resourceDao.insertOrUpdate(dto);
+ }
+
+ }
+}
*/
package org.sonar.batch;
+import org.apache.commons.configuration.PropertiesConfiguration;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
public ExpectedException thrown = ExpectedException.none();
private ProfilesDao dao;
- private Project javaProject = new Project("project").setLanguageKey(Java.KEY);
+ private Project javaProject = newProject(Java.KEY);
@Before
public void setUp() {
*/
@Test
public void should_give_explicit_message_if_default_profile_not_found() {
- Project cobolProject = new Project("cobol-javaProject").setLanguageKey("cobol");
+ Project cobolProject = newProject("cobol");
thrown.expect(SonarException.class);
thrown.expectMessage("You must install a plugin that supports the language 'cobol'");
new DefaultProfileLoader(dao, new Settings()).load(cobolProject);
}
+ private Project newProject(String language) {
+ PropertiesConfiguration configuration = new PropertiesConfiguration();
+ configuration.setProperty("sonar.language", language);
+ return new Project("project").setConfiguration(configuration);
+ }
}
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
+import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.maven.project.MavenProject;
import org.junit.Test;
import org.sonar.api.BatchExtension;
}
private static Project newJavaProject() {
- Project project = new Project("foo").setLanguageKey(Java.KEY).setAnalysisType(Project.AnalysisType.DYNAMIC);
+ PropertiesConfiguration configuration = new PropertiesConfiguration();
+ configuration.setProperty("sonar.language", Java.KEY);
+ Project project = new Project("foo").setConfiguration(configuration).setAnalysisType(Project.AnalysisType.DYNAMIC);
return project;
}
private static Project newGroovyProject() {
- return new Project("foo").setLanguageKey("grvy").setAnalysisType(Project.AnalysisType.DYNAMIC);
+ PropertiesConfiguration configuration = new PropertiesConfiguration();
+ configuration.setProperty("sonar.language", "grvy");
+ return new Project("foo").setConfiguration(configuration).setAnalysisType(Project.AnalysisType.DYNAMIC);
}
@Test
*/
package org.sonar.batch.index;
+import org.apache.commons.configuration.PropertiesConfiguration;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.database.model.ResourceModel;
@Before
public void before() throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
- singleProject = new Project("foo");
- singleProject.setName("Foo").setDescription("some description").setLanguageKey("java").setAnalysisDate(format.parse("25/12/2010"));
+ singleProject = newProject("foo", "java");
+ singleProject.setName("Foo").setDescription("some description").setAnalysisDate(format.parse("25/12/2010"));
- multiModuleProject = new Project("root");
- multiModuleProject.setName("Root").setLanguageKey("java").setAnalysisDate(format.parse("25/12/2010"));
+ multiModuleProject = newProject("root", "java");
+ multiModuleProject.setName("Root").setAnalysisDate(format.parse("25/12/2010"));
- moduleA = new Project("a");
- moduleA.setName("A").setLanguageKey("java").setAnalysisDate(format.parse("25/12/2010"));
+ moduleA = newProject("a", "java");
+ moduleA.setName("A").setAnalysisDate(format.parse("25/12/2010"));
moduleA.setParent(multiModuleProject);
- moduleB = new Project("b");
- moduleB.setName("B").setLanguageKey("java").setAnalysisDate(format.parse("25/12/2010"));
+ moduleB = newProject("b", "java");
+ moduleB.setName("B").setAnalysisDate(format.parse("25/12/2010"));
moduleB.setParent(multiModuleProject);
- moduleB1 = new Project("b1");
- moduleB1.setName("B1").setLanguageKey("java").setAnalysisDate(format.parse("25/12/2010"));
+ moduleB1 = newProject("b1", "java");
+ moduleB1.setName("B1").setAnalysisDate(format.parse("25/12/2010"));
moduleB1.setParent(moduleB);
}
// check that the directory is attached to the project
checkTables("shouldSaveNewDirectory", new String[] {"build_date", "created_at"}, "projects", "snapshots");
}
-
@Test
public void shouldSaveNewLibrary() {
setupData("shared");
checkTables("shouldRemoveRootIndexIfResourceIsProject", new String[] {"build_date", "created_at"}, "projects", "snapshots");
}
+ private static Project newProject(String key, String language) {
+ PropertiesConfiguration configuration = new PropertiesConfiguration();
+ configuration.setProperty("sonar.language", language);
+ return new Project(key).setConfiguration(configuration).setAnalysisType(Project.AnalysisType.DYNAMIC);
+ }
+
}
* @param name the display name of the language in the interface
*/
public AbstractLanguage(String key, String name) {
- Preconditions.checkArgument(key.length() < 21, "The following language key exceeds 20 characters: '" + key + "'");
+ Preconditions.checkArgument(key.length() <= 20, "The following language key exceeds 20 characters: '" + key + "'");
this.key = key.toLowerCase(Locale.ENGLISH);
this.name = name;
}
import org.sonar.api.BatchExtension;
import org.sonar.api.ServerExtension;
-import org.sonar.api.batch.InstantiationStrategy;
/**
* The extension point to define a new language
*
* @since 1.10
*/
-@InstantiationStrategy(InstantiationStrategy.PER_BATCH)
public interface Language extends BatchExtension, ServerExtension {
/**
*
* @since 1.10
*/
-@InstantiationStrategy(InstantiationStrategy.PER_BATCH)
public class Languages implements BatchComponent, ServerComponent {
private final Map<String, Language> map = Maps.newHashMap();
private String description;
private String packaging;
private Language language;
- private String languageKey;
private Date analysisDate;
private AnalysisType analysisType;
private String analysisVersion;
* @return the language key
*/
public String getLanguageKey() {
- return languageKey;
- }
-
- /**
- * For internal use only.
- */
- public Project setLanguageKey(String languageKey) {
- this.languageKey = languageKey;
- return this;
+ return configuration.getString("sonar.language", Java.KEY);
}
/**