[jbpm-commits] JBoss JBPM SVN: r6519 - in jbpm4/trunk/modules: test-db/src/test/java/org/jbpm/test/history and 1 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Tue Jul 20 05:37:57 EDT 2010


Author: alex.guizar at jboss.com
Date: 2010-07-20 05:37:57 -0400 (Tue, 20 Jul 2010)
New Revision: 6519

Modified:
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/history/model/HistoryProcessInstanceImpl.java
   jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/history/ProcessInstanceHistoryTest.java
   jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/HistoryProcessInstanceQueryTest.java
Log:
fix history process instance duration failure once and for all

Modified: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/history/model/HistoryProcessInstanceImpl.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/history/model/HistoryProcessInstanceImpl.java	2010-07-18 09:39:38 UTC (rev 6518)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/history/model/HistoryProcessInstanceImpl.java	2010-07-20 09:37:57 UTC (rev 6519)
@@ -27,6 +27,7 @@
 import java.util.Set;
 
 import org.jbpm.api.history.HistoryProcessInstance;
+import org.jbpm.internal.log.Log;
 import org.jbpm.pvm.internal.model.ExecutionImpl;
 import org.jbpm.pvm.internal.model.ProcessDefinitionImpl;
 import org.jbpm.pvm.internal.util.Clock;
@@ -82,8 +83,17 @@
   }
 
   public void setEndTime(Date endTime) {
-    this.endTime = endTime;
-    this.duration = endTime.getTime() - startTime.getTime();
+    long difference = endTime.getTime() - startTime.getTime();
+    if (difference >= 0) {
+      this.endTime = endTime;
+      duration = difference;
+    }
+    else {
+      Log.getLog(getClass().getName()).warn("end time " + endTime + " precedes start time "
+        + startTime);
+      this.endTime = startTime;
+      duration = 0L;
+    }
     this.state = "ended";
   }
 

Modified: jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/history/ProcessInstanceHistoryTest.java
===================================================================
--- jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/history/ProcessInstanceHistoryTest.java	2010-07-18 09:39:38 UTC (rev 6518)
+++ jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/history/ProcessInstanceHistoryTest.java	2010-07-20 09:37:57 UTC (rev 6519)
@@ -28,50 +28,42 @@
 import org.jbpm.api.history.HistoryProcessInstanceQuery;
 import org.jbpm.test.JbpmTestCase;
 
