You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProjectConfigurationFactoryTest.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.ce.task.projectanalysis.analysis;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.sonar.api.config.Configuration;
  24. import org.sonar.api.config.internal.MapSettings;
  25. import org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl;
  26. import org.sonar.db.DbTester;
  27. import org.sonar.db.component.ComponentDto;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. import static org.mockito.Mockito.mock;
  30. import static org.mockito.Mockito.when;
  31. import static org.sonar.db.property.PropertyTesting.newComponentPropertyDto;
  32. public class ProjectConfigurationFactoryTest {
  33. private static final String PROJECT_KEY = "PROJECT_KEY";
  34. @Rule
  35. public DbTester db = DbTester.create();
  36. private MapSettings settings = new MapSettings();
  37. private ProjectConfigurationFactory underTest = new ProjectConfigurationFactory(settings, db.getDbClient());
  38. @Test
  39. public void return_global_settings() {
  40. settings.setProperty("key", "value");
  41. Configuration config = underTest.newProjectConfiguration(PROJECT_KEY, new DefaultBranchImpl());
  42. assertThat(config.get("key")).hasValue("value");
  43. }
  44. @Test
  45. public void return_project_settings() {
  46. ComponentDto project = db.components().insertPrivateProject();
  47. db.properties().insertProperties(
  48. newComponentPropertyDto(project).setKey("1").setValue("val1"),
  49. newComponentPropertyDto(project).setKey("2").setValue("val2"),
  50. newComponentPropertyDto(project).setKey("3").setValue("val3"));
  51. Configuration config = underTest.newProjectConfiguration(project.getDbKey(), new DefaultBranchImpl());
  52. assertThat(config.get("1")).hasValue("val1");
  53. assertThat(config.get("2")).hasValue("val2");
  54. assertThat(config.get("3")).hasValue("val3");
  55. }
  56. @Test
  57. public void project_settings_override_global_settings() {
  58. settings.setProperty("key", "value");
  59. ComponentDto project = db.components().insertPrivateProject();
  60. db.properties().insertProperties(newComponentPropertyDto(project).setKey("key").setValue("value2"));
  61. Configuration projectConfig = underTest.newProjectConfiguration(project.getDbKey(), new DefaultBranchImpl());
  62. assertThat(projectConfig.get("key")).hasValue("value2");
  63. }
  64. @Test
  65. public void branch_settings() {
  66. ComponentDto project = db.components().insertMainBranch();
  67. ComponentDto branch = db.components().insertProjectBranch(project);
  68. db.properties().insertProperties(newComponentPropertyDto(branch).setKey("sonar.leak.period").setValue("1"));
  69. Configuration config = underTest.newProjectConfiguration(project.getKey(), createBranch(branch.getBranch(), false));
  70. assertThat(config.get("sonar.leak.period")).hasValue("1");
  71. }
  72. @Test
  73. public void branch_settings_contains_global_settings() {
  74. settings.setProperty("global", "global_value");
  75. ComponentDto project = db.components().insertMainBranch();
  76. ComponentDto branch = db.components().insertProjectBranch(project);
  77. db.properties().insertProperties(newComponentPropertyDto(branch).setKey("sonar.leak.period").setValue("1"));
  78. Configuration config = underTest.newProjectConfiguration(project.getKey(), createBranch(branch.getBranch(), false));
  79. assertThat(config.get("global")).hasValue("global_value");
  80. assertThat(config.get("sonar.leak.period")).hasValue("1");
  81. }
  82. @Test
  83. public void branch_settings_contains_project_settings() {
  84. ComponentDto project = db.components().insertMainBranch();
  85. db.properties().insertProperties(newComponentPropertyDto(project).setKey("key").setValue("value"));
  86. ComponentDto branch = db.components().insertProjectBranch(project);
  87. db.properties().insertProperties(newComponentPropertyDto(branch).setKey("sonar.leak.period").setValue("1"));
  88. Configuration config = underTest.newProjectConfiguration(project.getKey(), createBranch(branch.getBranch(), false));
  89. assertThat(config.get("key")).hasValue("value");
  90. assertThat(config.get("sonar.leak.period")).hasValue("1");
  91. }
  92. @Test
  93. public void branch_settings_override_project_settings() {
  94. ComponentDto project = db.components().insertMainBranch();
  95. db.properties().insertProperties(newComponentPropertyDto(project).setKey("sonar.leak.period").setValue("1"));
  96. ComponentDto branch = db.components().insertProjectBranch(project);
  97. db.properties().insertProperties(newComponentPropertyDto(branch).setKey("sonar.leak.period").setValue("2"));
  98. Configuration config = underTest.newProjectConfiguration(project.getKey(), createBranch(branch.getBranch(), false));
  99. assertThat(config.get("sonar.leak.period")).hasValue("2");
  100. }
  101. @Test
  102. public void main_branch() {
  103. ComponentDto project = db.components().insertMainBranch();
  104. db.properties().insertProperties(newComponentPropertyDto(project).setKey("sonar.leak.period").setValue("1"));
  105. Branch branch = createBranch("master", true);
  106. when(branch.isMain()).thenReturn(true);
  107. Configuration config = underTest.newProjectConfiguration(project.getKey(), createBranch(branch.getName(), true));
  108. assertThat(config.get("sonar.leak.period")).hasValue("1");
  109. }
  110. private static Branch createBranch(String name, boolean isMain) {
  111. Branch branch = mock(Branch.class);
  112. when(branch.getName()).thenReturn(name);
  113. when(branch.isMain()).thenReturn(isMain);
  114. return branch;
  115. }
  116. }