-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35362a0
commit c674b76
Showing
3 changed files
with
110 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 56 additions & 19 deletions
75
src/test/java/com/fasterxml/jackson/databind/interop/ExceptionSerializableTest1195.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,82 @@ | ||
package com.fasterxml.jackson.databind.interop; | ||
|
||
import java.io.*; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.databind.*; | ||
|
||
public class ExceptionSerializableTest1195 extends BaseMapTest | ||
{ | ||
abstract static class ClassToRead { | ||
static class ClassToRead { | ||
public int x; | ||
} | ||
|
||
static class ContainerClassToRead { | ||
public ClassToRead classToRead; | ||
} | ||
|
||
public void testExceptionSerializability() throws Exception | ||
static class ContainerClassesToRead { | ||
public List<ClassToRead> classesToRead; | ||
} | ||
|
||
final ObjectMapper MAPPER = new ObjectMapper(); | ||
|
||
public void testExceptionSerializabilitySimple() throws Exception | ||
{ | ||
final ObjectMapper mapper = new ObjectMapper(); | ||
try { | ||
mapper.readValue("{\"type\": \"B\"}", ClassToRead.class); | ||
MAPPER.readValue("{\"x\": \"B\"}", ClassToRead.class); | ||
fail("Should not have passed"); | ||
} catch (JsonMappingException e) { | ||
ObjectOutputStream stream = new ObjectOutputStream(new ByteArrayOutputStream()); | ||
try { | ||
stream.writeObject(e); | ||
stream.close(); | ||
} catch (Exception e2) { | ||
fail("Failed to serialize "+e.getClass().getName()+": "+e2); | ||
} | ||
verifyException(e, "not a valid Integer"); | ||
_testSerializability(e); | ||
} | ||
try { | ||
mapper.readValue("{\"classToRead\": {\"type\": \"B\"}}", ContainerClassToRead.class); | ||
MAPPER.readValue("{\"classToRead\": {\"x\": \"B\"}}", ContainerClassToRead.class); | ||
fail("Should not have passed"); | ||
} catch (JsonMappingException e) { | ||
verifyException(e, "not a valid Integer"); | ||
_testSerializability(e); | ||
} | ||
} | ||
|
||
public void testExceptionSerializabilityStructured() throws Exception | ||
{ | ||
try { | ||
MAPPER.readValue("{\"classesToRead\": [{\"x\": 1}, {\"x\": \"B\"}]}", | ||
ContainerClassesToRead.class); | ||
fail("Should not have passed"); | ||
} catch (JsonMappingException e) { | ||
ObjectOutputStream stream = new ObjectOutputStream(new ByteArrayOutputStream()); | ||
try { | ||
stream.writeObject(e); | ||
stream.close(); | ||
} catch (Exception e2) { | ||
fail("Failed to serialize "+e.getClass().getName()+": "+e2); | ||
} | ||
verifyException(e, "not a valid Integer"); | ||
_testSerializability(e); | ||
} | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* Helper methods | ||
/********************************************************** | ||
*/ | ||
|
||
private void _testSerializability(Exception e) throws IOException | ||
{ | ||
ByteArrayOutputStream bytes = new ByteArrayOutputStream(1000); | ||
ObjectOutputStream stream = new ObjectOutputStream(bytes); | ||
try { | ||
stream.writeObject(e); | ||
stream.close(); | ||
} catch (Exception e2) { | ||
fail("Failed to JDK serialize "+e.getClass().getName()+": "+e2); | ||
} | ||
// and then back... | ||
byte[] b = bytes.toByteArray(); | ||
ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(b)); | ||
Object result = null; | ||
try { | ||
result = objIn.readObject(); | ||
} catch (Exception e2) { | ||
fail("Failed to JDK deserialize "+e.getClass().getName()+": "+e2); | ||
} | ||
objIn.close(); | ||
assertNotNull(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters