From 627dd5013ba1807e4bfbbd6f7dd2ab7561dfaf51 Mon Sep 17 00:00:00 2001
From: brinn <brinn>
Date: Sat, 5 Jan 2008 20:32:23 +0000
Subject: [PATCH] add: unit tests for SqlScriptExecutor

SVN: 3310
---
 dbmigration/.classpath                        |   1 +
 .../cisd/dbmigration/SqlScriptExecutor.java   |  10 +-
 .../dbmigration/SqlScriptExecutorTest.java    | 167 ++++++++++++++++++
 3 files changed, 176 insertions(+), 2 deletions(-)
 create mode 100644 dbmigration/sourceTest/java/ch/systemsx/cisd/dbmigration/SqlScriptExecutorTest.java

diff --git a/dbmigration/.classpath b/dbmigration/.classpath
index 1e9f32ebbdd..b5854f63a06 100644
--- a/dbmigration/.classpath
+++ b/dbmigration/.classpath
@@ -17,5 +17,6 @@
 	<classpathentry kind="lib" path="/libraries/jmock/third-party-libs/objenesis-1.0.jar"/>
 	<classpathentry kind="lib" path="/libraries/postgresql/postgresql.jar" sourcepath="/libraries/postgresql/src.zip"/>
 	<classpathentry kind="lib" path="/libraries/commons-io/commons-io.jar" sourcepath="/libraries/commons-io/src.zip"/>
+	<classpathentry kind="lib" path="/libraries/commons-logging/commons-logging.jar" sourcepath="/libraries/commons-logging/src.zip"/>
 	<classpathentry kind="output" path="targets/classes"/>
 </classpath>
diff --git a/dbmigration/source/java/ch/systemsx/cisd/dbmigration/SqlScriptExecutor.java b/dbmigration/source/java/ch/systemsx/cisd/dbmigration/SqlScriptExecutor.java
index b1eee39e98c..24bcce1c3eb 100644
--- a/dbmigration/source/java/ch/systemsx/cisd/dbmigration/SqlScriptExecutor.java
+++ b/dbmigration/source/java/ch/systemsx/cisd/dbmigration/SqlScriptExecutor.java
@@ -65,7 +65,7 @@ public class SqlScriptExecutor extends JdbcDaoSupport implements ISqlScriptExecu
                 {
                     try
                     {
-                        getJdbcTemplate().execute(sqlStatement);
+                        execute(sqlStatement);
                     } catch (BadSqlGrammarException ex2)
                     {
                         throw new BadSqlGrammarException(getTask(ex2), lastSqlStatement + ">-->" + sqlStatement
@@ -75,7 +75,7 @@ public class SqlScriptExecutor extends JdbcDaoSupport implements ISqlScriptExecu
                 }
             } else
             {
-                getJdbcTemplate().execute(sqlScriptCode);
+                execute(sqlScriptCode);
             }
             if (loggerOrNull != null)
             {
@@ -98,6 +98,12 @@ public class SqlScriptExecutor extends JdbcDaoSupport implements ISqlScriptExecu
         }
     }
 
