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

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

Introduction

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

Prototype

LongConverter

Source Link

Usage

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

License:Apache License

/**
 * Test long conversions.//from w ww . j a  v  a 2 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
    }
}

From source file:org.patientview.radar.web.RadarApplication.java

License:Open Source License

protected IConverterLocator newConverterLocator() {
    // change the number formatters do disable grouping e.g. the comma in 1,500

    ConverterLocator converterLocator = new ConverterLocator();
    converterLocator.set(Integer.class, new IntegerConverter() {
        @Override//  www  .  ja va 2 s. c o  m
        public NumberFormat getNumberFormat(Locale locale) {
            NumberFormat numberFormat = super.getNumberFormat(Locale.getDefault());
            numberFormat.setGroupingUsed(false);
            return numberFormat;
        }
    });

    converterLocator.set(Double.class, new DoubleConverter() {
        @Override
        public NumberFormat getNumberFormat(Locale locale) {
            NumberFormat numberFormat = super.getNumberFormat(Locale.getDefault());
            numberFormat.setGroupingUsed(false);
            return numberFormat;
        }
    });

    converterLocator.set(Long.class, new LongConverter() {
        @Override
        public NumberFormat getNumberFormat(Locale locale) {
            NumberFormat numberFormat = super.getNumberFormat(Locale.getDefault());
            numberFormat.setGroupingUsed(false);
            return numberFormat;
        }
    });

    return converterLocator;
}