Skip to content

Commit

Permalink
Fix some IDE warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
centic9 committed Apr 29, 2018
1 parent bd494ee commit 113a9e6
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

package org.dstadler.commons.email;

import com.dumbster.smtp.SimpleSmtpServer;
import com.dumbster.smtp.SmtpActionType;
import com.dumbster.smtp.SmtpMessage;
import com.dumbster.smtp.SmtpRequest;
Expand Down Expand Up @@ -85,7 +84,7 @@ public final class SafeCloseSmtpServer implements Runnable {
/**
* Port the server listens on - set to the default SMTP port initially.
*/
private int port = SimpleSmtpServer.DEFAULT_SMTP_PORT;
private final int port;

private Semaphore semaphore = new Semaphore(MAXIMUM_CONCURRENT_READERS);

Expand All @@ -102,7 +101,6 @@ private SafeCloseSmtpServer(int port) {
/**
* Main loop of the SMTP server.
*/
@SuppressWarnings("resource")
@Override
public void run() {
stopped = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public int getPort() {
}

@Override
public void close() throws IOException {
public void close() {
httpd.stop();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -156,8 +157,9 @@ public static void deleteDirectory(File directory) throws IOException {

try {
FileUtils.deleteDirectory(directory);
} catch (@SuppressWarnings("unused") IOException e1) {
} catch (IOException e1) {
String[] list = directory.list();
Objects.requireNonNull(list, "Directory " + directory + " not found or not accessible");
StringBuilder builder = new StringBuilder("Had leftover files/directories: ").append(Arrays.toString(list)).append("\n");
for(String file : list) {
File subFile = new File(directory, file);
Expand Down
99 changes: 43 additions & 56 deletions src/main/java/org/dstadler/commons/testing/TestHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,58 +48,49 @@ public static void EqualsTest(final Object obj, final Object equal, final Object
assertNotNull("Non-equal-object in EqualsTest should not be null!", notequal);

// make sure different objects are passed in
assertFalse("Object and equals-object in EqualsTest should not be identical", obj == equal); // NOPMD
assertFalse("Object and non-equals-object in EqualsTest should not be identical", obj == notequal); // NOPMD
assertNotSame("Object and equals-object in EqualsTest should not be identical", obj, equal); // NOPMD
assertNotSame("Object and non-equals-object in EqualsTest should not be identical", obj, notequal); // NOPMD

// make sure correct objects are passed
assertTrue("Classes of objects in EqualsTest should be equal!", obj.getClass().equals(equal.getClass())); // NOPMD
assertTrue("Classes of objects in EqualsTest should be equal!", obj.getClass().equals( // NOPMD
notequal.getClass()));
assertEquals("Classes of objects in EqualsTest should be equal!", obj.getClass(), equal.getClass()); // NOPMD
assertEquals("Classes of objects in EqualsTest should be equal!", obj.getClass(), notequal.getClass());

// make sure correct parameters are passed
// equal should be equal to obj, not-equal should not be equal to obj!
assertTrue("Object and equal-object should be equal in EqualsTest!", obj.equals(equal)); // NOPMD
assertFalse("Object and non-equal-object should not be equal in EqualsTest!", obj.equals(notequal)); // NOPMD
assertEquals("Object and equal-object should be equal in EqualsTest!", obj, equal); // NOPMD
assertNotEquals("Object and non-equal-object should not be equal in EqualsTest!", obj, notequal); // NOPMD

// first test some general things that should be true with equals

// reflexive: equals to itself
assertTrue("Reflexive: object should be equal to itself in EqualsTest!", obj.equals(obj)); // NOPMD
assertTrue("Reflexive: equal-object should be equal to itself in EqualsTest!", equal.equals(equal)); // NOPMD
assertTrue("Reflexive: non-equal-object should be equal to itself in EqualsTest!", notequal // NOPMD
.equals(notequal));
assertEquals("Reflexive: object should be equal to itself in EqualsTest!", obj, obj); // NOPMD
assertEquals("Reflexive: equal-object should be equal to itself in EqualsTest!", equal, equal); // NOPMD
assertEquals("Reflexive: non-equal-object should be equal to itself in EqualsTest!", notequal, notequal);

// not equals to null
assertFalse("Object should not be equal to null in EqualsTest!", obj.equals(null)); // NOPMD - null-equals() is intended here
assertFalse("Equal-object should not be equal to null in EqualsTest!", equal.equals(null)); // NOPMD - null-equals() is intended here
assertFalse("Non-equal-object should not be equal to null in EqualsTest!", notequal.equals(null)); // NOPMD - null-equals() is intended here
assertNotEquals("Object should not be equal to null in EqualsTest!", null, obj); // NOPMD - null-equals() is intended here
assertNotEquals("Equal-object should not be equal to null in EqualsTest!", null, equal); // NOPMD - null-equals() is intended here
assertNotEquals("Non-equal-object should not be equal to null in EqualsTest!", null, notequal); // NOPMD - null-equals() is intended here

// not equals to a different type of object
assertFalse("Object should not be equal to an arbitrary string in EqualsTest!", obj
.equals("TestString")); // NOPMD
assertNotEquals("Object should not be equal to an arbitrary string in EqualsTest!", "TestString", obj); // NOPMD

// then test some things with another object that should be equal

// symmetric, if one is (not) equal to another then the reverse must be true
assertTrue("Symmetric: Object should be equal to equal-object in EqualsTest", obj.equals(equal)); // NOPMD
assertTrue("Symmetric: Equals-object should be equal to object in EqualsTest!", equal.equals(obj)); // NOPMD
assertFalse("Symmetric: Object should NOT be equal to non-equal-object in EqualsTest", obj // NOPMD
.equals(notequal));
assertFalse("Symmetric: Non-equals-object should NOT be equal to object in EqualsTest!", notequal // NOPMD
.equals(obj));
assertEquals("Symmetric: Object should be equal to equal-object in EqualsTest", obj, equal); // NOPMD
assertEquals("Symmetric: Equals-object should be equal to object in EqualsTest!", equal, obj); // NOPMD
assertNotEquals("Symmetric: Object should NOT be equal to non-equal-object in EqualsTest", obj, notequal);
assertNotEquals("Symmetric: Non-equals-object should NOT be equal to object in EqualsTest!", notequal, obj);

// transitive: if a.equals(b) and b.equals(c) then a.equals(c)
// not tested right now

// hashCode: equal objects should have equal hash code
assertTrue("Transitive: Equal objects should have equal hash-code in EqualsTest!", // NOPMD
obj.hashCode() == equal.hashCode());
assertTrue("Transitive: Equal objects should have equal hash-code in EqualsTest!", obj.hashCode() == obj // NOPMD
.hashCode());
assertTrue("Transitive: Equal objects should have equal hash-code in EqualsTest!", // NOPMD
equal.hashCode() == equal.hashCode());
assertTrue("Transitive: Equal objects should have equal hash-code in EqualsTest!", // NOPMD
notequal.hashCode() == notequal.hashCode());
assertEquals("Transitive: Equal objects should have equal hash-code in EqualsTest!", obj.hashCode(), equal.hashCode());
assertEquals("Transitive: Equal objects should have equal hash-code in EqualsTest!", obj.hashCode(), obj.hashCode()); // NOPMD
assertEquals("Transitive: Equal objects should have equal hash-code in EqualsTest!", equal.hashCode(), equal.hashCode());
assertEquals("Transitive: Equal objects should have equal hash-code in EqualsTest!", notequal.hashCode(), notequal.hashCode());
}

/**
Expand All @@ -125,15 +116,15 @@ public static <T extends Comparable<T>> void CompareToTest(final T obj, final T
assertNotNull("Non-equal-object in CompareToTest should not be null!", notequal); // NOPMD

// make sure different objects are passed in
assertFalse("Object and equals-object in CompareToTest should not be identical", obj == equal); // NOPMD
assertFalse("Object and non-equals-object in CompareToTest should not be identical", obj == notequal); // NOPMD
assertNotSame("Object and equals-object in CompareToTest should not be identical", obj, equal); // NOPMD
assertNotSame("Object and non-equals-object in CompareToTest should not be identical", obj, notequal); // NOPMD

// make sure correct parameters are passed
// equal should be equal to obj, not-equal should not be equal to obj!
assertEquals("Object and equal-object should compare in CompareToTest!", 0, obj.compareTo(equal));
assertFalse("Object and non-equal-object should not compare in CompareToTest!", 0 == obj // NOPMD
assertNotEquals("Object and non-equal-object should not compare in CompareToTest!", 0, obj // NOPMD
.compareTo(notequal));
assertFalse("Equal-object and non-equal-object should not compare in CompareToTest!", 0 == equal // NOPMD
assertNotEquals("Equal-object and non-equal-object should not compare in CompareToTest!", 0, equal // NOPMD
.compareTo(notequal));

// first test some general things that should be true with equals
Expand Down Expand Up @@ -166,10 +157,9 @@ public static <T extends Comparable<T>> void CompareToTest(final T obj, final T
.compareTo(equal));
assertEquals("Symmetric: Equals-object should be equal to object in CompareToTest!", 0, equal // NOPMD
.compareTo(obj));
assertFalse("Symmetric: Object should NOT be equal to non-equal-object in CompareToTest", 0 == obj // NOPMD
assertNotEquals("Symmetric: Object should NOT be equal to non-equal-object in CompareToTest", 0, obj // NOPMD
.compareTo(notequal));
assertFalse("Symmetric: Non-equals-object should NOT be equal to object in CompareToTest!", // NOPMD
0 == notequal.compareTo(obj));
assertNotEquals("Symmetric: Non-equals-object should NOT be equal to object in CompareToTest!", 0, notequal.compareTo(obj));
assertEquals("Symnmetric: Comparing object and non-equal-object in both directions should lead to the same result.",
signum(obj.compareTo(notequal)), (-1)*signum(notequal.compareTo(obj)));

Expand All @@ -188,8 +178,8 @@ public static <T extends Comparable<T>> void CompareToTest(final T obj, final T
}

// ensure equals() and hashCode() are implemented as well here
assertTrue("Findbugs: Comparable objects should implement equals() the same way as compareTo().", obj.equals(equal));
assertFalse("Findbugs: Comparable objects should implement equals() the same way as compareTo().", obj.equals(notequal));
assertEquals("Findbugs: Comparable objects should implement equals() the same way as compareTo().", obj, equal);
assertNotEquals("Findbugs: Comparable objects should implement equals() the same way as compareTo().", obj, notequal);
EqualsTest(obj, equal, notequal);
assertEquals("Findbugs: Comparable objects should implement hashCode() the same way as compareTo().", obj.hashCode(), equal.hashCode());
HashCodeTest(obj, equal);
Expand Down Expand Up @@ -219,13 +209,13 @@ public static <T> void ComparatorTest(final Comparator<T> comparator, final T ob
assertNotNull("Non-equal-object in ComparatorTest should not be null!", notequal); // NOPMD

// make sure different objects are passed in
assertFalse("Object and equals-object in ComparatorTest should not be identical", obj == equal); // NOPMD
assertFalse("Object and non-equals-object in ComparatorTest should not be identical", obj == notequal); // NOPMD
assertNotSame("Object and equals-object in ComparatorTest should not be identical", obj, equal); // NOPMD
assertNotSame("Object and non-equals-object in ComparatorTest should not be identical", obj, notequal); // NOPMD

// make sure correct parameters are passed
// equal should be equal to obj, not-equal should not be equal to obj!
assertEquals("Object and equal-object should compare in ComparatorTest!", 0, comparator.compare(obj, equal));
assertFalse("Object and non-equal-object should not compare in ComparatorTest!", 0 == comparator.compare(obj // NOPMD
assertNotEquals("Object and non-equal-object should not compare in ComparatorTest!", 0, comparator.compare(obj // NOPMD
, notequal));

// first test some general things that should be true with equals
Expand Down Expand Up @@ -258,10 +248,9 @@ public static <T> void ComparatorTest(final Comparator<T> comparator, final T ob
, equal));
assertEquals("Symmetric: Equals-object should be equal to object in ComparatorTest!", 0, comparator.compare(equal // NOPMD
, obj));
assertFalse("Symmetric: Object should NOT be equal to non-equal-object in ComparatorTest", 0 == comparator.compare(obj // NOPMD
assertNotEquals("Symmetric: Object should NOT be equal to non-equal-object in ComparatorTest", 0, comparator.compare(obj // NOPMD
, notequal));
assertFalse("Symmetric: Non-equals-object should NOT be equal to object in ComparatorTest!", // NOPMD
0 == comparator.compare(notequal, obj));
assertNotEquals("Symmetric: Non-equals-object should NOT be equal to object in ComparatorTest!", 0, comparator.compare(notequal, obj));
assertEquals("Symnmetric: Comparing object and non-equal-object in both directions should lead to the same result.",
signum(comparator.compare(obj, notequal)), (-1)*signum(comparator.compare(notequal, obj)));

Expand Down Expand Up @@ -308,7 +297,7 @@ public static void ToStringTest(final Object obj) {
assertNotNull("A derived toString() should not return null!", obj.toString());

// toString should not return an empty string
assertFalse("A derived toString() should not return an empty string!", obj.toString().equals(""));
assertNotEquals("A derived toString() should not return an empty string!", "", obj.toString());

// check that calling it multiple times leads to the same value
String value = obj.toString();
Expand Down Expand Up @@ -338,16 +327,16 @@ public static void CloneTest(final Cloneable obj) throws Exception {
// m.isAccessible());

// clone should return a different object, not the same again
assertTrue("clone() should not return the object itself in CloneTest!", obj != m.invoke(obj, // NOPMD
new Object[] {}));
assertNotSame("clone() should not return the object itself in CloneTest!",
obj, m.invoke(obj)); // NOPMD

// should return the same type of object
assertTrue("clone() should return the same type of object (i.e. the same class) in CloneTest!", m // NOPMD
.invoke(obj).getClass() == obj.getClass());
assertSame("clone() should return the same type of object (i.e. the same class) in CloneTest!", m // NOPMD
.invoke(obj).getClass(), obj.getClass());

// cloned objects should be equal to the original object
assertTrue("clone() should return an object that is equal() to the original object in CloneTest!", m
.invoke(obj).equals(obj));
assertEquals("clone() should return an object that is equal() to the original object in CloneTest!", m
.invoke(obj), obj);
}

/**
Expand All @@ -358,9 +347,7 @@ public static void CloneTest(final Cloneable obj) throws Exception {
* @param equ An Object which should return the same hashCode() as obj.
*/
public static void HashCodeTest(final Object obj, final Object equ) {
assertFalse( // NOPMD
"HashCodeTest expects two distinct objects with equal hashCode, but the same object is provided twice!",
obj == equ);
assertNotSame("HashCodeTest expects two distinct objects with equal hashCode, but the same object is provided twice!", obj, equ);

// The same object returns the same hashCode always
final int hash = obj.hashCode();
Expand Down Expand Up @@ -598,7 +585,7 @@ public static void assumeCanShowDialogs() {
}

/**
* Creates a temporary directory which is guaranted to be unique (via File.createTempFile)
* Creates a temporary directory which is guaranteed to be unique (via File.createTempFile)
* and ensures that the directory exists.
*
* Note: The caller needs to ensure that the directory is removed again after use else it
Expand Down
62 changes: 24 additions & 38 deletions src/main/java/org/dstadler/commons/testing/ThreadTestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.junit.Assert.assertEquals;

import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -43,7 +42,6 @@ public void run(int threadNum, int iter) throws Exception {
}
</code>
*/
@SuppressWarnings("Convert2Lambda") // should still compile with Java 7
public class ThreadTestHelper {

private static Logger log = LoggerFactory.make();
Expand All @@ -52,7 +50,7 @@ public class ThreadTestHelper {
private final int testsPerThread;

private volatile Throwable exception = null;
private int executions[] = null;
private int executions[];

/**
* Initialize the class with the number of tests that should be executed
Expand Down Expand Up @@ -115,23 +113,15 @@ public static <T> List<T> executeTest(final Callable<T> testable, int runs) thro
final CyclicBarrier barrier = new CyclicBarrier(runs);
ExecutorService executor = Executors.newCachedThreadPool(
new BasicThreadFactory.Builder()
.uncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
log.log(Level.SEVERE, "An uncaught exception happened in Thread " + t.getName(), e);
}
})
.uncaughtExceptionHandler((t, e) -> log.log(Level.SEVERE, "An uncaught exception happened in Thread " + t.getName(), e))
.namingPattern(ThreadTestHelper.class.getSimpleName() + "-Thread-%d")
.build());
try {
List<Callable<T>> tasks = new ArrayList<>(runs);
for (int i = 0; i < runs; i++) {
tasks.add(new Callable<T>() {
@Override
public T call() throws Exception {
barrier.await(); // causes more contention
return testable.call();
}
tasks.add(() -> {
barrier.await(); // causes more contention
return testable.call();
});
}

Expand Down Expand Up @@ -164,32 +154,28 @@ public T call() throws Exception {
private Thread startThread(final int threadNum, final TestRunnable run) {
log.fine("Starting thread number: " + threadNum);

Thread t1 = new Thread(new Runnable() {

@Override
public void run() {
try {
for (int iter = 0; iter < testsPerThread && exception == null; iter++) {
// log.fine("Executing iteration " + iter +
// " in thread" +
// Thread.currentThread().getName());

// call the actual testcode
run.run(threadNum, iter);

executions[threadNum]++;
}

// do end-work here, we don't do this in a finally as we log
// Exception
// then anyway
run.doEnd(threadNum);
} catch (Throwable e) {
// log.log(Level.SEVERE, "Caught unexpected Throwable", e);
exception = e;
Thread t1 = new Thread(() -> {
try {
for (int iter = 0; iter < testsPerThread && exception == null; iter++) {
// log.fine("Executing iteration " + iter +
// " in thread" +
// Thread.currentThread().getName());

// call the actual testcode
run.run(threadNum, iter);

executions[threadNum]++;
}

// do end-work here, we don't do this in a finally as we log
// Exception
// then anyway
run.doEnd(threadNum);
} catch (Throwable e) {
// log.log(Level.SEVERE, "Caught unexpected Throwable", e);
exception = e;
}

}, "ThreadTestHelper-Thread " + threadNum + ": " + run.getClass().getName());

t1.start();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.dstadler.commons.testing;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import org.junit.Test;
Expand All @@ -14,7 +15,7 @@ public class PrivateConstructorCoverageTest {
@Test
public void testExecutePrivateConstructor() throws Exception {
// run this on itself to cover it!
PrivateConstructorCoverage.executePrivateConstructor(PrivateConstructorCoverage.class);
assertNotNull(PrivateConstructorCoverage.executePrivateConstructor(PrivateConstructorCoverage.class));

// run it with an abstract class to check for exception
try {
Expand Down
Loading

0 comments on commit 113a9e6

Please sign in to comment.