LogRecordDateBolt extends AbstractLoggingBolt { void logDate(LogRecord record) { Date date = record.getDate(); if (date == null) { logger.error("Record date is null!"); } else { SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); sdf.setTimeZone(TimeZone.getTimeZone(ZoneId.of("UTC"))); super.log(logger, sdf.format(date)); } }  @Override void configure(Configuration conf); static final String DATE_FORMAT; static final String SAMPLE; }
@Test public void testLogDate(@Injectable LogRecord record, @Injectable Logger logger) throws Exception { bolt.logger = logger; bolt.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"; bolt.level = Level.DEBUG; Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); calendar.set(2014, Calendar.JUNE, 10, 20, 22, 4); new Expectations(bolt) {{ record.getDate(); result = calendar.getTime(); bolt.log(logger, "2014-06-10T20:22:04.000Z"); }}; bolt.logDate(record); }
@Test public void testLogDateFail(@Injectable LogRecord record, @Injectable Logger logger) throws Exception { bolt.logger = logger; bolt.level = Level.DEBUG; new Expectations(bolt) {{ record.getDate(); result = null; logger.error(anyString); bolt.log(logger, anyString); times = 0; }}; bolt.logDate(record); }