[jbpm-commits] JBoss JBPM SVN: r1822 - in jbpm4/pvm/trunk/modules/core/src: main/java/org/jbpm/pvm/test/base and 2 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Tue Aug 5 06:38:38 EDT 2008


Author: tom.baeyens at jboss.com
Date: 2008-08-05 06:38:38 -0400 (Tue, 05 Aug 2008)
New Revision: 1822

Modified:
   jbpm4/pvm/trunk/modules/core/src/main/java/org/jbpm/pvm/internal/env/PvmEnvironment.java
   jbpm4/pvm/trunk/modules/core/src/main/java/org/jbpm/pvm/test/base/EnvironmentDbTestCase.java
   jbpm4/pvm/trunk/modules/core/src/main/java/org/jbpm/pvm/test/base/EnvironmentTestCase.java
   jbpm4/pvm/trunk/modules/core/src/test/java/org/jbpm/pvm/internal/db/tx/BasicTransactionTest.java
   jbpm4/pvm/trunk/modules/core/src/test/resources/logging.properties
Log:
fixed the EnvironmentDbTestCases

Modified: jbpm4/pvm/trunk/modules/core/src/main/java/org/jbpm/pvm/internal/env/PvmEnvironment.java
===================================================================
--- jbpm4/pvm/trunk/modules/core/src/main/java/org/jbpm/pvm/internal/env/PvmEnvironment.java	2008-08-05 07:36:52 UTC (rev 1821)
+++ jbpm4/pvm/trunk/modules/core/src/main/java/org/jbpm/pvm/internal/env/PvmEnvironment.java	2008-08-05 10:38:38 UTC (rev 1822)
@@ -193,7 +193,7 @@
   // close ////////////////////////////////////////////////////////////////////
 
   public void close() {
-    log.debug("closing "+this+"...");
+    log.trace("closing "+this);
 
     pvmEnvironmentFactory.getApplicationWireContext().fire(PvmEnvironment.EVENT_CLOSEENVIRONMENT, this);
     

Modified: jbpm4/pvm/trunk/modules/core/src/main/java/org/jbpm/pvm/test/base/EnvironmentDbTestCase.java
===================================================================
--- jbpm4/pvm/trunk/modules/core/src/main/java/org/jbpm/pvm/test/base/EnvironmentDbTestCase.java	2008-08-05 07:36:52 UTC (rev 1821)
+++ jbpm4/pvm/trunk/modules/core/src/main/java/org/jbpm/pvm/test/base/EnvironmentDbTestCase.java	2008-08-05 10:38:38 UTC (rev 1822)
@@ -23,9 +23,10 @@
 
 import java.lang.reflect.Field;
 
+import org.hibernate.Session;
 import org.hibernate.SessionFactory;
+import org.hibernate.Transaction;
 import org.jbpm.pvm.Execution;
-import org.jbpm.pvm.env.Transaction;
 import org.jbpm.pvm.internal.model.ExecutionImpl;
 import org.jbpm.pvm.internal.model.ProcessDefinitionImpl;
 import org.jbpm.pvm.internal.util.ReflectUtil;
@@ -44,33 +45,57 @@
  */
 public class EnvironmentDbTestCase extends EnvironmentTestCase {
   
+  Transaction transaction = null;
+  
   public void setUp() throws Exception {
     if (isEnvironmentFactoryCached()) {
       Db.clean(getEnvironmentFactory());
     }
-    super.setUp();  
+    super.setUp();
+    beginTransaction();
   }
+  
+  public void tearDown() throws Exception {
+    commitTransaction();
+    super.tearDown();
+  }
 
+  void beginTransaction() {
+    Session session = environment.get(Session.class);
+    transaction = session.beginTransaction();
+  }
+
+  void commitTransaction() {
+    transaction.commit();
+    transaction = null;
+  }
+  
+  void rollbackTransaction() {
+    transaction.rollback();
+    transaction = null;
+  }
+  
   public DbSession getDbSession() {
     return environment.get(DbSession.class);
   }
   
   public void rollbackAndBeginNewTransaction() {
-    Transaction transaction = environment.get(Transaction.class);
-    transaction.setRollbackOnly();
-    environment.close();
-    environment = null;
+    rollbackTransaction();
+    closeEnvironment();
+    openEnvironment();
+    beginTransaction();
   }
   
   public void newTransaction() {
     try {
-      environment.close();
+      commitTransaction();
+      closeEnvironment();
     } finally {
-      log.debug("### new transaction ###################################################");
-      environment = getEnvironmentFactory().openEnvironment();
+      openEnvironment();
+      beginTransaction();
     }
   }
-  
+
   public void beginCacheTest() {
     SessionFactory sessionFactory = environment.get(SessionFactory.class);
     if (sessionFactory != null) {

Modified: jbpm4/pvm/trunk/modules/core/src/main/java/org/jbpm/pvm/test/base/EnvironmentTestCase.java
===================================================================
--- jbpm4/pvm/trunk/modules/core/src/main/java/org/jbpm/pvm/test/base/EnvironmentTestCase.java	2008-08-05 07:36:52 UTC (rev 1821)
+++ jbpm4/pvm/trunk/modules/core/src/main/java/org/jbpm/pvm/test/base/EnvironmentTestCase.java	2008-08-05 10:38:38 UTC (rev 1822)
@@ -39,12 +39,20 @@
 
   public void setUp() throws Exception {
     super.setUp();
-    environment = getEnvironmentFactory().openEnvironment();
+    openEnvironment();
   }
 
   public void tearDown() throws Exception {
+    closeEnvironment();
+    super.tearDown();
+  }
+  
+  void closeEnvironment() {
     environment.close();
     environment = null;
-    super.tearDown();
   }
+
+  void openEnvironment() {
+    environment = getEnvironmentFactory().openEnvironment();
+  }
 }

Modified: jbpm4/pvm/trunk/modules/core/src/test/java/org/jbpm/pvm/internal/db/tx/BasicTransactionTest.java
===================================================================
--- jbpm4/pvm/trunk/modules/core/src/test/java/org/jbpm/pvm/internal/db/tx/BasicTransactionTest.java	2008-08-05 07:36:52 UTC (rev 1821)
+++ jbpm4/pvm/trunk/modules/core/src/test/java/org/jbpm/pvm/internal/db/tx/BasicTransactionTest.java	2008-08-05 10:38:38 UTC (rev 1822)
@@ -23,11 +23,15 @@
 
 import java.util.List;
 
+import javax.transaction.Synchronization;
+
 import org.hibernate.Session;
 import org.jbpm.pvm.PvmException;
 import org.jbpm.pvm.env.Environment;
+import org.jbpm.pvm.env.Transaction;
 import org.jbpm.pvm.internal.cmd.Command;
 import org.jbpm.pvm.internal.model.CommentImpl;
+import org.jbpm.pvm.internal.type.variable.StringVariable;
 import org.jbpm.pvm.test.base.DbTestCase;
 
 /**
@@ -109,4 +113,79 @@
       }
     });
   }
+
+  
+  public static class SuccessfulSynchronization implements Synchronization {
+    public void beforeCompletion() {
+      Environment environment = Environment.getCurrent();
+      Session session = environment.get(Session.class);
+      StringVariable stringVariable = new StringVariable();
+      stringVariable.setValue("hello");
+      session.save(stringVariable);
+    }
+    public void afterCompletion(int arg0) {
+    }
+  }
+  
+  public void testSuccessfulSynchronization() {
+    commandService.execute(new Command<Object>(){
+      public Object execute(Environment environment) throws Exception {
+        Session session = environment.get(Session.class);
+        session.save(new CommentImpl("if i only had the time to write code"));
+        Transaction transaction = environment.get(Transaction.class);
+        SuccessfulSynchronization successfulSynchronization = new SuccessfulSynchronization(); 
+        transaction.registerSynchronization(successfulSynchronization);
+        return null;
+      }
+    });
+    
+    commandService.execute(new Command<Object>(){
+      public Object execute(Environment environment) {
+        Session session = environment.get(Session.class);
+        List<CommentImpl> comments = session.createQuery("from "+CommentImpl.class.getName()).list();
+        assertEquals("if i only had the time to write code", comments.get(0).getMessage());
+        List<StringVariable> stringVariables = session.createQuery("from "+StringVariable.class.getName()).list();
+        assertEquals("hello", stringVariables.get(0).getValue());
+        return null;
+      }
+    });
+  }
+
+  public static class UnsuccessfulSynchronization implements Synchronization {
+    public void beforeCompletion() {
+      Environment environment = Environment.getCurrent();
+      Session session = environment.get(Session.class);
+      StringVariable stringVariable = new StringVariable();
+      stringVariable.setValue("hello");
+      session.save(stringVariable);
+      throw new MyOwnRuntimeException();
+    }
+    public void afterCompletion(int arg0) {
+    }
+  }
+  
+  public void testUnsuccessfulSynchronization() {
+    commandService.execute(new Command<Object>(){
+      public Object execute(Environment environment) throws Exception {
+        Session session = environment.get(Session.class);
+        session.save(new CommentImpl("if i only had the time to write code"));
+        Transaction transaction = environment.get(Transaction.class);
+        UnsuccessfulSynchronization unsuccessfulSynchronization = new UnsuccessfulSynchronization(); 
+        transaction.registerSynchronization(unsuccessfulSynchronization);
+        return null;
+      }
+    });
+    
+    commandService.execute(new Command<Object>(){
+      public Object execute(Environment environment) {
+        Session session = environment.get(Session.class);
+        List<CommentImpl> comments = session.createQuery("from "+CommentImpl.class.getName()).list();
+        assertEquals(0, comments.size());
+        List<StringVariable> stringVariables = session.createQuery("from "+StringVariable.class.getName()).list();
+        assertEquals(0, stringVariables.size());
+        return null;
+      }
+    });
+  }
+
 }

Modified: jbpm4/pvm/trunk/modules/core/src/test/resources/logging.properties
===================================================================
--- jbpm4/pvm/trunk/modules/core/src/test/resources/logging.properties	2008-08-05 07:36:52 UTC (rev 1821)
+++ jbpm4/pvm/trunk/modules/core/src/test/resources/logging.properties	2008-08-05 10:38:38 UTC (rev 1822)
@@ -22,7 +22,7 @@
 org.hibernate.cfg.HbmBinder.level=SEVERE
 org.hibernate.cfg.SettingsFactory.level=SEVERE
 # org.hibernate.level=FINE
-# org.hibernate.SQL.level=FINEST
+org.hibernate.SQL.level=FINEST
 # org.hibernate.type.level=FINEST
 # org.hibernate.tool.hbm2ddl.SchemaExport.level=FINEST
 # org.hibernate.transaction.level=FINEST




More information about the jbpm-commits mailing list