[jbpm-commits] JBoss JBPM SVN: r5759 - in jbpm4/trunk/modules/db/src: test/java/org/jbpm/db/internal/upgrade and 1 other directory.

do-not-reply at jboss.org do-not-reply at jboss.org
Tue Oct 20 03:37:23 EDT 2009


Author: alex.guizar at jboss.com
Date: 2009-10-20 03:37:23 -0400 (Tue, 20 Oct 2009)
New Revision: 5759

Added:
   jbpm4/trunk/modules/db/src/test/java/org/jbpm/db/internal/upgrade/UpdateSchemaCmdTest.java
Modified:
   jbpm4/trunk/modules/db/src/main/java/org/jbpm/db/internal/upgrade/UpdateSchemaCmd.java
   jbpm4/trunk/modules/db/src/main/java/org/jbpm/db/internal/upgrade/UpgradeTool.java
Log:
[JBPM-2509] test UpdateSchemaCmd


Modified: jbpm4/trunk/modules/db/src/main/java/org/jbpm/db/internal/upgrade/UpdateSchemaCmd.java
===================================================================
--- jbpm4/trunk/modules/db/src/main/java/org/jbpm/db/internal/upgrade/UpdateSchemaCmd.java	2009-10-20 04:16:28 UTC (rev 5758)
+++ jbpm4/trunk/modules/db/src/main/java/org/jbpm/db/internal/upgrade/UpdateSchemaCmd.java	2009-10-20 07:37:23 UTC (rev 5759)
@@ -33,15 +33,19 @@
  * 
  * @author Alejandro Guizar
  */
