~ubuntu-branches/ubuntu/quantal/commons-csv/quantal

« back to all changes in this revision

Viewing changes to src/test/org/apache/commons/csv/CSVStrategyTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Jan-Pascal van Best
  • Date: 2007-07-27 09:45:30 UTC
  • Revision ID: james.westby@ubuntu.com-20070727094530-iy6ls22i7yj3p0sg
Tags: upstream-0.1-SNAPSHOT+svn558885
ImportĀ upstreamĀ versionĀ 0.1-SNAPSHOT+svn558885

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
8
 * 
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 * 
 
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.
 
16
 */
 
17
package org.apache.commons.csv;
 
18
 
 
19
import java.io.IOException;
 
20
import java.io.Reader;
 
21
import java.io.StringReader;
 
22
import java.util.Arrays;
 
23
 
 
24
import junit.framework.Test;
 
25
import junit.framework.TestCase;
 
26
import junit.framework.TestSuite;
 
27
 
 
28
/**
 
29
 * CSVStrategyTest
 
30
 *
 
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
 
35
 * has problems...).
 
36
 */
 
37
public class CSVStrategyTest extends TestCase {
 
38
  
 
39
  /**
 
40
   * Constructor for JUnit.
 
41
   * @param name Name to be used in JUnit Test Environment
 
42
   */
 
43
  public CSVStrategyTest(String name) {
 
44
    super(name);
 
45
  }
 
46
 
 
47
  /**
 
48
   * Returns a Test suite for JUnit.
 
49
   * @return Test suite for JUnit
 
50
   */
 
51
  public static Test suite() {
 
52
    return new TestSuite(CSVStrategyTest.class);
 
53
  }
 
54
 
 
55
 
 
56
  // ======================================================
 
57
  //   getters / setters
 
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(), '!');
 
66
  }
 
67
 
 
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(), '\'');
 
75
  }
 
76
 
 
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');
 
86
  }
 
87
 
 
88
  public void testSetCSVStrategy() {
 
89
    CSVParser parser = new CSVParser(new StringReader("hello world"));
 
90
    CSVStrategy strategy = parser.getStrategy();
 
91
    // default settings
 
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());
 
106
  }
 
107
  
 
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());
 
116
  }
 
117
  
 
118