Search in sources :

Example 1 with GenericTypeReference

use of com.ctrip.xpipe.api.codec.GenericTypeReference in project x-pipe by ctripcorp.

the class ErrorMessageTest method testSerializa.

@Test
public void testSerializa() throws JsonParseException, JsonMappingException, IOException {
    ErrorMessage<ERRORCODE> error = new ErrorMessage<ERRORCODE>(ERRORCODE.NET_EXCEPTION, "conntect refused");
    String result = Codec.DEFAULT.encode(error);
    logger.info("{}", result);
    ObjectMapper om = new ObjectMapper();
    ErrorMessage<ERRORCODE> desr = om.readValue(result, new TypeReference<ErrorMessage<ERRORCODE>>() {
    });
    Assert.assertEquals(error, desr);
    desr = Codec.DEFAULT.decode(result, new GenericTypeReference<ErrorMessage<ERRORCODE>>() {
    });
    Assert.assertEquals(error, desr);
    // test wrong message
    try {
        String wrong = "{\"errorType\":\"NET_EXCEPTION1\",\"errorMessage\":\"conntect refused\"}";
        desr = om.readValue(wrong, new TypeReference<ErrorMessage<ERRORCODE>>() {
        });
        Assert.fail();
    } catch (Exception e) {
    }
}
Also used : GenericTypeReference(com.ctrip.xpipe.api.codec.GenericTypeReference) GenericTypeReference(com.ctrip.xpipe.api.codec.GenericTypeReference) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Aggregations

AbstractTest (com.ctrip.xpipe.AbstractTest)1 GenericTypeReference (com.ctrip.xpipe.api.codec.GenericTypeReference)1 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 Test (org.junit.Test)1