hivemind-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject cvs commit: jakarta-hivemind/framework/src/test/org/apache/hivemind TestDefense.java
Date Sat, 20 Nov 2004 16:55:18 GMT
hlship      2004/11/20 08:55:18

  Modified:    framework/src/java/org/apache/hivemind HiveMindMessages.java
                        HiveMindStrings.properties Defense.java
               framework/src/test/org/apache/hivemind TestDefense.java
  Log:
  Add additional method to Defense for checking method parameter types.
  
  Revision  Changes    Path
  1.5       +9 -0      jakarta-hivemind/framework/src/java/org/apache/hivemind/HiveMindMessages.java
  
  Index: HiveMindMessages.java
  ===================================================================
  RCS file: /home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/HiveMindMessages.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- HiveMindMessages.java	2 Nov 2004 17:46:09 -0000	1.4
  +++ HiveMindMessages.java	20 Nov 2004 16:55:18 -0000	1.5
  @@ -15,6 +15,7 @@
   package org.apache.hivemind;
   
   import org.apache.hivemind.impl.MessageFormatter;
  +import org.apache.hivemind.service.ClassFabUtils;
   
   /**
    * Used to format general-purpose messages used in code within HiveMind.
  @@ -45,5 +46,13 @@
       public static String paramNotNull(String parameterName)
       {
           return _formatter.format("param-not-null", parameterName);
  +    }
  +
  +    /** @since 1.1 */
  +    public static String paramWrongType(String parameterName, Object parameter, Class expectedType)
  +    {
  +        return _formatter.format("param-wrong-type", parameterName, ClassFabUtils
  +                .getJavaClassName(parameter.getClass()), ClassFabUtils
  +                .getJavaClassName(expectedType));
       }
   }
  
  
  
  1.4       +1 -0      jakarta-hivemind/framework/src/java/org/apache/hivemind/HiveMindStrings.properties
  
  Index: HiveMindStrings.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/HiveMindStrings.properties,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- HiveMindStrings.properties	2 Nov 2004 17:46:09 -0000	1.3
  +++ HiveMindStrings.properties	20 Nov 2004 16:55:18 -0000	1.4
  @@ -17,6 +17,7 @@
   unimplemented-method=Class {0} must provide an overridden implementation of method {1}().
   registry-shutdown=The HiveMind Registry has been shutdown.
   param-not-null=Parameter {0} must not be null.
  +param-wrong-type=Parameter {0} is of type {1} which is not compatible with {2}.
   
   
   
  
  
  
  1.3       +18 -1     jakarta-hivemind/framework/src/java/org/apache/hivemind/Defense.java
  
  Index: Defense.java
  ===================================================================
  RCS file: /home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/Defense.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Defense.java	4 Nov 2004 14:32:33 -0000	1.2
  +++ Defense.java	20 Nov 2004 16:55:18 -0000	1.3
  @@ -14,7 +14,6 @@
   
   package org.apache.hivemind;
   
  -
   /**
    * Simple utilities for defensive programming.
    * 
  @@ -38,5 +37,23 @@
       {
           if (parameter == null)
               throw new NullPointerException(HiveMindMessages.paramNotNull(parameterName));
  +    }
  +
  +    /**
  +     * Check that a parameter is not null, and assignable to a particular type.
  +     * 
  +     * @throws ClassCastException
  +     *             if not assignable (with a useable message)
  +     */
  +
  +    public static void isAssignable(Object parameter, Class expectedType, String parameterName)
  +    {
  +        Defense.notNull(parameter, parameterName);
  +
  +        if (!expectedType.isAssignableFrom(parameter.getClass()))
  +            throw new ClassCastException(HiveMindMessages.paramWrongType(
  +                    parameterName,
  +                    parameter,
  +                    expectedType));
       }
   }
  
  
  
  1.3       +38 -3     jakarta-hivemind/framework/src/test/org/apache/hivemind/TestDefense.java
  
  Index: TestDefense.java
  ===================================================================
  RCS file: /home/cvs/jakarta-hivemind/framework/src/test/org/apache/hivemind/TestDefense.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestDefense.java	4 Nov 2004 14:32:33 -0000	1.2
  +++ TestDefense.java	20 Nov 2004 16:55:18 -0000	1.3
  @@ -18,19 +18,19 @@
   import org.apache.hivemind.test.HiveMindTestCase;
   
   /**
  - * Test for {@link org.apache.hivemind.Defense#notNull(Object, String)}.
  + * Test for {@link org.apache.hivemind.Defense}.
    * 
    * @author Howard Lewis Ship
    * @since 1.1
    */
   public class TestDefense extends HiveMindTestCase
   {
  -    public void testSuccess()
  +    public void testNotNull()
       {
           Defense.notNull("foo", "bar");
       }
   
  -    public void testFailure()
  +    public void testIsNull()
       {
           try
           {
  @@ -40,6 +40,41 @@
           catch (NullPointerException ex)
           {
               assertEquals(HiveMindMessages.paramNotNull("woops"), ex.getMessage());
  +        }
  +    }
  +
  +    public void testCorrectType()
  +    {
  +        Defense.isAssignable("Hello", String.class, "param");
  +    }
  +
  +    public void testIncorrectType()
  +    {
  +        try
  +        {
  +            Defense.isAssignable("Hello", Number.class, "param");
  +            unreachable();
  +        }
  +        catch (ClassCastException ex)
  +        {
  +            assertEquals(
  +                    "Parameter param is of type java.lang.String which is not compatible
with java.lang.Number.",
  +                    ex.getMessage());
  +        }
  +    }
  +
  +    public void testIncorrectTypeWithArrays()
  +    {
  +        try
  +        {
  +            Defense.isAssignable(new int[0], String[].class, "param");
  +            unreachable();
  +        }
  +        catch (ClassCastException ex)
  +        {
  +            assertEquals(
  +                    "Parameter param is of type int[] which is not compatible with java.lang.String[].",
  +                    ex.getMessage());
           }
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-cvs-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-cvs-help@jakarta.apache.org


Mime
View raw message