MonthlyPeriodType extends CalendarPeriodType { @Override public Period createPeriod( DateTimeUnit dateTimeUnit, Calendar calendar ) { DateTimeUnit start = new DateTimeUnit( dateTimeUnit ); start.setDay( 1 ); DateTimeUnit end = new DateTimeUnit( dateTimeUnit ); end.setDay( calendar.daysInMonth( end.getYear(), end.getMonth() ) ); return toIsoPeriod( start, end, calendar ); }  @Override String getName(); @Override Period createPeriod( DateTimeUnit dateTimeUnit, Calendar calendar ); @Override int getFrequencyOrder(); @Override String getSqlInterval(); @Override DateTimeUnit getDateWithOffset( DateTimeUnit dateTimeUnit, int offset, Calendar calendar ); @Override List<Period> generatePeriods( DateTimeUnit dateTimeUnit ); @Override List<Period> generateRollingPeriods( DateTimeUnit dateTimeUnit, Calendar calendar ); @Override String getIsoDate( DateTimeUnit dateTimeUnit, Calendar calendar ); @Override String getIsoFormat(); @Override String getIso8601Duration(); @Override Date getRewindedDate( Date date, Integer rewindedPeriods ); static final String NAME; static final int FREQUENCY_ORDER; static final String SQL_INTERVAL; }
@Test public void testCreatePeriod() { testDate = new DateTime( 2009, 8, 15, 0, 0 ); startDate = new DateTime( 2009, 8, 1, 0, 0 ); endDate = new DateTime( 2009, 8, 31, 0, 0 ); Period period = periodType.createPeriod( testDate.toDate() ); assertEquals( startDate.toDate(), period.getStartDate() ); assertEquals( endDate.toDate(), period.getEndDate() ); testDate = new DateTime( 2009, 6, 15, 0, 0 ); startDate = new DateTime( 2009, 6, 1, 0, 0 ); endDate = new DateTime( 2009, 6, 30, 0, 0 ); period = periodType.createPeriod( testDate.toDate() ); assertEquals( startDate.toDate(), period.getStartDate() ); assertEquals( endDate.toDate(), period.getEndDate() ); }
@Test public void testCreatePeriodFromISOString() { String isoPeriod = "201001"; String alternativeIsoPeriod = "201001"; Period period1 = periodType.createPeriod( isoPeriod ); Period period2 = periodType.createPeriod( alternativeIsoPeriod ); testDate = new DateTime( period1.getStartDate()); assertEquals( 2010, testDate.getYear() ); assertEquals( 1, testDate.getMonthOfYear() ); testDate = new DateTime( period2.getStartDate()); assertEquals( 2010, testDate.getYear() ); assertEquals( 1, testDate.getMonthOfYear() ); }
@Test public void testGetDaysInPeriod() { Period pA = periodType.createPeriod( "20040315" ); Period pB = periodType.createPeriod( "201403" ); Period pC = periodType.createPeriod( "2014Q2" ); assertEquals( 1, pA.getDaysInPeriod() ); assertEquals( 31, pB.getDaysInPeriod() ); assertEquals( 91, pC.getDaysInPeriod() ); }
@Test public void testGetNextPeriod() { testDate = new DateTime( 2009, 8, 15, 0, 0 ); Period period = periodType.createPeriod( testDate.toDate() ); period = periodType.getNextPeriod( period ); startDate = new DateTime( 2009, 9, 1, 0, 0 ); endDate = new DateTime( 2009, 9, 30, 0, 0 ); assertEquals( startDate.toDate(), period.getStartDate() ); assertEquals( endDate.toDate(), period.getEndDate() ); }
@Test public void testGetNextPeriods() { testDate = new DateTime( 2009, 8, 15, 0, 0 ); Period period = periodType.createPeriod( testDate.toDate() ); period = periodType.getNextPeriod( period, 3 ); startDate = new DateTime( 2009, 11, 1, 0, 0 ); endDate = new DateTime( 2009, 11, 30, 0, 0 ); assertEquals( startDate.toDate(), period.getStartDate() ); assertEquals( endDate.toDate(), period.getEndDate() ); }
@Test public void testGetPreviousPeriod() { testDate = new DateTime( 2009, 8, 15, 0, 0 ); Period period = periodType.createPeriod( testDate.toDate() ); period = periodType.getPreviousPeriod( period ); startDate = new DateTime( 2009, 7, 1, 0, 0 ); endDate = new DateTime( 2009, 7, 31, 0, 0 ); assertEquals( startDate.toDate(), period.getStartDate() ); assertEquals( endDate.toDate(), period.getEndDate() ); }
@Test public void testIsAfter() { Period april = periodType.createPeriod( new DateTime( 2009, 4, 1, 0, 0 ).toDate() ); Period may = periodType.createPeriod( new DateTime( 2009, 5, 1, 0, 0 ).toDate() ); assertTrue( may.isAfter( april ) ); assertFalse( april.isAfter( may ) ); assertFalse( may.isAfter( null ) ); }
@Test public void testGenerateLast5Years() { testDate = new DateTime( 2009, 8, 15, 0, 0 ); List<Period> periods = periodType.generateLast5Years( testDate.toDate() ); assertEquals( 60, periods.size() ); assertEquals( periodType.createPeriod( new DateTime( 2005, 1, 1, 0, 0 ).toDate() ), periods.get( 0 ) ); assertEquals( periodType.createPeriod( new DateTime( 2005, 2, 1, 0, 0 ).toDate() ), periods.get( 1 ) ); assertEquals( periodType.createPeriod( new DateTime( 2005, 3, 1, 0, 0 ).toDate() ), periods.get( 2 ) ); assertEquals( periodType.createPeriod( new DateTime( 2005, 4, 1, 0, 0 ).toDate() ), periods.get( 3 ) ); assertEquals( periodType.createPeriod( new DateTime( 2005, 5, 1, 0, 0 ).toDate() ), periods.get( 4 ) ); assertEquals( periodType.createPeriod( new DateTime( 2005, 6, 1, 0, 0 ).toDate() ), periods.get( 5 ) ); assertEquals( periodType.createPeriod( new DateTime( 2005, 7, 1, 0, 0 ).toDate() ), periods.get( 6 ) ); assertEquals( periodType.createPeriod( new DateTime( 2005, 8, 1, 0, 0 ).toDate() ), periods.get( 7 ) ); assertEquals( periodType.createPeriod( new DateTime( 2005, 9, 1, 0, 0 ).toDate() ), periods.get( 8 ) ); assertEquals( periodType.createPeriod( new DateTime( 2005, 10, 1, 0, 0 ).toDate() ), periods.get( 9 ) ); assertEquals( periodType.createPeriod( new DateTime( 2005, 11, 1, 0, 0 ).toDate() ), periods.get( 10 ) ); assertEquals( periodType.createPeriod( new DateTime( 2005, 12, 1, 0, 0 ).toDate() ), periods.get( 11 ) ); }
@Test public void testGetPeriodsBetween() { assertEquals( 1, periodType.createPeriod().getPeriodSpan( periodType ) ); assertEquals( 2, new BiMonthlyPeriodType().createPeriod().getPeriodSpan( periodType ) ); assertEquals( 3, new QuarterlyPeriodType().createPeriod().getPeriodSpan( periodType ) ); assertEquals( 6, new SixMonthlyPeriodType().createPeriod().getPeriodSpan( periodType ) ); assertEquals( 12, new YearlyPeriodType().createPeriod().getPeriodSpan( periodType ) ); }