[jboss-cvs] JBoss Messaging SVN: r4428 - in trunk: src/main/org/jboss/messaging/core/security/impl and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Jun 11 08:52:05 EDT 2008


Author: jmesnil
Date: 2008-06-11 08:52:05 -0400 (Wed, 11 Jun 2008)
New Revision: 4428

Added:
   trunk/tests/src/org/jboss/messaging/tests/unit/core/security/RoleTest.java
Modified:
   trunk/src/main/org/jboss/messaging/core/security/CheckType.java
   trunk/src/main/org/jboss/messaging/core/security/impl/SecurityStoreImpl.java
   trunk/tests/src/org/jboss/messaging/tests/unit/core/security/impl/SecurityStoreImplTest.java
Log:
refactored CheckType as an enum
added unit test for security.Role class

Modified: trunk/src/main/org/jboss/messaging/core/security/CheckType.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/security/CheckType.java	2008-06-11 12:06:26 UTC (rev 4427)
+++ trunk/src/main/org/jboss/messaging/core/security/CheckType.java	2008-06-11 12:52:05 UTC (rev 4428)
@@ -25,31 +25,12 @@
  * 
  * @author Peter Antman
  * @author <a href="mailto:Scott.Stark at jboss.org">Scott Stark</a>
+ * @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
  * @version $Revision: 2925 $
  *
  * $Id: $
  */
-public class CheckType
+public enum CheckType
 {	
-   public final int type;
-   public CheckType(int type)
-   {
-      this.type = type;
-   }      
-   public static final int TYPE_READ = 0;
-   public static final int TYPE_WRITE = 1;
-   public static final int TYPE_CREATE = 2;
-   public static CheckType READ = new CheckType(TYPE_READ);
-   public static CheckType WRITE = new CheckType(TYPE_WRITE);
-   public static CheckType CREATE = new CheckType(TYPE_CREATE);      
-   public boolean equals(Object other)
-   {
-      if (!(other instanceof CheckType)) return false;
-      CheckType ct = (CheckType)other;
-      return ct.type == this.type;
-   }
-   public int hashCode() 
-   {
-      return type;
-   }
+   READ, WRITE, CREATE;   
 }

Modified: trunk/src/main/org/jboss/messaging/core/security/impl/SecurityStoreImpl.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/security/impl/SecurityStoreImpl.java	2008-06-11 12:06:26 UTC (rev 4427)
+++ trunk/src/main/org/jboss/messaging/core/security/impl/SecurityStoreImpl.java	2008-06-11 12:52:05 UTC (rev 4428)
@@ -120,19 +120,19 @@
          }
          // if we get here we're granted, add to the cache
    
