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