Author: [email protected]
Date: Wed May  2 11:57:36 2012
New Revision: 2307

Log:
[AMDATUAUTH-153] Renamed package

Added:
   
trunk/amdatu-auth/tools/fsstore/src/main/java/org/amdatu/auth/tools/fsstore/FSStorageBase.java
   
trunk/amdatu-auth/tools/fsstore/src/main/java/org/amdatu/auth/tools/fsstore/FSStoreBase.java
   
trunk/amdatu-auth/tools/fsstore/src/main/java/org/amdatu/auth/tools/fsstore/FSStoreEntity.java
   
trunk/amdatu-auth/tools/fsstore/src/main/java/org/amdatu/auth/tools/fsstore/FSStoreList.java
   
trunk/amdatu-auth/tools/fsstore/src/main/java/org/amdatu/auth/tools/fsstore/FSStoreUtil.java
   trunk/amdatu-auth/tools/fsstore/src/test/java/org/amdatu/auth/tools/
   trunk/amdatu-auth/tools/fsstore/src/test/java/org/amdatu/auth/tools/fsstore/
   
trunk/amdatu-auth/tools/fsstore/src/test/java/org/amdatu/auth/tools/fsstore/FSStoreTest.java
   
trunk/amdatu-auth/tools/fsstore/src/test/java/org/amdatu/auth/tools/fsstore/test/
   
trunk/amdatu-auth/tools/fsstore/src/test/java/org/amdatu/auth/tools/fsstore/test/FSTestEntity.java
   
trunk/amdatu-auth/tools/fsstore/src/test/java/org/amdatu/auth/tools/fsstore/test/FSTestStorage.java
   
trunk/amdatu-auth/tools/fsstore/src/test/java/org/amdatu/auth/tools/fsstore/test/FSTestStore.java
Modified:
   
trunk/amdatu-auth/auth-stores/fs-store-consumer/src/main/java/org/amdatu/auth/oauth/consumerregistry/fs/internal/FSConsumerEntity.java
   
trunk/amdatu-auth/auth-stores/fs-store-consumer/src/main/java/org/amdatu/auth/oauth/consumerregistry/fs/internal/FSConsumerStorage.java
   
trunk/amdatu-auth/auth-stores/fs-store-consumer/src/main/java/org/amdatu/auth/oauth/consumerregistry/fs/internal/FSConsumerStore.java
   
trunk/amdatu-auth/tools/fsstore/src/main/java/org/amdatu/auth/fsstore/FSStorageBase.java
   
trunk/amdatu-auth/tools/fsstore/src/main/java/org/amdatu/auth/fsstore/FSStoreBase.java
   
trunk/amdatu-auth/tools/fsstore/src/main/java/org/amdatu/auth/fsstore/FSStoreEntity.java
   
trunk/amdatu-auth/tools/fsstore/src/main/java/org/amdatu/auth/fsstore/FSStoreList.java
   
trunk/amdatu-auth/tools/fsstore/src/main/java/org/amdatu/auth/fsstore/FSStoreUtil.java
   trunk/amdatu-auth/tools/fsstore/src/test/java/org/amdatu/auth/fsstore/

Modified: 
trunk/amdatu-auth/auth-stores/fs-store-consumer/src/main/java/org/amdatu/auth/oauth/consumerregistry/fs/internal/FSConsumerEntity.java
==============================================================================
--- 
trunk/amdatu-auth/auth-stores/fs-store-consumer/src/main/java/org/amdatu/auth/oauth/consumerregistry/fs/internal/FSConsumerEntity.java
      (original)
+++ 
trunk/amdatu-auth/auth-stores/fs-store-consumer/src/main/java/org/amdatu/auth/oauth/consumerregistry/fs/internal/FSConsumerEntity.java
      Wed May  2 11:57:36 2012
@@ -15,8 +15,8 @@
  */
 package org.amdatu.auth.oauth.consumerregistry.fs.internal;
 
