All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src.test.main.TestScopeMatching Maven / Gradle / Ivy

Go to download

Jackson is a high-performance JSON processor (parser, generator)

There is a newer version: 0.9.5
Show newest version
package main;

import org.codehaus.jackson.*;

/**
 * Set of basic unit tests for verifying that Array/Object scopes
 * are properly matched.
 */
public class TestScopeMatching
    extends BaseTest
{
    public void testUnclosedArray()
        throws Exception
    {
        JsonParser jp = createParserUsingReader("[ 1, 2");
        assertToken(JsonToken.START_ARRAY, jp.nextToken());
        assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
        assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());

        try {
            jp.nextToken();
            fail("Expected an exception for unclosed ARRAY");
        } catch (JsonParseException jpe) {
            verifyException(jpe, "expected close marker for ARRAY");
        }
    }

    public void testUnclosedObject()
        throws Exception
    {
        JsonParser jp = createParserUsingReader("{ \"key\" : 3  ");
        assertToken(JsonToken.START_OBJECT, jp.nextToken());
        assertToken(JsonToken.FIELD_NAME, jp.nextToken());
        assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());

        try {
            jp.nextToken();
            fail("Expected an exception for unclosed OBJECT");
        } catch (JsonParseException jpe) {
            verifyException(jpe, "expected close marker for OBJECT");
        }
    }

    public void testMismatchArrayToObject()
        throws Exception
    {
        JsonParser jp = createParserUsingReader("[ 1, 2 }");
        assertToken(JsonToken.START_ARRAY, jp.nextToken());
        assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
        assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());

        try {
            jp.nextToken();
            fail("Expected an exception for incorrectly closed ARRAY");
        } catch (JsonParseException jpe) {
            verifyException(jpe, "Unexpected close marker");
        }
    }

    public void testMismatchObjectToArray()
        throws Exception
    {
        JsonParser jp = createParserUsingReader("{ ]");
        assertToken(JsonToken.START_OBJECT, jp.nextToken());

        try {
            jp.nextToken();
            fail("Expected an exception for incorrectly closed OBJECT");
        } catch (JsonParseException jpe) {
            verifyException(jpe, "Unexpected close marker");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy