[jbpm-commits] JBoss JBPM SVN: r6017 - in jbpm4/trunk/modules: api/src/main/java/org/jbpm/api/history and 3 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Tue Dec 22 08:00:19 EST 2009


Author: jbarrez
Date: 2009-12-22 08:00:18 -0500 (Tue, 22 Dec 2009)
New Revision: 6017

Added:
   jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/ProcessInstanceQueryTest.java
Modified:
   jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/DeploymentQuery.java
   jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/JobQuery.java
   jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/ProcessDefinitionQuery.java
   jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/ProcessInstanceQuery.java
   jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/TaskQuery.java
   jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/history/HistoryActivityInstanceQuery.java
   jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/history/HistoryProcessInstanceQuery.java
   jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/history/HistoryTaskQuery.java
   jbpm4/trunk/modules/distro/src/main/files/readme.html
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/AbstractQuery.java
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/DeploymentQueryImpl.java
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/HistoryActivityInstanceQueryImpl.java
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/HistoryProcessInstanceQueryImpl.java
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/HistoryTaskQueryImpl.java
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/JobQueryImpl.java
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/ProcessInstanceQueryImpl.java
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/TaskQueryImpl.java
   jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/DeploymentQueryTest.java
   jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/HistoryActivityInstanceQueryTest.java
   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/HistoryTaskQueryTest.java
   jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/JobQueryTest.java
   jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/TaskQueryTest.java
Log:
JBPM-2567: allow count() on all query types

Modified: jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/DeploymentQuery.java
===================================================================
--- jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/DeploymentQuery.java	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/DeploymentQuery.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -29,15 +29,11 @@
  */
 public interface DeploymentQuery {
   
-  /** sorting by name does not work on oracle.  that's why it is deprecated.
-   * will be removed in jBPM 4.3 */
-  @Deprecated
-  String PROPERTY_NAME = "name";
   String PROPERTY_TIMESTAMP = "timestamp";
   String PROPERTY_STATE = "state";
 
   /** only include a specific deployment by id */
-  DeploymentQuery deploymentDbid(long dbid);
+  DeploymentQuery deploymentId(String id);
   
   /** only select suspended deployments */
   DeploymentQuery suspended();
@@ -50,5 +46,9 @@
   DeploymentQuery page(int firstResult, int maxResults);
   
   List<Deployment> list();
+  
   Deployment uniqueResult();
+  
+  /** Returns the count for this query. */
+  Long count();
 }

Modified: jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/JobQuery.java
===================================================================
--- jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/JobQuery.java	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/JobQuery.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -69,4 +69,8 @@
 
   /** execute the query and get the unique result */ 
   Job uniqueResult();
+  
+  /** Returns the count for the query. */
+  Long count();
+  
 }

Modified: jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/ProcessDefinitionQuery.java
===================================================================
--- jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/ProcessDefinitionQuery.java	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/ProcessDefinitionQuery.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -76,4 +76,5 @@
 
   /** execute the query and obtain the unique {@link ProcessDefinition} */
   ProcessDefinition uniqueResult();
+  
 }

Modified: jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/ProcessInstanceQuery.java
===================================================================
--- jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/ProcessInstanceQuery.java	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/ProcessInstanceQuery.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -62,4 +62,8 @@
 
   /** execute the query and obtain the unique {@link ProcessInstance} */
   ProcessInstance uniqueResult();
+  
+  /** execute the query and return the count */
+  Long count();
+  
 }

Modified: jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/TaskQuery.java
===================================================================
--- jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/TaskQuery.java	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/TaskQuery.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -83,6 +83,9 @@
   /** order selected tasks descending for certain {@link #PROPERTY_NAME properties} */
   TaskQuery orderDesc(String property);
   
+  /** does a count(*) on the given query. */
+  Long count();
+  
   /** execute the query and obtain the list of {@link Task}s */
   List<Task> list();
 

Modified: jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/history/HistoryActivityInstanceQuery.java
===================================================================
--- jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/history/HistoryActivityInstanceQuery.java	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/history/HistoryActivityInstanceQuery.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -78,4 +78,7 @@
 
   /** execute the query and obtain the unique {@link HistoryActivityInstance} */
   HistoryActivityInstance uniqueResult();
+  
+  /** Returns the count of this query. */
+  Long count();
 }

Modified: jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/history/HistoryProcessInstanceQuery.java
===================================================================
--- jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/history/HistoryProcessInstanceQuery.java	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/history/HistoryProcessInstanceQuery.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -69,4 +69,8 @@
 
   /** execute the query and obtain the unique {@link HistoryProcessInstance} */
   HistoryProcessInstance uniqueResult();
+  
+  /** Return the count of the query. */
+  Long count();
+  
 }

Modified: jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/history/HistoryTaskQuery.java
===================================================================
--- jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/history/HistoryTaskQuery.java	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/history/HistoryTaskQuery.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -89,4 +89,8 @@
   
   /** execute the query and obtain the unique {@link HistoryTask} */
   HistoryTask uniqueResult();
+  
+  /** Return the count for this query. */
+  Long count();
+  
 }

