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.

CacheTest.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * SonarSource :: IT :: SonarQube Scanner
  3. * Copyright (C) 2009 SonarSource
  4. * sonarqube@googlegroups.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
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package com.sonar.runner.it;
  21. import com.sonar.orchestrator.build.BuildFailureException;
  22. import com.sonar.orchestrator.build.BuildResult;
  23. import com.sonar.orchestrator.build.SonarRunner;
  24. import com.sonar.orchestrator.locator.ResourceLocation;
  25. import java.io.File;
  26. import java.io.IOException;
  27. import org.junit.AfterClass;
  28. import org.junit.Assume;
  29. import org.junit.BeforeClass;
  30. import org.junit.Rule;
  31. import org.junit.Test;
  32. import org.junit.rules.TemporaryFolder;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. public class CacheTest extends ScannerTestCase {
  35. @Rule
  36. public TemporaryFolder temp = new TemporaryFolder();
  37. private File currentTemp = null;
  38. private static boolean serverRunning = false;
  39. @BeforeClass
  40. public static void setUpClass() {
  41. orchestrator.resetData();
  42. orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml"));
  43. orchestrator.getServer().provisionProject("java:sample", "Java Sample, with comma");
  44. orchestrator.getServer().associateProjectToQualityProfile("java:sample", "java", "sonar-way");
  45. serverRunning = true;
  46. }
  47. @AfterClass
  48. public static void restartForOtherTests() {
  49. ensureStarted();
  50. }
  51. private static void ensureStarted() {
  52. if (!serverRunning) {
  53. orchestrator.start();
  54. serverRunning = true;
  55. }
  56. }
  57. private static void ensureStopped() {
  58. if (serverRunning) {
  59. orchestrator.stop();
  60. serverRunning = false;
  61. }
  62. }
  63. @Test
  64. public void testIssuesMode() throws IOException {
  65. Assume.assumeTrue(orchestrator.getServer().version().isGreaterThanOrEquals("5.3"));
  66. // online, cache empty -> should sync
  67. ensureStarted();
  68. SonarRunner build = createRunner("issues", true, "java-sample");
  69. BuildResult result = orchestrator.executeBuild(build, false);
  70. assertThat(result.isSuccess()).isTrue();
  71. // offline, don't use cache by default -> should fail
  72. ensureStopped();
  73. build = createRunner("issues", false, "java-sample");
  74. result = orchestrator.executeBuildQuietly(build, false);
  75. assertThat(result.isSuccess()).isFalse();
  76. // offline, don't use cache -> should run from cache
  77. build = createRunner("issues", false, "java-sample");
  78. build.setProperty("sonar.useWsCache", "true");
  79. result = orchestrator.executeBuild(build, false);
  80. assertThat(result.isSuccess()).isTrue();
  81. // offline, cache empty -> should fail
  82. build = createRunner("issues", true, "java-sample");
  83. build.setProperty("sonar.useWsCache", "true");
  84. result = orchestrator.executeBuildQuietly(build, false);
  85. assertThat(result.isSuccess()).isFalse();
  86. // this message is specific to the server_first cache strategy
  87. assertThat(result.getLogs()).contains("can not be reached, trying cache");
  88. assertThat(result.getLogs()).contains("can not be reached and data is not cached");
  89. }
  90. @Test
  91. public void testNonAssociatedMode() throws IOException {
  92. Assume.assumeTrue(orchestrator.getServer().version().isGreaterThanOrEquals("5.2"));
  93. // online, without cache -> should sync
  94. ensureStarted();
  95. SonarRunner build = createRunner("issues", true, "java-sample-non-associated");
  96. BuildResult result = orchestrator.executeBuild(build, false);
  97. assertThat(result.isSuccess()).isTrue();
  98. // offline, with cache -> should run from cache
  99. ensureStopped();
  100. build = createRunner("issues", false, "java-sample-non-associated");
  101. build.setProperty("sonar.useWsCache", "true");
  102. result = orchestrator.executeBuild(build, false);
  103. assertThat(result.isSuccess()).isTrue();
  104. }
  105. @Test
  106. public void testPublishModeOffline() throws IOException {
  107. Assume.assumeTrue(orchestrator.getServer().version().isGreaterThanOrEquals("5.2"));
  108. // online (cache not used)
  109. ensureStarted();
  110. SonarRunner build = createRunner("publish", "java-sample");
  111. BuildResult result = orchestrator.executeBuild(build, false);
  112. assertThat(result.isSuccess()).isTrue();
  113. // offline (cache not used) -> should fail
  114. ensureStopped();
  115. build = createRunner("publish", false, "java-sample");
  116. try {
  117. result = orchestrator.executeBuild(build);
  118. } catch (BuildFailureException e) {
  119. assertThat(e.getResult().getLogs()).contains("Fail to download libraries from server");
  120. }
  121. }
  122. private SonarRunner createRunner(String mode, String project) throws IOException {
  123. return createRunner(mode, false, project);
  124. }
  125. private SonarRunner createRunner(String mode, boolean refreshCache, String project) throws IOException {
  126. if (refreshCache || currentTemp == null) {
  127. currentTemp = temp.newFolder();
  128. }
  129. SonarRunner runner = newScanner(new File("projects/" + project))
  130. .setProperty("sonar.analysis.mode", mode)
  131. .setProperty("sonar.userHome", currentTemp.getAbsolutePath());
  132. return runner;
  133. }
  134. }