OPEN - issue BATCH-787: Add write(List) to ItemWriter
Make samples and cli archetype compile again
This commit is contained in:
@@ -16,6 +16,8 @@
|
||||
package org.springframework.batch.sample.common;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
@@ -42,39 +44,37 @@ public class CustomItemWriterTests extends TestCase {
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
public void testFlush() throws Exception{
|
||||
|
||||
|
||||
public void testFlush() throws Exception {
|
||||
|
||||
CustomItemWriter<String> itemWriter = new CustomItemWriter<String>();
|
||||
itemWriter.write("1");
|
||||
itemWriter.write(Collections.singletonList("1"));
|
||||
assertEquals(0, itemWriter.getOutput().size());
|
||||
itemWriter.flush();
|
||||
assertEquals(1, itemWriter.getOutput().size());
|
||||
itemWriter.write("2");
|
||||
itemWriter.write("3");
|
||||
itemWriter.write(Arrays.asList(new String[] {"2","3"}));
|
||||
itemWriter.clear();
|
||||
assertEquals(1, itemWriter.getOutput().size());
|
||||
}
|
||||
|
||||
public class CustomItemWriter<T> implements ItemWriter<T>{
|
||||
|
||||
public class CustomItemWriter<T> implements ItemWriter<T> {
|
||||
|
||||
List<T> output = new ArrayList<T>();
|
||||
|
||||
List<T> buffer = new ArrayList<T>();
|
||||
|
||||
public void write(T item) throws Exception {
|
||||
buffer.add(item);
|
||||
public void write(List<? extends T> items) throws Exception {
|
||||
buffer.addAll(items);
|
||||
}
|
||||
|
||||
|
||||
public void clear() throws ClearFailedException {
|
||||
buffer.clear();
|
||||
}
|
||||
|
||||
public void flush() throws FlushFailedException {
|
||||
for(T t:buffer){
|
||||
output.add(t);
|
||||
}
|
||||
output.addAll(buffer);
|
||||
}
|
||||
|
||||
|
||||
public List<T> getOutput() {
|
||||
return output;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package org.springframework.batch.sample.common;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.After;
|
||||
@@ -53,10 +55,7 @@ public class StagingItemReaderTests {
|
||||
new JobParameters(), "testJob")));
|
||||
reader.beforeStep(stepExecution);
|
||||
writer.beforeStep(stepExecution);
|
||||
writer.write("FOO");
|
||||
writer.write("BAR");
|
||||
writer.write("SPAM");
|
||||
writer.write("BUCKET");
|
||||
writer.write(Arrays.asList(new String[] {"FOO","BAR","SPAM","BUCKET"}));
|
||||
reader.open(new ExecutionContext());
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@ package org.springframework.batch.sample.common;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -57,7 +59,7 @@ public class StagingItemWriterTests {
|
||||
@Test
|
||||
public void testProcessInsertsNewItem() throws Exception {
|
||||
int before = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) from BATCH_STAGING");
|
||||
writer.write("FOO");
|
||||
writer.write(Collections.singletonList("FOO"));
|
||||
int after = simpleJdbcTemplate.queryForInt("SELECT COUNT(*) from BATCH_STAGING");
|
||||
assertEquals(before + 1, after);
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@@ -79,7 +80,7 @@ public class JdbcGameDaoIntegrationTests {
|
||||
@Transactional @Test
|
||||
public void testWrite() {
|
||||
|
||||
gameDao.write(game);
|
||||
gameDao.write(Collections.singletonList(game));
|
||||
|
||||
Game tempGame = simpleJdbcTemplate.queryForObject("SELECT * FROM GAMES where PLAYER_ID=? AND YEAR_NO=?",
|
||||
new GameRowMapper(), "XXXXX00 ", game.getYear());
|
||||
|
||||
@@ -17,6 +17,8 @@ package org.springframework.batch.sample.domain.football.internal;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -29,13 +31,12 @@ import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = {"/data-source-context.xml"})
|
||||
@ContextConfiguration(locations = { "/data-source-context.xml" })
|
||||
public class JdbcPlayerSummaryDaoIntegrationTests {
|
||||
|
||||
private JdbcPlayerSummaryDao playerSummaryDao;
|
||||
@@ -73,17 +74,18 @@ public class JdbcPlayerSummaryDaoIntegrationTests {
|
||||
simpleJdbcTemplate.getJdbcOperations().execute("delete from PLAYER_SUMMARY");
|
||||
|
||||
}
|
||||
|
||||
@Transactional @Test
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
public void testWrite() {
|
||||
|
||||
playerSummaryDao.write(summary);
|
||||
playerSummaryDao.write(Collections.singletonList(summary));
|
||||
|
||||
PlayerSummary testSummary = simpleJdbcTemplate.queryForObject("SELECT * FROM PLAYER_SUMMARY",
|
||||
new PlayerSummaryMapper());
|
||||
new PlayerSummaryMapper());
|
||||
|
||||
assertEquals(summary, testSummary);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.springframework.batch.sample.domain.trade.internal;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
@@ -40,7 +41,7 @@ public class CustomerCreditIncreaseProcessorTests {
|
||||
|
||||
customerCredit.setCredit(oldCredit);
|
||||
|
||||
writer.write(customerCredit);
|
||||
writer.write(Collections.singletonList(customerCredit));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.springframework.batch.sample.domain.trade.internal;
|
||||
import static org.easymock.EasyMock.*;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -35,7 +36,7 @@ public class CustomerCreditUpdateProcessorTests {
|
||||
CustomerCredit credit = new CustomerCredit();
|
||||
credit.setCredit(new BigDecimal(CREDIT_FILTER));
|
||||
//call tested method
|
||||
writer.write(credit);
|
||||
writer.write(Collections.singletonList(credit));
|
||||
//verify method calls - no method should be called
|
||||
//because credit is not greater then credit filter
|
||||
verify(dao);
|
||||
@@ -48,7 +49,7 @@ public class CustomerCreditUpdateProcessorTests {
|
||||
replay(dao);
|
||||
|
||||
//call tested method
|
||||
writer.write(credit);
|
||||
writer.write(Collections.singletonList(credit));
|
||||
|
||||
//verify method calls
|
||||
verify(dao);
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.springframework.batch.sample.domain.trade.internal;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerDebit;
|
||||
@@ -32,6 +33,6 @@ public class CustomerUpdateProcessorTests {
|
||||
processor.setDao(dao);
|
||||
|
||||
//call tested method - see asserts in dao.write() method
|
||||
processor.write(trade);
|
||||
processor.write(Collections.singletonList(trade));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -84,7 +85,7 @@ public class FlatFileCustomerCreditDaoTests {
|
||||
writer.setSeparator(";");
|
||||
|
||||
//set-up OutputSource mock
|
||||
output.write("testName;1");
|
||||
output.write(Collections.singletonList("testName;1"));
|
||||
output.open(new ExecutionContext());
|
||||
replay(output);
|
||||
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package org.springframework.batch.sample.domain.trade.internal;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -32,7 +36,7 @@ public class TradeProcessorTests {
|
||||
replay(writer);
|
||||
|
||||
//call tested method
|
||||
processor.write(trade);
|
||||
processor.write(Collections.singletonList(trade));
|
||||
|
||||
//verify method calls
|
||||
verify(writer);
|
||||
|
||||
@@ -3,6 +3,9 @@ package org.springframework.batch.sample.support;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
@@ -20,20 +23,18 @@ public class RetrySampleItemWriterTests {
|
||||
@Test
|
||||
public void testProcess() throws Exception {
|
||||
Object item = null;
|
||||
processor.write(item);
|
||||
processor.write(Collections.singletonList(item));
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
try {
|
||||
processor.write(item);
|
||||
fail();
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
processor.write(Arrays.asList(new Object[] { item, item, item }));
|
||||
fail();
|
||||
}
|
||||
|
||||
processor.write(item);
|
||||
|
||||
catch (RuntimeException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
processor.write(Collections.singletonList(item));
|
||||
|
||||
assertEquals(4, processor.getCounter());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user