Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
src.test.main.TestJsonGenerator Maven / Gradle / Ivy
package main;
import org.codehaus.jackson.*;
import java.io.*;
/**
* Set of basic unit tests for verifying that the basic generator
* functionality works as expected.
*/
public class TestJsonGenerator
extends BaseTest
{
// // // First, tests for primitive (non-structured) values
public void testStringWrite()
throws Exception
{
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
String VALUE = "";
gen.writeString(VALUE);
gen.close();
String docStr = sw.toString();
JsonParser jp = createParserUsingReader(docStr);
JsonToken t = jp.nextToken();
assertNotNull("Document \""+docStr+"\" yielded no tokens", t);
assertEquals(JsonToken.VALUE_STRING, t);
assertEquals(VALUE, jp.getText());
assertEquals(null, jp.nextToken());
jp.close();
}
public void testIntWrite()
throws Exception
{
doTestIntWrite(false);
doTestIntWrite(true);
}
public void testLongWrite()
throws Exception
{
doTestLongWrite(false);
doTestLongWrite(true);
}
public void testBooleanWrite()
throws Exception
{
for (int i = 0; i < 4; ++i) {
boolean state = (i & 1) == 0;
boolean pad = (i & 2) == 0;
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
gen.writeBoolean(state);
if (pad) {
gen.writeRaw(" ");
}
gen.close();
String docStr = sw.toString();
JsonParser jp = createParserUsingReader(docStr);
JsonToken t = jp.nextToken();
String exp = Boolean.valueOf(state).toString();
if (!exp.equals(jp.getText())) {
fail("Expected '"+exp+"', got '"+jp.getText());
}
assertEquals(state ? JsonToken.VALUE_TRUE : JsonToken.VALUE_FALSE, t);
assertEquals(null, jp.nextToken());
jp.close();
}
}
public void testNullWrite()
throws Exception
{
for (int i = 0; i < 2; ++i) {
boolean pad = (i & 1) == 0;
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
gen.writeNull();
if (pad) {
gen.writeRaw(" ");
}
gen.close();
String docStr = sw.toString();
JsonParser jp = createParserUsingReader(docStr);
JsonToken t = jp.nextToken();
String exp = "null";
if (!exp.equals(jp.getText())) {
fail("Expected '"+exp+"', got '"+jp.getText());
}
assertEquals(JsonToken.VALUE_NULL, t);
assertEquals(null, jp.nextToken());
jp.close();
}
}
// // // Then tests for structured values
public void testEmptyArrayWrite()
throws Exception
{
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
gen.writeStartArray();
gen.writeEndArray();
gen.close();
String docStr = sw.toString();
JsonParser jp = createParserUsingReader(docStr);
assertEquals(JsonToken.START_ARRAY, jp.nextToken());
assertEquals(JsonToken.END_ARRAY, jp.nextToken());
jp.close();
// Ok, then array with nested empty array
sw = new StringWriter();
gen = new JsonFactory().createJsonGenerator(sw);
gen.writeStartArray();
gen.writeStartArray();
gen.writeEndArray();
gen.writeEndArray();
gen.close();
docStr = sw.toString();
jp = createParserUsingReader(docStr);
assertEquals(JsonToken.START_ARRAY, jp.nextToken());
assertEquals(JsonToken.START_ARRAY, jp.nextToken());
assertEquals(JsonToken.END_ARRAY, jp.nextToken());
assertEquals(JsonToken.END_ARRAY, jp.nextToken());
assertEquals(null, jp.nextToken());
jp.close();
}
public void testInvalidArrayWrite()
throws Exception
{
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
gen.writeStartArray();
// Mismatch:
try {
gen.writeEndObject();
fail("Expected an exception for mismatched array/object write");
} catch (JsonGenerationException e) {
verifyException(e, "Current context not an object");
}
}
public void testSimpleArrayWrite()
throws Exception
{
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
gen.writeStartArray();
gen.writeNumber(13);
gen.writeBoolean(true);
gen.writeString("foobar");
gen.writeEndArray();
gen.close();
String docStr = sw.toString();
JsonParser jp = createParserUsingReader(docStr);
assertEquals(JsonToken.START_ARRAY, jp.nextToken());
assertEquals(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
assertEquals(13, jp.getIntValue());
assertEquals(JsonToken.VALUE_TRUE, jp.nextToken());
assertEquals(JsonToken.VALUE_STRING, jp.nextToken());
assertEquals("foobar", jp.getText());
assertEquals(JsonToken.END_ARRAY, jp.nextToken());
assertEquals(null, jp.nextToken());
jp.close();
}
public void testEmptyObjectWrite()
throws Exception
{
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
gen.writeStartObject();
gen.writeEndObject();
gen.close();
String docStr = sw.toString();
JsonParser jp = createParserUsingReader(docStr);
assertEquals(JsonToken.START_OBJECT, jp.nextToken());
assertEquals(JsonToken.END_OBJECT, jp.nextToken());
assertEquals(null, jp.nextToken());
}
public void testInvalidObjectWrite()
throws Exception
{
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
gen.writeStartObject();
// Mismatch:
try {
gen.writeEndArray();
fail("Expected an exception for mismatched array/object write");
} catch (JsonGenerationException e) {
verifyException(e, "Current context not an array");
}
}
public void testSimpleObjectWrite()
throws Exception
{
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
gen.writeStartObject();
gen.writeFieldName("first");
gen.writeNumber(-901);
gen.writeFieldName("sec");
gen.writeBoolean(false);
gen.writeFieldName("3rd!"); // json field names are just strings, not ids with restrictions
gen.writeString("yee-haw");
gen.writeEndObject();
gen.close();
String docStr = sw.toString();
JsonParser jp = createParserUsingReader(docStr);
assertEquals(JsonToken.START_OBJECT, jp.nextToken());
assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("first", jp.getText());
assertEquals(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
assertEquals(-901, jp.getIntValue());
assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("sec", jp.getText());
assertEquals(JsonToken.VALUE_FALSE, jp.nextToken());
assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("3rd!", jp.getText());
assertEquals(JsonToken.VALUE_STRING, jp.nextToken());
assertEquals("yee-haw", jp.getText());
assertEquals(JsonToken.END_OBJECT, jp.nextToken());
assertEquals(null, jp.nextToken());
jp.close();
}
// // Then root-level output testing
public void testRootIntsWrite()
throws Exception
{
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
gen.writeNumber(1);
gen.writeNumber(2);
gen.writeNumber(-13);
gen.close();
String docStr = sw.toString();
JsonParser jp = createParserUsingReader(docStr);
assertEquals(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
assertEquals(1, jp.getIntValue());
assertEquals(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
assertEquals(2, jp.getIntValue());
assertEquals(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
assertEquals(-13, jp.getIntValue());
jp.close();
}
/*
//////////////////////////////////////////////////
// Internal methods
//////////////////////////////////////////////////
*/
private void doTestIntWrite(boolean pad)
throws Exception
{
int[] VALUES = new int[] {
0, 1, -9, 32, -32, 57, 13240, -9999, Integer.MAX_VALUE, Integer.MAX_VALUE
};
for (int i = 0; i < VALUES.length; ++i) {
int VALUE = VALUES[i];
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
gen.writeNumber(VALUE);
if (pad) {
gen.writeRaw(" ");
}
gen.close();
String docStr = sw.toString();
JsonParser jp = createParserUsingReader(docStr);
JsonToken t = jp.nextToken();
assertNotNull("Document \""+docStr+"\" yielded no tokens", t);
// Number are always available as lexical representation too
String exp = ""+VALUE;
if (!exp.equals(jp.getText())) {
fail("Expected '"+exp+"', got '"+jp.getText());
}
assertEquals(JsonToken.VALUE_NUMBER_INT, t);
assertEquals(VALUE, jp.getIntValue());
assertEquals(null, jp.nextToken());
jp.close();
}
}
private void doTestLongWrite(boolean pad)
throws Exception
{
long[] VALUES = new long[] {
0L, 1L, -1L, -12005002294L, Long.MIN_VALUE, Long.MAX_VALUE
};
for (int i = 0; i < VALUES.length; ++i) {
long VALUE = VALUES[i];
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
gen.writeNumber(VALUE);
if (pad) {
gen.writeRaw(" ");
}
gen.close();
String docStr = sw.toString();
JsonParser jp = createParserUsingReader(docStr);
JsonToken t = jp.nextToken();
assertNotNull("Document \""+docStr+"\" yielded no tokens", t);
String exp = ""+VALUE;
if (!exp.equals(jp.getText())) {
fail("Expected '"+exp+"', got '"+jp.getText());
}
assertEquals(JsonToken.VALUE_NUMBER_INT, t);
assertEquals(VALUE, jp.getLongValue());
assertEquals(null, jp.nextToken());
jp.close();
}
}
}