[jboss-svn-commits] JBoss Portal SVN: r5147 - in trunk/common: . src/main/org/jboss/portal/common/invocation src/main/org/jboss/portal/test/common

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Sep 5 10:43:44 EDT 2006


Author: julien at jboss.com
Date: 2006-09-05 10:43:38 -0400 (Tue, 05 Sep 2006)
New Revision: 5147

Added:
   trunk/common/src/main/org/jboss/portal/test/common/AbstractInvocationContextTestCase.java
Modified:
   trunk/common/build.xml
   trunk/common/src/main/org/jboss/portal/common/invocation/AbstractInvocationContext.java
   trunk/common/src/main/org/jboss/portal/common/invocation/InvocationContext.java
Log:
- adding test case for AbstractInvocationContext variable resolving strategies

Modified: trunk/common/build.xml
===================================================================
--- trunk/common/build.xml	2006-09-05 14:01:44 UTC (rev 5146)
+++ trunk/common/build.xml	2006-09-05 14:43:38 UTC (rev 5147)
@@ -186,6 +186,7 @@
    <target name="tests" depends="compile">
       <execute-tests>
          <x-test>
+            <test todir="${test.reports}" name="org.jboss.portal.test.common.AbstractInvocationContextTestCase"/>
             <test todir="${test.reports}" name="org.jboss.portal.test.common.LocaleInfoTestCase"/>
             <test todir="${test.reports}" name="org.jboss.portal.test.common.ParentChildResourceBundleTestCase"/>
             <test todir="${test.reports}" name="org.jboss.portal.test.common.ValveTestCase"/>

Modified: trunk/common/src/main/org/jboss/portal/common/invocation/AbstractInvocationContext.java
===================================================================
--- trunk/common/src/main/org/jboss/portal/common/invocation/AbstractInvocationContext.java	2006-09-05 14:01:44 UTC (rev 5146)
+++ trunk/common/src/main/org/jboss/portal/common/invocation/AbstractInvocationContext.java	2006-09-05 14:43:38 UTC (rev 5147)
@@ -39,14 +39,30 @@
       this.resolvers = new HashMap();
    }
 
-   protected final void addResolver(Scope resolverScope, InvocationContext federatedContext)
+   protected final void addResolver(Scope resolverScope, InvocationContext federatedContext) throws IllegalArgumentException
    {
-      resolvers.put(resolverScope, federatedContext);
+      if (resolverScope == null)
+      {
+         throw new IllegalArgumentException();
+      }
+      if (federatedContext == null)
+      {
+         throw new IllegalArgumentException();
+      }
+      resolvers.put(resolverScope, new InvocationContextRegistration(federatedContext));
    }
 
-   protected final void addResolver(Scope resolverScope, AttributeResolver resolver)
+   protected final void addResolver(Scope resolverScope, AttributeResolver resolver) throws IllegalArgumentException
    {
-      resolvers.put(resolverScope, resolver);
+      if (resolverScope == null)
+      {
+         throw new IllegalArgumentException();
+      }
+      if (resolver == null)
+      {
+         throw new IllegalArgumentException();
+      }
+      resolvers.put(resolverScope, new  AttributeResolverRegistration(resolver));
    }
 
    public AttributeResolver getAttributeResolver(Scope attrScope) throws IllegalArgumentException
@@ -56,16 +72,20 @@
          throw new IllegalArgumentException("Attribute name must not be null");
       }
       AttributeResolver resolver = null;
-      Object o = resolvers.get(attrScope);
-      if (o instanceof AttributeResolver)
+      Registration registration = (Registration)resolvers.get(attrScope);
+      if (registration instanceof AttributeResolverRegistration)
       {
-         resolver = (AttributeResolver)o;
+         resolver = ((AttributeResolverRegistration)registration).resolver;
       }
-      else if (o instanceof InvocationContext)
+      else if (registration instanceof InvocationContextRegistration)
       {
-         InvocationContext federaredContext = (InvocationContext)o;
+         InvocationContext federaredContext = ((InvocationContextRegistration)registration).context;
          resolver = federaredContext.getAttributeResolver(attrScope);
       }
+      else
+      {
+         throw new IllegalArgumentException("Scope not recognized " + attrScope);
+      }
       return resolver;
    }
 
@@ -75,6 +95,10 @@
       {
          throw new IllegalArgumentException("Attribute name must not be null");
       }
