Skip to content

Commit

Permalink
Split out the java.awt.Point check into its own test method. (#2757)
Browse files Browse the repository at this point in the history
  • Loading branch information
eamonnmcmanus authored Oct 10, 2024
1 parent 2aff513 commit ec5a5e4
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.junit.AssumptionViolatedException;
import org.junit.Test;

public class ReflectionAccessFilterTest {
Expand All @@ -57,7 +58,7 @@ public void close() throws IOException {}
}

@Test
public void testBlockInaccessibleJava() throws ReflectiveOperationException {
public void testBlockInaccessibleJava() {
Gson gson =
new GsonBuilder()
.addReflectionAccessFilter(ReflectionAccessFilter.BLOCK_INACCESSIBLE_JAVA)
Expand All @@ -77,14 +78,23 @@ public void testBlockInaccessibleJava() throws ReflectiveOperationException {
+ " permit making it accessible. Register a TypeAdapter for the declaring type,"
+ " adjust the access filter or increase the visibility of the element and its"
+ " declaring type.");
}

@Test
public void testDontBlockAccessibleJava() throws ReflectiveOperationException {
Gson gson =
new GsonBuilder()
.addReflectionAccessFilter(ReflectionAccessFilter.BLOCK_INACCESSIBLE_JAVA)
.create();

// But serialization should succeed for classes with only public fields.
// Serialization should succeed for classes with only public fields.
// Not many JDK classes have mutable public fields, thank goodness, but java.awt.Point does.
Class<?> pointClass;
try {
pointClass = Class.forName("java.awt.Point");
} catch (ClassNotFoundException ignored) {
return; // If not found then we don't have AWT and the rest of the test can be skipped.
} catch (ClassNotFoundException e) {
// If not found then we don't have AWT and the rest of the test can be skipped.
throw new AssumptionViolatedException("java.awt.Point not present", e);
}
Constructor<?> pointConstructor = pointClass.getConstructor(int.class, int.class);
Object point = pointConstructor.newInstance(1, 2);
Expand Down

0 comments on commit ec5a5e4

Please sign in to comment.