From 18a4c15b060d1b56f08826bf6006d08eaf2625ec Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Tue, 15 Oct 2013 17:14:44 +0200 Subject: SONAR-4748 Introduce a new extension for managing temp files/folders. --- .../java/org/sonar/server/platform/Platform.java | 2 + .../org/sonar/server/util/ServerTempUtils.java | 41 ++++++++++++++++ .../org/sonar/server/util/ServerTempUtilsTest.java | 56 ++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 sonar-server/src/main/java/org/sonar/server/util/ServerTempUtils.java create mode 100644 sonar-server/src/test/java/org/sonar/server/util/ServerTempUtilsTest.java (limited to 'sonar-server') 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 6c5f06364c7..b30b64b45bc 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,6 +148,7 @@ 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; @@ -239,6 +240,7 @@ public final class Platform { rootContainer.addSingleton(PreviewDatabaseFactory.class); rootContainer.addSingleton(SemaphoreUpdater.class); rootContainer.addSingleton(SemaphoresImpl.class); + rootContainer.addSingleton(ServerTempUtils.class); 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/util/ServerTempUtils.java new file mode 100644 index 00000000000..bc43ca71481 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/util/ServerTempUtils.java @@ -0,0 +1,41 @@ +/* + * 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.server.util; + +import org.apache.commons.io.FileUtils; +import org.sonar.api.platform.ServerFileSystem; +import org.sonar.core.utils.AbstractTempUtils; + +import java.io.File; +import java.io.IOException; + +public class ServerTempUtils extends AbstractTempUtils { + + public ServerTempUtils(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); + } + +} diff --git a/sonar-server/src/test/java/org/sonar/server/util/ServerTempUtilsTest.java b/sonar-server/src/test/java/org/sonar/server/util/ServerTempUtilsTest.java new file mode 100644 index 00000000000..7d057de0f7d --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/util/ServerTempUtilsTest.java @@ -0,0 +1,56 @@ +/* + * 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.server.util; + +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 java.io.File; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ServerTempUtilsTest { + + @Rule + public ExpectedException throwable = ExpectedException.none(); + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Test + public void createTempFolder() throws Exception { + ServerFileSystem fs = mock(ServerFileSystem.class); + File serverTempFolder = temp.newFolder(); + when(fs.getTempDir()).thenReturn(serverTempFolder); + ServerTempUtils tempUtils = new ServerTempUtils(fs); + tempUtils.createTempDirectory(); + tempUtils.createTempFile(); + assertThat(new File(serverTempFolder, "tmp")).exists(); + assertThat(new File(serverTempFolder, "tmp").list()).hasSize(2); + + tempUtils.stop(); + assertThat(new File(serverTempFolder, "tmp")).doesNotExist(); + } +} -- cgit v1.2.3