add a simple test for validating jackson handling of binary data added while generating content

This commit is contained in:
kimchy 2010-02-22 23:18:45 +02:00
parent 3f033a04e7
commit 8adcbb2832

View File

@ -19,6 +19,11 @@
package org.elasticsearch.util.json;
import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonNode;
import org.elasticsearch.util.io.FastByteArrayInputStream;
import org.elasticsearch.util.io.FastByteArrayOutputStream;
import org.elasticsearch.util.io.FastCharArrayWriter;
import org.testng.annotations.Test;
@ -56,4 +61,24 @@ public class JsonBuilderTests {
builder.reset();
assertThat(builder.startObject().field("test", "value").endObject().string(), equalTo("{\"test\":\"value\"}"));
}
@Test public void testWritingBinaryToStream() throws Exception {
FastByteArrayOutputStream bos = new FastByteArrayOutputStream();
JsonGenerator gen = Jackson.defaultJsonFactory().createJsonGenerator(bos, JsonEncoding.UTF8);
gen.writeStartObject();
gen.writeStringField("name", "something");
gen.flush();
bos.write(", source : { test : \"value\" }".getBytes("UTF8"));
gen.writeStringField("name2", "something2");
gen.writeEndObject();
gen.close();
byte[] data = bos.copiedByteArray();
String sData = new String(data, "UTF8");
System.out.println("DATA: " + sData);
JsonNode node = Jackson.newObjectMapper().readValue(new FastByteArrayInputStream(data), JsonNode.class);
assertThat(node.get("source").get("test").getTextValue(), equalTo("value"));
}
}