diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2013-10-17 17:51:12 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2013-10-17 17:51:43 +0200 |
commit | fad84b62f24cf7934413c3ffe241feb135e8c69a (patch) | |
tree | 4ba2f74aff17cdeec4c3a164bdac6c08559354c9 | |
parent | b5ef2c363df26358d782fab47d0e42f03c9d1c5b (diff) | |
download | sonarqube-fad84b62f24cf7934413c3ffe241feb135e8c69a.tar.gz sonarqube-fad84b62f24cf7934413c3ffe241feb135e8c69a.zip |
SONAR-4748 Refactoring of new TempFolder component
21 files changed, 120 insertions, 105 deletions
diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/InitialOpenIssuesStackTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/InitialOpenIssuesStackTest.java index 6a6d8c140a8..c6fbeaf5cb5 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/InitialOpenIssuesStackTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/InitialOpenIssuesStackTest.java @@ -27,9 +27,9 @@ import org.junit.ClassRule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonar.api.CoreProperties; -import org.sonar.batch.bootstrap.BatchTempUtils; import org.sonar.batch.bootstrap.BootstrapProperties; import org.sonar.batch.bootstrap.BootstrapSettings; +import org.sonar.batch.bootstrap.TempFolderProvider; import org.sonar.batch.index.Caches; import org.sonar.core.issue.db.IssueDto; @@ -50,7 +50,7 @@ public class InitialOpenIssuesStackTest { } catch (IOException e) { throw new RuntimeException(e); } - return new Caches(new BatchTempUtils(bootstrapSettings)); + return new Caches(new TempFolderProvider().provide(bootstrapSettings)); } InitialOpenIssuesStack stack; diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java index 762525fd992..e0e7be4ffcb 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java @@ -30,7 +30,7 @@ import org.sonar.api.Plugin; import org.sonar.api.config.Settings; import org.sonar.api.platform.PluginMetadata; import org.sonar.api.platform.PluginRepository; -import org.sonar.api.utils.TempUtils; +import org.sonar.api.utils.TempFolder; import org.sonar.core.plugins.PluginClassloaders; import org.sonar.core.plugins.PluginInstaller; import org.sonar.core.plugins.RemotePlugin; @@ -54,10 +54,10 @@ public class BatchPluginRepository implements PluginRepository { private Map<String, PluginMetadata> metadataByKey; private Settings settings; private PluginClassloaders classLoaders; - private TempUtils tempDirectories; + private TempFolder tempDirectories; private final AnalysisMode analysisMode; - public BatchPluginRepository(PluginDownloader pluginDownloader, TempUtils tempDirectories, Settings settings, AnalysisMode analysisMode) { + public BatchPluginRepository(PluginDownloader pluginDownloader, TempFolder tempDirectories, Settings settings, AnalysisMode analysisMode) { this.pluginDownloader = pluginDownloader; this.tempDirectories = tempDirectories; this.settings = settings; @@ -77,7 +77,7 @@ public class BatchPluginRepository implements PluginRepository { if (filter.accepts(remote.getKey())) { List<File> pluginFiles = pluginDownloader.downloadPlugin(remote); List<File> extensionFiles = pluginFiles.subList(1, pluginFiles.size()); - File targetDir = tempDirectories.createDirectory("plugins/" + remote.getKey()); + File targetDir = tempDirectories.newDir("plugins/" + remote.getKey()); LOG.debug("Installing plugin {} into {}", remote.getKey(), targetDir.getAbsolutePath()); PluginMetadata metadata = extractor.install(pluginFiles.get(0), remote.isCore(), extensionFiles, targetDir); if (StringUtils.isBlank(metadata.getBasePlugin()) || filter.accepts(metadata.getBasePlugin())) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java index 163d9a86ec3..abfd8de5fc2 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java @@ -87,7 +87,7 @@ public class BootstrapContainer extends ComponentContainer { Logback.class, ServerMetadata.class, org.sonar.batch.ServerMetadata.class, - BatchTempUtils.class, + new TempFolderProvider(), HttpDownloader.class, UriReader.class, new FileCacheProvider()); diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PreviewDatabase.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PreviewDatabase.java index 3ba51cd2f1a..09c70872f75 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PreviewDatabase.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PreviewDatabase.java @@ -30,7 +30,7 @@ import org.sonar.api.config.Settings; import org.sonar.api.database.DatabaseProperties; import org.sonar.api.utils.HttpDownloader.HttpException; import org.sonar.api.utils.SonarException; -import org.sonar.api.utils.TempUtils; +import org.sonar.api.utils.TempFolder; import java.io.File; import java.net.SocketTimeoutException; @@ -51,10 +51,10 @@ public class PreviewDatabase implements BatchComponent { private final Settings settings; private final ServerClient server; - private final TempUtils tempUtils; + private final TempFolder tempUtils; private final AnalysisMode mode; - public PreviewDatabase(Settings settings, ServerClient server, TempUtils tempUtils, AnalysisMode mode) { + public PreviewDatabase(Settings settings, ServerClient server, TempFolder tempUtils, AnalysisMode mode) { this.settings = settings; this.server = server; this.tempUtils = tempUtils; @@ -63,7 +63,7 @@ public class PreviewDatabase implements BatchComponent { public void start() { if (mode.isPreview()) { - File databaseFile = tempUtils.createTempFile("preview", ".h2.db"); + File databaseFile = tempUtils.newFile("preview", ".h2.db"); int readTimeoutSec = getReadTimeout(); downloadDatabase(databaseFile, readTimeoutSec * 1000); diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchTempUtils.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempFolderProvider.java index d31e9691f8e..fc773552216 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchTempUtils.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempFolderProvider.java @@ -20,15 +20,17 @@ package org.sonar.batch.bootstrap; import org.apache.commons.io.FileUtils; +import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.CoreProperties; -import org.sonar.core.utils.AbstractTempUtils; +import org.sonar.api.utils.TempFolder; +import org.sonar.api.utils.internal.DefaultTempFolder; import java.io.File; import java.io.IOException; -public class BatchTempUtils extends AbstractTempUtils { +public class TempFolderProvider extends ProviderAdapter { - public BatchTempUtils(BootstrapSettings bootstrapSettings) { + public TempFolder provide(BootstrapSettings bootstrapSettings) { String workingDirPath = bootstrapSettings.property(CoreProperties.WORKING_DIRECTORY, CoreProperties.WORKING_DIRECTORY_DEFAULT_VALUE); File workingDir = new File(workingDirPath); File tempDir = new File(workingDir, "tmp"); @@ -37,7 +39,7 @@ public class BatchTempUtils extends AbstractTempUtils { } catch (IOException e) { throw new IllegalStateException("Unable to create root temp directory " + tempDir, e); } - setTempDir(tempDir); + return new DefaultTempFolder(tempDir); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/Caches.java b/sonar-batch/src/main/java/org/sonar/batch/index/Caches.java index 7783e62380c..0a985e2ed5d 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/Caches.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/Caches.java @@ -30,7 +30,7 @@ import org.apache.commons.io.FileUtils; import org.picocontainer.Startable; import org.slf4j.LoggerFactory; import org.sonar.api.BatchComponent; -import org.sonar.api.utils.TempUtils; +import org.sonar.api.utils.TempFolder; import java.io.File; import java.io.Serializable; @@ -48,16 +48,16 @@ public class Caches implements BatchComponent, Startable { private File tempDir; private Persistit persistit; private Volume volume; - private final TempUtils tempUtils; + private final TempFolder tempFolder; - public Caches(TempUtils tempUtils) { - this.tempUtils = tempUtils; + public Caches(TempFolder tempFolder) { + this.tempFolder = tempFolder; initPersistit(); } private void initPersistit() { try { - tempDir = tempUtils.createTempDirectory("caches"); + tempDir = tempFolder.newDir("caches"); persistit = new Persistit(); persistit.setPersistitLogger(new Slf4jAdapter(LoggerFactory.getLogger("PERSISTIT"))); Properties props = new Properties(); diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializer.java index 8b3d726c6d4..9bd7dce15c9 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializer.java @@ -24,7 +24,7 @@ import org.apache.commons.io.FileUtils; import org.sonar.api.BatchComponent; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.scan.filesystem.PathResolver; -import org.sonar.api.utils.TempUtils; +import org.sonar.api.utils.TempFolder; import java.io.File; import java.util.List; @@ -41,7 +41,7 @@ public class ModuleFileSystemInitializer implements BatchComponent { private List<File> additionalSourceFiles; private List<File> additionalTestFiles; - public ModuleFileSystemInitializer(ProjectDefinition module, TempUtils tempUtils, PathResolver pathResolver) { + public ModuleFileSystemInitializer(ProjectDefinition module, TempFolder tempUtils, PathResolver pathResolver) { baseDir = module.getBaseDir(); buildDir = module.getBuildDir(); initWorkingDir(module, tempUtils); @@ -50,10 +50,10 @@ public class ModuleFileSystemInitializer implements BatchComponent { initTests(module, pathResolver); } - private void initWorkingDir(ProjectDefinition module, TempUtils tempUtils) { + private void initWorkingDir(ProjectDefinition module, TempFolder tempUtils) { workingDir = module.getWorkDir(); if (workingDir == null) { - workingDir = tempUtils.createTempDirectory("work"); + workingDir = tempUtils.newDir("work"); } else { try { FileUtils.forceMkdir(workingDir); diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java index 1b13dbbf591..952426ed91d 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java @@ -28,7 +28,7 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonar.api.CoreProperties; import org.sonar.api.config.Settings; -import org.sonar.api.utils.TempUtils; +import org.sonar.api.utils.TempFolder; import org.sonar.core.plugins.RemotePlugin; import org.sonar.core.plugins.RemotePluginFile; import org.sonar.test.TestUtils; @@ -64,9 +64,9 @@ public class BatchPluginRepositoryTest { @Test public void shouldLoadPlugin() throws IOException { - TempUtils tempDirs = mock(TempUtils.class); + TempFolder tempDirs = mock(TempFolder.class); File toDir = temp.newFolder(); - when(tempDirs.createDirectory("plugins/checkstyle")).thenReturn(toDir); + when(tempDirs.newDir("plugins/checkstyle")).thenReturn(toDir); RemotePlugin checkstyle = new RemotePlugin("checkstyle", true); PluginDownloader downloader = mock(PluginDownloader.class); @@ -84,11 +84,11 @@ public class BatchPluginRepositoryTest { @Test public void shouldLoadPluginExtension() throws IOException { - TempUtils tempDirs = mock(TempUtils.class); + TempFolder tempDirs = mock(TempFolder.class); File toDir1 = temp.newFolder(); File toDir2 = temp.newFolder(); - when(tempDirs.createDirectory("plugins/checkstyle")).thenReturn(toDir1); - when(tempDirs.createDirectory("plugins/checkstyleextensions")).thenReturn(toDir2); + when(tempDirs.newDir("plugins/checkstyle")).thenReturn(toDir1); + when(tempDirs.newDir("plugins/checkstyleextensions")).thenReturn(toDir2); RemotePlugin checkstyle = new RemotePlugin("checkstyle", true); RemotePlugin checkstyleExt = new RemotePlugin("checkstyleextensions", false); @@ -109,9 +109,9 @@ public class BatchPluginRepositoryTest { @Test public void shouldLoadPluginDeprecatedExtensions() throws IOException { - TempUtils tempDirs = mock(TempUtils.class); + TempFolder tempDirs = mock(TempFolder.class); File toDir = temp.newFolder(); - when(tempDirs.createDirectory("plugins/checkstyle")).thenReturn(toDir); + when(tempDirs.newDir("plugins/checkstyle")).thenReturn(toDir); RemotePlugin checkstyle = new RemotePlugin("checkstyle", true); checkstyle.getFiles().add(new RemotePluginFile("checkstyle-ext.xml", "fakemd5")); @@ -131,11 +131,11 @@ public class BatchPluginRepositoryTest { @Test public void shouldExcludePluginAndItsExtensions() throws IOException { - BatchTempUtils tempDirs = mock(BatchTempUtils.class); + TempFolder tempDirs = mock(TempFolder.class); File toDir1 = temp.newFolder(); File toDir2 = temp.newFolder(); - when(tempDirs.createDirectory("plugins/checkstyle")).thenReturn(toDir1); - when(tempDirs.createDirectory("plugins/checkstyleextensions")).thenReturn(toDir2); + when(tempDirs.newDir("plugins/checkstyle")).thenReturn(toDir1); + when(tempDirs.newDir("plugins/checkstyleextensions")).thenReturn(toDir2); RemotePlugin checkstyle = new RemotePlugin("checkstyle", true); RemotePlugin checkstyleExt = new RemotePlugin("checkstyleextensions", false); diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapContainerTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapContainerTest.java index 3e62bf619cd..d8944ab5340 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapContainerTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapContainerTest.java @@ -26,6 +26,7 @@ import org.sonar.api.BatchExtension; import org.sonar.api.Plugin; import org.sonar.api.SonarPlugin; import org.sonar.api.platform.PluginMetadata; +import org.sonar.api.utils.TempFolder; import org.sonar.core.config.Logback; import java.util.Arrays; @@ -46,7 +47,7 @@ public class BootstrapContainerTest { container.doBeforeStart(); assertThat(container.getComponentByType(Logback.class)).isNotNull(); - assertThat(container.getComponentByType(BatchTempUtils.class)).isNotNull(); + assertThat(container.getComponentByType(TempFolder.class)).isNotNull(); } @Test diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/PreviewDatabaseTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/PreviewDatabaseTest.java index d0a0ff569f9..a9209088726 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/PreviewDatabaseTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/PreviewDatabaseTest.java @@ -30,7 +30,7 @@ import org.sonar.api.config.Settings; import org.sonar.api.database.DatabaseProperties; import org.sonar.api.utils.HttpDownloader; import org.sonar.api.utils.SonarException; -import org.sonar.api.utils.TempUtils; +import org.sonar.api.utils.TempFolder; import java.io.File; import java.net.SocketTimeoutException; @@ -45,7 +45,7 @@ import static org.mockito.Mockito.when; public class PreviewDatabaseTest { Settings settings; ServerClient server = mock(ServerClient.class); - TempUtils tempUtils = mock(TempUtils.class); + TempFolder tempUtils = mock(TempFolder.class); File databaseFile; private AnalysisMode mode; @@ -58,7 +58,7 @@ public class PreviewDatabaseTest { @Before public void setUp() throws Exception { databaseFile = temp.newFile("preview.h2.db"); - when(tempUtils.createTempFile("preview", ".h2.db")).thenReturn(databaseFile); + when(tempUtils.newFile("preview", ".h2.db")).thenReturn(databaseFile); settings = new Settings(); settings.setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "group:project"); diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchTempUtilsTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TempFolderProviderTest.java index 04005adea68..8596dd33afd 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchTempUtilsTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TempFolderProviderTest.java @@ -25,12 +25,14 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.sonar.api.CoreProperties; +import org.sonar.api.utils.TempFolder; +import org.sonar.api.utils.internal.DefaultTempFolder; import java.io.File; import static org.fest.assertions.Assertions.assertThat; -public class BatchTempUtilsTest { +public class TempFolderProviderTest { @Rule public ExpectedException throwable = ExpectedException.none(); @@ -41,14 +43,14 @@ public class BatchTempUtilsTest { @Test public void createTempFolder() throws Exception { File workingDir = temp.newFolder(); - BatchTempUtils tempUtils = new BatchTempUtils(new BootstrapSettings( + TempFolder tempFolder = new TempFolderProvider().provide(new BootstrapSettings( new BootstrapProperties(ImmutableMap.of(CoreProperties.WORKING_DIRECTORY, workingDir.getAbsolutePath())))); - tempUtils.createTempDirectory(); - tempUtils.createTempFile(); + tempFolder.newDir(); + tempFolder.newFile(); assertThat(new File(workingDir, "tmp")).exists(); assertThat(new File(workingDir, "tmp").list()).hasSize(2); - tempUtils.stop(); + ((DefaultTempFolder) tempFolder).stop(); assertThat(new File(workingDir, "tmp")).doesNotExist(); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/CachesTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/CachesTest.java index eafec808dc8..c8d5fc7dc0e 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/CachesTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/CachesTest.java @@ -26,9 +26,9 @@ import org.junit.ClassRule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonar.api.CoreProperties; -import org.sonar.batch.bootstrap.BatchTempUtils; import org.sonar.batch.bootstrap.BootstrapProperties; import org.sonar.batch.bootstrap.BootstrapSettings; +import org.sonar.batch.bootstrap.TempFolderProvider; import java.io.File; import java.io.IOException; @@ -49,7 +49,7 @@ public class CachesTest { } catch (IOException e) { throw new RuntimeException(e); } - return new Caches(new BatchTempUtils(bootstrapSettings)); + return new Caches(new TempFolderProvider().provide(bootstrapSettings)); } Caches caches; diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializerTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializerTest.java index 3662390946a..0c75ab3d89b 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializerTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializerTest.java @@ -26,7 +26,7 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.scan.filesystem.PathResolver; -import org.sonar.api.utils.TempUtils; +import org.sonar.api.utils.TempFolder; import java.io.File; import java.io.IOException; @@ -47,7 +47,7 @@ public class ModuleFileSystemInitializerTest { File workDir = temp.newFolder("work"); ProjectDefinition module = ProjectDefinition.create().setBaseDir(baseDir).setWorkDir(workDir); - ModuleFileSystemInitializer initializer = new ModuleFileSystemInitializer(module, mock(TempUtils.class), pathResolver); + ModuleFileSystemInitializer initializer = new ModuleFileSystemInitializer(module, mock(TempFolder.class), pathResolver); assertThat(initializer.baseDir().getCanonicalPath()).isEqualTo(baseDir.getCanonicalPath()); assertThat(initializer.workingDir().getCanonicalPath()).isEqualTo(workDir.getCanonicalPath()); @@ -73,7 +73,7 @@ public class ModuleFileSystemInitializerTest { .addTestDirs("src/test/java", "src/test/unknown") .addBinaryDir("target/classes"); - ModuleFileSystemInitializer initializer = new ModuleFileSystemInitializer(project, mock(TempUtils.class), pathResolver); + ModuleFileSystemInitializer initializer = new ModuleFileSystemInitializer(project, mock(TempFolder.class), pathResolver); assertThat(initializer.baseDir().getCanonicalPath()).isEqualTo(baseDir.getCanonicalPath()); assertThat(initializer.buildDir().getCanonicalPath()).isEqualTo(buildDir.getCanonicalPath()); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/TempFileUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/TempFileUtils.java index fb940b5af04..8bad04165a4 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/TempFileUtils.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/TempFileUtils.java @@ -23,7 +23,7 @@ import java.io.File; import java.io.IOException; /** - * @deprecated since 4.0 use {@link TempUtils} + * @deprecated since 4.0 use {@link TempFolder} */ @Deprecated public final class TempFileUtils { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/TempUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/TempFolder.java index 7557d502676..3bcc72239c3 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/TempUtils.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/TempFolder.java @@ -36,25 +36,20 @@ import java.io.File; * @since 4.0 * */ -public interface TempUtils extends TaskComponent, ServerComponent { +public interface TempFolder extends TaskComponent, ServerComponent { /** * Create a directory in temp folder with a random unique name. */ - File createTempDirectory(); - - /** - * Create a directory in temp folder with a random unique name and using the provided prefix when possible. - */ - File createTempDirectory(@Nullable String prefix); + File newDir(); /** * Create a directory in temp folder using provided name. */ - File createDirectory(String name); + File newDir(String name); - File createTempFile(); + File newFile(); - File createTempFile(@Nullable String prefix, @Nullable String suffix); + File newFile(@Nullable String prefix, @Nullable String suffix); } diff --git a/sonar-core/src/main/java/org/sonar/core/utils/AbstractTempUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java index 20155a31d95..3a33f349575 100644 --- a/sonar-core/src/main/java/org/sonar/core/utils/AbstractTempUtils.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java @@ -17,36 +17,32 @@ * 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.core.utils; +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.TempUtils; +import org.sonar.api.utils.TempFolder; import javax.annotation.Nullable; import java.io.File; import java.io.IOException; -public abstract class AbstractTempUtils implements TempUtils, Startable { +public class DefaultTempFolder implements TempFolder, Startable { /** Maximum loop count when creating temp directories. */ private static final int TEMP_DIR_ATTEMPTS = 10000; - private File tempDir; + private final File tempDir; - protected void setTempDir(File tempDir) { + public DefaultTempFolder(File tempDir) { this.tempDir = tempDir; } @Override - public File createTempDirectory() { - return createTempDirectory(null); - } - - @Override - public File createTempDirectory(@Nullable String prefix) { - return createTempDir(tempDir, prefix); + public File newDir() { + return createTempDir(tempDir, ""); } /** @@ -67,7 +63,7 @@ public abstract class AbstractTempUtils implements TempUtils, Startable { } @Override - public File createDirectory(String name) { + public File newDir(String name) { File dir = new File(tempDir, name); try { FileUtils.forceMkdir(dir); @@ -78,12 +74,12 @@ public abstract class AbstractTempUtils implements TempUtils, Startable { } @Override - public File createTempFile() { - return createTempFile(null, null); + public File newFile() { + return newFile(null, null); } @Override - public File createTempFile(@Nullable String prefix, @Nullable String suffix) { + public File newFile(@Nullable String prefix, @Nullable String suffix) { return createTempFile(tempDir, prefix, suffix); } @@ -91,7 +87,7 @@ public abstract class AbstractTempUtils implements TempUtils, Startable { * Inspired by guava waiting for JDK 7 Files#createTempFile */ private static File createTempFile(File baseDir, String prefix, String suffix) { - String baseName = prefix + System.currentTimeMillis() + "-"; + String baseName = StringUtils.defaultIfEmpty(prefix, "") + System.currentTimeMillis() + "-"; try { for (int counter = 0; counter < TEMP_DIR_ATTEMPTS; counter++) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/package-info.java new file mode 100644 index 00000000000..9d2dda0410b --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/package-info.java @@ -0,0 +1,24 @@ +/* + * 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. + */ +@ParametersAreNonnullByDefault +package org.sonar.api.utils.internal; + +import javax.annotation.ParametersAreNonnullByDefault; + diff --git a/sonar-core/src/test/java/org/sonar/core/utils/AbstractTempUtilsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/DefaultTempFolderTest.java index a5d030fda2a..93a795474ab 100644 --- a/sonar-core/src/test/java/org/sonar/core/utils/AbstractTempUtilsTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/DefaultTempFolderTest.java @@ -17,7 +17,7 @@ * 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.core.utils; +package org.sonar.api.utils.internal; import org.junit.Rule; import org.junit.Test; @@ -28,7 +28,7 @@ import java.io.File; import static org.fest.assertions.Assertions.assertThat; -public class AbstractTempUtilsTest { +public class DefaultTempFolderTest { @Rule public ExpectedException throwable = ExpectedException.none(); @@ -39,10 +39,10 @@ public class AbstractTempUtilsTest { @Test public void createTempFolderAndFile() throws Exception { File tempFolder = temp.newFolder(); - AbstractTempUtils tempUtils = new CustomTempUtils(tempFolder); - File dir = tempUtils.createTempDirectory(); + DefaultTempFolder tempUtils = new DefaultTempFolder(tempFolder); + File dir = tempUtils.newDir(); assertThat(dir).exists().isDirectory(); - File file = tempUtils.createTempFile(); + File file = tempUtils.newFile(); assertThat(file).exists().isFile(); tempUtils.stop(); @@ -52,20 +52,12 @@ public class AbstractTempUtilsTest { @Test public void createTempFolderWithName() throws Exception { File tempFolder = temp.newFolder(); - AbstractTempUtils tempUtils = new CustomTempUtils(tempFolder); - File dir = tempUtils.createDirectory("sample"); + DefaultTempFolder tempUtils = new DefaultTempFolder(tempFolder); + File dir = tempUtils.newDir("sample"); assertThat(dir).exists().isDirectory(); assertThat(new File(tempFolder, "sample")).isEqualTo(dir); tempUtils.stop(); assertThat(tempFolder).doesNotExist(); } - - private static class CustomTempUtils extends AbstractTempUtils { - - public CustomTempUtils(File tempDir) { - setTempDir(tempDir); - } - - } } diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index b30b64b45bc..72c62fbc304 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java @@ -148,7 +148,6 @@ import org.sonar.server.ui.SecurityRealmFactory; import org.sonar.server.ui.Views; import org.sonar.server.user.DefaultUserService; import org.sonar.server.user.NewUserNotifier; -import org.sonar.server.util.ServerTempUtils; import javax.servlet.ServletContext; @@ -240,7 +239,7 @@ public final class Platform { rootContainer.addSingleton(PreviewDatabaseFactory.class); rootContainer.addSingleton(SemaphoreUpdater.class); rootContainer.addSingleton(SemaphoresImpl.class); - rootContainer.addSingleton(ServerTempUtils.class); + rootContainer.addSingleton(new TempFolderProvider()); rootContainer.startComponents(); } diff --git a/sonar-server/src/main/java/org/sonar/server/util/ServerTempUtils.java b/sonar-server/src/main/java/org/sonar/server/platform/TempFolderProvider.java index bc43ca71481..a7bafe61a63 100644 --- a/sonar-server/src/main/java/org/sonar/server/util/ServerTempUtils.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/TempFolderProvider.java @@ -17,25 +17,27 @@ * 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.server.util; +package org.sonar.server.platform; import org.apache.commons.io.FileUtils; +import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.platform.ServerFileSystem; -import org.sonar.core.utils.AbstractTempUtils; +import org.sonar.api.utils.TempFolder; +import org.sonar.api.utils.internal.DefaultTempFolder; import java.io.File; import java.io.IOException; -public class ServerTempUtils extends AbstractTempUtils { +public class TempFolderProvider extends ProviderAdapter { - public ServerTempUtils(ServerFileSystem fs) { + public TempFolder provide(ServerFileSystem fs) { File tempDir = new File(fs.getTempDir(), "tmp"); try { FileUtils.forceMkdir(tempDir); } catch (IOException e) { throw new IllegalStateException("Unable to create root temp directory " + tempDir, e); } - setTempDir(tempDir); + return new DefaultTempFolder(tempDir); } } diff --git a/sonar-server/src/test/java/org/sonar/server/util/ServerTempUtilsTest.java b/sonar-server/src/test/java/org/sonar/server/platform/TempFolderProviderTest.java index 7d057de0f7d..0d157d591ee 100644 --- a/sonar-server/src/test/java/org/sonar/server/util/ServerTempUtilsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/platform/TempFolderProviderTest.java @@ -17,13 +17,15 @@ * 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.server.util; +package org.sonar.server.platform; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.sonar.api.platform.ServerFileSystem; +import org.sonar.api.utils.TempFolder; +import org.sonar.api.utils.internal.DefaultTempFolder; import java.io.File; @@ -31,7 +33,7 @@ import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public class ServerTempUtilsTest { +public class TempFolderProviderTest { @Rule public ExpectedException throwable = ExpectedException.none(); @@ -44,13 +46,13 @@ public class ServerTempUtilsTest { ServerFileSystem fs = mock(ServerFileSystem.class); File serverTempFolder = temp.newFolder(); when(fs.getTempDir()).thenReturn(serverTempFolder); - ServerTempUtils tempUtils = new ServerTempUtils(fs); - tempUtils.createTempDirectory(); - tempUtils.createTempFile(); + TempFolder tempUtils = new TempFolderProvider().provide(fs); + tempUtils.newDir(); + tempUtils.newFile(); assertThat(new File(serverTempFolder, "tmp")).exists(); assertThat(new File(serverTempFolder, "tmp").list()).hasSize(2); - tempUtils.stop(); + ((DefaultTempFolder) tempUtils).stop(); assertThat(new File(serverTempFolder, "tmp")).doesNotExist(); } } |