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 @@ -78,6 +78,11 @@ protected Long parse(final Object value, final String pattern) throws ParseExcep
return (Long) result;
}

final double doubleValue = 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(result.longValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

package org.apache.commons.beanutils2.converters;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.text.DecimalFormat;

import org.apache.commons.beanutils2.ConversionException;
import org.apache.commons.beanutils2.locale.converters.LongLocaleConverter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -189,4 +195,17 @@ void testConstructorMain() {
convertInvalid(converter, "(C)", defaultValue);
convertNull(converter, "(C)", defaultValue);
}

/**
* Test Long limits
*/
@Test
void testLongLimits() {
converter = LongLocaleConverter.builder().setLocale(defaultLocale).get();
final DecimalFormat fmt = new DecimalFormat("#");
assertEquals(Long.valueOf(Long.MAX_VALUE), converter.convert(fmt.format(Long.MAX_VALUE)), "Long.MAX_VALUE");
assertEquals(Long.valueOf(Long.MIN_VALUE), converter.convert(fmt.format(Long.MIN_VALUE)), "Long.MIN_VALUE");
assertThrows(ConversionException.class, () -> converter.convert("99999999999999999999"));
assertThrows(ConversionException.class, () -> converter.convert("-99999999999999999999"));
}
}
Loading