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

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Jan 30 08:51:16 EST 2008


Author: sguilhen at redhat.com
Date: 2008-01-30 08:51:15 -0500 (Wed, 30 Jan 2008)
New Revision: 69487

Added:
   projects/security/security-jboss-sx/trunk/acl/src/tests/org/jboss/test/security/acl/PersistenceTestCase.java
Removed:
   projects/security/security-jboss-sx/trunk/acl/src/tests/org/jboss/test/security/acl/PersistenceTest.java
Modified:
   projects/security/security-jboss-sx/trunk/acl/.classpath
Log:
Changed PersistenceTestCase to instantiate the EntityManagerFactory in the setup method (instead of creating it as a static final class variable). With this change maven is capable of locating the persistence.xml file and run the persistence testcase.



Modified: projects/security/security-jboss-sx/trunk/acl/.classpath
===================================================================
--- projects/security/security-jboss-sx/trunk/acl/.classpath	2008-01-30 13:10:59 UTC (rev 69486)
+++ projects/security/security-jboss-sx/trunk/acl/.classpath	2008-01-30 13:51:15 UTC (rev 69487)
@@ -3,7 +3,7 @@
 	<classpathentry excluding="**/*.java" including="JBossORG-EULA.txt" kind="src" path=""/>
 	<classpathentry kind="src" path="src/main"/>
 	<classpathentry excluding="**/*.java" including="**/*.dtd|**/*.xsd|**/*.xml" kind="src" path="src/resources"/>
-	<classpathentry including="**/*.java;resources/*" kind="src" output="target/test-classes" path="src/tests"/>
+	<classpathentry excluding="resources/" including="**/*.java" kind="src" output="target/test-classes" path="src/tests"/>
 	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
 	<classpathentry kind="var" path="M2_REPO/antlr/antlr/2.7.6/antlr-2.7.6.jar"/>
 	<classpathentry kind="var" path="M2_REPO/log4j/log4j/1.2.14/log4j-1.2.14.jar"/>

