[jbpm-commits] JBoss JBPM SVN: r5473 - jbpm4/trunk/modules/test-cfg/src/test/java/org/jbpm/test/hibernatelongid.

do-not-reply at jboss.org do-not-reply at jboss.org
Thu Aug 13 02:14:48 EDT 2009


Author: jbarrez
Date: 2009-08-13 02:14:47 -0400 (Thu, 13 Aug 2009)
New Revision: 5473

Added:
   jbpm4/trunk/modules/test-cfg/src/test/java/org/jbpm/test/hibernatelongid/HibernateLongIdTest2.java
Log:
Added second version of HibernateLongIdTest for internal discussion

Added: jbpm4/trunk/modules/test-cfg/src/test/java/org/jbpm/test/hibernatelongid/HibernateLongIdTest2.java
===================================================================
--- jbpm4/trunk/modules/test-cfg/src/test/java/org/jbpm/test/hibernatelongid/HibernateLongIdTest2.java	                        (rev 0)
+++ jbpm4/trunk/modules/test-cfg/src/test/java/org/jbpm/test/hibernatelongid/HibernateLongIdTest2.java	2009-08-13 06:14:47 UTC (rev 5473)
@@ -0,0 +1,176 @@
+/*
+ * 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.hibernatelongid;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.hibernate.SessionFactory;
+import org.hibernate.Transaction;
+import org.hibernate.classic.Session;
+import org.jbpm.api.ProcessInstance;
+import org.jbpm.test.JbpmCustomCfgTestCase;
+
+
+/**
+ * @author Tom Baeyens
+ */
+public class HibernateLongIdTest2 extends JbpmCustomCfgTestCase {
+  
+  private HibernateTemplate hibernateTemplate;
+  
+  @Override
+  protected void setUp() throws Exception {
+    super.setUp();
+    SessionFactory sessionFactory = processEngine.get(SessionFactory.class);
+    this.hibernateTemplate = new HibernateTemplate(sessionFactory);
+  }
+  
+  protected void deleteRegisteredDeployments() {
+    
+    hibernateTemplate.execute(new VoidHibernateCallback() {
+
+      public void executeNoReturn(Session session) {
+
+        // cannot do super.x() in anonymous inner class method :-(
+        // super.deleteRegisteredDeployments();
+        // so we repeat the functionality ... for the moment
+
+        for (String deploymentId : registeredDeployments) {
+          repositoryService.deleteDeploymentCascade(deploymentId);
+        }
+      
+        List<Order> orders = session.createQuery("from " + Order.class.getName()).list();
+        for (Order order : orders) {
+          session.delete(order);
+        }
+      }
+
+    });
+    
+  }
+
+  public void testHibernateLongId() {
+    
+    hibernateTemplate.execute(new VoidHibernateCallback() {
+
+      public void executeNoReturn(Session session) {
+        deployJpdlXmlString(
+                "<process name='HibernateLongId'>" +
+                "  <start>" +
+                "    <transition to='a' />" +
+                "  </start>" +
+                "  <state name='a' />" +
+                "</process>"
+              );
+      }
+      
+    });
+
+    final String processInstanceId = (String) hibernateTemplate.execute(new HibernateCallback() {
+
+      public Object execute(Session session) {
+        Order order = new Order();
+        order.setClient("Contador");
+        order.setProduct("Shampoo");
+
+        session.save(order);
+        //session.flush();
+        
+        Map<String, Object> variables = new HashMap<String, Object>();
+        variables.put("order", order);
+
+        ProcessInstance processInstance = executionService.startProcessInstanceByKey("HibernateLongId", variables);
+        return processInstance.getId();
+      }
+      
+    });
+
+
+    hibernateTemplate.execute(new VoidHibernateCallback() {
+
+      public void executeNoReturn(Session session) {
+        Order order = (Order) executionService.getVariable(processInstanceId, "order");
+        assertNotNull(order);
+        assertEquals("Contador", order.getClient());
+        assertEquals("Shampoo", order.getProduct());
+      }
+      
+    });
+  }
+  
+  
+  // Quick template mechanism
+  
+  class HibernateTemplate {
+    
+    private SessionFactory sessionFactory;
+    
+    public HibernateTemplate(SessionFactory sessionFactory) {
+      this.sessionFactory = sessionFactory;
+    }
+    
+    public Object execute(HibernateCallback callback) {
+      Session session = sessionFactory.openSession();
+      Transaction transaction = session.beginTransaction();
+      try {
+       return callback.execute(session);
+      } catch (Exception e) {
+        transaction.rollback();
+      } finally {
+        if (!transaction.wasRolledBack()) {
+          transaction.commit();
+        }
+        session.close();
+      }
+      return null;
+    }
+
+    public SessionFactory getSessionFactory() {
+      return sessionFactory;
+    }
+
+    public void setSessionFactory(SessionFactory sessionFactory) {
+      this.sessionFactory = sessionFactory;
+    }
+    
+  }
+  
+  interface HibernateCallback {
+    
+    public Object execute(Session session);
+    
+  }
+  
+  abstract class VoidHibernateCallback implements HibernateCallback {
+    
+    public abstract void executeNoReturn(Session session);
+    
+    public Object execute(Session session) {
+      executeNoReturn(session);
+      return null;
+    }
+    
+  }
+  
+}



More information about the jbpm-commits mailing list