-         switch (checkType.type)
+         switch (checkType)
          {
-            case CheckType.TYPE_READ:
+            case READ:
             {
                readCache.add(address);
                break;
             }
-            case CheckType.TYPE_WRITE:
+            case WRITE:
             {
                writeCache.add(address);
                break;
             }
-            case CheckType.TYPE_CREATE:
+            case CREATE:
             {
                createCache.add(address);
                break;
@@ -191,19 +191,19 @@
       }
       else
       {
-         switch (checkType.type)
+         switch (checkType)
          {
-            case CheckType.TYPE_READ:
+            case READ:
             {
                granted = readCache.contains(dest);
                break;
             }
-            case CheckType.TYPE_WRITE:
+            case WRITE:
             {
                granted = writeCache.contains(dest);
                break;
             }
-            case CheckType.TYPE_CREATE:
+            case CREATE:
             {
                granted = createCache.contains(dest);
                break;

Added: trunk/tests/src/org/jboss/messaging/tests/unit/core/security/RoleTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/tests/unit/core/security/RoleTest.java	                        (rev 0)
+++ trunk/tests/src/org/jboss/messaging/tests/unit/core/security/RoleTest.java	2008-06-11 12:52:05 UTC (rev 4428)
@@ -0,0 +1,104 @@
+/*
+ * JBoss, Home of Professional Open Source
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package org.jboss.messaging.tests.unit.core.security;
+
+import static org.jboss.messaging.core.security.CheckType.CREATE;
+import static org.jboss.messaging.core.security.CheckType.READ;
+import static org.jboss.messaging.core.security.CheckType.WRITE;
+import junit.framework.TestCase;
+
+import org.jboss.messaging.core.security.CheckType;
+import org.jboss.messaging.core.security.Role;
+
+/**
+ * @author <a href="mailto:jmesnil at redhat.com">Jeff Mesnil</a>
+ *
+ * @version <tt>$Revision$</tt>
+ *
+ */
+public class RoleTest extends TestCase
+{
+   // Constants -----------------------------------------------------
+
+   // Attributes ----------------------------------------------------
+
+   // Static --------------------------------------------------------
+
+   // Constructors --------------------------------------------------
+
+   // Public --------------------------------------------------------
+
+   public void testDefaultRole() throws Exception
+   {
+      Role role = new Role("testDefaultRole");
+      assertEquals("testDefaultRole", role.getName());
+      assertFalse(role.isCheckType(READ));
+      assertFalse(role.isCheckType(WRITE));
+      assertFalse(role.isCheckType(CREATE));      
+   }
+   
+   public void testReadRole() throws Exception
+   {
+      Role role = new Role("testReadRole", true, false, false);
+      assertTrue(role.isCheckType(READ));
+      assertFalse(role.isCheckType(WRITE));
+      assertFalse(role.isCheckType(CREATE));      
+   }
+   
+   public void testWriteRole() throws Exception
+   {
+      Role role = new Role("testWriteRole", false, true, false);
+      assertFalse(role.isCheckType(READ));
+      assertTrue(role.isCheckType(WRITE));
+      assertFalse(role.isCheckType(CREATE));      
+   }
+
+   public void testCreateRole() throws Exception
+   {
+      Role role = new Role("testWriteRole", false, false, true);
+      assertFalse(role.isCheckType(READ));
+      assertFalse(role.isCheckType(WRITE));
+      assertTrue(role.isCheckType(CREATE));      
+   }
+   
+   public void testEqualsAndHashcode() throws Exception
+   {
+      Role role = new Role("testEquals", true, true, true);
+      Role sameRole = new Role("testEquals", true, true, true);
+      Role roleWithDifferentName = new Role("notEquals", true, true, true);
+      Role roleWithDifferentRead = new Role("testEquals", false, true, true);
+      Role roleWithDifferentWrite = new Role("testEquals", true, false, true);
+      Role roleWithDifferentCreate = new Role("testEquals", true, true, false);
+
+      assertTrue(role.equals(role));
+
+      assertTrue(role.equals(sameRole));
+      assertTrue(role.hashCode() == sameRole.hashCode());
+      
+      assertFalse(role.equals(roleWithDifferentName));
+      assertFalse(role.hashCode() == roleWithDifferentName.hashCode());
+      
+      assertFalse(role.equals(roleWithDifferentRead));
+      assertFalse(role.hashCode() == roleWithDifferentRead.hashCode());
+      
+      assertFalse(role.equals(roleWithDifferentWrite));
+      assertFalse(role.hashCode() == roleWithDifferentWrite.hashCode());
+      
+      assertFalse(role.equals(roleWithDifferentCreate));
+      assertFalse(role.hashCode() == roleWithDifferentCreate.hashCode());
+      
+      assertFalse(role.equals(null));
+   }
+   
+   // Package protected ---------------------------------------------
+
+   // Protected -----------------------------------------------------
+
+   // Private -------------------------------------------------------
+
+   // Inner classes -------------------------------------------------
+}

Modified: trunk/tests/src/org/jboss/messaging/tests/unit/core/security/impl/SecurityStoreImplTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/tests/unit/core/security/impl/SecurityStoreImplTest.java	2008-06-11 12:06:26 UTC (rev 4427)
+++ trunk/tests/src/org/jboss/messaging/tests/unit/core/security/impl/SecurityStoreImplTest.java	2008-06-11 12:52:05 UTC (rev 4428)
@@ -200,47 +200,4 @@
       securityStore.check(address, CheckType.CREATE, serverConnection);
 
    }
-
-   public void testInvalidCheckType() throws Exception
-   {
-
-      CheckType badCheckType = new CheckType(4);
-      JBMSecurityManager securityManager = EasyMock.createStrictMock(JBMSecurityManager.class);
-      securityStore.setSecurityManager(securityManager);
-      //noinspection unchecked
-      HierarchicalRepository<HashSet<Role>> repository = EasyMock.createStrictMock(HierarchicalRepository.class);
-
-      SimpleString address = new SimpleString("anaddress");
-      HashSet<Role> roles = new HashSet<Role>();
-      roles.add(new Role("user", false, false, true));
-      repository.registerListener(securityStore);
-      EasyMock.expect(repository.getMatch(address.toString())).andReturn(roles);
-      ServerConnection serverConnection = EasyMock.createNiceMock(ServerConnection.class);
-      EasyMock.expect(serverConnection.getUsername()).andReturn("user");
-      EasyMock.expect(serverConnection.getPassword()).andReturn("password");
-      EasyMock.expect(securityManager.validateUserAndRole("user", "password", roles, badCheckType)).andReturn(true);
-      EasyMock.replay(repository);
-      EasyMock.replay(securityManager);
-      EasyMock.replay(serverConnection);
-      securityStore.setSecurityRepository(repository);
-      try
-      {
-         securityStore.check(address, badCheckType, serverConnection);
-         fail("should throw exception");
-      }
-      catch (IllegalArgumentException e)
-      {
-         e.printStackTrace();
-      }
-      //now try cached
-      try
-      {
-         securityStore.check(address, badCheckType, serverConnection);
-         fail("should throw exception");
-      }
-      catch (IllegalArgumentException e)
-      {
-         e.printStackTrace();
-      }
-   }
 }




More information about the jboss-cvs-commits mailing list