BATCH-1882: Add support for AMQP backed ItemReader / ItemWriter implementations

Add Spring AMQP and Spring Rabbit dependencies. Update
EasyMock from 2.3 to 2.4 to allow for concrete class mocking.
This commit is contained in:
Chris Schaefer
2012-07-30 22:41:51 -04:00
committed by Dave Syer
parent 7476ac1628
commit 1607014886
8 changed files with 316 additions and 3 deletions

View File

@@ -0,0 +1,109 @@
/*
* Copyright 2012 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.amqp;
import org.easymock.classextension.EasyMock;
import org.junit.Test;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* <p>
* Test cases around {@link AmqpItemReader}.
* </p>
*
* @author Chris Schaefer
*/
public class AmqpItemReaderTests {
@Test(expected = IllegalArgumentException.class)
public void testNullAmqpTemplate() {
new AmqpItemReader<String>(null);
}
@Test
public void testNoItemType() {
final AmqpTemplate amqpTemplate = EasyMock.createMock(AmqpTemplate.class);
EasyMock.expect(amqpTemplate.receiveAndConvert()).andReturn("foo");
EasyMock.replay(amqpTemplate);
final AmqpItemReader<String> amqpItemReader = new AmqpItemReader<String>(amqpTemplate);
assertEquals("foo", amqpItemReader.read());
EasyMock.verify(amqpTemplate);
}
@Test
public void testNonMessageItemType() {
final AmqpTemplate amqpTemplate = EasyMock.createMock(AmqpTemplate.class);
EasyMock.expect(amqpTemplate.receiveAndConvert()).andReturn("foo");
EasyMock.replay(amqpTemplate);
final AmqpItemReader<String> amqpItemReader = new AmqpItemReader<String>(amqpTemplate);
amqpItemReader.setItemType(String.class);
assertEquals("foo", amqpItemReader.read());
EasyMock.verify(amqpTemplate);
}
@Test
public void testMessageItemType() {
final AmqpTemplate amqpTemplate = EasyMock.createMock(AmqpTemplate.class);
final Message message = EasyMock.createMock(Message.class);
EasyMock.expect(amqpTemplate.receive()).andReturn(message);
EasyMock.replay(amqpTemplate, message);
final AmqpItemReader<Message> amqpItemReader = new AmqpItemReader<Message>(amqpTemplate);
amqpItemReader.setItemType(Message.class);
assertEquals(message, amqpItemReader.read());
EasyMock.verify(amqpTemplate);
}
@Test
public void testTypeMismatch() {
final AmqpTemplate amqpTemplate = EasyMock.createMock(AmqpTemplate.class);
EasyMock.expect(amqpTemplate.receiveAndConvert()).andReturn("foo");
EasyMock.replay(amqpTemplate);
final AmqpItemReader<Integer> amqpItemReader = new AmqpItemReader<Integer>(amqpTemplate);
amqpItemReader.setItemType(Integer.class);
try {
amqpItemReader.read();
fail("Expected IllegalStateException");
} catch (IllegalStateException e) {
assertTrue(e.getMessage().contains("wrong type"));
}
EasyMock.verify(amqpTemplate);
}
@Test(expected = IllegalArgumentException.class)
public void testNullItemType() {
final AmqpTemplate amqpTemplate = EasyMock.createMock(AmqpTemplate.class);
final AmqpItemReader<String> amqpItemReader = new AmqpItemReader<String>(amqpTemplate);
amqpItemReader.setItemType(null);
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2012 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.amqp;
import org.easymock.EasyMock;
import org.junit.Test;
import org.springframework.amqp.core.AmqpTemplate;
import java.util.Arrays;
/**
* <p>
* Test cases around {@link AmqpItemWriter}.
* </p>
*
* @author Chris Schaefer
*/
public class AmqpItemWriterTests {
@Test(expected = IllegalArgumentException.class)
public void testNullAmqpTemplate() {
new AmqpItemWriter<String>(null);
}
@Test
public void voidTestWrite() throws Exception {
AmqpTemplate amqpTemplate = EasyMock.createMock(AmqpTemplate.class);
amqpTemplate.convertAndSend("foo");
EasyMock.expectLastCall();
amqpTemplate.convertAndSend("bar");
EasyMock.expectLastCall();
EasyMock.replay(amqpTemplate);
AmqpItemWriter<String> amqpItemWriter = new AmqpItemWriter<String>(amqpTemplate);
amqpItemWriter.write(Arrays.asList("foo", "bar"));
EasyMock.verify(amqpTemplate);
}
}