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.

DefaultTempFolderTest.java 3.8KB

10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.api.impl.utils;
  21. import java.io.File;
  22. import org.apache.commons.io.FileUtils;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.junit.rules.ExpectedException;
  26. import org.junit.rules.TemporaryFolder;
  27. import org.sonar.api.utils.log.LogTester;
  28. import org.sonar.api.utils.log.LoggerLevel;
  29. import static org.assertj.core.api.Assertions.assertThat;
  30. public class DefaultTempFolderTest {
  31. @Rule
  32. public ExpectedException expectedException = ExpectedException.none();
  33. @Rule
  34. public TemporaryFolder temp = new TemporaryFolder();
  35. @Rule
  36. public LogTester logTester = new LogTester();
  37. @Test
  38. public void createTempFolderAndFile() throws Exception {
  39. File rootTempFolder = temp.newFolder();
  40. DefaultTempFolder underTest = new DefaultTempFolder(rootTempFolder);
  41. File dir = underTest.newDir();
  42. assertThat(dir).exists().isDirectory();
  43. File file = underTest.newFile();
  44. assertThat(file).exists().isFile();
  45. underTest.clean();
  46. assertThat(rootTempFolder).doesNotExist();
  47. }
  48. @Test
  49. public void createTempFolderWithName() throws Exception {
  50. File rootTempFolder = temp.newFolder();
  51. DefaultTempFolder underTest = new DefaultTempFolder(rootTempFolder);
  52. File dir = underTest.newDir("sample");
  53. assertThat(dir).exists().isDirectory();
  54. assertThat(new File(rootTempFolder, "sample")).isEqualTo(dir);
  55. underTest.clean();
  56. assertThat(rootTempFolder).doesNotExist();
  57. }
  58. @Test
  59. public void newDir_throws_ISE_if_name_is_not_valid() throws Exception {
  60. File rootTempFolder = temp.newFolder();
  61. DefaultTempFolder underTest = new DefaultTempFolder(rootTempFolder);
  62. String tooLong = "tooooolong";
  63. for (int i = 0; i < 50; i++) {
  64. tooLong += "tooooolong";
  65. }
  66. expectedException.expect(IllegalStateException.class);
  67. expectedException.expectMessage("Failed to create temp directory");
  68. underTest.newDir(tooLong);
  69. }
  70. @Test
  71. public void newFile_throws_ISE_if_name_is_not_valid() throws Exception {
  72. File rootTempFolder = temp.newFolder();
  73. DefaultTempFolder underTest = new DefaultTempFolder(rootTempFolder);
  74. String tooLong = "tooooolong";
  75. for (int i = 0; i < 50; i++) {
  76. tooLong += "tooooolong";
  77. }
  78. expectedException.expect(IllegalStateException.class);
  79. expectedException.expectMessage("Failed to create temp file");
  80. underTest.newFile(tooLong, ".txt");
  81. }
  82. @Test
  83. public void clean_deletes_non_empty_directory() throws Exception {
  84. File dir = temp.newFolder();
  85. FileUtils.touch(new File(dir, "foo.txt"));
  86. DefaultTempFolder underTest = new DefaultTempFolder(dir);
  87. underTest.clean();
  88. assertThat(dir).doesNotExist();
  89. }
  90. @Test
  91. public void clean_does_not_fail_if_directory_has_already_been_deleted() throws Exception {
  92. File dir = temp.newFolder();
  93. DefaultTempFolder underTest = new DefaultTempFolder(dir);
  94. underTest.clean();
  95. assertThat(dir).doesNotExist();
  96. // second call does not fail, nor log ERROR logs
  97. underTest.clean();
  98. assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty();
  99. }
  100. }