@Deployment(resources={"org/activiti/engine/test/history/oneTaskProcess.bpmn20.xml"}) public void testHistoricProcessInstanceQuery(){
  Calendar startTime=Calendar.getInstance();
  processEngineConfiguration.getClock().setCurrentTime(startTime.getTime());
  ProcessInstance processInstance=runtimeService.startProcessInstanceByKey("oneTaskProcess","businessKey123");
  runtimeService.addUserIdentityLink(processInstance.getId(),"kermit","someType");
  runtimeService.setProcessInstanceName(processInstance.getId(),"The name");
  Calendar hourAgo=Calendar.getInstance();
  hourAgo.add(Calendar.HOUR_OF_DAY,-1);
  Calendar hourFromNow=Calendar.getInstance();
  hourFromNow.add(Calendar.HOUR_OF_DAY,1);
  assertEquals("The name",historyService.createHistoricProcessInstanceQuery().processInstanceName("The name").singleResult().getName());
  assertEquals(1,historyService.createHistoricProcessInstanceQuery().processInstanceName("The name").count());
  assertEquals(0,historyService.createHistoricProcessInstanceQuery().processInstanceName("Other name").count());
  assertEquals(1,historyService.createHistoricProcessInstanceQuery().processInstanceNameLike("% name").count());
  assertEquals(0,historyService.createHistoricProcessInstanceQuery().processInstanceNameLike("%nope").count());
  runtimeService.setProcessInstanceName(processInstance.getId(),"New name");
  assertEquals("New name",historyService.createHistoricProcessInstanceQuery().processInstanceName("New name").singleResult().getName());
  assertEquals(1,historyService.createHistoricProcessInstanceQuery().processInstanceName("New name").count());
  assertEquals(0,historyService.createHistoricProcessInstanceQuery().processInstanceName("The name").count());
  assertEquals(1,historyService.createHistoricProcessInstanceQuery().processInstanceNameLike("New %").count());
  assertEquals(0,historyService.createHistoricProcessInstanceQuery().processInstanceNameLike("The %").count());
  assertEquals(0,historyService.createHistoricProcessInstanceQuery().finishedBefore(hourAgo.getTime()).count());
  assertEquals(0,historyService.createHistoricProcessInstanceQuery().finishedBefore(hourFromNow.getTime()).count());
  assertEquals(0,historyService.createHistoricProcessInstanceQuery().finishedAfter(hourAgo.getTime()).count());
  assertEquals(0,historyService.createHistoricProcessInstanceQuery().finishedAfter(hourFromNow.getTime()).count());
  assertEquals(1,historyService.createHistoricProcessInstanceQuery().startedBefore(hourFromNow.getTime()).count());
  assertEquals(0,historyService.createHistoricProcessInstanceQuery().startedBefore(hourAgo.getTime()).count());
  assertEquals(1,historyService.createHistoricProcessInstanceQuery().startedAfter(hourAgo.getTime()).count());
  assertEquals(0,historyService.createHistoricProcessInstanceQuery().startedAfter(hourFromNow.getTime()).count());
  assertEquals(0,historyService.createHistoricProcessInstanceQuery().finished().count());
  assertEquals(1,historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstance.getId()).count());
  assertEquals(1,historyService.createHistoricProcessInstanceQuery().processDefinitionId(processInstance.getProcessDefinitionId()).count());
  assertEquals(1,historyService.createHistoricProcessInstanceQuery().processDefinitionKey("oneTaskProcess").count());
  assertEquals(1,historyService.createHistoricProcessInstanceQuery().processDefinitionKeyIn(Collections.singletonList("oneTaskProcess")).count());
  assertEquals(1,historyService.createHistoricProcessInstanceQuery().processDefinitionKeyIn(Arrays.asList("undefined","oneTaskProcess")).count());
  assertEquals(0,historyService.createHistoricProcessInstanceQuery().processDefinitionKeyIn(Arrays.asList("undefined1","undefined2")).count());
  assertEquals(1,historyService.createHistoricProcessInstanceQuery().processInstanceBusinessKey("businessKey123").count());
  List<String> excludeIds=new ArrayList<String>();
  excludeIds.add("unexistingProcessDefinition");
  assertEquals(1,historyService.createHistoricProcessInstanceQuery().processDefinitionKeyNotIn(excludeIds).count());
  excludeIds.add("oneTaskProcess");
  assertEquals(0,historyService.createHistoricProcessInstanceQuery().processDefinitionKeyNotIn(excludeIds).count());
  taskService.complete(taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult().getId());
  assertEquals(1,historyService.createHistoricProcessInstanceQuery().finished().count());
  assertEquals(0,historyService.createHistoricProcessInstanceQuery().finishedBefore(hourAgo.getTime()).count());
  assertEquals(1,historyService.createHistoricProcessInstanceQuery().finishedBefore(hourFromNow.getTime()).count());
  assertEquals(1,historyService.createHistoricProcessInstanceQuery().finishedAfter(hourAgo.getTime()).count());
  assertEquals(0,historyService.createHistoricProcessInstanceQuery().finishedAfter(hourFromNow.getTime()).count());
  assertEquals(1,historyService.createHistoricProcessInstanceQuery().involvedUser("kermit").count());
  assertEquals(0,historyService.createHistoricProcessInstanceQuery().involvedUser("gonzo").count());
}
