1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.kuali.kfs.module.ec.util;
20
21 import java.util.EnumSet;
22 import java.util.Map;
23 import java.util.Set;
24
25 import junit.framework.TestCase;
26
27 public class AccountingPeriodMonthTest extends TestCase {
28
29
30
31
32 protected void setUp() throws Exception {
33 super.setUp();
34 }
35
36
37
38
39
40
41 public void testFindAccountingPeriod() throws Exception {
42 AccountingPeriodMonth month1 = AccountingPeriodMonth.findAccountingPeriod(AccountingPeriodMonth.MONTH1.periodCode);
43 assertEquals(AccountingPeriodMonth.MONTH1, month1);
44
45 AccountingPeriodMonth month12 = AccountingPeriodMonth.findAccountingPeriod(AccountingPeriodMonth.MONTH12.periodCode);
46 assertEquals(AccountingPeriodMonth.MONTH12, month12);
47
48 AccountingPeriodMonth month6 = AccountingPeriodMonth.findAccountingPeriod(AccountingPeriodMonth.MONTH6.periodCode);
49 assertEquals(AccountingPeriodMonth.MONTH6, month6);
50 assertFalse(AccountingPeriodMonth.MONTH1.compareTo(month6) == 0);
51 assertFalse(AccountingPeriodMonth.MONTH12.compareTo(month6) == 0);
52
53 AccountingPeriodMonth unknownMonth = AccountingPeriodMonth.findAccountingPeriod("UNKNOWN");
54 assertNull(unknownMonth);
55
56 AccountingPeriodMonth emptyMonth = AccountingPeriodMonth.findAccountingPeriod("");
57 assertNull(emptyMonth);
58 }
59
60
61
62
63
64
65 public void testFindAccountingPeriodsBetween_WithinSameYear() throws Exception {
66 Integer year = 2008;
67
68 this.assertAccountingPeriodsWithinYear(year, AccountingPeriodMonth.MONTH1, AccountingPeriodMonth.MONTH12);
69
70 this.assertAccountingPeriodsWithinYear(year, AccountingPeriodMonth.MONTH1, AccountingPeriodMonth.MONTH3);
71 this.assertAccountingPeriodsWithinYear(year, AccountingPeriodMonth.MONTH4, AccountingPeriodMonth.MONTH8);
72 this.assertAccountingPeriodsWithinYear(year, AccountingPeriodMonth.MONTH9, AccountingPeriodMonth.MONTH12);
73
74 try {
75 this.assertAccountingPeriodsWithinYear(year, AccountingPeriodMonth.MONTH9, AccountingPeriodMonth.MONTH5);
76 fail();
77 }
78 catch (IllegalArgumentException e) {
79 }
80 }
81
82
83
84
85
86
87 public void testFindAccountingPeriodsBetween_AcrossMultipleYears() throws Exception {
88 Integer beginYear = 2008;
89 Integer endYear = 2010;
90
91 this.assertAccountingPeriodsAcrossYears(beginYear, AccountingPeriodMonth.MONTH1, endYear, AccountingPeriodMonth.MONTH12);
92
93 this.assertAccountingPeriodsAcrossYears(beginYear, AccountingPeriodMonth.MONTH1, endYear, AccountingPeriodMonth.MONTH4);
94 this.assertAccountingPeriodsAcrossYears(beginYear, AccountingPeriodMonth.MONTH5, endYear, AccountingPeriodMonth.MONTH12);
95 this.assertAccountingPeriodsAcrossYears(beginYear, AccountingPeriodMonth.MONTH5, endYear, AccountingPeriodMonth.MONTH4);
96
97 try {
98 this.assertAccountingPeriodsAcrossYears(endYear, AccountingPeriodMonth.MONTH5, beginYear, AccountingPeriodMonth.MONTH4);
99 fail();
100 }
101 catch (IllegalArgumentException e) {
102 }
103 }
104
105
106
107
108
109
110 public void testBuildPeriodCodeSetWithinRange() throws Exception {
111 this.assertAccountingPeriodsWithinRange(AccountingPeriodMonth.MONTH1, AccountingPeriodMonth.MONTH12);
112
113 this.assertAccountingPeriodsWithinRange(AccountingPeriodMonth.MONTH1, AccountingPeriodMonth.MONTH3);
114 this.assertAccountingPeriodsWithinRange(AccountingPeriodMonth.MONTH4, AccountingPeriodMonth.MONTH8);
115 this.assertAccountingPeriodsWithinRange(AccountingPeriodMonth.MONTH9, AccountingPeriodMonth.MONTH12);
116
117 try {
118 this.assertAccountingPeriodsWithinRange(AccountingPeriodMonth.MONTH9, AccountingPeriodMonth.MONTH5);
119 fail();
120 }
121 catch (IllegalArgumentException e) {
122 }
123 }
124
125 private void assertAccountingPeriodsWithinYear(Integer year, AccountingPeriodMonth beginPeriod, AccountingPeriodMonth endPeriod) {
126 Map<Integer, Set<String>> periods = AccountingPeriodMonth.findAccountingPeriodsBetween(year, beginPeriod.periodCode, year, endPeriod.periodCode);
127
128 Set<String> periodCodes = periods.get(year);
129 this.assertAccountingPeriodMonthEqual(periodCodes, beginPeriod, endPeriod);
130 }
131
132 private void assertAccountingPeriodsWithinRange(AccountingPeriodMonth beginPeriod, AccountingPeriodMonth endPeriod) {
133 Set<String> periodCodes = AccountingPeriodMonth.buildPeriodCodeSetWithinRange(beginPeriod, endPeriod);
134 this.assertAccountingPeriodMonthEqual(periodCodes, beginPeriod, endPeriod);
135 }
136
137 private void assertAccountingPeriodsAcrossYears(Integer beginYear, AccountingPeriodMonth beginPeriod, Integer endYear, AccountingPeriodMonth endPeriod) {
138 Map<Integer, Set<String>> periods = AccountingPeriodMonth.findAccountingPeriodsBetween(beginYear, beginPeriod.periodCode, endYear, endPeriod.periodCode);
139
140 Set<String> beginPeriodCodes = periods.get(beginYear);
141 this.assertAccountingPeriodMonthEqual(beginPeriodCodes, beginPeriod, AccountingPeriodMonth.MONTH12);
142
143 for (int year = beginYear + 1; year <= endYear - 1; year++) {
144 Set<String> periodCodes = periods.get(year);
145 this.assertAccountingPeriodMonthEqual(periodCodes, AccountingPeriodMonth.MONTH1, AccountingPeriodMonth.MONTH12);
146 }
147
148 Set<String> endPeriodCodes = periods.get(endYear);
149 this.assertAccountingPeriodMonthEqual(endPeriodCodes, AccountingPeriodMonth.MONTH1, endPeriod);
150 }
151
152 private void assertAccountingPeriodMonthEqual(Set<String> periodCodes, AccountingPeriodMonth beginPeriod, AccountingPeriodMonth endPeriod) {
153 Set<AccountingPeriodMonth> accountingPeriodMonth = EnumSet.range(beginPeriod, endPeriod);
154 assertTrue(periodCodes.size() == accountingPeriodMonth.size());
155
156 for (String code : periodCodes) {
157 AccountingPeriodMonth month = AccountingPeriodMonth.findAccountingPeriod(code);
158 assertTrue(accountingPeriodMonth.contains(month));
159 }
160 }
161 }