View Javadoc
1   package org.djutils.data;
2   
3   import static org.junit.Assert.assertArrayEquals;
4   import static org.junit.Assert.assertEquals;
5   import static org.junit.Assert.assertFalse;
6   import static org.junit.Assert.assertTrue;
7   
8   import java.io.File;
9   import java.io.IOException;
10  import java.util.ArrayList;
11  import java.util.Iterator;
12  import java.util.List;
13  
14  import org.djutils.data.csv.CSVData;
15  import org.djutils.data.serialization.TextSerializationException;
16  import org.junit.Test;
17  
18  /**
19   * TestCSVData tests writing and reading of a CSV file, and checks that all data is read back correctly into the DataTable. <br>
20   * <br>
21   * Copyright (c) 2020-2022 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
22   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
23   * distributed under a three-clause BSD-style license, which can be found at
24   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>. <br>
25   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
26   * @author <a href="https://www.tudelft.nl/pknoppers">Peter Knoppers</a>
27   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
28   */
29  public class TestCSVData
30  {
31      /**
32       * test reading and writing of a CSV file.
33       * @throws IOException on error
34       * @throws TextSerializationException on unknown data type for (de)serialization
35       */
36      @Test
37      public void testreadWriteCSV() throws IOException, TextSerializationException
38      {
39          File tempDataFile = File.createTempFile("testdata", ".csv");
40          File tempMetaDataFile = File.createTempFile("testmetadata", ".csv");
41          tempDataFile.deleteOnExit();
42          tempMetaDataFile.deleteOnExit();
43  
44          DataColumn<Integer> column1 = new SimpleDataColumn<>("time", "time, rounded to second [s]", int.class);
45          DataColumn<Double> column2 = new SimpleDataColumn<>("value", "measured value [m]", double.class);
46          DataColumn<String> column3 = new SimpleDataColumn<>("remark", "remark about the measurement", String.class);
47          List<DataColumn<?>> columns = new ArrayList<>();
48          columns.add(column1);
49          columns.add(column2);
50          columns.add(column3);
51          ListDataTable table1 = new ListDataTable("tableId", "tableDescription", columns);
52          table1.addRecord(new Object[] {1, 5.0, "normal"});
53          table1.addRecord(new Object[] {2, 10.0, "normal"});
54          table1.addRecord(new Object[] {3, 15.0, "normal"});
55          table1.addRecord(new Object[] {4, 20.0, "abnormal"});
56          CSVData.writeData(tempDataFile.getAbsolutePath(), tempMetaDataFile.getAbsolutePath(), table1);
57  
58          DataTable table2 = CSVData.readData(tempDataFile.getAbsolutePath(), tempMetaDataFile.getAbsolutePath());
59          assertTrue(table2 instanceof ListDataTable);
60          assertEquals(table1.getId(), table2.getId());
61          assertEquals(table1.getDescription(), table2.getDescription());
62          assertEquals(table1.getNumberOfColumns(), table2.getNumberOfColumns());
63          assertArrayEquals(table1.getColumnIds(), table2.getColumnIds());
64          assertArrayEquals(table1.getColumnDescriptions(), table2.getColumnDescriptions());
65          assertArrayEquals(table1.getColumnDataTypes(), table2.getColumnDataTypes());
66          assertArrayEquals(table1.getColumnDataTypeStrings(), table2.getColumnDataTypeStrings());
67  
68          Iterator<DataRecord> it1 = table1.iterator();
69          Iterator<DataRecord> it2 = table2.iterator();
70          while (it1.hasNext() && it2.hasNext())
71          {
72              DataRecord r1 = it1.next();
73              DataRecord r2 = it2.next();
74              assertArrayEquals(r1.getValues(), r2.getValues());
75          }
76          assertFalse(it1.hasNext());
77          assertFalse(it2.hasNext());
78      }
79  }