You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by di...@apache.org on 2004/11/24 00:25:44 UTC

cvs commit: ws-axis/java/src/org/apache/axis/types UnsignedLong.java

dims        2004/11/23 15:25:44

  Modified:    java/test/types TestUnsignedLong.java
               java/src/org/apache/axis/types UnsignedLong.java
  Log:
  Fix for AXIS-1344 - UnsignedLong corrupts data
  from  Tommy Alander (tommy@alander.se)
  
  Revision  Changes    Path
  1.6       +20 -26    ws-axis/java/test/types/TestUnsignedLong.java
  
  Index: TestUnsignedLong.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/java/test/types/TestUnsignedLong.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TestUnsignedLong.java	25 Feb 2004 14:02:59 -0000	1.5
  +++ TestUnsignedLong.java	23 Nov 2004 23:25:44 -0000	1.6
  @@ -19,6 +19,8 @@
   import junit.framework.TestCase;
   import org.apache.axis.types.UnsignedLong;
   
  +import java.math.BigInteger;
  +
   /**
    * Test validation of types.UnsignedLong
    */
  @@ -31,28 +33,28 @@
       /**
        * Run a failure test.  value should be invalid.
        */
  -    private void runFailTest(double value) throws Exception {
  +    private void runFailTest(BigInteger value) throws Exception {
           UnsignedLong oUnsignedLong = null;
           try {
               oUnsignedLong = new UnsignedLong(value);
  -        }
  -        catch (Exception e) { // catch the validation exception
  +        } catch (Exception e) { // catch the validation exception
           }
           // object is not iNstantiated on bad data value
           assertNull("validation restriction failed [" +
  -                String.valueOf(value) + "]. did not restrict bad value.", oUnsignedLong);
  +                String.valueOf(value) + "]. did not restrict bad value.",
  +                oUnsignedLong);
       }
   
       /**
        * Run a successful test.  value should be valid.  String should come out
        * as expected.
        */
  -    private void runPassTest(double value, String strValue) throws Exception {
  +    private void runPassTest(BigInteger value, String strValue)
  +            throws Exception {
           UnsignedLong oUnsignedLong = null;
           try {
               oUnsignedLong = new UnsignedLong(value);
  -        }
  -        catch (Exception e) { // catch the validation exception
  +        } catch (Exception e) { // catch the validation exception
               // error!
               assertTrue("validation error thrown and it shouldn't be", false);
           }
  @@ -64,40 +66,32 @@
        * Test that a positive value succeeeds
        */
       public void testPositiveValue() throws Exception {
  -        runPassTest(100, "100");
  +        runPassTest(new BigInteger("100"), "100");
       }
   
       /**
        * Test that a negative number fails
        */
       public void testNegativeValue() throws Exception {
  -        runFailTest(-100);
  +        runFailTest(new BigInteger("-100"));
       }
   
       /**
  -    * Test that a big number that send the double in scientific notation is OK
  -    */
  +     * Test Max unsigned long
  +     */
       public void testMaxInclusive() throws Exception {
  -      runPassTest(123456789, "123456789");
  +        runPassTest(new BigInteger("18446744073709551615"),
  +                "18446744073709551615");
       }
   
  -    /**
  -    * Test that a number over MaxInclusive fails
  -     * This test wont pass because of precision issues:
  -     * expected:<18446744073709551615> but was:<18446744073709552000>
  -    */
  -/*    
  -      public void testBigNumber() throws Exception {
  -      runPassTest(18446744073709551615D, "18446744073709551615");
  +    public void testTooLarge() throws Exception {
  +        runFailTest(new BigInteger("184467440737095516152"));
       }
  -*/
   
       /**
  -    * Test that a number at MinInclusive succeeds
  -    */
  +     * Test that a number at MinInclusive succeeds
  +     */
       public void testMinExclusive() throws Exception {
  -       runPassTest(0L, "0");
  +        runPassTest(BigInteger.ZERO, "0");
       }
  -
  -
   }
  
  
  
  1.13      +69 -73    ws-axis/java/src/org/apache/axis/types/UnsignedLong.java
  
  Index: UnsignedLong.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/java/src/org/apache/axis/types/UnsignedLong.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- UnsignedLong.java	25 Feb 2004 14:02:47 -0000	1.12
  +++ UnsignedLong.java	23 Nov 2004 23:25:44 -0000	1.13
  @@ -17,8 +17,7 @@
   
   import org.apache.axis.utils.Messages;
   
  -import java.text.FieldPosition;
  -import java.text.NumberFormat;
  +import java.math.BigInteger;
   
   /**
    * Custom class for supporting primitive XSD data type UnsignedLong
  @@ -26,79 +25,61 @@
    * @author Chris Haddad <ch...@cobia.net>
    * @see <a href="http://www.w3.org/TR/xmlschema-2/#unsignedLong">XML Schema 3.3.21</a>
    */
  -public class UnsignedLong extends java.lang.Number implements java.lang.Comparable {
  +public class UnsignedLong extends java.lang.Number
  +        implements java.lang.Comparable {
   
  -    protected Double lValue = new Double(0);
  +    protected BigInteger lValue = BigInteger.ZERO;
  +    private static BigInteger MAX = new BigInteger("18446744073709551615"); // max unsigned long
   
       public UnsignedLong() {
  +    }
  +
  +    public UnsignedLong(double value) throws NumberFormatException {
  +        setValue(new BigInteger(Double.toString(value)));
  +    }
   
  +    public UnsignedLong(BigInteger value) throws NumberFormatException {
  +        setValue(value);
       }
   
  -    /**
  -     * ctor for UnsignedLong
  -     * @exception NumberFormatException will be thrown if validation fails
  -     */
  -    public UnsignedLong(double lValue) throws NumberFormatException {
  -      setValue(lValue);
  +    public UnsignedLong(long lValue) throws NumberFormatException {
  +        setValue(BigInteger.valueOf(lValue));
       }
   
       public UnsignedLong(String stValue) throws NumberFormatException {
  -      setValue(Double.parseDouble(stValue));
  +        setValue(new BigInteger(stValue));
  +    }
  +
  +    private void setValue(BigInteger val) {
  +        if (!UnsignedLong.isValid(val)) {
  +            throw new NumberFormatException(Messages.getMessage(
  +                    "badUnsignedLong00") +
  +                    String.valueOf(val) + "]");
  +        }
  +        this.lValue = val;
       }
   
  -    /**
  -     *
  -     * validates the data and sets the value for the object.
  -     *
  -     * @param long value
  -     */
  -    public void setValue(double lValue) throws NumberFormatException {
  -        if (UnsignedLong.isValid(lValue) == false)
  -            throw new NumberFormatException(
  -                    Messages.getMessage("badUnsignedLong00") +
  -                    String.valueOf(lValue) + "]");
  -        this.lValue = new Double(lValue);
  -    }
  -
  -    /**
  -     * Format the Double in to a string
  -     */ 
  -    private String convertDoubleToUnsignedLong(Double lValue) {
  -      if (lValue != null) {
  -          NumberFormat nf = NumberFormat.getInstance();
  -          nf.setGroupingUsed(false);
  -          StringBuffer buf = new StringBuffer();
  -          FieldPosition pos = new FieldPosition(NumberFormat.INTEGER_FIELD);
  -          nf.format(lValue.doubleValue(), buf, pos);
  -          return buf.toString();
  -      }
  -      return null;
  -    }
  -
  -    public String toString(){
  -        return convertDoubleToUnsignedLong(lValue);
  -    }
  -
  -    public int hashCode(){
  -      if (lValue != null)
  -        return lValue.hashCode();
  -      else
  -        return 0;
  -    }
  -
  -    /**
  -     *
  -     * validate the value against the xsd definition
  -     *
  -     */
  -    public static boolean isValid(double lValue) {
  -      if ( (lValue < 0L)  || (lValue > 18446744073709551615D) )
  -        return false;
  -      else
  +    public static boolean isValid(BigInteger value) {
  +        if (value.compareTo(BigInteger.ZERO) == -1 || // less than zero
  +                value.compareTo(MAX) == 1) {
  +            return false;
  +        }
           return true;
       }
   
  +    public String toString() {
  +        return lValue.toString();
  +    }
  +
  +    public int hashCode() {
  +        if (lValue != null)
  +            return lValue.hashCode();
  +        else
  +            return 0;
  +    }
  +
       private Object __equalsCalc = null;
  +
       public synchronized boolean equals(Object obj) {
           if (!(obj instanceof UnsignedLong)) return false;
           UnsignedLong other = (UnsignedLong) obj;
  @@ -110,31 +91,46 @@
           __equalsCalc = obj;
           boolean _equals;
           _equals = true &&
  -            ((lValue ==null && other.lValue ==null) ||
  -             (lValue !=null &&
  -              lValue.equals(other.lValue)));
  +                ((lValue == null && other.lValue == null) ||
  +                (lValue != null &&
  +                lValue.equals(other.lValue)));
           __equalsCalc = null;
           return _equals;
       }
   
       // implement java.lang.comparable interface
       public int compareTo(Object obj) {
  -      if (lValue != null)
  -        return lValue.compareTo(obj);
  -      else
  -        if (equals(obj) == true)
  +        if (lValue != null)
  +            return lValue.compareTo(obj);
  +        else if (equals(obj) == true)
               return 0;  // null == null
           else
               return 1;  // object is greater
       }
   
       // Implement java.lang.Number interface
  -    public byte byteValue() { return lValue.byteValue(); }
  -    public short shortValue() { return lValue.shortValue(); }
  -    public int intValue() { return lValue.intValue(); }
  -    public long longValue() { return lValue.longValue(); }
  -    public double doubleValue() { return lValue.doubleValue(); }
  -    public float floatValue() { return lValue.floatValue(); }
  +    public byte byteValue() {
  +        return lValue.byteValue();
  +    }
  +
  +    public short shortValue() {
  +        return lValue.shortValue();
  +    }
  +
  +    public int intValue() {
  +        return lValue.intValue();
  +    }
   
  +    public long longValue() {
  +        return lValue.longValue();
  +    }
  +
  +    public double doubleValue() {
  +        return lValue.doubleValue();
  +    }
  +
  +    public float floatValue() {
  +        return lValue.floatValue();
  +    }
   
   }