Skip to content

Commit 1933f8a

Browse files
committed
Avoid resource leak warnings
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1582813 13f79535-47bb-0310-9956-ffa450edef68
1 parent d6294bc commit 1933f8a

6 files changed

Lines changed: 49 additions & 4 deletions

File tree

src/test/java/org/apache/commons/csv/CSVFileParserTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public void testCSVFile() throws Exception {
124124
final int count = record.size();
125125
assertEquals(testName, readTestData(), count+":"+parsed);
126126
}
127+
parser.close();
127128
}
128129

129130
@Test
@@ -167,5 +168,6 @@ public void testCSVUrl() throws Exception {
167168
final int count = record.size();
168169
assertEquals(testName, readTestData(), count + ":" + parsed);
169170
}
171+
parser.close();
170172
}
171173
}

src/test/java/org/apache/commons/csv/CSVParserTest.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public void testBackslashEscaping() throws IOException {
114114
assertTrue(records.size() > 0);
115115

116116
Utils.compare("Records do not match expected result", res, records);
117+
parser.close();
117118
}
118119

119120
@Test
@@ -143,6 +144,7 @@ public void testBackslashEscaping2() throws IOException {
143144
assertTrue(records.size() > 0);
144145

145146
Utils.compare("", res, records);
147+
parser.close();
146148
}
147149

148150
@Test
@@ -176,6 +178,7 @@ public void testBackslashEscapingOld() throws IOException {
176178
for (int i = 0; i < res.length; i++) {
177179
assertArrayEquals(res[i], records.get(i).values());
178180
}
181+
parser.close();
179182
}
180183

181184
@Test
@@ -207,6 +210,7 @@ public void testBOMInputStream() throws IOException {
207210
}
208211
} finally {
209212
parser.close();
213+
reader.close();
210214
}
211215
}
212216

@@ -216,6 +220,7 @@ public void testCarriageReturnEndings() throws IOException {
216220
final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
217221
final List<CSVRecord> records = parser.getRecords();
218222
assertEquals(4, records.size());
223+
parser.close();
219224
}
220225

221226
@Test
@@ -224,6 +229,7 @@ public void testCarriageReturnLineFeedEndings() throws IOException {
224229
final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
225230
final List<CSVRecord> records = parser.getRecords();
226231
assertEquals(4, records.size());
232+
parser.close();
227233
}
228234

229235
@Test(expected = NoSuchElementException.class)
@@ -243,6 +249,7 @@ public void testCSV57() throws Exception {
243249
final List<CSVRecord> list = parser.getRecords();
244250
assertNotNull(list);
245251
assertEquals(0, list.size());
252+
parser.close();
246253
}
247254

248255
@Test
@@ -275,16 +282,19 @@ public void testDefaultFormat() throws IOException {
275282
};
276283

277284
format = CSVFormat.DEFAULT.withCommentStart('#');
285+
parser.close();
278286
parser = CSVParser.parse(code, format);
279287
records = parser.getRecords();
280288

281289
Utils.compare("Failed to parse with comments", res_comments, records);
290+
parser.close();
282291
}
283292

284293
@Test
285294
public void testEmptyFile() throws Exception {
286295
final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT);
287296
assertNull(parser.nextRecord());
297+
parser.close();
288298
}
289299

290300
@Test
@@ -306,6 +316,7 @@ public void testEmptyLineBehaviourCSV() throws Exception {
306316
for (int i = 0; i < res.length; i++) {
307317
assertArrayEquals(res[i], records.get(i).values());
308318
}
319+
parser.close();
309320
}
310321
}
311322

@@ -330,6 +341,7 @@ public void testEmptyLineBehaviourExcel() throws Exception {
330341
for (int i = 0; i < res.length; i++) {
331342
assertArrayEquals(res[i], records.get(i).values());
332343
}
344+
parser.close();
333345
}
334346
}
335347

@@ -357,6 +369,7 @@ public void testEndOfFileBehaviorCSV() throws Exception {
357369
for (int i = 0; i < res.length; i++) {
358370
assertArrayEquals(res[i], records.get(i).values());
359371
}
372+
parser.close();
360373
}
361374
}
362375

@@ -386,6 +399,7 @@ public void testEndOfFileBehaviourExcel() throws Exception {
386399
for (int i = 0; i < res.length; i++) {
387400
assertArrayEquals(res[i], records.get(i).values());
388401
}
402+
parser.close();
389403
}
390404
}
391405

@@ -408,6 +422,7 @@ public void testExcelFormat1() throws IOException {
408422
for (int i = 0; i < res.length; i++) {
409423
assertArrayEquals(res[i], records.get(i).values());
410424
}
425+
parser.close();
411426
}
412427

