Review, refactor and polish all Utility classes in the org.springframework.data.gemfire.tests.util package.

This commit is contained in:
John Blum
2018-03-22 10:43:10 -07:00
parent 92be8ef208
commit 7ed58ff8e8
7 changed files with 54 additions and 48 deletions

View File

@@ -31,10 +31,10 @@ import org.springframework.util.StringUtils;
* by working with {@link File} objects.
*
* @author John Blum
* @see File
* @see FileReader
* @see FileWriter
* @see org.springframework.data.gemfire.test.support.IOUtils
* @see java.io.File
* @see java.io.FileReader
* @see java.io.FileWriter
* @see org.springframework.data.gemfire.tests.util.IOUtils
* @since 0.0.1
*/
@SuppressWarnings("unused")
@@ -43,34 +43,31 @@ public abstract class FileUtils extends IOUtils {
public static final String FILE_SEPARATOR = System.getProperty("file.separator");
public static final String LINE_SEPARATOR = System.getProperty("line.separator");
/* (non-Javadoc) */
public static boolean isDirectory(File path) {
return (path != null && path.isDirectory());
return path != null && path.isDirectory();
}
/* (non-Javadoc) */
public static boolean isFile(File path) {
return (path != null && path.isFile());
return path != null && path.isFile();
}
/* (non-Javadoc) */
public static File newFile(String pathname) {
return new File(pathname);
}
/* (non-Javadoc) */
public static File newFile(File parent, String pathname) {
return new File(parent, pathname);
}
/* (non-Javadoc) */
@SuppressWarnings("all")
public static String read(File file) throws IOException {
Assert.isTrue(isFile(file), String.format("The file [%s] to read the contents from is not a valid file", file));
Assert.isTrue(isFile(file), String.format("The File [%s] to read the contents from is not a valid file", file));
BufferedReader fileReader = new BufferedReader(new FileReader(file));
try {
StringBuilder buffer = new StringBuilder();
for (String line = fileReader.readLine(); line != null; line = fileReader.readLine()) {
@@ -87,10 +84,11 @@ public abstract class FileUtils extends IOUtils {
/* (non-Javadoc) */
public static void write(File file, String contents) throws IOException {
Assert.notNull(file, "File must not be null");
Assert.isTrue(StringUtils.hasText(contents), String.format(
"The contents for File [%1$s] cannot be null or empty", file));
Assert.notNull(file, "File is required");
Assert.isTrue(StringUtils.hasText(contents),
String.format("The contents for File [%1$s] cannot be null or empty", file));
BufferedWriter fileWriter = null;

View File

@@ -30,7 +30,8 @@ import java.util.logging.Logger;
* The {@link IOUtils} class is an abstract utility class for working with IO operations.
*
* @author John Blum
* @see Closeable
* @see java.io.Closeable
* @see java.io.Serializable
* @since 0.0.1
*/
@SuppressWarnings("unused")
@@ -38,7 +39,6 @@ public abstract class IOUtils {
protected static final Logger log = Logger.getLogger(IOUtils.class.getName());
/* (non-Javadoc) */
public static boolean close(Closeable closeable) {
if (closeable != null) {
@@ -47,6 +47,7 @@ public abstract class IOUtils {
return true;
}
catch (IOException cause) {
if (log.isLoggable(Level.FINE)) {
log.fine(String.format("Failed to close the Closeable object (%1$s) due to an I/O error:%n%2$s",
closeable, ThrowableUtils.toString(cause)));
@@ -57,7 +58,6 @@ public abstract class IOUtils {
return false;
}
/* (non-Javadoc) */
@SuppressWarnings("unchecked")
public static <T> T deserializeObject(byte[] objectBytes) throws IOException, ClassNotFoundException {
@@ -75,7 +75,6 @@ public abstract class IOUtils {
}
}
/* (non-Javadoc) */
public static byte[] serializeObject(Serializable obj) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

View File

@@ -25,17 +25,17 @@ import java.util.logging.Logger;
* {@link SocketUtils} is a utility class for managing {@link Socket} and {@link ServerSocket} objects.
*
* @author John Blum
* @see ServerSocket
* @see Socket
* @since 1.9.0
* @see java.net.ServerSocket
* @see java.net.Socket
* @since 0.0.1
*/
@SuppressWarnings("unused")
public abstract class SocketUtils {
private static final Logger log = Logger.getLogger(SocketUtils.class.getName());
/* (non-Javadoc) */
public static boolean close(Socket socket) {
try {
if (socket != null) {
socket.close();
@@ -50,8 +50,8 @@ public abstract class SocketUtils {
return false;
}
/* (non-Javadoc) */
public static boolean close(ServerSocket serverSocket) {
try {
if (serverSocket != null) {
serverSocket.close();

View File

@@ -17,12 +17,13 @@
package org.springframework.data.gemfire.tests.util;
/**
* The StackTraceUtils class is a utility class for working with stack trace frames (elements) of the current Thread.
* The {@link StackTraceUtils} class is a utility class for working with stack trace frames (elements)
* of the current {@link Thread}.
*
* @author John Blum
* @see StackTraceElement
* @see Thread
* @see org.springframework.data.gemfire.test.support.ThreadUtils
* @see org.springframework.data.gemfire.tests.util.ThreadUtils
* @since 0.0.1
*/
@SuppressWarnings("unused")
@@ -49,6 +50,7 @@ public abstract class StackTraceUtils extends ThreadUtils {
}
public static StackTraceElement getTestCaller(final Thread thread) {
for (StackTraceElement stackTraceElement : thread.getStackTrace()) {
if (isTestSuiteClass(stackTraceElement) && isTestCaseMethod(stackTraceElement)) {
return stackTraceElement;
@@ -59,6 +61,7 @@ public abstract class StackTraceUtils extends ThreadUtils {
}
private static boolean isTestCaseMethod(final StackTraceElement element) {
boolean result = element.getMethodName().toLowerCase().startsWith("test");
try {
@@ -71,9 +74,11 @@ public abstract class StackTraceUtils extends ThreadUtils {
}
private static boolean isTestSuiteClass(final StackTraceElement element) {
boolean result = element.getClass().getSimpleName().toLowerCase().endsWith("test");
result |= element.getClass().isAssignableFrom(junit.framework.TestCase.class);
return result;
}
}

View File

@@ -22,14 +22,14 @@ import java.util.concurrent.TimeUnit;
* {@link ThreadUtils} is an abstract utility class for managing Java {@link Thread Threads}.
*
* @author John Blum
* @see Thread
* @see java.lang.Thread
* @since 0.0.1
*/
@SuppressWarnings("unused")
public abstract class ThreadUtils {
/* (non-Javadoc) */
public static boolean sleep(long milliseconds) {
try {
Thread.sleep(milliseconds);
return true;
@@ -45,16 +45,13 @@ public abstract class ThreadUtils {
}
public static boolean timedWait(long duration, long interval) {
return timedWait(duration, interval, new WaitCondition() {
@Override public boolean waiting() {
return true;
}
});
return timedWait(duration, interval, () -> true);
}
@SuppressWarnings("all")
public static boolean timedWait(long duration, long interval, WaitCondition waitCondition) {
final long timeout = (System.currentTimeMillis() + duration);
final long timeout = System.currentTimeMillis() + duration;
interval = Math.min(interval, duration);

View File

@@ -20,21 +20,24 @@ import java.io.PrintWriter;
import java.io.StringWriter;
/**
* The ThrowableUtils class is a utility class for working with Throwable, Exception and Error objects.
* The {@link ThrowableUtils} class is a utility class for working with {@link Throwable},
* {@link Exception} and {@link Error} objects.
*
* @author John Blum
* @see Error
* @see Exception
* @see Throwable
* @see java.lang.Error
* @see java.lang.Exception
* @see java.lang.Throwable
* @since 0.0.1
*/
@SuppressWarnings("unused")
public abstract class ThrowableUtils {
public static String toString(final Throwable t) {
public static String toString(Throwable throwable) {
StringWriter writer = new StringWriter();
t.printStackTrace(new PrintWriter(writer));
throwable.printStackTrace(new PrintWriter(writer));
return writer.toString();
}
}

View File

@@ -30,28 +30,32 @@ import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
/**
* The ZipUtils class is an abstract utility class for working with JAR and ZIP archives.
* The {@link ZipUtils} class is an abstract utility class for working with JAR and ZIP archives.
*
* @author John Blum
* @see File
* @see ZipFile
* @see java.io.File
* @see java.util.zip.ZipFile
* @since 0.0.1
*/
@SuppressWarnings("unused")
public abstract class ZipUtils {
public static void unzip(final Resource zipResource, final File directory) throws IOException {
Assert.notNull(zipResource, "The ZIP Resource must not be null!");
Assert.isTrue(directory != null && directory.isDirectory(), String.format(
"The file system pathname (%1$s) is not a valid directory!", directory));
Assert.notNull(zipResource, "ZIP Resource is required");
Assert.isTrue(directory != null && directory.isDirectory(),
String.format("The file system pathname (%1$s) is not a valid directory!", directory));
ZipFile zipFile = new ZipFile(zipResource.getFile(), ZipFile.OPEN_READ);
for (ZipEntry entry : CollectionUtils.iterable(zipFile.entries())) {
if (entry.isDirectory()) {
new File(directory, entry.getName()).mkdirs();
}
else {
DataInputStream entryInputStream = new DataInputStream(zipFile.getInputStream(entry));
DataOutputStream entryOutputStream = new DataOutputStream(new FileOutputStream(