-import org.amdatu.auth.fsstore.FSStoreEntity;
 import org.amdatu.auth.oauth.api.OAuthServiceConsumer;
+import org.amdatu.auth.tools.fsstore.FSStoreEntity;
 
 import java.util.ArrayList;
 import java.util.List;

Modified: 
trunk/amdatu-auth/auth-stores/fs-store-consumer/src/main/java/org/amdatu/auth/oauth/consumerregistry/fs/internal/FSConsumerStorage.java
==============================================================================
--- 
trunk/amdatu-auth/auth-stores/fs-store-consumer/src/main/java/org/amdatu/auth/oauth/consumerregistry/fs/internal/FSConsumerStorage.java
     (original)
+++ 
trunk/amdatu-auth/auth-stores/fs-store-consumer/src/main/java/org/amdatu/auth/oauth/consumerregistry/fs/internal/FSConsumerStorage.java
     Wed May  2 11:57:36 2012
@@ -15,7 +15,7 @@
  */
 package org.amdatu.auth.oauth.consumerregistry.fs.internal;
 
-import org.amdatu.auth.fsstore.FSStorageBase;
+import org.amdatu.auth.tools.fsstore.FSStorageBase;
 
 import java.io.File;
 import java.io.IOException;

Modified: 
trunk/amdatu-auth/auth-stores/fs-store-consumer/src/main/java/org/amdatu/auth/oauth/consumerregistry/fs/internal/FSConsumerStore.java
==============================================================================
--- 
trunk/amdatu-auth/auth-stores/fs-store-consumer/src/main/java/org/amdatu/auth/oauth/consumerregistry/fs/internal/FSConsumerStore.java
       (original)
+++ 
trunk/amdatu-auth/auth-stores/fs-store-consumer/src/main/java/org/amdatu/auth/oauth/consumerregistry/fs/internal/FSConsumerStore.java
       Wed May  2 11:57:36 2012
@@ -15,8 +15,8 @@
  */
 package org.amdatu.auth.oauth.consumerregistry.fs.internal;
 
-import org.amdatu.auth.fsstore.FSStoreBase;
-import org.amdatu.auth.fsstore.FSStoreUtil;
+import org.amdatu.auth.tools.fsstore.FSStoreBase;
+import org.amdatu.auth.tools.fsstore.FSStoreUtil;
 
 import java.io.File;
 import java.io.IOException;

Added: 
trunk/amdatu-auth/tools/fsstore/src/main/java/org/amdatu/auth/tools/fsstore/FSStorageBase.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-auth/tools/fsstore/src/main/java/org/amdatu/auth/tools/fsstore/FSStorageBase.java
      Wed May  2 11:57:36 2012
