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