|
Java example source code file (ThrowableFunctionalTest.java)
The ThrowableFunctionalTest.java Java example source code// Copyright (C) 2014 Trymph Inc. package com.google.gson.functional; import java.io.IOException; import junit.framework.TestCase; import com.google.gson.Gson; import com.google.gson.annotations.SerializedName; @SuppressWarnings("serial") public final class ThrowableFunctionalTest extends TestCase { private final Gson gson = new Gson(); public void testExceptionWithoutCause() { RuntimeException e = new RuntimeException("hello"); String json = gson.toJson(e); assertTrue(json.contains("hello")); e = gson.fromJson("{'detailMessage':'hello'}", RuntimeException.class); assertEquals("hello", e.getMessage()); } public void testExceptionWithCause() { Exception e = new Exception("top level", new IOException("io error")); String json = gson.toJson(e); assertTrue(json.contains("{\"detailMessage\":\"top level\",\"cause\":{\"detailMessage\":\"io error\"")); e = gson.fromJson("{'detailMessage':'top level','cause':{'detailMessage':'io error'}}", Exception.class); assertEquals("top level", e.getMessage()); assertTrue(e.getCause() instanceof Throwable); // cause is not parameterized so type info is lost assertEquals("io error", e.getCause().getMessage()); } public void testSerializedNameOnExceptionFields() { MyException e = new MyException(); String json = gson.toJson(e); assertTrue(json.contains("{\"my_custom_name\":\"myCustomMessageValue\"")); } public void testErrorWithoutCause() { OutOfMemoryError e = new OutOfMemoryError("hello"); String json = gson.toJson(e); assertTrue(json.contains("hello")); e = gson.fromJson("{'detailMessage':'hello'}", OutOfMemoryError.class); assertEquals("hello", e.getMessage()); } public void testErrornWithCause() { Error e = new Error("top level", new IOException("io error")); String json = gson.toJson(e); assertTrue(json.contains("top level")); assertTrue(json.contains("io error")); e = gson.fromJson("{'detailMessage':'top level','cause':{'detailMessage':'io error'}}", Error.class); assertEquals("top level", e.getMessage()); assertTrue(e.getCause() instanceof Throwable); // cause is not parameterized so type info is lost assertEquals("io error", e.getCause().getMessage()); } private static final class MyException extends Throwable { @SerializedName("my_custom_name") String myCustomMessage = "myCustomMessageValue"; } } Other Java examples (source code examples)Here is a short list of links related to this Java ThrowableFunctionalTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.