@@ -0,0 +1,133 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * 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 org.amdatu.auth.tools.fsstore;
+
+
+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.
+ * 
+ * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
+ */
+public abstract class FSStorageBase<S extends FSStoreBase<E>, E extends 
FSStoreEntity> {
+
+    private static final String DEFAULT_ENTITYLIST_FILENAME = "entityList.ser";
+    private static final String DEFAULT_STOREFILE_PREFIX = "e_";
+    private static final 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(final File dataDirectory) throws IOException {
+        this(dataDirectory, DEFAULT_ENTITYLIST_FILENAME, 
DEFAULT_STOREFILE_PREFIX, DEFAULT_STOREFILE_POSTFIX);
+    }
+
+    public FSStorageBase(final File dataDirectory, final String 
entityListFileName, final String storeFilePrefix,
+        final 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 synchronized void addEntity(final 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 synchronized E removeEntity(final 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 synchronized E getEntity(final String entityId) throws 
IOException {
+        S store = getFSStore(entityId);
+        return store.getEntity(entityId);
+    }
+
+    public final synchronized 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(final 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(final 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-auth/tools/fsstore/src/main/java/org/amdatu/auth/tools/fsstore/FSStoreBase.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-auth/tools/fsstore/src/main/java/org/amdatu/auth/tools/fsstore/FSStoreBase.java
        Wed May  2 11:57:36 2012
@@ -0,0 +1,107 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * 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 org.amdatu.auth.tools.fsstore;
+
+
+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.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.
+ * 
+ * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
+ */
+public abstract class FSStoreBase<T extends FSStoreEntity> {
+
+    private final File m_file;
+    private final Map<String, T> m_entities;
+
+    public FSStoreBase(final 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 {
+            FSStoreUtil.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 {
+            FSStoreUtil.closeOutputStreamsSafely(oos, fos);
+        }
+    }
+
+    protected abstract T readEntity(ObjectInputStream ois) throws IOException;
+
+    protected abstract void writeEntity(ObjectOutputStream oos, T entity) 
throws IOException;
+}

Added: 
trunk/amdatu-auth/tools/fsstore/src/main/java/org/amdatu/auth/tools/fsstore/FSStoreEntity.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-auth/tools/fsstore/src/main/java/org/amdatu/auth/tools/fsstore/FSStoreEntity.java
      Wed May  2 11:57:36 2012
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * 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 org.amdatu.auth.tools.fsstore;
+
+/**
+ * Generic interface for entities that can be stored in an FSStore. Concrete
+ * implementations must implement this.
+ * 
+ * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
+ */
+public interface FSStoreEntity {
+    String getId();
+}

Added: 
trunk/amdatu-auth/tools/fsstore/src/main/java/org/amdatu/auth/tools/fsstore/FSStoreList.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-auth/tools/fsstore/src/main/java/org/amdatu/auth/tools/fsstore/FSStoreList.java
        Wed May  2 11:57:36 2012
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * 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 org.amdatu.auth.tools.fsstore;
+
+
+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.
+ * 
+ * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
+ */
+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 = FSStoreUtil.readList(ois);
+        }
+        finally {
+            FSStoreUtil.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);
+            FSStoreUtil.writeList(oos, m_stringList);
+            oos.flush();
+        }
+        finally {
+            FSStoreUtil.closeOutputStreamsSafely(oos, fos);
+        }
+    }
+}

Added: 
trunk/amdatu-auth/tools/fsstore/src/main/java/org/amdatu/auth/tools/fsstore/FSStoreUtil.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-auth/tools/fsstore/src/main/java/org/amdatu/auth/tools/fsstore/FSStoreUtil.java
        Wed May  2 11:57:36 2012
@@ -0,0 +1,193 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * 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 org.amdatu.auth.tools.fsstore;
+
+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.Dictionary;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Some generic (but limited!) utility methods for assiting implementations 
with
+ * common marshal / unmarshall tasks.
+ * 
+ * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
+ */
+public final class FSStoreUtil {
+
+    private FSStoreUtil() {
+    }
+
+    public static byte[] readBytes(final ObjectInputStream ois) throws 
IOException {
+        final int bytesLength = ois.readInt();
+        final byte[] bytes = new byte[bytesLength];
+        ois.readFully(bytes);
+        return bytes;
+    }
+
+    public static Dictionary<String, Object> readDictionary(final 
ObjectInputStream ois) throws IOException {
+        final int numberOfEntries = ois.readInt();
+        if (numberOfEntries == 0) {
+            return null;
+        }
+        final Dictionary<String, Object> dictionary = new Hashtable<String, 
Object>();
+        for (int j = 0; j < numberOfEntries; j++) {
+            final String key = FSStoreUtil.readString(ois);
+            final int type = ois.readInt();
+            switch (type) {
+                case 0:
+                    byte[] byteValue = FSStoreUtil.readBytes(ois);
+                    dictionary.put(key, byteValue);
+                    break;
+                case 1:
+                    String stringValue = FSStoreUtil.readString(ois);
+                    dictionary.put(key, stringValue);
+                    break;
+                default:
+                    break;
+            }
+        }
+        return dictionary;
+    }
+
+    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 writeBytes(final ObjectOutputStream oos, final byte[] 
bytes) throws IOException {
+        oos.writeInt(bytes.length);
+        oos.write(bytes);
+    }
+
+    public static void writeDictionary(final ObjectOutputStream oos, final 
Dictionary<String, Object> dictionary)
+        throws IOException {
+        if (dictionary == null) {
+            oos.writeInt(0);
+        }
+        else {
+            oos.writeInt(dictionary.size());
+            Enumeration<String> keys = dictionary.keys();
+            while (keys.hasMoreElements()) {
+                String key = (String) keys.nextElement();
+                FSStoreUtil.writeString(oos, key);
+                Object value = dictionary.get(key);
+                if (value instanceof byte[]) {
+                    oos.writeInt(0);
+                    FSStoreUtil.writeBytes(oos, (byte[]) value);
+                }
+                else if (value instanceof String) {
+                    oos.writeInt(1);
+                    FSStoreUtil.writeString(oos, (String) value);
+                }
+            }
+        }
+    }
+
+    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(final ObjectInputStream ois, 
final FileInputStream fis)
+        throws IOException {
+        try {
+            if (ois != null) {
+                ois.close();
+            }
+        }
+        catch (IOException e) {
+            if (fis != null) {
+                fis.close();
+            }
+            throw e;
+        }
+    }
+
+    public static void closeOutputStreamsSafely(final ObjectOutputStream oos, 
final FileOutputStream fos)
+        throws IOException {
+        try {
+            if (oos != null) {
+                oos.close();
+            }
+        }
+        catch (IOException e) {
+            if (fos != null) {
+                fos.close();
+            }
+            throw e;
+        }
+    }
+
+}

Added: 
trunk/amdatu-auth/tools/fsstore/src/test/java/org/amdatu/auth/tools/fsstore/FSStoreTest.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-auth/tools/fsstore/src/test/java/org/amdatu/auth/tools/fsstore/FSStoreTest.java
        Wed May  2 11:57:36 2012
@@ -0,0 +1,160 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * 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 org.amdatu.auth.tools.fsstore;
+
+import org.amdatu.auth.tools.fsstore.test.FSTestEntity;
+import org.amdatu.auth.tools.fsstore.test.FSTestStorage;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+
+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;
+
+/**
+ * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
+ */
+public class FSStoreTest {
+
+    private static final String JAVA_IO_TMPDIR = 
System.getProperty("java.io.tmpdir");
+
+    private static File ABS_TEST_ROOT_DIR;
+    private static String REL_TEST_ROOT_DIR;
+
+    // @checkstyle:off
+    @Rule
+    public TestName m_testName = new TestName();
+    // @checkstyle:on
+
+    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());
+        REL_TEST_ROOT_DIR = FSStoreTest.class.getSimpleName() + "_" + 
randomInt;
+        ABS_TEST_ROOT_DIR =
+            new File(JAVA_IO_TMPDIR + File.separator + REL_TEST_ROOT_DIR);
+        ABS_TEST_ROOT_DIR.mkdirs();
+    }
+
+    @Before
+    public void setUp() throws IOException {
+        m_testDirectory = new File(ABS_TEST_ROOT_DIR, 
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());
+    }
+
+    @Test
+    public void testTheTestStoreWithProperties() throws IOException {
+
+        Map<String, String> props = new HashMap<String, String>();
+        props.put("key1", "value1");
+        props.put("key2", "value2");
+
+        FSTestEntity e1 = new FSTestEntity("123", props);
+        m_testStorage.addEntity(e1);
+
+        e1 = m_testStorage.getEntity(e1.getId());
+
+        props = e1.getProperties();
+        Assert.assertNotNull(props);
+        Assert.assertEquals(2, props.size());
+        Assert.assertEquals("value1", props.get("key1"));
+        Assert.assertEquals("value2", props.get("key2"));
+    }
+
+    /**
+     * Testing behavior on tenantEntities with same hashcode() for the id. 
This is based on knowledge of the
+     * implementation but as it is a likely pitfall let's test it anyway to 
catch future mistakes.
+     * 
+     * @throws TenantStorageException
+     */
+    @Test
+    public void testWithEqualHashcodes() throws IOException {
+
+        // Assert the reason for this test
+        Assert.assertEquals("BB".hashCode(), "Aa".hashCode());
+
+        FSTestEntity e1 = new FSTestEntity("BB", null);
+        m_testStorage.addEntity(e1);
+        Assert.assertEquals(e1, m_testStorage.getEntity(e1.getId()));
+
+        FSTestEntity e3 = new FSTestEntity("Aa", null);
+        m_testStorage.addEntity(e3);
+        Assert.assertEquals(e3, m_testStorage.getEntity(e3.getId()));
+
+        List<FSTestEntity> eList = m_testStorage.getAll();
+        Assert.assertEquals(2, eList.size());
+    }
+}

Added: 
trunk/amdatu-auth/tools/fsstore/src/test/java/org/amdatu/auth/tools/fsstore/test/FSTestEntity.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-auth/tools/fsstore/src/test/java/org/amdatu/auth/tools/fsstore/test/FSTestEntity.java
  Wed May  2 11:57:36 2012
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * 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 org.amdatu.auth.tools.fsstore.test;
+
+import org.amdatu.auth.tools.fsstore.FSStoreEntity;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+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(final String id, final 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 int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result;
+        if (m_id != null) {
+            result += m_id.hashCode();
+        }
+        return result;
+    }
+
+    @Override
+    public boolean equals(final Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        FSTestEntity other = (FSTestEntity) obj;
+        if (m_id == null) {
+            if (other.m_id != null) {
+                return false;
+            }
+        }
+        else if (!m_id.equals(other.m_id)) {
+            return false;
+        }
+        return true;
+    }
+}

