Clean up, convert all to JUnit 4 style tests, replace system.out calls
with loggers.
This commit is contained in:
Timothy Bish 2015-07-10 17:14:48 -04:00
parent 39a1e04554
commit 18300c6514
12 changed files with 136 additions and 100 deletions

View File

@ -16,6 +16,10 @@
*/
package org.apache.activemq.thread;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
@ -28,28 +32,34 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
public class PooledTaskRunnerTest {
@Rule public TestName name = new TestName();
public class PooledTaskRunnerTest extends TestCase {
private ExecutorService executor;
@Override
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
executor = Executors.newCachedThreadPool(new IgnoreUncaughtExceptionThreadFactory());
}
@Override
protected void tearDown() throws Exception {
@After
public void tearDown() throws Exception {
executor.shutdownNow();
super.tearDown();
}
@Test
public void testNormalBehavior() throws Exception {
final CountDownLatch latch = new CountDownLatch( 1 );
PooledTaskRunner runner = new PooledTaskRunner( executor, new Task() {
@Override
public boolean iterate() {
latch.countDown();
@ -64,12 +74,13 @@ public class PooledTaskRunnerTest extends TestCase {
runner.shutdown();
}
@Test
public void testWakeupResultsInThreadSafeCalls() throws Exception {
ThreadPoolExecutor executor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 10, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
Thread thread = new Thread(runnable, getName());
Thread thread = new Thread(runnable, name.getMethodName());
thread.setDaemon(true);
thread.setPriority(Thread.NORM_PRIORITY);
return thread;
@ -82,6 +93,7 @@ public class PooledTaskRunnerTest extends TestCase {
final PooledTaskRunner runner = new PooledTaskRunner(executor, new Task() {
String threadUnSafeVal = null;
@Override
public boolean iterate() {
if (threadUnSafeVal != null) {
clashCount.incrementAndGet();
@ -98,6 +110,7 @@ public class PooledTaskRunnerTest extends TestCase {
}, 1 );
Runnable doWakeup = new Runnable() {
@Override
public void run() {
try {
runner.wakeup();
@ -105,27 +118,30 @@ public class PooledTaskRunnerTest extends TestCase {
}
}
};
final int iterations = 1000;
for (int i=0; i< iterations; i++) {
if (i%100 == 0) {
Thread.sleep(10);
}
executor.execute(doWakeup);
}
}
doneLatch.await(20, TimeUnit.SECONDS);
assertEquals("thread safety clash", 0, clashCount.get());
assertTrue("called more than once", count.get() > 1);
runner.shutdown();
}
@Test
public void testShutsDownAfterRunnerFailure() throws Exception {
Future<Object> future = executor.submit( new Callable<Object>() {
@Override
public Object call() throws Exception {
final CountDownLatch latch = new CountDownLatch( 1 );
PooledTaskRunner runner = new PooledTaskRunner( executor, new Task() {
@Override
public boolean iterate() {
latch.countDown();
@ -149,15 +165,19 @@ public class PooledTaskRunnerTest extends TestCase {
fail( "TaskRunner did not shut down cleanly" );
}
}
class IgnoreUncaughtExceptionThreadFactory implements ThreadFactory, Thread.UncaughtExceptionHandler {
private class IgnoreUncaughtExceptionThreadFactory implements ThreadFactory, Thread.UncaughtExceptionHandler {
ThreadFactory threadFactory = Executors.defaultThreadFactory();
@Override
public Thread newThread(Runnable r) {
Thread thread = threadFactory.newThread(r);
thread.setUncaughtExceptionHandler(this);
return thread;
}
@Override
public void uncaughtException(Thread t, Throwable e) {
// ignore ie: no printStackTrace that would sully the test console
}

View File

@ -16,24 +16,29 @@
*/
package org.apache.activemq.thread;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import junit.framework.TestCase;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TaskRunnerTest extends TestCase {
public class TaskRunnerTest {
private static final Logger LOG = LoggerFactory.getLogger(TaskRunnerTest.class);
@Test
public void testWakeupPooled() throws InterruptedException, BrokenBarrierException {
System.setProperty("org.apache.activemq.UseDedicatedTaskRunner", "false");
doTestWakeup();
}
@Test
public void testWakeupDedicated() throws InterruptedException, BrokenBarrierException {
System.setProperty("org.apache.activemq.UseDedicatedTaskRunner", "true");
doTestWakeup();
@ -42,7 +47,7 @@ public class TaskRunnerTest extends TestCase {
/**
* Simulate multiple threads queuing work for the TaskRunner. The Task
* Runner dequeues the work.
*
*
* @throws InterruptedException
* @throws BrokenBarrierException
*/
@ -56,6 +61,7 @@ public class TaskRunnerTest extends TestCase {
TaskRunnerFactory factory = new TaskRunnerFactory();
final TaskRunner runner = factory.createTaskRunner(new Task() {
@Override
public boolean iterate() {
if (queue.get() == 0) {
return false;
@ -78,6 +84,7 @@ public class TaskRunnerTest extends TestCase {
final CyclicBarrier barrier = new CyclicBarrier(workerCount + 1);
for (int i = 0; i < workerCount; i++) {
new Thread() {
@Override
public void run() {
try {
barrier.await();
@ -104,9 +111,4 @@ public class TaskRunnerTest extends TestCase {
runner.shutdown();
}
public static void main(String[] args) {
junit.textui.TestRunner.run(TaskRunnerTest.class);
}
}

View File

@ -30,13 +30,17 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MemoryUsageConcurrencyTest {
private static final Logger LOG = LoggerFactory.getLogger(MemoryUsageConcurrencyTest.class);
@Test
public void testCycle() throws Exception {
Random r = new Random(0xb4a14);
for (int i = 0; i < 50000; i++) {
for (int i = 0; i < 30000; i++) {
checkPercentage(i, i, r.nextInt(100) + 10, i % 2 == 0, i % 5 == 0);
}
}
@ -154,7 +158,7 @@ public class MemoryUsageConcurrencyTest {
try {
waitForSpaceThread.join(1000);
} catch (InterruptedException e) {
System.out.println("Attempt: " + attempt + " : " + memUsage + " waitForSpace never returned");
LOG.debug("Attempt: {} : {} waitForSpace never returned", attempt, memUsage);
waitForSpaceThread.interrupt();
waitForSpaceThread.join();
}
@ -164,8 +168,8 @@ public class MemoryUsageConcurrencyTest {
addThread.join();
if (memUsage.getPercentUsage() != 0 || memUsage.getUsage() != memUsage.getPercentUsage()) {
System.out.println("Attempt: " + attempt + " : " + memUsage);
System.out.println("Operations: " + ops);
LOG.debug("Attempt: {} : {}", attempt, memUsage);
LOG.debug("Operations: {}", ops);
assertEquals(0, memUsage.getPercentUsage());
}
}

View File

@ -16,18 +16,21 @@
*/
package org.apache.activemq.util;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
public class DataByteArrayInputStreamTest extends TestCase {
import org.junit.Test;
public class DataByteArrayInputStreamTest {
/**
* https://issues.apache.org/activemq/browse/AMQ-1911
*/
@Test
public void testNonAscii() throws Exception {
doMarshallUnMarshallValidation("mei\u00DFen");
String accumulator = new String();
int test = 0; // int to get Supplementary chars
while(Character.isDefined(test)) {
String toTest = String.valueOf((char)test);
@ -35,15 +38,15 @@ public class DataByteArrayInputStreamTest extends TestCase {
doMarshallUnMarshallValidation(toTest);
test++;
}
int massiveThreeByteCharValue = 0x0FFF;
String toTest = String.valueOf((char)massiveThreeByteCharValue);
accumulator += toTest;
doMarshallUnMarshallValidation(String.valueOf((char)massiveThreeByteCharValue));
// Altogether
doMarshallUnMarshallValidation(accumulator);
// the three byte values
char t = '\u0800';
final char max = '\uffff';
@ -54,20 +57,20 @@ public class DataByteArrayInputStreamTest extends TestCase {
doMarshallUnMarshallValidation(val);
t++;
}
// Altogether so long as it is not too big
while (accumulator.length() > 20000) {
accumulator = accumulator.substring(20000);
}
doMarshallUnMarshallValidation(accumulator);
}
void doMarshallUnMarshallValidation(String value) throws Exception {
DataByteArrayOutputStream out = new DataByteArrayOutputStream();
out.writeBoolean(true);
out.writeUTF(value);
out.close();
DataByteArrayInputStream in = new DataByteArrayInputStream(out.getData());
in.readBoolean();
String readBack = in.readUTF();

View File

@ -18,14 +18,15 @@ package org.apache.activemq.util;
import java.io.IOException;
import junit.framework.TestCase;
import org.junit.Test;
public class DataByteArrayOutputStreamTest extends TestCase {
public class DataByteArrayOutputStreamTest {
/**
* This test case assumes that an ArrayIndexOutOfBoundsException will be thrown when the buffer fails to resize
* @throws IOException
*/
@Test
public void testResize() throws IOException {
int initSize = 64;
DataByteArrayOutputStream out = new DataByteArrayOutputStream();

View File

@ -16,10 +16,13 @@
*/
package org.apache.activemq.util;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
public class IdGeneratorTest extends TestCase {
import org.junit.Test;
public class IdGeneratorTest {
@Test
public void testSanitizeHostName() throws Exception {
assertEquals("somehost.lan", IdGenerator.sanitizeHostName("somehost.lan"));
// include a UTF-8 char in the text \u0E08 is a Thai elephant

View File

@ -16,14 +16,14 @@
*/
package org.apache.activemq.util;
import junit.framework.TestCase;
import static org.junit.Assert.assertTrue;
/**
* @author <a href="http://www.christianposta.com/blog">Christian Posta</a>
*/
public class IntrospectionSupportTest extends TestCase {
import org.junit.Test;
public class IntrospectionSupportTest {
private class DummyClass {
class DummyClass {
private boolean trace;
DummyClass(boolean trace) {
@ -39,11 +39,13 @@ public class IntrospectionSupportTest extends TestCase {
}
}
@Test
public void testSetPropertyPrimitiveWithWrapperValue() {
// Wrapper value
Boolean value = Boolean.valueOf(true);
DummyClass dummyClass = new DummyClass(false);
dummyClass.setTrace(false);
// dummy field expects a primitive
IntrospectionSupport.setProperty(dummyClass, "trace", value);

View File

@ -16,37 +16,21 @@
*/
package org.apache.activemq.util;
import static org.junit.Assert.assertEquals;
import java.util.Properties;
import junit.framework.TestCase;
import org.junit.Test;
/**
* @author rajdavies
*/
public class MarshallingSupportTest extends TestCase {
/**
* @throws java.lang.Exception
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
}
/**
* @throws java.lang.Exception
* @see junit.framework.TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
}
public class MarshallingSupportTest {
/**
* Test method for
* {@link org.apache.activemq.util.MarshallingSupport#propertiesToString(java.util.Properties)}.
*
*
* @throws Exception
*/
@Test
public void testPropertiesToString() throws Exception {
Properties props = new Properties();
for (int i = 0; i < 10; i++) {

View File

@ -16,13 +16,15 @@
*/
package org.apache.activemq.util;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
/**
*
*/
public class StopWatchTest extends TestCase {
import org.junit.Test;
public class StopWatchTest {
@Test
public void testStopWatch() throws Exception {
StopWatch watch = new StopWatch();
Thread.sleep(200);
@ -32,6 +34,7 @@ public class StopWatchTest extends TestCase {
assertTrue("Should take approx 200 millis, was: " + taken, taken > 150);
}
@Test
public void testStopWatchNotStarted() throws Exception {
StopWatch watch = new StopWatch(false);
long taken = watch.stop();
@ -45,6 +48,7 @@ public class StopWatchTest extends TestCase {
assertTrue("Should take approx 200 millis, was: " + taken, taken > 150);
}
@Test
public void testStopWatchRestart() throws Exception {
StopWatch watch = new StopWatch();
Thread.sleep(200);
@ -61,6 +65,7 @@ public class StopWatchTest extends TestCase {
assertTrue("Should take approx 100 millis, was: " + taken, taken > 50);
}
@Test
public void testStopWatchTaken() throws Exception {
StopWatch watch = new StopWatch();
Thread.sleep(100);

View File

@ -16,16 +16,17 @@
*/
package org.apache.activemq.util;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
/**
*
*/
public class StringArrayConverterTest extends TestCase {
import org.junit.Test;
public class StringArrayConverterTest {
@Test
public void testConvertToStringArray() throws Exception {
assertEquals(null, StringArrayConverter.convertToStringArray(null));
assertEquals(null, StringArrayConverter.convertToStringArray(""));
assertNull(StringArrayConverter.convertToStringArray(null));
assertNull(StringArrayConverter.convertToStringArray(""));
String[] array = StringArrayConverter.convertToStringArray("foo");
assertEquals(1, array.length);
@ -43,6 +44,7 @@ public class StringArrayConverterTest extends TestCase {
assertEquals("baz", array[2]);
}
@Test
public void testConvertToString() throws Exception {
assertEquals(null, StringArrayConverter.convertToString(null));
assertEquals(null, StringArrayConverter.convertToString(new String[]{}));
@ -51,5 +53,4 @@ public class StringArrayConverterTest extends TestCase {
assertEquals("foo,bar", StringArrayConverter.convertToString(new String[]{"foo", "bar"}));
assertEquals("foo,bar,baz", StringArrayConverter.convertToString(new String[]{"foo", "bar", "baz"}));
}
}

View File

@ -23,20 +23,10 @@ import static org.junit.Assert.assertNull;
import java.util.List;
import org.apache.activemq.command.ActiveMQDestination;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class StringToListOfActiveMQDestinationConverterTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testConvertToActiveMQDestination() {
@ -62,5 +52,4 @@ public class StringToListOfActiveMQDestinationConverterTest {
String result = StringToListOfActiveMQDestinationConverter.convertFromActiveMQDestination(null);
assertNull(result);
}
}

View File

@ -16,22 +16,28 @@
*/
package org.apache.activemq.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
import org.apache.activemq.util.URISupport.CompositeData;
import org.junit.Test;
public class URISupportTest extends TestCase {
public class URISupportTest {
@Test
public void testEmptyCompositePath() throws Exception {
CompositeData data = URISupport.parseComposite(new URI("broker:()/localhost?persistent=false"));
assertEquals(0, data.getComponents().length);
}
@Test
public void testCompositePath() throws Exception {
CompositeData data = URISupport.parseComposite(new URI("test:(path)/path"));
assertEquals("path", data.getPath());
@ -39,17 +45,20 @@ public class URISupportTest extends TestCase {
assertNull(data.getPath());
}
@Test
public void testSimpleComposite() throws Exception {
CompositeData data = URISupport.parseComposite(new URI("test:part1"));
assertEquals(1, data.getComponents().length);
}
@Test
public void testComposite() throws Exception {
URI uri = new URI("test:(part1://host,part2://(sub1://part,sube2:part))");
CompositeData data = URISupport.parseComposite(uri);
assertEquals(2, data.getComponents().length);
}
@Test
public void testEmptyCompositeWithParenthesisInParam() throws Exception {
URI uri = new URI("failover://()?updateURIsURL=file:/C:/Dir(1)/a.csv");
CompositeData data = URISupport.parseComposite(uri);
@ -59,6 +68,7 @@ public class URISupportTest extends TestCase {
assertEquals("file:/C:/Dir(1)/a.csv", data.getParameters().get("updateURIsURL"));
}
@Test
public void testCompositeWithParenthesisInParam() throws Exception {
URI uri = new URI("failover://(test)?updateURIsURL=file:/C:/Dir(1)/a.csv");
CompositeData data = URISupport.parseComposite(uri);
@ -68,6 +78,7 @@ public class URISupportTest extends TestCase {
assertEquals("file:/C:/Dir(1)/a.csv", data.getParameters().get("updateURIsURL"));
}
@Test
public void testCompositeWithComponentParam() throws Exception {
CompositeData data = URISupport.parseComposite(new URI("test:(part1://host?part1=true)?outside=true"));
assertEquals(1, data.getComponents().length);
@ -77,6 +88,7 @@ public class URISupportTest extends TestCase {
assertTrue(part1Params.containsKey("part1"));
}
@Test
public void testParsingURI() throws Exception {
URI source = new URI("tcp://localhost:61626/foo/bar?cheese=Edam&x=123");
@ -95,12 +107,14 @@ public class URISupportTest extends TestCase {
assertEquals("Map key: " + key, map.get(key), expected);
}
@Test
public void testParsingCompositeURI() throws URISyntaxException {
CompositeData data = URISupport.parseComposite(new URI("broker://(tcp://localhost:61616)?name=foo"));
assertEquals("one component", 1, data.getComponents().length);
assertEquals("Size: " + data.getParameters(), 1, data.getParameters().size());
}
@Test
public void testCheckParenthesis() throws Exception {
String str = "fred:(((ddd))";
assertFalse(URISupport.checkParenthesis(str));
@ -108,6 +122,7 @@ public class URISupportTest extends TestCase {
assertTrue(URISupport.checkParenthesis(str));
}
@Test
public void testCreateWithQuery() throws Exception {
URI source = new URI("vm://localhost");
URI dest = URISupport.createURIWithQuery(source, "network=true&one=two");
@ -118,6 +133,7 @@ public class URISupportTest extends TestCase {
assertFalse("same uri, ssp", dest.getQuery().equals(source.getQuery()));
}
@Test
public void testParsingParams() throws Exception {
URI uri = new URI("static:(http://localhost:61617?proxyHost=jo&proxyPort=90)?proxyHost=localhost&proxyPort=80");
Map<String,String>parameters = URISupport.parseParameters(uri);
@ -129,6 +145,7 @@ public class URISupportTest extends TestCase {
parameters = URISupport.parseParameters(uri);
}
@Test
public void testCompositeCreateURIWithQuery() throws Exception {
String queryString = "query=value";
URI originalURI = new URI("outerscheme:(innerscheme:innerssp)");
@ -151,6 +168,7 @@ public class URISupportTest extends TestCase {
assertEquals(new URI(querylessURI + "?" + queryString), URISupport.createURIWithQuery(originalURI, queryString));
}
@Test
public void testApplyParameters() throws Exception {
URI uri = new URI("http://0.0.0.0:61616");
@ -178,6 +196,7 @@ public class URISupportTest extends TestCase {
assertEquals(parameters.get("proxyPort"), "80");
}
@Test
public void testIsCompositeURIWithQueryNoSlashes() throws URISyntaxException {
URI[] compositeURIs = new URI[] { new URI("test:(part1://host?part1=true)?outside=true"), new URI("broker:(tcp://localhost:61616)?name=foo") };
for (URI uri : compositeURIs) {
@ -185,6 +204,7 @@ public class URISupportTest extends TestCase {
}
}
@Test
public void testIsCompositeURIWithQueryAndSlashes() throws URISyntaxException {
URI[] compositeURIs = new URI[] { new URI("test://(part1://host?part1=true)?outside=true"), new URI("broker://(tcp://localhost:61616)?name=foo") };
for (URI uri : compositeURIs) {
@ -192,6 +212,7 @@ public class URISupportTest extends TestCase {
}
}
@Test
public void testIsCompositeURINoQueryNoSlashes() throws URISyntaxException {
URI[] compositeURIs = new URI[] { new URI("test:(part1://host,part2://(sub1://part,sube2:part))"), new URI("test:(path)/path") };
for (URI uri : compositeURIs) {
@ -199,10 +220,12 @@ public class URISupportTest extends TestCase {
}
}
@Test
public void testIsCompositeURINoQueryNoSlashesNoParentheses() throws URISyntaxException {
assertFalse("test:part1" + " must be detected as non-composite URI", URISupport.isCompositeURI(new URI("test:part1")));
}
@Test
public void testIsCompositeURINoQueryWithSlashes() throws URISyntaxException {
URI[] compositeURIs = new URI[] { new URI("failover://(tcp://bla:61616,tcp://bla:61617)"),
new URI("failover://(tcp://localhost:61616,ssl://anotherhost:61617)") };
@ -210,5 +233,4 @@ public class URISupportTest extends TestCase {
assertTrue(uri + " must be detected as composite URI", URISupport.isCompositeURI(uri));
}
}
}