Author: bdekruijff at gmail.com
Date: Thu Jan 13 14:50:15 2011
New Revision: 618

Log:
AMDATU-207 new lib with base impl for fs storage providers

Added:
   trunk/amdatu-libraries/fsstorage/   (props changed)
   trunk/amdatu-libraries/fsstorage/pom.xml
   trunk/amdatu-libraries/fsstorage/src/
   trunk/amdatu-libraries/fsstorage/src/main/
   trunk/amdatu-libraries/fsstorage/src/main/java/
   trunk/amdatu-libraries/fsstorage/src/main/java/org/
   trunk/amdatu-libraries/fsstorage/src/main/java/org/amdatu/
   trunk/amdatu-libraries/fsstorage/src/main/java/org/amdatu/libraries/
   
trunk/amdatu-libraries/fsstorage/src/main/java/org/amdatu/libraries/fsstorage/
   
trunk/amdatu-libraries/fsstorage/src/main/java/org/amdatu/libraries/fsstorage/FSStorageBase.java
   
trunk/amdatu-libraries/fsstorage/src/main/java/org/amdatu/libraries/fsstorage/FSStorageUtil.java
   
trunk/amdatu-libraries/fsstorage/src/main/java/org/amdatu/libraries/fsstorage/FSStoreBase.java
   
trunk/amdatu-libraries/fsstorage/src/main/java/org/amdatu/libraries/fsstorage/FSStoreEntity.java
   
trunk/amdatu-libraries/fsstorage/src/main/java/org/amdatu/libraries/fsstorage/FSStoreList.java
   trunk/amdatu-libraries/fsstorage/src/test/
   trunk/amdatu-libraries/fsstorage/src/test/java/
   trunk/amdatu-libraries/fsstorage/src/test/java/org/
   trunk/amdatu-libraries/fsstorage/src/test/java/org/amdatu/
   trunk/amdatu-libraries/fsstorage/src/test/java/org/amdatu/libraries/
   
trunk/amdatu-libraries/fsstorage/src/test/java/org/amdatu/libraries/fsstorage/
   
trunk/amdatu-libraries/fsstorage/src/test/java/org/amdatu/libraries/fsstorage/FSStorageTest.java
   
trunk/amdatu-libraries/fsstorage/src/test/java/org/amdatu/libraries/fsstorage/test/
   
trunk/amdatu-libraries/fsstorage/src/test/java/org/amdatu/libraries/fsstorage/test/FSTestEntity.java
   
trunk/amdatu-libraries/fsstorage/src/test/java/org/amdatu/libraries/fsstorage/test/FSTestStorage.java
   
trunk/amdatu-libraries/fsstorage/src/test/java/org/amdatu/libraries/fsstorage/test/FSTestStore.java
Modified:
   trunk/amdatu-libraries/pom.xml

Added: trunk/amdatu-libraries/fsstorage/pom.xml
==============================================================================
--- (empty file)
+++ trunk/amdatu-libraries/fsstorage/pom.xml    Thu Jan 13 14:50:15 2011
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.amdatu</groupId>
+    <artifactId>org.amdatu.libraries</artifactId>
+    <version>0.1.0-SNAPSHOT</version>
+  </parent>
+  <groupId>org.amdatu.libraries</groupId>
+  <artifactId>fsstorage</artifactId>
+  <packaging>jar</packaging>
+  <name>Amdatu Libraries - FSStorage</name>
+  <description>Base and utility classes for components that use simple 
filesystem storage</description>
+  <build>
+    
<finalName>${project.groupId}.${project.artifactId}-${platform.version}</finalName>
+  </build> 
+</project>

Added: 
trunk/amdatu-libraries/fsstorage/src/main/java/org/amdatu/libraries/fsstorage/FSStorageBase.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-libraries/fsstorage/src/main/java/org/amdatu/libraries/fsstorage/FSStorageBase.java
    Thu Jan 13 14:50:15 2011
