IMMFutureAndFutureOptionQuarterlyExpiryCalculator implements ExchangeTradedInstrumentExpiryCalculator { @Override public LocalDate getExpiryDate(final int n, final LocalDate today, final Calendar holidayCalendar) { ArgumentChecker.isTrue(n > 0, "n must be greater than zero"); ArgumentChecker.notNull(today, "today"); ArgumentChecker.notNull(holidayCalendar, "holiday calendar"); final LocalDate nextExpiryMonth = today.with(MONTH_ADJUSTER); LocalDate result; if (!today.getMonth().equals(nextExpiryMonth.getMonth())) { result = nextExpiryMonth.plusMonths(3 * (n - 1)).with(THIRD_WEDNESDAY_ADJUSTER); } else { final LocalDate thirdWednesday = today.with(THIRD_WEDNESDAY_ADJUSTER); if (today.isAfter(adjustForSettlement(thirdWednesday, holidayCalendar))) { result = nextExpiryMonth.plusMonths(3 * n).with(THIRD_WEDNESDAY_ADJUSTER); } else { result = nextExpiryMonth.plusMonths(3 * (n - 1)).with(THIRD_WEDNESDAY_ADJUSTER); } } return adjustForSettlement(result, holidayCalendar); } private  IMMFutureAndFutureOptionQuarterlyExpiryCalculator(); static IMMFutureAndFutureOptionQuarterlyExpiryCalculator getInstance(); @Override LocalDate getExpiryDate(final int n, final LocalDate today, final Calendar holidayCalendar); @Override LocalDate getExpiryMonth(final int n, final LocalDate today); @Override String getName(); static final String NAME; }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNegativeN() { CALCULATOR.getExpiryDate(-1, AUGUST_START, CALENDAR); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testZeroN() { CALCULATOR.getExpiryDate(0, AUGUST_START, CALENDAR); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullDate() { CALCULATOR.getExpiryDate(1, null, CALENDAR); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullCalendar() { CALCULATOR.getExpiryDate(2, AUGUST_START, null); }
@Test public void testDifferentMonthStart() { assertEquals(LocalDate.of(2012, 9, 17), CALCULATOR.getExpiryDate(1, AUGUST_START, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2012, 9, 17), CALCULATOR.getExpiryDate(1, AUGUST_START, CALENDAR)); assertEquals(LocalDate.of(2012, 12, 17), CALCULATOR.getExpiryDate(2, AUGUST_START, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2012, 12, 14), CALCULATOR.getExpiryDate(2, AUGUST_START, CALENDAR)); assertEquals(LocalDate.of(2013, 3, 18), CALCULATOR.getExpiryDate(3, AUGUST_START, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 3, 18), CALCULATOR.getExpiryDate(3, AUGUST_START, CALENDAR)); assertEquals(LocalDate.of(2013, 6, 17), CALCULATOR.getExpiryDate(4, AUGUST_START, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 6, 17), CALCULATOR.getExpiryDate(4, AUGUST_START, CALENDAR)); assertEquals(LocalDate.of(2013, 9, 16), CALCULATOR.getExpiryDate(5, AUGUST_START, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 9, 16), CALCULATOR.getExpiryDate(5, AUGUST_START, CALENDAR)); assertEquals(LocalDate.of(2013, 12, 16), CALCULATOR.getExpiryDate(6, AUGUST_START, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 12, 16), CALCULATOR.getExpiryDate(6, AUGUST_START, CALENDAR)); }
@Test public void testDifferentMonthEnd() { assertEquals(LocalDate.of(2012, 9, 17), CALCULATOR.getExpiryDate(1, AUGUST_END, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2012, 9, 17), CALCULATOR.getExpiryDate(1, AUGUST_END, CALENDAR)); assertEquals(LocalDate.of(2012, 12, 17), CALCULATOR.getExpiryDate(2, AUGUST_END, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2012, 12, 14), CALCULATOR.getExpiryDate(2, AUGUST_END, CALENDAR)); assertEquals(LocalDate.of(2013, 3, 18), CALCULATOR.getExpiryDate(3, AUGUST_END, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 3, 18), CALCULATOR.getExpiryDate(3, AUGUST_END, CALENDAR)); assertEquals(LocalDate.of(2013, 6, 17), CALCULATOR.getExpiryDate(4, AUGUST_END, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 6, 17), CALCULATOR.getExpiryDate(4, AUGUST_END, CALENDAR)); assertEquals(LocalDate.of(2013, 9, 16), CALCULATOR.getExpiryDate(5, AUGUST_END, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 9, 16), CALCULATOR.getExpiryDate(5, AUGUST_END, CALENDAR)); assertEquals(LocalDate.of(2013, 12, 16), CALCULATOR.getExpiryDate(6, AUGUST_END, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 12, 16), CALCULATOR.getExpiryDate(6, AUGUST_END, CALENDAR)); }
@Test public void testExpiryMonthBeforeExpiry() { assertEquals(LocalDate.of(2012, 9, 17), CALCULATOR.getExpiryDate(1, SEPTEMBER_START, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2012, 9, 17), CALCULATOR.getExpiryDate(1, SEPTEMBER_START, CALENDAR)); assertEquals(LocalDate.of(2012, 12, 17), CALCULATOR.getExpiryDate(2, SEPTEMBER_START, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2012, 12, 14), CALCULATOR.getExpiryDate(2, SEPTEMBER_START, CALENDAR)); assertEquals(LocalDate.of(2013, 3, 18), CALCULATOR.getExpiryDate(3, SEPTEMBER_START, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 3, 18), CALCULATOR.getExpiryDate(3, SEPTEMBER_START, CALENDAR)); assertEquals(LocalDate.of(2013, 6, 17), CALCULATOR.getExpiryDate(4, SEPTEMBER_START, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 6, 17), CALCULATOR.getExpiryDate(4, SEPTEMBER_START, CALENDAR)); assertEquals(LocalDate.of(2013, 9, 16), CALCULATOR.getExpiryDate(5, SEPTEMBER_START, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 9, 16), CALCULATOR.getExpiryDate(5, SEPTEMBER_START, CALENDAR)); assertEquals(LocalDate.of(2013, 12, 16), CALCULATOR.getExpiryDate(6, SEPTEMBER_START, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 12, 16), CALCULATOR.getExpiryDate(6, SEPTEMBER_START, CALENDAR)); }
@Test public void testExpiryMonthAfterExpiry() { assertEquals(LocalDate.of(2012, 12, 17), CALCULATOR.getExpiryDate(1, SEPTEMBER_END, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2012, 12, 14), CALCULATOR.getExpiryDate(1, SEPTEMBER_END, CALENDAR)); assertEquals(LocalDate.of(2013, 3, 18), CALCULATOR.getExpiryDate(2, SEPTEMBER_END, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 3, 18), CALCULATOR.getExpiryDate(2, SEPTEMBER_END, CALENDAR)); assertEquals(LocalDate.of(2013, 6, 17), CALCULATOR.getExpiryDate(3, SEPTEMBER_END, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 6, 17), CALCULATOR.getExpiryDate(3, SEPTEMBER_END, CALENDAR)); assertEquals(LocalDate.of(2013, 9, 16), CALCULATOR.getExpiryDate(4, SEPTEMBER_END, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 9, 16), CALCULATOR.getExpiryDate(4, SEPTEMBER_END, CALENDAR)); assertEquals(LocalDate.of(2013, 12, 16), CALCULATOR.getExpiryDate(5, SEPTEMBER_END, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2013, 12, 16), CALCULATOR.getExpiryDate(5, SEPTEMBER_END, CALENDAR)); assertEquals(LocalDate.of(2014, 3, 17), CALCULATOR.getExpiryDate(6, SEPTEMBER_END, WEEKEND_CALENDAR)); assertEquals(LocalDate.of(2014, 3, 17), CALCULATOR.getExpiryDate(6, SEPTEMBER_END, CALENDAR)); }