CompactFormatter extends java.util.logging.Formatter { @Override public String format(final LogRecord record) { ResourceBundle rb = record.getResourceBundle(); Locale l = rb == null ? null : rb.getLocale(); String msg = formatMessage(record); String thrown = formatThrown(record); Object[] params = { new Date(record.getMillis()), formatSource(record), formatLoggerName(record), formatLevel(record), msg, thrown, new Alternate(msg, thrown), new Alternate(thrown, msg)}; return String.format(l, fmt, params); } CompactFormatter();  CompactFormatter(final String format); @Override String format(final LogRecord record); @Override String formatMessage(final LogRecord record); String formatMessage(final Throwable t); String formatLevel(final LogRecord record); String formatSource(final LogRecord record); String formatLoggerName(final LogRecord record); String formatThrown(final LogRecord record); String formatBackTrace(LogRecord record);  }
@Test public void testNewFormatterWithPattern() { CompactFormatter cf = new CompactFormatter("%4$s"); String result = cf.format(new LogRecord(Level.SEVERE, "")); assertEquals(Level.SEVERE.getLocalizedName(), result); }
@Test public void testFormatWithMessage() { LogRecord record = new LogRecord(Level.SEVERE, "message"); CompactFormatter cf = new CompactFormatter(); String result = cf.format(record); assertTrue(result, result.startsWith(record.getMessage())); assertTrue(result, result.endsWith(LINE_SEP)); }
@Test public void testFormatWithMessagePrecisionOverWidth() { LogRecord record = new LogRecord(Level.SEVERE, "message"); record.setThrown(new Throwable("thrown")); CompactFormatter cf = new CompactFormatter("%7$#6.12s"); String result = cf.format(record); assertEquals("mes|Throwable", result); }
@Test public void testFormatWithMessageWidthOverPrecision() { LogRecord record = new LogRecord(Level.SEVERE, "message"); record.setThrown(new Throwable("thrown")); CompactFormatter cf = new CompactFormatter("%7$#12.6s"); String result = cf.format(record); assertEquals("mes\u0020\u0020\u0020|Thr\u0020\u0020\u0020", result); }
@Test public void testFormatWithMessageEmpty() { LogRecord record = new LogRecord(Level.SEVERE, ""); CompactFormatter cf = new CompactFormatter(); String result = cf.format(record); assertEquals(result, LINE_SEP); }
@Test public void testFormatMessageSurrogate() { LogRecord record = new LogRecord(Level.SEVERE, "a\ud801\udc00\ud801\udc00\ud801\udc00\ud801\udc00"); record.setThrown(new Throwable("thrown")); CompactFormatter cf = new CompactFormatter("%7$#.6s%n"); String result = cf.format(record); assertTrue(result, result.startsWith("a\ud801\udc00")); assertTrue(result, result.endsWith("|Thr" + LINE_SEP)); }
@Test public void testFormatWithMessageAndThrownLeftToRight() { LogRecord record = new LogRecord(Level.SEVERE, "message"); record.setThrown(new Throwable("thrown")); CompactFormatter cf = new CompactFormatter(); String result = cf.format(record); assertTrue(result, result.startsWith(record.getMessage())); assertTrue(result, result.contains("|")); assertTrue(result, result.contains(Throwable.class.getSimpleName())); assertTrue(result, result.contains(CompactFormatterTest.class.getSimpleName())); assertTrue(result, result.contains("testFormatWithMessageAndThrown")); assertTrue(result, result.contains(String.valueOf( record.getThrown().getStackTrace()[0].getLineNumber()))); assertTrue(result, result.endsWith(LINE_SEP)); }
@Test public void testFormatMaxThrowableWidthLeftToRight() { String fmt = LEFT_TO_RIGHT; int width = MAX_PRE; assertTrue(fmt, fmt.contains(Integer.toString(width))); assertTrue(String.valueOf(width), width < Integer.MAX_VALUE / 4); CompactFormatter cf = new CompactFormatter(fmt); LogRecord record = new LogRecord(Level.SEVERE, ""); int padding = LINE_SEP.length(); for (int i = 0; i < width; i++) { record.setThrown(new Throwable(rpad("", i, "A"))); String result = cf.format(record); assertTrue(result, result.length() <= width + padding); assertTrue(result, result.endsWith(LINE_SEP)); } for (int i = width; i <= (width * 4); i++) { record.setThrown(new Throwable(rpad("", i, "A"))); String result = cf.format(record); assertTrue(result.length() + ", " + (width + padding), result.length() == width + padding); assertTrue(result, result.endsWith(LINE_SEP)); } }