Search in sources :

Example 1 with CSVSerializerListener

use of org.apache.pivot.serialization.CSVSerializerListener in project pivot by apache.

the class CSVSerializerTest method testBasicReadObject.

@SuppressWarnings("unchecked")
@Test
public void testBasicReadObject() throws IOException, SerializationException {
    // Test multiple line break formats
    StringBuilder buf = new StringBuilder();
    buf.append("a1,b1,c1\r\n");
    buf.append("a2,b2,c2\n");
    buf.append("a3,b3,c3\r");
    buf.append("a4,b4,c4");
    StringReader reader = new StringReader(buf.toString());
    CSVSerializer serializer = new CSVSerializer();
    serializer.setKeys("A", "B", "C");
    serializer.getCSVSerializerListeners().add(new CSVSerializerListener() {

        @Override
        public void beginList(CSVSerializer csvSerializer, List<?> list) {
            System.out.println("Begin list: " + list);
        }

        @Override
        public void endList(CSVSerializer csvSerializer) {
            System.out.println("End list");
        }

        @Override
        public void readItem(CSVSerializer csvSerializer, Object item) {
            System.out.println("Read item: " + item);
        }
    });
    List<?> result = serializer.readObject(reader);
    Dictionary<String, Object> row;
    // Test the first row
    row = (Dictionary<String, Object>) result.get(0);
    assertEquals(row.get("A"), "a1");
    assertEquals(row.get("B"), "b1");
    assertEquals(row.get("C"), "c1");
    // Test the second row
    row = (Dictionary<String, Object>) result.get(1);
    assertEquals(row.get("A"), "a2");
    assertEquals(row.get("B"), "b2");
    assertEquals(row.get("C"), "c2");
    // Test the third row
    row = (Dictionary<String, Object>) result.get(2);
    assertEquals(row.get("A"), "a3");
    assertEquals(row.get("B"), "b3");
    assertEquals(row.get("C"), "c3");
    // Test the fourth row
    row = (Dictionary<String, Object>) result.get(3);
    assertEquals(row.get("A"), "a4");
    assertEquals(row.get("B"), "b4");
    assertEquals(row.get("C"), "c4");
}
Also used : StringReader(java.io.StringReader) CSVSerializerListener(org.apache.pivot.serialization.CSVSerializerListener) CSVSerializer(org.apache.pivot.serialization.CSVSerializer) Test(org.junit.Test)

Aggregations

StringReader (java.io.StringReader)1 CSVSerializer (org.apache.pivot.serialization.CSVSerializer)1 CSVSerializerListener (org.apache.pivot.serialization.CSVSerializerListener)1 Test (org.junit.Test)1