### Question:
RollingIndexNameFormatter implements IndexNameFormatter<LogEvent> { @Override public final String format(LogEvent event) { long eventTimeInMillis = event.getTimeMillis(); if (eventTimeInMillis < currentFileTime) { return doFormat(indexName, eventTimeInMillis); } if (eventTimeInMillis >= nextRolloverTime && rollingOver.compareAndSet(false, true)) { rollover(indexName, eventTimeInMillis); rollingOver.set(false); } if (!rollingOver.get()) { return currentName; } return doFormat(indexName, eventTimeInMillis); } protected  RollingIndexNameFormatter(String indexName, String pattern, long initTimeInMillis, TimeZone timeZone); protected  RollingIndexNameFormatter(String indexName, String pattern, long initTimeInMillis, TimeZone timeZone, String separator); @Override final String format(LogEvent event); @PluginBuilderFactory static RollingIndexNameFormatter.Builder newBuilder(); static final String DEFAULT_SEPARATOR; }### Answer:
@Test public void returnsCurrentTimeIfEventTimeIsBeforeRolloverTime() { LogEvent logEvent = mock(LogEvent.class); when(logEvent.getTimeMillis()).thenReturn(DEFAULT_TEST_TIME_IN_MILLIS); IndexNameFormatter formatter = createRollingIndexNameFormatterBuilder().build(); String formattedIndexName = formatter.format(logEvent); Assert.assertEquals("testIndexName-2017-12-20-23.54", formattedIndexName); }
@Test public void returnsNextRolloverTimeIfEventTimeIsAfterRolloverTime() { LogEvent logEvent = mock(LogEvent.class); when(logEvent.getTimeMillis()).thenReturn(DEFAULT_TEST_TIME_IN_MILLIS + TimeUnit.HOURS.toMillis(1)); IndexNameFormatter formatter = createRollingIndexNameFormatterBuilder().build(); String formattedIndexName = formatter.format(logEvent); Assert.assertEquals("testIndexName-2017-12-21-00.54", formattedIndexName); }
@Test public void returnsPreviousRolloverTimeIfEventTimeIsBeforeCurrentTime() { LogEvent logEvent = mock(LogEvent.class); when(logEvent.getTimeMillis()).thenReturn(DEFAULT_TEST_TIME_IN_MILLIS - TimeUnit.HOURS.toMillis(1)); IndexNameFormatter formatter = createRollingIndexNameFormatterBuilder().build(); String formattedIndexName = formatter.format(logEvent); Assert.assertEquals("testIndexName-2017-12-20-22.54", formattedIndexName); }
@Test public void returnsCustomSeparatorFormattedIndexName() { LogEvent logEvent = mock(LogEvent.class); when(logEvent.getTimeMillis()).thenReturn(DEFAULT_TEST_TIME_IN_MILLIS); RollingIndexNameFormatter.Builder builder = createRollingIndexNameFormatterBuilder(); builder.withSeparator("."); IndexNameFormatter formatter = builder.build(); String formattedIndexName = formatter.format(logEvent); Assert.assertEquals("testIndexName.2017-12-20-23.54", formattedIndexName); }
@Test public void returnsDefaultSeparatorFormattedIndexNameWithoutCustomSeparator() { LogEvent logEvent = mock(LogEvent.class); when(logEvent.getTimeMillis()).thenReturn(DEFAULT_TEST_TIME_IN_MILLIS); IndexNameFormatter formatter = new RollingIndexNameFormatter(TEST_INDEX_NAME, DATE_PATTERN_WITH_MINUTES, DEFAULT_TEST_TIME_IN_MILLIS, TEST_TIME_ZONE); String formattedIndexName = formatter.format(logEvent); Assert.assertEquals("testIndexName-2017-12-20-23.54", formattedIndexName); }