You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2004/03/10 02:59:56 UTC

cvs commit: jakarta-tapestry/framework/src/org/apache/tapestry/valid NumberValidator.java

hlship      2004/03/09 17:59:56

  Modified:    .        status.xml
               junit/src/org/apache/tapestry/junit/valid
                        TestNumberValidator.java
               framework/src/org/apache/tapestry/valid NumberValidator.java
  Log:
  [ 27082 ] ClassCastException when entering "0" as minimum for a "double" NumberValidator
  
  Revision  Changes    Path
  1.25      +4 -0      jakarta-tapestry/status.xml
  
  Index: status.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/status.xml,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- status.xml	10 Mar 2004 00:03:53 -0000	1.24
  +++ status.xml	10 Mar 2004 01:59:55 -0000	1.25
  @@ -248,6 +248,10 @@
   Check for duplication of ids between the HTML template and the specification.
   </action>
   
  +<action type="fix" dev="HLS" fixed-bug="27082">
  +Fix some number conversion problems inside NumberValidator.	
  +</action>
  +
    </release>
     	
     
  
  
  
  1.7       +73 -1     jakarta-tapestry/junit/src/org/apache/tapestry/junit/valid/TestNumberValidator.java
  
  Index: TestNumberValidator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/junit/src/org/apache/tapestry/junit/valid/TestNumberValidator.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TestNumberValidator.java	19 Feb 2004 17:37:51 -0000	1.6
  +++ TestNumberValidator.java	10 Mar 2004 01:59:55 -0000	1.7
  @@ -213,4 +213,76 @@
               BigDecimal.class,
               new BigDecimal("-29574923857342908743.29058734289734907543289752345897234590872349085"));
       }
  +
  +    /** @since 3.0 **/
  +
  +    private void checkAdaptorType(int expectedType, Class numberType)
  +    {
  +        NumberValidator.NumberAdaptor a = NumberValidator.getAdaptor(numberType);
  +
  +        assertEquals(expectedType, a.getNumberType());
  +    }
  +
  +    /** @since 3.0 **/
  +
  +    public void testAdaptorTypes() throws Exception
  +    {
  +        checkAdaptorType(NumberValidator.NUMBER_TYPE_INTEGER, Byte.class);
  +        checkAdaptorType(NumberValidator.NUMBER_TYPE_INTEGER, Short.class);
  +        checkAdaptorType(NumberValidator.NUMBER_TYPE_INTEGER, Integer.class);
  +        checkAdaptorType(NumberValidator.NUMBER_TYPE_INTEGER, Long.class);
  +        checkAdaptorType(NumberValidator.NUMBER_TYPE_INTEGER, BigInteger.class);
  +        checkAdaptorType(NumberValidator.NUMBER_TYPE_REAL, Float.class);
  +        checkAdaptorType(NumberValidator.NUMBER_TYPE_REAL, Double.class);
  +        checkAdaptorType(NumberValidator.NUMBER_TYPE_REAL, BigDecimal.class);
  +    }
  +
  +    /** @since 3.0 **/
  +
  +    private void checkCompare(Number left, Number right)
  +    {
  +        NumberValidator.NumberAdaptor a = NumberValidator.getAdaptor(left.getClass());
  +
  +        assertEquals(0, a.compare(left, right));
  +    }
  +
  +    public void testByteCompare()
  +    {
  +        checkCompare(new Byte((byte) 3), new Long(3));
  +    }
  +
  +    public void testShortCompare()
  +    {
  +        checkCompare(new Short((short) 14), new Double(14.0));
  +    }
  +
  +    public void testIntegerCompare()
  +    {
  +        checkCompare(new Integer(19), new Long(19));
  +    }
  +
  +    public void testLongCompare()
  +    {
  +        checkCompare(new Long(-22), new Short((short) - 22));
  +    }
  +
  +    public void testBigIntegerCompare()
  +    {
  +        checkCompare(new BigInteger("300"), new Long("300"));
  +    }
  +    
  +    public void testFloatCompare()
  +    {
  +    	checkCompare(new Float("0"), new Double("0"));
  +    }
  +    
  +    public void testDoubleCompare()
  +    {
  +    	checkCompare(new Double("0"), new Float("0"));
  +    }
  +    
  +    public void testBigDecimalCompare()
  +    {
  +    	checkCompare(new BigDecimal("-137.75"), new Double("-137.75"));
  +    }
   }
  
  
  
  1.10      +80 -7     jakarta-tapestry/framework/src/org/apache/tapestry/valid/NumberValidator.java
  
  Index: NumberValidator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/valid/NumberValidator.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- NumberValidator.java	19 Feb 2004 17:38:03 -0000	1.9
  +++ NumberValidator.java	10 Mar 2004 01:59:56 -0000	1.10
  @@ -89,10 +89,15 @@
   
       private static AdaptorRegistry _numberAdaptors = new AdaptorRegistry();
   
  -    private final static int NUMBER_TYPE_INTEGER    = 0;
  -    private final static int NUMBER_TYPE_REAL       = 1;
  +    public final static int NUMBER_TYPE_INTEGER = 0;
  +	public final static int NUMBER_TYPE_REAL = 1;
   
  -    private static abstract class NumberAdaptor
  +	/**
  +	 * This class is not meant for use outside of NumberValidator; it
  +	 * is public only to fascilitate some unit testing.
  +	 * 
  +	 */
  +    public static abstract class NumberAdaptor
       {
           /**
            *  Parses a non-empty {@link String} into the correct subclass of
  @@ -115,10 +120,22 @@
   
           public int compare(Number left, Number right)
           {
  +            if (!left.getClass().equals(right.getClass()))
  +                right = coerce(right);
  +
               Comparable lc = (Comparable) left;
   
               return lc.compareTo(right);
           }
  +
  +        /**
  +         * Invoked when comparing two Numbers of different types.
  +         * The number is cooerced from its ordinary type to 
  +         * the correct type for comparison.
  +         * 
  +         * @since 3.0
  +         */
  +        protected abstract Number coerce(Number number);
       }
   
       private static abstract class IntegerNumberAdaptor extends NumberAdaptor
  @@ -143,6 +160,11 @@
           {
               return new Byte(value);
           }
  +
  +        protected Number coerce(Number number)
  +        {
  +            return new Byte(number.byteValue());
  +        }
       }
   
       private static class ShortAdaptor extends IntegerNumberAdaptor
  @@ -151,6 +173,11 @@
           {
               return new Short(value);
           }
  +
  +        protected Number coerce(Number number)
  +        {
  +            return new Short(number.shortValue());
  +        }
       }
   
       private static class IntAdaptor extends IntegerNumberAdaptor
  @@ -159,6 +186,11 @@
           {
               return new Integer(value);
           }
  +
  +        protected Number coerce(Number number)
  +        {
  +            return new Integer(number.intValue());
  +        }
       }
   
       private static class LongAdaptor extends IntegerNumberAdaptor
  @@ -167,6 +199,11 @@
           {
               return new Long(value);
           }
  +
  +        protected Number coerce(Number number)
  +        {
  +            return new Long(number.longValue());
  +        }
       }
   
       private static class FloatAdaptor extends RealNumberAdaptor
  @@ -175,6 +212,11 @@
           {
               return new Float(value);
           }
  +
  +        protected Number coerce(Number number)
  +        {
  +            return new Float(number.floatValue());
  +        }
       }
   
       private static class DoubleAdaptor extends RealNumberAdaptor
  @@ -183,6 +225,11 @@
           {
               return new Double(value);
           }
  +
  +        protected Number coerce(Number number)
  +        {
  +            return new Double(number.doubleValue());
  +        }
       }
   
       private static class BigDecimalAdaptor extends RealNumberAdaptor
  @@ -191,6 +238,11 @@
           {
               return new BigDecimal(value);
           }
  +
  +        protected Number coerce(Number number)
  +        {
  +            return new BigDecimal(number.doubleValue());
  +        }
       }
   
       private static class BigIntegerAdaptor extends IntegerNumberAdaptor
  @@ -199,6 +251,11 @@
           {
               return new BigInteger(value);
           }
  +
  +        protected Number coerce(Number number)
  +        {
  +            return new BigInteger(number.toString());
  +        }
       }
   
       static {
  @@ -244,7 +301,7 @@
   
       private NumberAdaptor getAdaptor(IFormComponent field)
       {
  -        NumberAdaptor result = (NumberAdaptor) _numberAdaptors.getAdaptor(_valueTypeClass);
  +        NumberAdaptor result = getAdaptor(_valueTypeClass);
   
           if (result == null)
               throw new ApplicationRuntimeException(
  @@ -256,6 +313,22 @@
           return result;
       }
   
  +	/**
  +	 * Returns an adaptor for the given type.
  +	 * 
  +	 * <p>
  +	 * Note: this method exists only for testing purposes. It is not meant to
  +	 * be invoked by user code and is subject to change at any time.
  +	 * 
  +	 * @param type the type (a Number subclass) for which to return an adaptor
  +	 * @returns the adaptor, or null if no such adaptor may be found
  +	 * @since 3.0
  +	 */
  +    public static NumberAdaptor getAdaptor(Class type)
  +    {
  +        return (NumberAdaptor) _numberAdaptors.getAdaptor(type);
  +    }
  +
       public Object toObject(IFormComponent field, String value) throws ValidatorException
       {
           if (checkRequired(field, value))
  @@ -597,13 +670,13 @@
       }
   
       /** @since 3.0 */
  -    
  +
       public boolean isIntegerNumber()
       {
           NumberAdaptor result = (NumberAdaptor) _numberAdaptors.getAdaptor(_valueTypeClass);
           if (result == null)
               return false;
  -        
  +
           return result.getNumberType() == NUMBER_TYPE_INTEGER;
       }
   }
  
  
  

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