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.

ScannerTestCase.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * SonarSource :: IT :: SonarQube Scanner
  3. * Copyright (C) 2009-2017 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 com.sonar.runner.it;
  21. import com.sonar.orchestrator.Orchestrator;
  22. import com.sonar.orchestrator.build.SonarScanner;
  23. import com.sonar.orchestrator.version.Version;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.IOException;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Properties;
  30. import java.util.function.Function;
  31. import java.util.stream.Collectors;
  32. import javax.annotation.CheckForNull;
  33. import org.apache.commons.lang.StringUtils;
  34. import org.junit.ClassRule;
  35. import org.junit.Rule;
  36. import org.junit.rules.ExpectedException;
  37. import org.slf4j.Logger;
  38. import org.slf4j.LoggerFactory;
  39. import org.sonarqube.ws.WsComponents;
  40. import org.sonarqube.ws.WsComponents.Component;
  41. import org.sonarqube.ws.WsMeasures;
  42. import org.sonarqube.ws.WsMeasures.Measure;
  43. import org.sonarqube.ws.client.HttpConnector;
  44. import org.sonarqube.ws.client.WsClient;
  45. import org.sonarqube.ws.client.WsClientFactories;
  46. import org.sonarqube.ws.client.component.ShowWsRequest;
  47. import org.sonarqube.ws.client.measure.ComponentWsRequest;
  48. import static java.util.Arrays.asList;
  49. import static java.util.Collections.singletonList;
  50. public abstract class ScannerTestCase {
  51. private static final Logger LOG = LoggerFactory.getLogger(ScannerTestCase.class);
  52. @Rule
  53. public ExpectedException thrown = ExpectedException.none();
  54. @ClassRule
  55. public static Orchestrator orchestrator = SonarScannerTestSuite.ORCHESTRATOR;
  56. private static Version artifactVersion;
  57. private static Version artifactVersion() {
  58. if (artifactVersion == null) {
  59. String scannerVersion = System.getProperty("scanner.version");
  60. if (StringUtils.isNotBlank(scannerVersion)) {
  61. LOG.info("Use provided Scanner version: " + scannerVersion);
  62. artifactVersion = Version.create(scannerVersion);
  63. } else {
  64. try (FileInputStream fis = new FileInputStream(new File("../target/maven-archiver/pom.properties"))) {
  65. Properties props = new Properties();
  66. props.load(fis);
  67. artifactVersion = Version.create(props.getProperty("version"));
  68. return artifactVersion;
  69. } catch (IOException e) {
  70. throw new IllegalStateException(e);
  71. }
  72. }
  73. }
  74. return artifactVersion;
  75. }
  76. SonarScanner newScanner(File baseDir, String... keyValueProperties) {
  77. SonarScanner scannerCli = SonarScanner.create(baseDir, keyValueProperties);
  78. scannerCli.setScannerVersion(artifactVersion().toString());
  79. return scannerCli;
  80. }
  81. @CheckForNull
  82. static Map<String, Measure> getMeasures(String componentKey, String... metricKeys) {
  83. return newWsClient().measures().component(new ComponentWsRequest()
  84. .setComponentKey(componentKey)
  85. .setMetricKeys(asList(metricKeys)))
  86. .getComponent().getMeasuresList()
  87. .stream()
  88. .collect(Collectors.toMap(Measure::getMetric, Function.identity()));
  89. }
  90. @CheckForNull
  91. static Measure getMeasure(String componentKey, String metricKey) {
  92. WsMeasures.ComponentWsResponse response = newWsClient().measures().component(new ComponentWsRequest()
  93. .setComponentKey(componentKey)
  94. .setMetricKeys(singletonList(metricKey)));
  95. List<Measure> measures = response.getComponent().getMeasuresList();
  96. return measures.size() == 1 ? measures.get(0) : null;
  97. }
  98. @CheckForNull
  99. static Integer getMeasureAsInteger(String componentKey, String metricKey) {
  100. Measure measure = getMeasure(componentKey, metricKey);
  101. return (measure == null) ? null : Integer.parseInt(measure.getValue());
  102. }
  103. @CheckForNull
  104. static Double getMeasureAsDouble(String componentKey, String metricKey) {
  105. Measure measure = getMeasure(componentKey, metricKey);
  106. return (measure == null) ? null : Double.parseDouble(measure.getValue());
  107. }
  108. @CheckForNull
  109. static Component getComponent(String componentKey) {
  110. return newWsClient().components().show(new ShowWsRequest().setKey(componentKey)).getComponent();
  111. }
  112. static WsClient newWsClient() {
  113. return WsClientFactories.getDefault().newClient(HttpConnector.newBuilder()
  114. .url(orchestrator.getServer().getUrl())
  115. .build());
  116. }
  117. }