diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/amqp/AmqpItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/amqp/AmqpItemReader.java index ee4d10cd3..113bb2725 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/amqp/AmqpItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/amqp/AmqpItemReader.java @@ -33,6 +33,11 @@ public class AmqpItemReader implements ItemReader { private final AmqpTemplate amqpTemplate; private Class itemType; + /** + * Initialize the AmqpItemReader. + * + * @param amqpTemplate the template to be used. Must not be null. + */ public AmqpItemReader(final AmqpTemplate amqpTemplate) { Assert.notNull(amqpTemplate, "AmpqTemplate must not be null"); @@ -56,6 +61,11 @@ public class AmqpItemReader implements ItemReader { return (T) result; } + /** + * Establish the itemType for the reader. + * + * @param itemType class type that will be returned by the reader. + */ public void setItemType(Class itemType) { Assert.notNull(itemType, "Item type cannot be null"); this.itemType = itemType; diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/amqp/builder/AmqpItemReaderBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/amqp/builder/AmqpItemReaderBuilder.java new file mode 100644 index 000000000..e13f97f7b --- /dev/null +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/amqp/builder/AmqpItemReaderBuilder.java @@ -0,0 +1,75 @@ +/* + * Copyright 2017 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.builder; + +import org.springframework.amqp.core.AmqpTemplate; +import org.springframework.batch.item.amqp.AmqpItemReader; +import org.springframework.util.Assert; + +/** + * A builder implementation for the {@link AmqpItemReader} + * + * @author Glenn Renfro + * @since 4.0 + * @see AmqpItemReader + */ +public class AmqpItemReaderBuilder { + + private AmqpTemplate amqpTemplate; + + private Class itemType; + + /** + * Establish the amqpTemplate to be used by the AmqpItemReader. + * @param amqpTemplate the template to be used. + * @return this instance for method chaining + * @see AmqpItemReader#AmqpItemReader(AmqpTemplate) + */ + public AmqpItemReaderBuilder amqpTemplate(AmqpTemplate amqpTemplate) { + this.amqpTemplate = amqpTemplate; + + return this; + } + + /** + * Establish the itemType for the reader. + * @param itemType class type that will be returned by the reader. + * @return this instance for method chaining. + * @see AmqpItemReader#setItemType(Class) + */ + public AmqpItemReaderBuilder itemType(Class itemType) { + this.itemType = itemType; + + return this; + } + + /** + * Validates and builds a {@link AmqpItemReader}. + * + * @return a {@link AmqpItemReader} + */ + public AmqpItemReader build() { + Assert.notNull(this.amqpTemplate, "amqpTemplate is required."); + + AmqpItemReader reader = new AmqpItemReader<>(this.amqpTemplate); + if(this.itemType != null) { + reader.setItemType(this.itemType); + } + + return reader; + } +} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/amqp/builder/AmqpItemReaderBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/amqp/builder/AmqpItemReaderBuilderTests.java new file mode 100644 index 000000000..08899fa9f --- /dev/null +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/amqp/builder/AmqpItemReaderBuilderTests.java @@ -0,0 +1,88 @@ +/* + * Copyright 2017 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.builder; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import org.springframework.amqp.core.AmqpTemplate; +import org.springframework.amqp.core.Message; +import org.springframework.batch.item.amqp.AmqpItemReader; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * @author Glenn Renfro + */ +public class AmqpItemReaderBuilderTests { + + @Mock + AmqpTemplate amqpTemplate; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testNoItemType() { + when(this.amqpTemplate.receiveAndConvert()).thenReturn("foo"); + + final AmqpItemReader amqpItemReader = new AmqpItemReaderBuilder() + .amqpTemplate(this.amqpTemplate).build(); + assertEquals("foo", amqpItemReader.read()); + } + + @Test + public void testNonMessageItemType() { + when(this.amqpTemplate.receiveAndConvert()).thenReturn("foo"); + + final AmqpItemReader amqpItemReader = new AmqpItemReaderBuilder() + .amqpTemplate(this.amqpTemplate).itemType(String.class).build(); + + assertEquals("foo", amqpItemReader.read()); + } + + @Test + public void testMessageItemType() { + final Message message = mock(Message.class); + + when(this.amqpTemplate.receive()).thenReturn(message); + + final AmqpItemReader amqpItemReader = new AmqpItemReaderBuilder() + .amqpTemplate(this.amqpTemplate).itemType(Message.class).build(); + + assertEquals(message, amqpItemReader.read()); + } + + @Test + public void testNullAmqpTemplate() { + try { + new AmqpItemReaderBuilder().build(); + fail("IllegalArgumentException should have been thrown"); + } + catch (IllegalArgumentException iae) { + assertEquals("IllegalArgumentException message did not match the expected result.", + "amqpTemplate is required.", iae.getMessage()); + } + } +}