AnsweredAssumed Answered

HistoryServiceTest, 2 results instead of max 1

Question asked by tc1 on Sep 1, 2014
Latest reply on Sep 2, 2014 by martin.grofcik
Hello,

I'm having an issue using the HistoryServiceTest class from the book. When I run this class, I get this error of the JUnit tests:

JUnit test error:

org.activiti.engine.ActivitiException: Query return 2 results instead of max 1
   at org.activiti.engine.impl.AbstractQuery.executeSingleResult(AbstractQuery.java:162)
   at org.activiti.engine.impl.AbstractQuery.execute(AbstractQuery.java:141)
   at org.activiti.engine.impl.interceptor.CommandInvoker.execute(CommandInvoker.java:24)
   at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:57)
   at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:31)
   at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:40)
   at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:35)
   at org.activiti.engine.impl.AbstractQuery.singleResult(AbstractQuery.java:104)
   at BookOrderHistoryTest.startAndComplete(BookOrderHistoryTest.java:33)
   at BookOrderHistoryTest.queryHistoricInstances(BookOrderHistoryTest.java:44)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
   at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
   at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
   at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
   at org.activiti.engine.test.ActivitiRule$1.evaluate(ActivitiRule.java:126)
   at org.junit.rules.RunRules.evaluate(RunRules.java:20)
   at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
   at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
   at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
   at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
   at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
   at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
   at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
   at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
   at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
   at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
   at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
   at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


Does any one have an idea?

Here is the XML and the HistoryServiceTest:

bookorder.bpmn20.xml

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.bpmnwithactiviti.org">
  <process id="bookorder" name="Order book" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <sequenceFlow id="sequenceflow1" name="Validate order" sourceRef="startevent1" targetRef="scripttask1"></sequenceFlow>
    <scriptTask id="scripttask1" name="Validate order" scriptFormat="groovy" activiti:autoStoreVariables="false">
      <script>
            out:println "validating order for isbn " + isbn;
         </script>
    </scriptTask>
    <sequenceFlow id="sequenceflow2" name="Sending to sales" sourceRef="scripttask1" targetRef="usertask1"></sequenceFlow>
    <userTask id="usertask1" name="Work on order" activiti:candidateGroups="Sales">
      <documentation>book order user task</documentation>
    </userTask>
    <sequenceFlow id="sequenceflow3" name="Ending process" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
    <endEvent id="endevent1" name="End"></endEvent>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_bookorder">
    <bpmndi:BPMNPlane bpmnElement="bookorder" id="BPMNPlane_bookorder">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="30.0" y="74.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="scripttask1" id="BPMNShape_scripttask1">
        <omgdc:Bounds height="60.0" width="100.0" x="190.0" y="49.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="60.0" width="100.0" x="420.0" y="49.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="710.0" y="74.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sequenceflow1" id="BPMNEdge_sequenceflow1">
        <omgdi:waypoint x="65.0" y="91.0"></omgdi:waypoint>
        <omgdi:waypoint x="190.0" y="79.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="68.0" x="91.0" y="65.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sequenceflow2" id="BPMNEdge_sequenceflow2">
        <omgdi:waypoint x="290.0" y="79.0"></omgdi:waypoint>
        <omgdi:waypoint x="262.0" y="78.0"></omgdi:waypoint>
        <omgdi:waypoint x="420.0" y="79.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="80.0" x="300.0" y="79.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sequenceflow3" id="BPMNEdge_sequenceflow3">
        <omgdi:waypoint x="520.0" y="79.0"></omgdi:waypoint>
        <omgdi:waypoint x="710.0" y="91.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="75.0" x="600.0" y="89.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>


HistoryServiceTest.class

package org.bpmnwithactiviti.chapter4.api;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricDetail;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricVariableUpdate;
import org.activiti.engine.task.Task;
import org.activiti.engine.test.ActivitiRule;
import org.activiti.engine.test.Deployment;
import org.bpmnwithactiviti.common.AbstractTest;
import org.junit.Rule;
import org.junit.Test;

public class HistoryServiceTest extends AbstractTest {
   
   @Rule
   public ActivitiRule activitiRule = new ActivitiRule("activiti.cfg-mem-fullhistory.xml");
   
