Convert some old tests to JUnit 4

This commit is contained in:
dsyer
2008-11-10 10:09:25 +00:00
parent d85b64836b
commit fe42d53fc5
5 changed files with 84 additions and 10 deletions

View File

@@ -18,8 +18,7 @@ package org.springframework.batch.repeat.support;
import java.util.List;
import junit.framework.TestCase;
import org.junit.Before;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
@@ -36,7 +35,7 @@ import org.springframework.core.io.Resource;
* @author Dave Syer
*
*/
public abstract class AbstractTradeBatchTests extends TestCase {
public abstract class AbstractTradeBatchTests {
public static final int NUMBER_OF_ITEMS = 5;
@@ -46,8 +45,8 @@ public abstract class AbstractTradeBatchTests extends TestCase {
protected TradeItemReader provider;
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
provider = new TradeItemReader(resource);
provider.open(new ExecutionContext());
}

View File

@@ -16,25 +16,32 @@
package org.springframework.batch.repeat.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.springframework.batch.repeat.RepeatStatus;
import org.junit.Test;
import org.springframework.batch.repeat.RepeatCallback;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
public class AsynchronousRepeatTests extends AbstractTradeBatchTests {
/**
* Run a batch with a single template that itself has an asynch task
* Run a batch with a single template that itself has an async task
* executor. The result is a batch that runs in multiple threads (up to the
* throttle limit of the template).
*
* @throws Exception
*/
@Test
public void testMultiThreadAsynchronousExecution() throws Exception {
TaskExecutorRepeatTemplate template = new TaskExecutorRepeatTemplate();
template.setTaskExecutor(new SimpleAsyncTaskExecutor());
@@ -60,6 +67,36 @@ public class AsynchronousRepeatTests extends AbstractTradeBatchTests {
assertEquals(NUMBER_OF_ITEMS, processor.count);
assertTrue(threadNames.size() > 1);
}
@Test
public void testThrottleLimit() throws Exception {
TaskExecutorRepeatTemplate template = new TaskExecutorRepeatTemplate();
SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
taskExecutor.setConcurrencyLimit(3);
template.setTaskExecutor(taskExecutor);
template.setThrottleLimit(12);
final String threadName = Thread.currentThread().getName();
final Set<String> threadNames = new HashSet<String>();
final RepeatCallback callback = new RepeatCallback() {
public RepeatStatus doInIteration(RepeatContext context) throws Exception {
assertNotSame(threadName, Thread.currentThread().getName());
threadNames.add(Thread.currentThread().getName());
Trade item = provider.read();
if (item!=null) {
processor.write(Collections.singletonList(item));
}
return RepeatStatus.continueIf(item!=null);
}
};
template.iterate(callback);
// Shouldn't be necessary to wait:
// Thread.sleep(500);
assertEquals(NUMBER_OF_ITEMS, processor.count);
assertTrue(threadNames.size() > 1);
}
/**
* Wrap an otherwise synchronous batch in a callback to an asynchronous
@@ -67,6 +104,7 @@ public class AsynchronousRepeatTests extends AbstractTradeBatchTests {
*
* @throws Exception
*/
@Test
public void testSingleThreadAsynchronousExecution() throws Exception {
TaskExecutorRepeatTemplate jobTemplate = new TaskExecutorRepeatTemplate();
final RepeatTemplate stepTemplate = new RepeatTemplate();
@@ -103,6 +141,6 @@ public class AsynchronousRepeatTests extends AbstractTradeBatchTests {
assertTrue(threadNames.size() >= 1);
}
// TODO: test transactional callback with asynch template.
// TODO: test transactional callback with async template.
}

View File

@@ -16,10 +16,15 @@
package org.springframework.batch.repeat.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.batch.repeat.RepeatCallback;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.batch.repeat.callback.NestedRepeatCallback;
import org.springframework.batch.repeat.policy.SimpleCompletionPolicy;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
@@ -41,6 +46,7 @@ public class ChunkedRepeatTests extends AbstractTradeBatchTests {
*
* @throws Exception
*/
@Test
public void testChunkedBatchWithTerminationPolicy() throws Exception {
RepeatTemplate repeatTemplate = new RepeatTemplate();
@@ -75,6 +81,7 @@ public class ChunkedRepeatTests extends AbstractTradeBatchTests {
*
* @throws Exception
*/
@Test
public void testAsynchronousChunkedBatchWithCompletionPolicy() throws Exception {
RepeatTemplate repeatTemplate = new RepeatTemplate();
@@ -107,6 +114,7 @@ public class ChunkedRepeatTests extends AbstractTradeBatchTests {
*
* @throws Exception
*/
@Test
public void testChunksWithTruncatedItemProvider() throws Exception {
RepeatTemplate template = new RepeatTemplate();

View File

@@ -16,14 +16,23 @@
package org.springframework.batch.repeat.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.List;
import org.springframework.batch.repeat.RepeatStatus;
import org.junit.Test;
import org.springframework.batch.repeat.RepeatCallback;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.RepeatException;
import org.springframework.batch.repeat.RepeatListener;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.batch.repeat.callback.NestedRepeatCallback;
import org.springframework.batch.repeat.context.RepeatContextSupport;
import org.springframework.batch.repeat.exception.ExceptionHandler;
@@ -44,6 +53,7 @@ public class SimpleRepeatTemplateTests extends AbstractTradeBatchTests {
return new RepeatTemplate();
}
@Test
public void testExecute() throws Exception {
template.iterate(new ItemReaderRepeatCallback<Trade>(provider, processor));
assertEquals(NUMBER_OF_ITEMS, processor.count);
@@ -54,6 +64,7 @@ public class SimpleRepeatTemplateTests extends AbstractTradeBatchTests {
*
* @throws Exception
*/
@Test
public void testEarlyCompletionWithPolicy() throws Exception {
template.setCompletionPolicy(new SimpleCompletionPolicy(2));
@@ -69,6 +80,7 @@ public class SimpleRepeatTemplateTests extends AbstractTradeBatchTests {
*
* @throws Exception
*/
@Test
public void testEarlyCompletionWithException() throws Exception {
try {
@@ -93,6 +105,7 @@ public class SimpleRepeatTemplateTests extends AbstractTradeBatchTests {
*
* @throws Exception
*/
@Test
public void testContextClosedOnNormalCompletion() throws Exception {
final List<String> list = new ArrayList<String>();
@@ -125,6 +138,7 @@ public class SimpleRepeatTemplateTests extends AbstractTradeBatchTests {
*
* @throws Exception
*/
@Test
public void testContextClosedOnAbnormalCompletion() throws Exception {
final List<String> list = new ArrayList<String>();
@@ -163,6 +177,7 @@ public class SimpleRepeatTemplateTests extends AbstractTradeBatchTests {
*
* @throws Exception
*/
@Test
public void testExceptionHandlerCalledOnAbnormalCompletion() throws Exception {
final List<Throwable> list = new ArrayList<Throwable>();
@@ -196,6 +211,7 @@ public class SimpleRepeatTemplateTests extends AbstractTradeBatchTests {
*
* @throws Exception
*/
@Test
public void testEarlyCompletionWithContext() throws Exception {
RepeatStatus result = template.iterate(new ItemReaderRepeatCallback<Trade>(provider, processor) {
@@ -224,6 +240,7 @@ public class SimpleRepeatTemplateTests extends AbstractTradeBatchTests {
*
* @throws Exception
*/
@Test
public void testEarlyCompletionWithContextTerminated() throws Exception {
RepeatStatus result = template.iterate(new ItemReaderRepeatCallback<Trade>(provider, processor) {
@@ -247,6 +264,7 @@ public class SimpleRepeatTemplateTests extends AbstractTradeBatchTests {
}
@Test
public void testNestedSession() throws Exception {
RepeatTemplate outer = getRepeatTemplate();
RepeatTemplate inner = getRepeatTemplate();
@@ -268,6 +286,7 @@ public class SimpleRepeatTemplateTests extends AbstractTradeBatchTests {
assertEquals(2, count);
}
@Test
public void testNestedSessionTerminatesBeforeIteration() throws Exception {
RepeatTemplate outer = getRepeatTemplate();
RepeatTemplate inner = getRepeatTemplate();
@@ -288,6 +307,7 @@ public class SimpleRepeatTemplateTests extends AbstractTradeBatchTests {
assertEquals(1, count);
}
@Test
public void testOuterContextPreserved() throws Exception {
RepeatTemplate outer = getRepeatTemplate();
outer.setCompletionPolicy(new SimpleCompletionPolicy(2));
@@ -315,6 +335,7 @@ public class SimpleRepeatTemplateTests extends AbstractTradeBatchTests {
* Test that a result is returned from the batch.
* @throws Exception
*/
@Test
public void testResult() throws Exception {
RepeatStatus result = template.iterate(new ItemReaderRepeatCallback<Trade>(provider, processor));
assertEquals(NUMBER_OF_ITEMS, processor.count);
@@ -322,6 +343,7 @@ public class SimpleRepeatTemplateTests extends AbstractTradeBatchTests {
assertFalse(result.isContinuable());
}
@Test
public void testExceptionThrownOnLastItem() throws Exception {
template.setCompletionPolicy(new SimpleCompletionPolicy(2));
try {
@@ -348,6 +370,7 @@ public class SimpleRepeatTemplateTests extends AbstractTradeBatchTests {
*
* @throws Exception
*/
@Test
public void testEarlyCompletionWithSessionAndException() throws Exception {
template.setCompletionPolicy(new SimpleCompletionPolicy(4));
@@ -388,6 +411,7 @@ public class SimpleRepeatTemplateTests extends AbstractTradeBatchTests {
* RepeatException should be unwrapped before before it is passed to
* listeners and exception handler.
*/
@Test
public void testExceptionUnwrapping() {
class TestException extends Exception {

View File

@@ -16,6 +16,10 @@
package org.springframework.batch.repeat.support;
import static org.junit.Assert.fail;
import org.junit.Test;
/**
* @author Dave Syer
@@ -26,6 +30,7 @@ public class TaskExecutorRepeatTemplateTests extends SimpleRepeatTemplateTests {
return new TaskExecutorRepeatTemplate();
}
@Test
public void testSetThrottleLimit() throws Exception {
try {
new TaskExecutorRepeatTemplate().setThrottleLimit(-1);