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.

DuplicationDataMeasuresStepTest.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.step;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
  24. import org.sonar.ce.task.projectanalysis.duplication.DuplicationRepositoryRule;
  25. import org.sonar.ce.task.projectanalysis.duplication.TextBlock;
  26. import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
  27. import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
  28. import org.sonar.ce.task.step.ComputationStep;
  29. import org.sonar.ce.task.step.TestComputationStepContext;
  30. import static org.assertj.core.api.Assertions.assertThat;
  31. import static org.sonar.api.measures.CoreMetrics.DUPLICATIONS_DATA;
  32. import static org.sonar.api.measures.CoreMetrics.DUPLICATIONS_DATA_KEY;
  33. import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
  34. import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
  35. import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
  36. public class DuplicationDataMeasuresStepTest extends BaseStepTest {
  37. private static final int ROOT_REF = 1;
  38. private static final String PROJECT_KEY = "PROJECT_KEY";
  39. private static final int FILE_1_REF = 2;
  40. private static final String FILE_1_KEY = "FILE_1_KEY";
  41. private static final int FILE_2_REF = 3;
  42. private static final String FILE_2_KEY = "FILE_2_KEY";
  43. @Rule
  44. public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule()
  45. .setRoot(
  46. builder(PROJECT, ROOT_REF).setKey(PROJECT_KEY)
  47. .addChildren(
  48. builder(FILE, FILE_1_REF).setKey(FILE_1_KEY)
  49. .build(),
  50. builder(FILE, FILE_2_REF).setKey(FILE_2_KEY)
  51. .build())
  52. .build());
  53. @Rule
  54. public DuplicationRepositoryRule duplicationRepository = DuplicationRepositoryRule.create(treeRootHolder);
  55. @Rule
  56. public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
  57. .add(DUPLICATIONS_DATA);
  58. @Rule
  59. public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
  60. private DuplicationDataMeasuresStep underTest = new DuplicationDataMeasuresStep(treeRootHolder, metricRepository, measureRepository, duplicationRepository);
  61. @Override
  62. protected ComputationStep step() {
  63. return underTest;
  64. }
  65. @Test
  66. public void nothing_to_do_when_no_duplication() {
  67. underTest.execute(new TestComputationStepContext());
  68. assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATIONS_DATA_KEY)).isNotPresent();
  69. assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, DUPLICATIONS_DATA_KEY)).isNotPresent();
  70. }
  71. @Test
  72. public void compute_duplications_on_same_file() {
  73. duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(1, 5), new TextBlock(6, 10));
  74. underTest.execute(new TestComputationStepContext());
  75. assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATIONS_DATA_KEY)).isPresent();
  76. assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATIONS_DATA_KEY).get().getData()).isEqualTo(
  77. "<duplications><g><b s=\"1\" l=\"5\" t=\"false\" r=\"" + FILE_1_KEY + "\"/><b s=\"6\" l=\"5\" t=\"false\" r=\""
  78. + FILE_1_KEY + "\"/></g></duplications>");
  79. assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, DUPLICATIONS_DATA_KEY)).isNotPresent();
  80. }
  81. @Test
  82. public void compute_duplications_on_different_files() {
  83. duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(1, 5), FILE_2_REF, new TextBlock(6, 10));
  84. underTest.execute(new TestComputationStepContext());
  85. assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATIONS_DATA_KEY)).isPresent();
  86. assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATIONS_DATA_KEY).get().getData()).isEqualTo(
  87. "<duplications><g><b s=\"1\" l=\"5\" t=\"false\" r=\"" + FILE_1_KEY + "\"/><b s=\"6\" l=\"5\" t=\"false\" r=\""
  88. + FILE_2_KEY + "\"/></g></duplications>");
  89. assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, DUPLICATIONS_DATA_KEY)).isNotPresent();
  90. }
  91. @Test
  92. public void compute_duplications_on_unchanged_file() {
  93. duplicationRepository.addExtendedProjectDuplication(FILE_1_REF, new TextBlock(1, 5), FILE_2_REF, new TextBlock(6, 10));
  94. underTest.execute(new TestComputationStepContext());
  95. assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATIONS_DATA_KEY)).isPresent();
  96. assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATIONS_DATA_KEY).get().getData()).isEqualTo(
  97. "<duplications><g><b s=\"1\" l=\"5\" t=\"false\" r=\"" + FILE_1_KEY + "\"/><b s=\"6\" l=\"5\" t=\"true\" r=\""
  98. + FILE_2_KEY + "\"/></g></duplications>");
  99. assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, DUPLICATIONS_DATA_KEY)).isNotPresent();
  100. }
  101. @Test
  102. public void compute_duplications_on_different_projects() {
  103. String fileKeyFromOtherProject = "PROJECT2_KEY:file2";
  104. duplicationRepository.addCrossProjectDuplication(FILE_1_REF, new TextBlock(1, 5), fileKeyFromOtherProject, new TextBlock(6, 10));
  105. underTest.execute(new TestComputationStepContext());
  106. assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATIONS_DATA_KEY)).isPresent();
  107. assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATIONS_DATA_KEY).get().getData()).isEqualTo(
  108. "<duplications><g><b s=\"1\" l=\"5\" t=\"false\" r=\"" + FILE_1_KEY + "\"/><b s=\"6\" l=\"5\" t=\"false\" r=\""
  109. + fileKeyFromOtherProject + "\"/></g></duplications>");
  110. assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, DUPLICATIONS_DATA_KEY)).isNotPresent();
  111. }
  112. }