You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ba...@apache.org on 2006/06/19 15:01:39 UTC
svn commit: r415323 - in /jakarta/commons/sandbox/csv/trunk/src:
java/org/apache/commons/csv/ test/org/apache/commons/csv/
Author: bayard
Date: Mon Jun 19 06:01:39 2006
New Revision: 415323
URL: http://svn.apache.org/viewvc?rev=415323&view=rev
Log:
Pulled some static methods into a CSVUtils class, switched CSVPrinter to use a CSVStrategy though it doesn't use it fully yet, added a COMMENTS_DISABLED constant instead of relying on (char) 0.
Added:
jakarta/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVUtils.java
jakarta/commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVUtilsTest.java
Modified:
jakarta/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVParser.java
jakarta/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVPrinter.java
jakarta/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVStrategy.java
jakarta/commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVParserTest.java
jakarta/commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVPrinterTest.java
Modified: jakarta/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVParser.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVParser.java?rev=415323&r1=415322&r2=415323&view=diff
==============================================================================
--- jakarta/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVParser.java (original)
+++ jakarta/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVParser.java Mon Jun 19 06:01:39 2006
@@ -91,53 +91,6 @@
}
// ======================================================
- // static parsers
- // ======================================================
-
- /**
- * Parses the given String according to the default CSV strategy.
- *
- * @param s CSV String to be parsed.
- * @return parsed String matrix (which is never null)
- * @throws IOException in case of error
- * @see #setStrategy()
- */
- public static String[][] parse(String s) throws IOException {
- if (s == null) {
- throw new IllegalArgumentException("Null argument not allowed.");
- }
- String[][] result = (new CSVParser(new StringReader(s))).getAllValues();
- if (result == null) {
- // since CSVStrategy ignores empty lines an empty array is returned
- // (i.e. not "result = new String[][] {{""}};")
- result = new String[0][0];
- }
- return result;
- }
-
- /**
- * Parses the first line only according to the default CSV strategy.
- *
- * Parsing empty string will be handled as valid records containing zero
- * elements, so the following property holds: parseLine("").length == 0.
- *
- * @param s CSV String to be parsed.
- * @return parsed String vector (which is never null)
- * @throws IOException in case of error
- * @see #setStrategy()
- */
- public static String[] parseLine(String s) throws IOException {
- if (s == null) {
- throw new IllegalArgumentException("Null argument not allowed.");
- }
- // uh,jh: make sure that parseLine("").length == 0
- if (s.length() == 0) {
- return new String[0];
- }
- return (new CSVParser(new StringReader(s))).getLine();
- }
-
- // ======================================================
// the constructor
// ======================================================
@@ -366,7 +319,7 @@
eol = isEndOfLine(c);
}
// ok, start of token reached: comment, encapsulated, or token
- if (c == strategy.getCommentStart()) {
+ if (!strategy.isCommentingDisabled() && c == strategy.getCommentStart()) {
// ignore everything till end of line and continue (incr linecount)
in.readLine();
tkn = nextToken();
@@ -602,7 +555,7 @@
// ======================================================
/**
- * @return true if the given char is a whitespache character
+ * @return true if the given char is a whitespace character
*/
private boolean isWhitespace(int c) {
return Character.isWhitespace((char) c);
Modified: jakarta/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVPrinter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVPrinter.java?rev=415323&r1=415322&r2=415323&view=diff
==============================================================================
--- jakarta/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVPrinter.java (original)
+++ jakarta/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVPrinter.java Mon Jun 19 06:01:39 2006
@@ -31,11 +31,7 @@
/** True if we just began a new line. */
protected boolean newLine = true;
- /** Character used to start comments. (Default is '#') */
- protected char commentStart = '#';
-
- /** Character used to separate entities. (Default is ',') */
- protected char separatorChar = ',';
+ private CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY;
/**
* Create a printer that will print values to the given
@@ -66,67 +62,33 @@
}
- /**
- * Create a printer that will print values to the given
- * stream. Character to byte conversion is done using
- * the default character encoding.
- *
- * @param out stream to which to print.
- * @param commentStart Character used to start comments.
- */
- public CSVPrinter(OutputStream out, char commentStart) {
- this(out);
- this.commentStart = commentStart;
- }
-
-
- /**
- * Create a printer that will print values to the given
- * stream.
- *
- * @param out stream to which to print.
- * @param commentStart Character used to start comments.
- */
- public CSVPrinter(Writer out, char commentStart) {
- this(out);
- this.commentStart = commentStart;
- }
-
- /**
- * Gets the comment start character.
- *
- * @return the commentStart character
- */
- public char getCommentStart() {
- return commentStart;
- }
+ // ======================================================
+ // strategies
+ // ======================================================
/**
- * Sets the comment start character.
- *
- * @param commentStart commentStart character to set.
+ * Sets the specified CSV Strategy
+ *
+ * @return current instance of CSVParser to allow chained method calls
*/
- public void setCommentStart(char commentStart) {
- this.commentStart = commentStart;
+ public CSVPrinter setStrategy(CSVStrategy strategy) {
+ this.strategy = strategy;
+ return this;
}
/**
- * Gets the separator character.
- *
- * @return Returns the separatorChar.
- */
- public char getSeparatorChar() {
- return separatorChar;
- }
- /**
- * Sets the separator character.
+ * Obtain the specified CSV Strategy
*
- * @param separatorChar The separatorChar to set.
+ * @return strategy currently being used
*/
- public void setSeparatorChar(char separatorChar) {
- this.separatorChar = separatorChar;
+ public CSVStrategy getStrategy() {
+ return this.strategy;
}
+ // ======================================================
+ // printing implementation
+ // ======================================================
+
/**
* Print the string as the last value on the line. The value
* will be quoted if needed.
@@ -197,10 +159,13 @@
* @param comment the comment to output
*/
public void printlnComment(String comment) {
+ if(this.strategy.isCommentingDisabled()) {
+ return;
+ }
if (!newLine) {
out.println();
}
- out.print(commentStart);
+ out.print(this.strategy.getCommentStart());
out.print(' ');
for (int i = 0; i < comment.length(); i++) {
char c = comment.charAt(i);
@@ -212,7 +177,7 @@
// break intentionally excluded.
case '\n' :
out.println();
- out.print(commentStart);
+ out.print(this.strategy.getCommentStart());
out.print(' ');
break;
default :
@@ -248,7 +213,7 @@
}
for (int i = 0; i < value.length(); i++) {
c = value.charAt(i);
- if (c == '"' || c == separatorChar || c == '\n' || c == '\r') {
+ if (c == '"' || c == this.strategy.getDelimiter() || c == '\n' || c == '\r') {
quote = true;
}
}
@@ -265,7 +230,7 @@
if (newLine) {
newLine = false;
} else {
- out.print(separatorChar);
+ out.print(this.strategy.getDelimiter());
}
if (quote) {
out.print(escapeAndQuote(value));
@@ -273,40 +238,6 @@
out.print(value);
}
out.flush();
- }
-
-
- /**
- * Converts an array of string values into a single CSV line. All
- * <code>null</code> values are converted to the string <code>"null"</code>,
- * all strings equal to <code>"null"</code> will additionally get quotes
- * around.
- *
- * @param values the value array
- * @return the CSV string, will be an empty string if the length of the
- * value array is 0
- */
- public static String printLine(String[] values) {
-
- // set up a CSVPrinter
- StringWriter csvWriter = new StringWriter();
- CSVPrinter csvPrinter = new CSVPrinter(csvWriter);
-
- // check for null values an "null" as strings and convert them
- // into the strings "null" and "\"null\""
- for (int i = 0; i < values.length; i++) {
- if (values[i] == null) {
- values[i] = "null";
- } else if (values[i].equals("null")) {
- values[i] = "\"null\"";
- }
- }
-
- // convert to CSV
- csvPrinter.println(values);
-
- // as the resulting string has \r\n at the end, we will trim that away
- return csvWriter.toString().trim();
}
Modified: jakarta/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVStrategy.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVStrategy.java?rev=415323&r1=415322&r2=415323&view=diff
==============================================================================
--- jakarta/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVStrategy.java (original)
+++ jakarta/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVStrategy.java Mon Jun 19 06:01:39 2006
@@ -29,8 +29,11 @@
private boolean interpretUnicodeEscapes;
private boolean ignoreEmptyLines;
- public static CSVStrategy DEFAULT_STRATEGY = new CSVStrategy(',', '"', (char) 0, true, false, true);
- public static CSVStrategy EXCEL_STRATEGY = new CSVStrategy(';', '"', (char) 0, false, false, false);
+ public static char COMMENTS_DISABLED = (char) 0;
+
+ public static CSVStrategy DEFAULT_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, true, false, true);
+ public static CSVStrategy EXCEL_STRATEGY = new CSVStrategy(';', '"', COMMENTS_DISABLED, false, false, false);
+ public static CSVStrategy TDF_STRATEGY = new CSVStrategy(' ', '"', COMMENTS_DISABLED, true, false, true);
public CSVStrategy(char delimiter, char encapsulator, char commentStart) {
@@ -74,6 +77,7 @@
public void setCommentStart(char commentStart) { this.commentStart = commentStart; }
public char getCommentStart() { return this.commentStart; }
+ public boolean isCommentingDisabled() { return this.commentStart == COMMENTS_DISABLED; }
public void setIgnoreLeadingWhitespaces(boolean ignoreLeadingWhitespaces) { this.ignoreLeadingWhitespaces = ignoreLeadingWhitespaces; }
public boolean getIgnoreLeadingWhitespaces() { return this.ignoreLeadingWhitespaces; }
Added: jakarta/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVUtils.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVUtils.java?rev=415323&view=auto
==============================================================================
--- jakarta/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVUtils.java (added)
+++ jakarta/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/CSVUtils.java Mon Jun 19 06:01:39 2006
@@ -0,0 +1,116 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.csv;
+
+import java.io.StringWriter;
+import java.io.StringReader;
+import java.io.IOException;
+
+/**
+ * Utility methods for dealing with CSV files
+ */
+public class CSVUtils {
+
+ /**
+ * <p><code>CSVUtils</code> instances should NOT be constructed in
+ * standard programming.
+ *
+ * <p>This constructor is public to permit tools that require a JavaBean
+ * instance to operate.</p>
+ */
+ public CSVUtils() {
+ }
+
+ /**
+ * Converts an array of string values into a single CSV line. All
+ * <code>null</code> values are converted to the string <code>"null"</code>,
+ * all strings equal to <code>"null"</code> will additionally get quotes
+ * around.
+ *
+ * @param values the value array
+ * @return the CSV string, will be an empty string if the length of the
+ * value array is 0
+ */
+ public static String printLine(String[] values) {
+ // set up a CSVUtils
+ StringWriter stringWriter = new StringWriter();
+ CSVPrinter csvPrinter = new CSVPrinter(stringWriter);
+
+ // check for null values an "null" as strings and convert them
+ // into the strings "null" and "\"null\""
+ for (int i = 0; i < values.length; i++) {
+ if (values[i] == null) {
+ values[i] = "null";
+ } else if (values[i].equals("null")) {
+ values[i] = "\"null\"";
+ }
+ }
+
+ // convert to CSV
+ csvPrinter.println(values);
+
+ // as the resulting string has \r\n at the end, we will trim that away
+ return stringWriter.toString().trim();
+ }
+
+ // ======================================================
+ // static parsers
+ // ======================================================
+
+ /**
+ * Parses the given String according to the default CSV strategy.
+ *
+ * @param s CSV String to be parsed.
+ * @return parsed String matrix (which is never null)
+ * @throws IOException in case of error
+ * @see #setStrategy()
+ */
+ public static String[][] parse(String s) throws IOException {
+ if (s == null) {
+ throw new IllegalArgumentException("Null argument not allowed.");
+ }
+ String[][] result = (new CSVParser(new StringReader(s))).getAllValues();
+ if (result == null) {
+ // since CSVStrategy ignores empty lines an empty array is returned
+ // (i.e. not "result = new String[][] {{""}};")
+ result = new String[0][0];
+ }
+ return result;
+ }
+
+ /**
+ * Parses the first line only according to the default CSV strategy.
+ *
+ * Parsing empty string will be handled as valid records containing zero
+ * elements, so the following property holds: parseLine("").length == 0.
+ *
+ * @param s CSV String to be parsed.
+ * @return parsed String vector (which is never null)
+ * @throws IOException in case of error
+ * @see #setStrategy()
+ */
+ public static String[] parseLine(String s) throws IOException {
+ if (s == null) {
+ throw new IllegalArgumentException("Null argument not allowed.");
+ }
+ // uh,jh: make sure that parseLine("").length == 0
+ if (s.length() == 0) {
+ return new String[0];
+ }
+ return (new CSVParser(new StringReader(s))).getLine();
+ }
+
+}
Modified: jakarta/commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVParserTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVParserTest.java?rev=415323&r1=415322&r2=415323&view=diff
==============================================================================
--- jakarta/commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVParserTest.java (original)
+++ jakarta/commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVParserTest.java Mon Jun 19 06:01:39 2006
@@ -474,128 +474,6 @@
}
}
- // ======================================================
- // static parser tests
- // ======================================================
- public void testParse1() throws IOException {
- String[][] data = CSVParser.parse("abc\ndef");
- assertEquals(2, data.length);
- assertEquals(1, data[0].length);
- assertEquals(1, data[1].length);
- assertEquals("abc", data[0][0]);
- assertEquals("def", data[1][0]);
- }
-
- public void testParse2() throws IOException {
- String[][] data = CSVParser.parse("abc,def,\"ghi,jkl\"\ndef");
- assertEquals(2, data.length);
- assertEquals(3, data[0].length);
- assertEquals(1, data[1].length);
- assertEquals("abc", data[0][0]);
- assertEquals("def", data[0][1]);
- assertEquals("ghi,jkl", data[0][2]);
- assertEquals("def", data[1][0]);
- }
-
- public void testParse3() throws IOException {
- String[][] data = CSVParser.parse("abc,\"def\nghi\"\njkl");
- assertEquals(2, data.length);
- assertEquals(2, data[0].length);
- assertEquals(1, data[1].length);
- assertEquals("abc", data[0][0]);
- assertEquals("def\nghi", data[0][1]);
- assertEquals("jkl", data[1][0]);
- }
-
- public void testParse4() throws IOException {
- String[][] data = CSVParser.parse("abc,\"def\\\\nghi\"\njkl");
- assertEquals(2, data.length);
- assertEquals(2, data[0].length);
- assertEquals(1, data[1].length);
- assertEquals("abc", data[0][0]);
- // an escape char in quotes only escapes a delimiter, not itself
- assertEquals("def\\\\nghi", data[0][1]);
- assertEquals("jkl", data[1][0]);
- }
-
- public void testParse5() throws IOException {
- String[][] data = CSVParser.parse("abc,def\\nghi\njkl");
- assertEquals(2, data.length);
- assertEquals(2, data[0].length);
- assertEquals(1, data[1].length);
- assertEquals("abc", data[0][0]);
- assertEquals("def\\nghi", data[0][1]);
- assertEquals("jkl", data[1][0]);
- }
-
- public void testParse6() throws IOException {
- String[][] data = CSVParser.parse("");
- // default strategy is CSV, which ignores empty lines
- assertEquals(0, data.length);
- }
-
- public void testParse7() throws IOException {
- boolean io = false;
- try {
- CSVParser.parse(null);
- } catch (IllegalArgumentException e) {
- io = true;
- }
- assertTrue(io);
- }
-
- public void testParseLine1() throws IOException {
- String[] data = CSVParser.parseLine("abc,def,ghi");
- assertEquals(3, data.length);
- assertEquals("abc", data[0]);
- assertEquals("def", data[1]);
- assertEquals("ghi", data[2]);
- }
-
- public void testParseLine2() throws IOException {
- String[] data = CSVParser.parseLine("abc,def,ghi\n");
- assertEquals(3, data.length);
- assertEquals("abc", data[0]);
- assertEquals("def", data[1]);
- assertEquals("ghi", data[2]);
- }
-
- public void testParseLine3() throws IOException {
- String[] data = CSVParser.parseLine("abc,\"def,ghi\"");
- assertEquals(2, data.length);
- assertEquals("abc", data[0]);
- assertEquals("def,ghi", data[1]);
- }
-
- public void testParseLine4() throws IOException {
- String[] data = CSVParser.parseLine("abc,\"def\nghi\"");
- assertEquals(2, data.length);
- assertEquals("abc", data[0]);
- assertEquals("def\nghi", data[1]);
- }
-
- public void testParseLine5() throws IOException {
- String[] data = CSVParser.parseLine("");
- assertEquals(0, data.length);
- // assertEquals("", data[0]);
- }
-
- public void testParseLine6() throws IOException {
- boolean io = false;
- try {
- CSVParser.parseLine(null);
- } catch (IllegalArgumentException e) {
- io = true;
- }
- assertTrue(io);
- }
-
- public void testParseLine7() throws IOException {
- String[] res = CSVParser.parseLine("");
- assertNotNull(res);
- assertEquals(0, res.length);
- }
-
public void testUnicodeEscape() throws IOException {
String code = "abc,\\u0070\\u0075\\u0062\\u006C\\u0069\\u0063";
CSVParser parser = new CSVParser(new StringReader(code));
Modified: jakarta/commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVPrinterTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVPrinterTest.java?rev=415323&r1=415322&r2=415323&view=diff
==============================================================================
--- jakarta/commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVPrinterTest.java (original)
+++ jakarta/commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVPrinterTest.java Mon Jun 19 06:01:39 2006
@@ -66,7 +66,7 @@
public void testExcelPrinter1() {
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw);
- printer.setSeparatorChar(';');
+ printer.setStrategy(CSVStrategy.EXCEL_STRATEGY);
String[] line1 = {"a", "b"};
printer.println(line1);
assertEquals("a;b" + lineSeparator, sw.toString());
@@ -75,7 +75,7 @@
public void testExcelPrinter2() {
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw);
- printer.setSeparatorChar(';');
+ printer.setStrategy(CSVStrategy.EXCEL_STRATEGY);
String[] line1 = {"a;b", "b"};
printer.println(line1);
assertEquals("\"a;b\";b" + lineSeparator, sw.toString());
Added: jakarta/commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVUtilsTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVUtilsTest.java?rev=415323&view=auto
==============================================================================
--- jakarta/commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVUtilsTest.java (added)
+++ jakarta/commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVUtilsTest.java Mon Jun 19 06:01:39 2006
@@ -0,0 +1,154 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.csv;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.Arrays;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * CSVUtilsTest
+ */
+public class CSVUtilsTest extends TestCase {
+
+ // ======================================================
+ // static parser tests
+ // ======================================================
+ public void testParse1() throws IOException {
+ String[][] data = CSVUtils.parse("abc\ndef");
+ assertEquals(2, data.length);
+ assertEquals(1, data[0].length);
+ assertEquals(1, data[1].length);
+ assertEquals("abc", data[0][0]);
+ assertEquals("def", data[1][0]);
+ }
+
+ public void testParse2() throws IOException {
+ String[][] data = CSVUtils.parse("abc,def,\"ghi,jkl\"\ndef");
+ assertEquals(2, data.length);
+ assertEquals(3, data[0].length);
+ assertEquals(1, data[1].length);
+ assertEquals("abc", data[0][0]);
+ assertEquals("def", data[0][1]);
+ assertEquals("ghi,jkl", data[0][2]);
+ assertEquals("def", data[1][0]);
+ }
+
+ public void testParse3() throws IOException {
+ String[][] data = CSVUtils.parse("abc,\"def\nghi\"\njkl");
+ assertEquals(2, data.length);
+ assertEquals(2, data[0].length);
+ assertEquals(1, data[1].length);
+ assertEquals("abc", data[0][0]);
+ assertEquals("def\nghi", data[0][1]);
+ assertEquals("jkl", data[1][0]);
+ }
+
+ public void testParse4() throws IOException {
+ String[][] data = CSVUtils.parse("abc,\"def\\\\nghi\"\njkl");
+ assertEquals(2, data.length);
+ assertEquals(2, data[0].length);
+ assertEquals(1, data[1].length);
+ assertEquals("abc", data[0][0]);
+ // an escape char in quotes only escapes a delimiter, not itself
+ assertEquals("def\\\\nghi", data[0][1]);
+ assertEquals("jkl", data[1][0]);
+ }
+
+ public void testParse5() throws IOException {
+ String[][] data = CSVUtils.parse("abc,def\\nghi\njkl");
+ assertEquals(2, data.length);
+ assertEquals(2, data[0].length);
+ assertEquals(1, data[1].length);
+ assertEquals("abc", data[0][0]);
+ assertEquals("def\\nghi", data[0][1]);
+ assertEquals("jkl", data[1][0]);
+ }
+
+ public void testParse6() throws IOException {
+ String[][] data = CSVUtils.parse("");
+ // default strategy is CSV, which ignores empty lines
+ assertEquals(0, data.length);
+ }
+
+ public void testParse7() throws IOException {
+ boolean io = false;
+ try {
+ CSVUtils.parse(null);
+ } catch (IllegalArgumentException e) {
+ io = true;
+ }
+ assertTrue(io);
+ }
+
+ public void testParseLine1() throws IOException {
+ String[] data = CSVUtils.parseLine("abc,def,ghi");
+ assertEquals(3, data.length);
+ assertEquals("abc", data[0]);
+ assertEquals("def", data[1]);
+ assertEquals("ghi", data[2]);
+ }
+
+ public void testParseLine2() throws IOException {
+ String[] data = CSVUtils.parseLine("abc,def,ghi\n");
+ assertEquals(3, data.length);
+ assertEquals("abc", data[0]);
+ assertEquals("def", data[1]);
+ assertEquals("ghi", data[2]);
+ }
+
+ public void testParseLine3() throws IOException {
+ String[] data = CSVUtils.parseLine("abc,\"def,ghi\"");
+ assertEquals(2, data.length);
+ assertEquals("abc", data[0]);
+ assertEquals("def,ghi", data[1]);
+ }
+
+ public void testParseLine4() throws IOException {
+ String[] data = CSVUtils.parseLine("abc,\"def\nghi\"");
+ assertEquals(2, data.length);
+ assertEquals("abc", data[0]);
+ assertEquals("def\nghi", data[1]);
+ }
+
+ public void testParseLine5() throws IOException {
+ String[] data = CSVUtils.parseLine("");
+ assertEquals(0, data.length);
+ // assertEquals("", data[0]);
+ }
+
+ public void testParseLine6() throws IOException {
+ boolean io = false;
+ try {
+ CSVUtils.parseLine(null);
+ } catch (IllegalArgumentException e) {
+ io = true;
+ }
+ assertTrue(io);
+ }
+
+ public void testParseLine7() throws IOException {
+ String[] res = CSVUtils.parseLine("");
+ assertNotNull(res);
+ assertEquals(0, res.length);
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org