Avoid resource leak warnings

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1582813 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2014-03-28 16:37:54 +00:00
parent d6294bc598
commit 1933f8abaf
6 changed files with 49 additions and 4 deletions

View File

@ -124,6 +124,7 @@ public class CSVFileParserTest {
final int count = record.size(); final int count = record.size();
assertEquals(testName, readTestData(), count+":"+parsed); assertEquals(testName, readTestData(), count+":"+parsed);
} }
parser.close();
} }
@Test @Test
@ -167,5 +168,6 @@ public class CSVFileParserTest {
final int count = record.size(); final int count = record.size();
assertEquals(testName, readTestData(), count + ":" + parsed); assertEquals(testName, readTestData(), count + ":" + parsed);
} }
parser.close();
} }
} }

View File

@ -114,6 +114,7 @@ public class CSVParserTest {
assertTrue(records.size() > 0); assertTrue(records.size() > 0);
Utils.compare("Records do not match expected result", res, records); Utils.compare("Records do not match expected result", res, records);
parser.close();
} }
@Test @Test
@ -143,6 +144,7 @@ public class CSVParserTest {
assertTrue(records.size() > 0); assertTrue(records.size() > 0);
Utils.compare("", res, records); Utils.compare("", res, records);
parser.close();
} }
@Test @Test
@ -176,6 +178,7 @@ public class CSVParserTest {
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values()); assertArrayEquals(res[i], records.get(i).values());
} }
parser.close();
} }
@Test @Test
@ -207,6 +210,7 @@ public class CSVParserTest {
} }
} finally { } finally {
parser.close(); parser.close();
reader.close();
} }
} }
@ -216,6 +220,7 @@ public class CSVParserTest {
final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT); final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords(); final List<CSVRecord> records = parser.getRecords();
assertEquals(4, records.size()); assertEquals(4, records.size());
parser.close();
} }
@Test @Test
@ -224,6 +229,7 @@ public class CSVParserTest {
final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT); final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords(); final List<CSVRecord> records = parser.getRecords();
assertEquals(4, records.size()); assertEquals(4, records.size());
parser.close();
} }
@Test(expected = NoSuchElementException.class) @Test(expected = NoSuchElementException.class)
@ -243,6 +249,7 @@ public class CSVParserTest {
final List<CSVRecord> list = parser.getRecords(); final List<CSVRecord> list = parser.getRecords();
assertNotNull(list); assertNotNull(list);
assertEquals(0, list.size()); assertEquals(0, list.size());
parser.close();
} }
@Test @Test
@ -275,16 +282,19 @@ public class CSVParserTest {
}; };
format = CSVFormat.DEFAULT.withCommentStart('#'); format = CSVFormat.DEFAULT.withCommentStart('#');
parser.close();
parser = CSVParser.parse(code, format); parser = CSVParser.parse(code, format);
records = parser.getRecords(); records = parser.getRecords();
Utils.compare("Failed to parse with comments", res_comments, records); Utils.compare("Failed to parse with comments", res_comments, records);
parser.close();
} }
@Test @Test
public void testEmptyFile() throws Exception { public void testEmptyFile() throws Exception {
final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT); final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT);
assertNull(parser.nextRecord()); assertNull(parser.nextRecord());
parser.close();
} }
@Test @Test
@ -306,6 +316,7 @@ public class CSVParserTest {
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values()); assertArrayEquals(res[i], records.get(i).values());
} }
parser.close();
} }
} }
@ -330,6 +341,7 @@ public class CSVParserTest {
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values()); assertArrayEquals(res[i], records.get(i).values());
} }
parser.close();
} }
} }
@ -357,6 +369,7 @@ public class CSVParserTest {
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values()); assertArrayEquals(res[i], records.get(i).values());
} }
parser.close();
} }
} }
@ -386,6 +399,7 @@ public class CSVParserTest {
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values()); assertArrayEquals(res[i], records.get(i).values());
} }
parser.close();
} }
} }
@ -408,6 +422,7 @@ public class CSVParserTest {
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values()); assertArrayEquals(res[i], records.get(i).values());
} }
parser.close();
} }
@Test @Test
@ -427,6 +442,7 @@ public class CSVParserTest {
for (int i = 0; i < res.length; i++) { for (int i = 0; i < res.length; i++) {
assertArrayEquals(res[i], records.get(i).values()); assertArrayEquals(res[i], records.get(i).values());
} }
parser.close();
} }
@Test @Test
@ -466,6 +482,7 @@ public class CSVParserTest {
} }
assertFalse(records.hasNext()); assertFalse(records.hasNext());
parser.close();
} }
@Test @Test
@ -476,6 +493,7 @@ public class CSVParserTest {
} }
assertNull(parser.nextRecord()); assertNull(parser.nextRecord());
parser.close();
} }
@Test @Test
@ -517,6 +535,7 @@ public class CSVParserTest {
for (int i = 0; i < RESULT.length; i++) { for (int i = 0; i < RESULT.length; i++) {
assertArrayEquals(RESULT[i], records.get(i).values()); assertArrayEquals(RESULT[i], records.get(i).values());
} }
parser.close();
} }
@Test @Test
@ -541,6 +560,7 @@ public class CSVParserTest {
assertNull(record = parser.nextRecord()); assertNull(record = parser.nextRecord());
assertEquals(8, parser.getCurrentLineNumber()); assertEquals(8, parser.getCurrentLineNumber());
assertEquals(3, parser.getRecordNumber()); assertEquals(3, parser.getRecordNumber());
parser.close();
} }
@Test @Test
@ -585,6 +605,7 @@ public class CSVParserTest {
final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT); final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords(); final List<CSVRecord> records = parser.getRecords();
assertEquals(3, records.size()); assertEquals(3, records.size());
parser.close();
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
@ -628,6 +649,7 @@ public class CSVParserTest {
final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT); final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
final List<CSVRecord> records = parser.getRecords(); final List<CSVRecord> records = parser.getRecords();
assertEquals(4, records.size()); assertEquals(4, records.size());
parser.close();
} }
@Test @Test
@ -681,22 +703,24 @@ public class CSVParserTest {
assertEquals("d", second.get(0)); assertEquals("d", second.get(0));
assertEquals("e", second.get(1)); assertEquals("e", second.get(1));
assertEquals("f", second.get(2)); assertEquals("f", second.get(2));
parser.close();
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testNewCSVParserNullReaderFormat() throws Exception { public void testNewCSVParserNullReaderFormat() throws Exception {
new CSVParser(null, CSVFormat.DEFAULT); new CSVParser(null, CSVFormat.DEFAULT).close();
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testNewCSVParserReaderNullFormat() throws Exception { public void testNewCSVParserReaderNullFormat() throws Exception {
new CSVParser(new StringReader(""), null); new CSVParser(new StringReader(""), null).close();
} }
@Test @Test
public void testNoHeaderMap() throws Exception { public void testNoHeaderMap() throws Exception {
final CSVParser parser = CSVParser.parse("a,b,c\n1,2,3\nx,y,z", CSVFormat.DEFAULT); final CSVParser parser = CSVParser.parse("a,b,c\n1,2,3\nx,y,z", CSVFormat.DEFAULT);
Assert.assertNull(parser.getHeaderMap()); Assert.assertNull(parser.getHeaderMap());
parser.close();
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
@ -824,6 +848,7 @@ public class CSVParserTest {
assertNull(parser.nextRecord()); assertNull(parser.nextRecord());
// Still 2 because the last line is does not have EOL chars // Still 2 because the last line is does not have EOL chars
assertEquals(2, parser.getCurrentLineNumber()); assertEquals(2, parser.getCurrentLineNumber());
parser.close();
} }
private void validateRecordNumbers(final String lineSeparator) throws IOException { private void validateRecordNumbers(final String lineSeparator) throws IOException {
@ -841,6 +866,7 @@ public class CSVParserTest {
assertEquals(3, parser.getRecordNumber()); assertEquals(3, parser.getRecordNumber());
assertNull(record = parser.nextRecord()); assertNull(record = parser.nextRecord());
assertEquals(3, parser.getRecordNumber()); assertEquals(3, parser.getRecordNumber());
parser.close();
} }
} }

View File

@ -89,6 +89,7 @@ public class CSVPrinterTest {
final List<CSVRecord> parseResult = parser.getRecords(); final List<CSVRecord> parseResult = parser.getRecords();
Utils.compare("Printer output :" + printable(result), lines, parseResult); Utils.compare("Printer output :" + printable(result), lines, parseResult);
parser.close();
} }
private void doRandom(final CSVFormat format, final int iter) throws Exception { private void doRandom(final CSVFormat format, final int iter) throws Exception {
@ -336,6 +337,7 @@ public class CSVPrinterTest {
assertEquals(null, record.get(1)); assertEquals(null, record.get(1));
assertEquals("b", record.get(2)); assertEquals("b", record.get(2));
assertFalse(iterator.hasNext()); assertFalse(iterator.hasNext());
((CSVParser) iterable).close();
} }
@Test @Test
@ -492,11 +494,11 @@ public class CSVPrinterTest {
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testNewCSVPrinterNullAppendableFormat() throws Exception { public void testNewCSVPrinterNullAppendableFormat() throws Exception {
new CSVPrinter(null, CSVFormat.DEFAULT); new CSVPrinter(null, CSVFormat.DEFAULT).close();
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testNewCsvPrinterAppendableNullFormat() throws Exception { public void testNewCsvPrinterAppendableNullFormat() throws Exception {
new CSVPrinter(new StringWriter(), null); new CSVPrinter(new StringWriter(), null).close();
} }
} }

View File

@ -151,6 +151,7 @@ public class CSVRecordTest {
Collections.sort(list); Collections.sort(list);
printer.printRecord(list); printer.printRecord(list);
Assert.assertEquals("A,B,C,NewValue" + CSVFormat.DEFAULT.getRecordSeparator(), printer.getOut().toString()); Assert.assertEquals("A,B,C,NewValue" + CSVFormat.DEFAULT.getRecordSeparator(), printer.getOut().toString());
printer.close();
} }
@Test @Test

View File

@ -42,6 +42,7 @@ public class ExtendedBufferedReaderTest {
assertEquals(END_OF_STREAM, br.getLastChar()); assertEquals(END_OF_STREAM, br.getLastChar());
assertNull(br.readLine()); assertNull(br.readLine());
assertEquals(0, br.read(new char[10], 0, 0)); assertEquals(0, br.read(new char[10], 0, 0));
br.close();
} }
@Test @Test
@ -100,6 +101,7 @@ public class ExtendedBufferedReaderTest {
assertEquals(END_OF_STREAM, br.lookAhead()); assertEquals(END_OF_STREAM, br.lookAhead());
assertEquals(3, br.getCurrentLineNumber()); assertEquals(3, br.getCurrentLineNumber());
br.close();
} }
@Test @Test
@ -120,6 +122,7 @@ public class ExtendedBufferedReaderTest {
assertEquals(1, br.read(res, 4, 1)); assertEquals(1, br.read(res, 4, 1));
assertArrayEquals(ref, res); assertArrayEquals(ref, res);
assertEquals('d', br.getLastChar()); assertEquals('d', br.getLastChar());
br.close();
} }
@Test @Test
@ -127,10 +130,12 @@ public class ExtendedBufferedReaderTest {
ExtendedBufferedReader br = getBufferedReader(""); ExtendedBufferedReader br = getBufferedReader("");
assertNull(br.readLine()); assertNull(br.readLine());
br.close();
br = getBufferedReader("\n"); br = getBufferedReader("\n");
assertEquals("",br.readLine()); assertEquals("",br.readLine());
assertNull(br.readLine()); assertNull(br.readLine());
br.close();
br = getBufferedReader("foo\n\nhello"); br = getBufferedReader("foo\n\nhello");
assertEquals(0, br.getCurrentLineNumber()); assertEquals(0, br.getCurrentLineNumber());
assertEquals("foo",br.readLine()); assertEquals("foo",br.readLine());
@ -142,6 +147,7 @@ public class ExtendedBufferedReaderTest {
assertNull(br.readLine()); assertNull(br.readLine());
assertEquals(3, br.getCurrentLineNumber()); assertEquals(3, br.getCurrentLineNumber());
br.close();
br = getBufferedReader("foo\n\nhello"); br = getBufferedReader("foo\n\nhello");
assertEquals('f', br.read()); assertEquals('f', br.read());
assertEquals('o', br.lookAhead()); assertEquals('o', br.lookAhead());
@ -156,6 +162,7 @@ public class ExtendedBufferedReaderTest {
assertEquals(3, br.getCurrentLineNumber()); assertEquals(3, br.getCurrentLineNumber());
br.close();
br = getBufferedReader("foo\rbaar\r\nfoo"); br = getBufferedReader("foo\rbaar\r\nfoo");
assertEquals("foo",br.readLine()); assertEquals("foo",br.readLine());
assertEquals('b', br.lookAhead()); assertEquals('b', br.lookAhead());
@ -163,6 +170,7 @@ public class ExtendedBufferedReaderTest {
assertEquals('f', br.lookAhead()); assertEquals('f', br.lookAhead());
assertEquals("foo",br.readLine()); assertEquals("foo",br.readLine());
assertNull(br.readLine()); assertNull(br.readLine());
br.close();
} }
/* /*
@ -184,6 +192,7 @@ public class ExtendedBufferedReaderTest {
} }
assertEquals(EOLeolct, br.getCurrentLineNumber()); assertEquals(EOLeolct, br.getCurrentLineNumber());
br.close();
br = getBufferedReader(test); br = getBufferedReader(test);
assertEquals(0, br.getCurrentLineNumber()); assertEquals(0, br.getCurrentLineNumber());
while (br.read() != -1) { while (br.read() != -1) {
@ -191,6 +200,7 @@ public class ExtendedBufferedReaderTest {
} }
assertEquals(EOLeolct, br.getCurrentLineNumber()); assertEquals(EOLeolct, br.getCurrentLineNumber());
br.close();
br = getBufferedReader(test); br = getBufferedReader(test);
assertEquals(0, br.getCurrentLineNumber()); assertEquals(0, br.getCurrentLineNumber());
final char[] buff = new char[10]; final char[] buff = new char[10];
@ -198,6 +208,7 @@ public class ExtendedBufferedReaderTest {
// consume all // consume all
} }
assertEquals(EOLeolct, br.getCurrentLineNumber()); assertEquals(EOLeolct, br.getCurrentLineNumber());
br.close();
} }
private ExtendedBufferedReader getBufferedReader(final String s) { private ExtendedBufferedReader getBufferedReader(final String s) {

View File

@ -219,6 +219,7 @@ public class PerformanceTest {
final Stats s = iterate(parser); final Stats s = iterate(parser);
reader.close(); reader.close();
show("CSV", s, t0); show("CSV", s, t0);
parser.close();
} }
show(); show();
} }
@ -266,6 +267,8 @@ public class PerformanceTest {
break; break;
case COMMENT: // not really expecting these case COMMENT: // not really expecting these
break; break;
default:
throw new IllegalStateException("Unexpected Token type: " + token.type);
} }
} while (!token.type.equals(Token.Type.EOF)); } while (!token.type.equals(Token.Type.EOF));