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.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   * Tests the HashMapper processor.
37   * 
38   * @author Dominique De Vito
39   * @author James Bassett
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  	 * Sets up the processors for the test using all constructor combinations.
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  	 * Tests chained/unchained execution with a valid key from the map.
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  	 * Tests chained/unchained execution with a key not in the map.
82  	 */
83  	@Test
84  	public void testInvalidKey() {
85  		int invalidKey = 4;
86  		assertFalse(VALUE_MAP.containsKey(invalidKey));
87  		
88  		// no default values
89  		assertNull(processor.execute(invalidKey, ANONYMOUS_CSVCONTEXT));
90  		assertNull(processorChain.execute(invalidKey, ANONYMOUS_CSVCONTEXT));
91  		
92  		// with default values
93  		assertEquals(DEFAULT_VALUE, processor2.execute(invalidKey, ANONYMOUS_CSVCONTEXT));
94  		assertEquals(DEFAULT_VALUE, processorChain2.execute(invalidKey, ANONYMOUS_CSVCONTEXT));
95  	}
96  	
97  	/**
98  	 * Tests that other kinds of processors (e.g. LongCellProcessors) can be chained to HashMapper.
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 	 * Tests execution with a null input (should throw an Exception).
114 	 */
115 	@Test(expected = SuperCsvCellProcessorException.class)
116 	public void testWithNull() {
117 		processor.execute(null, ANONYMOUS_CSVCONTEXT);
118 	}
119 	
120 	/**
121 	 * Tests construction with a null Map (should throw an Exception).
122 	 */
123 	@Test(expected = NullPointerException.class)
124 	public void testConstructionWithNullMap() {
125 		new HashMapper(null, DEFAULT_VALUE);
126 	}
127 	
128 	/**
129 	 * Tests chained execution with a null Map (should throw an Exception).
130 	 */
131 	@Test(expected = NullPointerException.class)
132 	public void testChainedConstructionWithNullMap() {
133 		new HashMapper(null, DEFAULT_VALUE, new IdentityTransform());
134 	}
135 	
136 	/**
137 	 * Tests construction with an empty Map (should throw an Exception).
138 	 */
139 	@Test(expected = IllegalArgumentException.class)
140 	public void testConstructionWithEmptyMap() {
141 		new HashMapper(new HashMap<Object, Object>(), DEFAULT_VALUE);
142 	}
143 	
144 	/**
145 	 * Tests chained execution with an empty Map (should throw an Exception).
146 	 */
147 	@Test(expected = IllegalArgumentException.class)
148 	public void testChainedConstructionWithEmptyMap() {
149 		new HashMapper(new HashMap<Object, Object>(), DEFAULT_VALUE, new IdentityTransform());
150 	}
151 	
152 }