+      if (attrScope == null)
+      {
+         throw new IllegalArgumentException("Attribute scope must not be null");
+      }
       AttributeResolver resolver = getAttributeResolver(attrScope);
       if (resolver == null)
       {
@@ -89,6 +113,10 @@
       {
          throw new IllegalArgumentException("Attribute name must not be null");
       }
+      if (attrScope == null)
+      {
+         throw new IllegalArgumentException("Attribute scope must not be null");
+      }
       AttributeResolver resolver = getAttributeResolver(attrScope);
       if (resolver == null)
       {
@@ -101,4 +129,33 @@
    {
       setAttribute(attrScope, attrName, null);
    }
+
+   /**
+    * Typed registration to avoid issues classes that implement both interfaces AttributeResolver and InvocationContext.
+    */
+   private abstract static class Registration
+   {
+   }
+
+   private final static class AttributeResolverRegistration extends Registration
+   {
+      /** . */
+      private final AttributeResolver resolver;
+
+      private AttributeResolverRegistration(AttributeResolver resolver)
+      {
+         this.resolver = resolver;
+      }
+   }
+
+   private final static class InvocationContextRegistration extends Registration
+   {
+      /** . */
+      private final InvocationContext context;
+
+      private InvocationContextRegistration(InvocationContext context)
+      {
+         this.context = context;
+      }
+   }
 }

Modified: trunk/common/src/main/org/jboss/portal/common/invocation/InvocationContext.java
===================================================================
--- trunk/common/src/main/org/jboss/portal/common/invocation/InvocationContext.java	2006-09-05 14:01:44 UTC (rev 5146)
+++ trunk/common/src/main/org/jboss/portal/common/invocation/InvocationContext.java	2006-09-05 14:43:38 UTC (rev 5147)
@@ -34,7 +34,7 @@
     *
     * @param attrScope the  attribute resolver scope
     * @return the attribute resolver or null if not found
-    * @throws IllegalArgumentException if the attribute scope is null
+    * @throws IllegalArgumentException if the attribute scope is invalid
     */
    AttributeResolver getAttributeResolver(Scope attrScope) throws IllegalArgumentException;
 
@@ -44,7 +44,7 @@
     * @param attrScope the attribute scope
     * @param attrName the attribute name
     * @return the attribute value or null if not found
-    * @throws IllegalArgumentException if the attribute name or the attribute scope is null
+    * @throws IllegalArgumentException if the attribute name or the attribute scope is invalid
     */
    Object getAttribute(Scope attrScope, String attrName) throws IllegalArgumentException;
 
@@ -54,7 +54,7 @@
     * @param attrScope the attribute scope
     * @param attrName the attribute name
     * @param attrValue the attribute value
-    * @throws IllegalArgumentException if the attribute name of the attribute scope is null
+    * @throws IllegalArgumentException if the attribute name of the attribute scope is invalid
     */
    void setAttribute(Scope attrScope, String attrName, Object attrValue) throws IllegalArgumentException;
 
@@ -63,7 +63,7 @@
     * the resolver must treat the operation as a removal of the attribute.
     *
     * @param attrName the attribute name
-    * @throws IllegalArgumentException if the name is null
+    * @throws IllegalArgumentException if the attribute name is null or the attribute scope is invalid
     */
    void removeAttribute(Scope attrScope, String attrName);
 }

