[jboss-cvs] JBossAS SVN: r72882 - in projects/security/security-jboss-sx/trunk/identity/src: tests/org/jboss/test/identity/impl and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Apr 29 20:51:59 EDT 2008


Author: mmoyses
Date: 2008-04-29 20:51:58 -0400 (Tue, 29 Apr 2008)
New Revision: 72882

Modified:
   projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/FilePersistenceStrategy.java
   projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/PersistenceStrategy.java
   projects/security/security-jboss-sx/trunk/identity/src/tests/org/jboss/test/identity/impl/FilePersistenceStrategyUnitTestCase.java
Log:
Improvements to the interface

Modified: projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/FilePersistenceStrategy.java
===================================================================
--- projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/FilePersistenceStrategy.java	2008-04-29 23:29:54 UTC (rev 72881)
+++ projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/FilePersistenceStrategy.java	2008-04-30 00:51:58 UTC (rev 72882)
@@ -21,6 +21,7 @@
   */
 package org.jboss.security.identity.plugins;
 
+import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
@@ -38,24 +39,36 @@
  */
 public class FilePersistenceStrategy implements PersistenceStrategy
 {
-   private String fileName;
+   private String path;
 
-   public FilePersistenceStrategy(String fileName)
+   /**
+    * Create a new FilePersistenceStrategy.
+    * 
+    * @param path directory where the files will be stored.
+    */
+   public FilePersistenceStrategy(String path)
    {
-      this.fileName = fileName;
+      this.path = path;
    }
 
    /**
     * @see PersistenceStrategy#persistIdentity(Identity).
     */
-   public void persistIdentity(Identity identity)
+   public Identity persistIdentity(Identity identity)
    {
       ObjectOutputStream oos = null;
       try
       {
-         FileOutputStream fos = new FileOutputStream(fileName);
+         File file = new File(path + File.separator + identity.getName());
+         if (file.exists())
+         {
+            // identity already exists
+            return null;
+         }
+         FileOutputStream fos = new FileOutputStream(file);
          oos = new ObjectOutputStream(fos);
          oos.writeObject(identity);
+         return identity;
       }
       catch (Exception e)
       {
@@ -75,17 +88,19 @@
             }
          }
       }
+
+      return null;
    }
 
    /**
-    * @see PersistenceStrategy#retrieveIdentity(String).
+    * @see PersistenceStrategy#getIdentity(String).
     */
-   public Identity retrieveIdentity(String name)
+   public Identity getIdentity(String name)
    {
       ObjectInputStream ois = null;
       try
       {
-         FileInputStream fis = new FileInputStream(fileName);
+         FileInputStream fis = new FileInputStream(path + File.separator + name);
          ois = new ObjectInputStream(fis);
          Identity identity = (Identity) ois.readObject();
          return identity;
@@ -111,4 +126,28 @@
 
       return null;
    }
+
+   /**
+    * @see PersistenceStrategy#removeIdentity(Identity).
+    */
+   public boolean removeIdentity(Identity identity)
+   {
+      File file = new File(path + File.separator + identity.getName());
+
+      return file.delete();
+   }
+
+   /**
+    * @see PersistenceStrategy#updateIdentity(Identity).
+    */
+   public Identity updateIdentity(Identity identity)
+   {
+      if (removeIdentity(identity))
+      {
+         return persistIdentity(identity);
+      }
+
+      return null;
+   }
+
 }

Modified: projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/PersistenceStrategy.java
===================================================================
--- projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/PersistenceStrategy.java	2008-04-29 23:29:54 UTC (rev 72881)
+++ projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/PersistenceStrategy.java	2008-04-30 00:51:58 UTC (rev 72882)
@@ -36,15 +36,32 @@
     * Persists the <code>Identity</code> in the backend.
     * 
     * @param identity <code>Identity</code> to be persisted.
+    * @return the persisted <code>Identity</code> or <code>null</code> if persistence failed.
     */
-   public void persistIdentity(Identity identity);
+   public Identity persistIdentity(Identity identity);
 
    /**
     * Retrieves an <code>Identity</code> from the backend.
     * 
     * @param name unique name of the <code>Identity</code>.
-    * @return the <code>Identity</code> or <code>null</code> if not found
+    * @return the <code>Identity</code> or <code>null</code> if not found.
     */
-   public Identity retrieveIdentity(String name);
+   public Identity getIdentity(String name);
 
+   /**
+    * Updates the <code>Identity</code> in the backend.
+    * 
+    * @param identity <code>Identity</code> to be updated.
+    * @return the updated <code>Identity</code> or <code>null</code> if the update was not successful.
+    */
+   public Identity updateIdentity(Identity identity);
+
+   /**
+    * Removes an <code>Identity</code> from the backend.
+    * 
+    * @param identity <code>Identity</code> to be removed. 
+    * @return <code>true</code> if successfully removed, <code>false</code> otherwise.
+    */
+   public boolean removeIdentity(Identity identity);
+
 }

Modified: projects/security/security-jboss-sx/trunk/identity/src/tests/org/jboss/test/identity/impl/FilePersistenceStrategyUnitTestCase.java
===================================================================
--- projects/security/security-jboss-sx/trunk/identity/src/tests/org/jboss/test/identity/impl/FilePersistenceStrategyUnitTestCase.java	2008-04-29 23:29:54 UTC (rev 72881)
+++ projects/security/security-jboss-sx/trunk/identity/src/tests/org/jboss/test/identity/impl/FilePersistenceStrategyUnitTestCase.java	2008-04-30 00:51:58 UTC (rev 72882)
@@ -29,51 +29,52 @@
 import org.jboss.security.identity.Role;
 import org.jboss.security.identity.plugins.FilePersistenceStrategy;
 import org.jboss.security.identity.plugins.IdentityFactory;
