Search in sources :

Example 1 with GsonObjectParser

use of com.koushikdutta.ion.gson.GsonObjectParser in project ion by koush.

the class GsonTests method testParserCastingCallbackError.

public void testParserCastingCallbackError() throws Exception {
    ByteBufferList b = new ByteBufferList(ByteBuffer.wrap("[]".getBytes()));
    FilteredDataEmitter emitter = new FilteredDataEmitter() {

        @Override
        public boolean isPaused() {
            return false;
        }
    };
    GsonObjectParser g = new GsonObjectParser();
    Future<JsonObject> ret = g.parse(emitter);
    emitter.onDataAvailable(emitter, b);
    emitter.getEndCallback().onCompleted(null);
    final Semaphore s = new Semaphore(0);
    ret.setCallback(new FutureCallback<JsonObject>() {

        @Override
        public void onCompleted(Exception e, JsonObject result) {
            assertNull(result);
            assertNotNull(e);
            assertTrue(e instanceof ClassCastException);
            s.release();
        }
    });
    s.acquire();
}
Also used : FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) GsonObjectParser(com.koushikdutta.ion.gson.GsonObjectParser) ByteBufferList(com.koushikdutta.async.ByteBufferList) JsonObject(com.google.gson.JsonObject) Semaphore(java.util.concurrent.Semaphore) JsonParseException(com.google.gson.JsonParseException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with GsonObjectParser

use of com.koushikdutta.ion.gson.GsonObjectParser in project ion by koush.

the class GsonTests method testParserCastingError.

public void testParserCastingError() throws Exception {
    ByteBufferList b = new ByteBufferList(ByteBuffer.wrap("[]".getBytes()));
    FilteredDataEmitter emitter = new FilteredDataEmitter() {

        @Override
        public boolean isPaused() {
            return false;
        }
    };
    GsonObjectParser g = new GsonObjectParser();
    Future<JsonObject> ret = g.parse(emitter);
    emitter.onDataAvailable(emitter, b);
    emitter.getEndCallback().onCompleted(null);
    try {
        JsonObject j = ret.get();
        fail(j.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) GsonObjectParser(com.koushikdutta.ion.gson.GsonObjectParser) ByteBufferList(com.koushikdutta.async.ByteBufferList) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with GsonObjectParser

use of com.koushikdutta.ion.gson.GsonObjectParser in project ion by koush.

the class GsonTests method testParserCastingSuccess.

public void testParserCastingSuccess() throws Exception {
    ByteBufferList b = new ByteBufferList(ByteBuffer.wrap("{}".getBytes()));
    FilteredDataEmitter emitter = new FilteredDataEmitter() {

        @Override
        public boolean isPaused() {
            return false;
        }
    };
    GsonObjectParser g = new GsonObjectParser();
    Future<JsonObject> ret = g.parse(emitter);
    emitter.onDataAvailable(emitter, b);
    emitter.getEndCallback().onCompleted(null);
    JsonObject j = ret.get();
    assertNotNull(j);
}
Also used : FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) GsonObjectParser(com.koushikdutta.ion.gson.GsonObjectParser) ByteBufferList(com.koushikdutta.async.ByteBufferList) JsonObject(com.google.gson.JsonObject)

Aggregations

JsonObject (com.google.gson.JsonObject)3 ByteBufferList (com.koushikdutta.async.ByteBufferList)3 FilteredDataEmitter (com.koushikdutta.async.FilteredDataEmitter)3 GsonObjectParser (com.koushikdutta.ion.gson.GsonObjectParser)3 JsonParseException (com.google.gson.JsonParseException)2 ExecutionException (java.util.concurrent.ExecutionException)2 Semaphore (java.util.concurrent.Semaphore)1