OPEN - issue BATCH-777: Parametrise RetryCallback and related interfaces

Move key generator and item identifier to retry interceptor package
This commit is contained in:
dsyer
2008-08-25 16:08:19 +00:00
parent 6e36093b2e
commit d41464c176
19 changed files with 519 additions and 299 deletions

View File

@@ -16,19 +16,21 @@
package org.springframework.batch.item;
import org.springframework.batch.retry.interceptor.MethodInvocationRecoverer;
import junit.framework.TestCase;
public class ItemRecoveryHandlerTests extends TestCase {
ItemRecoverer<String, String> recoverer = new ItemRecoverer<String,String>() {
public String recover(String data, Throwable cause) {
MethodInvocationRecoverer<String> recoverer = new MethodInvocationRecoverer<String>() {
public String recover(Object[] data, Throwable cause) {
return null;
}
};
public void testRecover() throws Exception {
try {
recoverer.recover("foo", null);
recoverer.recover(new Object[]{"foo"}, null);
} catch (Exception e) {
fail("Unexpected Exception");
}

View File

@@ -23,7 +23,6 @@ import static org.junit.Assert.fail;
import java.util.Date;
import javax.jms.Message;
import javax.jms.Queue;
import org.easymock.EasyMock;
import org.junit.Test;
@@ -106,84 +105,4 @@ public class JmsItemReaderTests {
EasyMock.verify(jmsTemplate);
}
@Test
public void testRecoverWithNoDestination() throws Exception {
JmsOperations jmsTemplate = EasyMock.createMock(JmsOperations.class);
EasyMock.replay(jmsTemplate);
itemReader.setJmsTemplate(jmsTemplate);
itemReader.setItemType(String.class);
itemReader.recover("foo", null);
EasyMock.verify(jmsTemplate);
}
@Test
public void testErrorQueueWithDestinationName() throws Exception {
JmsOperations jmsTemplate = EasyMock.createMock(JmsOperations.class);
jmsTemplate.convertAndSend("queue", "foo");
EasyMock.expectLastCall();
EasyMock.replay(jmsTemplate);
itemReader.setJmsTemplate(jmsTemplate);
itemReader.setItemType(String.class);
itemReader.setErrorDestinationName("queue");
itemReader.recover("foo", null);
EasyMock.verify(jmsTemplate);
}
@Test
public void testErrorQueueWithDestination() throws Exception {
JmsOperations jmsTemplate = EasyMock.createMock(JmsOperations.class);
Queue queue = EasyMock.createMock(Queue.class);
jmsTemplate.convertAndSend(queue, "foo");
EasyMock.expectLastCall();
EasyMock.replay(jmsTemplate, queue);
itemReader.setJmsTemplate(jmsTemplate);
itemReader.setItemType(String.class);
itemReader.setErrorDestination(queue);
itemReader.recover("foo", null);
EasyMock.verify(jmsTemplate, queue);
}
@Test
public void testGetKeyFromMessage() throws Exception {
Message message = EasyMock.createMock(Message.class);
EasyMock.expect(message.getJMSMessageID()).andReturn("foo");
EasyMock.replay(message);
JmsItemReader<Message> itemReader = new JmsItemReader<Message>();
itemReader.setItemType(Message.class);
assertEquals("foo", itemReader.getKey(message));
EasyMock.verify(message);
}
@Test
public void testGetKeyFromNonMessage() throws Exception {
itemReader.setItemType(String.class);
assertEquals("foo", itemReader.getKey("foo"));
}
@Test
public void testIsNewForMessage() throws Exception {
Message message = EasyMock.createMock(Message.class);
EasyMock.expect(message.getJMSRedelivered()).andReturn(true);
EasyMock.replay(message);
JmsItemReader<Message> itemReader = new JmsItemReader<Message>();
itemReader.setItemType(Message.class);
assertEquals(false, itemReader.isNew(message));
EasyMock.verify(message);
}
@Test
public void testIsNewForNonMessage() throws Exception {
itemReader.setItemType(String.class);
assertEquals(false, itemReader.isNew("foo"));
}
}

View File

@@ -0,0 +1,52 @@
/*
* 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.jms;
import static org.junit.Assert.assertEquals;
import javax.jms.Message;
import org.easymock.EasyMock;
import org.junit.Test;
/**
* @author Dave Syer
*
*/
public class JmsMethodArgumentsKeyGeneratorTests {
private JmsMethodArgumentsKeyGenerator methodArgumentsKeyGenerator = new JmsMethodArgumentsKeyGenerator();
@Test
public void testGetKeyFromMessage() throws Exception {
Message message = EasyMock.createMock(Message.class);
EasyMock.expect(message.getJMSMessageID()).andReturn("foo");
EasyMock.replay(message);
JmsItemReader<Message> itemReader = new JmsItemReader<Message>();
itemReader.setItemType(Message.class);
assertEquals("foo", methodArgumentsKeyGenerator.getKey(new Object[]{message}));
EasyMock.verify(message);
}
@Test
public void testGetKeyFromNonMessage() throws Exception {
assertEquals("foo", methodArgumentsKeyGenerator.getKey(new Object[]{"foo"}));
}
}

View File

@@ -0,0 +1,42 @@
/*
* 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.jms;
import org.easymock.EasyMock;
import org.junit.Test;
import org.springframework.jms.core.JmsOperations;
/**
* @author Dave Syer
*
*/
public class JmsMethodInvocationRecovererTests {
private JmsMethodInvocationRecoverer<String> itemReader = new JmsMethodInvocationRecoverer<String>();
@Test
public void testRecoverWithNoDestination() throws Exception {
JmsOperations jmsTemplate = EasyMock.createMock(JmsOperations.class);
jmsTemplate.convertAndSend("foo");
EasyMock.replay(jmsTemplate);
itemReader.setJmsTemplate(jmsTemplate);
itemReader.recover(new Object[] { "foo" }, null);
EasyMock.verify(jmsTemplate);
}
}

View File

@@ -0,0 +1,48 @@
/*
* 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.jms;
import static org.junit.Assert.assertEquals;
import javax.jms.Message;
import org.easymock.EasyMock;
import org.junit.Test;
/**
* @author Dave Syer
*
*/
public class JmsNewMethodArgumentsIdentifierTests {
private JmsNewMethodArgumentsIdentifier<String> newMethodArgumentsIdentifier = new JmsNewMethodArgumentsIdentifier<String>();
@Test
public void testIsNewForMessage() throws Exception {
Message message = EasyMock.createMock(Message.class);
EasyMock.expect(message.getJMSRedelivered()).andReturn(true);
EasyMock.replay(message);
assertEquals(false, newMethodArgumentsIdentifier.isNew(new Object[]{message}));
EasyMock.verify(message);
}
@Test
public void testIsNewForNonMessage() throws Exception {
assertEquals(false, newMethodArgumentsIdentifier.isNew(new Object[]{"foo"}));
}
}

View File

@@ -27,7 +27,6 @@ import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.target.SingletonTargetSource;
import org.springframework.batch.item.ItemRecoverer;
import org.springframework.batch.retry.ExhaustedRetryException;
import org.springframework.batch.retry.policy.AlwaysRetryPolicy;
import org.springframework.batch.retry.policy.NeverRetryPolicy;
@@ -170,7 +169,7 @@ public class StatefulRetryOperationsInterceptorTests extends TestCase {
assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
}
assertEquals(1, count);
interceptor.setRecoverer(new ItemRecoverer<Object, Object[]>() {
interceptor.setRecoverer(new MethodInvocationRecoverer<Object>() {
public Object recover(Object[] data, Throwable cause) {
count++;
return null;
@@ -192,7 +191,7 @@ public class StatefulRetryOperationsInterceptorTests extends TestCase {
assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
}
assertEquals(1, count);
interceptor.setRecoverer(new ItemRecoverer<Collection<String>, Object[]>() {
interceptor.setRecoverer(new MethodInvocationRecoverer<Collection<String>>() {
public Collection<String> recover(Object[] data, Throwable cause) {
count++;
return Collections.singleton((String)data[0]);