Search in sources :

Example 11 with CSVSerializer

use of org.apache.pivot.serialization.CSVSerializer 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

CSVSerializer (org.apache.pivot.serialization.CSVSerializer)11 Test (org.junit.Test)9 StringReader (java.io.StringReader)5 ArrayList (org.apache.pivot.collections.ArrayList)5 StringWriter (java.io.StringWriter)4 Dictionary (org.apache.pivot.collections.Dictionary)4 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 DateFormat (java.text.DateFormat)1 Date (java.util.Date)1 ServletException (javax.servlet.ServletException)1 List (org.apache.pivot.collections.List)1 CSVSerializerListener (org.apache.pivot.serialization.CSVSerializerListener)1 SerializationException (org.apache.pivot.serialization.SerializationException)1 Task (org.apache.pivot.util.concurrent.Task)1 TaskListener (org.apache.pivot.util.concurrent.TaskListener)1 GetQuery (org.apache.pivot.web.GetQuery)1