BATCH-&*&: work in progress (some tests disabled)

This commit is contained in:
dsyer
2008-08-19 13:49:38 +00:00
parent cfc9529bab
commit 75fca027cb
39 changed files with 276 additions and 362 deletions

View File

@@ -1,17 +1,20 @@
package org.springframework.batch.item.adapter;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.sample.Foo;
import org.springframework.batch.item.sample.FooService;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.junit.runner.RunWith;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Tests for {@link ItemWriterAdapter}.
@@ -34,9 +37,11 @@ public class ItemWriterAdapterTests {
@Test
public void testProcess() throws Exception {
Foo foo;
List<Foo> foos = new ArrayList<Foo>();
while ((foo = fooService.generateFoo()) != null) {
processor.write(foo);
foos.add(foo);
}
processor.write(foos);
List<Foo> input = fooService.getGeneratedFoos();
List<Foo> processed = fooService.getProcessedFoos();

View File

@@ -4,6 +4,7 @@ import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.junit.Test;
import java.util.Collections;
import java.util.List;
import org.springframework.batch.item.sample.Foo;
@@ -34,7 +35,7 @@ public class PropertyExtractingDelegatingItemProccessorIntegrationTests {
public void testProcess() throws Exception {
Foo foo;
while ((foo = fooService.generateFoo()) != null) {
processor.write(foo);
processor.write(Collections.singletonList(foo));
}
List<Foo> input = fooService.getGeneratedFoos();

View File

@@ -109,12 +109,12 @@ public class BatchSqlUpdateItemWriterTests extends TestCase {
/**
* Test method for
* {@link org.springframework.batch.item.database.BatchSqlUpdateItemWriter#write(java.lang.Object)}.
* {@link org.springframework.batch.item.database.BatchSqlUpdateItemWriter#write(List)}.
* @throws Exception
*/
public void testWrite() throws Exception {
writer.setSql("foo");
writer.write("bar");
writer.write(Collections.singletonList("bar"));
// Nothing happens till we flush
assertEquals(0, list.size());
}
@@ -157,7 +157,7 @@ public class BatchSqlUpdateItemWriterTests extends TestCase {
expectLastCall().times(2);
expect(ps.executeBatch()).andReturn(new int[] { 123 });
replay(ps);
writer.write("bar");
writer.write(Collections.singletonList("bar"));
writer.flush();
assertFalse(TransactionSynchronizationManager.hasResource(writer.getResourceKey()));
assertEquals(3, list.size());
@@ -175,7 +175,7 @@ public class BatchSqlUpdateItemWriterTests extends TestCase {
expectLastCall().times(2);
expect(ps.executeBatch()).andReturn(new int[] {0});
replay(ps);
writer.write("bar");
writer.write(Collections.singletonList("bar"));
try {
writer.flush();
fail("Expected EmptyResultDataAccessException");
@@ -201,7 +201,7 @@ public class BatchSqlUpdateItemWriterTests extends TestCase {
expectLastCall().times(1);
expect(ps.executeBatch()).andReturn(new int[] {123});
replay(ps);
writer.write("foo");
writer.write(Collections.singletonList("foo"));
try {
writer.flush();
fail("Expected RuntimeException");
@@ -216,7 +216,7 @@ public class BatchSqlUpdateItemWriterTests extends TestCase {
list.add(item);
}
});
writer.write("foo");
writer.write(Collections.singletonList("foo"));
writer.flush();
verify(ps);
assertEquals(4, list.size());

View File

@@ -16,6 +16,7 @@
package org.springframework.batch.item.database;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import junit.framework.TestCase;
@@ -45,8 +46,8 @@ public class HibernateAwareItemWriterTests extends TestCase {
}
private class StubItemWriter implements ItemWriter<Object> {
public void write(Object item) {
list.add(item);
public void write(List<? extends Object> items) {
list.addAll(items);
}
public void clear() throws ClearFailedException {
@@ -116,21 +117,12 @@ public class HibernateAwareItemWriterTests extends TestCase {
writer.afterPropertiesSet();
}
/**
* Test method for
* {@link org.springframework.batch.item.database.HibernateAwareItemWriter#write(java.lang.Object)}.
* @throws Exception
*/
public void testWrite() throws Exception {
writer.write("foo");
writer.write(Collections.singletonList("foo"));
assertEquals(1, list.size());
assertTrue(list.contains("foo"));
}
/**
* Test method for
* {@link org.springframework.batch.item.database.HibernateAwareItemWriter#write(java.lang.Object)}.
*/
public void testFlushWithFailure() throws Exception{
final RuntimeException ex = new RuntimeException("bar");
writer.setHibernateTemplate(new HibernateTemplate() {
@@ -146,11 +138,6 @@ public class HibernateAwareItemWriterTests extends TestCase {
}
}
/**
* Test method for
* {@link org.springframework.batch.item.database.HibernateAwareItemWriter#write(java.lang.Object)}.
* @throws Exception
*/
public void testWriteAndFlushWithFailure() throws Exception {
final RuntimeException ex = new RuntimeException("bar");
writer.setHibernateTemplate(new HibernateTemplateWrapper() {
@@ -158,7 +145,7 @@ public class HibernateAwareItemWriterTests extends TestCase {
throw ex;
}
});
writer.write("foo");
writer.write(Collections.singletonList("foo"));
try {
writer.flush();
fail("Expected RuntimeException");
@@ -173,11 +160,10 @@ public class HibernateAwareItemWriterTests extends TestCase {
list.add("flush");
}
});
writer.write("foo");
writer.write(Collections.singletonList("foo"));
assertEquals(6, list.size());
assertTrue(list.contains("flush"));
assertTrue(list.contains("clear"));
assertTrue(list.contains("delegateFlush"));
assertTrue(context.isCompleteOnly());
}

View File

@@ -27,6 +27,8 @@ import org.springframework.transaction.support.TransactionSynchronizationManager
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityManager;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
@@ -80,9 +82,9 @@ public class JpaAwareItemWriterTests {
@Test
public void testWrite() throws Exception {
delegate.write("foo");
delegate.write(Collections.singletonList("foo"));
replay(delegate);
writer.write("foo");
writer.write(Collections.singletonList("foo"));
verify(delegate);
}
@@ -119,13 +121,13 @@ public class JpaAwareItemWriterTests {
replay(em);
replay(emf);
TransactionSynchronizationManager.bindResource(emf, new EntityManagerHolder(em));
delegate.write("foo");
delegate.write(Collections.singletonList("foo"));
delegate.flush();
delegate.write("spam");
delegate.write(Collections.singletonList("spam"));
delegate.flush();
replay(delegate);
writer.write("foo");
writer.write(Collections.singletonList("foo"));
try {
writer.flush();
fail("Expected RuntimeException");
@@ -133,7 +135,7 @@ public class JpaAwareItemWriterTests {
assertEquals("bar", e.getMessage());
}
writer.write("spam");
writer.write(Collections.singletonList("spam"));
writer.flush();
verify(delegate);

View File

@@ -21,6 +21,8 @@ import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Arrays;
import java.util.Collections;
import junit.framework.TestCase;
@@ -106,10 +108,10 @@ public class FlatFileItemWriterTests extends TestCase {
public void testWriteWithMultipleOpen() throws Exception {
writer.open(executionContext);
writer.write("test1");
writer.write(Collections.singletonList("test1"));
writer.flush();
writer.open(executionContext);
writer.write("test2");
writer.write(Collections.singletonList("test2"));
writer.flush();
assertEquals("test1", readLine());
assertEquals("test2", readLine());
@@ -128,7 +130,7 @@ public class FlatFileItemWriterTests extends TestCase {
*/
public void testWriteString() throws Exception {
writer.open(executionContext);
writer.write(TEST_STRING);
writer.write(Collections.singletonList(TEST_STRING));
writer.flush();
writer.close(null);
String lineFromFile = readLine();
@@ -149,7 +151,7 @@ public class FlatFileItemWriterTests extends TestCase {
});
String data = "string";
writer.open(executionContext);
writer.write(data);
writer.write(Collections.singletonList(data));
writer.flush();
String lineFromFile = readLine();
// converter not used if input is String
@@ -168,7 +170,7 @@ public class FlatFileItemWriterTests extends TestCase {
}
});
writer.open(executionContext);
writer.write(TEST_STRING);
writer.write(Collections.singletonList(TEST_STRING));
writer.flush();
String lineFromFile = readLine();
assertEquals("FOO:" + TEST_STRING, lineFromFile);
@@ -180,19 +182,17 @@ public class FlatFileItemWriterTests extends TestCase {
* @throws Exception
*/
public void testWriteRecord() throws Exception {
String args = "1";
writer.open(executionContext);
writer.write(args);
writer.write(Collections.singletonList("1"));
writer.flush();
String lineFromFile = readLine();
assertEquals(args, lineFromFile);
assertEquals("1", lineFromFile);
}
public void testWriteRecordWithrecordSeparator() throws Exception {
writer.setLineSeparator("|");
writer.open(executionContext);
writer.write("1");
writer.write("2");
writer.write(Arrays.asList(new String[] { "1", "2" }));
writer.flush();
String lineFromFile = readLine();
assertEquals("1|2|", lineFromFile);
@@ -200,7 +200,7 @@ public class FlatFileItemWriterTests extends TestCase {
public void testRollback() throws Exception {
writer.open(executionContext);
writer.write("testLine1");
writer.write(Collections.singletonList("testLine1"));
// rollback
rollback();
writer.flush();
@@ -211,7 +211,7 @@ public class FlatFileItemWriterTests extends TestCase {
public void testCommit() throws Exception {
writer.open(executionContext);
writer.write("testLine1");
writer.write(Collections.singletonList("testLine1"));
// rollback
commit();
writer.close(null);
@@ -224,22 +224,19 @@ public class FlatFileItemWriterTests extends TestCase {
writer.open(executionContext);
// write some lines
writer.write("testLine1");
writer.write("testLine2");
writer.write("testLine3");
writer.write(Arrays.asList(new String[] { "testLine1", "testLine2", "testLine3" }));
// commit
commit();
// this will be rolled back...
writer.write("this will be rolled back");
writer.write(Collections.singletonList("this will be rolled back"));
// rollback
rollback();
// write more lines
writer.write("testLine4");
writer.write("testLine5");
writer.write(Arrays.asList(new String[] {"testLine4", "testLine5"}));
// commit
commit();
@@ -253,9 +250,7 @@ public class FlatFileItemWriterTests extends TestCase {
writer.open(executionContext);
// write more lines
writer.write("testLine6");
writer.write("testLine7");
writer.write("testLine8");
writer.write(Arrays.asList(new String[] {"testLine6","testLine7","testLine8"}));
commit();
@@ -344,7 +339,7 @@ public class FlatFileItemWriterTests extends TestCase {
testWriteStringWithBogusEncoding();
writer.setEncoding("UTF-8");
writer.open(executionContext);
writer.write(TEST_STRING);
writer.write(Collections.singletonList(TEST_STRING));
writer.flush();
String lineFromFile = readLine();
@@ -354,7 +349,7 @@ public class FlatFileItemWriterTests extends TestCase {
public void testWriteHeader() throws Exception {
writer.setHeaderLines(new String[] { "a", "b" });
writer.open(executionContext);
writer.write(TEST_STRING);
writer.write(Collections.singletonList(TEST_STRING));
writer.flush();
writer.close(null);
String lineFromFile = readLine();
@@ -368,11 +363,11 @@ public class FlatFileItemWriterTests extends TestCase {
public void testWriteHeaderAfterRestartOnFirstChunk() throws Exception {
writer.setHeaderLines(new String[] { "a", "b" });
writer.open(executionContext);
writer.write(TEST_STRING);
writer.write(Collections.singletonList(TEST_STRING));
writer.clear();
writer.close(executionContext);
writer.open(executionContext);
writer.write(TEST_STRING);
writer.write(Collections.singletonList(TEST_STRING));
writer.flush();
writer.close(executionContext);
String lineFromFile = readLine();
@@ -388,10 +383,10 @@ public class FlatFileItemWriterTests extends TestCase {
public void testWriteHeaderAfterRestartOnSecondChunk() throws Exception {
writer.setHeaderLines(new String[] { "a", "b" });
writer.open(executionContext);
writer.write(TEST_STRING);
writer.write(Collections.singletonList(TEST_STRING));
writer.flush();
writer.update(executionContext);
writer.write(TEST_STRING);
writer.write(Collections.singletonList(TEST_STRING));
writer.clear();
writer.close(executionContext);
String lineFromFile = readLine();
@@ -401,7 +396,7 @@ public class FlatFileItemWriterTests extends TestCase {
lineFromFile = readLine();
assertEquals(TEST_STRING, lineFromFile);
writer.open(executionContext);
writer.write(TEST_STRING);
writer.write(Collections.singletonList(TEST_STRING));
writer.flush();
writer.close(executionContext);
reader = null;

View File

@@ -4,6 +4,10 @@ import static org.easymock.EasyMock.createStrictMock;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import java.util.Collections;
import java.util.List;
import junit.framework.TestCase;
import org.springframework.batch.item.ItemWriter;
@@ -26,7 +30,7 @@ public class CompositeItemWriterTests extends TestCase {
public void testProcess() throws Exception {
final int NUMBER_OF_WRITERS = 10;
Object data = new Object();
List<Object> data = Collections.singletonList(new Object());
@SuppressWarnings("unchecked")
ItemWriter<Object>[] writers = new ItemWriter[NUMBER_OF_WRITERS];

View File

@@ -11,7 +11,10 @@ import static org.junit.Assert.fail;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLStreamException;
@@ -50,6 +53,8 @@ public class StaxEventItemWriterTests {
}
};
private List<? extends Object> items = Collections.singletonList(item);
private static final String TEST_STRING = "<!--" + ClassUtils.getShortName(StaxEventItemWriter.class)
+ "-testString-->";
@@ -73,7 +78,7 @@ public class StaxEventItemWriterTests {
writer.setSerializer(serializer);
// see asserts in the marshaller
writer.write(item);
writer.write(items);
assertFalse(marshaller.wasCalled);
writer.flush();
@@ -84,8 +89,7 @@ public class StaxEventItemWriterTests {
@Test
public void testClear() throws Exception {
writer.open(executionContext);
writer.write(item);
writer.write(item);
writer.write(Arrays.asList(new Object[] {item, item}));
writer.clear();
// writer.write(item);
writer.flush();
@@ -98,7 +102,7 @@ public class StaxEventItemWriterTests {
@Test
public void testRollback() throws Exception {
writer.open(executionContext);
writer.write(item);
writer.write(items);
// rollback
writer.clear();
assertFalse(outputFileContent().contains(TEST_STRING));
@@ -110,7 +114,7 @@ public class StaxEventItemWriterTests {
@Test
public void testWriteAndFlush() throws Exception {
writer.open(executionContext);
writer.write(item);
writer.write(items);
String content = outputFileContent();
assertFalse(content.contains(TEST_STRING));
writer.flush();
@@ -125,7 +129,7 @@ public class StaxEventItemWriterTests {
public void testRestart() throws Exception {
writer.open(executionContext);
// write item
writer.write(item);
writer.write(items);
writer.flush();
writer.update(executionContext);
writer.close(executionContext);
@@ -133,7 +137,7 @@ public class StaxEventItemWriterTests {
// create new writer from saved restart data and continue writing
writer = createItemWriter();
writer.open(executionContext);
writer.write(item);
writer.write(items);
writer.close(executionContext);
// check the output is concatenation of 'before restart' and 'after
@@ -158,7 +162,7 @@ public class StaxEventItemWriterTests {
Object header2 = new Object();
writer.setHeaderItems(new Object[] {header1, header2});
writer.open(executionContext);
writer.write(item);
writer.write(items);
writer.flush();
String content = outputFileContent();
assertTrue("Wrong content: "+content, contains(content, "<!--" + header1 + "-->"));
@@ -174,10 +178,10 @@ public class StaxEventItemWriterTests {
Object header = new Object();
writer.setHeaderItems(new Object[] {header});
writer.open(executionContext);
writer.write(item);
writer.write(items);
writer.clear();
writer.open(executionContext);
writer.write(item);
writer.write(items);
writer.flush();
String content = outputFileContent();
assertEquals("Wrong content: "+content, 1, countContains(content, "<!--" + header + "-->"));
@@ -192,12 +196,12 @@ public class StaxEventItemWriterTests {
Object header = new Object();
writer.setHeaderItems(new Object[] {header});
writer.open(executionContext);
writer.write(item);
writer.write(items);
writer.flush();
writer.update(executionContext);
writer.close(executionContext);
writer.open(executionContext);
writer.write(item);
writer.write(items);
writer.clear();
writer.flush();
String content = outputFileContent();
@@ -212,8 +216,10 @@ public class StaxEventItemWriterTests {
public void testStreamContext() throws Exception {
writer.open(executionContext);
final int NUMBER_OF_RECORDS = 10;
assertFalse(executionContext.containsKey(ClassUtils.getShortName(StaxEventItemWriter.class)
+ ".record.count"));
for (int i = 1; i <= NUMBER_OF_RECORDS; i++) {
writer.write(item);
writer.write(items);
writer.update(executionContext);
long writeStatistics = executionContext.getLong(ClassUtils.getShortName(StaxEventItemWriter.class)
+ ".record.count");

View File

@@ -1,54 +0,0 @@
/*
* Copyright 2006-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.repeat.callback;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import junit.framework.TestCase;
import org.springframework.batch.item.support.AbstractItemWriter;
import org.springframework.batch.item.support.ListItemReader;
public class ItemReaderRepeatCallbackTests extends TestCase {
ItemReaderRepeatCallback<String> callback;
List<Object> list = new ArrayList<Object>();
public void testDoWithRepeat() throws Exception {
callback = new ItemReaderRepeatCallback<String>(new ListItemReader<String>(Arrays.asList(new String[] { "foo", "bar" })),
new AbstractItemWriter<String>() {
public void write(String data) {
list.add(data);
}
});
callback.doInIteration(null);
assertEquals(1, list.size());
assertEquals("foo", list.get(0));
}
public void testDoWithRepeatNullProcessor() throws Exception {
ListItemReader<String> provider = new ListItemReader<String>(Arrays.asList(new String[] { "foo", "bar" }));
callback = new ItemReaderRepeatCallback<String>(provider);
callback.doInIteration(null);
assertEquals(0, list.size());
assertEquals("bar", provider.read());
}
}

View File

@@ -16,6 +16,8 @@
package org.springframework.batch.repeat.support;
import java.util.List;
import junit.framework.TestCase;
import org.springframework.batch.item.ExecutionContext;
@@ -70,7 +72,7 @@ public abstract class AbstractTradeBatchTests extends TestCase {
// This has to be synchronized because we are going to test the state
// (count) at the end of a concurrent batch run.
public synchronized void write(Trade data) {
public synchronized void write(List<? extends Trade> data) {
count++;
System.out.println("Executing trade '" + data + "'");
}

View File

@@ -16,13 +16,13 @@
package org.springframework.batch.repeat.support;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.repeat.RepeatCallback;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.callback.ItemReaderRepeatCallback;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
public class AsynchronousRepeatTests extends AbstractTradeBatchTests {
@@ -48,7 +48,7 @@ public class AsynchronousRepeatTests extends AbstractTradeBatchTests {
Thread.sleep(100);
Trade item = provider.read();
if (item!=null) {
processor.write(item);
processor.write(Collections.singletonList(item));
}
return new ExitStatus(item!=null);
}

View File

@@ -21,7 +21,6 @@ import org.springframework.batch.item.support.AbstractItemReader;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.repeat.RepeatCallback;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.callback.ItemReaderRepeatCallback;
import org.springframework.batch.repeat.callback.NestedRepeatCallback;
import org.springframework.batch.repeat.policy.SimpleCompletionPolicy;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

View File

@@ -24,7 +24,6 @@ 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.callback.ItemReaderRepeatCallback;
import org.springframework.batch.repeat.callback.NestedRepeatCallback;
import org.springframework.batch.repeat.context.RepeatContextSupport;
import org.springframework.batch.repeat.exception.ExceptionHandler;