diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/jms/builder/JmsItemWriterBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/jms/builder/JmsItemWriterBuilder.java new file mode 100644 index 000000000..705a9e197 --- /dev/null +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/jms/builder/JmsItemWriterBuilder.java @@ -0,0 +1,59 @@ +/* + * 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 org.springframework.batch.item.jms.JmsItemWriter; +import org.springframework.jms.core.JmsOperations; +import org.springframework.util.Assert; + +/** + * Creates a fully qualified JmsItemWriter. + * + * @author Glenn Renfro + * + * @since 4.0 + */ +public class JmsItemWriterBuilder { + + private JmsOperations jmsTemplate; + + /** + * Establish the JMS template that will be used by the {@link JmsItemWriter}. + * + * @param jmsTemplate a {@link JmsOperations} instance + * @return this instance for method chaining. + * @see JmsItemWriter#setJmsTemplate(JmsOperations) + */ + public JmsItemWriterBuilder jmsTemplate(JmsOperations jmsTemplate) { + this.jmsTemplate = jmsTemplate; + + return this; + } + + /** + * Returns a fully constructed {@link JmsItemWriter}. + * + * @return a new {@link JmsItemWriter} + */ + public JmsItemWriter build() { + Assert.notNull(this.jmsTemplate, "jmsTemplate is required."); + JmsItemWriter jmsItemWriter = new JmsItemWriter<>(); + + jmsItemWriter.setJmsTemplate(this.jmsTemplate); + return jmsItemWriter; + } +} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/jms/builder/JmsItemWriterBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/jms/builder/JmsItemWriterBuilderTests.java new file mode 100644 index 000000000..7009dfb02 --- /dev/null +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/jms/builder/JmsItemWriterBuilderTests.java @@ -0,0 +1,61 @@ +/* + * 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.Arrays; + +import org.junit.Test; +import org.mockito.ArgumentCaptor; + +import org.springframework.batch.item.jms.JmsItemWriter; +import org.springframework.jms.core.JmsOperations; +import org.springframework.jms.core.JmsTemplate; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +/** + * @author Glenn Renfro + */ +public class JmsItemWriterBuilderTests { + + @Test + public void testNoItem() throws Exception { + JmsOperations jmsTemplate = mock(JmsOperations.class); + JmsItemWriter itemWriter = new JmsItemWriterBuilder().jmsTemplate(jmsTemplate).build(); + ArgumentCaptor argCaptor = ArgumentCaptor.forClass(String.class); + itemWriter.write(Arrays.asList("foo", "bar")); + verify(jmsTemplate, times(2)).convertAndSend(argCaptor.capture()); + assertEquals("Expected foo", "foo", argCaptor.getAllValues().get(0)); + assertEquals("Expected bar", "bar", argCaptor.getAllValues().get(1)); + } + + @Test + public void testNullJmsTemplate() { + try { + new JmsItemWriterBuilder().build(); + fail("IllegalArgumentException should have been thrown"); + } + catch (IllegalArgumentException ise) { + assertEquals("IllegalArgumentException message did not match the expected result.", + "jmsTemplate is required.", ise.getMessage()); + } + } +}