Added: trunk/common/src/main/org/jboss/portal/test/common/AbstractInvocationContextTestCase.java
===================================================================
--- trunk/common/src/main/org/jboss/portal/test/common/AbstractInvocationContextTestCase.java	2006-09-05 14:01:44 UTC (rev 5146)
+++ trunk/common/src/main/org/jboss/portal/test/common/AbstractInvocationContextTestCase.java	2006-09-05 14:43:38 UTC (rev 5147)
@@ -0,0 +1,246 @@
+/*
+* 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.portal.test.common;
+
+import junit.framework.TestCase;
+import org.jboss.portal.common.invocation.AbstractInvocationContext;
+import org.jboss.portal.common.invocation.Scope;
+import org.jboss.portal.common.invocation.AttributeResolver;
+
+import java.util.HashMap;
+
+/**
+ * @author <a href="mailto:julien at jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class AbstractInvocationContextTestCase extends TestCase
+{
+
+   /** . */
+   private static final Scope TEST_SCOPE = new Scope("test");
+
+   public void testNonExistingScope()
+   {
+      AbstractInvocationContext ctx = new AbstractInvocationContext();
+      try
+      {
+         ctx.getAttribute(TEST_SCOPE, "foo");
+         fail("was expecting an IllegalArgumentException");
+      }
+      catch (IllegalArgumentException expected)
+      {
+
+      }
+      try
+      {
+         ctx.setAttribute(TEST_SCOPE, "foo", "bar");
+         fail("was expecting an IllegalArgumentException");
+      }
+      catch (IllegalArgumentException expected)
+      {
+
+      }
+      try
+      {
+         ctx.removeAttribute(TEST_SCOPE, "foo");
+         fail("was expecting an IllegalArgumentException");
+      }
+      catch (IllegalArgumentException expected)
+      {
+
+      }
+      try
+      {
+         ctx.getAttributeResolver(TEST_SCOPE);
+         fail("was expecting an IllegalArgumentException");
+      }
+      catch (IllegalArgumentException expected)
+      {
+
+      }
+   }
+
+   public void testExistingScopeWithResolver()
+   {
+      final MapResolver resolver = new MapResolver();
+      AbstractInvocationContext ctx = new AbstractInvocationContext()
+      {
+         {
+            addResolver(TEST_SCOPE, resolver);
+         }
+      };
+      testExistingScope(ctx, resolver);
+   }
+
+   public void testExistingScopeDelegation()
+   {
+      final MapResolver resolver = new MapResolver();
+      final AbstractInvocationContext parent = new AbstractInvocationContext()
+      {
+         {
+            addResolver(TEST_SCOPE, resolver);
+         }
+      };
+      AbstractInvocationContext child = new AbstractInvocationContext()
+      {
+         {
+            addResolver(TEST_SCOPE, parent);
+         }
+      };
+      testExistingScope(child, resolver);
+   }
+
+
+   private void testExistingScope(AbstractInvocationContext ctx, MapResolver resolver)
+   {
+      // Assert empty does not exist
+      assertNull(ctx.getAttribute(TEST_SCOPE, "foo"));
+      assertEquals(0, resolver.size());
+
+      // Remove non existing
+      ctx.removeAttribute(TEST_SCOPE, "foo");
+      assertNull(ctx.getAttribute(TEST_SCOPE, "foo"));
+      assertEquals(0, resolver.size());
+
+      // Set non existing
+      ctx.setAttribute(TEST_SCOPE, "foo", "bar");
+      assertEquals("bar", resolver.getAttribute("foo"));
+      assertEquals(1, resolver.size());
+
+      // Overwrite existing
+      ctx.setAttribute(TEST_SCOPE, "foo", "bar2");
+      assertEquals("bar2", resolver.getAttribute("foo"));
+      assertEquals(1, resolver.size());
+
+      // Remove existing
+      ctx.removeAttribute(TEST_SCOPE, "foo");
+      assertNull(ctx.getAttribute(TEST_SCOPE, "foo"));
+      assertEquals(0, resolver.size());
+
+      // Get resolver
+      assertEquals(resolver, ctx.getAttributeResolver(TEST_SCOPE));
+   }
+
+   public void testAPI()
+   {
+      AbstractInvocationContext ctx = new AbstractInvocationContext();
+      try
+      {
+         ctx.getAttribute(null, "foo");
+         fail("was expecting an IllegalArgumentException");
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+      try
+      {
+         ctx.getAttribute(TEST_SCOPE, null);
+         fail("was expecting an IllegalArgumentException");
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+      try
+      {
+         ctx.setAttribute(null, "foo", "bar");
+         fail("was expecting an IllegalArgumentException");
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+      try
+      {
+         ctx.setAttribute(TEST_SCOPE, null, "bar");
+         fail("was expecting an IllegalArgumentException");
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+      try
+      {
+         ctx.setAttribute(null, "foo", null);
+         fail("was expecting an IllegalArgumentException");
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+      try
+      {
+         ctx.setAttribute(TEST_SCOPE, null, null);
+         fail("was expecting an IllegalArgumentException");
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+      try
+      {
+         ctx.removeAttribute(null, "foo");
+         fail("was expecting an IllegalArgumentException");
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+      try
+      {
+         ctx.removeAttribute(TEST_SCOPE, null);
+         fail("was expecting an IllegalArgumentException");
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+      try
+      {
+         ctx.getAttributeResolver(null);
+         fail("was expecting an IllegalArgumentException");
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+   }
+
+   private static class MapResolver extends HashMap implements AttributeResolver
+   {
+      public Object getAttribute(String attrName) throws IllegalArgumentException
+      {
+         if (attrName == null)
+         {
+            throw new IllegalArgumentException();
+         }
+         return get(attrName);
+      }
+      public void setAttribute(String attrName, Object attrValue) throws IllegalArgumentException
+      {
+         if (attrName == null)
+         {
+            throw new IllegalArgumentException();
+         }
+         if (attrValue != null)
+         {
+            put(attrName, attrValue);
+         }
+         else
+         {
+            remove(attrName);
+         }
+      }
+   }
+}




More information about the jboss-svn-commits mailing list