Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.text.ParseException;
import java.util.Locale;

import org.apache.commons.beanutils.ConversionException;

/**
* Standard {@link org.apache.commons.beanutils.locale.LocaleConverter} implementation that converts an incoming locale-sensitive String into a
* {@code java.lang.Long} object, optionally using a default value or throwing a {@link org.apache.commons.beanutils.ConversionException} if a conversion error
Expand Down Expand Up @@ -173,6 +175,10 @@ protected Object parse(final Object value, final String pattern) throws ParseExc
if (result == null || result instanceof Long) {
return result;
}
final double doubleValue = ((Number) result).doubleValue();
if (doubleValue < Long.MIN_VALUE || doubleValue > Long.MAX_VALUE) {
throw new ConversionException("Supplied number is not of type Long: " + result);
}
return Long.valueOf(((Number) result).longValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

package org.apache.commons.beanutils.locale.converters;

import java.text.DecimalFormat;

import org.apache.commons.beanutils.ConversionException;

/**
* Test Case for the LongLocaleConverter class.
*
Expand Down Expand Up @@ -225,5 +229,27 @@ public void testConstructorMain() {

}

/**
* Test Long limits
*/
public void testLongLimits() {
converter = new LongLocaleConverter();
final DecimalFormat fmt = new DecimalFormat("#");
assertEquals(Long.valueOf(Long.MAX_VALUE), converter.convert(fmt.format(Long.MAX_VALUE)));
assertEquals(Long.valueOf(Long.MIN_VALUE), converter.convert(fmt.format(Long.MIN_VALUE)));
try {
converter.convert("99999999999999999999");
fail("Positive out of range should throw ConversionException");
} catch (final ConversionException expected) {
// expected result
}
try {
converter.convert("-99999999999999999999");
fail("Negative out of range should throw ConversionException");
} catch (final ConversionException expected) {
// expected result
}
}

}

Loading