Search in sources :

Example 71 with AsyncReaderWrapper

use of com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper in project jackson-core by FasterXML.

the class AsyncInvalidCharsTest method _testUTF8BomOk.

private void _testUTF8BomOk(int offset, int readSize) throws Exception {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    // first, write BOM:
    bytes.write(0xEF);
    bytes.write(0xBB);
    bytes.write(0xBF);
    bytes.write("[ 1 ]".getBytes("UTF-8"));
    byte[] doc = bytes.toByteArray();
    AsyncReaderWrapper p = asyncForBytes(JSON_F, readSize, doc, offset);
    assertEquals(JsonToken.START_ARRAY, p.nextToken());
    // should also have skipped first 3 bytes of BOM; but do we have offset available?
    /* Alas, due to [core#111], we have to omit BOM in calculations
         * as we do not know what the offset is due to -- may need to revisit, if this
         * discrepancy becomes an issue. For now it just means that BOM is considered
         * "out of stream" (not part of input).
         */
    JsonLocation loc = p.parser().getTokenLocation();
    // so if BOM was consider in-stream (part of input), this should expect 3:
    // (NOTE: this is location for START_ARRAY token, now)
    assertEquals(-1, loc.getCharOffset());
    // !!! TODO: fix location handling
    /*
        assertEquals(0, loc.getByteOffset());
        assertEquals(1, loc.getLineNr());
        assertEquals(1, loc.getColumnNr());
*/
    assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken());
    assertEquals(JsonToken.END_ARRAY, p.nextToken());
    p.close();
}
Also used : AsyncReaderWrapper(com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 72 with AsyncReaderWrapper

use of com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper in project jackson-core by FasterXML.

the class AsyncStringObjectTest method _testBasicFieldsNames2.

private void _testBasicFieldsNames2(JsonFactory f, byte[] data, int offset, int readSize, boolean verifyContents) throws IOException {
    AsyncReaderWrapper r = asyncForBytes(f, readSize, data, offset);
    // start with "no token"
    assertNull(r.currentToken());
    assertToken(JsonToken.START_OBJECT, r.nextToken());
    assertToken(JsonToken.FIELD_NAME, r.nextToken());
    if (verifyContents) {
        assertEquals(UNICODE_SHORT_NAME, r.currentName());
        assertEquals(UNICODE_SHORT_NAME, r.currentText());
    }
    assertToken(JsonToken.VALUE_STRING, r.nextToken());
    // also, should always be accessible this way:
    if (verifyContents) {
        assertTrue(r.parser().hasTextCharacters());
        assertEquals(UNICODE_LONG_NAME, r.currentText());
    }
    assertToken(JsonToken.FIELD_NAME, r.nextToken());
    if (verifyContents) {
        assertEquals(UNICODE_LONG_NAME, r.currentName());
        assertEquals(UNICODE_LONG_NAME, r.currentText());
    }
    assertToken(JsonToken.VALUE_STRING, r.nextToken());
    if (verifyContents) {
        assertEquals(UNICODE_SHORT_NAME, r.currentText());
    }
    // and ASCII entry
    assertToken(JsonToken.FIELD_NAME, r.nextToken());
    if (verifyContents) {
        assertEquals(ASCII_SHORT_NAME, r.currentName());
        assertEquals(ASCII_SHORT_NAME, r.currentText());
    }
    assertToken(JsonToken.VALUE_STRING, r.nextToken());
    if (verifyContents) {
        assertEquals(ASCII_SHORT_NAME, r.currentText());
    }
    assertToken(JsonToken.END_OBJECT, r.nextToken());
    assertNull(r.nextToken());
    // Second round, try with alternate read method
    if (verifyContents) {
        r = asyncForBytes(f, readSize, data, offset);
        assertToken(JsonToken.START_OBJECT, r.nextToken());
        assertToken(JsonToken.FIELD_NAME, r.nextToken());
        assertEquals(UNICODE_SHORT_NAME, r.currentTextViaWriter());
        assertToken(JsonToken.VALUE_STRING, r.nextToken());
        assertEquals(UNICODE_LONG_NAME, r.currentTextViaWriter());
        assertToken(JsonToken.FIELD_NAME, r.nextToken());
        assertEquals(UNICODE_LONG_NAME, r.currentTextViaWriter());
        assertToken(JsonToken.VALUE_STRING, r.nextToken());
        assertEquals(UNICODE_SHORT_NAME, r.currentTextViaWriter());
        assertToken(JsonToken.FIELD_NAME, r.nextToken());
        assertEquals(ASCII_SHORT_NAME, r.currentTextViaWriter());
        assertToken(JsonToken.VALUE_STRING, r.nextToken());
        assertEquals(ASCII_SHORT_NAME, r.currentTextViaWriter());
        assertToken(JsonToken.END_OBJECT, r.nextToken());
    }
}
Also used : AsyncReaderWrapper(com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper)

Example 73 with AsyncReaderWrapper

use of com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper in project jackson-core by FasterXML.

the class ConfigTest method testAsyncParerDefaults.

public void testAsyncParerDefaults() throws IOException {
    byte[] data = _jsonDoc("[true,false]");
    AsyncReaderWrapper r = asyncForBytes(DEFAULT_F, 100, data, 0);
    JsonParser p = r.parser();
    assertTrue(p.canParseAsync());
    assertNull(p.getInputSource());
    assertEquals(-1, p.releaseBuffered(new StringWriter()));
    assertEquals(0, p.releaseBuffered(new ByteArrayOutputStream()));
    assertToken(JsonToken.START_ARRAY, r.nextToken());
    assertEquals(11, p.releaseBuffered(new ByteArrayOutputStream()));
    p.close();
}
Also used : AsyncReaderWrapper(com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper)

Example 74 with AsyncReaderWrapper

use of com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper in project jackson-core by FasterXML.

the class AsyncTokenErrorTest method testMangledNonRootFloats.

public void testMangledNonRootFloats() throws Exception {
    AsyncReaderWrapper p = _createParser("[ 1.5false ]");
    assertToken(JsonToken.START_ARRAY, p.nextToken());
    try {
        JsonToken t = p.nextToken();
        fail("Should have gotten an exception; instead got token: " + t);
    } catch (JsonParseException e) {
        verifyException(e, "expected space");
    }
    p.close();
}
Also used : AsyncReaderWrapper(com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper)

Example 75 with AsyncReaderWrapper

use of com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper in project jackson-core by FasterXML.

the class AsyncTokenErrorTest method testMangledRootInts.

public void testMangledRootInts() throws Exception {
    AsyncReaderWrapper p = _createParser("123true");
    try {
        JsonToken t = p.nextToken();
        fail("Should have gotten an exception; instead got token: " + t + "; number: " + p.getNumberValue());
    } catch (JsonParseException e) {
        verifyException(e, "expected space");
    }
    p.close();
}
Also used : AsyncReaderWrapper(com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper)

Aggregations

AsyncReaderWrapper (com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper)87 Test (org.junit.Test)10 JsonFactory (com.fasterxml.jackson.core.json.JsonFactory)5 BigDecimal (java.math.BigDecimal)4 IOException (java.io.IOException)3 BigInteger (java.math.BigInteger)3 JsonParseException (com.fasterxml.jackson.core.JsonParseException)2 NonBlockingJsonParserBase (com.fasterxml.jackson.core.json.async.NonBlockingJsonParserBase)2 ByteQuadsCanonicalizer (com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2