-public class UpdateSchemaCmd implements Command<List<Exception>> {
+public class UpdateSchemaCmd implements Command<Void> {
 
   private static final long serialVersionUID = 1L;
 
-  @SuppressWarnings("unchecked")
-  public List<Exception> execute(Environment environment) throws Exception {
+  public Void execute(Environment environment) throws Exception {
     SchemaUpdate schemaUpdate = new SchemaUpdate(environment.get(Configuration.class));
     schemaUpdate.execute(false, true);
-    return schemaUpdate.getExceptions();
+    List< ? > exceptions = schemaUpdate.getExceptions();
+    if (!exceptions.isEmpty()) {
+      // in case there were several exceptions, schema update logged them already
+      throw (Exception) exceptions.get(0);
+    }
+    return null;
   }
 
 }

Modified: jbpm4/trunk/modules/db/src/main/java/org/jbpm/db/internal/upgrade/UpgradeTool.java
===================================================================
--- jbpm4/trunk/modules/db/src/main/java/org/jbpm/db/internal/upgrade/UpgradeTool.java	2009-10-20 04:16:28 UTC (rev 5758)
+++ jbpm4/trunk/modules/db/src/main/java/org/jbpm/db/internal/upgrade/UpgradeTool.java	2009-10-20 07:37:23 UTC (rev 5759)
@@ -21,8 +21,6 @@
  */
 package org.jbpm.db.internal.upgrade;
 
-import java.util.List;
-
 import org.jbpm.api.Configuration;
 import org.jbpm.api.ProcessEngine;
 
@@ -41,11 +39,9 @@
     processEngine = new Configuration().setResource(configResource).buildProcessEngine();
   }
 
-  public void upgrade() throws Exception {
+  public void upgrade() {
     // update database schema
-    List<Exception> exceptions = processEngine.execute(new UpdateSchemaCmd());
-    if (!exceptions.isEmpty())
-      throw exceptions.get(0);
+    processEngine.execute(new UpdateSchemaCmd());
 
     // add property "langid" to each deployed process
     processEngine.execute(new AddLangIdCmd());
@@ -54,7 +50,7 @@
     processEngine.execute(new InitDbidGeneratorCmd());
   }
 
-  public static void main(String[] args) throws Exception {
+  public static void main(String[] args) {
     UpgradeTool upgradeTool = args.length == 0 ? new UpgradeTool() : new UpgradeTool(args[0]);
     upgradeTool.upgrade();
   }

Added: jbpm4/trunk/modules/db/src/test/java/org/jbpm/db/internal/upgrade/UpdateSchemaCmdTest.java
===================================================================
--- jbpm4/trunk/modules/db/src/test/java/org/jbpm/db/internal/upgrade/UpdateSchemaCmdTest.java	                        (rev 0)
+++ jbpm4/trunk/modules/db/src/test/java/org/jbpm/db/internal/upgrade/UpdateSchemaCmdTest.java	2009-10-20 07:37:23 UTC (rev 5759)
@@ -0,0 +1,205 @@
+/*
+ * 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.db.internal.upgrade;
+
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.SQLWarning;
+import java.sql.Statement;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import org.hibernate.HibernateException;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.connection.ConnectionProvider;
+import org.hibernate.connection.ConnectionProviderFactory;
+import org.hibernate.dialect.Dialect;
+import org.hibernate.mapping.Table;
+import org.hibernate.util.JDBCExceptionReporter;
+import org.jbpm.api.cmd.Command;
+import org.jbpm.api.cmd.Environment;
+import org.jbpm.test.JbpmTestCase;
+
+/**
+ * @author Alejandro Guizar
+ */
+public class UpdateSchemaCmdTest extends JbpmTestCase {
+
+  public void testUpdateSchema() {
+    // drop some jbpm table
+    processEngine.execute(new DropTableCmd("JBPM4_PROPERTY"));
+    assertFalse(processEngine.execute(new TableExistsCmd("JBPM4_PROPERTY")));
+
+    // run schema update tool
+    processEngine.execute(new UpdateSchemaCmd());
+
+    // verify jbpm table was created again
+    assertTrue(processEngine.execute(new TableExistsCmd("JBPM4_PROPERTY")));
+  }
+
+  /**
+   * Base class for SQL commands on top of Hibernate.
+   * 
+   * @author Alejandro Guizar
+   */
+  static abstract class SqlCmd<T> implements Command<T> {
+
+    private Configuration configuration;
+    private ConnectionProvider connectionProvider;
+
+    private static final long serialVersionUID = 1L;
+
+    public T execute(Environment environment) throws SQLException {
+      configuration = environment.get(Configuration.class);
+      Connection connection = null;
+      try {
+        connection = createConnection();
+        return execute(connection);
+      } finally {
+        closeConnection(connection);
+        configuration = null;
+      }
+    }
+
+    protected abstract T execute(Connection connection) throws SQLException;
+
+    protected Dialect getDialect() {
+      return Dialect.getDialect(configuration.getProperties());
+    }
+
+    protected String getDefaultCatalog() {
+      return configuration.getProperty(org.hibernate.cfg.Environment.DEFAULT_CATALOG);
+    }
+
+    protected String getDefaultSchema() {
+      return configuration.getProperty(org.hibernate.cfg.Environment.DEFAULT_SCHEMA);
+    }
+
+    protected Table findTable(String tableName) {
+      for (Iterator<?> i = configuration.getTableMappings(); i.hasNext();) {
+        Table table = (Table) i.next();
+        if (tableName.equals(table.getName()))
+          return table;
+      }
+      throw new NoSuchElementException(tableName);
+    }
+
+    private Connection createConnection() throws SQLException {
+      try {
+        connectionProvider = ConnectionProviderFactory.newConnectionProvider(configuration.getProperties());
+      } catch (HibernateException e) {
+        throw new SQLException(e.getMessage());
+      }
+      Connection connection = connectionProvider.getConnection();
+      if (connection.getAutoCommit() == false) {
+        connection.commit();
+        connection.setAutoCommit(true);
+      }
+      return connection;
+    }
+
+    private void closeConnection(Connection connection) {
+      if (connectionProvider != null) {
+        try {
+          if (connection != null) {
+            JDBCExceptionReporter.logAndClearWarnings(connection);
+            connectionProvider.closeConnection(connection);
+          }
+        } catch (SQLException e) {
+          JDBCExceptionReporter.logExceptions(e);
+        } finally {
+          connectionProvider.close();
+          connectionProvider = null;
+        }
+      }
+    }
+  }
+
+  /**
+   * Drop the named table from the database.
+   * 
+   * @author Alejandro Guizar
+   */
+  static class DropTableCmd extends SqlCmd<Void> {
+
+    private final String tableName;
+
+    private static final long serialVersionUID = 1L;
+
+    DropTableCmd(String tableName) {
+      this.tableName = tableName;
+    }
+
+    protected Void execute(Connection connection) throws SQLException {
+      Table table = findTable(tableName);
+
+      // generate sql to drop table
+      String dropSql = table.sqlDropString(getDialect(), getDefaultCatalog(), getDefaultSchema());
+
+      // execute sql to drop table
+      Statement statement = connection.createStatement();
+      try {
+        statement.executeUpdate(dropSql);
+
+        SQLWarning warning = statement.getWarnings();
+        if (warning != null) {
+          JDBCExceptionReporter.logWarnings(warning);
+          statement.clearWarnings();
+        }
+      } finally {
+        statement.close();
+      }
+
+      return null;
+    }
+  }
+
+  /**
+   * Tell whether the named table exists in the database.
+   * 
+   * @author Alejandro Guizar
+   */
+  static class TableExistsCmd extends SqlCmd<Boolean> {
+
+    private final String tableName;
+
+    private static final long serialVersionUID = 1L;
+    private static final String[] TABLE_TYPES = { "TABLE" };
+
+    TableExistsCmd(String tableName) {
+      this.tableName = tableName;
+    }
+
+    protected Boolean execute(Connection connection) throws SQLException {
+      DatabaseMetaData metaData = connection.getMetaData();
+      ResultSet resultSet = metaData.getTables(getDefaultCatalog(), getDefaultSchema(), tableName, TABLE_TYPES);
+      try {
+        return resultSet.next();
+      } finally {
+        resultSet.close();
+      }
+    }
+
+  }
+}


Property changes on: jbpm4/trunk/modules/db/src/test/java/org/jbpm/db/internal/upgrade/UpdateSchemaCmdTest.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF



More information about the jbpm-commits mailing list