2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.commons.csv;
19
import java.io.IOException;
20
import java.io.Reader;
21
import java.io.StringReader;
22
import java.util.Arrays;
24
import junit.framework.Test;
25
import junit.framework.TestCase;
26
import junit.framework.TestSuite;
31
* The test are organized in three different sections:
32
* The 'setter/getter' section, the lexer section and finally the strategy
33
* section. In case a test fails, you should follow a top-down approach for
34
* fixing a potential bug (its likely that the strategy itself fails if the lexer
37
public class CSVStrategyTest extends TestCase {
40
* Constructor for JUnit.
41
* @param name Name to be used in JUnit Test Environment
43
public CSVStrategyTest(String name) {
48
* Returns a Test suite for JUnit.
49
* @return Test suite for JUnit
51
public static Test suite() {
52
return new TestSuite(CSVStrategyTest.class);
56
// ======================================================
58
// ======================================================
59
public void testGetSetCommentStart() {
60
CSVParser parser = new CSVParser(new StringReader("hello world"));
61
CSVStrategy strategy = parser.getStrategy();
62
strategy.setCommentStart('#');
63
assertEquals(strategy.getCommentStart(), '#');
64
strategy.setCommentStart('!');
65
assertEquals(strategy.getCommentStart(), '!');
68
public void testGetSetEncapsulator() {
69
CSVParser parser = new CSVParser(new StringReader("hello world"));
70
CSVStrategy strategy = parser.getStrategy();
71
strategy.setEncapsulator('"');
72
assertEquals(strategy.getEncapsulator(), '"');
73
strategy.setEncapsulator('\'');
74
assertEquals(strategy.getEncapsulator(), '\'');
77
public void testGetSetDelimiter() {
78
CSVParser parser = new CSVParser(new StringReader("hello world"));
79
CSVStrategy strategy = parser.getStrategy();
80
strategy.setDelimiter(';');
81
assertEquals(strategy.getDelimiter(), ';');
82
strategy.setDelimiter(',');
83
assertEquals(strategy.getDelimiter(), ',');
84
strategy.setDelimiter('\t');
85
assertEquals(strategy.getDelimiter(), '\t');
88
public void testSetCSVStrategy() {
89
CSVParser parser = new CSVParser(new StringReader("hello world"));
90
CSVStrategy strategy = parser.getStrategy();
92
assertEquals(strategy.getDelimiter(), ',');
93
assertEquals(strategy.getEncapsulator(), '"');
94
assertEquals(strategy.getCommentStart(), '\0');
95
assertEquals(true, strategy.getIgnoreLeadingWhitespaces());
96
assertEquals(false, strategy.getUnicodeEscapeInterpretation());
97
assertEquals(true, strategy.getIgnoreEmptyLines());
98
// explicit csv settings
99
parser.setStrategy(CSVStrategy.DEFAULT_STRATEGY);
100
assertEquals(strategy.getDelimiter(), ',');
101
assertEquals(strategy.getEncapsulator(), '"');
102
assertEquals(strategy.getCommentStart(), '\0');
103
assertEquals(true, strategy.getIgnoreLeadingWhitespaces());
104
assertEquals(false, strategy.getUnicodeEscapeInterpretation());
105
assertEquals(true, strategy.getIgnoreEmptyLines());
108
public void testSetExcelStrategy() {
109
CSVStrategy strategy = CSVStrategy.EXCEL_STRATEGY;
110
assertEquals(strategy.getDelimiter(), ',');
111
assertEquals(strategy.getEncapsulator(), '"');
112
assertEquals(strategy.getCommentStart(), '\0');
113
assertEquals(false, strategy.getIgnoreLeadingWhitespaces());
114
assertEquals(false, strategy.getUnicodeEscapeInterpretation());
115
assertEquals(false, strategy.getIgnoreEmptyLines());