View Javadoc

1   package org.springframework.batch.item.support;
2   
3   import static org.easymock.EasyMock.createMock;
4   import static org.easymock.EasyMock.expect;
5   import static org.easymock.EasyMock.replay;
6   import static org.easymock.EasyMock.verify;
7   import static org.junit.Assert.assertSame;
8   import static org.junit.Assert.fail;
9   
10  import java.util.ArrayList;
11  
12  import org.junit.Assert;
13  import org.junit.Before;
14  import org.junit.Test;
15  import org.springframework.batch.item.ItemProcessor;
16  import org.springframework.batch.item.support.CompositeItemProcessor;
17  
18  /**
19   * Tests for {@link CompositeItemProcessor}.
20   * 
21   * @author Robert Kasanicky
22   */
23  public class CompositeItemProcessorTests {
24  
25  	private CompositeItemProcessor<Object, Object> composite = new CompositeItemProcessor<Object, Object>();
26  	
27  	private ItemProcessor<Object, Object> processor1;
28  	private ItemProcessor<Object, Object> processor2;
29  	
30  	@SuppressWarnings("unchecked")
31  	@Before
32  	public void setUp() throws Exception {
33  		processor1 = createMock(ItemProcessor.class);
34  		processor2 = createMock(ItemProcessor.class);
35  		
36  		composite.setDelegates(new ArrayList<ItemProcessor<Object,Object>>() {{ 
37  			add(processor1); add(processor2); 
38  		}});
39  		
40  		composite.afterPropertiesSet();
41  	}
42  	
43  	/**
44  	 * Regular usage scenario - item is passed through the processing chain,
45  	 * return value of the of the last transformation is returned by the composite.
46  	 */
47  	@Test
48  	public void testTransform() throws Exception {
49  		Object item = new Object();
50  		Object itemAfterFirstTransfromation = new Object();
51  		Object itemAfterSecondTransformation = new Object();
52  
53  		expect(processor1.process(item)).andReturn(itemAfterFirstTransfromation);
54  		
55  		expect(processor2.process(itemAfterFirstTransfromation)).andReturn(itemAfterSecondTransformation);
56  		
57  		replay(processor1);
58  		replay(processor2);
59  		
60  		assertSame(itemAfterSecondTransformation, composite.process(item));
61  
62  		verify(processor1);
63  		verify(processor2);
64  	}
65  	
66  	/**
67  	 * The list of transformers must not be null or empty and 
68  	 * can contain only instances of {@link ItemProcessor}.
69  	 */
70  	@Test
71  	public void testAfterPropertiesSet() throws Exception {
72  		
73  		// value not set
74  		composite.setDelegates(null);
75  		try {
76  			composite.afterPropertiesSet();
77  			fail();
78  		}
79  		catch (IllegalArgumentException e) {
80  			// expected
81  		}
82  		
83  		// empty list
84  		composite.setDelegates(new ArrayList<ItemProcessor<Object,Object>>());
85  		try {
86  			composite.afterPropertiesSet();
87  			fail();
88  		}
89  		catch (IllegalArgumentException e) {
90  			// expected
91  		}
92  		
93  	}
94  	
95  	@Test
96  	public void testFilteredItemInFirstProcessor() throws Exception{
97  		
98  		Object item = new Object();
99  		expect(processor1.process(item)).andReturn(null);
100 		replay(processor1, processor2);
101 		Assert.assertEquals(null,composite.process(item));
102 		verify(processor1,processor2);
103 	}
104 }