fixed test cases
[fiscal-year.java.git] / src / test / java / com / github / sebhoss / time / FiscalDatePlusMonthsTest.java
blob1421d6b8023c02b03740cf46ce708c86d3ce8471
1 /*
2 * Copyright © 2013 Sebastian Hoß <mail@shoss.de>
3 * This work is free. You can redistribute it and/or modify it under the
4 * terms of the Do What The Fuck You Want To Public License, Version 2,
5 * as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
6 */
7 /*
8 * This program is free software. It comes without any warranty, to
9 * the extent permitted by applicable law. You can redistribute it
10 * and/or modify it under the terms of the Do What The Fuck You Want
11 * To Public License, Version 2, as published by Sam Hocevar. See
12 * http://www.wtfpl.net/ for more details.
14 package com.github.sebhoss.time;
16 import java.time.LocalDate;
17 import java.time.Month;
18 import java.time.Period;
19 import java.util.Random;
21 import com.github.sebhoss.warnings.CompilerWarnings;
23 import org.junit.Assert;
24 import org.junit.experimental.theories.DataPoints;
25 import org.junit.experimental.theories.Theories;
26 import org.junit.experimental.theories.Theory;
27 import org.junit.runner.RunWith;
29 /**
30 * Test cases for {@link FiscalDate#plusMonths(long)}.
32 @RunWith(Theories.class)
33 @SuppressWarnings(CompilerWarnings.STATIC_METHOD)
34 public class FiscalDatePlusMonthsTest {
36 /** @see TestObjects#supportedMonths() */
37 @DataPoints
38 public static final Month[] START_DATES = TestObjects.supportedMonths();
40 /** @see TestObjects#startDates() */
41 @DataPoints
42 public static final LocalDate[] MONTH_START_DATES = TestObjects.startDates();
44 /** @see TestObjects#middleDates() */
45 @DataPoints
46 public static final LocalDate[] MONTH_MIDDLE_DATES = TestObjects.middleDates();
48 /** The amount of months to add a given date */
49 @DataPoints
50 public static final int[] ADDITIONAL_MONTHS = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25, 50, 100, 5000 };
52 private static final Random RANDOM = new Random();
54 /** The amount of random months to add a given date */
55 @DataPoints
56 public static final int[] RANDOM_MONTHS = RANDOM.ints(100, -292275054, 292278993).toArray();
58 /**
59 * Ensures that for any given date a number of years can be added in an early fiscal year.
61 * @param startDate
62 * The start date of the fiscal year.
63 * @param currentDate
64 * The current date in a calendar year.
65 * @param additionalMonths
66 * The amounts of months to add.
68 @Theory
69 public void shouldAddMonthsInEarlyFiscalYear(final Month startDate, final LocalDate currentDate,
70 final int additionalMonths) {
71 // Given
72 final FiscalDate fiscalDate = FiscalYears.earlyFiscalYear(startDate.getValue()).createFromCalendarDate(
73 currentDate);
75 // When
76 final FiscalDate newDate = fiscalDate.plusMonths(additionalMonths);
78 // Then
79 Assert.assertEquals(
80 additionalMonths,
81 Period.between(FiscalDatePlusMonthsTest.toLocalDate(fiscalDate),
82 FiscalDatePlusMonthsTest.toLocalDate(newDate)).toTotalMonths());
85 /**
86 * Ensures that for any given date a number of years can be added in an early fiscal year.
88 * @param startDate
89 * The start date of the fiscal year.
90 * @param currentDate
91 * The current date in a calendar year.
92 * @param additionalMonths
93 * The amounts of months to add.
95 @Theory
96 public void shouldAddMonthsInLateFiscalYear(final Month startDate, final LocalDate currentDate,
97 final int additionalMonths) {
98 // Given
99 final FiscalDate fiscalDate = FiscalYears.lateFiscalYear(startDate.getValue()).createFromCalendarDate(
100 currentDate);
102 // When
103 final FiscalDate newDate = fiscalDate.plusMonths(additionalMonths);
105 // Then
106 Assert.assertEquals(
107 additionalMonths,
108 Period.between(FiscalDatePlusMonthsTest.toLocalDate(fiscalDate),
109 FiscalDatePlusMonthsTest.toLocalDate(newDate)).toTotalMonths());
112 private static LocalDate toLocalDate(final FiscalDate fiscalDate) {
113 return LocalDate.of((int) fiscalDate.getCalendarYear(), (int) fiscalDate.getCalendarMonth(),
114 (int) fiscalDate.getCalendarDayOfMonth());