-
 /**
  * @author Tom Baeyens
  */
 public class ProcessInstanceHistoryTest extends JbpmTestCase {
 
   public void testProcessInstanceHistory() {
-    deployJpdlXmlString(
-      "<process name='Insurance claim' key='ICL'>" +
-      "  <start>" +
-      "    <transition to='end' />" +
-      "  </start>" +
-      "  <end name='end' />" +
-      "</process>"
-    );
+    deployJpdlXmlString("<process name='Insurance claim' key='ICL'>"
+      + "  <start>"
+      + "    <transition to='end' />"
+      + "  </start>"
+      + "  <end name='end' />"
+      + "</process>");
 
     executionService.startProcessInstanceByKey("ICL");
     executionService.startProcessInstanceByKey("ICL");
     executionService.startProcessInstanceByKey("ICL");
-    
-    List<HistoryProcessInstance> historyProcessInstances = historyService
-      .createHistoryProcessInstanceQuery()
+
+    List<HistoryProcessInstance> historyProcessInstances = historyService.createHistoryProcessInstanceQuery()
       .processDefinitionId("ICL-1")
       .orderAsc(HistoryProcessInstanceQuery.PROPERTY_STARTTIME)
       .list();
-
     assertEquals(3, historyProcessInstances.size());
-    
-    for (HistoryProcessInstance historyProcessInstance: historyProcessInstances) {
+
+    for (HistoryProcessInstance historyProcessInstance : historyProcessInstances) {
       assertTrue(historyProcessInstance.getProcessInstanceId().startsWith("ICL"));
       assertEquals(HistoryProcessInstance.STATE_ENDED, historyProcessInstance.getState());
       assertNotNull(historyProcessInstance.getStartTime());
       assertNotNull(historyProcessInstance.getEndTime());
-      assertTrue("hpi.duration should be bigger then 0: "+historyProcessInstance.getDuration(), historyProcessInstance.getDuration()>=0);
+      assertTrue("expected non-negative number: " + historyProcessInstance.getDuration(),
+        historyProcessInstance.getDuration() >= 0);
     }
-    
-    // also check that the ended process instances have been removed from the 
-    // runtime database
-    List<ProcessInstance> processInstances = executionService
-      .createProcessInstanceQuery()
+
+    // verify that the ended process instances have been removed from the database
+    List<ProcessInstance> processInstances = executionService.createProcessInstanceQuery()
       .processDefinitionId("ICL-1")
       .list();
-
     assertEquals(0, processInstances.size());
   }
-  
 }

Modified: jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/HistoryProcessInstanceQueryTest.java
===================================================================
--- jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/HistoryProcessInstanceQueryTest.java	2010-07-18 09:39:38 UTC (rev 6518)
+++ jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/HistoryProcessInstanceQueryTest.java	2010-07-20 09:37:57 UTC (rev 6519)
@@ -36,223 +36,255 @@
 import org.jbpm.test.JbpmTestCase;
 import org.jbpm.test.assertion.QueryAssertions;
 
-
 /**
  * @author Joram Barrez
  * @author Alejandro Guizar
  */
 public class HistoryProcessInstanceQueryTest extends JbpmTestCase {
-  
+
   public void testQueryByProcessInstanceId() {
     List<String> procInstIds = createTestHistoryProcessInstances(4);
     for (String processInstanceId : procInstIds) {
-      assertNotNull(historyService.createHistoryProcessInstanceQuery().processInstanceId(processInstanceId).uniqueResult());
+      assertNotNull(historyService.createHistoryProcessInstanceQuery()
+        .processInstanceId(processInstanceId)
+        .uniqueResult());
     }
   }
-  
+
   public void testQueryByProcessDefinitionId() {
     createTestHistoryProcessInstances(5);
-    ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().uniqueResult();
+    ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
+      .uniqueResult();
     assertEquals(5, historyService.createHistoryProcessInstanceQuery()
-            .processDefinitionId(processDefinition.getId()).list().size());  
+      .processDefinitionId(processDefinition.getId())
+      .list()
+      .size());
   }
-  
+
   public void testQueryByBusinessKey() {
     final int nrOfInstances = 3;
     createTestHistoryProcessInstances(nrOfInstances);
+
     for (int i = 0; i < nrOfInstances; i++) {
-      assertNotNull(historyService.createHistoryProcessInstanceQuery().processInstanceKey("theProcess-" + i).uniqueResult());
+      assertNotNull(historyService.createHistoryProcessInstanceQuery()
+        .processInstanceKey("theProcess-" + i)
+        .uniqueResult());
     }
   }
-  
+
   public void testCount() {
     List<String> procInstIds = createTestHistoryProcessInstances(7);
-    
-    assertEquals(0, historyService.createHistoryProcessInstanceQuery().processDefinitionId("-1").count());
-    
+
+    assertEquals(0, historyService.createHistoryProcessInstanceQuery()
+      .processDefinitionId("-1")
+      .count());
     assertEquals(7, historyService.createHistoryProcessInstanceQuery().count());
-    
+
     for (String id : procInstIds) {
-      assertEquals(1, historyService.createHistoryProcessInstanceQuery().processInstanceId(id).count());      
+      assertEquals(1, historyService.createHistoryProcessInstanceQuery()
+        .processInstanceId(id)
+        .count());
     }
-    
   }
-  
+
   public void testOrderByStartTime() {
     testOrderByNaturalOrdening(HistoryProcessInstanceQuery.PROPERTY_STARTTIME, 4);
   }
-  
+
   public void testOrderByEndTime() {
     testOrderByNaturalOrdening(HistoryProcessInstanceQuery.PROPERTY_ENDTIME, 4);
   }
-  
+
   public void testOrderByDuration() {
     testOrderByNaturalOrdening(HistoryProcessInstanceQuery.PROPERTY_DURATION, 4);
   }
-  
+
   public void testQueryEnded() {
     List<String> procInstIds = createTestHistoryProcessInstances(4);
     String endedProcInstId = procInstIds.get(0);
     executionService.endProcessInstance(endedProcInstId, "ended");
 
     List<HistoryProcessInstance> procInsts = historyService.createHistoryProcessInstanceQuery()
-        .ended()
-        .list();
+      .ended()
+      .list();
     assertEquals(1, procInsts.size());
 
     HistoryProcessInstance endedProcInst = procInsts.get(0);
     assertEquals(endedProcInstId, endedProcInst.getProcessInstanceId());
   }
-  
+
   public void testQueryEndedBefore() {
+    // go back in time two days
+    Calendar calendar = Calendar.getInstance();
+    calendar.add(Calendar.DATE, -2);
+    Date twoDaysAgo = calendar.getTime();
+    Clock.setExplicitTime(twoDaysAgo);
+
+    // the start date of the process instances will be two days before
     List<String> procInstIds = createTestHistoryProcessInstances(4);
-    
-    Calendar calendar = Calendar.getInstance();
-    calendar.add(Calendar.DATE, -1);
+
+    // end the first process instance two days before
+    String endedTwoDaysAgoProcInstId = procInstIds.get(0);
+    executionService.endProcessInstance(endedTwoDaysAgoProcInstId, "ended");
+
+    // go forward one day
+    calendar.add(Calendar.DATE, 1);
     Date yesterday = calendar.getTime();
     Clock.setExplicitTime(yesterday);
-    
-    String endedYesterdayProcInstId = procInstIds.get(0);
+
+    // end the second process instance the day before
+    String endedYesterdayProcInstId = procInstIds.get(1);
     executionService.endProcessInstance(endedYesterdayProcInstId, "ended");
-    
-    calendar.add(Calendar.DATE, -1);
-    Date twoDaysAgo = calendar.getTime();
-    Clock.setExplicitTime(twoDaysAgo);
-    
-    String endedTwoDaysAgoProcInstId = procInstIds.get(1);
-    executionService.endProcessInstance(endedTwoDaysAgoProcInstId, "ended");
-    
+
+    // restore the current time
+    Clock.setExplicitTime(null);
+
     List<HistoryProcessInstance> procInsts = historyService.createHistoryProcessInstanceQuery()
-        .endedBefore(yesterday)
-        .list();
+      .endedBefore(yesterday)
+      .list();
     assertEquals(1, procInsts.size());
-    
+
     HistoryProcessInstance endedTwoDaysAgoProcInst = procInsts.get(0);
     assertEquals(endedTwoDaysAgoProcInstId, endedTwoDaysAgoProcInst.getProcessInstanceId());
-    
-    Clock.setExplicitTime(null);
   }
 
   public void testQueryEndedAfter() {
+    // go back in time two days
+    Calendar calendar = Calendar.getInstance();
+    calendar.add(Calendar.DATE, -2);
+    Date twoDaysAgo = calendar.getTime();
+    Clock.setExplicitTime(twoDaysAgo);
+
+    // the start date of the process instances will be two days before
     List<String> procInstIds = createTestHistoryProcessInstances(4);
-    
-    Calendar calendar = Calendar.getInstance();
-    calendar.add(Calendar.DATE, -1);
+
+    // end the first process instance two days before
+    String endedTwoDaysAgoProcInstId = procInstIds.get(0);
+    executionService.endProcessInstance(endedTwoDaysAgoProcInstId, "ended");
+
+    // go forward one day
+    calendar.add(Calendar.DATE, 1);
     Date yesterday = calendar.getTime();
     Clock.setExplicitTime(yesterday);
-    
-    String endedYesterdayProcInstId = procInstIds.get(0);
+
+    // end the second process instance one day before
+    String endedYesterdayProcInstId = procInstIds.get(1);
     executionService.endProcessInstance(endedYesterdayProcInstId, "ended");
-    
-    calendar.add(Calendar.DATE, -1);
-    Date twoDaysAgo = calendar.getTime();
-    Clock.setExplicitTime(twoDaysAgo);
-    
-    String endedTwoDaysAgoProcInstId = procInstIds.get(1);
-    executionService.endProcessInstance(endedTwoDaysAgoProcInstId, "ended");
-    
+
+    // restore the current time
+    Clock.setExplicitTime(null);
+
     List<HistoryProcessInstance> procInsts = historyService.createHistoryProcessInstanceQuery()
-        .endedAfter(yesterday)
-        .list();
+      .endedAfter(yesterday)
+      .list();
     assertEquals(1, procInsts.size());
-    
+
     HistoryProcessInstance endedYesterdayProcInst = procInsts.get(0);
     assertEquals(endedYesterdayProcInstId, endedYesterdayProcInst.getProcessInstanceId());
-    
-    Clock.setExplicitTime(null);
   }
 
   public void testQueryEndedAfterAndBefore() {
+    // go back in time two days
+    Calendar calendar = Calendar.getInstance();
+    calendar.add(Calendar.DATE, -2);
+    Date twoDaysAgo = calendar.getTime();
+    Clock.setExplicitTime(twoDaysAgo);
+
+    // the start date of the process instances will be two days before
     List<String> procInstIds = createTestHistoryProcessInstances(4);
-    
-    String endedTodayProcInstId = procInstIds.get(0);
-    executionService.endProcessInstance(endedTodayProcInstId, "ended");
-    
-    Calendar calendar = Calendar.getInstance();
-    calendar.add(Calendar.DATE, -1);
+
+    // end the first process instance two days before
+    String endedTwoDaysAgoProcInstId = procInstIds.get(0);
+    executionService.endProcessInstance(endedTwoDaysAgoProcInstId, "ended");
+
+    // go forward one day
+    calendar.add(Calendar.DATE, 1);
     Date yesterday = calendar.getTime();
     Clock.setExplicitTime(yesterday);
     
+    // end the second process instance one day before
     String endedYesterdayProcInstId = procInstIds.get(1);
     executionService.endProcessInstance(endedYesterdayProcInstId, "ended");
-    
-    calendar.add(Calendar.DATE, -1);
-    Date twoDaysAgo = calendar.getTime();
-    Clock.setExplicitTime(twoDaysAgo);
-    
-    String endedTwoDaysAgoProcInstId = procInstIds.get(2);
-    executionService.endProcessInstance(endedTwoDaysAgoProcInstId, "ended");
-    
-    calendar.add(Calendar.DATE, 3);
+
+    // come back to the current time
+    Clock.setExplicitTime(null);
+
+    // end the third process instance the current day
+    String endedTodayProcInstId = procInstIds.get(2);
+    executionService.endProcessInstance(endedTodayProcInstId, "ended");
+
+    calendar.add(Calendar.DATE, 2);
     Date tomorrow = calendar.getTime();
-    
     List<HistoryProcessInstance> procInsts = historyService.createHistoryProcessInstanceQuery()
-        .endedAfter(yesterday)
-        .endedBefore(tomorrow)
-        .list();
+      .endedAfter(yesterday)
+      .endedBefore(tomorrow)
+      .list();
     assertEquals(2, procInsts.size());
 
     for (HistoryProcessInstance procInst : procInsts) {
       String procInstId = procInst.getProcessInstanceId();
-      assert procInstId.equals(endedYesterdayProcInstId) ||
-          procInstId.equals(endedTodayProcInstId) : procInstId;
+      assert procInstId.equals(endedYesterdayProcInstId)
+        || procInstId.equals(endedTodayProcInstId) : procInstId;
     }
-
-    Clock.setExplicitTime(null);
   }
 
-  /* -------------------------------------------------------------
-   * HELPER METHODS
-   * ------------------------------------------------------------- */
+  /*
+   * ------------------------------------------------------------- HELPER METHODS
+   * -------------------------------------------------------------
+   */
 
   // Don't delete because it isn't used. Could be handy in the future!
   private void testOrderBy(String property, List<Object> expectedValues) {
     createTestHistoryProcessInstances(4);
-    
-    List<HistoryProcessInstance> histProcListAsc = 
-        historyService.createHistoryProcessInstanceQuery()
-                      .orderAsc(property)
-                      .list();
-    
-    List<HistoryProcessInstance> histProcListDesc = 
-      historyService.createHistoryProcessInstanceQuery()
-                    .orderDesc(property)
-                    .list();
 
-    QueryAssertions.assertOrderOnProperty(HistoryProcessInstance.class, property, histProcListAsc, histProcListDesc, expectedValues);
+    List<HistoryProcessInstance> histProcListAsc = historyService.createHistoryProcessInstanceQuery()
+      .orderAsc(property)
+      .list();
+
+    List<HistoryProcessInstance> histProcListDesc = historyService.createHistoryProcessInstanceQuery()
+      .orderDesc(property)
+      .list();
+
+    QueryAssertions.assertOrderOnProperty(HistoryProcessInstance.class,
+      property,
+      histProcListAsc,
+      histProcListDesc,
+      expectedValues);
   }
 
   private void testOrderByNaturalOrdening(String property, int expectedNrOfResults) {
     createTestHistoryProcessInstances(4);
-    
-    List<HistoryProcessInstance> histProcListAsc = 
-        historyService.createHistoryProcessInstanceQuery()
-                      .orderAsc(property)
-                      .list();
-    
-    List<HistoryProcessInstance> histProcListDesc = 
-      historyService.createHistoryProcessInstanceQuery()
-                    .orderDesc(property)
-                    .list();
-    
-    QueryAssertions.assertOrderIsNatural(HistoryProcessInstance.class, property, histProcListAsc, histProcListDesc, expectedNrOfResults);
+
+    List<HistoryProcessInstance> histProcListAsc = historyService.createHistoryProcessInstanceQuery()
+      .orderAsc(property)
+      .list();
+
+    List<HistoryProcessInstance> histProcListDesc = historyService.createHistoryProcessInstanceQuery()
+      .orderDesc(property)
+      .list();
+
+    QueryAssertions.assertOrderIsNatural(HistoryProcessInstance.class,
+      property,
+      histProcListAsc,
+      histProcListDesc,
+      expectedNrOfResults);
   }
-  
+
   private List<String> createTestHistoryProcessInstances(int nrOfInstances) {
-    deployJpdlXmlString(
-          "<process name='theProcess'>" +
-          "  <start>" +
-          "    <transition to='wait' />" +
-          "  </start>" +
-          "  <state name='wait'>" +
-          "    <transition to='end' />" +
-          "  </state>" +
-          "  <end name='end' />" +
-          "</process>");
-    
+    deployJpdlXmlString("<process name='theProcess'>"
+      + "  <start>"
+      + "    <transition to='wait' />"
+      + "  </start>"
+      + "  <state name='wait'>"
+      + "    <transition to='end' />"
+      + "  </state>"
+      + "  <end name='end' />"
+      + "</process>");
+
     List<String> procInstIds = new ArrayList<String>();
     for (int i = 0; i < nrOfInstances; i++) {
-      procInstIds.add(executionService.startProcessInstanceByKey("theProcess", "theProcess-" + i).getId());
+      procInstIds.add(executionService.startProcessInstanceByKey("theProcess",
+        "theProcess-" + i).getId());
     }
     return procInstIds;
   }



More information about the jbpm-commits mailing list