Deleted: projects/security/security-jboss-sx/trunk/acl/src/tests/org/jboss/test/security/acl/PersistenceTest.java
===================================================================
--- projects/security/security-jboss-sx/trunk/acl/src/tests/org/jboss/test/security/acl/PersistenceTest.java	2008-01-30 13:10:59 UTC (rev 69486)
+++ projects/security/security-jboss-sx/trunk/acl/src/tests/org/jboss/test/security/acl/PersistenceTest.java	2008-01-30 13:51:15 UTC (rev 69487)
@@ -1,342 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software 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
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.test.security.acl;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
-import javax.persistence.EntityTransaction;
-import javax.persistence.Persistence;
-
-import junit.framework.TestCase;
-
-import org.jboss.security.acl.ACL;
-import org.jboss.security.acl.ACLEntry;
-import org.jboss.security.acl.ACLEntryImpl;
-import org.jboss.security.acl.ACLImpl;
-import org.jboss.security.acl.BasicACLPermission;
-import org.jboss.security.acl.CompositeACLPermission;
-import org.jboss.security.acl.Util;
-import org.jboss.security.identity.plugins.IdentityFactory;
-
-/**
- * <p>
- * This {@code TestCase} tests the functionality of the persistence layer added to the {@code ACL} implementation classes.
- * It uses an in-memory hsql test database, so there is no need to perform any special database cleanup in case one of the
- * tests fail. Every time the tests are run a clean new database is used.
- * </p>
- * 
- * @author <a href="mailto:sguilhen at redhat.com">Stefan Guilhen</a>
- */
-public class PersistenceTest extends TestCase
-{
-   private static final EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("ACL");
-
-   private EntityManager entityManager;
-
-   private List<ACLEntryImpl> persistedEntries;
-
-   private List<ACLImpl> persistedACLs;
-
-   /*
-    * (non-Javadoc)
-    * @see junit.framework.TestCase#setUp()
-    */
-   @Override
-   protected void setUp() throws Exception
-   {
-      this.entityManager = entityManagerFactory.createEntityManager();
-      this.persistedACLs = new ArrayList<ACLImpl>();
-      this.persistedEntries = new ArrayList<ACLEntryImpl>();
-
-      // create the test entries.
-      this.persistedEntries
-            .add(new ACLEntryImpl(BasicACLPermission.READ, IdentityFactory.createIdentity("Identity-1")));
-      this.persistedEntries.add(new ACLEntryImpl(new CompositeACLPermission(BasicACLPermission.CREATE,
-            BasicACLPermission.READ), IdentityFactory.createIdentity("Identity-2")));
-      this.persistedEntries.add(new ACLEntryImpl(new CompositeACLPermission(BasicACLPermission.values()),
-            IdentityFactory.createIdentity("Identity-3")));
-
-      // create the test acls.
-      this.persistedACLs.add(new ACLImpl(new TestResource(100, "Resource-1")));
-      this.persistedACLs.add(new ACLImpl(new TestResource(200, "Resource-2"), new ArrayList<ACLEntry>(
-            this.persistedEntries)));
-
-      // persist everything.
-      for (ACL acl : this.persistedACLs)
-         this.persistEntity(acl);
-   }
-
-   /*
-    * (non-Javadoc)
-    * @see junit.framework.TestCase#tearDown()
-    */
-   @Override
-   protected void tearDown() throws Exception
-   {
-      // remove the persisted entities - removing the acl also removes the entries.
-      for (ACLImpl acl : this.persistedACLs)
-      {
-         // re-attach the acl before removing.
-         ACLImpl attachedACL = this.entityManager.find(ACLImpl.class, acl.getACLId());
-         this.removeEntity(attachedACL);
-      }
-
-      // assert the acls have been removed.
-      for (ACLImpl acl : this.persistedACLs)
-         assertNull(this.entityManager.find(ACLImpl.class, acl.getACLId()));
-
-      // assert the entries have been also removed.
-      for (ACLEntryImpl entry : this.persistedEntries)
-         assertNull(this.entityManager.find(ACLEntryImpl.class, entry.getACLEntryId()));
-
-      if (this.entityManager != null)
-         entityManager.close();
-   }
-
-   /**
-    * <p>
-    * Tests persisting the {@code ACLEntry} objects to a database.
-    * </p>
-    * 
-    * @throws Exception if an error occurs when running the test.
-    */
-   public void testPersistACLEntry() throws Exception
-   {
-      // assert the entries have been created by checking if the auto-generated id has been set on each entry.
-      assertTrue("Entry1 id value has not been generated", this.persistedEntries.get(0).getACLEntryId() > 0);
-      assertTrue("Entry2 id value has not been generated", this.persistedEntries.get(1).getACLEntryId() > 0);
-      assertTrue("Entry3 id value has not been generated", this.persistedEntries.get(2).getACLEntryId() > 0);
-
-   }
-
-   /**
-    * <p>
-    * Tests searching for the persisted {@code ACLEntry} objects.
-    * </p>
-    * 
-    * @throws Exception if an error occurs when running the test.
-    */
-   public void testSearchACLEntry() throws Exception
-   {
-      // clear the entity manager so that it goes to the database when searching for entries.
-      this.entityManager.clear();
-
-      // load the entries from the database using their primary key and validate them.
-      for (ACLEntryImpl entry : this.persistedEntries)
-      {
-         ACLEntryImpl loadedEntry = this.entityManager.find(ACLEntryImpl.class, entry.getACLEntryId());
-         assertNotNull("Entry could not be retrieved by primary key", loadedEntry);
-         assertEquals(entry, loadedEntry);
-      }
-
-      // execute some queries and validate the results.
-      ACLEntryImpl entry = this.persistedEntries.get(1);
-      ACLEntryImpl queryResult = (ACLEntryImpl) this.entityManager.createQuery(
-            "SELECT e FROM ACLEntryImpl e WHERE e.identityString LIKE '"
-                  + Util.getIdentityAsString(entry.getIdentity()) + "'").getSingleResult();
-      assertNotNull("Entry2 could not be retrieved by it's identity", queryResult);
-      assertEquals(entry, queryResult);
-
-      entry = this.persistedEntries.get(0);
-      queryResult = (ACLEntryImpl) this.entityManager.createQuery(
-            "SELECT e FROM ACLEntryImpl e WHERE e.bitMask = " + BasicACLPermission.READ.getMaskValue())
-            .getSingleResult();
-      assertNotNull("Entry1 could not be retrieved by it's bitmask value", queryResult);
-      assertEquals(entry, queryResult);
-   }
-
-   /**
-    * <p>
-    * Tests persisting the {@code ACL} objects to a database.
-    * </p>
-    * 
-    * @throws Exception if an error occurs when running the test.
-    */
-   public void testPersistACL() throws Exception
-   {
-      // assert the ACLs and their associated entries have been persisted
-      for (ACLImpl acl : this.persistedACLs)
-      {
-         assertTrue("ACL id value has not been generated", acl.getACLId() > 0);
-         for (ACLEntry entry : acl.getEntries())
-            assertTrue("ACL entry has not been persisted", ((ACLEntryImpl) entry).getACLEntryId() > 0);
-      }
-   }
-
-   /**
-    * <p>
-    * Tests searching for the persisted {@code ACL} objects. 
-    * </p>
-    * 
-    * @throws Exception if an error occurs when running the test.
-    */
-   public void testSearchACL() throws Exception
-   {
-      // clear the entity manager's cache.
-      this.entityManager.clear();
-
-      // load the ACLs from the database using their primary key and validate them.
-      ACLImpl loadedACL1 = this.entityManager.find(ACLImpl.class, this.persistedACLs.get(0).getACLId());
-      assertNotNull("ACL1 could not be retrieved", loadedACL1);
-      assertEquals("Loaded ACL contains unexpected number of entries", 0, loadedACL1.getEntries().size());
-      assertNull(loadedACL1.getResource());
-
-      ACLImpl loadedACL2 = this.entityManager.find(ACLImpl.class, this.persistedACLs.get(1).getACLId());
-      assertNotNull("ACL2 could not be retrieved", loadedACL2);
-      assertEquals("Loaded ACL contains unexpected number of entries", 3, loadedACL2.getEntries().size());
-      assertTrue(loadedACL2.getEntries().contains(this.persistedEntries.get(0)));
-      assertTrue(loadedACL2.getEntries().contains(this.persistedEntries.get(1)));
-      assertTrue(loadedACL2.getEntries().contains(this.persistedEntries.get(2)));
-      assertNull(loadedACL2.getResource());
-
-      // find the ACLs using the resource and validate the result.
-      ACLImpl acl = this.persistedACLs.get(0);
-      ACLImpl queryResult = (ACLImpl) this.entityManager.createQuery(
-            "SELECT a FROM ACLImpl a WHERE a.resourceAsString LIKE '" + Util.getResourceAsString(acl.getResource())
-                  + "'").getSingleResult();
-      assertNotNull("ACL1 could not be retrieved by it's resource", queryResult);
-      assertEquals("Queried ACL id does not match the expected id", acl.getACLId(), queryResult.getACLId());
-      assertEquals("Queried ACL contains unexpected number of entries", 0, queryResult.getEntries().size());
-
-      acl = this.persistedACLs.get(1);
-      queryResult = (ACLImpl) this.entityManager.createQuery(
-            "SELECT a FROM ACLImpl a WHERE a.resourceAsString LIKE '" + Util.getResourceAsString(acl.getResource())
-                  + "'").getSingleResult();
-      assertNotNull("ACL2 could not be retrieved by it's resource", queryResult);
-      assertEquals("Queried ACL id does not match the expected id", acl.getACLId(), queryResult.getACLId());
-      assertEquals("Queried ACL contains unexpected number of entries", 3, queryResult.getEntries().size());
-      assertTrue(queryResult.getEntries().contains(this.persistedEntries.get(0)));
-      assertTrue(queryResult.getEntries().contains(this.persistedEntries.get(1)));
-      assertTrue(queryResult.getEntries().contains(this.persistedEntries.get(2)));
-
-   }
-
-   /**
-    * <p>
-    * Tests updating the persisted {@code ACL} objects.
-    * </p>
-    * 
-    * @throws Exception if an error occurs when running the test.
-    */
-   public void testUpdateACL() throws Exception
-   {
-      // add some entries to the acls and remove one of the existing entries from ACL2.
-      ACLEntryImpl entry4 = new ACLEntryImpl(BasicACLPermission.CREATE, IdentityFactory.createIdentity("Identity-4"));
-      ACLEntryImpl entry5 = new ACLEntryImpl(new CompositeACLPermission(BasicACLPermission.CREATE,
-            BasicACLPermission.DELETE), IdentityFactory.createIdentity("Identity-5"));
-      ACLEntryImpl entry6 = new ACLEntryImpl(new CompositeACLPermission(BasicACLPermission.values()), IdentityFactory
-            .createIdentity("Identity-6"));
-
-      ACLImpl acl1 = null;
-      ACLImpl acl2 = null;
-      EntityTransaction transaction = this.entityManager.getTransaction();
-      transaction.begin();
-      try
-      {
-         acl1 = this.entityManager.merge(this.persistedACLs.get(0));
-         acl1.addEntry(entry4);
-         acl1.addEntry(entry5);
-
-         acl2 = this.entityManager.merge(this.persistedACLs.get(1));
-         acl2.addEntry(entry6);
-         acl2.removeEntry(this.persistedEntries.get(0));
-         transaction.commit();
-      }
-      catch (RuntimeException re)
-      {
-         re.printStackTrace();
-         transaction.rollback();
-      }
-
-      // add the new entries to the persisted entries collection.
-      this.persistedEntries.add(entry4);
-      this.persistedEntries.add(entry5);
-      this.persistedEntries.add(entry6);
-
-      // clear the entity manager's cache.
-      this.entityManager.clear();
-
-      // load the ACLs again and validate the changes.
-      ACLImpl loadedACL1 = this.entityManager.find(ACLImpl.class, acl1.getACLId());
-      assertNotNull("ACL1 could not be retrieved", loadedACL1);
-      assertEquals("Loaded ACL contains unexpected number of entries", 2, loadedACL1.getEntries().size());
-      assertTrue(loadedACL1.getEntries().contains(entry4));
-      assertTrue(loadedACL1.getEntries().contains(entry5));
-
-      ACLImpl loadedACL2 = this.entityManager.find(ACLImpl.class, acl2.getACLId());
-      assertNotNull("ACL2 could not be retrieved", loadedACL2);
-      assertEquals("Loaded AC2 contains unexpected number of entries", 3, loadedACL2.getEntries().size());
-      assertFalse(loadedACL2.getEntries().contains(this.persistedEntries.get(0)));
-      assertTrue(loadedACL2.getEntries().contains(this.persistedEntries.get(1)));
-      assertTrue(loadedACL2.getEntries().contains(this.persistedEntries.get(2)));
-      assertTrue(loadedACL2.getEntries().contains(entry6));
-   }
-
-   /**
-    * <p>
-    * Persists the specified entity to the database.
-    * </p>
-    * 
-    * @param entity an {@code Object} representing the entity to be persisted.
-    */
-   private void persistEntity(Object entity)
-   {
-      EntityTransaction transaction = this.entityManager.getTransaction();
-      transaction.begin();
-      try
-      {
-         this.entityManager.persist(entity);
-         transaction.commit();
-      }
-      catch (RuntimeException re)
-      {
-         re.printStackTrace();
-         transaction.rollback();
-      }
-   }
-
-   /**
-    * <p>
-    * Removes the specified entity from the database.
-    * </p>
-    * 
-    * @param entity an {@code Object} representing the entity to be removed.
-    */
-   private void removeEntity(Object entity)
-   {
-      EntityTransaction transaction = this.entityManager.getTransaction();
-      transaction.begin();
-      try
-      {
-         this.entityManager.remove(entity);
-         transaction.commit();
-      }
-      catch (RuntimeException re)
-      {
-         re.printStackTrace();
-         transaction.rollback();
-      }
-   }
-}

