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.fail;
20  import static org.supercsv.SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT;
21  
22  import java.util.Arrays;
23  
24  import org.junit.Before;
25  import org.junit.Test;
26  import org.supercsv.cellprocessor.ift.CellProcessor;
27  import org.supercsv.exception.SuperCsvCellProcessorException;
28  import org.supercsv.mock.IdentityTransform;
29  
30  /**
31   * Tests the ParseEnum processor.
32   * 
33   * @author James Bassett
34   */
35  public class ParseEnumTest {
36  	
37  	public enum TestEnum {
38  		NORMAL, lowercase, MixedCase
39  	}
40  	
41  	private CellProcessor processor;
42  	private CellProcessor processorChain;
43  	private CellProcessor ignoreCaseProcessor;
44  	private CellProcessor ignoreCaseProcessorChain;
45  	
46  	/**
47  	 * Sets up the processors for the test using all constructor combinations.
48  	 */
49  	@Before
50  	public void setUp() {
51  		processor = new ParseEnum(TestEnum.class);
52  		processorChain = new ParseEnum(TestEnum.class, new IdentityTransform());
53  		ignoreCaseProcessor = new ParseEnum(TestEnum.class, true);
54  		ignoreCaseProcessorChain = new ParseEnum(TestEnum.class, true, new IdentityTransform());
55  	}
56  	
57  	/**
58  	 * Tests that a normal enum is parsed correctly.
59  	 */
60  	@Test
61  	public void testNormalEnum() {
62  		final String input = "NORMAL";
63  		assertEquals(TestEnum.NORMAL, processor.execute(input, ANONYMOUS_CSVCONTEXT));
64  		assertEquals(TestEnum.NORMAL, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
65  		assertEquals(TestEnum.NORMAL, ignoreCaseProcessor.execute(input, ANONYMOUS_CSVCONTEXT));
66  		assertEquals(TestEnum.NORMAL, ignoreCaseProcessorChain.execute(input, ANONYMOUS_CSVCONTEXT));
67  	}
68  	
69  	/**
70  	 * Tests that a lowercase enum is parsed correctly.
71  	 */
72  	@Test
73  	public void testLowerCaseEnum() {
74  		final String input = "lowercase";
75  		assertEquals(TestEnum.lowercase, processor.execute(input, ANONYMOUS_CSVCONTEXT));
76  		assertEquals(TestEnum.lowercase, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
77  		assertEquals(TestEnum.lowercase, ignoreCaseProcessor.execute(input, ANONYMOUS_CSVCONTEXT));
78  		assertEquals(TestEnum.lowercase, ignoreCaseProcessorChain.execute(input, ANONYMOUS_CSVCONTEXT));
79  	}
80  	
81  	/**
82  	 * Tests that a mixed case enum is parsed correctly.
83  	 */
84  	@Test
85  	public void testMixedCaseEnum() {
86  		final String input = "MixedCase";
87  		assertEquals(TestEnum.MixedCase, processor.execute(input, ANONYMOUS_CSVCONTEXT));
88  		assertEquals(TestEnum.MixedCase, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
89  		assertEquals(TestEnum.MixedCase, ignoreCaseProcessor.execute(input, ANONYMOUS_CSVCONTEXT));
90  		assertEquals(TestEnum.MixedCase, ignoreCaseProcessorChain.execute(input, ANONYMOUS_CSVCONTEXT));
91  	}
92  	
93  	/**
94  	 * Tests that an input with a different case fails when ignoreCase is false.
95  	 */
96  	@Test
97  	public void testDifferentCaseFailsWhenIgnoreCaseFalse() {
98  		final String input = "normal";
99  		for( CellProcessor p : Arrays.asList(processor, processorChain) ) {
100 			try {
101 				p.execute(input, ANONYMOUS_CSVCONTEXT);
102 				fail("should have thrown SuperCsvCellProcessorException");
103 			}
104 			catch(SuperCsvCellProcessorException e) {
105 				assertEquals("'normal' could not be parsed as a enum of type " + TestEnum.class.getName(),
106 					e.getMessage());
107 			}
108 		}
109 	}
110 	
111 	/**
112 	 * Tests that an input with a different case parses successfully when ignoreCase is true.
113 	 */
114 	@Test
115 	public void testDifferentCaseParsesWhenIgnoreCaseTrue() {
116 		final String input = "normal";
117 		for( CellProcessor p : Arrays.asList(ignoreCaseProcessor, ignoreCaseProcessorChain) ) {
118 			assertEquals(TestEnum.NORMAL, p.execute(input, ANONYMOUS_CSVCONTEXT));
119 		}
120 	}
121 	
122 	/**
123 	 * Tests execution with a null input (should throw an Exception).
124 	 */
125 	@Test(expected = SuperCsvCellProcessorException.class)
126 	public void testWithNull() {
127 		processor.execute(null, ANONYMOUS_CSVCONTEXT);
128 	}
129 	
130 	/**
131 	 * Tests the first constructor with a null enum class.
132 	 */
133 	@Test(expected = NullPointerException.class)
134 	public void testFirstConstructorsWithNullEnumClass(){
135 		new ParseEnum(null);
136 	}
137 	
138 	/**
139 	 * Tests the second constructor with a null enum class.
140 	 */
141 	@Test(expected = NullPointerException.class)
142 	public void testSecondConstructorsWithNullEnumClass(){
143 		new ParseEnum(null, false);
144 	}
145 	
146 	/**
147 	 * Tests the third constructor with a null enum class.
148 	 */
149 	@Test(expected = NullPointerException.class)
150 	public void testThirdConstructorsWithNullEnumClass(){
151 		new ParseEnum(null, new IdentityTransform());
152 	}
153 	
154 	/**
155 	 * Tests the fourth constructor with a null enum class.
156 	 */
157 	@Test(expected = NullPointerException.class)
158 	public void testFourthConstructorsWithNullEnumClass(){
159 		new ParseEnum(null, false, new IdentityTransform());
160 	}
161 }