harbor run -t openthoughts/tasktrove-inferredbugs-sandboxes-verifier-inferredbugs-4754harbor download openthoughts/tasktrove-inferredbugs-sandboxes-verifier-inferredbugs-4754Project: commons-csv Bug ID: 51 Language: java
Before (Buggy File):
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import org.junit.Test;
/**
*
*
* @version $Id$
*/
public class CSVPrinterTest {
public static String printable(final String s) {
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
final char ch = s.charAt(i);
if (ch <= ' ' || ch >= 128) {
sb.append("(").append((int) ch).append(")");
} else {
sb.append(ch);
}
}
return sb.toString();
}
String recordSeparator = CSVFormat.DEFAULT.getRecordSeparator();
public void doOneRandom(final CSVFormat format) throws Exception {
final Random r = new Random();
final int nLines = r.nextInt(4) + 1;
final int nCol = r.nextInt(3) + 1;
// nLines=1;nCol=2;
final String[][] lines = new String[nLines][];
for (int i = 0; i < nLines; i++) {
final String[] line = new String[nCol];
lines[i] = line;
for (int j = 0; j < nCol; j++) {
line[j] = randStr();
}
}
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, format);
for (int i = 0; i < nLines; i++) {
// for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j]));
printer.printRecord((Object[])lines[i]);
}
printer.flush();
printer.close();
final String result = sw.toString();
// System.out.println("### :" + printable(result));
final CSVParser parser = new CSVParser(result, format);
final List<CSVRecord> parseResult = parser.getRecords();
Utils.compare("Printer output :" + printable(result), lines, parseResult);
}
public void doRandom(final CSVFormat format, final int iter) throws Exception {
for (int i = 0; i < iter; i++) {
doOneRandom(format);
}
}
public String randStr() {
final Random r = new Random();
final int sz = r.nextInt(20);
// sz = r.nextInt(3);
final char[] buf = new char[sz];
for (int i = 0; i < sz; i++) {
// stick in special chars with greater frequency
char ch;
final int what = r.nextInt(20);
switch (what) {
case 0:
ch = '\r';
break;
case 1:
ch = '\n';
break;
case 2:
ch = '\t';
break;
case 3:
ch = '\f';
break;
case 4:
ch = ' ';
break;
case 5:
ch = ',';
break;
case 6:
ch = '"';
break;
case 7:
ch = '\'';
break;
case 8:
ch = '\\';
break;
default:
ch = (char) r.nextInt(300);
break;
// default: ch = 'a'; break;
}
buf[i] = ch;
}
return new String(buf);
}
@Test
public void testDisabledComment() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printComment("This is a comment");
assertEquals("", sw.toString());
}
@Test
public void testExcelPrintAllArrayOfArrays() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
printer.printRecords(new String[][] { { "r1c1", "r1c2" }, { "r2c1", "r2c2" } });
assertEquals("r1c1,r1c2" + recordSeparator + "r2c1,r2c2" + recordSeparator, sw.toString());
}
@Test
public void testExcelPrintAllArrayOfLists() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
printer.printRecords(new List[] { Arrays.asList(new String[] { "r1c1", "r1c2" }), Arrays.asList(new String[] { "r2c1", "r2c2" }) });
assertEquals("r1c1,r1c2" + recordSeparator + "r2c1,r2c2" + recordSeparator, sw.toString());
}
@Test
public void testExcelPrintAllIterableOfArrays() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
printer.printRecords(Arrays.asList(new String[][] { { "r1c1", "r1c2" }, { "r2c1", "r2c2" } }));
assertEquals("r1c1,r1c2" + recordSeparator + "r2c1,r2c2" + recordSeparator, sw.toString());
}
@Test
public void testExcelPrintAllIterableOfLists() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
printer.printRecords(Arrays.asList(new List[] { Arrays.asList(new String[] { "r1c1", "r1c2" }),
Arrays.asList(new String[] { "r2c1", "r2c2" }) }));
assertEquals("r1c1,r1c2" + recordSeparator + "r2c1,r2c2" + recordSeparator, sw.toString());
}
@Test
public void testExcelPrinter1() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
printer.printRecord("a", "b");
assertEquals("a,b" + recordSeparator, sw.toString());
}
@Test
public void testExcelPrinter2() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
printer.printRecord("a,b", "b");
assertEquals("\"a,b\",b" + recordSeparator, sw.toString());
}
@Test
public void testJdbcPrinter() throws IOException, ClassNotFoundException, SQLException {
final StringWriter sw = new StringWriter();
Class.forName("org.h2.Driver");
final Connection connection = DriverManager.getConnection("jdbc:h2:mem:my_test;", "sa", "");
try {
final Statement stmt = connection.createStatement();
stmt.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
stmt.execute("insert into TEST values(1, 'r1')");
stmt.execute("insert into TEST values(2, 'r2')");
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecords(stmt.executeQuery("select ID, NAME from TEST"));
assertEquals("1,r1" + recordSeparator + "2,r2" + recordSeparator, sw.toString());
} finally {
connection.close();
}
}
@Test
public void testMultiLineComment() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withCommentStart('#').build());
printer.printComment("This is a comment\non multiple lines");
assertEquals("# This is a comment" + recordSeparator + "# on multiple lines" + recordSeparator, sw.toString());
}
@Test
public void testPrinter1() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecord("a", "b");
assertEquals("a,b" + recordSeparator, sw.toString());
}
@Test
public void testPrinter2() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecord("a,b", "b");
assertEquals("\"a,b\",b" + recordSeparator, sw.toString());
}
@Test
public void testPrinter3() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecord("a, b", "b ");
assertEquals("\"a, b\",\"b \"" + recordSeparator, sw.toString());
}
@Test
public void testPrinter4() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecord("a", "b\"c");
assertEquals("a,\"b\"\"c\"" + recordSeparator, sw.toString());
}
@Test
public void testPrinter5() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecord("a", "b\nc");
assertEquals("a,\"b\nc\"" + recordSeparator, sw.toString());
}
@Test
public void testPrinter6() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecord("a", "b\r\nc");
assertEquals("a,\"b\r\nc\"" + recordSeparator, sw.toString());
}
@Test
public void testPrinter7() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecord("a", "b\\c");
assertEquals("a,b\\c" + recordSeparator, sw.toString());
}
@Test
public void testPrintNullValues() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printRecord("a", null, "b");
assertEquals("a,,b" + recordSeparator, sw.toString());
}
@Test
public void testQuoteAll() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withQuotePolicy(Quote.ALL).build());
printer.printRecord("a", "b\nc", "d");
assertEquals("\"a\",\"b\nc\",\"d\"" + recordSeparator, sw.toString());
}
@Test
public void testQuoteNonNumeric() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withQuotePolicy(Quote.NON_NUMERIC).build());
printer.printRecord("a", "b\nc", Integer.valueOf(1));
assertEquals("\"a\",\"b\nc\",1" + recordSeparator, sw.toString());
}
@Test
public void testRandom() throws Exception {
final int iter = 10000;
doRandom(CSVFormat.DEFAULT, iter);
doRandom(CSVFormat.EXCEL, iter);
doRandom(CSVFormat.MYSQL, iter);
}
@Test
public void testSingleLineComment() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withCommentStart('#').build());
printer.printComment("This is a comment");
assertEquals("# This is a comment" + recordSeparator, sw.toString());
}
}
PROVER
ERROR
RESOURCE_LEAK
resource of type org.apache.commons.csv.CSVPrinter acquired to printer by call to new() at line 156 is not released after line 157.
HIGH
user
void CSVPrinterTest.testExcelPrintAllArrayOfArrays()
org.apache.commons.csv.CSVPrinterTest.testExcelPrintAllArrayOfArrays():void.2ea7aa0e7e5d0e6c6e4db10d3176e30e
src/test/java/org/apache/commons/csv/CSVPrinterTest.java
CSVPrinterTest.java|testExcelPrintAllArrayOfArrays|RESOURCE_LEAK
24b323abf09399c888b6f1033b8f1206
8bd9ccf96edf4c11576949028226c900
Resource Leak
Fix the bug in the code.
Write the complete, corrected version of the file to: /app/src/test/java/org/apache/commons/csv/CSVPrinterTest.java.
The directory does not exist yet. You must create it and write the file:
mkdir -p /app/src/test/java/org/apache/commons/csv
cat > /app/src/test/java/org/apache/commons/csv/CSVPrinterTest.java << 'ENDOFFILE'
<full corrected file content here>
ENDOFFILE