Add builder for CompositeItemProcessor
resolves #BATCH-2617
This commit is contained in:
committed by
Michael Minella
parent
332a2017b3
commit
a6ed38d1d5
@@ -67,8 +67,14 @@ public class CompositeItemProcessor<I, O> implements ItemProcessor<I, O>, Initia
|
||||
Assert.notEmpty(delegates, "The 'delegates' may not be empty");
|
||||
}
|
||||
|
||||
/**
|
||||
* Establishes the {@link ItemProcessor} delegates that will work on the item to be
|
||||
* processed.
|
||||
* @param delegates list of {@link ItemProcessor} delegates that will work on the
|
||||
* item.
|
||||
*/
|
||||
public void setDelegates(List<? extends ItemProcessor<?, ?>> delegates) {
|
||||
this.delegates = delegates;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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.support.builder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.batch.item.support.CompositeItemProcessor;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Creates a fully qualified {@link CompositeItemProcessorBuilder}.
|
||||
*
|
||||
* @author Glenn Renfro
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class CompositeItemProcessorBuilder<I, O> {
|
||||
private List<? extends ItemProcessor<?, ?>> delegates;
|
||||
|
||||
/**
|
||||
* Establishes the {@link ItemProcessor} delegates that will work on the item to be processed.
|
||||
* @param delegates list of {@link ItemProcessor} delegates that will work on the item.
|
||||
* @return this instance for method chaining.
|
||||
* @see CompositeItemProcessor#setDelegates(List)
|
||||
*/
|
||||
public CompositeItemProcessorBuilder<I, O> delegates(List<? extends ItemProcessor<?, ?>> delegates) {
|
||||
this.delegates = delegates;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a fully constructed {@link CompositeItemProcessor}.
|
||||
*
|
||||
* @return a new {@link CompositeItemProcessor}
|
||||
*/
|
||||
public CompositeItemProcessor<I, O> build() {
|
||||
Assert.notNull(delegates, "A list of delegates is required.");
|
||||
Assert.notEmpty(delegates, "The delegates list must have one or more delegates.");
|
||||
|
||||
CompositeItemProcessor<I, O> processor = new CompositeItemProcessor<>();
|
||||
processor.setDelegates(this.delegates);
|
||||
return processor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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.support.builder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.batch.item.support.CompositeItemProcessor;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Glenn Renfro
|
||||
*/
|
||||
public class CompositeItemProcessorBuilderTests {
|
||||
|
||||
@Mock
|
||||
private ItemProcessor<Object, Object> processor1;
|
||||
|
||||
@Mock
|
||||
private ItemProcessor<Object, Object> processor2;
|
||||
|
||||
private List<ItemProcessor<Object, Object>> processors;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
this.processors = new ArrayList<>();
|
||||
this.processors.add(processor1);
|
||||
this.processors.add(processor2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransform() throws Exception {
|
||||
Object item = new Object();
|
||||
Object itemAfterFirstTransfromation = new Object();
|
||||
Object itemAfterSecondTransformation = new Object();
|
||||
CompositeItemProcessor<Object, Object> composite = new CompositeItemProcessorBuilder<>()
|
||||
.delegates(this.processors).build();
|
||||
|
||||
when(processor1.process(item)).thenReturn(itemAfterFirstTransfromation);
|
||||
when(processor2.process(itemAfterFirstTransfromation)).thenReturn(itemAfterSecondTransformation);
|
||||
|
||||
assertSame(itemAfterSecondTransformation, composite.process(item));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullOrEmptyDelegates() throws Exception {
|
||||
validateExceptionMessage(new CompositeItemProcessorBuilder<>().delegates(new ArrayList<>()),
|
||||
"The delegates list must have one or more delegates.");
|
||||
validateExceptionMessage(new CompositeItemProcessorBuilder<>(),
|
||||
"A list of delegates is required.");
|
||||
}
|
||||
|
||||
private void validateExceptionMessage(CompositeItemProcessorBuilder<?, ?> builder, String message) {
|
||||
try {
|
||||
builder.build();
|
||||
fail("IllegalArgumentException should have been thrown");
|
||||
}
|
||||
catch (IllegalArgumentException iae) {
|
||||
assertEquals("IllegalArgumentException message did not match the expected result.", message,
|
||||
iae.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user