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.

ReportLanguageDistributionMeasuresStepTest.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.Before;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.sonar.ce.task.projectanalysis.component.FileAttributes;
  25. import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
  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.NCLOC;
  32. import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
  33. import static org.sonar.api.measures.CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION;
  34. import static org.sonar.api.measures.CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION_KEY;
  35. import static org.sonar.ce.task.projectanalysis.component.Component.Type.DIRECTORY;
  36. import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
  37. import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
  38. import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
  39. import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
  40. public class ReportLanguageDistributionMeasuresStepTest {
  41. private static final String XOO_LANGUAGE = "xoo";
  42. private static final String JAVA_LANGUAGE = "java";
  43. private static final int ROOT_REF = 1;
  44. private static final int DIRECTORY_REF = 1234;
  45. private static final int FILE_1_REF = 12341;
  46. private static final int FILE_2_REF = 12342;
  47. private static final int FILE_3_REF = 12343;
  48. private static final int FILE_4_REF = 12344;
  49. @Rule
  50. public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
  51. @Rule
  52. public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
  53. .add(NCLOC)
  54. .add(NCLOC_LANGUAGE_DISTRIBUTION);
  55. @Rule
  56. public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
  57. ComputationStep underTest = new LanguageDistributionMeasuresStep(treeRootHolder, metricRepository, measureRepository);
  58. @Before
  59. public void setUp() {
  60. treeRootHolder.setRoot(
  61. builder(PROJECT, ROOT_REF)
  62. .addChildren(
  63. builder(DIRECTORY, DIRECTORY_REF)
  64. .addChildren(
  65. builder(FILE, FILE_1_REF).setFileAttributes(new FileAttributes(false, XOO_LANGUAGE, 1)).build(),
  66. builder(FILE, FILE_2_REF).setFileAttributes(new FileAttributes(false, XOO_LANGUAGE, 1)).build(),
  67. builder(FILE, FILE_3_REF).setFileAttributes(new FileAttributes(false, JAVA_LANGUAGE, 1)).build(),
  68. builder(FILE, FILE_4_REF).setFileAttributes(new FileAttributes(false, null, 1)).build())
  69. .build())
  70. .build());
  71. }
  72. @Test
  73. public void compute_ncloc_language_distribution() {
  74. measureRepository.addRawMeasure(FILE_1_REF, NCLOC_KEY, newMeasureBuilder().create(10));
  75. measureRepository.addRawMeasure(FILE_2_REF, NCLOC_KEY, newMeasureBuilder().create(8));
  76. measureRepository.addRawMeasure(FILE_3_REF, NCLOC_KEY, newMeasureBuilder().create(6));
  77. measureRepository.addRawMeasure(FILE_4_REF, NCLOC_KEY, newMeasureBuilder().create(2));
  78. underTest.execute(new TestComputationStepContext());
  79. assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, NCLOC_LANGUAGE_DISTRIBUTION_KEY).get().getStringValue()).isEqualTo("xoo=10");
  80. assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, NCLOC_LANGUAGE_DISTRIBUTION_KEY).get().getStringValue()).isEqualTo("xoo=8");
  81. assertThat(measureRepository.getAddedRawMeasure(FILE_3_REF, NCLOC_LANGUAGE_DISTRIBUTION_KEY).get().getStringValue()).isEqualTo("java=6");
  82. assertThat(measureRepository.getAddedRawMeasure(FILE_4_REF, NCLOC_LANGUAGE_DISTRIBUTION_KEY).get().getStringValue()).isEqualTo("<null>=2");
  83. assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, NCLOC_LANGUAGE_DISTRIBUTION_KEY).get().getStringValue()).isEqualTo("<null>=2;java=6;xoo=18");
  84. assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, NCLOC_LANGUAGE_DISTRIBUTION_KEY).get().getStringValue()).isEqualTo("<null>=2;java=6;xoo=18");
  85. }
  86. @Test
  87. public void do_not_compute_ncloc_language_distribution_when_no_ncloc() {
  88. underTest.execute(new TestComputationStepContext());
  89. assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, NCLOC_LANGUAGE_DISTRIBUTION_KEY)).isNotPresent();
  90. assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, NCLOC_LANGUAGE_DISTRIBUTION_KEY)).isNotPresent();
  91. assertThat(measureRepository.getAddedRawMeasure(FILE_3_REF, NCLOC_LANGUAGE_DISTRIBUTION_KEY)).isNotPresent();
  92. assertThat(measureRepository.getAddedRawMeasure(FILE_4_REF, NCLOC_LANGUAGE_DISTRIBUTION_KEY)).isNotPresent();
  93. assertThat(measureRepository.getAddedRawMeasure(DIRECTORY_REF, NCLOC_LANGUAGE_DISTRIBUTION_KEY)).isNotPresent();
  94. assertThat(measureRepository.getAddedRawMeasure(ROOT_REF, NCLOC_LANGUAGE_DISTRIBUTION_KEY)).isNotPresent();
  95. }
  96. }