1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-15 20:12:04 +01:00

Merge pull request #259 from vanitasvitae/fileutils

(Re)move duplicate FileUtils methods
This commit is contained in:
Florian Schmaus 2018-07-30 16:24:02 +02:00 committed by GitHub
commit f3b65f3dc8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 52 additions and 68 deletions

View file

@ -31,7 +31,6 @@ import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.Stack;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -156,48 +155,4 @@ public final class FileUtils {
return false; return false;
} }
} }
public static void deleteDirectory(File root) {
if (!root.exists()) {
return;
}
File[] currList;
Stack<File> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
if (stack.lastElement().isDirectory()) {
currList = stack.lastElement().listFiles();
if (currList != null && currList.length > 0) {
for (File curr : currList) {
stack.push(curr);
}
} else {
stack.pop().delete();
}
} else {
stack.pop().delete();
}
}
}
/**
* Returns a {@link File} pointing to a temporary directory. On unix like systems this might be {@code /tmp}
* for example.
* If {@code suffix} is not null, the returned file points to {@code <temp>/suffix}.
*
* @param suffix optional path suffix
* @return temp directory
*/
public static File getTempDir(String suffix) {
String temp = System.getProperty("java.io.tmpdir");
if (temp == null) {
temp = "tmp";
}
if (suffix == null) {
return new File(temp);
} else {
return new File(temp, suffix);
}
}
} }

View file

@ -17,6 +17,7 @@
package org.jivesoftware.smack.test.util; package org.jivesoftware.smack.test.util;
import java.io.File; import java.io.File;
import java.util.Stack;
public class FileTestUtil { public class FileTestUtil {
@ -40,4 +41,32 @@ public class FileTestUtil {
return new File(temp, suffix); return new File(temp, suffix);
} }
} }
/**
* Recursively delete a directory and its contents.
*
* @param root root directory
*/
public static void deleteDirectory(File root) {
if (!root.exists()) {
return;
}
File[] currList;
Stack<File> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
if (stack.lastElement().isDirectory()) {
currList = stack.lastElement().listFiles();
if (currList != null && currList.length > 0) {
for (File curr : currList) {
stack.push(curr);
}
} else {
stack.pop().delete();
}
} else {
stack.pop().delete();
}
}
}
} }

View file