+    // @Private
+    void execute(String script)
+    {
+        getJdbcTemplate().execute(script);
+    }
+    
     private String getTask(BadSqlGrammarException ex)
     {
         final String msg = ex.getMessage();
diff --git a/dbmigration/sourceTest/java/ch/systemsx/cisd/dbmigration/SqlScriptExecutorTest.java b/dbmigration/sourceTest/java/ch/systemsx/cisd/dbmigration/SqlScriptExecutorTest.java
new file mode 100644
index 00000000000..0ad0f0f4980
--- /dev/null
+++ b/dbmigration/sourceTest/java/ch/systemsx/cisd/dbmigration/SqlScriptExecutorTest.java
@@ -0,0 +1,167 @@
+/*
+ * Copyright 2008 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.dbmigration;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.springframework.jdbc.datasource.DelegatingDataSource;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import ch.systemsx.cisd.common.Script;
+import ch.systemsx.cisd.common.db.ISqlScriptExecutionLogger;
+import ch.systemsx.cisd.common.logging.LogInitializer;
+
+import static org.testng.AssertJUnit.*;
+
+/**
+ * Test cases for the {@link SqlScriptExecutor}.
+ * 
+ * @author Bernd Rinn
+ */
+public class SqlScriptExecutorTest
+{
+
+    private class TestSqlScriptExecutor extends SqlScriptExecutor
+    {
+        private final List<String> executions;
+        
+        private final Error th;
+
+        public TestSqlScriptExecutor(boolean singleStepMode)
+        {
+            this(null, singleStepMode);
+        }
+        public TestSqlScriptExecutor(Error th, boolean singleStepMode)
+        {
+            super(new DelegatingDataSource(), singleStepMode);
+            this.th = th;
+            this.executions = new ArrayList<String>();
+        }
+
+        @Override
+        void execute(String script)
+        {
+            if (th != null)
+            {
+                throw th;
+            }
+            executions.add(script);
+        }
+
+        List<String> getExecutions()
+        {
+            return executions;
+        }
+    }
+
+    @BeforeClass(alwaysRun = true)
+    public void beforeClass() throws Exception
+    {
+        LogInitializer.init();
+    }
+
+    @Test
+    public void testHappyCase()
+    {
+        final TestSqlScriptExecutor scriptExecutor = new TestSqlScriptExecutor(false);
+        final String simpleScriptContent1 = "statement1; statement2;";
+        final Script simpleScript1 = new Script("script1", simpleScriptContent1, "VERSION");
+        final String simpleScriptContent2 = "statement3; statement4;";
+        final Script simpleScript2 = new Script("script2", simpleScriptContent2, "VERSION");
+        scriptExecutor.execute(simpleScript1, true, null);
+        scriptExecutor.execute(simpleScript2, true, null);
+        assertEquals(2, scriptExecutor.getExecutions().size());
+        assertEquals(Arrays.asList(simpleScriptContent1, simpleScriptContent2), scriptExecutor.getExecutions());
+    }
+
+    @Test
+    public void testSingleStepMode()
+    {
+        final TestSqlScriptExecutor scriptExecutor = new TestSqlScriptExecutor(true);
+        final String simpleScriptContent1 = "statement1; statement2;";
+        final Script simpleScript1 = new Script("script1", simpleScriptContent1, "VERSION");
+        final String simpleScriptContent2 = "statement3.1 -- some coment\n statement3.2 ; ";
+        final Script simpleScript2 = new Script("script2", simpleScriptContent2, "VERSION");
+        scriptExecutor.execute(simpleScript1, true, null);
+        scriptExecutor.execute(simpleScript2, true, null);
+        assertEquals(3, scriptExecutor.getExecutions().size());
+        assertEquals(Arrays.asList("statement1;", "statement2;", "statement3.1 statement3.2;"), scriptExecutor
+                .getExecutions());
+    }
+
+    @Test
+    public void testLoggingHappyCase()
+    {
+        final Mockery context = new Mockery();
+        try
+        {
+            final TestSqlScriptExecutor scriptExecutor = new TestSqlScriptExecutor(false);
+            final String simpleScriptContent = "statement1; statement2;";
+            final Script simpleScript = new Script("script1", simpleScriptContent, "VERSION");
+            final ISqlScriptExecutionLogger logger = context.mock(ISqlScriptExecutionLogger.class);
+            context.checking(new Expectations()
+            {
+                {
+                    one(logger).logStart(simpleScript);
+                    one(logger).logSuccess(simpleScript);
+                }
+                
+            });
+            scriptExecutor.execute(simpleScript, true, logger);
+        } finally
+        {
+            context.assertIsSatisfied();
+        }
+    }
+
+    class MyError extends Error
+    {
+        private static final long serialVersionUID = 1L;
+    }
+    
+    @Test(expectedExceptions=MyError.class)
+    public void testLoggingScriptThrowsException()
+    {
+        final Mockery context = new Mockery();
+        try
+        {
+            final MyError error = new MyError();
+            final TestSqlScriptExecutor scriptExecutor = new TestSqlScriptExecutor(error, false);
+            final String simpleScriptContent = "statement1; statement2;";
+            final Script simpleScript = new Script("script1", simpleScriptContent, "VERSION");
+            final ISqlScriptExecutionLogger logger = context.mock(ISqlScriptExecutionLogger.class);
+            context.checking(new Expectations()
+            {
+                {
+                    one(logger).logStart(simpleScript);
+                    one(logger).logFailure(simpleScript, error);
+                }
+                
+            });
+            scriptExecutor.execute(simpleScript, true, logger);
+        } finally
+        {
+            context.assertIsSatisfied();
+        }
+    }
+
+}
-- 
GitLab