OPEN - issue BATCH-378: RepeatListener is confusing and too generic to use for 'intercepting' a step

http://jira.springframework.org/browse/BATCH-378

Fix hibernate aware item writer - no need for listener interface now that ItemWRiter has flush/clear
This commit is contained in:
dsyer
2008-02-29 16:15:52 +00:00
parent b1ae2efaae
commit e5f4413101
10 changed files with 453 additions and 217 deletions

View File

@@ -25,10 +25,8 @@ import junit.framework.TestCase;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.exception.ClearFailedException;
import org.springframework.batch.item.exception.FlushFailedException;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.RepeatListener;
import org.springframework.batch.repeat.context.RepeatContextSupport;
import org.springframework.batch.repeat.synch.RepeatSynchronizationManager;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.support.TransactionSynchronizationManager;
@@ -48,40 +46,17 @@ public class HibernateAwareItemWriterTests extends TestCase {
};
}
private class StubItemWriter implements ItemWriter, RepeatListener {
private class StubItemWriter implements ItemWriter {
public void write(Object item) {
list.add(item);
}
public void after(RepeatContext context, ExitStatus result) {
list.add(result);
}
public void before(RepeatContext context) {
list.add(context);
}
public void close(RepeatContext context) {
list.add(context);
}
public void onError(RepeatContext context, Throwable e) {
list.add(e);
}
public void open(RepeatContext context) {
list.add(context);
}
public void close() throws Exception {
}
public void clear() throws ClearFailedException {
list.add("clear");
list.add("delegateClear");
}
public void flush() throws FlushFailedException {
list.add("flush");
list.add("delegateFlush");
}
}
@@ -99,7 +74,7 @@ public class HibernateAwareItemWriterTests extends TestCase {
protected void setUp() throws Exception {
writer.setDelegate(new StubItemWriter());
context = new RepeatContextSupport(null);
writer.open(context);
RepeatSynchronizationManager.register(context);
writer.setHibernateTemplate(new HibernateTemplateWrapper());
list.clear();
}
@@ -158,7 +133,7 @@ public class HibernateAwareItemWriterTests extends TestCase {
* Test method for
* {@link org.springframework.batch.io.support.HibernateAwareItemWriter#write(java.lang.Object)}.
*/
public void testCloseWithFailure() throws Exception{
public void testFlushWithFailure() throws Exception{
final RuntimeException ex = new RuntimeException("bar");
writer.setHibernateTemplate(new HibernateTemplate() {
public void flush() throws DataAccessException {
@@ -166,14 +141,11 @@ public class HibernateAwareItemWriterTests extends TestCase {
}
});
try {
writer.close(context);
writer.flush();
fail("Expected RuntimeException");
} catch (RuntimeException e) {
assertEquals("bar", e.getMessage());
}
assertEquals(1, list.size());
System.err.println(list);
assertTrue(list.contains(context));
}
/**
@@ -181,7 +153,7 @@ public class HibernateAwareItemWriterTests extends TestCase {
* {@link org.springframework.batch.io.support.HibernateAwareItemWriter#write(java.lang.Object)}.
* @throws Exception
*/
public void testWriteAndCloseWithFailure() throws Exception {
public void testWriteAndFlushWithFailure() throws Exception {
final RuntimeException ex = new RuntimeException("bar");
writer.setHibernateTemplate(new HibernateTemplateWrapper() {
public void flush() throws DataAccessException {
@@ -190,41 +162,22 @@ 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(2, list.size());
assertTrue(list.contains(context));
assertEquals(1, list.size());
writer.setHibernateTemplate(new HibernateTemplateWrapper() {
public void flush() throws DataAccessException {
list.add("flush");
}
});
writer.open(context);
writer.write("foo");
assertEquals(6, list.size());
assertEquals(4, list.size());
assertTrue(list.contains("flush"));
assertTrue(list.contains("clear"));
}
/**
* Test method for
* {@link org.springframework.batch.io.support.HibernateAwareItemWriter#before(org.springframework.batch.repeat.RepeatContext)}.
*/
public void testBefore() {
writer.before(context);
assertEquals(1, list.size());
}
/**
* Test method for
* {@link org.springframework.batch.io.support.HibernateAwareItemWriter#after(org.springframework.batch.repeat.RepeatContext, org.springframework.batch.repeat.ExitStatus)}.
*/
public void testAfter() {
writer.after(context, ExitStatus.FINISHED);
assertEquals(1, list.size());
assertTrue(context.isCompleteOnly());
}
/**
@@ -233,40 +186,20 @@ public class HibernateAwareItemWriterTests extends TestCase {
*/
public void testFlush() throws Exception{
writer.flush();
assertEquals(1, list.size());
assertEquals(3, list.size());
assertTrue(list.contains("flush"));
assertTrue(list.contains("clear"));
assertTrue(list.contains("delegateFlush"));
}
/**
* Test method for
* {@link org.springframework.batch.io.support.HibernateAwareItemWriter#close(org.springframework.batch.repeat.RepeatContext)}.
*/
public void testCloseAfterClear() throws Exception{
Map map = TransactionSynchronizationManager.getResourceMap();
String key = (String) map.keySet().iterator().next();
TransactionSynchronizationManager.unbindResource(key);
writer.close(context);
assertEquals(3, list.size());
assertTrue(list.contains("flush"));
public void testClear() throws Exception{
writer.clear();
assertEquals(2, list.size());
assertTrue(list.contains("clear"));
assertTrue(list.contains("delegateClear"));
}
/**
* Test method for
* {@link org.springframework.batch.io.support.HibernateAwareItemWriter#onError(org.springframework.batch.repeat.RepeatContext, java.lang.Throwable)}.
*/
public void testOnError() {
writer.onError(context, new Exception());
assertEquals(1, list.size());
}
/**
* Test method for
* {@link org.springframework.batch.io.support.HibernateAwareItemWriter#open(org.springframework.batch.repeat.RepeatContext)}.
*/
public void testOpen() {
writer.open(context);
assertEquals(1, list.size());
}
}

View File

@@ -0,0 +1,98 @@
/*
* 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.interceptor;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.RepeatListener;
import org.springframework.batch.repeat.context.RepeatContextSupport;
/**
* @author Dave Syer
*
*/
public class CompositeRepeatListenerTests extends TestCase {
private CompositeRepeatListener listener = new CompositeRepeatListener();
private RepeatContext context = new RepeatContextSupport(null);
private List list = new ArrayList();
/**
* Test method for
* {@link org.springframework.batch.execution.listener.CompositeStepListener#setListeners(org.springframework.batch.core.domain.StepListener[])}.
*/
public void testSetListeners() {
listener.setListeners(new RepeatListener[] { new RepeatListenerSupport() {
public void open(RepeatContext context) {
list.add("fail");
}
}, new RepeatListenerSupport() {
public void open(RepeatContext context) {
list.add("continue");
}
} });
listener.open(context);
assertEquals(2, list.size());
}
/**
* Test method for
* {@link org.springframework.batch.execution.listener.CompositeStepListener#setListener(org.springframework.batch.core.domain.StepListener)}.
*/
public void testSetListener() {
listener.register(new RepeatListenerSupport() {
public void before(RepeatContext context) {
list.add("fail");
}
});
listener.before(context);
assertEquals(1, list.size());
}
/**
* Test method for
* {@link org.springframework.batch.execution.listener.CompositeStepListener#beforeStep(StepExecution)}.
*/
public void testClose() {
listener.register(new RepeatListenerSupport() {
public void close(RepeatContext context) {
list.add("foo");
}
});
listener.close(context);
assertEquals(1, list.size());
}
/**
* Test method for
* {@link org.springframework.batch.execution.listener.CompositeStepListener#beforeStep(StepExecution)}.
*/
public void testOnError() {
listener.register(new RepeatListenerSupport() {
public void onError(RepeatContext context, Throwable e) {
list.add(e);
}
});
listener.onError(context, new RuntimeException("foo"));
assertEquals(1, list.size());
}
}