@Deployment public void testHistoricTaskInstanceQuery() throws Exception {
  Calendar start=Calendar.getInstance();
  start.set(Calendar.MILLISECOND,0);
  processEngineConfiguration.getClock().setCurrentTime(start.getTime());
  ProcessInstance finishedInstance=runtimeService.startProcessInstanceByKey("HistoricTaskQueryTest","myBusinessKey");
  processEngineConfiguration.getClock().reset();
  Task task=taskService.createTaskQuery().processInstanceId(finishedInstance.getId()).singleResult();
  task.setPriority(1234);
  task.setOwner("fozzie");
  Date dueDate=new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").parse("01/02/2003 04:05:06");
  task.setDueDate(dueDate);
  taskService.saveTask(task);
  taskService.addUserIdentityLink(task.getId(),"gonzo","someType");
  String taskId=task.getId();
  taskService.complete(taskId);
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskId(taskId).count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskId("unexistingtaskid").count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskName("Clean up").count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskName("unexistingname").count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskNameLike("Clean u%").count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskNameLike("%lean up").count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskNameLike("%lean u%").count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskNameLike("%unexistingname%").count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskDescription("Historic task description").count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskDescription("unexistingdescription").count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskDescriptionLike("%task description").count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskDescriptionLike("Historic task %").count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskDescriptionLike("%task%").count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskDescriptionLike("%unexistingdescripton%").count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().processInstanceId(finishedInstance.getId()).count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().executionId("unexistingexecution").count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().processInstanceId(finishedInstance.getId()).count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().processInstanceId("unexistingid").count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().processInstanceBusinessKey("myBusinessKey").count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().processInstanceBusinessKey("unexistingKey").count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().processDefinitionId(finishedInstance.getProcessDefinitionId()).count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().processDefinitionId("unexistingdefinitionid").count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().processDefinitionName("Historic task query test process").count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().processDefinitionName("unexistingdefinitionname").count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().processDefinitionKey("HistoricTaskQueryTest").count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().processDefinitionKey("unexistingdefinitionkey").count());
  List<String> includeIds=new ArrayList<String>();
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().processDefinitionKeyIn(includeIds).count());
  includeIds.add("unexistingProcessDefinition");
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().processDefinitionKeyIn(includeIds).count());
  includeIds.add("HistoricTaskQueryTest");
  assertEquals(1,historyService.createHistoricProcessInstanceQuery().processDefinitionKeyIn(includeIds).count());
  HistoricTaskInstance historicTask=historyService.createHistoricTaskInstanceQuery().processInstanceId(finishedInstance.getId()).singleResult();
  assertEquals("testFormKey",historicTask.getFormKey());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskAssignee("kermit").count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskAssignee("johndoe").count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskAssigneeLike("%ermit").count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskAssigneeLike("kermi%").count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskAssigneeLike("%ermi%").count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskAssigneeLike("%johndoe%").count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskDeleteReason("deleted").count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskDefinitionKey("task").count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskDefinitionKey("unexistingkey").count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskPriority(1234).count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskPriority(5678).count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskMinPriority(1234).count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskMinPriority(1000).count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskMinPriority(1300).count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskMaxPriority(1234).count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskMaxPriority(1300).count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskMaxPriority(1000).count());
  Calendar anHourAgo=Calendar.getInstance();
  anHourAgo.setTime(dueDate);
  anHourAgo.add(Calendar.HOUR,-1);
  Calendar anHourLater=Calendar.getInstance();
  anHourLater.setTime(dueDate);
  anHourLater.add(Calendar.HOUR,1);
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskDueDate(dueDate).count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskDueDate(anHourAgo.getTime()).count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskDueDate(anHourLater.getTime()).count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskDueBefore(anHourLater.getTime()).count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskDueBefore(anHourAgo.getTime()).count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskDueAfter(anHourAgo.getTime()).count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskDueAfter(anHourLater.getTime()).count());
  anHourAgo=new GregorianCalendar();
  anHourAgo.setTime(start.getTime());
  anHourAgo.add(Calendar.HOUR,-1);
  anHourLater=Calendar.getInstance();
  anHourLater.setTime(start.getTime());
  anHourLater.add(Calendar.HOUR,1);
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskCreatedOn(start.getTime()).count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskCreatedOn(anHourAgo.getTime()).count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskCreatedAfter(anHourAgo.getTime()).count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskCreatedAfter(anHourLater.getTime()).count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskCreatedBefore(anHourAgo.getTime()).count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskCreatedBefore(anHourLater.getTime()).count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskCompletedAfter(anHourAgo.getTime()).count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskCompletedAfter(anHourLater.getTime()).count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskCompletedBefore(anHourAgo.getTime()).count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskCompletedBefore(anHourLater.getTime()).count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskInvolvedUser("kermit").count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskInvolvedUser("fozzie").count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskInvolvedUser("gonzo").count());
  runtimeService.startProcessInstanceByKey("HistoricTaskQueryTest");
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().finished().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().unfinished().count());
}
