Skip to content

Commit 5cb89a9

Browse files
committed
Fix tests on Windows
On Windows, files created during the tests could not be deleted, because of DataHandle objects not being closed. This commit fixes the issue by using try-with-resources to make sure the handles get closed after reading/writing.
1 parent 35c09a5 commit 5cb89a9

2 files changed

Lines changed: 131 additions & 111 deletions

File tree

src/main/java/net/imagej/table/DefaultTableIOPlugin.java

Lines changed: 103 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -191,131 +191,138 @@ else if (line.charAt(idx) == separator) {
191191
public GenericTable open(final String source) throws IOException {
192192
// FIXME Assumes FileLocation
193193
final Location sourceLocation = new FileLocation(source);
194-
final DataHandle<? extends Location> handle = //
195-
dataHandleService.create(sourceLocation);
196-
if (!handle.exists()) {
197-
throw new IOException("Cannot open source");
198-
}
199-
long length = handle.length();
200-
201-
final byte[] buffer = new byte[(int) length];
202-
handle.read(buffer);
203-
final String text = new String(buffer);
204-
205194
final GenericTable table = new DefaultGenericTable();
206195

207-
// split by any line delimiter
208-
final String[] lines = text.split("\\R");
209-
if (lines.length == 0) return table;
210-
// process first line to get number of cols
196+
try (final DataHandle<? extends Location> handle = //
197+
dataHandleService.create(sourceLocation))
211198
{
212-
final ArrayList<String> tokens = processRow(lines[0]);
213-
if (readColHeaders) {
214-
final List<String> colHeaders;
215-
if (readRowHeaders) colHeaders = tokens.subList(1, tokens.size());
216-
else colHeaders = tokens;
217-
final String[] colHeadersArr = new String[colHeaders.size()];
218-
table.appendColumns(colHeaders.toArray(colHeadersArr));
199+
if (!handle.exists()) {
200+
throw new IOException("Cannot open source");
219201
}
220-
else {
202+
long length = handle.length();
203+
204+
final byte[] buffer = new byte[(int) length];
205+
handle.read(buffer);
206+
207+
final String text = new String(buffer);
208+
209+
210+
// split by any line delimiter
211+
final String[] lines = text.split("\\R");
212+
if (lines.length == 0) return table;
213+
// process first line to get number of cols
214+
{
215+
final ArrayList<String> tokens = processRow(lines[0]);
216+
if (readColHeaders) {
217+
final List<String> colHeaders;
218+
if (readRowHeaders) colHeaders = tokens.subList(1, tokens.size());
219+
else colHeaders = tokens;
220+
final String[] colHeadersArr = new String[colHeaders.size()];
221+
table.appendColumns(colHeaders.toArray(colHeadersArr));
222+
}
223+
else {
224+
final List<String> cols;
225+
if (readRowHeaders) {
226+
cols = tokens.subList(1, tokens.size());
227+
table.appendColumns(cols.size());
228+
table.appendRow(tokens.get(0));
229+
}
230+
else {
231+
cols = tokens;
232+
table.appendColumns(cols.size());
233+
table.appendRow();
234+
}
235+
for (int i = 0; i < cols.size(); i++) {
236+
table.set(i, 0, parser.apply(cols.get(i)));
237+
}
238+
}
239+
}
240+
for (int lineNum = 1; lineNum < lines.length; lineNum++) {
241+
final String line = lines[lineNum];
242+
final ArrayList<String> tokens = processRow(line);
221243
final List<String> cols;
222244
if (readRowHeaders) {
223245
cols = tokens.subList(1, tokens.size());
224-
table.appendColumns(cols.size());
225246
table.appendRow(tokens.get(0));
226247
}
227248
else {
228249
cols = tokens;
229-
table.appendColumns(cols.size());
230250
table.appendRow();
231251
}
252+
if (cols.size() != table.getColumnCount()) {
253+
throw new IOException("Line " + table.getRowCount() +
254+
" is not the same length as the first line.");
255+
}
232256
for (int i = 0; i < cols.size(); i++) {
233-
table.set(i, 0, parser.apply(cols.get(i)));
257+
table.set(i, lineNum - 1, parser.apply(cols.get(i)));
234258
}
235259
}
236260
}
237-
for (int lineNum = 1; lineNum < lines.length; lineNum++) {
238-
final String line = lines[lineNum];
239-
final ArrayList<String> tokens = processRow(line);
240-
final List<String> cols;
241-
if (readRowHeaders) {
242-
cols = tokens.subList(1, tokens.size());
243-
table.appendRow(tokens.get(0));
244-
}
245-
else {
246-
cols = tokens;
247-
table.appendRow();
248-
}
249-
if (cols.size() != table.getColumnCount()) {
250-
throw new IOException("Line " + table.getRowCount() +
251-
" is not the same length as the first line.");
252-
}
253-
for (int i = 0; i < cols.size(); i++) {
254-
table.set(i, lineNum - 1, parser.apply(cols.get(i)));
255-
}
256-
}
257261
return table;
258262
}
259263

260264
@Override
261-
public void save(final GenericTable table, final String source)
265+
public void save(final GenericTable table, final String destination)
262266
throws IOException
263267
{
264268
// FIXME Assumes FileLocation
265-
final Location sourceLocation = new FileLocation(source);
266-
final DataHandle<Location> handle = //
267-
dataHandleService.create(sourceLocation);
269+
final Location dstLocation = new FileLocation(destination);
268270

269-
final boolean writeRH = this.writeRowHeaders && table.getRowCount() > 0 &&
270-
IntStream.range(0, table.getRowCount()).allMatch(row -> table
271-
.getRowHeader(row) != null);
272-
final boolean writeCH = this.writeColHeaders && table
273-
.getColumnCount() > 0 && table.stream().allMatch(col -> col
274-
.getHeader() != null);
271+
try (final DataHandle<Location> handle = //
272+
dataHandleService.create(dstLocation))
273+
{
274+
final boolean writeRH = this.writeRowHeaders && table.getRowCount() > 0 &&
275+
IntStream.range(0, table.getRowCount()).allMatch(row -> table
276+
.getRowHeader(row) != null);
277+
final boolean writeCH = this.writeColHeaders && table
278+
.getColumnCount() > 0 && table.stream().allMatch(col -> col
279+
.getHeader() != null);
275280

276-
final StringBuilder sb = new StringBuilder();
277-
// write column headers
278-
if (writeCH) {
279-
if (writeRH) {
280-
sb.append(tryQuote(cornerText));
281-
if (table.getColumnCount() > 0) {
282-
sb.append(separator);
283-
sb.append(tryQuote(table.getColumnHeader(0)));
281+
final StringBuilder sb = new StringBuilder();
282+
// write column headers
283+
if (writeCH) {
284+
if (writeRH) {
285+
sb.append(tryQuote(cornerText));
286+
if (table.getColumnCount() > 0) {
287+
sb.append(separator);
288+
sb.append(tryQuote(table.getColumnHeader(0)));
289+
}
290+
}
291+
// avoid adding extra separator when there is 0 column
292+
else if (table.getColumnCount() > 0) {
293+
sb.append(tryQuote(table.getColumnHeader(0)));
294+
}
295+
for (int col = 1; col < table.getColumnCount(); col++) {
296+
sb.append(separator);
297+
sb.append(tryQuote(table.getColumnHeader(col)));
298+
}
299+
sb.append(eol);
300+
handle.writeBytes(sb.toString());
301+
sb.setLength(0);
284302
}
285-
}
286-
// avoid adding extra separator when there is 0 column
287-
else if (table.getColumnCount() > 0) {
288-
sb.append(tryQuote(table.getColumnHeader(0)));
289-
}
290-
for (int col = 1; col < table.getColumnCount(); col++) {
291-
sb.append(separator);
292-
sb.append(tryQuote(table.getColumnHeader(col)));
293-
}
294-
sb.append(eol);
295-
handle.writeBytes(sb.toString());
296-
sb.setLength(0);
297-
}
298-
// write each row
299-
for (int row = 0; row < table.getRowCount(); row++) {
300-
if (writeRH) {
301-
sb.append(tryQuote(table.getRowHeader(row)));
302-
if (table.getColumnCount() > 0) {
303-
sb.append(separator);
304-
sb.append(tryQuote(formatter.apply(table.get(0, row))));
303+
// write each row
304+
for (int row = 0; row < table.getRowCount(); row++) {
305+
if (writeRH) {
306+
sb.append(tryQuote(table.getRowHeader(row)));
307+
if (table.getColumnCount() > 0) {
308+
sb.append(separator);
309+
sb.append(tryQuote(formatter.apply(table.get(0, row))));
310+
}
311+
}
312+
// avoid adding extra separator when there is 0 column
313+
else if (table.getColumnCount() > 0) {
314+
sb.append(tryQuote(formatter.apply(table.get(0, row))));
315+
}
316+
for (int col = 1; col < table.getColumnCount(); col++) {
317+
sb.append(separator);
318+
sb.append(tryQuote(formatter.apply(table.get(col, row))));
319+
}
320+
sb.append(eol);
321+
handle.writeBytes(sb.toString());
322+
sb.setLength(0);
305323
}
306-
}
307-
// avoid adding extra separator when there is 0 column
308-
else if (table.getColumnCount() > 0) {
309-
sb.append(tryQuote(formatter.apply(table.get(0, row))));
310-
}
311-
for (int col = 1; col < table.getColumnCount(); col++) {
312-
sb.append(separator);
313-
sb.append(tryQuote(formatter.apply(table.get(col, row))));
314-
}
315-
sb.append(eol);
316-
handle.writeBytes(sb.toString());
317-
sb.setLength(0);
318324
}
325+
319326
}
320327

321328
/**

src/test/java/net/imagej/table/DefaultTableIOPluginTest.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131
package net.imagej.table;
3232

3333
import static org.junit.Assert.assertEquals;
34+
import static org.junit.Assert.assertTrue;
3435
import static org.junit.Assert.fail;
3536

3637
import java.io.File;
3738
import java.io.IOException;
3839
import java.lang.reflect.Field;
40+
import java.util.ArrayList;
3941
import java.util.HashMap;
4042
import java.util.List;
4143
import java.util.function.Function;
@@ -44,11 +46,11 @@
4446
import org.junit.Before;
4547
import org.junit.Test;
4648
import org.scijava.Context;
49+
import org.scijava.io.IOPlugin;
50+
import org.scijava.io.IOService;
4751
import org.scijava.io.handle.DataHandle;
4852
import org.scijava.io.handle.DataHandleService;
4953
import org.scijava.io.location.FileLocation;
50-
import org.scijava.io.IOPlugin;
51-
import org.scijava.io.IOService;
5254
import org.scijava.io.location.Location;
5355
import org.scijava.plugin.Parameter;
5456
import org.scijava.util.ClassUtils;
@@ -61,14 +63,17 @@
6163
public class DefaultTableIOPluginTest {
6264

6365
private static final Context ctx = new Context();
66+
67+
private List<File> tempFiles = new ArrayList<>();
6468

6569
@Before
6670
@After
6771
public void removeTempFiles() {
68-
Location source = new FileLocation("table.txt");
69-
new File(source.getURI()).delete();
70-
source = new FileLocation("fake.csv");
72+
Location source = new FileLocation("fake.csv");
7173
new File(source.getURI()).delete();
74+
for (File f : tempFiles) {
75+
assertTrue(f.delete());
76+
}
7277
}
7378

7479
/**
@@ -263,21 +268,29 @@ private void assertTableEquals(final String[] colHeaders,
263268
private GenericTable openTable(final String tableSource,
264269
final IOPlugin<GenericTable> tableIO) throws IOException
265270
{
266-
final Location dest = new FileLocation("table.txt");
267-
final DataHandle<? extends Location> destHandle = ctx.service(DataHandleService.class).create(dest);
268-
destHandle.write(tableSource.getBytes());
269-
return tableIO.open("table.txt");
271+
final DataHandleService dataHandleService = ctx.service(DataHandleService.class);
272+
GenericTable result;
273+
File tempFile = File.createTempFile("openTest", ".txt");
274+
tempFiles.add(tempFile);
275+
try (DataHandle<Location> destHandle = dataHandleService.create(new FileLocation(tempFile))) {
276+
destHandle.write(tableSource.getBytes());
277+
result = tableIO.open(tempFile.getAbsolutePath());
278+
}
279+
return result;
270280
}
271281

272282
private String saveTable(final GenericTable table,
273283
final IOPlugin<GenericTable> tableIO) throws IOException
274284
{
275-
final Location source = new FileLocation("table.txt");
276-
// Remove file during tests
277-
new File(source.getURI()).delete();
278-
final DataHandle<? extends Location> sourceHandle = ctx.service(DataHandleService.class).create(source);
279-
tableIO.save(table, "table.txt");
280-
return sourceHandle.readString(Integer.MAX_VALUE);
285+
final DataHandleService dataHandleService = ctx.service(DataHandleService.class);
286+
String result;
287+
File tempFile = File.createTempFile("saveTest", ".txt");
288+
tempFiles.add(tempFile);
289+
try (DataHandle<Location> sourceHandle = dataHandleService.create(new FileLocation(tempFile))) {
290+
tableIO.save(table, tempFile.getAbsolutePath());
291+
result = sourceHandle.readString(Integer.MAX_VALUE);
292+
}
293+
return result;
281294
}
282295

283296
private void setValues(final Object instance, final String[] fieldNames,

0 commit comments

Comments
 (0)