@@ -0,0 +1,130 @@
+/*
+ Copyright (C) 2010 Amdatu.org
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.libraries.fsstorage;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Base generic implementation of the main storage component. Concrete 
implementations
+ * must extend this specifying the concrete types and implementing the factory 
method.
+ */
+public abstract class FSStorageBase<S extends FSStoreBase<E>, E extends 
FSStoreEntity> {
+
+    private final static String DEFAULT_ENTITYLIST_FILENAME = "entityList.ser";
+    private final static String DEFAULT_STOREFILE_PREFIX = "e_";
+    private final static String DEFAULT_STOREFILE_POSTFIX = ".ser";
+
+    private File m_dataDirectory;
+    private String m_entityListFilename;
+    private String m_storeFilePrefix;
+    private String m_storeFilepostfix;
+
+    private FSStoreList m_entityList;
+
+    public FSStorageBase(File dataDirectory) throws IOException {
+        this(dataDirectory, DEFAULT_ENTITYLIST_FILENAME, 
DEFAULT_STOREFILE_PREFIX, DEFAULT_STOREFILE_POSTFIX);
+    }
+
+    public FSStorageBase(File dataDirectory, String entityListFileName, String 
storeFilePrefix, String storeFilePostfix)
+        throws IOException {
+        m_entityListFilename = entityListFileName;
+        m_storeFilePrefix = storeFilePrefix;
+        m_storeFilepostfix = storeFilePostfix;
+        m_dataDirectory = dataDirectory;
+        m_entityList = new FSStoreList(new File(m_dataDirectory, 
m_entityListFilename));
+    }
+
+    public final File getDataDirectory() {
+        return m_dataDirectory;
+    }
+
+    public final String getEntityListFileName() {
+        return m_entityListFilename;
+    }
+
+    public final String getStoreFilePrefix() {
+        return m_storeFilePrefix;
+    }
+
+    public final String getStoreFilePostfix() {
+        return m_storeFilepostfix;
+    }
+
+    public final void addEntity(E entity) throws IOException {
+        S store = getFSStore(entity.getId());
+        E storedEntity = store.addEntity(entity);
+        if (storedEntity == null) {
+            m_entityList.addValue(entity.getId());
+        }
+        store.save();
+    }
+
+    public final E removeEntity(String entityId) throws IOException {
+        S store = getFSStore(entityId);
+        E storedEntity = store.removeEntity(entityId);
+        if (storedEntity != null) {
+            m_entityList.removeValue(entityId);
+            store.save();
+        }
+        return storedEntity;
+    }
+
+    public final E getEntity(String entityId) throws IOException {
+        S store = getFSStore(entityId);
+        return store.getEntity(entityId);
+    }
+
+    public final List<E> getAll() throws IOException {
+        List<String> entityIdList = m_entityList.getAll();
+        final List<E> entityList = new LinkedList<E>();
+        for (String entityId : entityIdList) {
+            entityList.add(getEntity(entityId));
+        }
+        return entityList;
+    }
+
+    /**
+     * Default implementation of returning an FSStore for a specific
+     * entity.
+     */
+    protected S getFSStore(String entityId) throws IOException {
+        return newFSStore(getStoreFile(entityId));
+    }
+
+    /**
+     * Determine the filename for this entity's store relative to the data 
directory.
+     * This default hashing strategy uses a directory depth of one with mod 50 
hashing
+     * over the entity id hashcode.
+     */
+    protected File getStoreFile(String entityId) throws IOException {
+        int hash = Math.abs(entityId.hashCode());
+        String subdirName = "" + (hash % 50);
+        final File subdirFile = new File(m_dataDirectory, subdirName);
+        if (!subdirFile.exists()) {
+            subdirFile.mkdir();
+        }
+        return new File(subdirFile, m_storeFilePrefix + hash + 
m_storeFilepostfix);
+    }
+
+    /**
+     * Subclasses must provide an concrete instance of the store 
implementation.
+     */
+    protected abstract S newFSStore(File file) throws IOException;
+}

Added: 
trunk/amdatu-libraries/fsstorage/src/main/java/org/amdatu/libraries/fsstorage/FSStorageUtil.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-libraries/fsstorage/src/main/java/org/amdatu/libraries/fsstorage/FSStorageUtil.java
    Thu Jan 13 14:50:15 2011
