Package org.apache.commons.beanutils

Examples of org.apache.commons.beanutils.Converter


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

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

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

        // Maximum
        assertEquals("Maximum", new Short(Short.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 = Integer.class;

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

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

        // Maximum
        assertEquals("Maximum", new Integer(Integer.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 Conversion to String
     */
    public void testConvertToString() {
        Converter converter = new ClassConverter();

        assertEquals("Class Test", "java.lang.Integer", converter.convert(String.class, Integer.class));
        assertEquals("Value Test", "foo", converter.convert(String.class, "foo"));
        assertEquals("Value Test", "bar", converter.convert(String.class, new StringBuffer("bar")));
        assertEquals("Null Test",   null, converter.convert(String.class, null));
    }
View Full Code Here

    /**
     * Test Conversion to Class
     */
    public void testConvertToClass() {
        Converter converter = new ClassConverter();

        assertEquals("Class Test",        Integer.class, converter.convert(Class.class, Integer.class));
        assertEquals("String Test",       Integer.class, converter.convert(Class.class, "java.lang.Integer"));
        assertEquals("StringBuffer Test", Integer.class, converter.convert(Class.class, new StringBuffer("java.lang.Integer")));

        // Invalid Test
        try {
            converter.convert(Class.class, new Integer(6));
            fail("Expected invalid value to fail");
        } catch (ConversionException e) {
            // expected result
        }

        // Test Null
        try {
            converter.convert(Class.class, null);
            fail("Expected null value to fail");
        } catch (ConversionException e) {
            // expected result
        }
    }
View Full Code Here

    /**
     * Test Invalid Conversion with default
     */
    public void testConvertToClassDefault() {

        Converter converter = new ClassConverter(Object.class);

        assertEquals("Invalid Test", Object.class, converter.convert(Class.class, new Integer(6)));
        assertEquals("Null Test",    Object.class, converter.convert(Class.class, null));
    }
View Full Code Here

    /**
     * Test Invalid Conversion with default "null"
     */
    public void testConvertToClassDefaultNull() {

        Converter converter = new ClassConverter(null);

        assertEquals("Invalid Test", null, converter.convert(Class.class, new Integer(6)));
        assertEquals("Null Test",    null, converter.convert(Class.class, null));
    }
View Full Code Here

    /**
     * Test Array Conversion
     */
    public void testArray() {
        Converter converter = new ClassConverter();

        // Test Array Class to String
        assertEquals("Array to String", "[Ljava.lang.Boolean;", converter.convert(String.class, Boolean[].class));

        // *** N.B. for some reason the following works on m1, but not m2
        // Test String to Array Class
        // assertEquals("String to Array", Boolean[].class, converter.convert(Class.class, "[Ljava.lang.Boolean;"));
    }
View Full Code Here

    /**
     * Test Invalid
     */
    public void testInvalid() {
        Converter converter = new ClassConverter();

        // Test invalid class name
        try {
            converter.convert(Class.class, "foo.bar");
            fail("Invalid class name, expected ConversionException");
        } catch (ConversionException e) {
            // expected result
        }
    }
View Full Code Here

            // create a custom classloader for a "component"
            // just like a container would.
            ClassLoader componentLoader1 = new ClassLoader() {};
            ClassLoader componentLoader2 = new ClassLoader() {};

            Converter origFloatConverter = ConvertUtils.lookup(Float.TYPE);
            Converter floatConverter1 = new FloatConverter();

            // Emulate the container invoking a component #1, and the component
            // registering a custom converter instance whose class is
            // available via the "shared" classloader.
            Thread.currentThread().setContextClassLoader(componentLoader1);
View Full Code Here

              ConvertUtils.register((Converter)newFloatConverter, Float.TYPE);

              // After registering a custom converter, lookup should return
              // it back to us. We'll try this lookup again with a different
              // context-classloader set, and shouldn't see it
              Converter componentConverter = ConvertUtils.lookup(Float.TYPE);
              assertTrue(componentConverter.getClass().getClassLoader() == componentLoader);

              newFloatConverter = null;
            }
            Thread.currentThread().setContextClassLoader(origContextClassLoader);

            // Because the context classloader has been reset, we shouldn't
            // see the custom registered converter here...
            Converter sharedConverter = ConvertUtils.lookup(Float.TYPE);
            assertFalse(sharedConverter.getClass().getClassLoader() == componentLoader);

            // and here we should see it again
            Thread.currentThread().setContextClassLoader(componentLoader);
            {
                Converter componentConverter = ConvertUtils.lookup(Float.TYPE);
                assertTrue(componentConverter.getClass().getClassLoader() == componentLoader);
            }
            Thread.currentThread().setContextClassLoader(origContextClassLoader);
            // Emulate a container "undeploying" the component. This should
            // make component loader available for garbage collection (we hope)
            WeakReference weakRefToComponent = new WeakReference(componentLoader);
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.