View Javadoc
1   /*
2    * Copyright 2007 Kasper B. Graversen
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.supercsv.cellprocessor;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertTrue;
20  import static org.supercsv.SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT;
21  
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.Date;
25  import java.util.HashSet;
26  import java.util.List;
27  import java.util.Set;
28  
29  import org.junit.Before;
30  import org.junit.Test;
31  import org.supercsv.mock.IdentityTransform;
32  
33  /**
34   * Tests the Collector processor.
35   * 
36   * @author James Bassett
37   */
38  public class CollectorTest {
39  	
40  	private Collector listCollector;
41  	private Collector setCollector;
42  	private Collector chainedCollector;
43  	private List<Object> list;
44  	private Set<Object> set;
45  	private List<Object> chainedList;
46  	private List<Object> input = Arrays.asList(new Object[] { "one", null, 2, 3.0, 2, null, true, new Date() });
47  	private final int duplicates = 2;
48  	
49  	/**
50  	 * Sets up the Collectors.
51  	 */
52  	@Before
53  	public void setUp() {
54  		list = new ArrayList<Object>();
55  		set = new HashSet<Object>();
56  		chainedList = new ArrayList<Object>();
57  		listCollector = new Collector(list);
58  		setCollector = new Collector(set);
59  		chainedCollector = new Collector(chainedList, new IdentityTransform());
60  	}
61  	
62  	/**
63  	 * Tests a Collector with a List.
64  	 */
65  	@Test
66  	public void testListCollector() {
67  		
68  		for( Object o : input ) {
69  			listCollector.execute(o, ANONYMOUS_CSVCONTEXT);
70  		}
71  		
72  		assertTrue(listCollector.getCollection() == list);
73  		assertEquals(input.size(), list.size());
74  		for( int i = 0; i < input.size(); i++ ) {
75  			assertEquals(input.get(i), list.get(i));
76  		}
77  		
78  	}
79  	
80  	/**
81  	 * Tests a Collector with a Set.
82  	 */
83  	@Test
84  	public void testSetCollector() {
85  		
86  		for( Object o : input ) {
87  			setCollector.execute(o, ANONYMOUS_CSVCONTEXT);
88  		}
89  		
90  		assertTrue(setCollector.getCollection() == set);
91  		assertEquals(input.size() - duplicates, set.size());
92  		for( Object o : input ) {
93  			assertTrue(set.contains(o));
94  		}
95  		
96  	}
97  	
98  	/**
99  	 * Tests a Collector with a List, chained to another processor.
100 	 */
101 	@Test
102 	public void testChainedCollector() {
103 		
104 		for( Object o : input ) {
105 			chainedCollector.execute(o, ANONYMOUS_CSVCONTEXT);
106 		}
107 		
108 		assertTrue(chainedCollector.getCollection() == chainedList);
109 		assertEquals(input.size(), chainedList.size());
110 		for( int i = 0; i < input.size(); i++ ) {
111 			assertEquals(input.get(i), chainedList.get(i));
112 		}
113 		
114 	}
115 	
116 	/**
117 	 * Tests the 1 arg constructor with a null collection.
118 	 */
119 	@Test(expected = NullPointerException.class)
120 	public void testConstructorWithNullCollection() {
121 		new Collector(null);
122 	}
123 	
124 	/**
125 	 * Tests the 2 arg constructor with a null collection.
126 	 */
127 	@Test(expected = NullPointerException.class)
128 	public void testChainedConstructorWithNullCollection() {
129 		new Collector(null, new IdentityTransform());
130 	}
131 	
132 }