TaskManager { public boolean createTask(Date period, String domain, String name, TaskCreationPolicy prolicy) { try { if (prolicy.shouldCreateHourlyTask()) { insertToDatabase(period, domain, name, REPORT_HOUR); } Calendar cal = Calendar.getInstance(); cal.setTime(period); int hour = cal.get(Calendar.HOUR_OF_DAY); cal.add(Calendar.HOUR_OF_DAY, -hour); Date currentDay = cal.getTime(); if (prolicy.shouldCreateDailyTask()) { insertToDatabase(new Date(currentDay.getTime() - ONE_DAY), domain, name, REPORT_DAILY); } if (prolicy.shouldCreateWeeklyTask()) { int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); if (dayOfWeek == 7) { insertToDatabase(new Date(currentDay.getTime() - 7 * ONE_DAY), domain, name, REPORT_WEEK); } } if (prolicy.shouldCreateMonthTask()) { int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); if (dayOfMonth == 1) { cal.add(Calendar.MONTH, -1); insertToDatabase(cal.getTime(), domain, name, REPORT_MONTH); } } return true; } catch (DalException e) { Cat.logError(e); return false; } }  boolean createTask(Date period, String domain, String name, TaskCreationPolicy prolicy); static final int REPORT_HOUR; static final int REPORT_DAILY; static final int REPORT_WEEK; static final int REPORT_MONTH; }
@Test public void testAll() throws Exception { MockTaskManager analyzer = new MockTaskManager(); Date start = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2013-04-23 00:00"); Date end = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2013-06-23 00:00"); long dateLong = start.getTime(); for (; dateLong < end.getTime(); dateLong = dateLong + HOUR) { Date date = new Date(dateLong); analyzer.createTask(date, "cat", "trasnaction", TaskProlicy.ALL); } Map<Integer, Set<String>> result = analyzer.getResults(); Assert.assertEquals(2, result.get(TaskManager.REPORT_MONTH).size()); Assert.assertEquals(9, result.get(TaskManager.REPORT_WEEK).size()); Assert.assertEquals(61, result.get(TaskManager.REPORT_DAILY).size()); Assert.assertEquals(61 * 24, result.get(TaskManager.REPORT_HOUR).size()); }
@Test public void testExcludeHourly() throws Exception { MockTaskManager analyzer = new MockTaskManager(); Date start = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2013-04-23 00:00"); Date end = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2013-06-23 00:00"); long dateLong = start.getTime(); for (; dateLong < end.getTime(); dateLong = dateLong + HOUR) { Date date = new Date(dateLong); analyzer.createTask(date, "cat", "trasnaction", TaskProlicy.ALL_EXCLUED_HOURLY); } Map<Integer, Set<String>> result = analyzer.getResults(); Assert.assertEquals(2, result.get(TaskManager.REPORT_MONTH).size()); Assert.assertEquals(9, result.get(TaskManager.REPORT_WEEK).size()); Assert.assertEquals(61, result.get(TaskManager.REPORT_DAILY).size()); Assert.assertEquals(null, result.get(TaskManager.REPORT_HOUR)); }