From f5703b0b64ed425e17a82da965babcdff4d62e17 Mon Sep 17 00:00:00 2001
From: cramakri <cramakri>
Date: Mon, 7 Feb 2011 10:12:21 +0000
Subject: [PATCH] LMS-1988 Add new tests.

SVN: 19779
---
 .../DataSetRegistrationTransactionTest.java   |  57 ++++++++++
 .../api/v1/impl/MoveFileCommandTest.java      | 106 ++++++++++++++----
 2 files changed, 144 insertions(+), 19 deletions(-)

diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/registrator/api/v1/impl/DataSetRegistrationTransactionTest.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/registrator/api/v1/impl/DataSetRegistrationTransactionTest.java
index a3fcbf6175c..418e8d25366 100644
--- a/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/registrator/api/v1/impl/DataSetRegistrationTransactionTest.java
+++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/registrator/api/v1/impl/DataSetRegistrationTransactionTest.java
@@ -214,6 +214,63 @@ public class DataSetRegistrationTransactionTest extends AbstractFileSystemTestCa
         context.assertIsSatisfied();
     }
 
+    @Test
+    public void testDoubleRollbackNormal()
+    {
+        setUpOpenBisExpectations(false);
+        createTransaction();
+
+        IDataSet newDataSet = tr.createNewDataSet();
+        String dst = tr.moveFile(srcFile.getAbsolutePath(), newDataSet);
+
+        checkContentsOfFile(new File(dst));
+
+        tr.rollback();
+        tr.rollback();
+
+        checkContentsOfFile(srcFile);
+
+        context.assertIsSatisfied();
+    }
+
+    @Test
+    public void testDoubleRollbackFromDuplicatedQueue() throws IOException
+    {
+        setUpOpenBisExpectations(false);
+        createTransaction();
+
+        // Move a file into the new data set and check that it is valid
+        IDataSet newDataSet = tr.createNewDataSet();
+        String dst = tr.moveFile(srcFile.getAbsolutePath(), newDataSet);
+        checkContentsOfFile(new File(dst));
+
+        // Duplicate the rollback queue so we can simulate a double rollback
+        File[] rollbackQueueFiles = listRollbackQueueFiles();
+        assertEquals(2, rollbackQueueFiles.length);
+        for (File rollbackQueueFile : rollbackQueueFiles)
+        {
+            File destFile = new File(workingDirectory, "dup-" + rollbackQueueFile.getName());
+            FileUtils.copyFile(rollbackQueueFile, destFile);
+        }
+        rollbackQueueFiles = listRollbackQueueFiles();
+        assertEquals(4, rollbackQueueFiles.length);
+
+        // Do a "normal" rollback
+        tr.rollback();
+        rollbackQueueFiles = listRollbackQueueFiles();
+        assertEquals(2, rollbackQueueFiles.length);
+
+        // Rollback again using the queue we duplicated
+        DataSetRegistrationTransaction.rollbackDeadTransactions(workingDirectory);
+        rollbackQueueFiles = listRollbackQueueFiles();
+        assertEquals(0, rollbackQueueFiles.length);
+
+        // The source file should be correct and now exceptions should have been thrown
+        checkContentsOfFile(srcFile);
+
+        context.assertIsSatisfied();
+    }
+
     private File[] listRollbackQueueFiles()
     {
         File[] rollbackQueueFiles = workingDirectory.listFiles(new FilenameFilter()
diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/registrator/api/v1/impl/MoveFileCommandTest.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/registrator/api/v1/impl/MoveFileCommandTest.java
index 26aef3e9e41..96efb38b9b4 100644
--- a/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/registrator/api/v1/impl/MoveFileCommandTest.java
+++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/registrator/api/v1/impl/MoveFileCommandTest.java
@@ -31,6 +31,22 @@ public class MoveFileCommandTest extends AbstractTestWithRollbackStack
 {
     private File srcFile;
 
+    private File dstDir;
+
+    private MkdirsCommand mkdirsCmd;
+
+    private File dstFile;
+
+    private MoveFileCommand mvOldFile;
+
+    private File newFile;
+
+    private NewFileCommand newFileCmd;
+
+    private File newNewFile;
+
+    private MoveFileCommand mvNewFile;
+
     @BeforeMethod
     @Override
     public void setUp() throws IOException
@@ -39,56 +55,108 @@ public class MoveFileCommandTest extends AbstractTestWithRollbackStack
 
         srcFile = new File(workingDirectory, "read.me");
         fillContentsOfSource();
-    }
 
-    @Test
-    public void testMoveToDir()
-    {
-        File dstDir = new File(workingDirectory, "bar-dir/");
-        MkdirsCommand mkdirsCmd = new MkdirsCommand(dstDir.getAbsolutePath());
+        dstDir = new File(workingDirectory, "bar-dir/");
+        mkdirsCmd = new MkdirsCommand(dstDir.getAbsolutePath());
 
-        File dstFile = new File(dstDir, srcFile.getName());
-        MoveFileCommand cmd =
+        dstFile = new File(dstDir, srcFile.getName());
+        mvOldFile =
                 new MoveFileCommand(workingDirectory.getAbsolutePath(), srcFile.getName(),
                         dstDir.getAbsolutePath(), srcFile.getName());
 
+        newFile = new File(dstDir, "new-file.txt");
+        newFileCmd = new NewFileCommand(newFile.getAbsolutePath());
+
+        newNewFile = new File(dstDir, "new-new-file.txt");
+        mvNewFile =
+                new MoveFileCommand(dstDir.getAbsolutePath(), newFile.getName(),
+                        dstDir.getAbsolutePath(), newNewFile.getName());
+    }
+
+    @Test
+    public void testFileCommands()
+    {
         rollbackStack.pushAndExecuteCommand(mkdirsCmd);
-        rollbackStack.pushAndExecuteCommand(cmd);
+        rollbackStack.pushAndExecuteCommand(mvOldFile);
+        rollbackStack.pushAndExecuteCommand(newFileCmd);
+        fillContentsOfFile(newFile, "This is a new file.");
+        rollbackStack.pushAndExecuteCommand(mvNewFile);
 
         checkContentsOfFile(dstFile);
+        checkContentsOfFile(newNewFile, "This is a new file.");
+        assertFalse("The new file should have been moved", newFile.exists());
     }
 
     @Test
-    public void testUndo()
+    public void testRollbackFileCommands()
     {
-        File dstDir = new File(workingDirectory, "bar-dir/");
-        MkdirsCommand mkdirsCmd = new MkdirsCommand(dstDir.getAbsolutePath());
+        rollbackStack.pushAndExecuteCommand(mkdirsCmd);
+        rollbackStack.pushAndExecuteCommand(mvOldFile);
+        rollbackStack.pushAndExecuteCommand(newFileCmd);
+        fillContentsOfFile(newFile, "This is a new file.");
+        rollbackStack.pushAndExecuteCommand(mvNewFile);
 
-        File dstFile = new File(dstDir, srcFile.getName());
-        MoveFileCommand cmd =
-                new MoveFileCommand(workingDirectory.getAbsolutePath(), srcFile.getName(),
-                        dstDir.getAbsolutePath(), srcFile.getName());
+        checkContentsOfFile(dstFile);
+        checkContentsOfFile(newNewFile, "This is a new file.");
+        assertFalse("The new file should have been moved", newFile.exists());
+
+        rollbackStack.rollbackAll();
 
+        assertTrue("The file that we created and moved have been removed",
+                false == newNewFile.exists());
+        assertTrue("The file that we created should have been removed", false == newFile.exists());
+        assertTrue("The file should have been deleted", false == dstFile.exists());
+        assertTrue("The directory should have been deleted", false == dstDir.exists());
+        checkContentsOfFile(srcFile);
+    }
+
+    @Test
+    public void testDoubleRollbackFileCommands()
+    {
         rollbackStack.pushAndExecuteCommand(mkdirsCmd);
-        rollbackStack.pushAndExecuteCommand(cmd);
+        rollbackStack.pushAndExecuteCommand(mvOldFile);
+        rollbackStack.pushAndExecuteCommand(newFileCmd);
+        fillContentsOfFile(newFile, "This is a new file.");
+        rollbackStack.pushAndExecuteCommand(mvNewFile);
 
         checkContentsOfFile(dstFile);
+        checkContentsOfFile(newNewFile, "This is a new file.");
+        assertFalse("The new file should have been moved", newFile.exists());
 
         rollbackStack.rollbackAll();
 
+        mvNewFile.rollback();
+        newFileCmd.rollback();
+        mvOldFile.rollback();
+        mkdirsCmd.rollback();
+
+        assertTrue("The file that we created and moved have been removed",
+                false == newNewFile.exists());
+        assertTrue("The file that we created should have been removed", false == newFile.exists());
         assertTrue("The file should have been deleted", false == dstFile.exists());
         assertTrue("The directory should have been deleted", false == dstDir.exists());
         checkContentsOfFile(srcFile);
     }
 
     private void checkContentsOfFile(File dst)
+    {
+        checkContentsOfFile(dst, "hello world");
+    }
+
+    private void checkContentsOfFile(File dst, String contents)
     {
         assertTrue("The file should exist", dst.exists());
-        assertEquals("hello world\n", FileUtilities.loadToString(dst));
+        assertEquals(contents + "\n", FileUtilities.loadToString(dst));
     }
 
     private void fillContentsOfSource()
     {
-        FileUtilities.writeToFile(srcFile, "hello world");
+        fillContentsOfFile(srcFile, "hello world");
     }
+
+    private void fillContentsOfFile(File aFile, String contents)
+    {
+        FileUtilities.writeToFile(aFile, contents);
+    }
+
 }
-- 
GitLab