diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/jms/builder/JmsItemReaderBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/jms/builder/JmsItemReaderBuilder.java new file mode 100644 index 000000000..caf394122 --- /dev/null +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/jms/builder/JmsItemReaderBuilder.java @@ -0,0 +1,81 @@ +/* + * 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.jms.builder; + +import javax.jms.Message; + +import org.springframework.batch.item.jms.JmsItemReader; +import org.springframework.jms.core.JmsOperations; +import org.springframework.util.Assert; + +/** + * Creates a fully qualified JmsItemReader. + * + * @author Glenn Renfro + * + * @since 4.0 + */ +public class JmsItemReaderBuilder { + + protected Class itemType; + + protected JmsOperations jmsTemplate; + + /** + * Establish the JMS template that will be used by the JmsItemReader. + * + * @param jmsTemplate a {@link JmsOperations} instance + * @return this instance for method chaining. + * @see JmsItemReader#setJmsTemplate(JmsOperations) + */ + public JmsItemReaderBuilder jmsTemplate(JmsOperations jmsTemplate) { + this.jmsTemplate = jmsTemplate; + + return this; + } + + /** + * Set the expected type of incoming message payloads. Set this to {@link Message} to + * receive the raw underlying message. + * + * @param itemType the java class of the items to be delivered. Typically the same as + * the class parameter + * @return this instance for method chaining. + * + * @throws IllegalStateException if the message payload is of the wrong type. + * @see JmsItemReader#setItemType(Class) + */ + public JmsItemReaderBuilder itemType(Class itemType) { + this.itemType = itemType; + + return this; + } + + /** + * Returns a fully constructed {@link JmsItemReader}. + * + * @return a new {@link JmsItemReader} + */ + public JmsItemReader build() { + Assert.notNull(this.jmsTemplate, "jmsTemplate is required."); + JmsItemReader jmsItemReader = new JmsItemReader<>(); + + jmsItemReader.setItemType(this.itemType); + jmsItemReader.setJmsTemplate(this.jmsTemplate); + return jmsItemReader; + } +} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/jms/builder/JmsItemReaderBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/jms/builder/JmsItemReaderBuilderTests.java new file mode 100644 index 000000000..ace6ee612 --- /dev/null +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/jms/builder/JmsItemReaderBuilderTests.java @@ -0,0 +1,104 @@ +/* + * 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.jms.builder; + +import java.util.Date; + +import javax.jms.Message; + +import org.junit.Before; +import org.junit.Test; + +import org.springframework.batch.item.jms.JmsItemReader; +import org.springframework.jms.core.JmsOperations; +import org.springframework.jms.core.JmsTemplate; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * @author Glenn Renfro + */ +public class JmsItemReaderBuilderTests { + + private JmsOperations defaultJmsTemplate; + + @Before + public void setupJmsTemplate() { + this.defaultJmsTemplate = mock(JmsOperations.class); + when(this.defaultJmsTemplate.receiveAndConvert()).thenReturn("foo"); + } + + @Test + public void testBasicRead() { + JmsItemReader itemReader = new JmsItemReaderBuilder().jmsTemplate(this.defaultJmsTemplate) + .build(); + assertEquals("foo", itemReader.read()); + } + + @Test + public void testSetItemSubclassType() { + JmsOperations jmsTemplate = mock(JmsOperations.class); + + Date date = new java.sql.Date(0L); + when(jmsTemplate.receiveAndConvert()).thenReturn(date); + + JmsItemReader itemReader = new JmsItemReaderBuilder().jmsTemplate(jmsTemplate).itemType(Date.class) + .build(); + assertEquals(date, itemReader.read()); + } + + @Test + public void testSetItemTypeMismatch() { + JmsItemReader itemReader = new JmsItemReaderBuilder().jmsTemplate(this.defaultJmsTemplate) + .itemType(Date.class).build(); + try { + itemReader.read(); + fail("Expected IllegalStateException"); + } + catch (IllegalStateException e) { + // expected + assertTrue(e.getMessage().indexOf("wrong type") >= 0); + } + } + + @Test + public void testMessageType() { + JmsOperations jmsTemplate = mock(JmsOperations.class); + Message message = mock(Message.class); + when(jmsTemplate.receive()).thenReturn(message); + + JmsItemReader itemReader = new JmsItemReaderBuilder().jmsTemplate(jmsTemplate) + .itemType(Message.class).build(); + assertEquals(message, itemReader.read()); + } + + @Test + public void testNullJmsTemplate() { + try { + new JmsItemReaderBuilder().itemType(String.class).build(); + fail("IllegalArgumentException should have been thrown"); + } + catch (IllegalArgumentException ise) { + assertEquals("IllegalArgumentException message did not match the expected result.", + "jmsTemplate is required.", ise.getMessage()); + } + } +}