[jboss-cvs] JBoss Messaging SVN: r3629 - in trunk: tests/src/org/jboss/jms/server/security/test/unit and 4 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Jan 24 09:21:00 EST 2008


Author: ataylor
Date: 2008-01-24 09:21:00 -0500 (Thu, 24 Jan 2008)
New Revision: 3629

Added:
   trunk/tests/src/org/jboss/messaging/util/
   trunk/tests/src/org/jboss/messaging/util/test/
   trunk/tests/src/org/jboss/messaging/util/test/unit/
   trunk/tests/src/org/jboss/messaging/util/test/unit/RepositoryTest.java
Removed:
   trunk/tests/src/org/jboss/jms/server/security/test/unit/SecurityRepositoryTest.java
Modified:
   trunk/src/main/org/jboss/messaging/util/HierarchicalObjectRepository.java
   trunk/src/main/org/jboss/messaging/util/Match.java
Log:
added proper matching for the hierarchicalrepository and added tests

Modified: trunk/src/main/org/jboss/messaging/util/HierarchicalObjectRepository.java
===================================================================
--- trunk/src/main/org/jboss/messaging/util/HierarchicalObjectRepository.java	2008-01-24 09:38:17 UTC (rev 3628)
+++ trunk/src/main/org/jboss/messaging/util/HierarchicalObjectRepository.java	2008-01-24 14:21:00 UTC (rev 3629)
@@ -54,7 +54,7 @@
       }
       else
       {
-         Match match1 = new Match(match);
+         Match<E> match1 = new Match<E>(match);
          match1.setValue(value);
          matches.put(match, match1);
       }
@@ -65,18 +65,11 @@
     * @param match the match to look for
     * @return the value
     */
-   //todo implement a better algorithm for returning a match!
    public E getMatch(String match)
    {
-      for (Match securityMatch : matches.values())
-      {
-         if(securityMatch.getPattern().matcher(match).matches())
-         {
-            //noinspection unchecked
-            return (E) securityMatch.getValue();
-         }
-      }
-      return defaultmatch;
+      HashMap<String, Match<E>> possibleMatches = getPossibleMatches(match);
+      E actualMatch  = getActualMatch(match, possibleMatches);
+      return actualMatch != null ? actualMatch:defaultmatch;
    }
 
    /**
@@ -96,4 +89,77 @@
    {
       defaultmatch = defaultValue;
    }
+
+   private HashMap<String, Match<E>> getPossibleMatches(String match)
+   {
+      HashMap<String, Match<E>> possibleMatches = new HashMap<String, Match<E>>();
+      for(String key : matches.keySet())
+      {
+         if(matches.get(key).getPattern().matcher(match).matches())
+         {
+            //noinspection unchecked
+            possibleMatches.put(key, matches.get(key));
+         }
+      }
+      return possibleMatches;
+   }
+
+
+   private E getActualMatch(String match, HashMap<String, Match<E>> possibleMatches)
+   {
+      E value = null;
+      Match<E> currentVal = null;
+      for(String key : possibleMatches.keySet())
+      {
+         currentVal = compareMatches(match, currentVal, possibleMatches.get(key));
+      }
+      if(currentVal != null)
+      {
+         value = currentVal.getValue();
+      }
+      return value;
+   }
+
+   private Match<E> compareMatches(String match, Match<E> currentVal, Match<E> replacementVal)
+   {
+      boolean moreSpecific = false;
+      if(currentVal == null)
+      {
+         moreSpecific = true;
+      }
+      else
+      {
+         String[] parts = match.split("\\.");
+         for(int i = 0; i < parts.length; i++)
+         {
+            String left = getPart(i, currentVal.getMatch());
+            String right = getPart(i, replacementVal.getMatch());
+            if(!left.equals(right) && parts[i].equals(right))
+            {
+               moreSpecific = true;
+               if("*".equals(left))
+               {
+                  break;
+               }
+            }
+            else
+            {
+               moreSpecific = false;
+            }
+         }
+      }
+      return moreSpecific? replacementVal : currentVal;
+   }
+
+   private String getPart(int i, String match)
+   {
+      String[] parts = match.split("\\.");
+      if(parts != null &&  parts.length > i)
+      {
+         return parts[i];
+      }
+      return null;
+   }
+
+
 }

Modified: trunk/src/main/org/jboss/messaging/util/Match.java
===================================================================
--- trunk/src/main/org/jboss/messaging/util/Match.java	2008-01-24 09:38:17 UTC (rev 3628)
+++ trunk/src/main/org/jboss/messaging/util/Match.java	2008-01-24 14:21:00 UTC (rev 3629)
@@ -26,11 +26,11 @@
 /**
     * a Match is the holder for the match string and the object to hold against it.
  */
