OPEN - issue BATCH-673: Add new Java 5.0 features

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

Fixed a couple of small implementation bugs, and cleaned up a bit.
This commit is contained in:
lucasward
2008-11-22 05:41:57 +00:00
parent af6257f8be
commit ae730065c7
8 changed files with 373 additions and 66 deletions

View File

@@ -39,6 +39,7 @@ import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.util.Assert;
/**
* @author Lucas Ward
@@ -101,9 +102,27 @@ public class SimpleMethodInvokerTests {
assertFalse(testClass.beforeJobCalled);
}
@Test
public void testMethodWithArgument() throws Exception{
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, "argumentTest", Object.class);
methodInvoker.invokeMethod(new Object());
assertTrue(testClass.argumentTestCalled);
}
@Test
public void testEquals() throws Exception{
Method method = TestClass.class.getMethod("beforeJobWithExecution", JobExecution.class);
MethodInvoker methodInvoker = new SimpleMethodInvoker(testClass, method);
method = TestClass.class.getMethod("beforeJobWithExecution", JobExecution.class);
MethodInvoker methodInvoker2 = new SimpleMethodInvoker(testClass, method);
assertEquals(methodInvoker, methodInvoker2);
}
private class TestClass{
boolean beforeJobCalled = false;
boolean argumentTestCalled = false;
public void beforeJob(){
beforeJobCalled = true;
@@ -116,5 +135,10 @@ public class SimpleMethodInvokerTests {
public void beforeJobWithTooManyArguments(JobExecution jobExecution, int someInt){
beforeJobCalled = true;
}
public void argumentTest(Object object){
Assert.notNull(object);
argumentTestCalled = true;
}
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.batch.core.listener;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.springframework.batch.core.listener.StepListenerMetaData.AFTER_CHUNK;
import static org.springframework.batch.core.listener.StepListenerMetaData.AFTER_STEP;
@@ -27,6 +28,7 @@ import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.core.ChunkListener;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.ItemProcessListener;
import org.springframework.batch.core.ItemReadListener;
import org.springframework.batch.core.ItemWriteListener;
@@ -37,6 +39,7 @@ import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.core.StepListener;
import org.springframework.batch.core.annotation.AfterProcess;
import org.springframework.batch.core.annotation.AfterRead;
import org.springframework.batch.core.annotation.AfterStep;
import org.springframework.batch.core.annotation.AfterWrite;
import org.springframework.batch.core.annotation.BeforeChunk;
import org.springframework.batch.core.annotation.BeforeProcess;
@@ -45,9 +48,6 @@ import org.springframework.batch.core.annotation.BeforeStep;
import org.springframework.batch.core.annotation.BeforeWrite;
import org.springframework.batch.core.annotation.OnProcessError;
import org.springframework.batch.core.annotation.OnReadError;
import org.springframework.batch.core.annotation.OnSkipInProcess;
import org.springframework.batch.core.annotation.OnSkipInRead;
import org.springframework.batch.core.annotation.OnSkipInWrite;
import org.springframework.batch.core.annotation.OnWriteError;
import org.springframework.util.Assert;
@@ -86,7 +86,7 @@ public class StepListenerFactoryBeanTests {
((ChunkListener)listener).beforeChunk();
((ChunkListener)listener).afterChunk();
((ItemReadListener<Object>)listener).beforeRead();
//((ItemReadListener<Object>)listener).afterRead(item);
((ItemReadListener<Object>)listener).afterRead(item);
((ItemReadListener)listener).onReadError(new Exception());
((ItemProcessListener<Object, Object>)listener).beforeProcess(item);
((ItemProcessListener<Object, Object>)listener).afterProcess(item, item);
@@ -101,7 +101,7 @@ public class StepListenerFactoryBeanTests {
assertTrue(testClass.beforeChunkCalled);
assertTrue(testClass.afterChunkCalled);
assertTrue(testClass.beforeReadCalled);
//assertTrue(testClass.afterReadCalled);
assertTrue(testClass.afterReadCalled);
assertTrue(testClass.onReadErrorCalled);
assertTrue(testClass.beforeProcessCalled);
assertTrue(testClass.afterProcessCalled);
@@ -114,7 +114,74 @@ public class StepListenerFactoryBeanTests {
assertTrue(testClass.onSkipInWriteCalled);
}
@Test
public void testAllThreeTypes() throws Exception{
//Test to make sure if someone has annotated a method, implemented the interface, and given a string
//method name, that all three will be called
ThreeStepExecutionListener delegate = new ThreeStepExecutionListener();
factoryBean.setDelegate(delegate);
Map<StepListenerMetaData, String> metaDataMap = new HashMap<StepListenerMetaData, String>();;
metaDataMap.put(AFTER_STEP, "destroy");
factoryBean.setMetaDataMap(metaDataMap);
StepListener listener = (StepListener) factoryBean.getObject();
((StepExecutionListener)listener).afterStep(stepExecution);
assertEquals(3, delegate.callcount);
}
@Test
public void testAnnotatingInterfaceResultsInOneCall() throws Exception{
MultipleAfterStep delegate = new MultipleAfterStep();
factoryBean.setDelegate(delegate);
Map<StepListenerMetaData, String> metaDataMap = new HashMap<StepListenerMetaData, String>();;
metaDataMap.put(AFTER_STEP, "afterStep");
factoryBean.setMetaDataMap(metaDataMap);
StepListener listener = (StepListener) factoryBean.getObject();
((StepExecutionListener)listener).afterStep(stepExecution);
assertEquals(1, delegate.callcount);
}
private class MultipleAfterStep implements StepExecutionListener{
int callcount = 0;
@AfterStep
public ExitStatus afterStep(StepExecution stepExecution) {
Assert.notNull(stepExecution);
callcount++;
return null;
}
public void beforeStep(StepExecution stepExecution) {
callcount++;
}
}
private class ThreeStepExecutionListener implements StepExecutionListener{
int callcount = 0;
public ExitStatus afterStep(StepExecution stepExecution) {
Assert.notNull(stepExecution);
callcount++;
return null;
}
public void beforeStep(StepExecution stepExecution) {
callcount++;
}
public void destroy(){
callcount++;
}
@AfterStep
public void after(){
callcount++;
}
}
private class TestClass implements SkipListener<Object, Object>{
@@ -199,17 +266,14 @@ public class StepListenerFactoryBeanTests {
onWriteErrorCalled = true;
}
@OnSkipInProcess
public void onSkipInProcess(Object item, Throwable t) {
onSkipInProcessCalled = true;
}
@OnSkipInRead
public void onSkipInRead(Throwable t) {
onSkipInReadCalled = true;
}
@OnSkipInWrite
public void onSkipInWrite(Object item, Throwable t) {
onSkipInWriteCalled = true;
}

View File

@@ -0,0 +1,129 @@
package org.springframework.batch.core.listener;
import static org.junit.Assert.assertEquals;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.configuration.util.MethodInvoker;
import org.springframework.batch.core.configuration.util.MethodInvokerUtils;
import org.springframework.batch.core.configuration.util.SimpleMethodInvoker;
public class StepListenerMethodInterceptorTests {
StepListenerMethodInterceptor interceptor;
TestClass testClass;
@Before
public void setUp(){
testClass = new TestClass();
}
@Test
public void testNormalCase() throws Throwable{
Map<String, Set<MethodInvoker>> invokerMap = new HashMap<String, Set<MethodInvoker>>();
for(Method method : TestClass.class.getMethods()){
invokerMap.put(method.getName(), asSet( new SimpleMethodInvoker(testClass, method)));
}
interceptor = new StepListenerMethodInterceptor(invokerMap);
interceptor.invoke(new StubMethodInvocation(TestClass.class.getMethod("method1")));
assertEquals(1, testClass.method1Count);
interceptor.invoke(new StubMethodInvocation(TestClass.class.getMethod("method2")));
assertEquals(1, testClass.method2Count);
}
@Test
public void testMultipleInvokersPerName() throws Throwable{
Map<String, Set<MethodInvoker>> invokerMap = new HashMap<String, Set<MethodInvoker>>();
Set<MethodInvoker> invokers = asSet(MethodInvokerUtils.createMethodInvokerByName(testClass, "method1", false));
invokers.add(MethodInvokerUtils.createMethodInvokerByName(testClass, "method2", false));
invokerMap.put("method1", invokers);
interceptor = new StepListenerMethodInterceptor(invokerMap);
interceptor.invoke(new StubMethodInvocation(TestClass.class.getMethod("method1")));
assertEquals(1, testClass.method1Count);
interceptor.invoke(new StubMethodInvocation(TestClass.class.getMethod("method2")));
assertEquals(1, testClass.method2Count);
}
@Test
public void testExitStatusReturn() throws Throwable{
Map<String, Set<MethodInvoker>> invokerMap = new HashMap<String, Set<MethodInvoker>>();
Set<MethodInvoker> invokers = asSet(MethodInvokerUtils.createMethodInvokerByName(testClass, "method3", false));
invokers.add(MethodInvokerUtils.createMethodInvokerByName(testClass, "method3", false));
invokerMap.put("method3", invokers);
interceptor = new StepListenerMethodInterceptor(invokerMap);
assertEquals(ExitStatus.FINISHED, interceptor.invoke(new StubMethodInvocation(TestClass.class.getMethod("method3"))));
}
public Set<MethodInvoker> asSet(MethodInvoker methodInvoker){
Set<MethodInvoker> invokerSet = new HashSet<MethodInvoker>();
invokerSet.add(methodInvoker);
return invokerSet;
}
private class TestClass{
int method1Count = 0;
int method2Count = 0;
int method3Count = 0;
public void method1(){
method1Count++;
}
public void method2(){
method2Count++;
}
public ExitStatus method3(){
method3Count++;
return ExitStatus.FINISHED;
}
}
private class StubMethodInvocation implements MethodInvocation{
Method method;
Object[] args;
public StubMethodInvocation(Method method, Object... args) {
this.method = method;
this.args = args;
}
public Method getMethod() {
return method;
}
public Object[] getArguments() {
// TODO Auto-generated method stub
return null;
}
public AccessibleObject getStaticPart() {
// TODO Auto-generated method stub
return null;
}
public Object getThis() {
// TODO Auto-generated method stub
return null;
}
public Object proceed() throws Throwable {
// TODO Auto-generated method stub
return null;
}
}
}