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.supercsv.SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT;
20  
21  import org.junit.Before;
22  import org.junit.Test;
23  import org.supercsv.cellprocessor.ift.CellProcessor;
24  import org.supercsv.exception.SuperCsvCellProcessorException;
25  import org.supercsv.mock.IdentityTransform;
26  
27  /**
28   * Tests the Truncate processor.
29   * 
30   * @author Kasper B. Graversen
31   * @author James Bassett
32   */
33  public class TruncateTest {
34  	
35  	private static final int MAX_SIZE = 3;
36  	private static final String SUFFIX = "...";
37  	
38  	private CellProcessor processor;
39  	private CellProcessor processor2;
40  	private CellProcessor processorChain;
41  	private CellProcessor processorChain2;
42  	
43  	/**
44  	 * Sets up the processors for the test using all constructor combinations.
45  	 */
46  	@Before
47  	public void setUp() {
48  		processor = new Truncate(MAX_SIZE);
49  		processor2 = new Truncate(MAX_SIZE, SUFFIX);
50  		processorChain = new Truncate(MAX_SIZE, new IdentityTransform());
51  		processorChain2 = new Truncate(MAX_SIZE, SUFFIX, new IdentityTransform());
52  	}
53  	
54  	/**
55  	 * Tests unchained/chained execution with input the same as the max size (no truncating!).
56  	 */
57  	@Test
58  	public void testInputSameAsMax() {
59  		String input = "abc";
60  		assertEquals(input, processor.execute(input, ANONYMOUS_CSVCONTEXT));
61  		assertEquals(input, processor2.execute(input, ANONYMOUS_CSVCONTEXT));
62  		assertEquals(input, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
63  		assertEquals(input, processorChain2.execute(input, ANONYMOUS_CSVCONTEXT));
64  	}
65  	
66  	/**
67  	 * Tests unchained/chained execution with input longer than the max size (truncating!).
68  	 */
69  	@Test
70  	public void testInputLongerThanMax() {
71  		String input = "abcd";
72  		String expected = "abc";
73  		String expectedSuffix = expected + "...";
74  		
75  		// no suffix
76  		assertEquals(expected, processor.execute(input, ANONYMOUS_CSVCONTEXT));
77  		assertEquals(expected, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
78  		
79  		// suffix
80  		assertEquals(expectedSuffix, processor2.execute(input, ANONYMOUS_CSVCONTEXT));
81  		assertEquals(expectedSuffix, processorChain2.execute(input, ANONYMOUS_CSVCONTEXT));
82  	}
83  	
84  	/**
85  	 * Tests execution with an invalid max value (should throw an Exception).
86  	 */
87  	@Test(expected = IllegalArgumentException.class)
88  	public void testWithInvalidMax() {
89  		processor = new Truncate(0);
90  	}
91  	
92  	/**
93  	 * Tests execution with an invalid max value and chaining (should throw an Exception).
94  	 */
95  	@Test(expected = IllegalArgumentException.class)
96  	public void testWithInvalidMaxChained() {
97  		processorChain = new Truncate(0, new IdentityTransform());
98  	}
99  	
100 	/**
101 	 * Tests execution with a null input (should throw an Exception).
102 	 */
103 	@Test(expected = SuperCsvCellProcessorException.class)
104 	public void testWithNull() {
105 		processor.execute(null, ANONYMOUS_CSVCONTEXT);
106 	}
107 	
108 	/**
109 	 * Tests construction with a null suffix String (should throw an Exception).
110 	 */
111 	@Test(expected = NullPointerException.class)
112 	public void testConstructionWithNullSuffix() {
113 		new Truncate(MAX_SIZE, (String) null);
114 	}
115 	
116 }