Search in sources :

Example 1 with TextDataCodec

use of com.linkedin.data.codec.TextDataCodec in project rest.li by linkedin.

the class TestData method testDataCodec.

public void testDataCodec(DataCodec codec, DataMap map) throws IOException {
    boolean debug = false;
    StringBuilder sb1 = new StringBuilder();
    Data.dump("map", map, "", sb1);
    if (debug)
        out.print(sb1);
    // test mapToBytes
    byte[] bytes = codec.mapToBytes(map);
    if (debug)
        TestUtil.dumpBytes(out, bytes);
    // test bytesToMap
    DataMap map2 = codec.bytesToMap(bytes);
    StringBuilder sb2 = new StringBuilder();
    Data.dump("map", map2, "", sb2);
    if (debug)
        out.print(sb2);
    assertEquivalent(map2, map);
    // test writeMap
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(bytes.length * 2);
    codec.writeMap(map, outputStream);
    byte[] outputStreamBytes = outputStream.toByteArray();
    assertEquals(outputStreamBytes, bytes);
    // test readMap
    ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStreamBytes);
    DataMap map3 = codec.readMap(inputStream);
    StringBuilder sb3 = new StringBuilder();
    Data.dump("map", map3, "", sb3);
    if (debug)
        out.print(sb3);
    assertEquivalent(map3, map);
    assertEquivalent(map3, map2);
    if (codec instanceof TextDataCodec) {
        TextDataCodec textCodec = (TextDataCodec) codec;
        // test mapToString
        String string = textCodec.mapToString(map);
        if (debug)
            out.println(string);
        // test stringToMap
        DataMap map4 = textCodec.stringToMap(string);
        StringBuilder sb4 = new StringBuilder();
        Data.dump("map", map4, "", sb4);
        assertEquals(sb4.toString(), sb1.toString());
        // test writeMap
        StringWriter writer = new StringWriter();
        textCodec.writeMap(map, writer);
        assertEquals(writer.toString(), string);
        // test readMap
        StringReader reader = new StringReader(string);
        DataMap map5 = textCodec.readMap(reader);
        StringBuilder sb5 = new StringBuilder();
        Data.dump("map", map5, "", sb5);
    }
}
Also used : TextDataCodec(com.linkedin.data.codec.TextDataCodec) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TestUtil.dataMapFromString(com.linkedin.data.TestUtil.dataMapFromString)

Example 2 with TextDataCodec

use of com.linkedin.data.codec.TextDataCodec in project rest.li by linkedin.

the class TestData method testDataCodec.

public void testDataCodec(DataCodec codec, DataList list) throws IOException {
    boolean debug = false;
    StringBuilder sb1 = new StringBuilder();
    Data.dump("list", list, "", sb1);
    if (debug)
        out.print(sb1);
    // test listToBytes
    byte[] bytes = codec.listToBytes(list);
    if (debug)
        TestUtil.dumpBytes(out, bytes);
    // test bytesToList
    DataList list2 = codec.bytesToList(bytes);
    StringBuilder sb2 = new StringBuilder();
    Data.dump("list", list2, "", sb2);
    assertEquals(sb2.toString(), sb1.toString());
    // test writeList
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(bytes.length * 2);
    codec.writeList(list, outputStream);
    byte[] outputStreamBytes = outputStream.toByteArray();
    assertEquals(outputStreamBytes, bytes);
    // test readList
    ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStreamBytes);
    DataList list3 = codec.readList(inputStream);
    StringBuilder sb3 = new StringBuilder();
    Data.dump("list", list3, "", sb3);
    assertEquals(sb3.toString(), sb1.toString());
    if (codec instanceof TextDataCodec) {
        TextDataCodec textCodec = (TextDataCodec) codec;
        // test listToString
        String string = textCodec.listToString(list);
        if (debug)
            out.println(string);
        // test stringToList
        DataList list4 = textCodec.stringToList(string);
        StringBuilder sb4 = new StringBuilder();
        Data.dump("list", list4, "", sb4);
        assertEquals(sb4.toString(), sb1.toString());
        // test writeList
        StringWriter writer = new StringWriter();
        textCodec.writeList(list, writer);
        assertEquals(writer.toString(), string);
        // test readList
        StringReader reader = new StringReader(string);
        DataList list5 = textCodec.readList(reader);
        StringBuilder sb5 = new StringBuilder();
        Data.dump("list", list5, "", sb5);
    }
}
Also used : TextDataCodec(com.linkedin.data.codec.TextDataCodec) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TestUtil.dataMapFromString(com.linkedin.data.TestUtil.dataMapFromString)

Aggregations

TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)2 TextDataCodec (com.linkedin.data.codec.TextDataCodec)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 StringReader (java.io.StringReader)2 StringWriter (java.io.StringWriter)2