   private String startAndComplete() {
      RuntimeService runtimeService = activitiRule.getRuntimeService();
      
      Map<String, Object> variableMap = new HashMap<String, Object>();
      variableMap.put("isbn", "123456");
      String processInstanceID = runtimeService.startProcessInstanceByKey("bookorder", variableMap).getId();
      
      TaskService taskService = activitiRule.getTaskService();
      Task task = taskService.createTaskQuery().taskCandidateGroup("sales").singleResult();
      variableMap = new HashMap<String, Object>();
      variableMap.put("extraInfo", "Extra information");
      variableMap.put("isbn", "654321");
      taskService.complete(task.getId(), variableMap);
      return processInstanceID;
   }
   
   @Test
   @Deployment(resources={"chapter4/bookorder.bpmn20.xml"})
   public void queryHistoricInstances() {
      String processInstanceID = startAndComplete();
      HistoryService historyService = activitiRule.getHistoryService();
      HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
         .processInstanceId(processInstanceID)
         .singleResult();
      assertNotNull(historicProcessInstance);
      assertEquals(processInstanceID, historicProcessInstance.getId());
      System.out.println("history process with definition id " + historicProcessInstance.getProcessDefinitionId() +
            ", started at " + historicProcessInstance.getStartTime() +
            ", ended at " + historicProcessInstance.getEndTime() +
            ", duration was " + historicProcessInstance.getDurationInMillis());
   }
   
   @Test
   @Deployment(resources={"chapter4/bookorder.bpmn20.xml"})
   public void queryHistoricActivities() {
      startAndComplete();
      HistoryService historyService = activitiRule.getHistoryService();
      List<HistoricActivityInstance> activityList = historyService.createHistoricActivityInstanceQuery().list();
      assertEquals(3, activityList.size());
      for (HistoricActivityInstance historicActivityInstance : activityList) {
         assertNotNull(historicActivityInstance.getActivityId());
         System.out.println("history activity " + historicActivityInstance.getActivityName() +
               ", type " + historicActivityInstance.getActivityType() +
               ", starttime " + historicActivityInstance.getStartTime() +
               ", endtime " + historicActivityInstance.getEndTime() +
               ", duration was " + historicActivityInstance.getDurationInMillis());
      }
   }

   @Test
   @Deployment(resources={"chapter4/bookorder.bpmn20.xml"})
   public void queryHistoricVariableUpdates() {
      startAndComplete();
      HistoryService historyService = activitiRule.getHistoryService();
      List<HistoricDetail> historicVariableUpdateList = historyService.createHistoricDetailQuery().variableUpdates().list();
      assertNotNull(historicVariableUpdateList);
      assertEquals(3, historicVariableUpdateList.size());
      for (HistoricDetail historicDetail : historicVariableUpdateList) {
         assertTrue(historicDetail instanceof HistoricVariableUpdate);
         HistoricVariableUpdate historicVariableUpdate = (HistoricVariableUpdate) historicDetail;
         assertNotNull(historicVariableUpdate.getExecutionId());
         System.out.println("historic variable update, revision " + historicVariableUpdate.getRevision() +
               ", variable type name " + historicVariableUpdate.getVariableTypeName() +
               ", variable name " + historicVariableUpdate.getVariableName() +
               ", Variable value '" + historicVariableUpdate.getValue()+"'");
      }
   }
}


AbstractTest class

package org.bpmnwithactiviti.common;

import java.util.logging.Handler;
import java.util.logging.LogManager;
import java.util.logging.Logger;

//import org.activiti.engine.impl.util.LogUtil;
import org.junit.BeforeClass;
import org.slf4j.*;
import org.slf4j.bridge.SLF4JBridgeHandler;

public abstract class AbstractTest {
   
   @BeforeClass
   public static void routeLoggingToSlf4j() {
   //   LogUtil.readJavaUtilLoggingConfigFromClasspath();
      Logger rootLogger = LogManager.getLogManager().getLogger(""); 
      Handler[] handlers = rootLogger.getHandlers(); 
      for (int i = 0; i < handlers.length; i++) { 
         rootLogger.removeHandler(handlers); 
      }
      SLF4JBridgeHandler.install();
   }

}


Thanks in advance.

Outcomes