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) {
}
}
Aggregations