Example usage for org.apache.wicket.util.convert.converter LongConverter convertToObject

List of usage examples for org.apache.wicket.util.convert.converter LongConverter convertToObject

Introduction

In this page you can find the example usage for org.apache.wicket.util.convert.converter LongConverter convertToObject.

Prototype

@Override
public Long convertToObject(final String value, final Locale locale) 

Source Link

Usage

From source file:com.github.zeratul021.wicketnumberconversion.ConvertersTest.java

License:Apache License

/**
 * Test long conversions./*  w  w  w. j  av a2 s.co m*/
 */
@Test
public void longConversions() {
    LongConverter converter = new LongConverter();
    assertNull(converter.convertToObject("", Locale.US));
    assertEquals(new Long(10), converter.convertToObject("10", Locale.US));
    assertEquals("10", converter.convertToString((long) 10, Locale.US));
    try {
        converter.convertToObject("whatever", Locale.US);
        fail("Conversion should have thrown an exception");
    } catch (ConversionException e) {
        // This is correct
    }
    try {
        converter.convertToObject("10whatever", Locale.US);
        fail("Conversion should have thrown an exception");
    } catch (ConversionException e) {
        // This is correct
    }
    try {
        converter.convertToObject("" + Long.MAX_VALUE + "0", Locale.US);
        fail("Conversion should have thrown an exception");
    } catch (ConversionException e) {
        // This is correct
    }

    try {
        final String biggerThanLong = "9223372036854776833";
        // assert that the compared number is out of range of Long
        assertEquals(1, new BigDecimal(biggerThanLong).compareTo(BigDecimal.valueOf(Long.MAX_VALUE)));
        converter.convertToObject(biggerThanLong, Locale.US);
        fail("Conversion should have thrown an exception");
    } catch (ConversionException e) {
        // This is correct
    }

    // Currently broken test case
    // Loss of precision when converting big number from string to double and logic inside
    // AbstractNumberConverter lines 82 and 88 don't lead to conversion exception up to this value
    // Combined with the fact that the number itself is then retrieved by casting to long
    // it will always pass validation and submit the form when there is Long.MAX range validator present
    try {
        final String biggerThanLong = "9223372036854776832";
        // assert that the compared number is out of range of Long
        assertEquals(1, new BigDecimal(biggerThanLong).compareTo(BigDecimal.valueOf(Long.MAX_VALUE)));
        converter.convertToObject(biggerThanLong, Locale.US);
        fail("Conversion should have thrown an exception");
    } catch (ConversionException e) {
        // This is correct
    }
}