Add builder for JmsItemReader
resolves BATCH-2602
This commit is contained in:
committed by
Michael Minella
parent
70a1a83a9a
commit
3850e4c2fe
@@ -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<T> {
|
||||
|
||||
protected Class<? extends T> 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<T> 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<T> itemType(Class<? extends T> itemType) {
|
||||
this.itemType = itemType;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a fully constructed {@link JmsItemReader}.
|
||||
*
|
||||
* @return a new {@link JmsItemReader}
|
||||
*/
|
||||
public JmsItemReader<T> build() {
|
||||
Assert.notNull(this.jmsTemplate, "jmsTemplate is required.");
|
||||
JmsItemReader<T> jmsItemReader = new JmsItemReader<>();
|
||||
|
||||
jmsItemReader.setItemType(this.itemType);
|
||||
jmsItemReader.setJmsTemplate(this.jmsTemplate);
|
||||
return jmsItemReader;
|
||||
}
|
||||
}
|
||||
@@ -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<String> itemReader = new JmsItemReaderBuilder<String>().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<Date> itemReader = new JmsItemReaderBuilder<Date>().jmsTemplate(jmsTemplate).itemType(Date.class)
|
||||
.build();
|
||||
assertEquals(date, itemReader.read());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetItemTypeMismatch() {
|
||||
JmsItemReader<Date> itemReader = new JmsItemReaderBuilder<Date>().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<Message> itemReader = new JmsItemReaderBuilder<Message>().jmsTemplate(jmsTemplate)
|
||||
.itemType(Message.class).build();
|
||||
assertEquals(message, itemReader.read());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullJmsTemplate() {
|
||||
try {
|
||||
new JmsItemReaderBuilder<String>().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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user