LinuxSyslogYearSetter { public final int getDesiredYear(DateTime currentMessageDate) { int yearToReturn; DateTime curMessageDate = new DateTime(currentMessageDate); curMessageDate = curMessageDate.withYear(m_currentYear); if (m_lastSeenMessageDate == null) { m_lastSeenMessageDate = curMessageDate; } final long curDateMillis = curMessageDate.getMillis(); final long lastDateMillis = m_lastSeenMessageDate.getMillis(); final long messageTimeDiff = curDateMillis - lastDateMillis; if (messageTimeDiff < 0) { if (Math.abs(messageTimeDiff) < DECREMENT_DAYS_ALLOWANCE_IN_MILLIS) { yearToReturn = m_currentYear; } else { m_currentYear++; curMessageDate = curMessageDate.withYear(m_currentYear); yearToReturn = m_currentYear; m_lastSeenMessageDate = curMessageDate; final String msg = m_source + ": Year increase by 1, to " + m_currentYear; LinuxSyslog3164ParserBase.s_logger.info(msg); } } else { if (messageTimeDiff < INCREMENT_DAYS_LIMIT_IN_MILLIS) { yearToReturn = m_currentYear; m_lastSeenMessageDate = curMessageDate; } else { yearToReturn = m_currentYear - 1; } } return yearToReturn; } LinuxSyslogYearSetter(String source, int startingYear); static LinuxSyslogYearSetter getYearSetter(String source); final void setYear(int startingYear); final int getDesiredYear(DateTime currentMessageDate); @Override String toString();  }
@Test public void testGetDesiredYearWithLastSeenMessageNotNull() { lsys = new LinuxSyslogYearSetter("", 0); DateTimeZone dtz = DateTimeZone.forID("America/New_York"); DateTime dt = new DateTime(dtz); lsys.getDesiredYear(dt); assertEquals("Last seen message != null ",0,lsys.getDesiredYear(dt)); }
@Test public void testGetDesiredYearWithMsgTimeDiffLessThanIncrementDays() { lsys = new LinuxSyslogYearSetter("", 1988); DateTimeZone dtz = DateTimeZone.forID("Europe/Berlin"); DateTime dt = new DateTime(dtz); assertEquals("messageTimeDiff < INCREMENT_DAYS_LIMIT_IN_MILLIS ",1988,lsys.getDesiredYear(dt)); }
@Test public void testGetDesiredYearWithNegativeMessageTimeDifference() { lsys = new LinuxSyslogYearSetter("", -100); DateTimeZone dtz = DateTimeZone.forID("Europe/Berlin"); DateTime dt = new DateTime(dtz); lsys.getDesiredYear(dt); dtz = DateTimeZone.forID("America/New_York"); dt = new DateTime(dtz); assertEquals("messageTimeDifference < 0 ",-100,lsys.getDesiredYear(dt)); }
@Test public void testGetDesiredYearWithTimeDifferenceOverAllowedTime() { lsys = new LinuxSyslogYearSetter("", -2); DateTimeZone dtz; dtz = DateTimeZone.forID("America/New_York"); DateTime dt = new DateTime(1L, dtz); lsys.getDesiredYear(dt); dt = new DateTime(dtz); int actual = lsys.getDesiredYear(dt); assertEquals("absolute messageTimeDifference > DECREMENT_DAYS_ALLOWANCE_IN_MILLIS ",-1, actual); }