import org.sonar.core.issue.db.UpdateConflictResolver;
import org.sonar.core.platform.ComponentContainer;
import org.sonar.server.computation.component.DbComponentsRefCache;
+import org.sonar.server.computation.component.ProjectSettingsRepository;
import org.sonar.server.computation.issue.IssueCache;
import org.sonar.server.computation.issue.IssueComputation;
import org.sonar.server.computation.issue.RuleCache;
return Arrays.asList(
// context-scope repositories
PlatformLanguageRepository.class,
+ ProjectSettingsRepository.class,
ComputationService.class,
ComputationSteps.class,
public class ComputationContext implements org.sonar.server.computation.context.ComputationContext {
private final BatchReportReader reportReader;
- private final Settings projectSettings;
private final DbClient dbClient;
// cache of metadata as it's frequently accessed
private final BatchReport.Metadata reportMetadata;
public ComputationContext(BatchReportReader reportReader, String projectKey, Settings projectSettings, DbClient dbClient,
ComponentTreeBuilder componentTreeBuilder, LanguageRepository languageRepository) {
this.reportReader = reportReader;
- this.projectSettings = projectSettings;
this.dbClient = dbClient;
this.reportMetadata = reportReader.readMetadata();
this.component = componentTreeBuilder.build(this);
return reportReader;
}
- public Settings getProjectSettings() {
- return projectSettings;
- }
-
@Override
public Component getRoot() {
return component;
import com.google.common.base.Throwables;
import org.apache.commons.io.FileUtils;
-import org.sonar.api.config.Settings;
import org.sonar.api.server.ServerSide;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.TempFolder;
try {
File reportDir = extractReportInDir(item);
BatchReportReader reader = new BatchReportReader(reportDir);
- Settings projectSettings = projectSettingsFactory.newProjectSettings(projectKey);
- ComputationContext context = new ComputationContext(reader, null, projectSettings, dbClient, ComponentTreeBuilders.from(reader), languageRepository);
+ ComputationContext context = new ComputationContext(reader, null, null, dbClient, ComponentTreeBuilders.from(reader), languageRepository);
for (ComputationStep step : steps.orderedSteps()) {
Profiler stepProfiler = Profiler.createIfDebug(LOG).startDebug(step.getDescription());
step.execute(context);
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.computation.component;
+
+import org.sonar.api.config.Settings;
+import org.sonar.server.properties.ProjectSettingsFactory;
+
+/**
+ * Lazy loading of project settings.
+ *
+ * Should be refactored to be able to load settings from any components.
+ */
+public class ProjectSettingsRepository {
+
+ private final ProjectSettingsFactory projectSettingsFactory;
+
+ private Settings projectSettings;
+
+ public ProjectSettingsRepository(ProjectSettingsFactory projectSettingsFactory) {
+ this.projectSettingsFactory = projectSettingsFactory;
+ }
+
+ public Settings getProjectSettings(String projectKey){
+ if (projectSettings == null) {
+ projectSettings = projectSettingsFactory.newProjectSettings(projectKey);
+ }
+ return projectSettings;
+ }
+
+}
import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.core.rule.RuleDto;
import org.sonar.server.computation.ComputationContext;
+import org.sonar.server.computation.component.ProjectSettingsRepository;
import org.sonar.server.user.index.UserDoc;
import org.sonar.server.user.index.UserIndex;
import org.sonar.server.util.cache.DiskCache;
private final SourceLinesCache linesCache;
private final DiskCache<DefaultIssue>.DiskAppender diskIssuesAppender;
private final UserIndex userIndex;
+ private final ProjectSettingsRepository projectSettingsRepository;
private boolean hasAssigneeBeenComputed = false;
private String defaultAssignee = null;
public IssueComputation(RuleCache ruleCache, SourceLinesCache linesCache, ScmAccountCache scmAccountCache,
- IssueCache issueCache, UserIndex userIndex) {
+ IssueCache issueCache, UserIndex userIndex, ProjectSettingsRepository projectSettingsRepository) {
this.ruleCache = ruleCache;
this.linesCache = linesCache;
this.scmAccountCache = scmAccountCache;
this.userIndex = userIndex;
+ this.projectSettingsRepository = projectSettingsRepository;
this.diskIssuesAppender = issueCache.newAppender();
}
public void processComponentIssues(ComputationContext context, Iterable<BatchReport.Issue> issues, String componentUuid, @Nullable Integer componentReportRef,
String projectKey, String projectUuid) {
linesCache.init(componentUuid, componentReportRef, context.getReportReader());
- computeDefaultAssignee(context.getProjectSettings().getString(CoreProperties.DEFAULT_ISSUE_ASSIGNEE));
+ computeDefaultAssignee(projectSettingsRepository.getProjectSettings(projectKey).getString(CoreProperties.DEFAULT_ISSUE_ASSIGNEE));
for (BatchReport.Issue reportIssue : issues) {
DefaultIssue issue = toDefaultIssue(context, componentUuid, reportIssue, projectKey, projectUuid);
if (issue.isNew()) {
import org.sonar.core.purge.IdUuidPair;
import org.sonar.server.computation.ComputationContext;
import org.sonar.server.computation.component.DbComponentsRefCache;
+import org.sonar.server.computation.component.ProjectSettingsRepository;
import org.sonar.server.db.DbClient;
public class PurgeDatastoresStep implements ComputationStep {
private final ProjectCleaner projectCleaner;
private final DbClient dbClient;
private final DbComponentsRefCache dbComponentsRefCache;
+ private final ProjectSettingsRepository projectSettingsRepository;
- public PurgeDatastoresStep(DbClient dbClient, ProjectCleaner projectCleaner, DbComponentsRefCache dbComponentsRefCache) {
+ public PurgeDatastoresStep(DbClient dbClient, ProjectCleaner projectCleaner, DbComponentsRefCache dbComponentsRefCache, ProjectSettingsRepository projectSettingsRepository) {
this.projectCleaner = projectCleaner;
this.dbClient = dbClient;
this.dbComponentsRefCache = dbComponentsRefCache;
+ this.projectSettingsRepository = projectSettingsRepository;
}
@Override
DbSession session = dbClient.openSession(true);
try {
DbComponentsRefCache.DbComponent project = dbComponentsRefCache.getByRef(context.getReportMetadata().getRootComponentRef());
- projectCleaner.purge(session, new IdUuidPair(project.getId(), project.getUuid()), context.getProjectSettings());
+ projectCleaner.purge(session, new IdUuidPair(project.getId(), project.getUuid()), projectSettingsRepository.getProjectSettings(project.getKey()));
session.commit();
} finally {
MyBatis.closeQuietly(session);
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.computation.component;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.sonar.api.config.Settings;
+import org.sonar.core.component.ComponentDto;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.persistence.DbTester;
+import org.sonar.core.properties.PropertiesDao;
+import org.sonar.core.properties.PropertyDto;
+import org.sonar.server.component.ComponentTesting;
+import org.sonar.server.component.db.ComponentDao;
+import org.sonar.server.db.DbClient;
+import org.sonar.server.properties.ProjectSettingsFactory;
+import org.sonar.test.DbTests;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+@Category(DbTests.class)
+public class ProjectSettingsRepositoryTest {
+
+ private static final String PROJECT_KEY = "PROJECT_KEY";
+
+ @ClassRule
+ public static final DbTester dbTester = new DbTester();
+
+ DbClient dbClient;
+
+ DbSession session;
+
+ Settings globalSettings;
+
+ ProjectSettingsRepository sut;
+
+ @Before
+ public void createDao() {
+ dbTester.truncateTables();
+ globalSettings = new Settings();
+ PropertiesDao propertiesDao = new PropertiesDao(dbTester.myBatis());
+ dbClient = new DbClient(dbTester.database(), dbTester.myBatis(), propertiesDao, new ComponentDao());
+ session = dbClient.openSession(false);
+ sut = new ProjectSettingsRepository(new ProjectSettingsFactory(globalSettings, propertiesDao));
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ session.close();
+ }
+
+ @Test
+ public void get_project_settings_from_global_settings() throws Exception {
+ globalSettings.setProperty("key", "value");
+
+ Settings settings = sut.getProjectSettings(PROJECT_KEY);
+
+ assertThat(settings.getString("key")).isEqualTo("value");
+ }
+
+ @Test
+ public void get_project_settings_from_db() throws Exception {
+ ComponentDto project = ComponentTesting.newProjectDto().setKey(PROJECT_KEY);
+ dbClient.componentDao().insert(session, project);
+ dbClient.propertiesDao().setProperty(new PropertyDto().setResourceId(project.getId()).setKey("key").setValue("value"), session);
+ session.commit();
+
+ Settings settings = sut.getProjectSettings(PROJECT_KEY);
+
+ assertThat(settings.getString("key")).isEqualTo("value");
+ }
+
+ @Test
+ public void call_twice_get_project_settings() throws Exception {
+ globalSettings.setProperty("key", "value");
+
+ Settings settings = sut.getProjectSettings(PROJECT_KEY);
+ assertThat(settings.getString("key")).isEqualTo("value");
+
+ settings = sut.getProjectSettings(PROJECT_KEY);
+ assertThat(settings.getString("key")).isEqualTo("value");
+ }
+}
import org.junit.rules.TemporaryFolder;
import org.mockito.Mockito;
import org.sonar.api.CoreProperties;
+import org.sonar.api.config.Settings;
import org.sonar.api.issue.Issue;
import org.sonar.api.issue.internal.DefaultIssue;
import org.sonar.api.rule.RuleKey;
import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.core.rule.RuleDto;
import org.sonar.server.computation.ComputationContext;
+import org.sonar.server.computation.component.ProjectSettingsRepository;
import org.sonar.server.user.index.UserDoc;
import org.sonar.server.user.index.UserIndex;
public class IssueComputationTest {
- public static final RuleKey RULE_KEY = RuleKey.of("squid", "R1");
+ private static final RuleKey RULE_KEY = RuleKey.of("squid", "R1");
+ private static final String PROJECT_KEY = "PROJECT_KEY";
@Rule
public TemporaryFolder temp = new TemporaryFolder();
.setRuleRepository(RULE_KEY.repository())
.setRuleKey(RULE_KEY.rule())
.setStatus(Issue.STATUS_OPEN);
+ Settings projectSettings;
+ ProjectSettingsRepository projectSettingsRepository = mock(ProjectSettingsRepository.class);
ComputationContext context = mock(ComputationContext.class, Mockito.RETURNS_DEEP_STUBS);
UserIndex userIndex = mock(UserIndex.class);
public void setUp() throws IOException {
when(ruleCache.get(RULE_KEY)).thenReturn(rule);
outputIssues = new IssueCache(temp.newFile(), System2.INSTANCE);
- sut = new IssueComputation(ruleCache, lineCache, scmAccountCache, outputIssues, userIndex);
+ projectSettings = new Settings();
+ when(projectSettingsRepository.getProjectSettings(PROJECT_KEY)).thenReturn(projectSettings);
+ sut = new IssueComputation(ruleCache, lineCache, scmAccountCache, outputIssues, userIndex, projectSettingsRepository);
}
@After
public void assign_default_assignee_when_available() {
inputIssue.setIsNew(true);
String wolinski = "wolinski";
- when(context.getProjectSettings().getString(CoreProperties.DEFAULT_ISSUE_ASSIGNEE)).thenReturn(wolinski);
+ projectSettings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, wolinski);
when(userIndex.getNullableByLogin(wolinski)).thenReturn(new UserDoc());
process();
public void do_not_assign_default_assignee_when_not_found_in_index() {
inputIssue.setIsNew(true);
String wolinski = "wolinski";
- when(context.getProjectSettings().getString(CoreProperties.DEFAULT_ISSUE_ASSIGNEE)).thenReturn(wolinski);
+ projectSettings.setProperty(CoreProperties.DEFAULT_ISSUE_ASSIGNEE, wolinski);
when(userIndex.getNullableByLogin(wolinski)).thenReturn(null);
process();
}
private void process() {
- sut.processComponentIssues(context, Arrays.asList(inputIssue.build()), "FILE_A", 1, "PROJECT_KEY", "PROJECT_UUID");
+ sut.processComponentIssues(context, Arrays.asList(inputIssue.build()), "FILE_A", 1, PROJECT_KEY, "PROJECT_UUID");
}
}
package org.sonar.server.computation.step;
+import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.batch.protocol.output.BatchReportReader;
import org.sonar.batch.protocol.output.BatchReportWriter;
-import org.sonar.core.component.ComponentDto;
import org.sonar.core.computation.dbcleaner.ProjectCleaner;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.purge.IdUuidPair;
import org.sonar.server.computation.ComputationContext;
import org.sonar.server.computation.component.ComponentTreeBuilders;
+import org.sonar.server.computation.component.DbComponentsRefCache;
import org.sonar.server.computation.component.DumbComponent;
+import org.sonar.server.computation.component.ProjectSettingsRepository;
import org.sonar.server.computation.language.LanguageRepository;
-import org.sonar.server.computation.component.DbComponentsRefCache;
import org.sonar.server.db.DbClient;
import java.io.File;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
public class PurgeDatastoresStepTest extends BaseStepTest {
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
private static final String PROJECT_KEY = "PROJECT_KEY";
ProjectCleaner projectCleaner = mock(ProjectCleaner.class);
DbComponentsRefCache dbComponentsRefCache = new DbComponentsRefCache();
- PurgeDatastoresStep sut = new PurgeDatastoresStep(mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS), projectCleaner, dbComponentsRefCache);
+ ProjectSettingsRepository projectSettingsRepository = mock(ProjectSettingsRepository.class);
+ PurgeDatastoresStep sut = new PurgeDatastoresStep(mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS), projectCleaner, dbComponentsRefCache, projectSettingsRepository);
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
+ Settings projectSettings;
+
+ @Before
+ public void setUp() throws Exception {
+ projectSettings = new Settings();
+ when(projectSettingsRepository.getProjectSettings(PROJECT_KEY)).thenReturn(projectSettings);
+ }
@Test
public void call_purge_method_of_the_purge_task() throws IOException {
.setRootComponentRef(1)
.build());
- ComponentDto project = mock(ComponentDto.class);
- ComputationContext context = new ComputationContext(new BatchReportReader(reportDir), PROJECT_KEY, new Settings(), mock(DbClient.class), ComponentTreeBuilders.from(DumbComponent.DUMB_PROJECT), mock(LanguageRepository.class));
+ ComputationContext context = new ComputationContext(new BatchReportReader(reportDir), PROJECT_KEY, new Settings(),
+ mock(DbClient.class), ComponentTreeBuilders.from(DumbComponent.DUMB_PROJECT), mock(LanguageRepository.class));
sut.execute(context);
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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 this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-
-package org.sonar.server.properties;
-
-import com.google.common.collect.Lists;
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.api.config.Settings;
-import org.sonar.core.properties.PropertiesDao;
-import org.sonar.core.properties.PropertyDto;
-
-import java.util.Map;
-
-import static com.google.common.collect.Lists.newArrayList;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-
-public class ProjectSettingsFactoryTest {
-
- private ProjectSettingsFactory sut;
-
- @Before
- public void before() {
- Settings settings = mock(Settings.class);
- PropertiesDao dao = mock(PropertiesDao.class);
-
- this.sut = new ProjectSettingsFactory(settings, dao);
- }
-
- @Test
- public void newProjectSettings_returns_a_ProjectSettings() {
- Settings projectSettings = sut.newProjectSettings("PROJECT_KEY");
-
- assertThat(projectSettings).isInstanceOf(ProjectSettings.class);
- }
-
- @Test
- public void transform_empty_list_into_empty_map() {
- Map<String, String> propertyMap = sut.getPropertyMap(Lists.<PropertyDto>newArrayList());
-
- assertThat(propertyMap).isEmpty();
- }
-
- @Test
- public void transform_list_of_properties_in_map_key_value() {
- PropertyDto property1 = new PropertyDto().setKey("1").setValue("val1");
- PropertyDto property2 = new PropertyDto().setKey("2").setValue("val2");
- PropertyDto property3 = new PropertyDto().setKey("3").setValue("val3");
-
- Map<String, String> propertyMap = sut.getPropertyMap(newArrayList(property1, property2, property3));
-
- assertThat(propertyMap.get("1")).isEqualTo("val1");
- assertThat(propertyMap.get("2")).isEqualTo("val2");
- assertThat(propertyMap.get("3")).isEqualTo("val3");
- }
-}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.properties;
+
+import com.google.common.collect.Lists;
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.config.Settings;
+import org.sonar.core.properties.PropertiesDao;
+import org.sonar.core.properties.PropertyDto;
+
+import java.util.Map;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+
+public class ProjectSettingsRespositoryFactoryTest {
+
+ private ProjectSettingsFactory sut;
+
+ @Before
+ public void before() {
+ Settings settings = mock(Settings.class);
+ PropertiesDao dao = mock(PropertiesDao.class);
+
+ this.sut = new ProjectSettingsFactory(settings, dao);
+ }
+
+ @Test
+ public void newProjectSettings_returns_a_ProjectSettings() {
+ Settings projectSettings = sut.newProjectSettings("PROJECT_KEY");
+
+ assertThat(projectSettings).isInstanceOf(ProjectSettings.class);
+ }
+
+ @Test
+ public void transform_empty_list_into_empty_map() {
+ Map<String, String> propertyMap = sut.getPropertyMap(Lists.<PropertyDto>newArrayList());
+
+ assertThat(propertyMap).isEmpty();
+ }
+
+ @Test
+ public void transform_list_of_properties_in_map_key_value() {
+ PropertyDto property1 = new PropertyDto().setKey("1").setValue("val1");
+ PropertyDto property2 = new PropertyDto().setKey("2").setValue("val2");
+ PropertyDto property3 = new PropertyDto().setKey("3").setValue("val3");
+
+ Map<String, String> propertyMap = sut.getPropertyMap(newArrayList(property1, property2, property3));
+
+ assertThat(propertyMap.get("1")).isEqualTo("val1");
+ assertThat(propertyMap.get("2")).isEqualTo("val2");
+ assertThat(propertyMap.get("3")).isEqualTo("val3");
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.properties;
+
+import com.google.common.collect.Maps;
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.config.Settings;
+
+import java.util.HashMap;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+public class ProjectSettingsRespositoryTest {
+
+ private ProjectSettings sut;
+
+ private Settings settings;
+
+ @Before
+ public void before() {
+ this.settings = mock(Settings.class);
+ }
+
+ @Test
+ public void call_global_settings_method_when_no_project_specific_settings() {
+ this.sut = new ProjectSettings(settings, Maps.<String, String>newHashMap());
+
+ sut.getInt("anyKey");
+ sut.getBoolean("anyKey");
+ sut.getString("anyKey");
+
+ verify(settings, times(1)).getBoolean(anyString());
+ verify(settings, times(1)).getInt(anyString());
+ verify(settings, times(1)).getString(anyString());
+ }
+
+ @Test(expected = NumberFormatException.class)
+ public void getInt_property_throws_exception_when_value_is_not_formatted_correctly() {
+ HashMap<String, String> properties = Maps.newHashMap();
+ properties.put("intKey", "wrongIntValue");
+ this.sut = new ProjectSettings(settings, properties);
+
+ sut.getInt("intKey");
+ }
+
+ @Test
+ public void getInt_property_return_0_when_empty_property() {
+ HashMap<String, String> properties = Maps.newHashMap();
+ properties.put("intKey", "");
+ this.sut = new ProjectSettings(settings, properties);
+
+ int value = sut.getInt("intKey");
+
+ assertThat(value).isEqualTo(0);
+ }
+
+ @Test
+ public void getInt_property_return_the_int_value() {
+ HashMap<String, String> properties = Maps.newHashMap();
+ properties.put("intKey", "123");
+ this.sut = new ProjectSettings(settings, properties);
+
+ int value = sut.getInt("intKey");
+
+ assertThat(value).isEqualTo(123);
+ }
+
+ @Test
+ public void getString_returns_String_property() {
+ HashMap<String, String> properties = Maps.newHashMap();
+ properties.put("stringKey", "stringValue");
+ this.sut = new ProjectSettings(settings, properties);
+
+ String value = sut.getString("stringKey");
+
+ assertThat(value).isEqualTo("stringValue");
+ }
+
+ @Test
+ public void getBoolean_returns_exception_when_value_is_not_formatted_correctly() {
+ HashMap<String, String> properties = Maps.newHashMap();
+ properties.put("boolKey", "wronglyFormattedBoolean");
+ this.sut = new ProjectSettings(settings, properties);
+
+ boolean key = sut.getBoolean("boolKey");
+
+ assertThat(key).isFalse();
+ }
+
+ @Test
+ public void getBoolean_returns_false_when_value_is_empty() {
+ HashMap<String, String> properties = Maps.newHashMap();
+ properties.put("boolKey", "");
+ this.sut = new ProjectSettings(settings, properties);
+
+ boolean key = sut.getBoolean("boolKey");
+
+ assertThat(key).isFalse();
+ }
+
+ @Test
+ public void getBoolean_returns_true_when_value_is_true_ignoring_case() {
+ HashMap<String, String> properties = Maps.newHashMap();
+ properties.put("boolKey1", "true");
+ properties.put("boolKey2", "True");
+ this.sut = new ProjectSettings(settings, properties);
+
+ boolean key1 = sut.getBoolean("boolKey1");
+ boolean key2 = sut.getBoolean("boolKey2");
+
+ assertThat(key1).isTrue();
+ assertThat(key2).isTrue();
+ }
+}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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 this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-
-package org.sonar.server.properties;
-
-import com.google.common.collect.Maps;
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.api.config.Settings;
-
-import java.util.HashMap;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.*;
-
-public class ProjectSettingsTest {
-
- private ProjectSettings sut;
-
- private Settings settings;
-
- @Before
- public void before() {
- this.settings = mock(Settings.class);
- }
-
- @Test
- public void call_global_settings_method_when_no_project_specific_settings() {
- this.sut = new ProjectSettings(settings, Maps.<String, String>newHashMap());
-
- sut.getInt("anyKey");
- sut.getBoolean("anyKey");
- sut.getString("anyKey");
-
- verify(settings, times(1)).getBoolean(anyString());
- verify(settings, times(1)).getInt(anyString());
- verify(settings, times(1)).getString(anyString());
- }
-
- @Test(expected = NumberFormatException.class)
- public void getInt_property_throws_exception_when_value_is_not_formatted_correctly() {
- HashMap<String, String> properties = Maps.newHashMap();
- properties.put("intKey", "wrongIntValue");
- this.sut = new ProjectSettings(settings, properties);
-
- sut.getInt("intKey");
- }
-
- @Test
- public void getInt_property_return_0_when_empty_property() {
- HashMap<String, String> properties = Maps.newHashMap();
- properties.put("intKey", "");
- this.sut = new ProjectSettings(settings, properties);
-
- int value = sut.getInt("intKey");
-
- assertThat(value).isEqualTo(0);
- }
-
- @Test
- public void getInt_property_return_the_int_value() {
- HashMap<String, String> properties = Maps.newHashMap();
- properties.put("intKey", "123");
- this.sut = new ProjectSettings(settings, properties);
-
- int value = sut.getInt("intKey");
-
- assertThat(value).isEqualTo(123);
- }
-
- @Test
- public void getString_returns_String_property() {
- HashMap<String, String> properties = Maps.newHashMap();
- properties.put("stringKey", "stringValue");
- this.sut = new ProjectSettings(settings, properties);
-
- String value = sut.getString("stringKey");
-
- assertThat(value).isEqualTo("stringValue");
- }
-
- @Test
- public void getBoolean_returns_exception_when_value_is_not_formatted_correctly() {
- HashMap<String, String> properties = Maps.newHashMap();
- properties.put("boolKey", "wronglyFormattedBoolean");
- this.sut = new ProjectSettings(settings, properties);
-
- boolean key = sut.getBoolean("boolKey");
-
- assertThat(key).isFalse();
- }
-
- @Test
- public void getBoolean_returns_false_when_value_is_empty() {
- HashMap<String, String> properties = Maps.newHashMap();
- properties.put("boolKey", "");
- this.sut = new ProjectSettings(settings, properties);
-
- boolean key = sut.getBoolean("boolKey");
-
- assertThat(key).isFalse();
- }
-
- @Test
- public void getBoolean_returns_true_when_value_is_true_ignoring_case() {
- HashMap<String, String> properties = Maps.newHashMap();
- properties.put("boolKey1", "true");
- properties.put("boolKey2", "True");
- this.sut = new ProjectSettings(settings, properties);
-
- boolean key1 = sut.getBoolean("boolKey1");
- boolean key2 = sut.getBoolean("boolKey2");
-
- assertThat(key1).isTrue();
- assertThat(key2).isTrue();
- }
-}