@@ -0,0 +1,123 @@
+/*
+    Copyright (C) 2010 Amdatu.org
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program 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 General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.libraries.fsstorage;
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Some generic utility methods for assiting implementations with 
+ * common marshal / unmarshall tasks.
+ */
+public final class FSStorageUtil {
+
+    public static String readString(final ObjectInputStream ois) throws 
IOException {
+        final int keyLength = ois.readInt();
+        final byte[] keyBytes = new byte[keyLength];
+        ois.readFully(keyBytes);
+        return new String(keyBytes, "utf-8");
+    }
+
+    public static List<String> readList(final ObjectInputStream ois) throws 
IOException {
+        final int listLength = ois.readInt();
+        List<String> list = new ArrayList<String>(listLength);
+        for (int i = 0; i < listLength; i++) {
+            list.add(readString(ois));
+        }
+        return list;
+    }
+
+    public static Map<String, String> readProperties(final ObjectInputStream 
ois) throws IOException {
+        final int numberOfProperties = ois.readInt();
+        if (numberOfProperties == 0) {
+            return null;
+        }
+        final Map<String, String> properties = new HashMap<String, String>();
+        for (int i = 0; i < numberOfProperties; i++) {
+            final String key = readString(ois);
+            final String value = readString(ois);
+            properties.put(key, value);
+        }
+        return properties;
+    }
+
+    public static void writeString(final ObjectOutputStream oos, final String 
value) throws IOException {
+        String mvalue = value;
+        if (mvalue == null) {
+            mvalue = "";
+        }
+        final byte[] bytes = mvalue.getBytes("utf-8");
+        oos.writeInt(bytes.length);
+        oos.write(bytes);
+    }
+
+    public static void writeList(final ObjectOutputStream oos, final 
List<String> values) throws IOException {
+        oos.writeInt(values.size());
+        for (String value : values) {
+            writeString(oos, value);
+        }
+    }
+
+    public static void writeProperties(final ObjectOutputStream oos, final 
Map<String, String> properties)
+        throws IOException {
+        if (properties == null) {
+            oos.writeInt(0);
+            return;
+        }
+        oos.writeInt(properties.size());
+        for (final String key : properties.keySet()) {
+            writeString(oos, key);
+            writeString(oos, properties.get(key));
+        }
+    }
+
+    public static void closeInputStreamsSafely(ObjectInputStream ois, 
FileInputStream fis) throws IOException {
+        try {
+            if (ois != null) {
+                ois.close();
+            }
+        }
+        catch (IOException e) {
+            if (fis != null) {
+                fis.close();
+            }
+            throw e;
+        }
+    }
+
+    public static void closeOutputStreamsSafely(ObjectOutputStream oos, 
FileOutputStream fos) throws IOException {
+        try {
+            if (oos != null) {
+                oos.close();
+            }
+        }
+        catch (IOException e) {
+            if (fos != null) {
+                fos.close();
+            }
+            throw e;
+        }
+    }
+
+}

Added: 
trunk/amdatu-libraries/fsstorage/src/main/java/org/amdatu/libraries/fsstorage/FSStoreBase.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-libraries/fsstorage/src/main/java/org/amdatu/libraries/fsstorage/FSStoreBase.java
      Thu Jan 13 14:50:15 2011