-public class Match
+public class Match<E>
 {
    private String match;
    private Pattern pattern;
-   private Object value;
+   private E value;
 
 
    public Match(String match)
@@ -63,12 +63,12 @@
    }
 
 
-   public Object getValue()
+   public E getValue()
    {
       return value;
    }
 
-   public void setValue(Object value)
+   public void setValue(E value)
    {
       this.value = value;
    }

Deleted: trunk/tests/src/org/jboss/jms/server/security/test/unit/SecurityRepositoryTest.java
===================================================================
--- trunk/tests/src/org/jboss/jms/server/security/test/unit/SecurityRepositoryTest.java	2008-01-24 09:38:17 UTC (rev 3628)
+++ trunk/tests/src/org/jboss/jms/server/security/test/unit/SecurityRepositoryTest.java	2008-01-24 14:21:00 UTC (rev 3629)
@@ -1,85 +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.jms.server.security.test.unit;
-
-import junit.framework.TestCase;
-import org.jboss.jms.server.security.Role;
-import org.jboss.messaging.util.HierarchicalObjectRepository;
-import org.jboss.messaging.util.HierarchicalRepository;
-
-import java.util.HashSet;
-
-/**
- * @author <a href="ataylor at redhat.com">Andy Taylor</a>
- */
-public class SecurityRepositoryTest extends TestCase
-{
-   HierarchicalRepository<HashSet<Role>> securityRepository;
-
-
-   protected void setUp() throws Exception
-   {
-      securityRepository = new HierarchicalObjectRepository<HashSet<Role>>();
-   }
-
-   public void testDefault()
-   {
-      securityRepository.setDefault(new HashSet<Role>());
-      HashSet<Role> roles = securityRepository.getMatch("queues.something");
-
-      assertEquals(roles.size(), 0);
-   }
-
-   public void testSingleMatch()
-   {
-      securityRepository.addMatch("queues.*", new HashSet<Role>());
-      HashSet<Role> hashSet = securityRepository.getMatch("queues.something");
-      assertEquals(hashSet.size(), 0);
-   }
-
-   public void testSingletwo()
-   {
-      securityRepository.addMatch("queues.another.aq.*", new HashSet<Role>());
-      HashSet<Role> roles = new HashSet<Role>(2);
-      roles.add(new Role("test1"));
-      roles.add(new Role("test2"));
-      securityRepository.addMatch("queues.aq", roles);
-      HashSet<Role> roles2 = new HashSet<Role>(2);
-      roles2.add(new Role("test1"));
-      roles2.add(new Role("test2"));
-      roles2.add(new Role("test3"));
-      securityRepository.addMatch("queues.another.andanother.*", roles2);
-      HashSet<Role> hashSet = securityRepository.getMatch("queues.another.andanother");
-      assertEquals(hashSet.size(), 3);
-   }
-
-   public void testWithoutWildcard()
-   {
-      securityRepository.addMatch("queues.1.*", new HashSet<Role>());
-      HashSet<Role> roles = new HashSet<Role>(2);
-      roles.add(new Role("test1"));
-      roles.add(new Role("test2"));
-      securityRepository.addMatch("queues.2.aq", roles);
-      HashSet<Role> hashSet = securityRepository.getMatch("queues.2.aq");
-      assertEquals(hashSet.size(), 2);
-   }
-}

