diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2013-10-21 11:17:16 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2013-10-21 11:17:55 +0200 |
commit | 8842c50c19054ced2809a8ebdde854a83312a8d2 (patch) | |
tree | 6c40289430c8dc4ec8cf82a1c5369abdb7b92a41 /sonar-plugin-api | |
parent | eb321c1da9949532872a6a7c64be6d51e11affe4 (diff) | |
download | sonarqube-8842c50c19054ced2809a8ebdde854a83312a8d2.tar.gz sonarqube-8842c50c19054ced2809a8ebdde854a83312a8d2.zip |
SONAR-4748 Fix issues and regression for temp folders
Diffstat (limited to 'sonar-plugin-api')
3 files changed, 58 insertions, 21 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java index 3a33f349575..a1862ede487 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java @@ -21,7 +21,6 @@ package org.sonar.api.utils.internal; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; -import org.picocontainer.Startable; import org.sonar.api.utils.TempFolder; import javax.annotation.Nullable; @@ -29,7 +28,7 @@ import javax.annotation.Nullable; import java.io.File; import java.io.IOException; -public class DefaultTempFolder implements TempFolder, Startable { +public class DefaultTempFolder implements TempFolder { /** Maximum loop count when creating temp directories. */ private static final int TEMP_DIR_ATTEMPTS = 10000; @@ -104,13 +103,7 @@ public class DefaultTempFolder implements TempFolder, Startable { + baseName + "0" + suffix + " to " + baseName + (TEMP_DIR_ATTEMPTS - 1) + suffix + ")"); } - @Override - public void start() { - // Nothing to do - } - - @Override - public void stop() { + public void clean() { FileUtils.deleteQuietly(tempDir); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/TempFolderCleaner.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/TempFolderCleaner.java new file mode 100644 index 00000000000..be28c981798 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/TempFolderCleaner.java @@ -0,0 +1,44 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.utils.internal; + +import org.picocontainer.Startable; +import org.sonar.api.ServerComponent; +import org.sonar.api.task.TaskComponent; +import org.sonar.api.utils.TempFolder; + +public class TempFolderCleaner implements TaskComponent, ServerComponent, Startable { + + private TempFolder defaultTempFolder; + + public TempFolderCleaner(TempFolder defaultTempFolder) { + this.defaultTempFolder = defaultTempFolder; + } + + @Override + public void start() { + // Nothing to do + } + + @Override + public void stop() { + ((DefaultTempFolder) defaultTempFolder).clean(); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/DefaultTempFolderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/DefaultTempFolderTest.java index 93a795474ab..07f6d7545c6 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/DefaultTempFolderTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/DefaultTempFolderTest.java @@ -38,26 +38,26 @@ public class DefaultTempFolderTest { @Test public void createTempFolderAndFile() throws Exception { - File tempFolder = temp.newFolder(); - DefaultTempFolder tempUtils = new DefaultTempFolder(tempFolder); - File dir = tempUtils.newDir(); + File rootTempFolder = temp.newFolder(); + DefaultTempFolder tempFolder = new DefaultTempFolder(rootTempFolder); + File dir = tempFolder.newDir(); assertThat(dir).exists().isDirectory(); - File file = tempUtils.newFile(); + File file = tempFolder.newFile(); assertThat(file).exists().isFile(); - tempUtils.stop(); - assertThat(tempFolder).doesNotExist(); + new TempFolderCleaner(tempFolder).stop(); + assertThat(rootTempFolder).doesNotExist(); } @Test public void createTempFolderWithName() throws Exception { - File tempFolder = temp.newFolder(); - DefaultTempFolder tempUtils = new DefaultTempFolder(tempFolder); - File dir = tempUtils.newDir("sample"); + File rootTempFolder = temp.newFolder(); + DefaultTempFolder tempFolder = new DefaultTempFolder(rootTempFolder); + File dir = tempFolder.newDir("sample"); assertThat(dir).exists().isDirectory(); - assertThat(new File(tempFolder, "sample")).isEqualTo(dir); + assertThat(new File(rootTempFolder, "sample")).isEqualTo(dir); - tempUtils.stop(); - assertThat(tempFolder).doesNotExist(); + new TempFolderCleaner(tempFolder).stop(); + assertThat(rootTempFolder).doesNotExist(); } } |