413428
@Test
@@ -427,6 +442,7 @@ public void testExcelFormat2() throws Exception {
427442
for (int i = 0; i < res.length; i++) {
428443
assertArrayEquals(res[i], records.get(i).values());
429444
}
445+
parser.close();
430446
}
431447

432448
@Test
@@ -466,6 +482,7 @@ public void testGetHeaderMap() throws Exception {
466482
}
467483

468484
assertFalse(records.hasNext());
485+
parser.close();
469486
}
470487

471488
@Test
@@ -476,6 +493,7 @@ public void testGetLine() throws IOException {
476493
}
477494

478495
assertNull(parser.nextRecord());
496+
parser.close();
479497
}
480498

481499
@Test
@@ -517,6 +535,7 @@ public void testGetRecords() throws IOException {
517535
for (int i = 0; i < RESULT.length; i++) {
518536
assertArrayEquals(RESULT[i], records.get(i).values());
519537
}
538+
parser.close();
520539
}
521540

522541
@Test
@@ -541,6 +560,7 @@ public void testGetRecordWithMultiLineValues() throws Exception {
541560
assertNull(record = parser.nextRecord());
542561
assertEquals(8, parser.getCurrentLineNumber());
543562
assertEquals(3, parser.getRecordNumber());
563+
parser.close();
544564
}
545565

546566
@Test
@@ -585,6 +605,7 @@ public void testIgnoreEmptyLines() throws IOException {
585605
final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
586606
final List<CSVRecord> records = parser.getRecords();
587607
assertEquals(3, records.size());
608+
parser.close();
588609
}
589610

590611
@Test(expected = IllegalArgumentException.class)
@@ -628,6 +649,7 @@ public void testLineFeedEndings() throws IOException {
628649
final CSVParser parser = CSVParser.parse(code, CSVFormat.DEFAULT);
629650
final List<CSVRecord> records = parser.getRecords();
630651
assertEquals(4, records.size());
652+
parser.close();
631653
}
632654

633655
@Test
@@ -681,22 +703,24 @@ public void testMultipleIterators() throws Exception {
681703
assertEquals("d", second.get(0));
682704
assertEquals("e", second.get(1));
683705
assertEquals("f", second.get(2));
706+
parser.close();
684707
}
685708

686709
@Test(expected = IllegalArgumentException.class)
687710
public void testNewCSVParserNullReaderFormat() throws Exception {
688-
new CSVParser(null, CSVFormat.DEFAULT);
711+
new CSVParser(null, CSVFormat.DEFAULT).close();
689712
}
690713

691714
@Test(expected = IllegalArgumentException.class)
692715
public void testNewCSVParserReaderNullFormat() throws Exception {
693-
new CSVParser(new StringReader(""), null);
716+
new CSVParser(new StringReader(""), null).close();
694717
}
695718

696719
@Test
697720
public void testNoHeaderMap() throws Exception {
698721
final CSVParser parser = CSVParser.parse("a,b,c\n1,2,3\nx,y,z", CSVFormat.DEFAULT);
699722
Assert.assertNull(parser.getHeaderMap());
723+
parser.close();
700724
}
701725

702726
@Test(expected = IllegalArgumentException.class)
@@ -824,6 +848,7 @@ private void validateLineNumbers(final String lineSeparator) throws IOException
824848
assertNull(parser.nextRecord());
825849
// Still 2 because the last line is does not have EOL chars
826850
assertEquals(2, parser.getCurrentLineNumber());
851+
parser.close();
827852
}
828853

829854
private void validateRecordNumbers(final String lineSeparator) throws IOException {
@@ -841,6 +866,7 @@ private void validateRecordNumbers(final String lineSeparator) throws IOExceptio
841866
assertEquals(3, parser.getRecordNumber());
842867
assertNull(record = parser.nextRecord());
843868
assertEquals(3, parser.getRecordNumber());
869+
parser.close();
844870
}
845871

846872
}

src/test/java/org/apache/commons/csv/CSVPrinterTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ private void doOneRandom(final CSVFormat format) throws Exception {
8989
final List<CSVRecord> parseResult = parser.getRecords();
9090

9191
Utils.compare("Printer output :" + printable(result), lines, parseResult);
92+
parser.close();
9293
}
9394

