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
22
23
24
25
26
27
28
29
30
31 public class TestXMLData
32 {
33
34
35
36
37
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 }