1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.supercsv.cellprocessor;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.assertNull;
21  import static org.supercsv.SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT;
22  
23  import java.text.DecimalFormat;
24  import java.text.NumberFormat;
25  import java.util.HashMap;
26  import java.util.Locale;
27  import java.util.Map;
28  
29  import org.junit.Before;
30  import org.junit.Test;
31  import org.supercsv.cellprocessor.ift.CellProcessor;
32  import org.supercsv.exception.SuperCsvCellProcessorException;
33  import org.supercsv.mock.IdentityTransform;
34  
35  
36  
37  
38  
39  
40  
41  public class HashMapperTest {
42  	
43  	private static final String DEFAULT_VALUE = "Default";
44  	
45  	private static final Map<Object, Object> VALUE_MAP = new HashMap<Object, Object>();
46  	static {
47  		VALUE_MAP.put(1, "1");
48  		VALUE_MAP.put(2, "2");
49  		VALUE_MAP.put(3, "3");
50  	}
51  	
52  	private CellProcessor processor;
53  	private CellProcessor processor2;
54  	private CellProcessor processorChain;
55  	private CellProcessor processorChain2;
56  	
57  	
58  
59  
60  	@Before
61  	public void setUp() {
62  		processor = new HashMapper(VALUE_MAP);
63  		processor2 = new HashMapper(VALUE_MAP, DEFAULT_VALUE);
64  		processorChain = new HashMapper(VALUE_MAP, new IdentityTransform());
65  		processorChain2 = new HashMapper(VALUE_MAP, DEFAULT_VALUE, new IdentityTransform());
66  	}
67  	
68  	
69  
70  
71  	@Test
72  	public void testValidKey() {
73  		int validKey = 1;
74  		assertEquals("1", processor.execute(validKey, ANONYMOUS_CSVCONTEXT));
75  		assertEquals("1", processor2.execute(validKey, ANONYMOUS_CSVCONTEXT));
76  		assertEquals("1", processorChain.execute(validKey, ANONYMOUS_CSVCONTEXT));
77  		assertEquals("1", processorChain2.execute(validKey, ANONYMOUS_CSVCONTEXT));
78  	}
79  	
80  	
81  
82  
83  	@Test
84  	public void testInvalidKey() {
85  		int invalidKey = 4;
86  		assertFalse(VALUE_MAP.containsKey(invalidKey));
87  		
88  		
89  		assertNull(processor.execute(invalidKey, ANONYMOUS_CSVCONTEXT));
90  		assertNull(processorChain.execute(invalidKey, ANONYMOUS_CSVCONTEXT));
91  		
92  		
93  		assertEquals(DEFAULT_VALUE, processor2.execute(invalidKey, ANONYMOUS_CSVCONTEXT));
94  		assertEquals(DEFAULT_VALUE, processorChain2.execute(invalidKey, ANONYMOUS_CSVCONTEXT));
95  	}
96  	
97  	
98  
99  
100 	@Test
101 	public void testChainedToLongCellProcessor() {
102 		final String input = "one million";
103 		final Map<Object, Object> map = new HashMap<Object, Object>();
104 		map.put(input, 1000000);
105 		final DecimalFormat format = (DecimalFormat) NumberFormat.getNumberInstance(Locale.UK);
106 		format.applyPattern("###,###,###");
107 		
108 		final CellProcessor p = new HashMapper(map, new FmtNumber(format));
109 		assertEquals("1,000,000", p.execute(input, ANONYMOUS_CSVCONTEXT));
110 	}
111 	
112 	
113 
114 
115 	@Test(expected = SuperCsvCellProcessorException.class)
116 	public void testWithNull() {
117 		processor.execute(null, ANONYMOUS_CSVCONTEXT);
118 	}
119 	
120 	
121 
122 
123 	@Test(expected = NullPointerException.class)
124 	public void testConstructionWithNullMap() {
125 		new HashMapper(null, DEFAULT_VALUE);
126 	}
127 	
128 	
129 
130 
131 	@Test(expected = NullPointerException.class)
132 	public void testChainedConstructionWithNullMap() {
133 		new HashMapper(null, DEFAULT_VALUE, new IdentityTransform());
134 	}
135 	
136 	
137 
138 
139 	@Test(expected = IllegalArgumentException.class)
140 	public void testConstructionWithEmptyMap() {
141 		new HashMapper(new HashMap<Object, Object>(), DEFAULT_VALUE);
142 	}
143 	
144 	
145 
146 
147 	@Test(expected = IllegalArgumentException.class)
148 	public void testChainedConstructionWithEmptyMap() {
149 		new HashMapper(new HashMap<Object, Object>(), DEFAULT_VALUE, new IdentityTransform());
150 	}
151 	
152 }