9495
private void doRandom(final CSVFormat format, final int iter) throws Exception {
@@ -336,6 +337,7 @@ public void testParseCustomNullValues() throws IOException {
336337
assertEquals(null, record.get(1));
337338
assertEquals("b", record.get(2));
338339
assertFalse(iterator.hasNext());
340+
((CSVParser) iterable).close();
339341
}
340342

341343
@Test
@@ -492,11 +494,11 @@ public void testInvalidFormat() throws Exception {
492494

493495
@Test(expected = IllegalArgumentException.class)
494496
public void testNewCSVPrinterNullAppendableFormat() throws Exception {
495-
new CSVPrinter(null, CSVFormat.DEFAULT);
497+
new CSVPrinter(null, CSVFormat.DEFAULT).close();
496498
}
497499

498500
@Test(expected = IllegalArgumentException.class)
499501
public void testNewCsvPrinterAppendableNullFormat() throws Exception {
500-
new CSVPrinter(new StringWriter(), null);
502+
new CSVPrinter(new StringWriter(), null).close();
501503
}
502504
}

src/test/java/org/apache/commons/csv/CSVRecordTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ public void testRemoveAndAddColumns() throws IOException {
151151
Collections.sort(list);
152152
printer.printRecord(list);
153153
Assert.assertEquals("A,B,C,NewValue" + CSVFormat.DEFAULT.getRecordSeparator(), printer.getOut().toString());
154+
printer.close();
154155
}
155156

156157
@Test

src/test/java/org/apache/commons/csv/ExtendedBufferedReaderTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public void testEmptyInput() throws Exception {
4242
assertEquals(END_OF_STREAM, br.getLastChar());
4343
assertNull(br.readLine());
4444
assertEquals(0, br.read(new char[10], 0, 0));
45+
br.close();
4546
}
4647

4748
@Test
@@ -100,6 +101,7 @@ public void testReadLookahead1() throws Exception {
100101
assertEquals(END_OF_STREAM, br.lookAhead());
101102
assertEquals(3, br.getCurrentLineNumber());
102103

104+
br.close();
103105
}
104106

105107
@Test
@@ -120,17 +122,20 @@ public void testReadLookahead2() throws Exception {
120122
assertEquals(1, br.read(res, 4, 1));
121123
assertArrayEquals(ref, res);
122124
assertEquals('d', br.getLastChar());
125+
br.close();
123126
}
124127

125128
@Test
126129
public void testReadLine() throws Exception {
127130
ExtendedBufferedReader br = getBufferedReader("");
128131
assertNull(br.readLine());
129132

133+
br.close();
130134
br = getBufferedReader("\n");
131135
assertEquals("",br.readLine());
132136
assertNull(br.readLine());
133137

138+
br.close();
134139
br = getBufferedReader("foo\n\nhello");
135140
assertEquals(0, br.getCurrentLineNumber());
136141
assertEquals("foo",br.readLine());
@@ -142,6 +147,7 @@ public void testReadLine() throws Exception {
142147
assertNull(br.readLine());
143148
assertEquals(3, br.getCurrentLineNumber());
144149

150+
br.close();
145151
br = getBufferedReader("foo\n\nhello");
146152
assertEquals('f', br.read());
147153
assertEquals('o', br.lookAhead());
@@ -156,13 +162,15 @@ public void testReadLine() throws Exception {
156162
assertEquals(3, br.getCurrentLineNumber());
157163

158164

165+
br.close();
159166
br = getBufferedReader("foo\rbaar\r\nfoo");
160167
assertEquals("foo",br.readLine());
161168
assertEquals('b', br.lookAhead());
162169
assertEquals("baar",br.readLine());
163170
assertEquals('f', br.lookAhead());
164171
assertEquals("foo",br.readLine());
165172
assertNull(br.readLine());
173+
br.close();
166174
}
167175

168176
/*
@@ -184,20 +192,23 @@ public void testReadChar() throws Exception {
184192
}
185193
assertEquals(EOLeolct, br.getCurrentLineNumber());
186194

195+
br.close();
187196
br = getBufferedReader(test);
188197
assertEquals(0, br.getCurrentLineNumber());
189198
while (br.read() != -1) {
190199
// consume all
191200
}
192201
assertEquals(EOLeolct, br.getCurrentLineNumber());
193202

203+
br.close();
194204
br = getBufferedReader(test);
195205
assertEquals(0, br.getCurrentLineNumber());
196206
final char[] buff = new char[10];
197207
while (br.read(buff, 0, 3) != -1) {
198208
// consume all
199209
}
200210
assertEquals(EOLeolct, br.getCurrentLineNumber());
211+
br.close();
201212
}
202213

203214
private ExtendedBufferedReader getBufferedReader(final String s) {

src/test/java/org/apache/commons/csv/PerformanceTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ private static void testParseCommonsCSV() throws Exception {
219219
final Stats s = iterate(parser);
220220
reader.close();
221221
show("CSV", s, t0);
222+
parser.close();
222223
}
223224
show();
224225
}
@@ -266,6 +267,8 @@ private static void testCSVLexer(final boolean newToken, final String test) thro
266267
break;
267268
case COMMENT: // not really expecting these
268269
break;
270+
default:
271+
throw new IllegalStateException("Unexpected Token type: " + token.type);
269272
}
270273

271274
} while (!token.type.equals(Token.Type.EOF));

0 commit comments

Comments
 (0)