[utils] Extract FileUtil.deleteDirectory() from duplicated code
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/c2af743b Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/c2af743b Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/c2af743b Branch: refs/heads/master Commit: c2af743b6c7daa392b1301946e9b5632b4a61fc1 Parents: fbb6a6f Author: Paul Campbell <pcampb...@kemitix.net> Authored: Mon Nov 5 22:03:15 2018 +0000 Committer: Paul Campbell <pcampb...@kemitix.net> Committed: Tue Nov 6 15:15:58 2018 +0000 ---------------------------------------------------------------------- .../brooklyn/util/core/osgi/OsgiTestBase.java | 21 ++++++-------------- .../org/apache/brooklyn/util/io/FileUtil.java | 17 ++++++++++++++++ .../rt/felix/EmbeddedFelixFrameworkTest.java | 18 ++++------------- 3 files changed, 27 insertions(+), 29 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/c2af743b/core/src/test/java/org/apache/brooklyn/util/core/osgi/OsgiTestBase.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/util/core/osgi/OsgiTestBase.java b/core/src/test/java/org/apache/brooklyn/util/core/osgi/OsgiTestBase.java index 31b8065..4bcadbd 100644 --- a/core/src/test/java/org/apache/brooklyn/util/core/osgi/OsgiTestBase.java +++ b/core/src/test/java/org/apache/brooklyn/util/core/osgi/OsgiTestBase.java @@ -16,15 +16,14 @@ package org.apache.brooklyn.util.core.osgi; import java.io.File; -import java.io.IOException; import org.apache.brooklyn.api.mgmt.ManagementContext; import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal; import org.apache.brooklyn.test.support.TestResourceUnavailableException; import org.apache.brooklyn.util.exceptions.Exceptions; +import org.apache.brooklyn.util.io.FileUtil; import org.apache.brooklyn.util.os.Os; import org.apache.brooklyn.util.osgi.OsgiTestResources; -import org.apache.commons.io.FileUtils; import org.osgi.framework.Bundle; import org.osgi.framework.BundleException; import org.osgi.framework.launch.Framework; @@ -44,7 +43,7 @@ public class OsgiTestBase { public static final String BROOKLYN_OSGI_TEST_A_0_1_0_PATH = OsgiTestResources.BROOKLYN_OSGI_TEST_A_0_1_0_PATH; public static final String BROOKLYN_OSGI_TEST_A_0_1_0_URL = "classpath:"+BROOKLYN_OSGI_TEST_A_0_1_0_PATH; - protected Bundle install(String url) throws BundleException { + protected Bundle install(String url) { try { return Osgis.install(framework, url); } catch (Exception e) { @@ -52,7 +51,7 @@ public class OsgiTestBase { } } - protected Bundle installFromClasspath(String resourceName) throws BundleException { + protected Bundle installFromClasspath(String resourceName) { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), resourceName); try { return Osgis.install(framework, String.format("classpath:%s", resourceName)); @@ -71,21 +70,13 @@ public class OsgiTestBase { } @AfterMethod(alwaysRun = true) - public void tearDown() throws BundleException, IOException, InterruptedException { + public void tearDown() { tearDownOsgiFramework(framework, storageTempDir); } - public static void tearDownOsgiFramework(Framework framework, File storageTempDir) throws BundleException, InterruptedException, IOException { + public static void tearDownOsgiFramework(Framework framework, File storageTempDir) { Osgis.ungetFramework(framework); - framework = null; - if (storageTempDir != null) { - try { - FileUtils.deleteDirectory(storageTempDir); - } catch (IOException e) { - log.warn(e.getMessage()); - } - storageTempDir = null; - } + FileUtil.deleteDirectory(storageTempDir); } public static void preinstallLibrariesLowLevelToPreventCatalogBomParsing(ManagementContext mgmt, String ...libraries) { http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/c2af743b/utils/common/src/main/java/org/apache/brooklyn/util/io/FileUtil.java ---------------------------------------------------------------------- diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/io/FileUtil.java b/utils/common/src/main/java/org/apache/brooklyn/util/io/FileUtil.java index a4908f2..176ad82 100644 --- a/utils/common/src/main/java/org/apache/brooklyn/util/io/FileUtil.java +++ b/utils/common/src/main/java/org/apache/brooklyn/util/io/FileUtil.java @@ -184,4 +184,21 @@ public class FileUtil { } return file.createNewFile(); } + + /** + * Recursively delete a directory. + * + * <p>Any IOException that might be raised is logged and not thrown.</p> + * + * @param file The directory to be deleted + */ + public static void deleteDirectory(final File file) { + if (file != null) { + try { + FileUtils.deleteDirectory(file); + } catch (IOException e) { + LOG.warn(e.getMessage()); + } + } + } } http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/c2af743b/utils/rt-felix/src/test/java/org/apache/brooklyn/rt/felix/EmbeddedFelixFrameworkTest.java ---------------------------------------------------------------------- diff --git a/utils/rt-felix/src/test/java/org/apache/brooklyn/rt/felix/EmbeddedFelixFrameworkTest.java b/utils/rt-felix/src/test/java/org/apache/brooklyn/rt/felix/EmbeddedFelixFrameworkTest.java index cde02b1..0c837cd 100644 --- a/utils/rt-felix/src/test/java/org/apache/brooklyn/rt/felix/EmbeddedFelixFrameworkTest.java +++ b/utils/rt-felix/src/test/java/org/apache/brooklyn/rt/felix/EmbeddedFelixFrameworkTest.java @@ -16,7 +16,6 @@ package org.apache.brooklyn.rt.felix; import java.io.File; -import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.Enumeration; @@ -25,11 +24,10 @@ import java.util.jar.JarInputStream; import org.apache.brooklyn.test.support.TestResourceUnavailableException; import org.apache.brooklyn.util.collections.MutableSet; +import org.apache.brooklyn.util.io.FileUtil; import org.apache.brooklyn.util.os.Os; import org.apache.brooklyn.util.osgi.OsgiTestResources; import org.apache.brooklyn.util.stream.Streams; -import org.apache.commons.io.FileUtils; -import org.osgi.framework.BundleException; import org.osgi.framework.launch.Framework; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -58,21 +56,13 @@ public class EmbeddedFelixFrameworkTest { } @AfterMethod(alwaysRun = true) - public void tearDown() throws BundleException, IOException, InterruptedException { + public void tearDown() { tearDownOsgiFramework(framework, storageTempDir); } - public static void tearDownOsgiFramework(Framework framework, File storageTempDir) throws BundleException, InterruptedException, IOException { + private static void tearDownOsgiFramework(Framework framework, File storageTempDir) { EmbeddedFelixFramework.stopFramework(framework); - framework = null; - if (storageTempDir != null) { - try { - FileUtils.deleteDirectory(storageTempDir); - } catch (IOException e) { - log.warn(e.getMessage()); - } - storageTempDir = null; - } + FileUtil.deleteDirectory(storageTempDir); } @Test