()
| 31 | public class TestJsonFormatter { |
| 32 | |
| 33 | @Test |
| 34 | public void testFormat() throws Exception { |
| 35 | |
| 36 | Formatter formatter = new JsonFormatter(); |
| 37 | LogRecord logRecord = new LogRecord(Level.FINE, "Test log message"); |
| 38 | logRecord.setSourceClassName("org.apache.juli.TestJsonFormatter"); |
| 39 | logRecord.setSourceMethodName("testFormat"); |
| 40 | try { |
| 41 | throw new IllegalStateException("Bad state"); |
| 42 | } catch (IllegalStateException e) { |
| 43 | logRecord.setThrown(e); |
| 44 | } |
| 45 | |
| 46 | String result = formatter.format(logRecord); |
| 47 | |
| 48 | // Verify JSON content |
| 49 | Assert.assertTrue(result.startsWith("{")); |
| 50 | JSONParser parser = new JSONParser(new StringReader(result)); |
| 51 | LinkedHashMap<String,Object> json = parser.object(); |
| 52 | Assert.assertEquals(json.get("method"), "testFormat"); |
| 53 | @SuppressWarnings("unchecked") |
| 54 | ArrayList<Object> trace = (ArrayList<Object>) json.get("throwable"); |
| 55 | Assert.assertEquals(trace.get(0), "java.lang.IllegalStateException: Bad state"); |
| 56 | } |
| 57 | |
| 58 | @Test |
| 59 | public void testEscaping() { |
nothing calls this directly
no test coverage detected