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.constraint;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  import static org.supercsv.SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT;
23  
24  import java.util.regex.PatternSyntaxException;
25  
26  import org.junit.Before;
27  import org.junit.Test;
28  import org.supercsv.cellprocessor.ift.CellProcessor;
29  import org.supercsv.exception.SuperCsvCellProcessorException;
30  import org.supercsv.exception.SuperCsvConstraintViolationException;
31  import org.supercsv.mock.IdentityTransform;
32  
33  /**
34   * Tests the StrRegEx constraint.
35   * 
36   * @author James Bassett
37   */
38  public class StrRegExTest {
39  	
40  	private static final String REGEX = "\\$[0-9]+\\.[0-9]{2}";
41  	private static final String MSG = "Must be a valid dollar amount, e.g. $123.45";
42  	
43  	private CellProcessor processor;
44  	private CellProcessor processorChain;
45  	
46  	/**
47  	 * Sets up the processors for the test using all constructor combinations.
48  	 */
49  	@Before
50  	public void setUp() {
51  		processor = new StrRegEx(REGEX);
52  		processorChain = new StrRegEx(REGEX, new IdentityTransform());
53  		StrRegEx.registerMessage(REGEX, MSG);
54  	}
55  	
56  	/**
57  	 * Tests unchained/chained execution with a String matching the regex.
58  	 */
59  	@Test
60  	public void testValidInput() {
61  		String input = "$123.45";
62  		assertEquals(input, processor.execute(input, ANONYMOUS_CSVCONTEXT));
63  		assertEquals(input, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
64  	}
65  	
66  	/**
67  	 * Tests execution with input that doesn't match the regex.
68  	 */
69  	@Test
70  	public void testInvalidInput() {
71  		String input = "12345";
72  		try {
73  			processor.execute(input, ANONYMOUS_CSVCONTEXT);
74  			fail("should have thrown SuperCsvConstraintViolationException");
75  		}
76  		catch(SuperCsvConstraintViolationException e) {
77  			// exception msg should contain registered message
78  			assertTrue(e.getMessage().contains(MSG));
79  		}
80  	}
81  	
82  	/**
83  	 * Tests execution with input that doesn't match the regex (and no message is registered).
84  	 */
85  	@Test
86  	public void testInvalidInputWithNoMessage() {
87  		processor = new StrRegEx("\\s"); // only whitespace
88  		String input = "12345";
89  		try {
90  			processor.execute(input, ANONYMOUS_CSVCONTEXT);
91  			fail("should have thrown SuperCsvConstraintViolationException");
92  		}
93  		catch(SuperCsvConstraintViolationException e) {
94  			// exception msg should not contain the registered message (it's a different regex)
95  			assertFalse(e.getMessage().contains(MSG));
96  		}
97  	}
98  	
99  	/**
100 	 * Tests construction of the processor with a null regex (should throw an Exception).
101 	 */
102 	@Test(expected = NullPointerException.class)
103 	public void testWithNullRegex() {
104 		new StrRegEx(null);
105 	}
106 	
107 	/**
108 	 * Tests construction of the processor with an empty regex (should throw an Exception).
109 	 */
110 	@Test(expected = IllegalArgumentException.class)
111 	public void testWithEmptyRegex() {
112 		new StrRegEx("");
113 	}
114 	
115 	/**
116 	 * Tests construction of the processor with an invalid regex (should throw an Exception).
117 	 */
118 	@Test(expected = PatternSyntaxException.class)
119 	public void testWithInvalidRegex() {
120 		new StrRegEx("*****");
121 	}
122 	
123 	/**
124 	 * Tests execution with a null input (should throw an Exception).
125 	 */
126 	@Test(expected = SuperCsvCellProcessorException.class)
127 	public void testWithNull() {
128 		processor.execute(null, ANONYMOUS_CSVCONTEXT);
129 	}
130 	
131 }