public void testQueryDateVariable() throws Exception {
Map<String, Object> vars = new HashMap<String, Object>();
Date date1 = Calendar.getInstance().getTime();
vars.put("dateVar", date1);
ProcessInstance processInstance1 = runtimeService.startProcessInstanceByKey("oneTaskProcess", vars);
taskService.complete(taskService.createTaskQuery().processInstanceId(processInstance1.getId()).singleResult().getId());
Date date2 = Calendar.getInstance().getTime();
vars = new HashMap<String, Object>();
vars.put("dateVar", date1);
vars.put("dateVar2", date2);
ProcessInstance processInstance2 = runtimeService.startProcessInstanceByKey("oneTaskProcess", vars);
taskService.complete(taskService.createTaskQuery().processInstanceId(processInstance2.getId()).singleResult().getId());
Calendar nextYear = Calendar.getInstance();
nextYear.add(Calendar.YEAR, 1);
vars = new HashMap<String, Object>();
vars.put("dateVar", nextYear.getTime());
ProcessInstance processInstance3 = runtimeService.startProcessInstanceByKey("oneTaskProcess", vars);
taskService.complete(taskService.createTaskQuery().processInstanceId(processInstance3.getId()).singleResult().getId());
Calendar nextMonth = Calendar.getInstance();
nextMonth.add(Calendar.MONTH, 1);
Calendar twoYearsLater = Calendar.getInstance();
twoYearsLater.add(Calendar.YEAR, 2);
Calendar oneYearAgo = Calendar.getInstance();
oneYearAgo.add(Calendar.YEAR, -1);
// Query on single short variable, should result in 2 matches
HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery().variableValueEquals("dateVar", date1);
List<HistoricProcessInstance> processInstances = query.list();
Assert.assertNotNull(processInstances);
Assert.assertEquals(2, processInstances.size());
// Query on two short variables, should result in single value
query = historyService.createHistoricProcessInstanceQuery().variableValueEquals("dateVar", date1).variableValueEquals("dateVar2", date2);
HistoricProcessInstance resultInstance = query.singleResult();
Assert.assertNotNull(resultInstance);
Assert.assertEquals(processInstance2.getId(), resultInstance.getId());
// Query with unexisting variable value
Date unexistingDate = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").parse("01/01/1989 12:00:00");
resultInstance = historyService.createHistoricProcessInstanceQuery().variableValueEquals("dateVar", unexistingDate).singleResult();
Assert.assertNull(resultInstance);
// Test NOT_EQUALS
resultInstance = historyService.createHistoricProcessInstanceQuery().variableValueNotEquals("dateVar", date1).singleResult();
Assert.assertNotNull(resultInstance);
Assert.assertEquals(processInstance3.getId(), resultInstance.getId());
// Test GREATER_THAN
resultInstance = historyService.createHistoricProcessInstanceQuery().variableValueGreaterThan("dateVar", nextMonth.getTime()).singleResult();
Assert.assertNotNull(resultInstance);
Assert.assertEquals(processInstance3.getId(), resultInstance.getId());
Assert.assertEquals(0, historyService.createHistoricProcessInstanceQuery().variableValueGreaterThan("dateVar", nextYear.getTime()).count());
Assert.assertEquals(3, historyService.createHistoricProcessInstanceQuery().variableValueGreaterThan("dateVar", oneYearAgo.getTime()).count());
// Test GREATER_THAN_OR_EQUAL
resultInstance = historyService.createHistoricProcessInstanceQuery().variableValueGreaterThanOrEqual("dateVar", nextMonth.getTime()).singleResult();
Assert.assertNotNull(resultInstance);
Assert.assertEquals(processInstance3.getId(), resultInstance.getId());
resultInstance = historyService.createHistoricProcessInstanceQuery().variableValueGreaterThanOrEqual("dateVar", nextYear.getTime()).singleResult();
Assert.assertNotNull(resultInstance);
Assert.assertEquals(processInstance3.getId(), resultInstance.getId());
Assert.assertEquals(3, historyService.createHistoricProcessInstanceQuery().variableValueGreaterThanOrEqual("dateVar", oneYearAgo.getTime()).count());
// Test LESS_THAN
processInstances = historyService.createHistoricProcessInstanceQuery().variableValueLessThan("dateVar", nextYear.getTime()).list();
Assert.assertEquals(2, processInstances.size());
List<String> expecedIds = Arrays.asList(processInstance1.getId(), processInstance2.getId());
List<String> ids = new ArrayList<String>(Arrays.asList(processInstances.get(0).getId(), processInstances.get(1).getId()));
ids.removeAll(expecedIds);
assertTrue(ids.isEmpty());
Assert.assertEquals(0, historyService.createHistoricProcessInstanceQuery().variableValueLessThan("dateVar", date1).count());
Assert.assertEquals(3, historyService.createHistoricProcessInstanceQuery().variableValueLessThan("dateVar", twoYearsLater.getTime()).count());
// Test LESS_THAN_OR_EQUAL
processInstances = historyService.createHistoricProcessInstanceQuery().variableValueLessThanOrEqual("dateVar", nextYear.getTime()).list();
Assert.assertEquals(3, processInstances.size());
Assert.assertEquals(0, historyService.createHistoricProcessInstanceQuery().variableValueLessThanOrEqual("dateVar", oneYearAgo.getTime()).count());
historyService.deleteHistoricProcessInstance(processInstance1.getId());
historyService.deleteHistoricProcessInstance(processInstance2.getId());
historyService.deleteHistoricProcessInstance(processInstance3.getId());
}