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.

ProjectLockTest.java 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.scanner.scan;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.nio.file.Files;
  24. import java.nio.file.Path;
  25. import org.junit.Before;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.junit.rules.ExpectedException;
  29. import org.junit.rules.TemporaryFolder;
  30. import org.sonar.api.batch.bootstrap.ProjectDefinition;
  31. import org.sonar.api.batch.fs.internal.DefaultInputProject;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. public class ProjectLockTest {
  34. @Rule
  35. public TemporaryFolder tempFolder = new TemporaryFolder();
  36. @Rule
  37. public ExpectedException exception = ExpectedException.none();
  38. private ProjectLock lock;
  39. private File baseDir;
  40. private File worDir;
  41. @Before
  42. public void setUp() throws IOException {
  43. baseDir = tempFolder.newFolder();
  44. worDir = new File(baseDir, ".sonar");
  45. lock = new ProjectLock(new DefaultInputProject(ProjectDefinition.create().setBaseDir(baseDir).setWorkDir(worDir)));
  46. }
  47. @Test
  48. public void tryLock() {
  49. Path lockFilePath = worDir.toPath().resolve(DirectoryLock.LOCK_FILE_NAME);
  50. lock.tryLock();
  51. assertThat(Files.exists(lockFilePath)).isTrue();
  52. assertThat(Files.isRegularFile(lockFilePath)).isTrue();
  53. lock.stop();
  54. assertThat(Files.exists(lockFilePath)).isTrue();
  55. }
  56. @Test
  57. public void tryLockConcurrently() {
  58. exception.expect(IllegalStateException.class);
  59. exception.expectMessage("Another SonarQube analysis is already in progress for this project");
  60. lock.tryLock();
  61. lock.tryLock();
  62. }
  63. @Test
  64. /**
  65. * If there is an error starting up the scan, we'll still try to unlock even if the lock
  66. * was never done
  67. */
  68. public void stopWithoutStarting() {
  69. lock.stop();
  70. lock.stop();
  71. }
  72. @Test
  73. public void tryLockTwice() {
  74. lock.tryLock();
  75. lock.stop();
  76. lock.tryLock();
  77. lock.stop();
  78. }
  79. @Test
  80. public void unLockWithNoLock() {
  81. lock.stop();
  82. }
  83. }