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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 org.junit.Assume;
  22. import org.junit.BeforeClass;
  23. import org.junit.rules.TemporaryFolder;
  24. import org.junit.Rule;
  25. import com.sonar.orchestrator.build.BuildFailureException;
  26. import com.sonar.orchestrator.locator.ResourceLocation;
  27. import com.sonar.orchestrator.build.BuildResult;
  28. import com.sonar.orchestrator.build.SonarRunner;
  29. import org.junit.Test;
  30. import java.io.File;
  31. import java.io.IOException;
  32. import static org.junit.Assert.fail;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. public class CacheTest extends RunnerTestCase {
  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.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml"));
  42. orchestrator.getServer().provisionProject("java:sample", "Java Sample, with comma");
  43. orchestrator.getServer().associateProjectToQualityProfile("java:sample", "java", "sonar-way");
  44. serverRunning = true;
  45. }
  46. private static void ensureStarted() {
  47. if (!serverRunning) {
  48. orchestrator.start();
  49. serverRunning = true;
  50. }
  51. }
  52. private static void ensureStopped() {
  53. if (serverRunning) {
  54. orchestrator.stop();
  55. serverRunning = false;
  56. }
  57. }
  58. @Test
  59. public void testIssuesMode() throws IOException {
  60. Assume.assumeTrue(orchestrator.getServer().version().isGreaterThanOrEquals("5.2"));
  61. // online, without cache -> should sync
  62. ensureStarted();
  63. SonarRunner build = createRunner("issues", true, "java-sample");
  64. BuildResult result = orchestrator.executeBuild(build, false);
  65. assertThat(result.isSuccess()).isTrue();
  66. // offline, with cache -> should run from cache
  67. ensureStopped();
  68. build = createRunner("issues", false, "java-sample");
  69. result = orchestrator.executeBuild(build, false);
  70. assertThat(result.isSuccess()).isTrue();
  71. // offline, without cache -> should fail
  72. build = createRunner("issues", true, "java-sample");
  73. try {
  74. result = orchestrator.executeBuild(build);
  75. fail("exception expected");
  76. } catch (BuildFailureException e) {
  77. // this message is specific to the server_first cache strategy
  78. assertThat(e.getResult().getLogs()).contains("can not be reached, trying cache");
  79. assertThat(e.getResult().getLogs()).contains("can not be reached and data is not cached");
  80. }
  81. }
  82. @Test
  83. public void testNonAssociatedMode() throws IOException {
  84. Assume.assumeTrue(orchestrator.getServer().version().isGreaterThanOrEquals("5.2"));
  85. // online, without cache -> should sync
  86. ensureStarted();
  87. SonarRunner build = createRunner("issues", true, "java-sample-non-associated");
  88. BuildResult result = orchestrator.executeBuild(build, false);
  89. assertThat(result.isSuccess()).isTrue();
  90. // offline, with cache -> should run from cache
  91. ensureStopped();
  92. build = createRunner("issues", false, "java-sample-non-associated");
  93. result = orchestrator.executeBuild(build, false);
  94. assertThat(result.isSuccess()).isTrue();
  95. }
  96. @Test
  97. public void testPublishModeOffline() throws IOException {
  98. Assume.assumeTrue(orchestrator.getServer().version().isGreaterThanOrEquals("5.2"));
  99. // online (cache not used)
  100. ensureStarted();
  101. SonarRunner build = createRunner("publish", "java-sample");
  102. BuildResult result = orchestrator.executeBuild(build, false);
  103. assertThat(result.isSuccess()).isTrue();
  104. // offline (cache not used) -> should fail
  105. ensureStopped();
  106. build = createRunner("publish", false, "java-sample");
  107. try {
  108. result = orchestrator.executeBuild(build);
  109. } catch (BuildFailureException e) {
  110. assertThat(e.getResult().getLogs()).contains("Fail to download libraries from server");
  111. }
  112. }
  113. private SonarRunner createRunner(String mode, String project) throws IOException {
  114. return createRunner(mode, false, project);
  115. }
  116. private SonarRunner createRunner(String mode, boolean refreshCache, String project) throws IOException {
  117. if (refreshCache || currentTemp == null) {
  118. currentTemp = temp.newFolder();
  119. }
  120. SonarRunner runner = newRunner(new File("projects/" + project))
  121. .setProperty("sonar.analysis.mode", mode)
  122. .setProperty("sonar.userHome", currentTemp.getAbsolutePath());
  123. return runner;
  124. }
  125. }