@ -32,7 +32,7 @@ import java.util.logging.Level;
import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.util.FileUtils; import org.jivesoftware.smack.test.util.FileTestUtil;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.ox.callback.backup.AskForBackupCodeCallback; import org.jivesoftware.smackx.ox.callback.backup.AskForBackupCodeCallback;
import org.jivesoftware.smackx.ox.callback.backup.DisplayBackupCodeCallback; import org.jivesoftware.smackx.ox.callback.backup.DisplayBackupCodeCallback;
@ -63,8 +63,8 @@ import org.pgpainless.key.protection.UnprotectedKeysProtector;
public class OXSecretKeyBackupIntegrationTest extends AbstractOpenPgpIntegrationTest { public class OXSecretKeyBackupIntegrationTest extends AbstractOpenPgpIntegrationTest {
private static final String sessionId = StringUtils.randomString(10); private static final String sessionId = StringUtils.randomString(10);
private static final File beforePath = FileUtils.getTempDir("ox_backup_" + sessionId); private static final File beforePath = FileTestUtil.getTempDir("ox_backup_" + sessionId);
private static final File afterPath = FileUtils.getTempDir("ox_restore_" + sessionId); private static final File afterPath = FileTestUtil.getTempDir("ox_restore_" + sessionId);
private String backupCode = null; private String backupCode = null;
@ -110,8 +110,8 @@ public class OXSecretKeyBackupIntegrationTest extends AbstractOpenPgpIntegration
@BeforeClass @BeforeClass
public static void cleanStore() { public static void cleanStore() {
LOGGER.log(Level.INFO, "Delete store directories..."); LOGGER.log(Level.INFO, "Delete store directories...");
FileUtils.deleteDirectory(afterPath); FileTestUtil.deleteDirectory(afterPath);
FileUtils.deleteDirectory(beforePath); FileTestUtil.deleteDirectory(beforePath);
} }
@After @After

View file

@ -25,7 +25,7 @@ import java.util.logging.Level;
import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.util.FileUtils; import org.jivesoftware.smack.test.util.FileTestUtil;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.ox.AbstractOpenPgpIntegrationTest; import org.jivesoftware.smackx.ox.AbstractOpenPgpIntegrationTest;
import org.jivesoftware.smackx.ox.OpenPgpContact; import org.jivesoftware.smackx.ox.OpenPgpContact;
@ -49,8 +49,8 @@ import org.pgpainless.key.protection.UnprotectedKeysProtector;
public class OXInstantMessagingIntegrationTest extends AbstractOpenPgpIntegrationTest { public class OXInstantMessagingIntegrationTest extends AbstractOpenPgpIntegrationTest {
private static final String sessionId = StringUtils.randomString(10); private static final String sessionId = StringUtils.randomString(10);
private static final File aliceStorePath = FileUtils.getTempDir("basic_ox_messaging_test_alice_" + sessionId); private static final File aliceStorePath = FileTestUtil.getTempDir("basic_ox_messaging_test_alice_" + sessionId);
private static final File bobStorePath = FileUtils.getTempDir("basic_ox_messaging_test_bob_" + sessionId); private static final File bobStorePath = FileTestUtil.getTempDir("basic_ox_messaging_test_bob_" + sessionId);
private OpenPgpV4Fingerprint aliceFingerprint = null; private OpenPgpV4Fingerprint aliceFingerprint = null;
private OpenPgpV4Fingerprint bobFingerprint = null; private OpenPgpV4Fingerprint bobFingerprint = null;
@ -97,8 +97,8 @@ public class OXInstantMessagingIntegrationTest extends AbstractOpenPgpIntegratio
@AfterClass @AfterClass
public static void deleteStore() { public static void deleteStore() {
LOGGER.log(Level.INFO, "Deleting storage directories..."); LOGGER.log(Level.INFO, "Deleting storage directories...");
FileUtils.deleteDirectory(aliceStorePath); FileTestUtil.deleteDirectory(aliceStorePath);
FileUtils.deleteDirectory(bobStorePath); FileTestUtil.deleteDirectory(bobStorePath);
} }
@SmackIntegrationTest @SmackIntegrationTest

View file

@ -35,8 +35,8 @@ import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.jivesoftware.smack.test.util.FileTestUtil;
import org.jivesoftware.smack.test.util.SmackTestSuite; import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.util.FileUtils;
import org.jivesoftware.smackx.ox.callback.SecretKeyPassphraseCallback; import org.jivesoftware.smackx.ox.callback.SecretKeyPassphraseCallback;
import org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException; import org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException;
import org.jivesoftware.smackx.ox.store.definition.OpenPgpStore; import org.jivesoftware.smackx.ox.store.definition.OpenPgpStore;
@ -65,7 +65,7 @@ import org.pgpainless.util.Passphrase;
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OpenPgpStoreTest extends SmackTestSuite { public class OpenPgpStoreTest extends SmackTestSuite {
private static File storagePath; private static final File storagePath;
private static final BareJid alice = JidTestUtil.BARE_JID_1; private static final BareJid alice = JidTestUtil.BARE_JID_1;
private static final BareJid bob = JidTestUtil.BARE_JID_2; private static final BareJid bob = JidTestUtil.BARE_JID_2;
@ -78,7 +78,7 @@ public class OpenPgpStoreTest extends SmackTestSuite {
private final OpenPgpStore openPgpStoreInstance2; private final OpenPgpStore openPgpStoreInstance2;
static { static {
storagePath = FileUtils.getTempDir("storeTest"); storagePath = FileTestUtil.getTempDir("storeTest");
Security.addProvider(new BouncyCastleProvider()); Security.addProvider(new BouncyCastleProvider());
} }
@ -101,7 +101,7 @@ public class OpenPgpStoreTest extends SmackTestSuite {
@Before @Before
@After @After
public void deletePath() { public void deletePath() {
FileUtils.deleteDirectory(storagePath); FileTestUtil.deleteDirectory(storagePath);
} }
/* /*

View file

@ -32,8 +32,8 @@ import java.util.List;
import org.jivesoftware.smack.DummyConnection; import org.jivesoftware.smack.DummyConnection;
import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.test.util.FileTestUtil;
import org.jivesoftware.smack.test.util.SmackTestSuite; import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.util.FileUtils;
import org.jivesoftware.smackx.ox.crypto.OpenPgpElementAndMetadata; import org.jivesoftware.smackx.ox.crypto.OpenPgpElementAndMetadata;
import org.jivesoftware.smackx.ox.crypto.PainlessOpenPgpProvider; import org.jivesoftware.smackx.ox.crypto.PainlessOpenPgpProvider;
import org.jivesoftware.smackx.ox.element.CryptElement; import org.jivesoftware.smackx.ox.element.CryptElement;
@ -62,13 +62,13 @@ public class PainlessOpenPgpProviderTest extends SmackTestSuite {
private static final BareJid bob = JidTestUtil.BARE_JID_2; private static final BareJid bob = JidTestUtil.BARE_JID_2;
static { static {
storagePath = FileUtils.getTempDir("smack-painlessprovidertest"); storagePath = FileTestUtil.getTempDir("smack-painlessprovidertest");
} }
@BeforeClass @BeforeClass
@AfterClass @AfterClass
public static void deletePath() { public static void deletePath() {
FileUtils.deleteDirectory(storagePath); FileTestUtil.deleteDirectory(storagePath);
} }
@Test @Test

View file

@ -28,8 +28,8 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import org.jivesoftware.smack.DummyConnection; import org.jivesoftware.smack.DummyConnection;
import org.jivesoftware.smack.test.util.FileTestUtil;
import org.jivesoftware.smack.test.util.SmackTestSuite; import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.util.FileUtils;
import org.jivesoftware.smackx.ox.crypto.PainlessOpenPgpProvider; import org.jivesoftware.smackx.ox.crypto.PainlessOpenPgpProvider;
import org.jivesoftware.smackx.ox.element.SecretkeyElement; import org.jivesoftware.smackx.ox.element.SecretkeyElement;
import org.jivesoftware.smackx.ox.exception.InvalidBackupCodeException; import org.jivesoftware.smackx.ox.exception.InvalidBackupCodeException;
@ -54,7 +54,7 @@ public class SecretKeyBackupHelperTest extends SmackTestSuite {
private static final File basePath; private static final File basePath;
static { static {
basePath = FileUtils.getTempDir("ox_secret_keys"); basePath = FileTestUtil.getTempDir("ox_secret_keys");
} }
@Test @Test
@ -98,6 +98,6 @@ public class SecretKeyBackupHelperTest extends SmackTestSuite {
@AfterClass @AfterClass
@BeforeClass @BeforeClass
public static void deleteDirs() { public static void deleteDirs() {
FileUtils.deleteDirectory(basePath); FileTestUtil.deleteDirectory(basePath);
} }
} }

View file

@ -34,8 +34,8 @@ import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.test.util.FileTestUtil;
import org.jivesoftware.smack.test.util.SmackTestSuite; import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.util.FileUtils;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.eme.element.ExplicitMessageEncryptionElement; import org.jivesoftware.smackx.eme.element.ExplicitMessageEncryptionElement;
import org.jivesoftware.smackx.ox.OpenPgpContact; import org.jivesoftware.smackx.ox.OpenPgpContact;
@ -62,7 +62,7 @@ public class OXInstantMessagingManagerTest extends SmackTestSuite {
private static final File basePath; private static final File basePath;
static { static {
basePath = FileUtils.getTempDir("ox_im_test_" + StringUtils.randomString(10)); basePath = FileTestUtil.getTempDir("ox_im_test_" + StringUtils.randomString(10));
} }
@Test @Test
@ -170,6 +170,6 @@ public class OXInstantMessagingManagerTest extends SmackTestSuite {
@AfterClass @AfterClass
@BeforeClass @BeforeClass
public static void deleteDirs() { public static void deleteDirs() {
FileUtils.deleteDirectory(basePath); FileTestUtil.deleteDirectory(basePath);
} }
} }