+import org.jboss.security.identity.plugins.PersistenceStrategy;
 import org.jboss.security.identity.plugins.SimpleRole;
 
 /**
- * A PersistenceStrategyUnitTestCase.
+ * A FilePersistenceStrategyUnitTestCase.
  * 
  * @author <a href="mmoyses at redhat.com">Marcus Moyses</a>
  * @version $Revision: 1.1 $
  */
 public class FilePersistenceStrategyUnitTestCase extends TestCase
 {
+   private static final String identityName = "test";
 
-   private static final String fileName = System.getProperty("java.io.tmpdir") + File.separator + "identity.ser";
+   private static final String path = System.getProperty("java.io.tmpdir");
 
-   private static final File file = new File(fileName);
+   private static final File file = new File(path + File.separator + "test");
 
+   private static final PersistenceStrategy ps = new FilePersistenceStrategy(path);
+
    public void testWriteIdentity() throws Exception
    {
-      Identity identity = IdentityFactory.createIdentity("test");
-      FilePersistenceStrategy fps = new FilePersistenceStrategy(fileName);
+      Identity identity = IdentityFactory.createIdentity(identityName);
       assertFalse("File already exists", file.exists());
-      fps.persistIdentity(identity);
+      assertNotNull("Failed to persist", ps.persistIdentity(identity));
       assertTrue("File was not created", file.exists());
    }
 
    public void testReadIdentity() throws Exception
    {
-      Identity identity = IdentityFactory.createIdentity("test");
-      FilePersistenceStrategy fps = new FilePersistenceStrategy(fileName);
+      Identity identity = IdentityFactory.createIdentity(identityName);
       assertFalse("File already exists", file.exists());
-      fps.persistIdentity(identity);
+      assertNotNull("Failed to persist", ps.persistIdentity(identity));
       assertTrue("File was not created", file.exists());
 
-      Identity restored = fps.retrieveIdentity("test");
+      Identity restored = ps.getIdentity(identityName);
       assertEquals("Objects are different", identity, restored);
    }
 
    public void testReadIdentityWithRole() throws Exception
    {
-      Identity identity = IdentityFactory.createIdentityWithRole("test", "testRole");
-      FilePersistenceStrategy fps = new FilePersistenceStrategy(fileName);
+      Identity identity = IdentityFactory.createIdentityWithRole(identityName, "testRole");
       assertFalse("File already exists", file.exists());
-      fps.persistIdentity(identity);
+      assertNotNull("Failed to persist", ps.persistIdentity(identity));
       assertTrue("File was not created", file.exists());
 
-      Identity restored = fps.retrieveIdentity("test");
+      Identity restored = ps.getIdentity(identityName);
       assertEquals("Objects are different", identity, restored);
       assertEquals("Role names are different", identity.getRole().getRoleName(), restored.getRole().getRoleName());
    }
@@ -82,19 +83,58 @@
    {
       Role parent = new SimpleRole("parent");
       Role role = new SimpleRole("testRole", parent);
-      Identity identity = IdentityFactory.createIdentityWithRole("test", role);
-      FilePersistenceStrategy fps = new FilePersistenceStrategy(fileName);
+      Identity identity = IdentityFactory.createIdentityWithRole(identityName, role);
       assertFalse("File already exists", file.exists());
-      fps.persistIdentity(identity);
+      assertNotNull("Failed to persist", ps.persistIdentity(identity));
       assertTrue("File was not created", file.exists());
 
-      Identity restored = fps.retrieveIdentity("test");
+      Identity restored = ps.getIdentity(identityName);
       assertEquals("Objects are different", identity, restored);
       assertEquals("Role names are different", identity.getRole().getRoleName(), restored.getRole().getRoleName());
       assertEquals("Parent role names are different", identity.getRole().getParent().getRoleName(), restored.getRole()
             .getParent().getRoleName());
    }
 
+   public void testRemoveIdentity() throws Exception
+   {
+      Identity identity = IdentityFactory.createIdentity(identityName);
+      assertFalse("File already exists", file.exists());
+      assertNotNull("Failed to persist", ps.persistIdentity(identity));
+      assertTrue("File was not created", file.exists());
+
+      assertTrue("Identity was not removed", ps.removeIdentity(identity));
+   }
+
+   public void testUpdateIdentityWithRole() throws Exception
+   {
+      Identity identity = IdentityFactory.createIdentity(identityName);
+      assertFalse("File already exists", file.exists());
+      assertNotNull("Failed to persist", ps.persistIdentity(identity));
+      assertTrue("File was not created", file.exists());
+
+      Identity restored = ps.getIdentity(identityName);
+      assertEquals("Objects are different", identity, restored);
+      assertNull("Role must be null", restored.getRole());
+
+      identity = IdentityFactory.createIdentityWithRole(identityName, "testRole");
+      ps.updateIdentity(identity);
+      assertTrue("File was not re-created", file.exists());
+      restored = ps.getIdentity(identityName);
+      assertEquals("Objects are different", identity, restored);
+      assertEquals("Role names are different", identity.getRole().getRoleName(), restored.getRole().getRoleName());
+   }
+
+   public void testWriteDuplicateIdentity() throws Exception
+   {
+      Identity identity = IdentityFactory.createIdentity(identityName);
+      assertFalse("File already exists", file.exists());
+      assertNotNull("Failed to persist", ps.persistIdentity(identity));
+      assertTrue("File was not created", file.exists());
+
+      Identity duplicate = IdentityFactory.createIdentity(identityName);
+      assertNull("Should not persist duplicate Identity", ps.persistIdentity(duplicate));
+   }
+
    @Override
    protected void tearDown() throws Exception
    {




More information about the jboss-cvs-commits mailing list