Search in sources :

Example 1 with ContainerNode

use of com.fasterxml.jackson.databind.node.ContainerNode in project hadoop by apache.

the class TestLog4Json method testNestedException.

@Test
public void testNestedException() throws Throwable {
    Exception e = new NoRouteToHostException("that box caught fire 3 years ago");
    Exception ioe = new IOException("Datacenter problems", e);
    ThrowableInformation ti = new ThrowableInformation(ioe);
    Log4Json l4j = new Log4Json();
    long timeStamp = Time.now();
    String outcome = l4j.toJson(new StringWriter(), "testNestedException", timeStamp, "INFO", "quoted\"", "new line\n and {}", ti).toString();
    println("testNestedException", outcome);
    ContainerNode rootNode = Log4Json.parse(outcome);
    assertEntryEquals(rootNode, Log4Json.LEVEL, "INFO");
    assertEntryEquals(rootNode, Log4Json.NAME, "testNestedException");
    assertEntryEquals(rootNode, Log4Json.TIME, timeStamp);
    assertEntryEquals(rootNode, Log4Json.EXCEPTION_CLASS, ioe.getClass().getName());
    JsonNode node = assertNodeContains(rootNode, Log4Json.STACK);
    assertTrue("Not an array: " + node, node.isArray());
    node = assertNodeContains(rootNode, Log4Json.DATE);
    assertTrue("Not a string: " + node, node.isTextual());
    //rather than try and make assertions about the format of the text
    //message equalling another ISO date, this test asserts that the hypen
    //and colon characters are in the string.
    String dateText = node.textValue();
    assertTrue("No '-' in " + dateText, dateText.contains("-"));
    assertTrue("No '-' in " + dateText, dateText.contains(":"));
}
Also used : StringWriter(java.io.StringWriter) ThrowableInformation(org.apache.log4j.spi.ThrowableInformation) ContainerNode(com.fasterxml.jackson.databind.node.ContainerNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) NoRouteToHostException(java.net.NoRouteToHostException) IOException(java.io.IOException) NoRouteToHostException(java.net.NoRouteToHostException) Test(org.junit.Test)

Example 2 with ContainerNode

use of com.fasterxml.jackson.databind.node.ContainerNode in project hadoop by apache.

the class Log4Json method parse.

/**
   * For use in tests
   *
   * @param json incoming JSON to parse
   * @return a node tree
   * @throws IOException on any parsing problems
   */
public static ContainerNode parse(String json) throws IOException {
    ObjectMapper mapper = new ObjectMapper(factory);
    JsonNode jsonNode = mapper.readTree(json);
    if (!(jsonNode instanceof ContainerNode)) {
        throw new IOException("Wrong JSON data: " + json);
    }
    return (ContainerNode) jsonNode;
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) ContainerNode(com.fasterxml.jackson.databind.node.ContainerNode) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ContainerNode (com.fasterxml.jackson.databind.node.ContainerNode)2 IOException (java.io.IOException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 StringWriter (java.io.StringWriter)1 NoRouteToHostException (java.net.NoRouteToHostException)1 ThrowableInformation (org.apache.log4j.spi.ThrowableInformation)1 Test (org.junit.Test)1