Skip to content

Commit

Permalink
Add a new assertion Verify.assertNotSerializable().
Browse files Browse the repository at this point in the history
  • Loading branch information
motlin committed Mar 10, 2016
1 parent f76d15d commit e737cd3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015 Goldman Sachs.
* Copyright (c) 2016 Goldman Sachs.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand All @@ -13,6 +13,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
Expand Down Expand Up @@ -190,8 +191,7 @@ public static void throwMangledException(AssertionError e, int framesToPop)

public static void fail(String message, Throwable cause)
{
AssertionError failedException = new AssertionError(message);
failedException.initCause(cause);
AssertionError failedException = new AssertionError(message, cause);
Verify.throwMangledException(failedException);
}

Expand Down Expand Up @@ -3747,6 +3747,25 @@ private static String addFinalNewline(String string)
return string;
}

public static void assertNotSerializable(final Object actualObject)
{
try
{
Verify.assertThrows(NotSerializableException.class, new Callable<Void>()
{
public Void call() throws IOException
{
new ObjectOutputStream(new ByteArrayOutputStream()).writeObject(actualObject);
return null;
}
});
}
catch (AssertionError e)
{
Verify.throwMangledException(e);
}
}

/**
* Assert that {@code objectA} and {@code objectB} are equal (via the {@link Object#equals(Object)} method,
* and that they both return the same {@link Object#hashCode()}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015 Goldman Sachs.
* Copyright (c) 2016 Goldman Sachs.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -1910,4 +1910,20 @@ public void assertClassNonInstantiable()
Verify.assertContains(VerifyTest.class.getName(), ex.getStackTrace()[0].toString());
}
}

@Test
public void assertNotSerializable()
{
Verify.assertNotSerializable(new Object());

try
{
Verify.assertNotSerializable("serializable");
}
catch (AssertionError ex)
{
Assert.assertEquals("Block did not throw an exception of type java.io.NotSerializableException", ex.getMessage());
Verify.assertContains(VerifyTest.class.getName(), ex.getStackTrace()[0].toString());
}
}
}

0 comments on commit e737cd3

Please sign in to comment.