@@ -0,0 +1,108 @@
+/*
+    Copyright (C) 2010 Amdatu.org
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program 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 General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.libraries.fsstorage;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.UnsupportedEncodingException;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Base implementation of a persistent store on disk that holds 0 or more
+ * entities. Concrete implementations must extend this to implement their
+ * marshal / unmarshal strategy.
+ */
+public abstract class FSStoreBase<T extends FSStoreEntity> {
+
+    private final File m_file;
+    private final Map<String, T> m_entities;
+
+    public FSStoreBase(File file) throws IOException {
+        m_file = file;
+        m_entities = new HashMap<String, T>();
+        readEntities();
+    }
+
+    public T addEntity(final T entity) {
+        return m_entities.put(entity.getId(), entity);
+    }
+
+    public T removeEntity(final String entityId) {
+        return m_entities.remove(entityId);
+    }
+
+    public T getEntity(final String entityId) {
+        return m_entities.get(entityId);
+    }
+
+    public void save() throws IOException {
+        writeEntities();
+    }
+
+    private void readEntities() throws IOException {
+        if (!m_file.exists()) {
+            return;
+        }
+        m_entities.clear();
+        FileInputStream fis = null;
+        ObjectInputStream ois = null;
+        try {
+            fis = new FileInputStream(m_file);
+            ois = new ObjectInputStream(fis);
+            final int numberOfTenants = ois.readInt();
+            for (int i = 0; i < numberOfTenants; i++) {
+                final T entity = readEntity(ois);
+                m_entities.put(entity.getId(), entity);
+            }
+        }
+        finally {
+            FSStorageUtil.closeInputStreamsSafely(ois, fis);
+        }
+    }
+
+    private void writeEntities() throws IOException {
+        if ((m_entities == null || m_entities.size() == 0) && m_file.exists()) 
{
+            m_file.delete();
+            return;
+        }
+        FileOutputStream fos = null;
+        ObjectOutputStream oos = null;
+        try {
+            fos = new FileOutputStream(m_file);
+            oos = new ObjectOutputStream(fos);
+            oos.writeInt(m_entities.size());
+            for (T entity : m_entities.values()) {
+                writeEntity(oos, entity);
+            }
+            oos.flush();
+        }
+        finally {
+            FSStorageUtil.closeOutputStreamsSafely(oos, fos);
+        }
+    }
+
+    protected abstract T readEntity(ObjectInputStream ois) throws IOException,
+        UnsupportedEncodingException;
+
+    protected abstract void writeEntity(ObjectOutputStream oos, T entity) 
throws UnsupportedEncodingException,
+        IOException;
+}

Added: 
trunk/amdatu-libraries/fsstorage/src/main/java/org/amdatu/libraries/fsstorage/FSStoreEntity.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-libraries/fsstorage/src/main/java/org/amdatu/libraries/fsstorage/FSStoreEntity.java
    Thu Jan 13 14:50:15 2011
@@ -0,0 +1,26 @@
+/*
+    Copyright (C) 2010 Amdatu.org
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program 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 General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.libraries.fsstorage;
+
+/**
+ * Generic interface for entities that can be stored in an FSStore. Concrete
+ * implementations must implement this.
+ * 
+ */
+public interface FSStoreEntity {
+    String getId();
+}

Added: 
trunk/amdatu-libraries/fsstorage/src/main/java/org/amdatu/libraries/fsstorage/FSStoreList.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-libraries/fsstorage/src/main/java/org/amdatu/libraries/fsstorage/FSStoreList.java
      Thu Jan 13 14:50:15 2011
@@ -0,0 +1,95 @@
+/*
+ Copyright (C) 2010 Amdatu.org
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.libraries.fsstorage;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Implementation of a persistent list of Strings on disk used for keeping
+ * track of the entities in a a store.
+ */
+public final class FSStoreList {
+
+    private final File m_file;
+    private List<String> m_stringList;
+
+    public FSStoreList(final File file) throws IOException {
+        m_file = file;
+        m_stringList = new LinkedList<String>();
+        readStringList();
+    }
+
+    public List<String> getAll() throws IOException {
+        return new LinkedList<String>(m_stringList);
+    }
+
+    public void addValue(final String value) throws IOException {
+        if (!m_stringList.contains(value)) {
+            m_stringList.add(value);
+            writeValueList();
+        }
+    }
+
+    public void removeValue(final String value) throws IOException {
+        if (m_stringList.contains(value)) {
+            m_stringList.remove(value);
+            writeValueList();
+        }
+    }
+
+    private void readStringList() throws IOException {
+        if (!m_file.exists()) {
+            m_stringList.clear();
+            return;
+        }
+        FileInputStream fis = null;
+        ObjectInputStream ois = null;
+        try {
+            fis = new FileInputStream(m_file);
+            ois = new ObjectInputStream(fis);
+            m_stringList = FSStorageUtil.readList(ois);
+        }
+        finally {
+            FSStorageUtil.closeInputStreamsSafely(ois, fis);
+        }
+    }
+
+    private void writeValueList() throws IOException {
+        if (m_stringList.size() == 0 && m_file.exists()) {
+            m_file.delete();
+            return;
+        }
+        FileOutputStream fos = null;
+        ObjectOutputStream oos = null;
+        try {
+            fos = new FileOutputStream(m_file);
+            oos = new ObjectOutputStream(fos);
+            FSStorageUtil.writeList(oos, m_stringList);
+            oos.flush();
+        }
+        finally {
+            FSStorageUtil.closeOutputStreamsSafely(oos, fos);
+        }
+    }
+}

