Refactored MonthComparator into FiscalYearCalculator.
[fiscal-year.java.git] / src / test / java / com / github / sebhoss / time / FiscalDateGetFiscalMonthTest.java
blob9f06eecf1f215d07ae193e29424ebe23f852edcb
1 /*
2 * This program is free software. It comes without any warranty, to
3 * the extent permitted by applicable law. You can redistribute it
4 * and/or modify it under the terms of the Do What The Fuck You Want
5 * To Public License, Version 2, as published by Sam Hocevar. See
6 * http://www.wtfpl.net/ for more details.
7 */
8 package com.github.sebhoss.time;
10 import org.joda.time.LocalDate;
11 import org.joda.time.Months;
12 import org.junit.Assert;
13 import org.junit.Assume;
14 import org.junit.experimental.theories.DataPoints;
15 import org.junit.experimental.theories.Theories;
16 import org.junit.experimental.theories.Theory;
17 import org.junit.runner.RunWith;
19 import com.github.sebhoss.common.annotation.CompilerWarnings;
21 /**
22 * Test cases for {@link FiscalDate#getFiscalMonth()}.
24 @RunWith(Theories.class)
25 @SuppressWarnings(CompilerWarnings.STATIC_METHOD)
26 public class FiscalDateGetFiscalMonthTest {
28 /** @see TestObjects#supportedMonths() */
29 @DataPoints
30 public static Months[] START_DATES = TestObjects.supportedMonths();
32 /** @see TestObjects#startDates() */
33 @DataPoints
34 public static LocalDate[] MONTH_START_DATES = TestObjects.startDates();
36 /** @see TestObjects#middleDates() */
37 @DataPoints
38 public static LocalDate[] MONTH_MIDDLE_DATES = TestObjects.middleDates();
40 /**
41 * Ensures that for any given date the fiscal month will be decreased if the current calendar date is after the
42 * fiscal year start date.
44 * @param startDate
45 * The start date of the fiscal year.
46 * @param currentDate
47 * The current date in a calendar year.
49 @Theory
50 public void shouldSubtractMonthWhenCurrentDateIsAfterStartDate(final Months startDate, final LocalDate currentDate) {
51 // Given
52 Assume.assumeTrue(currentDate.getMonthOfYear() >= startDate.getMonths());
53 final FiscalDate fiscalDate = FiscalYears.earlyFiscalYear(startDate).create(currentDate);
55 // When
56 final int fiscalMonth = fiscalDate.getFiscalMonth();
58 // Then
59 Assert.assertEquals(currentDate.getMonthOfYear() - startDate.getMonths() + 1, fiscalMonth);
62 /**
63 * Ensures that for any given date the fiscal month will be increased if the current calendar date is before the
64 * fiscal year start date.
66 * @param startDate
67 * The start date of the fiscal year.
68 * @param currentDate
69 * The current date in a calendar year.
71 @Theory
72 public void shouldAddMonthWhenCurrentDateIsBeforeStartDate(final Months startDate, final LocalDate currentDate) {
73 // Given
74 Assume.assumeTrue(currentDate.getMonthOfYear() < startDate.getMonths());
75 final FiscalDate fiscalDate = FiscalYears.earlyFiscalYear(startDate).create(currentDate);
76 final int fiscalMonthOffset = currentDate.monthOfYear().getMaximumValue() - startDate.getMonths() + 1;
78 // When
79 final int fiscalMonth = fiscalDate.getFiscalMonth();
81 // Then
82 Assert.assertEquals(currentDate.getMonthOfYear() + fiscalMonthOffset, fiscalMonth);