Copied: projects/security/security-jboss-sx/trunk/acl/src/tests/org/jboss/test/security/acl/PersistenceTestCase.java (from rev 69486, projects/security/security-jboss-sx/trunk/acl/src/tests/org/jboss/test/security/acl/PersistenceTest.java)
===================================================================
--- projects/security/security-jboss-sx/trunk/acl/src/tests/org/jboss/test/security/acl/PersistenceTestCase.java	                        (rev 0)
+++ projects/security/security-jboss-sx/trunk/acl/src/tests/org/jboss/test/security/acl/PersistenceTestCase.java	2008-01-30 13:51:15 UTC (rev 69487)
@@ -0,0 +1,344 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.security.acl;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Persistence;
+
+import junit.framework.TestCase;
+
+import org.jboss.security.acl.ACL;
+import org.jboss.security.acl.ACLEntry;
+import org.jboss.security.acl.ACLEntryImpl;
+import org.jboss.security.acl.ACLImpl;
+import org.jboss.security.acl.BasicACLPermission;
+import org.jboss.security.acl.CompositeACLPermission;
+import org.jboss.security.acl.Util;
+import org.jboss.security.identity.plugins.IdentityFactory;
+
+/**
+ * <p>
+ * This {@code TestCase} tests the functionality of the persistence layer added to the {@code ACL} implementation classes.
+ * It uses an in-memory hsql test database, so there is no need to perform any special database cleanup in case one of the
+ * tests fail. Every time the tests are run a clean new database is used.
+ * </p>
+ * 
+ * @author <a href="mailto:sguilhen at redhat.com">Stefan Guilhen</a>
+ */
+public class PersistenceTestCase extends TestCase
+{
+   private EntityManagerFactory entityManagerFactory;
+
+   private EntityManager entityManager;
+
+   private List<ACLEntryImpl> persistedEntries;
+
+   private List<ACLImpl> persistedACLs;
+
+   /*
+    * (non-Javadoc)
+    * @see junit.framework.TestCase#setUp()
+    */
+   @Override
+   protected void setUp() throws Exception
+   {
+      this.entityManagerFactory = Persistence.createEntityManagerFactory("ACL");
+      this.entityManager = entityManagerFactory.createEntityManager();
+      this.persistedACLs = new ArrayList<ACLImpl>();
+      this.persistedEntries = new ArrayList<ACLEntryImpl>();
+
+      // create the test entries.
+      this.persistedEntries
+            .add(new ACLEntryImpl(BasicACLPermission.READ, IdentityFactory.createIdentity("Identity-1")));
+      this.persistedEntries.add(new ACLEntryImpl(new CompositeACLPermission(BasicACLPermission.CREATE,
+            BasicACLPermission.READ), IdentityFactory.createIdentity("Identity-2")));
+      this.persistedEntries.add(new ACLEntryImpl(new CompositeACLPermission(BasicACLPermission.values()),
+            IdentityFactory.createIdentity("Identity-3")));
+
+      // create the test acls.
+      this.persistedACLs.add(new ACLImpl(new TestResource(100, "Resource-1")));
+      this.persistedACLs.add(new ACLImpl(new TestResource(200, "Resource-2"), new ArrayList<ACLEntry>(
+            this.persistedEntries)));
+
+      // persist everything.
+      for (ACL acl : this.persistedACLs)
+         this.persistEntity(acl);
+   }
+
+   /*
+    * (non-Javadoc)
+    * @see junit.framework.TestCase#tearDown()
+    */
+   @Override
+   protected void tearDown() throws Exception
+   {
+      // remove the persisted entities - removing the acl also removes the entries.
+      for (ACLImpl acl : this.persistedACLs)
+      {
+         // re-attach the acl before removing.
+         ACLImpl attachedACL = this.entityManager.find(ACLImpl.class, acl.getACLId());
+         this.removeEntity(attachedACL);
+      }
+
+      // assert the acls have been removed.
+      for (ACLImpl acl : this.persistedACLs)
+         assertNull(this.entityManager.find(ACLImpl.class, acl.getACLId()));
+
+      // assert the entries have been also removed.
+      for (ACLEntryImpl entry : this.persistedEntries)
+         assertNull(this.entityManager.find(ACLEntryImpl.class, entry.getACLEntryId()));
+
+      if (this.entityManager != null)
+         entityManager.close();
+      this.entityManagerFactory.close();
+   }
+
+   /**
+    * <p>
+    * Tests persisting the {@code ACLEntry} objects to a database.
+    * </p>
+    * 
+    * @throws Exception if an error occurs when running the test.
+    */
+   public void testPersistACLEntry() throws Exception
+   {
+      // assert the entries have been created by checking if the auto-generated id has been set on each entry.
+      assertTrue("Entry1 id value has not been generated", this.persistedEntries.get(0).getACLEntryId() > 0);
+      assertTrue("Entry2 id value has not been generated", this.persistedEntries.get(1).getACLEntryId() > 0);
+      assertTrue("Entry3 id value has not been generated", this.persistedEntries.get(2).getACLEntryId() > 0);
+
+   }
+
+   /**
+    * <p>
+    * Tests searching for the persisted {@code ACLEntry} objects.
+    * </p>
+    * 
+    * @throws Exception if an error occurs when running the test.
+    */
+   public void testSearchACLEntry() throws Exception
+   {
+      // clear the entity manager so that it goes to the database when searching for entries.
+      this.entityManager.clear();
+
+      // load the entries from the database using their primary key and validate them.
+      for (ACLEntryImpl entry : this.persistedEntries)
+      {
+         ACLEntryImpl loadedEntry = this.entityManager.find(ACLEntryImpl.class, entry.getACLEntryId());
+         assertNotNull("Entry could not be retrieved by primary key", loadedEntry);
+         assertEquals(entry, loadedEntry);
+      }
+
+      // execute some queries and validate the results.
+      ACLEntryImpl entry = this.persistedEntries.get(1);
+      ACLEntryImpl queryResult = (ACLEntryImpl) this.entityManager.createQuery(
+            "SELECT e FROM ACLEntryImpl e WHERE e.identityString LIKE '"
+                  + Util.getIdentityAsString(entry.getIdentity()) + "'").getSingleResult();
+      assertNotNull("Entry2 could not be retrieved by it's identity", queryResult);
+      assertEquals(entry, queryResult);
+
+      entry = this.persistedEntries.get(0);
+      queryResult = (ACLEntryImpl) this.entityManager.createQuery(
+            "SELECT e FROM ACLEntryImpl e WHERE e.bitMask = " + BasicACLPermission.READ.getMaskValue())
+            .getSingleResult();
+      assertNotNull("Entry1 could not be retrieved by it's bitmask value", queryResult);
+      assertEquals(entry, queryResult);
+   }
+
+   /**
+    * <p>
+    * Tests persisting the {@code ACL} objects to a database.
+    * </p>
+    * 
+    * @throws Exception if an error occurs when running the test.
+    */
+   public void testPersistACL() throws Exception
+   {
+      // assert the ACLs and their associated entries have been persisted
+      for (ACLImpl acl : this.persistedACLs)
+      {
+         assertTrue("ACL id value has not been generated", acl.getACLId() > 0);
+         for (ACLEntry entry : acl.getEntries())
+            assertTrue("ACL entry has not been persisted", ((ACLEntryImpl) entry).getACLEntryId() > 0);
+      }
+   }
+
+   /**
+    * <p>
+    * Tests searching for the persisted {@code ACL} objects. 
+    * </p>
+    * 
+    * @throws Exception if an error occurs when running the test.
+    */
+   public void testSearchACL() throws Exception
+   {
+      // clear the entity manager's cache.
+      this.entityManager.clear();
+
+      // load the ACLs from the database using their primary key and validate them.
+      ACLImpl loadedACL1 = this.entityManager.find(ACLImpl.class, this.persistedACLs.get(0).getACLId());
+      assertNotNull("ACL1 could not be retrieved", loadedACL1);
+      assertEquals("Loaded ACL contains unexpected number of entries", 0, loadedACL1.getEntries().size());
+      assertNull(loadedACL1.getResource());
+
+      ACLImpl loadedACL2 = this.entityManager.find(ACLImpl.class, this.persistedACLs.get(1).getACLId());
+      assertNotNull("ACL2 could not be retrieved", loadedACL2);
+      assertEquals("Loaded ACL contains unexpected number of entries", 3, loadedACL2.getEntries().size());
+      assertTrue(loadedACL2.getEntries().contains(this.persistedEntries.get(0)));
+      assertTrue(loadedACL2.getEntries().contains(this.persistedEntries.get(1)));
+      assertTrue(loadedACL2.getEntries().contains(this.persistedEntries.get(2)));
+      assertNull(loadedACL2.getResource());
+
+      // find the ACLs using the resource and validate the result.
+      ACLImpl acl = this.persistedACLs.get(0);
+      ACLImpl queryResult = (ACLImpl) this.entityManager.createQuery(
+            "SELECT a FROM ACLImpl a WHERE a.resourceAsString LIKE '" + Util.getResourceAsString(acl.getResource())
+                  + "'").getSingleResult();
+      assertNotNull("ACL1 could not be retrieved by it's resource", queryResult);
+      assertEquals("Queried ACL id does not match the expected id", acl.getACLId(), queryResult.getACLId());
+      assertEquals("Queried ACL contains unexpected number of entries", 0, queryResult.getEntries().size());
+
+      acl = this.persistedACLs.get(1);
+      queryResult = (ACLImpl) this.entityManager.createQuery(
+            "SELECT a FROM ACLImpl a WHERE a.resourceAsString LIKE '" + Util.getResourceAsString(acl.getResource())
+                  + "'").getSingleResult();
+      assertNotNull("ACL2 could not be retrieved by it's resource", queryResult);
+      assertEquals("Queried ACL id does not match the expected id", acl.getACLId(), queryResult.getACLId());
+      assertEquals("Queried ACL contains unexpected number of entries", 3, queryResult.getEntries().size());
+      assertTrue(queryResult.getEntries().contains(this.persistedEntries.get(0)));
+      assertTrue(queryResult.getEntries().contains(this.persistedEntries.get(1)));
+      assertTrue(queryResult.getEntries().contains(this.persistedEntries.get(2)));
+
+   }
+
+   /**
+    * <p>
+    * Tests updating the persisted {@code ACL} objects.
+    * </p>
+    * 
+    * @throws Exception if an error occurs when running the test.
+    */
+   public void testUpdateACL() throws Exception
+   {
+      // add some entries to the acls and remove one of the existing entries from ACL2.
+      ACLEntryImpl entry4 = new ACLEntryImpl(BasicACLPermission.CREATE, IdentityFactory.createIdentity("Identity-4"));
+      ACLEntryImpl entry5 = new ACLEntryImpl(new CompositeACLPermission(BasicACLPermission.CREATE,
+            BasicACLPermission.DELETE), IdentityFactory.createIdentity("Identity-5"));
+      ACLEntryImpl entry6 = new ACLEntryImpl(new CompositeACLPermission(BasicACLPermission.values()), IdentityFactory
+            .createIdentity("Identity-6"));
+
+      ACLImpl acl1 = null;
+      ACLImpl acl2 = null;
+      EntityTransaction transaction = this.entityManager.getTransaction();
+      transaction.begin();
+      try
+      {
+         acl1 = this.entityManager.merge(this.persistedACLs.get(0));
+         acl1.addEntry(entry4);
+         acl1.addEntry(entry5);
+
+         acl2 = this.entityManager.merge(this.persistedACLs.get(1));
+         acl2.addEntry(entry6);
+         acl2.removeEntry(this.persistedEntries.get(0));
+         transaction.commit();
+      }
+      catch (RuntimeException re)
+      {
+         re.printStackTrace();
+         transaction.rollback();
+      }
+
+      // add the new entries to the persisted entries collection.
+      this.persistedEntries.add(entry4);
+      this.persistedEntries.add(entry5);
+      this.persistedEntries.add(entry6);
+
+      // clear the entity manager's cache.
+      this.entityManager.clear();
+
+      // load the ACLs again and validate the changes.
+      ACLImpl loadedACL1 = this.entityManager.find(ACLImpl.class, acl1.getACLId());
+      assertNotNull("ACL1 could not be retrieved", loadedACL1);
+      assertEquals("Loaded ACL contains unexpected number of entries", 2, loadedACL1.getEntries().size());
+      assertTrue(loadedACL1.getEntries().contains(entry4));
+      assertTrue(loadedACL1.getEntries().contains(entry5));
+
+      ACLImpl loadedACL2 = this.entityManager.find(ACLImpl.class, acl2.getACLId());
+      assertNotNull("ACL2 could not be retrieved", loadedACL2);
+      assertEquals("Loaded AC2 contains unexpected number of entries", 3, loadedACL2.getEntries().size());
+      assertFalse(loadedACL2.getEntries().contains(this.persistedEntries.get(0)));
+      assertTrue(loadedACL2.getEntries().contains(this.persistedEntries.get(1)));
+      assertTrue(loadedACL2.getEntries().contains(this.persistedEntries.get(2)));
+      assertTrue(loadedACL2.getEntries().contains(entry6));
+   }
+
+   /**
+    * <p>
+    * Persists the specified entity to the database.
+    * </p>
+    * 
+    * @param entity an {@code Object} representing the entity to be persisted.
+    */
+   private void persistEntity(Object entity)
+   {
+      EntityTransaction transaction = this.entityManager.getTransaction();
+      transaction.begin();
+      try
+      {
+         this.entityManager.persist(entity);
+         transaction.commit();
+      }
+      catch (RuntimeException re)
+      {
+         re.printStackTrace();
+         transaction.rollback();
+      }
+   }
+
+   /**
+    * <p>
+    * Removes the specified entity from the database.
+    * </p>
+    * 
+    * @param entity an {@code Object} representing the entity to be removed.
+    */
+   private void removeEntity(Object entity)
+   {
+      EntityTransaction transaction = this.entityManager.getTransaction();
+      transaction.begin();
+      try
+      {
+         this.entityManager.remove(entity);
+         transaction.commit();
+      }
+      catch (RuntimeException re)
+      {
+         re.printStackTrace();
+         transaction.rollback();
+      }
+   }
+}




More information about the jboss-cvs-commits mailing list