📄 testtimesheetutils.java
字号:
package net.java.workeffort.service.support;import java.math.BigDecimal;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.List;import junit.framework.Test;import junit.framework.TestCase;import junit.framework.TestSuite;import net.java.workeffort.infrastructure.uom.CurrencyMismatchException;import net.java.workeffort.infrastructure.uom.Money;import net.java.workeffort.service.domain.PartyRate;import net.java.workeffort.service.domain.TimeEntry;import net.java.workeffort.service.domain.Timesheet;/** * @author Antony Joseph */public class TestTimesheetUtils extends TestCase { TimesheetUtils timesheetUtils; public static Test suite() { TestSuite suite = new TestSuite(); suite.addTestSuite(TestTimesheetUtils.class); return suite; } protected void setUp() throws Exception { timesheetUtils = new TimesheetUtils(); timesheetUtils.setTimesheetPeriod(TimesheetUtils.WEEKLY); } public void testValidateRates_invalidRateDateRange() throws Exception { List partyRateList = new ArrayList(); //test for invalid date range. thruDt < fromDt PartyRate pr1 = new PartyRate(); pr1.setFromDt(timesheetUtils .convertStringToDate("20050606", "yyyyMMdd")); pr1.setThruDt(timesheetUtils .convertStringToDate("20050605", "yyyyMMdd")); pr1.setRateTypeCd("RT"); pr1.setRate(new BigDecimal(10.00)); pr1.setRateCur("USD"); pr1.setPartyCd("xyz"); partyRateList.add(pr1); try { timesheetUtils.validateRates("xyz", partyRateList); fail("An InvalidDateRangeException should be raised"); } catch (InvalidRateDateRangeException expected) { assertTrue(true); } } public void testValidateRates_conflictingRateDateRanges_thruDtNull() throws Exception { List partyRateList = new ArrayList(); // for conflicting dates. Thru dates are blank which default to a // date way into future PartyRate pr2 = new PartyRate(); pr2.setFromDt(timesheetUtils .convertStringToDate("20050606", "yyyyMMdd")); pr2.setRateTypeCd("RT"); pr2.setRate(new BigDecimal(10.00)); pr2.setRateCur("USD"); pr2.setPartyCd("xyz"); partyRateList.add(pr2); PartyRate pr3 = new PartyRate(); pr3.setFromDt(timesheetUtils .convertStringToDate("20050607", "yyyyMMdd")); pr3.setRateTypeCd("RT"); pr3.setRate(new BigDecimal(10.00)); pr3.setRateCur("USD"); pr3.setPartyCd("xyz"); partyRateList.add(pr3); try { timesheetUtils.validateRates("xyz", partyRateList); fail("An ConflictingRateDateRangeException should be raised"); } catch (ConflictingRateDateRangeException expected) { assertTrue(true); } } public void testValidateRates_conflictingRateDateRanges_thruDtNotNull() throws Exception { List partyRateList = new ArrayList(); // overlapping date ranges. PartyRate pr4 = new PartyRate(); pr4.setFromDt(timesheetUtils .convertStringToDate("20040601", "yyyyMMdd")); pr4.setThruDt(timesheetUtils .convertStringToDate("20050531", "yyyyMMdd")); pr4.setRateTypeCd("RT"); pr4.setRate(new BigDecimal(10.00)); pr4.setRateCur("USD"); pr4.setPartyCd("xyz"); partyRateList.add(pr4); PartyRate pr5 = new PartyRate(); pr5.setFromDt(timesheetUtils .convertStringToDate("20040607", "yyyyMMdd")); pr5.setThruDt(timesheetUtils .convertStringToDate("20040610", "yyyyMMdd")); pr5.setRateTypeCd("RT"); pr5.setRate(new BigDecimal(10.00)); pr5.setRateCur("USD"); pr5.setPartyCd("xyz"); partyRateList.add(pr5); try { timesheetUtils.validateRates("xyz", partyRateList); fail("An ConflictingRateDateRangesException should be raised"); } catch (ConflictingRateDateRangeException expected) { assertTrue(true); } } public void testValidateRates_differentRateTypesSuccess() throws Exception { List partyRateList = new ArrayList(); // There should be *NO* exception because the rate types are different PartyRate pr6 = new PartyRate(); pr6.setFromDt(timesheetUtils .convertStringToDate("20040601", "yyyyMMdd")); pr6.setThruDt(timesheetUtils .convertStringToDate("20050531", "yyyyMMdd")); pr6.setRateTypeCd("RT"); pr6.setRate(new BigDecimal(10.00)); pr6.setRateCur("USD"); pr6.setPartyCd("xyz"); partyRateList.add(pr6); PartyRate pr7 = new PartyRate(); pr7.setFromDt(timesheetUtils .convertStringToDate("20040607", "yyyyMMdd")); pr7.setThruDt(timesheetUtils .convertStringToDate("20040610", "yyyyMMdd")); pr7.setRateTypeCd("OT"); pr7.setRate(new BigDecimal(10.00)); pr7.setRateCur("USD"); pr7.setPartyCd("xyz"); partyRateList.add(pr7); try { timesheetUtils.validateRates("xyz", partyRateList); assertTrue(true); } catch (Exception unexpected) { fail("No exception should have been raised"); } } public void testValidateRates_partyCdMismatch() throws Exception { List partyRateList = new ArrayList(); // There should be *NO* exception because the rate types are different PartyRate pr6 = new PartyRate(); pr6.setFromDt(timesheetUtils .convertStringToDate("20040601", "yyyyMMdd")); pr6.setThruDt(timesheetUtils .convertStringToDate("20050531", "yyyyMMdd")); pr6.setRateTypeCd("RT"); pr6.setRate(new BigDecimal(10.00)); pr6.setRateCur("USD"); pr6.setPartyCd("xyz"); partyRateList.add(pr6); // The partyCd does not match the partyCd in partyRate try { timesheetUtils.validateRates("AAAA", partyRateList); fail("IllegalArgumentException should have been raised"); } catch (IllegalArgumentException expected) { assertTrue(true); } } public void testNoOfDaysInTimesheet_weekly() throws Exception { //WEEKLY test timesheetUtils.setTimesheetPeriod(TimesheetUtils.WEEKLY); Calendar cal = Calendar.getInstance(); cal.setTime(timesheetUtils.convertStringToDate("20050613", "yyyyMMdd")); int days = timesheetUtils.getNoOfDaysInTimesheet(cal); assertEquals(7, days); } public void testNoOfDaysInTimesheet_semiMonthly_firstHalf() throws Exception { //SEMI-MONTYLY: first half of month. Calendar cal = Calendar.getInstance(); timesheetUtils.setTimesheetPeriod(TimesheetUtils.SEMI_MONTHLY); cal.setTime(timesheetUtils.convertStringToDate("20050501", "yyyyMMdd")); int days = timesheetUtils.getNoOfDaysInTimesheet(cal); assertEquals(15, days); } public void testNoOfDaysInTimesheet_semiMonthly_secondHalf() throws Exception { // second half of month. timesheetUtils.setTimesheetPeriod(TimesheetUtils.SEMI_MONTHLY); Calendar cal = Calendar.getInstance(); cal.setTime(timesheetUtils.convertStringToDate("20050516", "yyyyMMdd")); int days = timesheetUtils.getNoOfDaysInTimesheet(cal); assertEquals(16, days); } public void testNoOfDaysInTimesheet_semiMonthly_leapYear() throws Exception { //leap year test timesheetUtils.setTimesheetPeriod(TimesheetUtils.SEMI_MONTHLY); Calendar cal = Calendar.getInstance(); cal.setTime(timesheetUtils.convertStringToDate("20040216", "yyyyMMdd")); int days = timesheetUtils.getNoOfDaysInTimesheet(cal); assertEquals(14, days); } public void testValidateTimesheetPeriod_weekly_fromDtNotMonday() throws Exception { timesheetUtils.setTimesheetPeriod(TimesheetUtils.WEEKLY); // test where start date is not a monday. Timesheet timesheet = new Timesheet(); // set date to a sunday. timesheet.setFromDt(timesheetUtils.convertStringToDate("20050612", "yyyyMMdd")); timesheet.setThruDt(timesheetUtils.convertStringToDate("20050618", "yyyyMMdd")); try { timesheetUtils.validateTimesheetPeriod(timesheet); fail("An InvalidTimesheetPeriodException should be raised"); } catch (InvalidTimesheetPeriodException expected) { assertTrue(true); } } public void testValidateTimesheetPeriod_weekly_invalidPeriod() throws Exception { // test where the fromDt is a monday but the range is invalid because // it has 8 days. Timesheet timesheet = new Timesheet(); timesheet.setFromDt(timesheetUtils.convertStringToDate("20050613", "yyyyMMdd")); timesheet.setThruDt(timesheetUtils.convertStringToDate("20050620", "yyyyMMdd")); try { timesheetUtils.validateTimesheetPeriod(timesheet); fail("An InvalidTimesheetPeriodException should be raised."); } catch (InvalidTimesheetPeriodException expected) { assertTrue(true); } } public void testValidateTimesheetPeriod_weekly_validPeriod() throws Exception { // These date ranges should succeed. Starts on a monday with 7 days Timesheet timesheet = new Timesheet(); timesheet.setFromDt(timesheetUtils.convertStringToDate("20050613", "yyyyMMdd")); timesheet.setThruDt(timesheetUtils.convertStringToDate("20050619", "yyyyMMdd")); try { timesheetUtils.validateTimesheetPeriod(timesheet); assertTrue(true); } catch (Exception unexpected) { fail("An Exception should not be raised."); } } public void testValidateTimesheetPeriod_semiMonthly_firstHalfValid() throws Exception { // test of success timesheetUtils.setTimesheetPeriod(TimesheetUtils.SEMI_MONTHLY); Timesheet timesheet = new Timesheet(); timesheet.setFromDt(timesheetUtils.convertStringToDate("20050601", "yyyyMMdd")); timesheet.setThruDt(timesheetUtils.convertStringToDate("20050615", "yyyyMMdd")); try { timesheetUtils.validateTimesheetPeriod(timesheet); assertTrue(true); } catch (Exception unexpected) { fail("An Exception should not be raised."); } } public void testValidateTimesheetPeriod_semiMonthly_secondHalfValid() throws Exception { // succes for 2nd half of month timesheetUtils.setTimesheetPeriod(TimesheetUtils.SEMI_MONTHLY); Timesheet timesheet = new Timesheet(); timesheet.setFromDt(timesheetUtils.convertStringToDate("20050616", "yyyyMMdd")); timesheet.setThruDt(timesheetUtils.convertStringToDate("20050630", "yyyyMMdd")); try { timesheetUtils.validateTimesheetPeriod(timesheet); assertTrue(true); } catch (Exception unexpected) { fail("An Exception should not be raised."); } } public void testValidateTimesheetPeriod_semiMonthly_leapYearValid() throws Exception { // leap year success timesheetUtils.setTimesheetPeriod(TimesheetUtils.SEMI_MONTHLY); Timesheet timesheet = new Timesheet(); timesheet.setFromDt(timesheetUtils.convertStringToDate("20040216", "yyyyMMdd")); timesheet.setThruDt(timesheetUtils.convertStringToDate("20040229", "yyyyMMdd")); try { timesheetUtils.validateTimesheetPeriod(timesheet); assertTrue(true); } catch (Exception unexpected) { fail("An Exception should not be raised."); } } public void testValidateTimesheetPeriod_semiMonthly_firstHalfInvalidFromDt()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -