BATCH-365: Added flush() and clear() to ItemWriter interface. These should be removed from ItemStream after the same is done for the ItemReader interface.
This commit is contained in:
@@ -24,7 +24,6 @@ import java.util.Collections;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.item.ItemStream;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.writer.ItemTransformer;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
@@ -302,12 +301,12 @@ public class FlatFileItemWriterTests extends TestCase {
|
||||
assertEquals(0, streamContext.getLong(FlatFileItemWriter.RESTART_DATA_NAME));
|
||||
}
|
||||
|
||||
private void commit() {
|
||||
((ItemStream) inputSource).mark();
|
||||
private void commit() throws Exception{
|
||||
inputSource.flush();
|
||||
}
|
||||
|
||||
private void rollback() {
|
||||
((ItemStream) inputSource).reset();
|
||||
private void rollback() throws Exception{
|
||||
inputSource.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -73,6 +73,14 @@ public class HibernateAwareItemWriterTests extends TestCase {
|
||||
|
||||
public void close() throws Exception {
|
||||
}
|
||||
|
||||
public void clear() throws Exception {
|
||||
list.add("clear");
|
||||
}
|
||||
|
||||
public void flush() throws Exception {
|
||||
list.add("flush");
|
||||
}
|
||||
}
|
||||
|
||||
HibernateAwareItemWriter writer = new HibernateAwareItemWriter();
|
||||
@@ -148,7 +156,7 @@ public class HibernateAwareItemWriterTests extends TestCase {
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.io.support.HibernateAwareItemWriter#write(java.lang.Object)}.
|
||||
*/
|
||||
public void testCloseWithFailure() {
|
||||
public void testCloseWithFailure() throws Exception{
|
||||
final RuntimeException ex = new RuntimeException("bar");
|
||||
writer.setHibernateTemplate(new HibernateTemplate() {
|
||||
public void flush() throws DataAccessException {
|
||||
@@ -156,14 +164,13 @@ public class HibernateAwareItemWriterTests extends TestCase {
|
||||
}
|
||||
});
|
||||
try {
|
||||
writer.close(context);
|
||||
writer.flush();
|
||||
fail("Expected RuntimeException");
|
||||
} catch (RuntimeException e) {
|
||||
assertEquals("bar", e.getMessage());
|
||||
}
|
||||
assertEquals(2, list.size());
|
||||
assertTrue(list.contains(ex));
|
||||
assertTrue(list.contains(context));
|
||||
assertEquals(1, list.size());
|
||||
assertTrue(list.contains("flush"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -180,14 +187,13 @@ public class HibernateAwareItemWriterTests extends TestCase {
|
||||
});
|
||||
writer.write("foo");
|
||||
try {
|
||||
writer.close(context);
|
||||
writer.flush();
|
||||
fail("Expected RuntimeException");
|
||||
} catch (RuntimeException e) {
|
||||
assertEquals("bar", e.getMessage());
|
||||
}
|
||||
assertEquals(3, list.size());
|
||||
assertTrue(list.contains(ex));
|
||||
assertTrue(list.contains(context));
|
||||
assertEquals(2, list.size());
|
||||
assertTrue(list.contains("flush"));
|
||||
writer.setHibernateTemplate(new HibernateTemplateWrapper() {
|
||||
public void flush() throws DataAccessException {
|
||||
list.add("flush");
|
||||
@@ -221,8 +227,8 @@ public class HibernateAwareItemWriterTests extends TestCase {
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.io.support.HibernateAwareItemWriter#close(org.springframework.batch.repeat.RepeatContext)}.
|
||||
*/
|
||||
public void testClose() {
|
||||
writer.close(context);
|
||||
public void testFlush() throws Exception{
|
||||
writer.flush();
|
||||
assertEquals(3, list.size());
|
||||
assertTrue(list.contains("flush"));
|
||||
}
|
||||
@@ -231,11 +237,11 @@ public class HibernateAwareItemWriterTests extends TestCase {
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.io.support.HibernateAwareItemWriter#close(org.springframework.batch.repeat.RepeatContext)}.
|
||||
*/
|
||||
public void testCloseAfterClear() {
|
||||
public void testCloseAfterClear() throws Exception{
|
||||
Map map = TransactionSynchronizationManager.getResourceMap();
|
||||
String key = (String) map.keySet().iterator().next();
|
||||
TransactionSynchronizationManager.unbindResource(key);
|
||||
writer.close(context);
|
||||
writer.flush();
|
||||
assertEquals(3, list.size());
|
||||
assertTrue(list.contains("flush"));
|
||||
assertTrue(list.contains("clear"));
|
||||
|
||||
@@ -23,7 +23,7 @@ import org.springframework.xml.transform.StaxResult;
|
||||
/**
|
||||
* Tests for {@link StaxStreamWriterOutputSource}.
|
||||
*/
|
||||
public class StaxEventWriterItemWriterTests extends TestCase {
|
||||
public class StaxEventItemWriterTests extends TestCase {
|
||||
|
||||
// object under test
|
||||
private StaxEventItemWriter writer;
|
||||
@@ -67,7 +67,7 @@ public class StaxEventWriterItemWriterTests extends TestCase {
|
||||
public void testRollback() throws Exception {
|
||||
writer.write(record);
|
||||
// rollback
|
||||
writer.reset();
|
||||
writer.clear();
|
||||
assertEquals("", outputFileContent());
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ public class StaxEventWriterItemWriterTests extends TestCase {
|
||||
public void testCommit() throws Exception {
|
||||
writer.write(record);
|
||||
// commit
|
||||
writer.mark();
|
||||
writer.flush();
|
||||
assertTrue(outputFileContent().contains(TEST_STRING));
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ public class StaxEventWriterItemWriterTests extends TestCase {
|
||||
/**
|
||||
* Open method writes the root tag, close method adds corresponding end tag.
|
||||
*/
|
||||
public void testOpenAndClose() throws IOException {
|
||||
public void testOpenAndClose() throws Exception {
|
||||
writer.setRootTagName("testroot");
|
||||
writer.setRootElementAttributes(new HashMap() {
|
||||
{
|
||||
@@ -134,7 +134,7 @@ public class StaxEventWriterItemWriterTests extends TestCase {
|
||||
}
|
||||
});
|
||||
writer.open();
|
||||
writer.mark();
|
||||
writer.flush();
|
||||
|
||||
assertTrue(outputFileContent().indexOf("<testroot attribute=\"value\"") != NOT_FOUND);
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright 2006-2008 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.item.writer;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class DelegatingItemWriterTests extends TestCase {
|
||||
|
||||
MockControl writerControl = MockControl.createControl(ItemWriter.class);
|
||||
ItemWriter itemWriter;
|
||||
DelegatingItemWriter delegatingWriter;
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see junit.framework.TestCase#setUp()
|
||||
*/
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
itemWriter = (ItemWriter)writerControl.getMock();
|
||||
delegatingWriter = new DelegatingItemWriter();
|
||||
delegatingWriter.setDelegate(itemWriter);
|
||||
}
|
||||
|
||||
public void testFlush() throws Exception{
|
||||
itemWriter.flush();
|
||||
writerControl.replay();
|
||||
delegatingWriter.flush();
|
||||
writerControl.verify();
|
||||
}
|
||||
|
||||
public void testClear() throws Exception{
|
||||
itemWriter.clear();
|
||||
writerControl.replay();
|
||||
delegatingWriter.clear();
|
||||
writerControl.verify();
|
||||
}
|
||||
|
||||
public void testCreation() throws Exception{
|
||||
try{
|
||||
delegatingWriter.setDelegate(null);
|
||||
delegatingWriter.afterPropertiesSet();
|
||||
fail();
|
||||
}
|
||||
catch(IllegalArgumentException ex){
|
||||
//expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testWrite() throws Exception{
|
||||
|
||||
ProcessingWriter writer = new ProcessingWriter();
|
||||
writer.setDelegate(itemWriter);
|
||||
Object item = new Object();
|
||||
itemWriter.write(item);
|
||||
writerControl.replay();
|
||||
writer.write(item);
|
||||
writerControl.verify();
|
||||
assertTrue(writer.isDoProcessCalled());
|
||||
}
|
||||
|
||||
private class ProcessingWriter extends DelegatingItemWriter{
|
||||
|
||||
boolean doProcessCalled = false;
|
||||
|
||||
protected Object doProcess(Object item) throws Exception {
|
||||
doProcessCalled = true;
|
||||
return super.doProcess(item);
|
||||
}
|
||||
|
||||
public boolean isDoProcessCalled() {
|
||||
return doProcessCalled;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,112 +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.item.writer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.io.Skippable;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.support.PropertiesConverter;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class ItemWriterItemProcessorTests extends TestCase {
|
||||
|
||||
private DelegatingItemWriter processor = new DelegatingItemWriter();
|
||||
|
||||
private ItemWriter writer;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see junit.framework.TestCase#setUp()
|
||||
*/
|
||||
protected void setUp() throws Exception {
|
||||
writer = new MockOutputSource("test");
|
||||
processor.setDelegate(writer);
|
||||
processor.afterPropertiesSet();
|
||||
}
|
||||
|
||||
public void testProcess() throws Exception {
|
||||
processor.write("foo");
|
||||
assertEquals(1, list.size());
|
||||
assertEquals("test:foo", list.get(0));
|
||||
}
|
||||
|
||||
public void testSkip() {
|
||||
processor.skip();
|
||||
assertEquals(1, list.size());
|
||||
assertEquals("after skip", list.get(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* ItemWriter property must be set.
|
||||
*/
|
||||
public void testAfterPropertiesSet() throws Exception {
|
||||
processor.setDelegate(null);
|
||||
try {
|
||||
processor.afterPropertiesSet();
|
||||
fail();
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
private List list = new ArrayList();
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class MockOutputSource extends AbstractItemStreamItemWriter implements Skippable {
|
||||
|
||||
private String value;
|
||||
|
||||
public MockOutputSource(String string) {
|
||||
this.value = string;
|
||||
}
|
||||
|
||||
public void write(Object output) {
|
||||
list.add(value + ":" + output);
|
||||
}
|
||||
|
||||
public void close() {
|
||||
}
|
||||
|
||||
public void open() {
|
||||
}
|
||||
|
||||
public ExecutionContext getExecutionContext() {
|
||||
return new ExecutionContext(PropertiesConverter.stringToProperties("value=foo"));
|
||||
}
|
||||
|
||||
public void restoreFrom(ExecutionContext data) {
|
||||
value = data.getProperties().getProperty("value");
|
||||
}
|
||||
|
||||
public void skip() {
|
||||
list.add("after skip");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user