Added: 
trunk/amdatu-auth/tools/fsstore/src/test/java/org/amdatu/auth/tools/fsstore/test/FSTestStorage.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-auth/tools/fsstore/src/test/java/org/amdatu/auth/tools/fsstore/test/FSTestStorage.java
 Wed May  2 11:57:36 2012
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * 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 org.amdatu.auth.tools.fsstore.test;
+
+import org.amdatu.auth.tools.fsstore.FSStorageBase;
+
+import java.io.File;
+import java.io.IOException;
+
+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(final File dataDirectory) throws IOException {
+        super(dataDirectory, ENTITYLIST_FILENAME, STORAGEFILE_PREFIX, 
STORAGEFILE_POSTFIX);
+    }
+
+    @Override
+    protected FSTestStore newFSStore(final File file) throws IOException {
+        return new FSTestStore(file);
+    }
+}

Added: 
trunk/amdatu-auth/tools/fsstore/src/test/java/org/amdatu/auth/tools/fsstore/test/FSTestStore.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-auth/tools/fsstore/src/test/java/org/amdatu/auth/tools/fsstore/test/FSTestStore.java
   Wed May  2 11:57:36 2012
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * 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 org.amdatu.auth.tools.fsstore.test;
+
+import org.amdatu.auth.tools.fsstore.FSStoreBase;
+import org.amdatu.auth.tools.fsstore.FSStoreUtil;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.Map;
+
+public final class FSTestStore extends FSStoreBase<FSTestEntity> {
+
+    public FSTestStore(final File file) throws IOException {
+        super(file);
+    }
+
+    protected FSTestEntity readEntity(final ObjectInputStream ois) throws 
IOException {
+        String id = FSStoreUtil.readString(ois);
+        Map<String, String> props = FSStoreUtil.readProperties(ois);
+        return new FSTestEntity(id, props);
+    }
+
+    protected void writeEntity(final ObjectOutputStream oos, final 
FSTestEntity entity)
+        throws IOException {
+        FSStoreUtil.writeString(oos, entity.getId());
+        FSStoreUtil.writeProperties(oos, entity.getProperties());
+    }
+}
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to