|
| 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 | + |
| 18 | +package org.apache.commons.csv; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.StringReader; |
| 22 | + |
| 23 | +import junit.framework.TestCase; |
| 24 | +import org.apache.commons.csv.CSVLexer.Token; |
| 25 | + |
| 26 | +import static org.apache.commons.csv.CSVLexer.Token.Type.*; |
| 27 | + |
| 28 | +public class CSVLexerTest extends TestCase { |
| 29 | + |
| 30 | + private CSVLexer getLexer(String input, CSVFormat format) { |
| 31 | + return new CSVLexer(format, new ExtendedBufferedReader(new StringReader(input))); |
| 32 | + } |
| 33 | + |
| 34 | + private void assertTokenEquals(Token.Type expectedType, String expectedContent, Token token) { |
| 35 | + assertEquals("Token type", expectedType, token.type); |
| 36 | + assertEquals("Token content", expectedContent, token.content.toString()); |
| 37 | + } |
| 38 | + |
| 39 | + // Single line (without comment) |
| 40 | + public void testNextToken1() throws IOException { |
| 41 | + String code = "abc,def, hijk, lmnop, qrst,uv ,wxy ,z , ,"; |
| 42 | + CSVLexer parser = getLexer(code, CSVFormat.DEFAULT); |
| 43 | + assertTokenEquals(TOKEN, "abc", parser.nextToken(new Token())); |
| 44 | + assertTokenEquals(TOKEN, "def", parser.nextToken(new Token())); |
| 45 | + assertTokenEquals(TOKEN, "hijk", parser.nextToken(new Token())); |
| 46 | + assertTokenEquals(TOKEN, "lmnop", parser.nextToken(new Token())); |
| 47 | + assertTokenEquals(TOKEN, "qrst", parser.nextToken(new Token())); |
| 48 | + assertTokenEquals(TOKEN, "uv", parser.nextToken(new Token())); |
| 49 | + assertTokenEquals(TOKEN, "wxy", parser.nextToken(new Token())); |
| 50 | + assertTokenEquals(TOKEN, "z", parser.nextToken(new Token())); |
| 51 | + assertTokenEquals(TOKEN, "", parser.nextToken(new Token())); |
| 52 | + assertTokenEquals(EOF, "", parser.nextToken(new Token())); |
| 53 | + } |
| 54 | + |
| 55 | + // multiline including comments (and empty lines) |
| 56 | + public void testNextToken2() throws IOException { |
| 57 | + /* file: 1,2,3, |
| 58 | + * a,b x,c |
| 59 | + * |
| 60 | + * # this is a comment |
| 61 | + * d,e, |
| 62 | + * |
| 63 | + */ |
| 64 | + String code = "1,2,3,\na,b x,c\n#foo\n\nd,e,\n\n"; |
| 65 | + CSVFormat format = CSVFormat.DEFAULT.withCommentStart('#'); |
| 66 | + |
| 67 | + CSVLexer parser = getLexer(code, format); |
| 68 | + |
| 69 | + |
| 70 | + assertTokenEquals(TOKEN, "1", parser.nextToken(new Token())); |
| 71 | + assertTokenEquals(TOKEN, "2", parser.nextToken(new Token())); |
| 72 | + assertTokenEquals(TOKEN, "3", parser.nextToken(new Token())); |
| 73 | + assertTokenEquals(EORECORD, "", parser.nextToken(new Token())); |
| 74 | + assertTokenEquals(TOKEN, "a", parser.nextToken(new Token())); |
| 75 | + assertTokenEquals(TOKEN, "b x", parser.nextToken(new Token())); |
| 76 | + assertTokenEquals(EORECORD, "c", parser.nextToken(new Token())); |
| 77 | + assertTokenEquals(EORECORD, "", parser.nextToken(new Token())); |
| 78 | + assertTokenEquals(TOKEN, "d", parser.nextToken(new Token())); |
| 79 | + assertTokenEquals(TOKEN, "e", parser.nextToken(new Token())); |
| 80 | + assertTokenEquals(EORECORD, "", parser.nextToken(new Token())); |
| 81 | + assertTokenEquals(EOF, "", parser.nextToken(new Token())); |
| 82 | + assertTokenEquals(EOF, "", parser.nextToken(new Token())); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + // simple token with escaping |
| 87 | + public void testNextToken3() throws IOException { |
| 88 | + /* file: a,\,,b |
| 89 | + * \,, |
| 90 | + */ |
| 91 | + String code = "a,\\,,b\n\\,,"; |
| 92 | + CSVFormat format = CSVFormat.DEFAULT.withCommentStart('#'); |
| 93 | + CSVLexer parser = getLexer(code, format); |
| 94 | + |
| 95 | + assertTokenEquals(TOKEN, "a", parser.nextToken(new Token())); |
| 96 | + // an unquoted single backslash is not an escape char |
| 97 | + assertTokenEquals(TOKEN, "\\", parser.nextToken(new Token())); |
| 98 | + assertTokenEquals(TOKEN, "", parser.nextToken(new Token())); |
| 99 | + assertTokenEquals(EORECORD, "b", parser.nextToken(new Token())); |
| 100 | + // an unquoted single backslash is not an escape char |
| 101 | + assertTokenEquals(TOKEN, "\\", parser.nextToken(new Token())); |
| 102 | + assertTokenEquals(TOKEN, "", parser.nextToken(new Token())); |
| 103 | + assertTokenEquals(EOF, "", parser.nextToken(new Token())); |
| 104 | + } |
| 105 | + |
| 106 | + // encapsulator tokenizer (sinle line) |
| 107 | + public void testNextToken4() throws IOException { |
| 108 | + /* file: a,"foo",b |
| 109 | + * a, " foo",b |
| 110 | + * a,"foo " ,b // whitespace after closing encapsulator |
| 111 | + * a, " foo " ,b |
| 112 | + */ |
| 113 | + String code = "a,\"foo\",b\na, \" foo\",b\na,\"foo \" ,b\na, \" foo \" ,b"; |
| 114 | + CSVLexer parser = getLexer(code, CSVFormat.DEFAULT); |
| 115 | + assertTokenEquals(TOKEN, "a", parser.nextToken(new Token())); |
| 116 | + assertTokenEquals(TOKEN, "foo", parser.nextToken(new Token())); |
| 117 | + assertTokenEquals(EORECORD, "b", parser.nextToken(new Token())); |
| 118 | + assertTokenEquals(TOKEN, "a", parser.nextToken(new Token())); |
| 119 | + assertTokenEquals(TOKEN, " foo", parser.nextToken(new Token())); |
| 120 | + assertTokenEquals(EORECORD, "b", parser.nextToken(new Token())); |
| 121 | + assertTokenEquals(TOKEN, "a", parser.nextToken(new Token())); |
| 122 | + assertTokenEquals(TOKEN, "foo ", parser.nextToken(new Token())); |
| 123 | + assertTokenEquals(EORECORD, "b", parser.nextToken(new Token())); |
| 124 | + assertTokenEquals(TOKEN, "a", parser.nextToken(new Token())); |
| 125 | + assertTokenEquals(TOKEN, " foo ", parser.nextToken(new Token())); |
| 126 | +// assertTokenEquals(EORECORD, "b", parser.nextToken(new Token())); |
| 127 | + assertTokenEquals(EOF, "b", parser.nextToken(new Token())); |
| 128 | + } |
| 129 | + |
| 130 | + // encapsulator tokenizer (multi line, delimiter in string) |
| 131 | + public void testNextToken5() throws IOException { |
| 132 | + String code = "a,\"foo\n\",b\n\"foo\n baar ,,,\"\n\"\n\t \n\""; |
| 133 | + CSVLexer parser = getLexer(code, CSVFormat.DEFAULT); |
| 134 | + assertTokenEquals(TOKEN, "a", parser.nextToken(new Token())); |
| 135 | + assertTokenEquals(TOKEN, "foo\n", parser.nextToken(new Token())); |
| 136 | + assertTokenEquals(EORECORD, "b", parser.nextToken(new Token())); |
| 137 | + assertTokenEquals(EORECORD, "foo\n baar ,,,", parser.nextToken(new Token())); |
| 138 | + assertTokenEquals(EOF, "\n\t \n", parser.nextToken(new Token())); |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + // change delimiters, comment, encapsulater |
| 143 | + public void testNextToken6() throws IOException { |
| 144 | + /* file: a;'b and \' more |
| 145 | + * ' |
| 146 | + * !comment;;;; |
| 147 | + * ;; |
| 148 | + */ |
| 149 | + String code = "a;'b and '' more\n'\n!comment;;;;\n;;"; |
| 150 | + CSVFormat format = new CSVFormat(';', '\'', '!'); |
| 151 | + CSVLexer parser = getLexer(code, format); |
| 152 | + assertTokenEquals(TOKEN, "a", parser.nextToken(new Token())); |
| 153 | + assertTokenEquals(EORECORD, "b and ' more\n", parser.nextToken(new Token())); |
| 154 | + } |
| 155 | + |
| 156 | + // From SANDBOX-153 |
| 157 | + public void testDelimiterIsWhitespace() throws IOException { |
| 158 | + String code = "one\ttwo\t\tfour \t five\t six"; |
| 159 | + CSVLexer parser = getLexer(code, CSVFormat.TDF); |
| 160 | + assertTokenEquals(TOKEN, "one", parser.nextToken(new Token())); |
| 161 | + assertTokenEquals(TOKEN, "two", parser.nextToken(new Token())); |
| 162 | + assertTokenEquals(TOKEN, "", parser.nextToken(new Token())); |
| 163 | + assertTokenEquals(TOKEN, "four", parser.nextToken(new Token())); |
| 164 | + assertTokenEquals(TOKEN, "five", parser.nextToken(new Token())); |
| 165 | + assertTokenEquals(EOF, "six", parser.nextToken(new Token())); |
| 166 | + } |
| 167 | +} |
0 commit comments