From ee547719b7301feaabac0b4a1a8675a74b7358c0 Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Thu, 11 Oct 2007 15:44:31 +0000
Subject: [PATCH] LMS-103 Storage: Javadoc added, IStorage contract modified,
 more tests in FileStorageTest

SVN: 2135
---
 .../cisd/bds/AbstractDataStructure.java       |  4 +-
 .../java/ch/systemsx/cisd/bds/Container.java  |  2 +-
 .../ch/systemsx/cisd/bds/storage/IFile.java   |  2 +-
 .../ch/systemsx/cisd/bds/storage/ILink.java   |  5 ++-
 .../systemsx/cisd/bds/storage/IStorage.java   | 21 +++++++++--
 .../bds/storage/filesystem/AbstractNode.java  | 11 +++++-
 .../bds/storage/filesystem/Directory.java     |  5 ++-
 .../bds/storage/filesystem/FileStorage.java   | 22 +++++++++--
 .../cisd/bds/storage/hdf5/HDF5Storage.java    |  6 +--
 .../storage/filesystem/FileStorageTest.java   | 37 ++++++++++++++++++-
 10 files changed, 95 insertions(+), 20 deletions(-)

diff --git a/bds/source/java/ch/systemsx/cisd/bds/AbstractDataStructure.java b/bds/source/java/ch/systemsx/cisd/bds/AbstractDataStructure.java
index 0e8f1c9c0ca..684ecd3d09d 100644
--- a/bds/source/java/ch/systemsx/cisd/bds/AbstractDataStructure.java
+++ b/bds/source/java/ch/systemsx/cisd/bds/AbstractDataStructure.java
@@ -39,7 +39,7 @@ public abstract class AbstractDataStructure implements IHasVersion
 
     public void load()
     {
-        storage.load();
+        storage.mount();
         Version loadedVersion = Version.loadFrom(root);
         if (getVersion().isBackwardsCompatibleWith(loadedVersion) == false)
         {
@@ -51,6 +51,6 @@ public abstract class AbstractDataStructure implements IHasVersion
     public void save()
     {
         getVersion().saveTo(root);
-        storage.save();
+        storage.unmount();
     }
 }
diff --git a/bds/source/java/ch/systemsx/cisd/bds/Container.java b/bds/source/java/ch/systemsx/cisd/bds/Container.java
index d161d843fa0..a3e88af305a 100644
--- a/bds/source/java/ch/systemsx/cisd/bds/Container.java
+++ b/bds/source/java/ch/systemsx/cisd/bds/Container.java
@@ -42,7 +42,7 @@ public class Container
     public AbstractDataStructure load(String name)
     {
         IStorage storage = createStorage(name);
-        storage.load();
+        storage.mount();
         Version version = Version.loadFrom(storage.getRoot());
         return DataStructureFactory.createDataStructure(storage, version);
     }
diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/IFile.java b/bds/source/java/ch/systemsx/cisd/bds/storage/IFile.java
index 6a3b733bbb2..ffe6e069e41 100644
--- a/bds/source/java/ch/systemsx/cisd/bds/storage/IFile.java
+++ b/bds/source/java/ch/systemsx/cisd/bds/storage/IFile.java
@@ -17,7 +17,7 @@
 package ch.systemsx.cisd.bds.storage;
 
 /**
- * Role of a file with a value (content) of type <code>T</code>.
+ * Role of a leaf node representing a file with some content.
  *
  * @author Franz-Josef Elmer
  */
diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/ILink.java b/bds/source/java/ch/systemsx/cisd/bds/storage/ILink.java
index c533b230cb2..ee0f5074e9a 100644
--- a/bds/source/java/ch/systemsx/cisd/bds/storage/ILink.java
+++ b/bds/source/java/ch/systemsx/cisd/bds/storage/ILink.java
@@ -17,11 +17,14 @@
 package ch.systemsx.cisd.bds.storage;
 
 /**
- * 
+ * Node representing a symbolic link.
  *
  * @author Franz-Josef Elmer
  */
 public interface ILink extends INode
 {
+    /**
+     * Returns the node referred by this node.
+     */
     public INode getReference();
 }
diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/IStorage.java b/bds/source/java/ch/systemsx/cisd/bds/storage/IStorage.java
index b02fd16a11c..98cf5ae7593 100644
--- a/bds/source/java/ch/systemsx/cisd/bds/storage/IStorage.java
+++ b/bds/source/java/ch/systemsx/cisd/bds/storage/IStorage.java
@@ -16,16 +16,29 @@
 
 package ch.systemsx.cisd.bds.storage;
 
+import ch.systemsx.cisd.common.exceptions.UserFailureException;
+
 /**
- * 
+ * Abstraction of a hierarchical storage.
  *
  * @author Franz-Josef Elmer
  */
 public interface IStorage
 {
-    public void load();
+    /**
+     * Mounts this storage. May perform some initializations. Should be called before calling {@link #getRoot()}.
+     */
+    public void mount();
     
-    public IDirectory getRoot();
+    /**
+     * Returns root directory of this storage. 
+     * 
+     * @throws UserFailureException if invoked before {@link #mount()} or after {@link #unmount()}.
+     */
+    public IDirectory getRoot() throws UserFailureException;
     
-    public void save();
+    /**
+     * Unmounts this storage. May perform some finalization (e.g. make cached data persistent).
+     */
+    public void unmount();
 }
diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/AbstractNode.java b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/AbstractNode.java
index a07ca45f6a4..073cd777486 100644
--- a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/AbstractNode.java
+++ b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/AbstractNode.java
@@ -20,6 +20,7 @@ import java.io.File;
 
 import ch.systemsx.cisd.bds.storage.IDirectory;
 import ch.systemsx.cisd.bds.storage.INode;
+import ch.systemsx.cisd.common.exceptions.UserFailureException;
 
 /**
  * 
@@ -32,8 +33,14 @@ abstract class AbstractNode implements INode
 
     AbstractNode(File file)
     {
-        assert file != null : "Unspecified file";
-        assert file.exists() : "Non existing file " + file;
+        if (file == null)
+        {
+            throw new UserFailureException("Unspecified file");
+        }
+        if (file.exists() == false)
+        {
+            throw new UserFailureException("Non existing file " + file);
+        }
         this.nodeFile = file;
     }
     
diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/Directory.java b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/Directory.java
index 0ddf8d91dba..d3b54e72311 100644
--- a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/Directory.java
+++ b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/Directory.java
@@ -34,7 +34,10 @@ class Directory extends AbstractNode implements IDirectory
     Directory(java.io.File directory)
     {
         super(directory);
-        assert directory.isDirectory() : "Not a directory: " + directory.getAbsolutePath();
+        if (directory.isDirectory() == false)
+        {
+            throw new UserFailureException("Not a directory: " + directory.getAbsolutePath());
+        }
     }
     
     public INode getNode(String name)
diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/FileStorage.java b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/FileStorage.java
index d5e74a49073..4a51cd3692e 100644
--- a/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/FileStorage.java
+++ b/bds/source/java/ch/systemsx/cisd/bds/storage/filesystem/FileStorage.java
@@ -20,16 +20,24 @@ import java.io.File;
 
 import ch.systemsx.cisd.bds.storage.IDirectory;
 import ch.systemsx.cisd.bds.storage.IStorage;
+import ch.systemsx.cisd.common.exceptions.UserFailureException;
 
 /**
- * 
+ * Implementation of {@link IStorage} based on the file system.
  *
  * @author Franz-Josef Elmer
  */
 public class FileStorage implements IStorage
 {
-    private Directory root;
+    private final Directory root;
+    
+    private boolean mounted;
 
+    /**
+     * Creates an instance with the specified folder as the root directory.
+     * 
+     * @throws UserFailureException if <code>folder</code> does not exist or is not a directory in the file system.
+     */
     public FileStorage(File folder)
     {
         root = new Directory(folder);
@@ -37,15 +45,21 @@ public class FileStorage implements IStorage
     
     public IDirectory getRoot()
     {
+        if (mounted == false)
+        {
+            throw new UserFailureException("Can not get root of an unmounted storage.");
+        }
         return root;
     }
 
-    public void load()
+    public void mount()
     {
+        mounted = true;
     }
 
-    public void save()
+    public void unmount()
     {
+        mounted = false;
     }
 
 }
diff --git a/bds/source/java/ch/systemsx/cisd/bds/storage/hdf5/HDF5Storage.java b/bds/source/java/ch/systemsx/cisd/bds/storage/hdf5/HDF5Storage.java
index 55f2a8a9c07..78b523a5327 100644
--- a/bds/source/java/ch/systemsx/cisd/bds/storage/hdf5/HDF5Storage.java
+++ b/bds/source/java/ch/systemsx/cisd/bds/storage/hdf5/HDF5Storage.java
@@ -22,7 +22,7 @@ import ch.systemsx.cisd.bds.storage.IDirectory;
 import ch.systemsx.cisd.bds.storage.IStorage;
 
 /**
- * 
+ * Storage based on HDF5.
  *
  * @author Franz-Josef Elmer
  */
@@ -39,13 +39,13 @@ public class HDF5Storage implements IStorage
         return null;
     }
 
-    public void load()
+    public void mount()
     {
         // TODO Auto-generated method stub
 
     }
 
-    public void save()
+    public void unmount()
     {
         // TODO Auto-generated method stub
 
diff --git a/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/FileStorageTest.java b/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/FileStorageTest.java
index 386431cf05b..67d2f5b687d 100644
--- a/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/FileStorageTest.java
+++ b/bds/sourceTest/java/ch/systemsx/cisd/bds/storage/filesystem/FileStorageTest.java
@@ -16,9 +16,13 @@
 
 package ch.systemsx.cisd.bds.storage.filesystem;
 
+import static org.testng.AssertJUnit.assertEquals;
+
 import org.testng.AssertJUnit;
 import org.testng.annotations.Test;
 
+import ch.systemsx.cisd.common.exceptions.UserFailureException;
+
 /**
  * 
  *
@@ -30,6 +34,37 @@ public class FileStorageTest extends StorageTestCase
     public void testGetRoot()
     {
         FileStorage fileStorage = new FileStorage(TEST_DIR);
-        AssertJUnit.assertEquals(TEST_DIR.getName(), fileStorage.getRoot().getName());
+        fileStorage.mount();
+        assertEquals(TEST_DIR.getName(), fileStorage.getRoot().getName());
+    }
+    
+    @Test
+    public void testGetRootOfNeverMountedStorage()
+    {
+        FileStorage fileStorage = new FileStorage(TEST_DIR);
+        try
+        {
+            fileStorage.getRoot();
+            AssertJUnit.fail("UserFailureException because storage isn't mounted.");
+        } catch (UserFailureException e)
+        {
+            assertEquals("Can not get root of an unmounted storage.", e.getMessage());
+        }
+    }
+    
+    @Test
+    public void testGetRootOfUnMountedStorage()
+    {
+        FileStorage fileStorage = new FileStorage(TEST_DIR);
+        fileStorage.mount();
+        fileStorage.unmount();
+        try
+        {
+            fileStorage.getRoot();
+            AssertJUnit.fail("UserFailureException because storage isn't mounted.");
+        } catch (UserFailureException e)
+        {
+            assertEquals("Can not get root of an unmounted storage.", e.getMessage());
+        }
     }
 }
-- 
GitLab