@Deployment public void testHistoricTaskInstanceOrQuery() 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().or().taskId(taskId).endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().taskId(taskId).or().taskId(taskId).endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskId("unexistingtaskid").count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskId("unexistingtaskid").endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskId(taskId).taskName("Clean up").endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskId("unexistingtaskid").taskName("Clean up").endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskId("unexistingtaskid").taskName("unexistingname").endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskName("Clean up").endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskName("unexistingname").endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskNameLike("Clean u%").endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskNameLike("%unexistingname%").endOr().count());
  final List<String> taskNameList=new ArrayList<String>(1);
  taskNameList.add("Clean up");
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskNameIn(taskNameList).endOr().count());
  taskNameList.clear();
  taskNameList.add("unexistingname");
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskNameIn(taskNameList).endOr().count());
  taskNameList.clear();
  taskNameList.add("clean up");
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskNameInIgnoreCase(taskNameList).endOr().count());
  taskNameList.clear();
  taskNameList.add("unexistingname");
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskNameInIgnoreCase(taskNameList).endOr().count());
  taskNameList.clear();
  taskNameList.add("clean up");
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskName("Clean up").endOr().or().taskNameInIgnoreCase(taskNameList).endOr().count());
  taskNameList.clear();
  taskNameList.add("unexistingname");
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskName("Clean up").endOr().or().taskNameInIgnoreCase(taskNameList).endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskDescription("Historic task description").endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskDescription("unexistingdescription").endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskDescriptionLike("%task description").endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskDescriptionLike("%task description").taskDescription("unexistingdescription").endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().taskDescriptionLike("%unexistingdescripton%").count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskDescriptionLike("%unexistingdescripton%").taskDescription("unexistingdescription").endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskDescription("Historic task description").endOr().or().taskDescriptionLike("%task description").endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskDescription("Historic task description").endOr().or().taskDescriptionLike("%task description2").endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().processInstanceId(finishedInstance.getId()).endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().executionId("unexistingexecution").endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().processInstanceId(finishedInstance.getId()).endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().processInstanceId("unexistingid").endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().processInstanceBusinessKey("myBusinessKey").endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().processInstanceBusinessKey("unexistingKey").endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().processDefinitionId(finishedInstance.getProcessDefinitionId()).endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().processDefinitionId("unexistingdefinitionid").endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().processDefinitionName("Historic task query test process").endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().processDefinitionName("unexistingdefinitionname").endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().processDefinitionKey("HistoricTaskQueryTest").endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().processDefinitionKey("unexistingdefinitionkey").endOr().count());
  Task adhocTask=taskService.newTask();
  taskService.saveTask(adhocTask);
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskId(adhocTask.getId()).processDefinitionKey("unexistingdefinitionkey").endOr().count());
  taskService.deleteTask(adhocTask.getId(),true);
  List<String> includeIds=new ArrayList<String>();
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().processDefinitionKey("unexistingdefinitionkey").processDefinitionKeyIn(includeIds).endOr().count());
  includeIds.add("unexistingProcessDefinition");
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().processDefinitionKey("unexistingdefinitionkey").processDefinitionKeyIn(includeIds).endOr().count());
  includeIds.add("unexistingProcessDefinition");
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().processDefinitionKey("HistoricTaskQueryTest").processDefinitionKeyIn(includeIds).endOr().count());
  includeIds.add("HistoricTaskQueryTest");
  assertEquals(1,historyService.createHistoricProcessInstanceQuery().or().processDefinitionKey("unexistingdefinitionkey").processDefinitionKeyIn(includeIds).endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskAssignee("kermit").endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskAssignee("johndoe").endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskAssigneeLike("%ermit").endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskAssigneeLike("%johndoe%").endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskAssignee("kermit").endOr().or().taskAssigneeLike("%ermit").endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskAssignee("kermit").endOr().or().taskAssigneeLike("%johndoe%").endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskDeleteReason("deleted").endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskDefinitionKey("task").endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskDefinitionKey("unexistingkey").endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskPriority(1234).endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskPriority(5678).endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskMinPriority(1234).endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskMinPriority(1000).endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskMinPriority(1300).endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskMaxPriority(1234).endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskMaxPriority(1300).endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskMaxPriority(1000).endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskPriority(1234).endOr().or().taskMinPriority(1234).endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskPriority(1234).endOr().or().taskMinPriority(1300).endOr().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().or().taskDueDate(dueDate).endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskDueDate(anHourAgo.getTime()).endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskDueDate(anHourLater.getTime()).endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskDueBefore(anHourLater.getTime()).endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskDueBefore(anHourAgo.getTime()).endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskDueAfter(anHourAgo.getTime()).endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskDueAfter(anHourLater.getTime()).endOr().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().or().taskCreatedOn(start.getTime()).endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskCreatedOn(anHourAgo.getTime()).endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskCreatedAfter(anHourAgo.getTime()).endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskCreatedAfter(anHourLater.getTime()).endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskCreatedBefore(anHourAgo.getTime()).endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskCreatedBefore(anHourLater.getTime()).endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskCompletedAfter(anHourAgo.getTime()).endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskCompletedAfter(anHourLater.getTime()).endOr().count());
  assertEquals(0,historyService.createHistoricTaskInstanceQuery().or().taskCompletedBefore(anHourAgo.getTime()).endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskCompletedBefore(anHourLater.getTime()).endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskInvolvedUser("kermit").endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskInvolvedUser("fozzie").endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().taskInvolvedUser("gonzo").endOr().count());
  runtimeService.startProcessInstanceByKey("HistoricTaskQueryTest");
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().finished().endOr().count());
  assertEquals(1,historyService.createHistoricTaskInstanceQuery().or().unfinished().endOr().count());
}
