Package org.apache.commons.beanutils

Examples of org.apache.commons.beanutils.Converter


  public long getParameterAsLong(String name)
  {
    assert name != null;

    Converter c = ConvertUtils.lookup(java.lang.Long.class);

    return ((Long) c.convert(java.lang.Long.class, getParameterAsString(name))).longValue();
  }
View Full Code Here


              req.addError(name, "Value '" + value + "' cannot be converted to a date");
            }
          }
          else
          {
            Converter c = ConvertUtils.lookup(clazz);

            if (c == null)
            {
              log.error("No converter found for class '" + type + "'");
              throw new IllegalArgumentException("No converter found for class '" + type + "'");
            }

            try
            {
              c.convert(clazz, value);
            }
            catch (ConversionException ce)
            {
              req.addError(name, "Value '" + value + "' cannot be converted to a '" + type + "'");
            }
View Full Code Here

        ISODateConverter converter = new ISODateConverter();
        ConvertUtils.register(converter, java.util.Date.class);
        ISOToStringConverter tsConverter = new ISOToStringConverter();
        ConvertUtils.register(tsConverter, String.class);
       
        Converter dateConverter = ConvertUtils.lookup(java.util.Date.class);
        assertEquals("Date converter successfully registered", dateConverter, converter);
        Converter stringConverter = ConvertUtils.lookup(String.class);
        assertEquals("Date converter successfully registered", tsConverter, stringConverter);
       
        java.util.Date conversionResult = (java.util.Date)
                                ConvertUtils.convert("20030101", java.util.Date.class);
       
View Full Code Here

        ObjectStringConverter converter = new ConvertUtilsObjectStringConverter();
        commonTestForConvertUtilsConverters( converter );
    }
   
    private void commonTestForConvertUtilsConverters(ObjectStringConverter objectStringConverter) {
        Converter converter = new Converter() {
            public Object convert(Class type, Object value) {
                if ( type == SecurityManager.class) {
                    return "Life, The Universe And Everything";
                }
                return "The answer is " + value.toString();
View Full Code Here

    }
   
    public void testDefaultOSConverterDates() {
       
   
        Converter converter = new Converter() {
            public Object convert(Class type, Object value) {
                return "Arthur Dent";
            }
        };
       
        ConvertUtils.register( converter, java.sql.Date.class );
       
        converter = new Converter() {
            public Object convert(Class type, Object value) {
                return "Ford Prefect";
            }
        };
       
        ConvertUtils.register( converter, String.class );
       
        converter = new Converter() {
            public Object convert(Class type, Object value) {
                return "Marvin";
            }
        };
       
View Full Code Here

    /**
     * Test Conversion to String
     */
    public void testConvertToString() {

        Converter converter = new CharacterConverter();

        assertEquals("Character Test", "N", converter.convert(String.class, new Character('N')));
        assertEquals("String Test",    "F", converter.convert(String.class, "FOO"));
        assertEquals("Integer Test",   "3", converter.convert(String.class, new Integer(321)));
        assertEquals("Null Test",     null, converter.convert(String.class, null));
    }
View Full Code Here

    /**
     * Test Conversion to Character
     */
    public void testConvertToCharacter() {
        Converter converter = new CharacterConverter();
        assertEquals("Character Test", new Character('N'), converter.convert(Character.class, new Character('N')));
        assertEquals("String Test",    new Character('F'), converter.convert(Character.class, "FOO"));
        assertEquals("Integer Test",   new Character('3'), converter.convert(Character.class, new Integer(321)));
        try {
            converter.convert(Character.class, null);
            fail("Expected a ConversionException for null value");
        } catch (Exception e) {
            // expected result
        }
    }
View Full Code Here

    /**
     * Test Conversion to Character (with default)
     */
    public void testDefault() {
        Converter converter = new CharacterConverter("C");
        assertEquals("Default Test",   new Character('C'), converter.convert(Character.class, null));
    }
View Full Code Here

    /**
     * Test Invalid Amounts (too big/small)
     */
    public void testInvalidAmount() {
        Converter converter = makeConverter();
        Class clazz = Byte.class;

        Long min         = new Long(Byte.MIN_VALUE);
        Long max         = new Long(Byte.MAX_VALUE);
        Long minMinusOne = new Long(min.longValue() - 1);
        Long maxPlusOne  = new Long(max.longValue() + 1);

        // Minimum
        assertEquals("Minimum", new Byte(Byte.MIN_VALUE), converter.convert(clazz, min));

        // Maximum
        assertEquals("Maximum", new Byte(Byte.MAX_VALUE), converter.convert(clazz, max));

        // Too Small
        try {
            assertEquals("Minimum - 1", null, converter.convert(clazz, minMinusOne));
            fail("Less than minimum, expected ConversionException");
        } catch (Exception e) {
            // expected result
        }

        // Too Large
        try {
            assertEquals("Maximum + 1", null, converter.convert(clazz, maxPlusOne));
            fail("More than maximum, expected ConversionException");
        } catch (Exception e) {
            // expected result
        }
    }
View Full Code Here

    /**
     * Test Invalid Amounts (too big/small)
     */
    public void testInvalidAmount() {
        Converter converter = makeConverter();
        Class clazz = Float.class;

        Double max     = new Double(Float.MAX_VALUE);
        Double tooBig  = new Double(Double.MAX_VALUE);

        // Maximum
        assertEquals("Maximum", new Float(Float.MAX_VALUE), converter.convert(clazz, max));

        // Too Large
        try {
            assertEquals("Too Big", null, converter.convert(clazz, tooBig));
            fail("More than maximum, expected ConversionException");
        } catch (Exception e) {
            // expected result
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.commons.beanutils.Converter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.