3131package org .scijava .table .io ;
3232
3333import static org .junit .Assert .assertEquals ;
34- import static org .junit .Assert .assertTrue ;
34+ import static org .junit .Assert .assertFalse ;
3535import static org .junit .Assert .fail ;
3636import static org .scijava .table .io .DefaultTableIOPlugin .guessParser ;
3737
3838import java .io .File ;
3939import java .io .IOException ;
40- import java .util .ArrayList ;
4140import java .util .Arrays ;
42- import java .util .List ;
4341
4442import org .junit .After ;
4543import org .junit .Before ;
4644import org .junit .Test ;
4745import org .scijava .Context ;
48- import org .scijava .io .IOPlugin ;
4946import org .scijava .io .IOService ;
5047import org .scijava .io .handle .DataHandle ;
5148import org .scijava .io .handle .DataHandleService ;
5956 * Tests for {@link DefaultTableIOPlugin}.
6057 *
6158 * @author Leon Yang
59+ * @author Curtis Rueden
6260 */
6361@ SuppressWarnings ("rawtypes" )
6462public class DefaultTableIOPluginTest {
6563
66- private static final Context ctx = new Context ();
67-
68- private final List <File > tempFiles = new ArrayList <>();
64+ private Context ctx ;
65+ private DefaultTableIOPlugin tableIO ;
6966
7067 @ Before
68+ public void setUp () {
69+ ctx = new Context (IOService .class , DataHandleService .class );
70+ tableIO = ctx .service (IOService .class )//
71+ .getInstance (DefaultTableIOPlugin .class );
72+ }
73+
7174 @ After
72- public void removeTempFiles () {
73- final Location source = new FileLocation ("fake.csv" );
74- new File (source .getURI ()).delete ();
75- for (final File f : tempFiles ) {
76- assertTrue (f .delete ());
77- }
75+ public void tearDown () {
76+ ctx .dispose ();
77+ ctx = null ;
78+ tableIO = null ;
7879 }
7980
8081 /**
@@ -102,12 +103,9 @@ public void testDefaultOptions() throws IOException {
102103
103104 assertTableEquals (colHeaders , rowHeaders , content , table );
104105
105- final TableIOPlugin tableIO = //
106- ctx .service (IOService .class ).getInstance (DefaultTableIOPlugin .class );
107-
108- assertEquals (expected , saveTable (table , tableIO , TableIOOptions .options ()));
106+ assertEquals (expected , saveTable (table , TableIOOptions .options ()));
109107
110- final Table table2 = openTable (expected , tableIO , TableIOOptions .options ());
108+ final Table table2 = openTable (expected , TableIOOptions .options ());
111109
112110 assertTableEquals (colHeaders , rowHeaders , content , table2 );
113111 }
@@ -136,8 +134,6 @@ public void testQuote() {
136134 "'should\t not,break',unnecessary_quotes,should,break\r \n " +
137135 "'some,empty,cells','','',''\r \n " ;
138136
139- final TableIOPlugin tableIO = //
140- ctx .service (IOService .class ).getInstance (DefaultTableIOPlugin .class );
141137 try {
142138 final TableIOOptions options = TableIOOptions .options ()//
143139 .readColumnHeaders (true )//
@@ -148,15 +144,14 @@ public void testQuote() {
148144 .rowDelimiter ("\r \n " )//
149145 .quote ('\'' )//
150146 .cornerText ("CORNER_TEXT" );
151- final Table table = openTable (tableSource , tableIO , options );
147+ final Table table = openTable (tableSource , options );
152148 assertTableEquals (colHeaders , rowHeaders , content , table );
153149
154150 options .columnDelimiter (',' );
155- assertEquals (expected , saveTable (table , tableIO , options ));
151+ assertEquals (expected , saveTable (table , options ));
156152 }
157153 catch (final Exception exc ) {
158- exc .printStackTrace ();
159- fail (exc .getMessage ());
154+ throw new AssertionError (exc .getMessage (), exc );
160155 }
161156 }
162157
@@ -180,8 +175,6 @@ public void testSmallTables() {
180175 final Double [][] content = { { 3.1415926 } };
181176 final Double [][] emptyContent = { {} };
182177
183- final TableIOPlugin tableIO = //
184- ctx .service (IOService .class ).getInstance (DefaultTableIOPlugin .class );
185178 try {
186179 Table table ;
187180 String expected ;
@@ -196,45 +189,45 @@ public void testSmallTables() {
196189 .quote ('\'' )//
197190 .parser (Double ::valueOf )//
198191 .formatter (val -> String .format ("%.3f" , val ));
199- table = openTable (makeTableSource (singleRow , "," , "\n " ), tableIO ,
192+ table = openTable (makeTableSource (singleRow , "," , "\n " ),
200193 options );
201194 assertTableEquals (emptyHeader , singleRowHeader , content , table );
202195 expected = "Row Header,3.142\n " ;
203- assertEquals (expected , saveTable (table , tableIO , options ));
196+ assertEquals (expected , saveTable (table , options ));
204197
205198 options .readRowHeaders (false ).writeRowHeaders (false );
206- table = openTable (makeTableSource (singleCell , "," , "\n " ), tableIO ,
199+ table = openTable (makeTableSource (singleCell , "," , "\n " ),
207200 options );
208201 assertTableEquals (emptyHeader , emptyHeader , content , table );
209202 expected = "3.142\n " ;
210- assertEquals (expected , saveTable (table , tableIO , options ));
203+ assertEquals (expected , saveTable (table , options ));
211204
212205 options .readColumnHeaders (true ).writeColumnHeaders (true );
213- table = openTable (makeTableSource (singleCol , "," , "\n " ), tableIO ,
206+ table = openTable (makeTableSource (singleCol , "," , "\n " ),
214207 options );
215208 assertTableEquals (singleColHeader , emptyHeader , content , table );
216209 expected = "Col Header\n 3.142\n " ;
217- assertEquals (expected , saveTable (table , tableIO , options ));
210+ assertEquals (expected , saveTable (table , options ));
218211
219212 options .readRowHeaders (true );
220- table = openTable (makeTableSource (onlyColHeader , "," , "\n " ), tableIO ,
213+ table = openTable (makeTableSource (onlyColHeader , "," , "\n " ),
221214 options );
222215 assertTableEquals (singleColHeader , empty , emptyContent , table );
223216 expected = "Col Header\n " ;
224- assertEquals (expected , saveTable (table , tableIO , options ));
217+ assertEquals (expected , saveTable (table , options ));
225218
226219 options .writeColumnHeaders (false ).writeRowHeaders (true );
227- table = openTable (makeTableSource (onlyRowHeader , "," , "\n " ), tableIO ,
220+ table = openTable (makeTableSource (onlyRowHeader , "," , "\n " ),
228221 options );
229222 assertTableEquals (empty , singleRowHeader , emptyContent , table );
230223 expected = "Row Header\n " ;
231- assertEquals (expected , saveTable (table , tableIO , options ));
224+ assertEquals (expected , saveTable (table , options ));
232225
233226 options .writeColumnHeaders (true );
234- table = openTable (makeTableSource (full , "," , "\n " ), tableIO , options );
227+ table = openTable (makeTableSource (full , "," , "\n " ), options );
235228 assertTableEquals (singleColHeader , singleRowHeader , content , table );
236229 expected = "CORNER TEXT,Col Header\n Row Header,3.142\n " ;
237- assertEquals (expected , saveTable (table , tableIO , options ));
230+ assertEquals (expected , saveTable (table , options ));
238231 }
239232 catch (final Exception exc ) {
240233 exc .printStackTrace ();
@@ -245,9 +238,9 @@ public void testSmallTables() {
245238
246239 @ Test (expected = IOException .class )
247240 public void testOpenNonExist () throws IOException {
248- final IOPlugin < Table > tableIO = //
249- ctx . service ( IOService . class ). getInstance ( DefaultTableIOPlugin . class );
250- tableIO .open ("fake.csv" );
241+ final File nonExistentCSV = new File ( "thisFileDoesNotExist.csv" );
242+ assertFalse ( nonExistentCSV . exists () );
243+ tableIO .open (nonExistentCSV . getPath () );
251244 }
252245
253246 @ Test
@@ -291,14 +284,13 @@ private void assertTableEquals(final String[] colHeaders,
291284 }
292285 }
293286
294- private Table openTable (final String tableSource , final TableIOPlugin tableIO ,
287+ private Table openTable (final String tableSource ,
295288 final TableIOOptions options ) throws IOException
296289 {
297290 final DataHandleService dataHandleService = //
298291 ctx .service (DataHandleService .class );
299292 Table result ;
300- final File tempFile = File .createTempFile ("openTest" , ".txt" );
301- tempFiles .add (tempFile );
293+ final File tempFile = createTempFile ("openTable" );
302294 try (final DataHandle <Location > destHandle = //
303295 dataHandleService .create (new FileLocation (tempFile )))
304296 {
@@ -308,14 +300,13 @@ private Table openTable(final String tableSource, final TableIOPlugin tableIO,
308300 return result ;
309301 }
310302
311- private String saveTable (final Table table , final TableIOPlugin tableIO ,
312- final TableIOOptions options ) throws IOException
303+ private String saveTable (final Table table , final TableIOOptions options )
304+ throws IOException
313305 {
314306 final DataHandleService dataHandleService = //
315307 ctx .service (DataHandleService .class );
316308 String result ;
317- final File tempFile = File .createTempFile ("saveTest" , ".txt" );
318- tempFiles .add (tempFile );
309+ final File tempFile = createTempFile ("saveTable" );
319310 try (final DataHandle <Location > sourceHandle = //
320311 dataHandleService .create (new FileLocation (tempFile )))
321312 {
@@ -334,4 +325,11 @@ private String makeTableSource(final String[][] cells, final String separator,
334325 }
335326 return table .toString ();
336327 }
328+
329+ private File createTempFile (final String prefix ) throws IOException {
330+ final File tempFile = //
331+ File .createTempFile (getClass ().getName () + "." + prefix , ".txt" );
332+ tempFile .deleteOnExit ();
333+ return tempFile ;
334+ }
337335}
0 commit comments