Copied: trunk/tests/src/org/jboss/messaging/util/test/unit/RepositoryTest.java (from rev 3626, trunk/tests/src/org/jboss/jms/server/security/test/unit/SecurityRepositoryTest.java)
===================================================================
--- trunk/tests/src/org/jboss/messaging/util/test/unit/RepositoryTest.java	                        (rev 0)
+++ trunk/tests/src/org/jboss/messaging/util/test/unit/RepositoryTest.java	2008-01-24 14:21:00 UTC (rev 3629)
@@ -0,0 +1,123 @@
+/*
+   * 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.messaging.util.test.unit;
+
+import junit.framework.TestCase;
+import org.jboss.jms.server.security.Role;
+import org.jboss.messaging.util.HierarchicalObjectRepository;
+import org.jboss.messaging.util.HierarchicalRepository;
+
+import java.util.HashSet;
+
+/**
+ * @author <a href="ataylor at redhat.com">Andy Taylor</a>
+ */
+public class RepositoryTest extends TestCase
+{
+   HierarchicalRepository<HashSet<Role>> securityRepository;
+
+
+   protected void setUp() throws Exception
+   {
+      securityRepository = new HierarchicalObjectRepository<HashSet<Role>>();
+   }
+
+   public void testDefault()
+   {
+      securityRepository.setDefault(new HashSet<Role>());
+      HashSet<Role> roles = securityRepository.getMatch("queues.something");
+
+      assertEquals(roles.size(), 0);
+   }
+
+   public void testSingleMatch()
+   {
+      securityRepository.addMatch("queues.*", new HashSet<Role>());
+      HashSet<Role> hashSet = securityRepository.getMatch("queues.something");
+      assertEquals(hashSet.size(), 0);
+   }
+
+   public void testSingletwo()
+   {
+      securityRepository.addMatch("queues.another.aq.*", new HashSet<Role>());
+      HashSet<Role> roles = new HashSet<Role>(2);
+      roles.add(new Role("test1"));
+      roles.add(new Role("test2"));
+      securityRepository.addMatch("queues.aq", roles);
+      HashSet<Role> roles2 = new HashSet<Role>(2);
+      roles2.add(new Role("test1"));
+      roles2.add(new Role("test2"));
+      roles2.add(new Role("test3"));
+      securityRepository.addMatch("queues.another.andanother.*", roles2);
+      HashSet<Role> hashSet = securityRepository.getMatch("queues.another.andanother");
+      assertEquals(hashSet.size(), 3);
+   }
+
+   public void testWithoutWildcard()
+   {
+      securityRepository.addMatch("queues.1.*", new HashSet<Role>());
+      HashSet<Role> roles = new HashSet<Role>(2);
+      roles.add(new Role("test1"));
+      roles.add(new Role("test2"));
+      securityRepository.addMatch("queues.2.aq", roles);
+      HashSet<Role> hashSet = securityRepository.getMatch("queues.2.aq");
+      assertEquals(hashSet.size(), 2);
+   }
+
+   public void testMultipleWildcards()
+   {
+      HierarchicalRepository<String> repository = new HierarchicalObjectRepository<String>();
+      repository.addMatch("a", "a");
+      repository.addMatch("a.*", "a.*");
+      repository.addMatch("a.b.c", "a.b.c");
+      repository.addMatch("a.*.c", "a.*.c");
+      repository.addMatch("a.d.c", "a.d.c");
+      repository.addMatch("a.b.*", "a.b.*");
+      repository.addMatch("a.b", "a.b");
+
+      repository.addMatch("a.b.c.*", "a.b.c.*");
+      repository.addMatch("a.b.c.d", "a.b.c.d");
+      repository.addMatch("a.*.*.d", "a.*.*.d");
+      repository.addMatch("a.*.d.*", "a.*.d.*");
+      String val = repository.getMatch("a.b");
+      assertEquals("a.b", val);
+      val = repository.getMatch("a.x");
+      assertEquals("a.*", val);
+      val = repository.getMatch("a.b.x");
+      assertEquals("a.b.*", val);
+      val = repository.getMatch("a.b.c");
+      assertEquals("a.b.c", val);
+      val = repository.getMatch("a.d.c");
+      assertEquals("a.d.c", val);
+      val = repository.getMatch("a.x.c");
+      assertEquals("a.*.c", val);
+      val = repository.getMatch("a.b.c.d");
+      assertEquals("a.b.c.d", val);
+      val = repository.getMatch("a.x.c.d");
+      assertEquals("a.*.*.d", val);
+      val = repository.getMatch("a.b.x.d");
+      assertEquals("a.b.*", val);
+      val = repository.getMatch("a.d.x.d");
+      assertEquals("a.*.*.d", val);
+
+   }
+}




More information about the jboss-cvs-commits mailing list