Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import com.google.common.base.Throwables;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
Expand All @@ -30,6 +31,7 @@
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.junit.function.ThrowingRunnable;

/**
* Functional tests related to circular reference detection and error reporting.
Expand All @@ -52,7 +54,7 @@ public void testCircularSerialization() {
a.children.add(b);
b.children.add(a);
// Circular types should not get printed
assertThrows(StackOverflowError.class, () -> gson.toJson(a));
assertThrowsStackOverflow(() -> gson.toJson(a));
}

@Test
Expand All @@ -70,7 +72,7 @@ public void testSelfReferenceArrayFieldSerialization() {
objA.children = new ClassWithSelfReferenceArray[] {objA};

// Circular reference to self can not be serialized
assertThrows(StackOverflowError.class, () -> gson.toJson(objA));
assertThrowsStackOverflow(() -> gson.toJson(objA));
}

@Test
Expand All @@ -96,7 +98,15 @@ public JsonElement serialize(
.create();

// Circular reference to self can not be serialized
assertThrows(StackOverflowError.class, () -> gson.toJson(obj));
assertThrowsStackOverflow(() -> gson.toJson(obj));
}

/** Asserts that a {@link StackOverflowError} is thrown. */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it doesn't assert that, does it? 🙂 I think we can probably assume that StackOverflowError is at least in the cause chain, and probably that it is the root cause. So then this?

Throwable t = assertThrows(Throwable.class, runnable);
assertThat(Throwables.getRootCause(t)).isInstanceOf(StackOverflowError.class);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it doesn't assert that, does it?

Yes you are right. My intention was to indicate that this methods is intended for cases where a StackOverflowError is expected, but then the implementation admits that it cannot reliably detect that.


I think we can probably assume that StackOverflowError is at least in the cause chain

Yes that could be an option. Let's just hope that there is no JDK code which sets it only as 'suppressed exception', or which entirely discards the cause.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. If that ever happens, we can adjust accordingly.

private static void assertThrowsStackOverflow(ThrowingRunnable runnable) {
// Obtain the root cause because the StackOverflowError might occur in JDK code, and that might
// wrap it in another exception class, for example InternalError
Throwable t = assertThrows(Throwable.class, runnable);
assertThat(Throwables.getRootCause(t)).isInstanceOf(StackOverflowError.class);
}

@Test
Expand Down