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.exception;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNull;
20  import static org.junit.Assert.fail;
21  import static org.supercsv.SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT;
22  
23  import org.junit.Test;
24  import org.supercsv.cellprocessor.ift.CellProcessor;
25  import org.supercsv.mock.IdentityTransform;
26  import org.supercsv.util.CsvContext;
27  
28  /**
29   * Tests the SuperCsvCellProcessorException class.
30   * 
31   * @author James Bassett
32   */
33  public class SuperCsvCellProcessorExceptionTest {
34  	
35  	private static final String MSG = "Cell processing failed!";
36  	private static final Throwable THROWABLE = new RuntimeException("I'm the cause of the problem");
37  	private static final CellProcessor PROCESSOR = new IdentityTransform();
38  	
39  	/**
40  	 * Tests the first constructor.
41  	 */
42  	@Test
43  	public void testConstuctor1() {
44  		SuperCsvCellProcessorException e = new SuperCsvCellProcessorException(MSG, ANONYMOUS_CSVCONTEXT, PROCESSOR);
45  		assertEquals(MSG, e.getMessage());
46  		assertEquals(ANONYMOUS_CSVCONTEXT, e.getCsvContext());
47  		assertEquals(PROCESSOR, e.getProcessor());
48  		
49  		// test with null values
50  		e = new SuperCsvCellProcessorException(null, (CsvContext) null, (CellProcessor) null);
51  		assertNull(e.getMessage());
52  		assertNull(e.getCsvContext());
53  		assertNull(e.getProcessor());
54  	}
55  	
56  	/**
57  	 * Tests the second constructor.
58  	 */
59  	@Test
60  	public void testConstuctor2() {
61  		SuperCsvCellProcessorException e = new SuperCsvCellProcessorException(MSG, ANONYMOUS_CSVCONTEXT, PROCESSOR, THROWABLE);
62  		assertEquals(MSG, e.getMessage());
63  		assertEquals(ANONYMOUS_CSVCONTEXT, e.getCsvContext());
64  		assertEquals(PROCESSOR, e.getProcessor());
65  		assertEquals(THROWABLE, e.getCause());
66  		
67  		// test with null values
68  		e = new SuperCsvCellProcessorException(null, null, null, (Throwable) null);
69  		assertNull(e.getMessage());
70  		assertNull(e.getCsvContext());
71  		assertNull(e.getProcessor());
72  		assertNull(e.getCause());
73  	}
74  	
75  	/**
76  	 * Tests the second constructor.
77  	 */
78  	@Test
79  	public void testConstuctor3() {
80  		
81  		// actual value not null
82  		SuperCsvCellProcessorException e = new SuperCsvCellProcessorException(String.class, 123, ANONYMOUS_CSVCONTEXT, PROCESSOR);
83  		assertEquals("the input value should be of type java.lang.String but is java.lang.Integer", e.getMessage());
84  		assertEquals(ANONYMOUS_CSVCONTEXT, e.getCsvContext());
85  		assertEquals(PROCESSOR, e.getProcessor());
86  		
87  		// null actual value
88  		e = new SuperCsvCellProcessorException(String.class, null, ANONYMOUS_CSVCONTEXT, PROCESSOR);
89  		assertEquals("the input value should be of type java.lang.String but is null", e.getMessage());
90  		assertEquals(ANONYMOUS_CSVCONTEXT, e.getCsvContext());
91  		assertEquals(PROCESSOR, e.getProcessor());
92  		
93  		// test with null values
94  		try {
95  			new SuperCsvCellProcessorException(null, null, null, (CellProcessor) null);
96  			fail("should have thrown NullPointerException");
97  		} catch (NullPointerException npe){
98  			assertEquals("expectedType should not be null", npe.getMessage());
99  		}
100 	}
101 	
102 	/**
103 	 * Tests the integrity of the CsvContext in a <code>SuperCsvCellProcessorException</code>
104 	 */
105 	@Test
106 	public void testCsvContext() {
107 		// This is my reference context. It is the control object to use as the reference for assertions
108 		CsvContext rc = new CsvContext(1, 2, 3);   // line, row, col
109 		
110 		// This is the test context object to use for the test. It must have the same
111 		// values as the reference context. 
112 		CsvContext tc = new CsvContext(rc.getLineNumber(), rc.getRowNumber(), rc.getColumnNumber()); 
113 		
114 		SuperCsvCellProcessorException e = new SuperCsvCellProcessorException
115 			(String.class, 123, tc, PROCESSOR);
116 
117 		// Pre-condition check
118 		assertEquals(rc, e.getCsvContext());
119 		
120 		// Test steps
121 		tc.setColumnNumber(2*rc.getColumnNumber() + 50);   // Set a column # that is different than the reference
122 		
123 		// Check that the exception still returns the context that it was created with
124 		assertEquals(rc, e.getCsvContext());
125 	}
126 	
127 }