1 2 /* 3 * Copyright (C) 2020 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * 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 19 package com.android.build.config; 20 21 import org.junit.Assert; 22 import org.junit.Test; 23 24 import java.io.StringReader; 25 import java.util.Arrays; 26 import java.util.List; 27 28 /** 29 * Test for CSV parser class. 30 */ 31 public class CsvParserTest { listsToStrings(String[] expected, List<String> actual)32 public String listsToStrings(String[] expected, List<String> actual) { 33 return "expected=" + Arrays.toString(expected) 34 + " actual=" + Arrays.toString(actual.toArray()); 35 } 36 assertLineEquals(CsvParser.Line actual, int lineno, String... fields)37 public void assertLineEquals(CsvParser.Line actual, int lineno, String... fields) { 38 if (actual.getLine() != lineno) { 39 throw new RuntimeException("lineno mismatch: expected=" + lineno 40 + " actual=" + actual.getLine()); 41 } 42 if (fields.length != actual.getFields().size()) { 43 throw new RuntimeException("getFields().size() mismatch: expected=" + fields.length 44 + " actual=" + actual.getFields().size() 45 + " values: " + listsToStrings(fields, actual.getFields())); 46 } 47 for (int i = 0; i < fields.length; i++) { 48 if (!fields[i].equals(actual.getFields().get(i))) { 49 throw new RuntimeException("getFields().get(" + i + ") mismatch: expected=" 50 + fields[i] + " actual=" + actual.getFields().get(i) 51 + " values: " + listsToStrings(fields, actual.getFields())); 52 53 } 54 } 55 } 56 57 @Test testEmptyString()58 public void testEmptyString() throws Exception { 59 List<CsvParser.Line> lines = CsvParser.parse(new StringReader( 60 "")); 61 62 Assert.assertEquals(0, lines.size()); 63 } 64 65 @Test testLexerOneCharacter()66 public void testLexerOneCharacter() throws Exception { 67 List<CsvParser.Line> lines = CsvParser.parse(new StringReader( 68 "a")); 69 70 Assert.assertEquals(1, lines.size()); 71 assertLineEquals(lines.get(0), 1, "a"); 72 } 73 74 @Test testLexerTwoFieldsNoNewline()75 public void testLexerTwoFieldsNoNewline() throws Exception { 76 List<CsvParser.Line> lines = CsvParser.parse(new StringReader( 77 "a,b")); 78 79 Assert.assertEquals(1, lines.size()); 80 assertLineEquals(lines.get(0), 1, "a", "b"); 81 } 82 83 @Test testLexerTwoFieldsNewline()84 public void testLexerTwoFieldsNewline() throws Exception { 85 List<CsvParser.Line> lines = CsvParser.parse(new StringReader( 86 "a,b\n")); 87 88 Assert.assertEquals(1, lines.size()); 89 assertLineEquals(lines.get(0), 1, "a", "b"); 90 } 91 92 @Test testEndsWithTwoNewlines()93 public void testEndsWithTwoNewlines() throws Exception { 94 List<CsvParser.Line> lines = CsvParser.parse(new StringReader( 95 "a,b\n\n")); 96 97 Assert.assertEquals(1, lines.size()); 98 assertLineEquals(lines.get(0), 1, "a", "b"); 99 } 100 101 @Test testOnlyNewlines()102 public void testOnlyNewlines() throws Exception { 103 List<CsvParser.Line> lines = CsvParser.parse(new StringReader( 104 "\n\n\n\n")); 105 106 Assert.assertEquals(0, lines.size()); 107 } 108 109 110 @Test testLexerComplex()111 public void testLexerComplex() throws Exception { 112 List<CsvParser.Line> lines = CsvParser.parse(new StringReader( 113 ",\"ab\"\"\nc\",,de\n" 114 + "fg,\n" 115 + "\n" 116 + ",\n" 117 + "hijk")); 118 119 Assert.assertEquals(4, lines.size()); 120 assertLineEquals(lines.get(0), 2, "", "ab\"\nc", "", "de"); 121 assertLineEquals(lines.get(1), 3, "fg", ""); 122 assertLineEquals(lines.get(2), 5, "", ""); 123 assertLineEquals(lines.get(3), 6, "hijk"); 124 } 125 126 @Test testEndInsideQuoted()127 public void testEndInsideQuoted() throws Exception { 128 try { 129 List<CsvParser.Line> lines = CsvParser.parse(new StringReader( 130 "\"asd")); 131 throw new RuntimeException("Didn't throw ParseException"); 132 } catch (CsvParser.ParseException ex) { 133 System.out.println("Caught: " + ex); 134 } 135 } 136 137 @Test testCharacterAfterQuotedField()138 public void testCharacterAfterQuotedField() throws Exception { 139 try { 140 List<CsvParser.Line> lines = CsvParser.parse(new StringReader( 141 "\"\"a")); 142 throw new RuntimeException("Didn't throw ParseException"); 143 } catch (CsvParser.ParseException ex) { 144 System.out.println("Caught: " + ex); 145 } 146 } 147 } 148 149