Modified: jbpm4/trunk/modules/distro/src/main/files/readme.html
===================================================================
--- jbpm4/trunk/modules/distro/src/main/files/readme.html	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/distro/src/main/files/readme.html	2009-12-22 13:00:18 UTC (rev 6017)
@@ -35,7 +35,21 @@
       </tr>
     </table>
     
+<h1>Release Notes for jBPM 4.3</h1>
+
+<h2>API changes</h2>
+
+<ul>
+  <li>
+    DeploymentQuery.PROPERTY_NAME has been removed due to errors when sorting on name on Oracle.
+  </li>
+  <li>
+    DeploymentQuery.deploymentDbId(long dbid) has been changed to DeploymentQuery.deploymentId(String id) 
+    to be conistent with the query API.
+  </li>
+</ul>
     
+    
 <h1>Release Notes for jBPM 4.2</h1>
 
 <h2>Known limitations</h2>

Modified: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/AbstractQuery.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/AbstractQuery.java	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/AbstractQuery.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -49,6 +49,7 @@
   protected String orderByClause = null;
   protected Page page = null;
   protected boolean isWhereAdded = false;
+  protected boolean count = false;
   
   protected abstract void applyParameters(Query query);
 
@@ -112,10 +113,28 @@
   }
   
   /**
+   * Returns the count of the query.
+   * Query types that want to use this count method should
+   *   - add the operation signature to their interface
+   *   - use the 'count' variable in their hql() method.
+   */
+  public Long count() {
+    count = true;
+    
+    // Page and count cannot be used together, because paging is applied after the query is formed
+    if (page != null) {
+      throw new JbpmException("page(firstResult, maxResult) and count() cannot be used together");
+    }
+    
+    return (Long) untypedUniqueResult();
+  }
+  
+  /**
    * Resets the query so it can be reused after an invocation.
    */
   private void resetQuery() {
     isWhereAdded = false;
+    count = false;
   }
 
   protected void appendWhereClause(String whereClause, StringBuilder hql) {

Modified: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/DeploymentQueryImpl.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/DeploymentQueryImpl.java	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/DeploymentQueryImpl.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -34,18 +34,25 @@
  */
 public class DeploymentQueryImpl extends AbstractQuery implements DeploymentQuery {
   
-  protected Long deploymentDbid = null;
+  protected String deploymentId = null;
   protected Boolean suspended = null;
 
   public String hql() {
     StringBuilder hql = new StringBuilder();
-    hql.append("select d ");
+    
+    hql.append("select ");
+    if (count) {
+      hql.append("count(d) ");
+    } else {
+      hql.append("d ");
+    }
+
     hql.append("from ");
     hql.append(DeploymentImpl.class.getName());
     hql.append(" as d ");
     
-    if (deploymentDbid!=null) {
-      appendWhereClause("d.dbid = "+deploymentDbid+" ", hql);
+    if (deploymentId!=null) {
+      appendWhereClause("d.dbid = "+deploymentId+" ", hql);
     }
 
     if (suspended!=null) {
@@ -64,8 +71,8 @@
   protected void applyParameters(Query query) {
   }
 
-  public DeploymentQuery deploymentDbid(long deploymentDbid) {
-    this.deploymentDbid = deploymentDbid;
+  public DeploymentQuery deploymentId(String deploymentId) {
+    this.deploymentId = deploymentId;
     return this;
   }
 

Modified: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/HistoryActivityInstanceQueryImpl.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/HistoryActivityInstanceQueryImpl.java	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/HistoryActivityInstanceQueryImpl.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -48,7 +48,14 @@
 
   public String hql() {
   	StringBuilder hql = new StringBuilder();
-    hql.append("select hai ");
+  	
+  	hql.append("select ");
+  	if (count) {
+  	  hql.append("count(hai) ");
+  	} else {
+  	  hql.append("hai ");
+  	}
+  	
     hql.append("from ");
     hql.append(HistoryActivityInstanceImpl.class.getName());
     hql.append(" as hai ");

Modified: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/HistoryProcessInstanceQueryImpl.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/HistoryProcessInstanceQueryImpl.java	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/HistoryProcessInstanceQueryImpl.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -42,7 +42,14 @@
 
   public String hql() {
   	StringBuilder hql = new StringBuilder();
-    hql.append("select hpi ");
+  	
+  	hql.append("select ");
+  	if (count) {
+  	  hql.append("count(hpi) ");
+  	} else {
+  	  hql.append("hpi ");
+  	}
+
     hql.append("from ");
     hql.append(HistoryProcessInstanceImpl.class.getName());
     hql.append(" as hpi ");

Modified: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/HistoryTaskQueryImpl.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/HistoryTaskQueryImpl.java	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/HistoryTaskQueryImpl.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -28,6 +28,7 @@
 import org.jbpm.api.history.HistoryTask;
 import org.jbpm.api.history.HistoryTaskQuery;
 import org.jbpm.pvm.internal.history.model.HistoryTaskImpl;
+import org.jbpm.pvm.internal.util.Clock;
 
 
 /**
@@ -47,7 +48,14 @@
 
   public String hql() {
     StringBuilder hql = new StringBuilder();
-    hql.append("select ht ");
+    
+    hql.append("select ");
+    if (count) {
+      hql.append("count(ht) ");
+    } else {
+      hql.append("ht ");
+    }
+    
     hql.append("from ");
     hql.append(HistoryTaskImpl.class.getName());
     hql.append(" as ht ");
@@ -79,13 +87,13 @@
     if (tookLongerThen!=null) {
       appendWhereClause(" ht.duration > :tookLongerThen ", hql);
     }
-    
+
     if (startedBefore!=null) {
-      appendWhereClause(" ht.startTime < :startedBefore ", hql);
+      appendWhereClause(" ht.createTime < :startedBefore ", hql);
     }
     
     if (startedAfter!=null) {
-      appendWhereClause(" ht.startTime > :startedAfter ", hql);
+      appendWhereClause(" ht.createTime > :startedAfter ", hql);
     }
     
     appendOrderByClause(hql);
@@ -103,11 +111,11 @@
     }
     
     if (startedBefore!=null) {
-      query.setTime("startedBefore", startedBefore);
+      query.setTimestamp("startedBefore", startedBefore);
     }
     
     if (startedAfter!=null) {
-      query.setTime("startedAfter", startedAfter);
+      query.setTimestamp("startedAfter", startedAfter);
     }
   }
 

Modified: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/JobQueryImpl.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/JobQueryImpl.java	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/JobQueryImpl.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -45,7 +45,14 @@
 
   public String hql() {
   	StringBuilder hql = new StringBuilder();
-    hql.append("select j ");
+  	
+  	hql.append("select ");
+  	if (count) {
+  	  hql.append("count(j) ");
+  	} else {
+  	  hql.append("j ");
+  	}
+
     hql.append("from ");
     if (messagesOnly) {
       hql.append(MessageImpl.class.getName());

Modified: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/ProcessInstanceQueryImpl.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/ProcessInstanceQueryImpl.java	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/ProcessInstanceQueryImpl.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -50,7 +50,14 @@
 
   public String hql() {
   	StringBuilder hql = new StringBuilder();
-    hql.append("select processInstance ");
+  	
+  	hql.append("select ");
+  	if (count) {
+  	  hql.append("count(processInstance) ");  	  
+  	} else {
+  	  hql.append("processInstance ");
+  	}
+  	
     hql.append("from ");
     hql.append(ExecutionImpl.class.getName());
     hql.append(" as processInstance ");

Modified: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/TaskQueryImpl.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/TaskQueryImpl.java	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/query/TaskQueryImpl.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -136,7 +136,14 @@
 
   public String hql() {
   	StringBuilder hql = new StringBuilder();
-    hql.append("select task ");
+    hql.append("select ");
+    
+    if (count) {
+      hql.append("count(task) ");
+    } else {
+      hql.append("task ");
+    }
+    
     hql.append("from ");
     hql.append(TaskImpl.class.getName());
     hql.append(" as task ");

Modified: jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/DeploymentQueryTest.java
===================================================================
--- jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/DeploymentQueryTest.java	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/DeploymentQueryTest.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -22,7 +22,6 @@
 package org.jbpm.test.query;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -35,6 +34,7 @@
 
 /**
  * @author Tom Baeyens
+ * @author Joram Barrez
  */
 public class DeploymentQueryTest extends JbpmTestCase {
 
@@ -84,6 +84,39 @@
     deleteCascade(deploymentIds);
   }
 
+  
+  
+  private void deleteCascade(List<String> deploymentIds) {
+    for (String deploymentId : deploymentIds) {
+      repositoryService.deleteDeploymentCascade(deploymentId);
+    }
+  }
+  
+  public void testOrderByTimeStamp() {
+    testOrderByNaturalOrdening(DeploymentQuery.PROPERTY_TIMESTAMP, 3);
+  }
+  
+  public void testCount() {
+    List<String> deploymentIds = deployTestProcesses();
+    
+    assertEquals(new Long(3L), repositoryService.createDeploymentQuery().count());
+    for (String deploymentId : deploymentIds) {
+      assertEquals(new Long(1L), repositoryService.createDeploymentQuery().deploymentId(deploymentId).count());      
+    }
+    
+    Deployment deployment = repositoryService.createDeploymentQuery().deploymentId(deploymentIds.get(0)).uniqueResult();
+    repositoryService.suspendDeployment(deployment.getId());
+    
+    assertEquals(new Long(1L), repositoryService.createDeploymentQuery().suspended().count());
+    assertEquals(new Long(2L), repositoryService.createDeploymentQuery().notSuspended().count());
+    
+    deleteCascade(deploymentIds);
+  }
+  
+  /* --------------
+   * HELPER METHODS
+   * -------------- */
+  
   private Set<String> getDeploymentNames(List<Deployment> deployments) {
     Set<String> deploymentNames = new HashSet<String>();
     for (Deployment deployment: deployments) {
@@ -141,16 +174,6 @@
     return processIds;
   }
   
-  private void deleteCascade(List<String> deploymentIds) {
-    for (String deploymentId : deploymentIds) {
-      repositoryService.deleteDeploymentCascade(deploymentId);
-    }
-  }
-  
-  public void testOrderByTimeStamp() {
-    testOrderByNaturalOrdening(DeploymentQuery.PROPERTY_TIMESTAMP, 3);
-  }
-  
   private void testOrderBy(String property, List expectedValues) {
     testOrderBy(property, expectedValues, null, false);
   }

Modified: jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/HistoryActivityInstanceQueryTest.java
===================================================================
--- jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/HistoryActivityInstanceQueryTest.java	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/HistoryActivityInstanceQueryTest.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -172,6 +172,28 @@
     testOrderByNaturalOrdening(HistoryActivityInstanceQuery.PROPERTY_EXECUTIONID, 9);
   }
   
+  public void testCount() {
+    List<String> procInstIds = deployStartAndSignalTestProcesses();
+    
+    assertEquals(new Long(9L), historyService.createHistoryActivityInstanceQuery().count());
+    assertEquals(new Long(4L), historyService.createHistoryActivityInstanceQuery().activityName("a").count());
+    assertEquals(new Long(3L), historyService.createHistoryActivityInstanceQuery().activityName("b").count());
+    assertEquals(new Long(2L), historyService.createHistoryActivityInstanceQuery().activityName("c").count());
+    assertEquals(new Long(0L), historyService.createHistoryActivityInstanceQuery().activityName("d").count());
+    
+    assertEquals(new Long(3L), historyService.createHistoryActivityInstanceQuery().executionId(procInstIds.get(0)).count());
+    assertEquals(new Long(3L), historyService.createHistoryActivityInstanceQuery().executionId(procInstIds.get(1)).count());
+    assertEquals(new Long(2L), historyService.createHistoryActivityInstanceQuery().executionId(procInstIds.get(2)).count());
+    assertEquals(new Long(1L), historyService.createHistoryActivityInstanceQuery().executionId(procInstIds.get(3)).count());
+    
+    assertEquals(new Long(1L), historyService.createHistoryActivityInstanceQuery()
+                                  .executionId(procInstIds.get(0)).activityName("a").count());
+    assertEquals(new Long(1L), historyService.createHistoryActivityInstanceQuery()
+            .executionId(procInstIds.get(0)).activityName("b").count());
+    assertEquals(new Long(1L), historyService.createHistoryActivityInstanceQuery()
+            .executionId(procInstIds.get(0)).activityName("c").count());
+  }
+  
   /**
    * Generates some history data.
    * 
@@ -180,6 +202,8 @@
    *   - 1 process instance in state c (signalled a,b)
    *   - 1 process instance in state b (signalled a)
    *   - 1 process instance in state a (just started)
+   *   
+   *   (in this exact order!)
    */
   private List<String> deployStartAndSignalTestProcesses() {
     deployTestProcess();

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	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/HistoryProcessInstanceQueryTest.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -24,8 +24,10 @@
  */
 package org.jbpm.test.query;
 
+import java.util.ArrayList;
 import java.util.List;
 
+import org.jbpm.api.ProcessDefinition;
 import org.jbpm.api.history.HistoryProcessInstance;
 import org.jbpm.api.history.HistoryProcessInstanceQuery;
 import org.jbpm.test.JbpmTestCase;
@@ -37,6 +39,41 @@
  */
 public class HistoryProcessInstanceQueryTest extends JbpmTestCase {
   
+  public void testQueryByProcessInstanceId() {
+    List<String> procInstIds = createTestHistoryProcessInstances(4);
+    for (String processInstanceId : procInstIds) {
+      assertNotNull(historyService.createHistoryProcessInstanceQuery().processInstanceId(processInstanceId).uniqueResult());
+    }
+  }
+  
+  public void testQueryByProcessDefinitionId() {
+    createTestHistoryProcessInstances(5);
+    ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().uniqueResult();
+    assertEquals(5, historyService.createHistoryProcessInstanceQuery()
+            .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());
+    }
+  }
+  
+  public void testCount() {
+    List<String> procInstIds = createTestHistoryProcessInstances(7);
+    
+    assertEquals(new Long(0L), historyService.createHistoryProcessInstanceQuery().processDefinitionId("-1").count());
+    
+    assertEquals(new Long(7L), historyService.createHistoryProcessInstanceQuery().count());
+    
+    for (String id : procInstIds) {
+      assertEquals(new Long(1L), historyService.createHistoryProcessInstanceQuery().processInstanceId(id).count());      
+    }
+    
+  }
+  
   public void testOrderByStartTime() {
     testOrderByNaturalOrdening(HistoryProcessInstanceQuery.PROPERTY_STARTTIME, 4);
   }
@@ -56,7 +93,7 @@
   
   // Don't delete because it isn't used. Could be handy in the future!
   private void testOrderBy(String property, List<Object> expectedValues) {
-    createTestHistoryProcessInstances();
+    createTestHistoryProcessInstances(4);
     
     List<HistoryProcessInstance> histProcListAsc = 
         historyService.createHistoryProcessInstanceQuery()
@@ -72,7 +109,7 @@
   }
 
   private void testOrderByNaturalOrdening(String property, int expectedNrOfResults) {
-    createTestHistoryProcessInstances();
+    createTestHistoryProcessInstances(4);
     
     List<HistoryProcessInstance> histProcListAsc = 
         historyService.createHistoryProcessInstanceQuery()
@@ -87,7 +124,7 @@
     QueryAssertions.assertOrderIsNatural(HistoryProcessInstance.class, property, histProcListAsc, histProcListDesc, expectedNrOfResults);
   }
   
-  private void createTestHistoryProcessInstances() {
+  private List<String> createTestHistoryProcessInstances(int nrOfInstances) {
     deployJpdlXmlString(
           "<process name='theProcess'>" +
           "  <start>" +
@@ -96,9 +133,11 @@
           "  <end name='end' />" +
           "</process>");
     
-    for (int i = 0; i < 4; i++) {
-      executionService.startProcessInstanceByKey("theProcess");
+    List<String> procInstIds = new ArrayList<String>();
+    for (int i = 0; i < nrOfInstances; i++) {
+      procInstIds.add(executionService.startProcessInstanceByKey("theProcess", "theProcess-" + i).getId());
     }
+    return procInstIds;
 
   }
 

Modified: jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/HistoryTaskQueryTest.java
===================================================================
--- jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/HistoryTaskQueryTest.java	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/HistoryTaskQueryTest.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -27,12 +27,14 @@
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Date;
 import java.util.List;
 
 import org.jbpm.api.history.HistoryTask;
 import org.jbpm.api.history.HistoryTaskQuery;
+import org.jbpm.pvm.internal.util.Clock;
 import org.jbpm.test.JbpmTestCase;
 import org.jbpm.test.assertion.QueryAssertions;
 
@@ -69,36 +71,6 @@
   public void testOrderByState() {
     testOrderByNaturalOrdening(HistoryTaskQuery.PROPERTY_STATE, 3);
   }
-  
-  private void createTestHistoryTasks() {
-    String processXml1 = 
-      "<process name='theProcess1'>" +
-      "  <start>" +
-      "    <transition to='theTask' />" +
-      "  </start>" +
-      "  <task name='theTask' assignee='Alex'>" +
-      "    <transition to='theEnd' />" +
-      "  </task>" +
-      "  <end name='theEnd' />" +
-      "</process>";
-    
-    String processXml3 = processXml1.replace("1", "2").replace("Alex", "Tom");
-    String processXml2 = processXml1.replace("1", "3").replace("Alex", "Joram");
-   
-    deployJpdlXmlString(processXml1);
-    deployJpdlXmlString(processXml2);
-    deployJpdlXmlString(processXml3);
-    
-    executionService.startProcessInstanceByKey("theProcess1");
-    executionService.startProcessInstanceByKey("theProcess2");
-    executionService.startProcessInstanceByKey("theProcess3");
-    
-    taskService.completeTask(taskService.findPersonalTasks("Alex").get(0).getId());
-    taskService.completeTask(taskService.findPersonalTasks("Joram").get(0).getId());
-    taskService.completeTask(taskService.findPersonalTasks("Tom").get(0).getId());
-    
-    taskService.findPersonalTasks("Alex");
-  }
 
   private void testOrderBy(String property, List expectedValues) {
     testOrderBy(property, expectedValues, null, false);
@@ -108,6 +80,25 @@
     testOrderBy(property, null, expectedNrOfResults, true);
   }
   
+  public void testCount() {
+    List<String> taskIds = createTestHistoryTasks();
+    
+    assertEquals(new Long(3L), historyService.createHistoryTaskQuery().count());
+    assertEquals(new Long(1L), historyService.createHistoryTaskQuery().assignee("Tom").count());
+    
+    for (String taskid : taskIds) {
+      assertEquals(new Long(1L), historyService.createHistoryTaskQuery().taskId(taskid).count());
+    }
+    
+    assertEquals(new Long(0L), historyService.createHistoryTaskQuery().startedBefore(new Date(CLOCK_TEST_TIME - 10000L)).count());
+    assertEquals(new Long(1L), 
+            historyService.createHistoryTaskQuery().startedAfter(new Date(CLOCK_TEST_TIME - 10000L))
+            .taskId(taskIds.get(0)).count());
+    assertEquals(new Long(0L), 
+            historyService.createHistoryTaskQuery().startedAfter(new Date(CLOCK_TEST_TIME - 10000L))
+            .taskId("-1").count());
+  }
+  
   @SuppressWarnings("unchecked")
   private void testOrderBy(String property, List expectedValues, 
           Integer expectedNrOfResults, boolean naturalOrderCheck) {
@@ -128,6 +119,45 @@
     
   }
   
+  private static final Long CLOCK_TEST_TIME = 30000L;
+  
+  private List<String> createTestHistoryTasks() {
+    String processXml1 = 
+      "<process name='theProcess1'>" +
+      "  <start>" +
+      "    <transition to='theTask' />" +
+      "  </start>" +
+      "  <task name='theTask' assignee='Alex'>" +
+      "    <transition to='theEnd' />" +
+      "  </task>" +
+      "  <end name='theEnd' />" +
+      "</process>";
+    
+    Clock.setCurrentTime(new Date(CLOCK_TEST_TIME));
+    
+    String processXml3 = processXml1.replace("1", "2").replace("Alex", "Tom");
+    String processXml2 = processXml1.replace("1", "3").replace("Alex", "Joram");
+   
+    deployJpdlXmlString(processXml1);
+    deployJpdlXmlString(processXml2);
+    deployJpdlXmlString(processXml3);
+    
+    executionService.startProcessInstanceByKey("theProcess1");
+    executionService.startProcessInstanceByKey("theProcess2");
+    executionService.startProcessInstanceByKey("theProcess3");
+    
+    List<String> taskIds = new ArrayList<String>();
+    taskIds.add(taskService.findPersonalTasks("Alex").get(0).getId());
+    taskIds.add(taskService.findPersonalTasks("Joram").get(0).getId());
+    taskIds.add(taskService.findPersonalTasks("Tom").get(0).getId());
+    
+    for (String taskId : taskIds) {
+      taskService.completeTask(taskId);
+    }
+    
+    return taskIds;
+  }
+  
   private Date stringToDate(String dateString) {
     DateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy");
     try {

Modified: jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/JobQueryTest.java
===================================================================
--- jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/JobQueryTest.java	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/JobQueryTest.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -1,6 +1,28 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
 package org.jbpm.test.query;
 
 import java.io.Serializable;
+import java.util.ArrayList;
 import java.util.List;
 
 import org.jbpm.api.JobQuery;
@@ -8,11 +30,14 @@
 import org.jbpm.test.JbpmTestCase;
 import org.jbpm.test.assertion.QueryAssertions;
 
-
+/**
+ * @author Tom Baeyens
+ * @author Joram Barrez
+ */
 public class JobQueryTest extends JbpmTestCase {
   
   public void testQueryOutstandingTimers() {
-   startTestProcessInstances();
+   startTestProcessInstances(4);
     
     List<Job> timers = managementService.createJobQuery()
       .timers()
@@ -43,27 +68,6 @@
     assertEquals(4, jobs.size());
   }
   
-  private void startTestProcessInstances() {
-    deployJpdlXmlString(
-            "<process name='TimerQueryTest' >" +
-            "  <start>" +
-            "    <transition to='t' />" +
-            "  </start>" +
-            "  <state name='t'>" +
-            "    <transition name='timeout' to='t'>" +
-            "      <timer duedate='20 seconds' />" +
-            "    </transition>" +
-            "  </state>" +
-            "</process>"
-          );
-
-    executionService.startProcessInstanceByKey("TimerQueryTest");
-    executionService.startProcessInstanceByKey("TimerQueryTest");
-    executionService.startProcessInstanceByKey("TimerQueryTest");
-    executionService.startProcessInstanceByKey("TimerQueryTest");
-  }
-
-  
   public static class Dog implements Serializable {
     private static final long serialVersionUID = 1L;
     public void bark() {
@@ -120,10 +124,44 @@
   }
   
   public void testOrderByDueDate() {
-    startTestProcessInstances();
+    startTestProcessInstances(4);
     List<Job> jobsAsc = managementService.createJobQuery().orderAsc(JobQuery.PROPERTY_DUEDATE).list();
     List<Job> jobsDesc = managementService.createJobQuery().orderDesc(JobQuery.PROPERTY_DUEDATE).list();
     QueryAssertions.assertOrderIsNatural(Job.class, JobQuery.PROPERTY_DUEDATE, jobsAsc, jobsDesc, 4);
   }
   
+  public void testCount() {
+    List<String> procInstIds = startTestProcessInstances(6);
+    
+    assertEquals(new Long(6L), managementService.createJobQuery().count());
+    assertEquals(new Long(6L), managementService.createJobQuery().timers().count());
+    
+    for (String id : procInstIds) {
+      assertEquals(new Long(1L), managementService.createJobQuery().processInstanceId(id).count()); 
+      assertEquals(new Long(1L), managementService.createJobQuery().processInstanceId(id).timers().count());
+    }
+
+  }
+  
+  private List<String> startTestProcessInstances(int nrOfInstances) {
+    deployJpdlXmlString(
+            "<process name='TimerQueryTest' >" +
+            "  <start>" +
+            "    <transition to='t' />" +
+            "  </start>" +
+            "  <state name='t'>" +
+            "    <transition name='timeout' to='t'>" +
+            "      <timer duedate='20 hours' />" +
+            "    </transition>" +
+            "  </state>" +
+            "</process>"
+          );
+    
+    List<String> ids = new ArrayList<String>();
+    for (int i = 0; i < nrOfInstances; i++) {
+      ids.add(executionService.startProcessInstanceByKey("TimerQueryTest").getId());      
+    }
+    return ids;
+  }
+  
 }

Added: jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/ProcessInstanceQueryTest.java
===================================================================
--- jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/ProcessInstanceQueryTest.java	                        (rev 0)
+++ jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/ProcessInstanceQueryTest.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -0,0 +1,253 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jbpm.test.query;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.jbpm.api.Deployment;
+import org.jbpm.api.JbpmException;
+import org.jbpm.api.ProcessDefinition;
+import org.jbpm.api.ProcessInstance;
+import org.jbpm.api.ProcessInstanceQuery;
+import org.jbpm.test.JbpmTestCase;
+
+
+public class ProcessInstanceQueryTest extends JbpmTestCase {
+  
+  private static final String TEST_PROCESS_1_KEY = "testProcess1";
+  
+  private static final String TEST_PROCESS_1 = 
+    "<process name='" + TEST_PROCESS_1_KEY + "'>" +
+    "  <start>" +
+    "    <transition to='a' />" +
+    "  </start>" +
+    "  <state name='a'>" +
+    "    <transition to='end' />" +
+    "  </state>" +
+    "  <end name='end' />" +
+    "</process>";
+  
+  private static final String TEST_PROCESS_2_KEY = "testProcess2";
+  
+  private static final String TEST_PROCESS_2 = 
+    "<process name='" + TEST_PROCESS_2_KEY + "'>" +
+    "  <start>" +
+    "    <transition to='f' />" +
+    "  </start>" +
+    "  <fork name='f'>" +
+    "    <transition to='assemble product' />" +
+    "    <transition to='print documents' />" +
+    "  </fork>" +
+    "  <task name='assemble product' assignee='johndoe'>" +
+    "    <transition to='j' />" +
+    "  </task>" +
+    "  <task name='print documents' assignee='johndoe'>" +
+    "    <transition to='j' />" +
+    "  </task>" +
+    "  <join name='j'>" +
+    "    <transition to='end' />" +
+    "  </join>" +
+    "  <end name='end' />" +
+    "</process>";
+  
+  public void testQueryByProcessInstanceId() {
+    Map<String, ArrayList<String>> processInstanceIds = startTestProcesses(3, 7);
+    List<String> idsForTestProcess1 = processInstanceIds.get(TEST_PROCESS_1_KEY);
+    List<String> idsForTestProcess2 = processInstanceIds.get(TEST_PROCESS_2_KEY);
+    
+    for (String processInstanceId : idsForTestProcess1) {
+      ProcessInstance pi = executionService.createProcessInstanceQuery()
+        .processInstanceId(processInstanceId).uniqueResult();
+      assertNotNull(pi);
+      
+    }
+    
+    for (String processInstanceId : idsForTestProcess2) {
+      ProcessInstance pi = executionService.createProcessInstanceQuery()
+        .processInstanceId(processInstanceId).uniqueResult();
+      assertNotNull(pi);
+      
+    }
+  }
+  
+  public void testQueryByProcessInstanceKey() {
+    startTestProcesses(7, 8);
+
+    for (int i = 0; i < 7; i++) {
+      ProcessInstance pi = executionService.createProcessInstanceQuery()
+        .processInstanceKey(TEST_PROCESS_1_KEY + "-" + i).uniqueResult();
+      assertNotNull(pi);
+    }
+    
+    for (int i = 0; i < 8; i++) {
+      ProcessInstance pi = executionService.createProcessInstanceQuery()
+        .processInstanceKey(TEST_PROCESS_2_KEY + "-" + i).uniqueResult();
+      assertNotNull(pi);
+    }
+    
+  }
+  
+  public void testQueryByProcessDefinitionId() {
+    startTestProcesses(10, 6);
+    ProcessDefinition definitionProcess1 = 
+      repositoryService.createProcessDefinitionQuery().processDefinitionKey(TEST_PROCESS_1_KEY).uniqueResult();
+    ProcessDefinition definitionProcess2 = 
+      repositoryService.createProcessDefinitionQuery().processDefinitionKey(TEST_PROCESS_2_KEY).uniqueResult();
+    
+    List<ProcessInstance> processInstances = 
+      executionService.createProcessInstanceQuery().processDefinitionId(definitionProcess1.getId()).list();
+    assertEquals(10, processInstances.size());
+    
+    processInstances = 
+      executionService.createProcessInstanceQuery().processDefinitionId(definitionProcess2.getId()).list();
+    assertEquals(6, processInstances.size());
+  }
+  
+  public void testQueryBySuspended() {
+    startTestProcesses(6, 0); // Don't start any instance of test process 2
+    
+    Deployment deployment = repositoryService.createDeploymentQuery().uniqueResult();
+    repositoryService.suspendDeployment(deployment.getId());
+    
+    List<ProcessInstance> processInstances = executionService.createProcessInstanceQuery().suspended().list();
+    assertEquals(6, processInstances.size());
+  }
+  
+  public void testQueryByNotSuspended() {
+    startTestProcesses(5, 5);
+    
+    ProcessInstanceQuery query = executionService.createProcessInstanceQuery().notSuspended();
+    List<ProcessInstance> processInstances = query.list();
+    assertEquals(10, processInstances.size());
+    
+    List<Deployment> deployments = repositoryService.createDeploymentQuery().list();
+    assertEquals(2, deployments.size());
+    repositoryService.suspendDeployment(deployments.get(0).getId());
+    
+    processInstances = query.list();
+    assertEquals(5, processInstances.size());
+  }
+  
+  public void testQueryByPage() {
+    startTestProcesses(8, 12);
+    
+    for (int i = 0; i < 20; i += 2) {
+      List<ProcessInstance> processInstances = executionService.createProcessInstanceQuery().page(i, 2).list();
+      assertEquals(2, processInstances.size());
+    }
+  }
+  
+  public void testOrderByAscKey() {
+    startTestProcesses(6, 10);
+    
+    List<ProcessInstance> processInstances = 
+      executionService.createProcessInstanceQuery().orderAsc(ProcessInstanceQuery.PROPERTY_KEY).list();
+    assertEquals(16, processInstances.size());
+    
+    for (int i = 0; i < processInstances.size(); i++) {
+      
+      if (i < 6 ) {
+        assertTrue(processInstances.get(i).getKey().startsWith(TEST_PROCESS_1_KEY));
+      } else {
+        assertTrue(processInstances.get(i).getKey().startsWith(TEST_PROCESS_2_KEY));
+      }
+      
+    }
+  }
+  
+  public void testOrderByDescKey() {
+    startTestProcesses(5, 9);
+    
+    List<ProcessInstance> processInstances = 
+      executionService.createProcessInstanceQuery().orderDesc(ProcessInstanceQuery.PROPERTY_KEY).list();
+    assertEquals(14, processInstances.size());
+    
+    for (int i = 0; i < processInstances.size(); i++) {
+      
+      if (i < 9 ) {
+        assertTrue(processInstances.get(i).getKey().startsWith(TEST_PROCESS_2_KEY));
+      } else {
+        assertTrue(processInstances.get(i).getKey().startsWith(TEST_PROCESS_1_KEY));
+      }
+      
+    }
+  }
+
+  public void testCount() {
+    Map<String, ArrayList<String>> processInstanceIds = startTestProcesses(4, 6);
+    List<String> idsForTestProcess1 = processInstanceIds.get(TEST_PROCESS_1_KEY);
+    
+    assertEquals(new Long(10L), executionService.createProcessInstanceQuery().count());
+    
+    for (String processInstanceId : idsForTestProcess1) {
+      assertEquals(new Long(1L), executionService.createProcessInstanceQuery().processInstanceId(processInstanceId).count());
+    }
+    
+    for (int i = 0; i < 6; i++) {
+      assertEquals(new Long(1L), executionService.createProcessInstanceQuery().processInstanceKey(TEST_PROCESS_2_KEY + "-" + i).count());
+    }
+    
+    try {
+      executionService.createProcessInstanceQuery().page(0, 3).count();
+      fail("expected exception");
+    } catch (JbpmException e) {  }
+    
+  }
+
+  
+  /**
+   * Returns a map containing a processKey - processInstanceId mapping.
+   * eg. TEST_PROCESS_1 - {"1", "5", "7", "8"}
+   * 
+   * Process instance will also have a business key of the form 'processDefKey-nr', where
+   * nr is from 0 to the given nr of required processes.
+   */
+  private Map<String, ArrayList<String>> startTestProcesses(int nrOfTestProcess1, int nrOfTestProcess2) {
+    if (nrOfTestProcess1 > 0) {
+      deployJpdlXmlString(TEST_PROCESS_1);
+    }
+    if (nrOfTestProcess2 > 0) {
+      deployJpdlXmlString(TEST_PROCESS_2);
+    }
+    
+    Map<String, ArrayList<String>> result = new HashMap<String, ArrayList<String>>();
+    result.put(TEST_PROCESS_1_KEY, new ArrayList<String>());
+    result.put(TEST_PROCESS_2_KEY, new ArrayList<String>());
+    
+    for (int i = 0; i < nrOfTestProcess1; i++) {
+      ProcessInstance pi = executionService.startProcessInstanceByKey(TEST_PROCESS_1_KEY, TEST_PROCESS_1_KEY + "-" + i);
+      result.get(TEST_PROCESS_1_KEY).add(pi.getId());
+    }
+    
+
+    for (int i = 0; i < nrOfTestProcess2; i++) {
+      ProcessInstance pi = executionService.startProcessInstanceByKey(TEST_PROCESS_2_KEY, TEST_PROCESS_2_KEY + "-" + i);
+      result.get(TEST_PROCESS_2_KEY).add(pi.getId());
+    }
+   
+    return result;
+  }
+
+}

Modified: jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/TaskQueryTest.java
===================================================================
--- jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/TaskQueryTest.java	2009-12-22 09:43:59 UTC (rev 6016)
+++ jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/query/TaskQueryTest.java	2009-12-22 13:00:18 UTC (rev 6017)
@@ -109,7 +109,30 @@
     testOrderBy(TaskQuery.PROPERTY_PROGRESS, new Integer[] {2, 15, 75, 99});
   }
   
+  public void testCount() {
+    List<Task> tasks = new ArrayList<Task>();
+    
+    final String assignee = "task1";
+    for (int i= 0; i < 30; i++) {
+      Task task = taskService.newTask();
+      
+      // assign half of the tasks
+      if ( i % 2 == 0) {
+        task.setAssignee(assignee);
+      }
+      
+      taskService.saveTask(task);
+      tasks.add(task);
+    }
+    
+    TaskQuery taskQuery = taskService.createTaskQuery();
+    assertEquals(new Long(30L), taskQuery.count());
+    assertEquals(new Long(15L), taskQuery.assignee(assignee).count());
+    
+    deleteTasks(tasks);
+  }
   
+  
   /* -------------------------------------------------------------------
    * HELPER METHODS
    * ------------------------------------------------------------------- */



More information about the jbpm-commits mailing list