Added: 
trunk/amdatu-libraries/fsstorage/src/test/java/org/amdatu/libraries/fsstorage/FSStorageTest.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-libraries/fsstorage/src/test/java/org/amdatu/libraries/fsstorage/FSStorageTest.java
    Thu Jan 13 14:50:15 2011
@@ -0,0 +1,110 @@
+/*
+    Copyright (C) 2010 Amdatu.org
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program 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 General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.libraries.fsstorage;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Random;
+
+import org.amdatu.libraries.fsstorage.test.FSTestEntity;
+import org.amdatu.libraries.fsstorage.test.FSTestStorage;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TestName;
+
+public class FSStorageTest {
+
+    private final static String JAVA_IO_TMPDIR = 
System.getProperty("java.io.tmpdir");
+
+    private static File m_absoluteTestRootDirectory;
+    private static String m_relativeTestRootDirectory;
+
+    @Rule
+    public TestName m_testName = new TestName();
+
+    private File m_testDirectory;
+    private FSTestStorage m_testStorage;
+
+    @BeforeClass
+    public static void setUpOnce() throws IOException {
+        Random rand = new Random();
+        int randomInt = 1 + Math.abs(rand.nextInt());
+        m_relativeTestRootDirectory = FSStorageTest.class.getSimpleName() + 
"_" + randomInt;
+        m_absoluteTestRootDirectory =
+            new File(JAVA_IO_TMPDIR + File.separator + 
m_relativeTestRootDirectory);
+        m_absoluteTestRootDirectory.mkdirs();
+    }
+
+    @Before
+    public void setUp() throws IOException {
+        m_testDirectory = new File(m_absoluteTestRootDirectory, 
m_testName.getMethodName());
+        m_testDirectory.mkdir();
+        m_testStorage = new FSTestStorage(m_testDirectory);
+    }
+
+    @Test
+    public void testTheTestStoreWithOneEntity() throws IOException {
+
+        FSTestEntity e1 = new FSTestEntity();
+        m_testStorage.addEntity(e1);
+        Assert.assertEquals(e1, m_testStorage.getEntity(e1.getId()));
+
+        e1 = m_testStorage.getEntity(e1.getId());
+        Assert.assertNotNull(e1);
+
+        List<FSTestEntity> eList = m_testStorage.getAll();
+        Assert.assertEquals(1, eList.size());
+
+        e1 = m_testStorage.removeEntity(e1.getId());
+        Assert.assertNotNull(e1);
+
+        e1 = m_testStorage.getEntity(e1.getId());
+        Assert.assertNull(e1);
+
+        eList = m_testStorage.getAll();
+        Assert.assertEquals(0, eList.size());
+
+    }
+
+    @Test
+    public void testTheTestStoreWithMultipleEntity() throws IOException {
+
+        FSTestEntity e1 = new FSTestEntity();
+        m_testStorage.addEntity(e1);
+        Assert.assertEquals(e1, m_testStorage.getEntity(e1.getId()));
+
+        FSTestEntity e3 = new FSTestEntity();
+        m_testStorage.addEntity(e3);
+        Assert.assertEquals(e3, m_testStorage.getEntity(e3.getId()));
+
+        FSTestEntity e2 = new FSTestEntity();
+        m_testStorage.addEntity(e2);
+        Assert.assertEquals(e2, m_testStorage.getEntity(e2.getId()));
+
+        List<FSTestEntity> eList = m_testStorage.getAll();
+        Assert.assertEquals(3, eList.size());
+
+        Assert.assertEquals(e1, m_testStorage.removeEntity(e1.getId()));
+
+        eList = m_testStorage.getAll();
+        Assert.assertEquals(2, eList.size());
+    }
+}

Added: 
trunk/amdatu-libraries/fsstorage/src/test/java/org/amdatu/libraries/fsstorage/test/FSTestEntity.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-libraries/fsstorage/src/test/java/org/amdatu/libraries/fsstorage/test/FSTestEntity.java
        Thu Jan 13 14:50:15 2011
@@ -0,0 +1,52 @@
+/*
+    Copyright (C) 2010 Amdatu.org
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program 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 General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.libraries.fsstorage.test;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+import org.amdatu.libraries.fsstorage.FSStoreEntity;
+
+public class FSTestEntity implements FSStoreEntity {
+
+    private final String m_id;
+    private final Map<String, String> m_properties;
+
+    public FSTestEntity() {
+        m_id = UUID.randomUUID().toString();
+        m_properties = new HashMap<String, String>();
+    }
+
+    public FSTestEntity(String id, Map<String, String> properties) {
+        m_id = id;
+        m_properties = properties;
+    }
+
+    public String getId() {
+        return m_id;
+    }
+
+    public Map<String, String> getProperties() {
+        return m_properties;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        return m_id.equals(((FSTestEntity) obj).getId());
+    }
+}
\ No newline at end of file

Added: 
trunk/amdatu-libraries/fsstorage/src/test/java/org/amdatu/libraries/fsstorage/test/FSTestStorage.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-libraries/fsstorage/src/test/java/org/amdatu/libraries/fsstorage/test/FSTestStorage.java
       Thu Jan 13 14:50:15 2011
@@ -0,0 +1,38 @@
+/*
+    Copyright (C) 2010 Amdatu.org
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program 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 General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.libraries.fsstorage.test;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.amdatu.libraries.fsstorage.FSStorageBase;
+
+public class FSTestStorage extends FSStorageBase<FSTestStore, FSTestEntity> {
+
+    private static final String ENTITYLIST_FILENAME = "testIdList.ser";
+    private static final String STORAGEFILE_PREFIX = "t_";
+    private static final String STORAGEFILE_POSTFIX = ".ser";
+
+    public FSTestStorage(File dataDirectory) throws IOException {
+        super(dataDirectory, ENTITYLIST_FILENAME, STORAGEFILE_PREFIX, 
STORAGEFILE_POSTFIX);
+    }
+
+    @Override
+    protected FSTestStore newFSStore(File file) throws IOException {
+        return new FSTestStore(file);
+    }
+}
\ No newline at end of file

Added: 
trunk/amdatu-libraries/fsstorage/src/test/java/org/amdatu/libraries/fsstorage/test/FSTestStore.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-libraries/fsstorage/src/test/java/org/amdatu/libraries/fsstorage/test/FSTestStore.java
 Thu Jan 13 14:50:15 2011
@@ -0,0 +1,47 @@
+/*
+    Copyright (C) 2010 Amdatu.org
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program 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 General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.libraries.fsstorage.test;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.UnsupportedEncodingException;
+import java.util.Map;
+
+import org.amdatu.libraries.fsstorage.FSStorageUtil;
+import org.amdatu.libraries.fsstorage.FSStoreBase;
+
+public final class FSTestStore extends FSStoreBase<FSTestEntity> {
+
+    public FSTestStore(File file) throws IOException {
+        super(file);
+    }
+
+    protected FSTestEntity readEntity(ObjectInputStream ois) throws 
IOException,
+        UnsupportedEncodingException {
+        String id = FSStorageUtil.readString(ois);
+        Map<String, String> props = FSStorageUtil.readProperties(ois);
+        return new FSTestEntity(id, props);
+    }
+
+    protected void writeEntity(ObjectOutputStream oos, FSTestEntity entity) 
throws UnsupportedEncodingException,
+        IOException {
+        FSStorageUtil.writeString(oos, entity.getId());
+        FSStorageUtil.writeProperties(oos, entity.getProperties());
+    }
+}

Modified: trunk/amdatu-libraries/pom.xml
==============================================================================
--- trunk/amdatu-libraries/pom.xml      (original)
+++ trunk/amdatu-libraries/pom.xml      Thu Jan 13 14:50:15 2011
@@ -15,6 +15,7 @@
   
   <modules>
     <module>utilities</module>
+    <module>fsstorage</module>
   </modules>
   
 </project>
\ No newline at end of file

Reply via email to