Example usage for org.apache.commons.beanutils ConversionException getMessage

List of usage examples for org.apache.commons.beanutils ConversionException getMessage

Introduction

In this page you can find the example usage for org.apache.commons.beanutils ConversionException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleTest.java

@Test
public void testGetOption() throws Exception {
    AnnotationUseStyleCheck check = new AnnotationUseStyleCheck();
    try {/* w w w .ja  v  a  2  s.  c o  m*/
        check.setElementStyle("SHOULD_PRODUCE_ERROR");
    } catch (ConversionException ex) {
        assertTrue(ex.getMessage().startsWith("unable to parse"));
        return;
    }

    Assert.fail();
}

From source file:com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheckTest.java

@Test
public void testGetOption() throws Exception {
    final AnnotationUseStyleCheck check = new AnnotationUseStyleCheck();
    try {/*from   w  ww . j  av a2 s  .c o m*/
        check.setElementStyle("SHOULD_PRODUCE_ERROR");
    } catch (ConversionException ex) {
        assertTrue(ex.getMessage().startsWith("unable to parse"));
        return;
    }

    Assert.fail();
}

From source file:com.puppycrawl.tools.checkstyle.checks.header.HeaderCheckTest.java

@Test
public void testIoExceptionWhenLoadingHeader() throws Exception {
    HeaderCheck check = PowerMockito.spy(new HeaderCheck());
    PowerMockito.doThrow(new IOException("expected exception")).when(check, "loadHeader", anyObject());

    try {/*w ww. j a v  a 2  s  .c o m*/
        check.setHeader("header");
        fail("Exception expected");
    } catch (ConversionException ex) {
        assertTrue(ex.getCause() instanceof IOException);
        assertEquals("unable to load header", ex.getMessage());
    }
}

From source file:com.puppycrawl.tools.checkstyle.checks.header.HeaderCheckTest.java

@Test
public void testSetHeaderTwice() {
    HeaderCheck check = new HeaderCheck();
    check.setHeader("Header");
    try {/*from   w  w w .  j  av a2s.  c o  m*/
        check.setHeader("Header2");
        fail();
    } catch (ConversionException ex) {
        assertEquals("header has already been set - " + "set either header or headerFile, not both",
                ex.getMessage());
    }
}

From source file:com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolderTest.java

@Test
public void testSetAliasListWrong() {
    SuppressWarningsHolder holder = new SuppressWarningsHolder();

    try {/*from  ww w .  java  2 s.co m*/
        holder.setAliasList("SomeAlias");
        fail("Exception expected");
    } catch (ConversionException ex) {
        assertEquals("'=' expected in alias list item: SomeAlias", ex.getMessage());
    }

}

From source file:com.puppycrawl.tools.checkstyle.checks.header.RegexpHeaderCheckTest.java

/**
 * Test of setHeader method, of class RegexpHeaderCheck.
 *//*  w w  w . j a v  a2  s .  com*/
@Test
public void testSetHeader() {
    // check invalid header passes
    RegexpHeaderCheck instance = new RegexpHeaderCheck();
    try {
        String header = "^/**\\n * Licensed to the Apache Software Foundation (ASF)";
        instance.setHeader(header);
        fail(String.format(Locale.ROOT, "%s should have been thrown", ConversionException.class));
    } catch (ConversionException ex) {
        assertEquals("Unable to parse format: ^/**\\n *" + " Licensed to the Apache Software Foundation (ASF)",
                ex.getMessage());
    }
}

From source file:org.jdesigner.platform.web.converter.AbstractConverter.java

/**
 * Handle Conversion Errors.//www. j ava 2  s . c om
 * <p>
 * If a default value has been specified then it is returned otherwise a
 * ConversionException is thrown.
 * 
 * @param type
 *            Data type to which this value should be converted.
 * @param value
 *            The input value to be converted
 * @param cause
 *            The exception thrown by the <code>convert</code> method
 * @return The default value.
 * @throws ConversionException
 *             if no default value has been specified for this
 *             {@link Converter}.
 */
protected Object handleError(Class type, Object value, Throwable cause) {
    if (log().isDebugEnabled()) {
        // if (cause instanceof ConversionException) {
        // log().debug("    Conversion threw ConversionException: " +
        // cause.getMessage());
        // } else {
        // log().debug("    Conversion threw " + cause);
        // }
    }

    if (useDefault) {
        return handleMissing(type);
    }

    ConversionException cex = null;
    if (cause instanceof ConversionException) {
        cex = (ConversionException) cause;
        if (log().isDebugEnabled()) {
            log().debug("    Re-throwing ConversionException: " + cex.getMessage());
            log().debug("    " + DEFAULT_CONFIG_MSG);
        }
    } else {
        String msg = "Error converting from '" + toString(value.getClass()) + "' to '" + toString(type) + "' "
                + cause.getMessage();
        cex = new ConversionException(msg, cause);
        // if (log().isDebugEnabled()) {
        // log().debug("    Throwing ConversionException: " + msg);
        // log().debug("    " + DEFAULT_CONFIG_MSG);
        // }
        BeanUtils.initCause(cex, cause);
    }

    throw cex;

}

From source file:org.marketcetera.module.StringToTypeConverter.java

/**
 * Converts the supplied string value to the instance of the
 * specified type./*from  w ww  . j a  v a2 s. com*/
 *
 * Calls to this method should only be made if {@link #isSupported(String)}
 * returns true for the <code>inTypeName</code>, otherwise this method
 * will throw an <code>IllegalArgumentException</code>.
 *
 * @param inType the type of instance to return
 * @param inValue the string value to convert to instance
 *
 * @return the instance converted from the string.
 *
 * @throws IllegalArgumentException if the supplied type name is
 * not supported OR if there were errors converting
 * the string to a numeric type.
 */
public static Object convert(Class inType, String inValue) throws IllegalArgumentException {
    try {
        return sConverter.convert(inValue, inType);
    } catch (ConversionException e) {
        throw new IllegalArgumentException(
                Messages.STRING_CONVERSION_ERROR.getText(inValue, inType.getName(), e.getMessage()));
    }
}

From source file:org.toobsframework.data.beanutil.BeanMonkey.java

public static Collection populateCollection(IValidator v, String beanClazz, String indexPropertyName,
        Map properties, boolean validate, boolean noload)
        throws IllegalAccessException, InvocationTargetException, ValidationException, ClassNotFoundException,
        InstantiationException, PermissionException {

    // Do nothing unless all arguments have been specified
    if ((beanClazz == null) || (properties == null) || (indexPropertyName == null)) {
        log.warn("Proper parameters not present.");
        return null;
    }//from  ww w . j  a  va2 s .  c o  m

    ArrayList returnObjs = new ArrayList();

    Object[] indexProperty = (Object[]) properties.get(indexPropertyName);
    if (indexProperty == null) {
        log.warn("indexProperty [" + indexProperty + "] does not exist in the map.");
        return returnObjs;
    }

    Class beanClass = Class.forName(beanClazz);

    String beanClazzName = beanClass.getSimpleName(); //  beanClazz.substring(beanClazz.lastIndexOf(".") + 1);
    IObjectLoader odao = null;
    ICollectionLoader cdao = null;
    if (objectClass.isAssignableFrom(beanClass)) {
        odao = (IObjectLoader) beanFactory.getBean(
                Introspector.decapitalize(beanClazzName.substring(0, beanClazzName.length() - 4)) + "Dao");
        if (odao == null) {
            throw new InvocationTargetException(new Exception("Object DAO class "
                    + Introspector.decapitalize(beanClazzName) + "Dao could not be loaded"));
        }
    } else {
        cdao = (ICollectionLoader) beanFactory.getBean(
                Introspector.decapitalize(beanClazzName.substring(0, beanClazzName.length() - 4)) + "Dao");
        if (cdao == null) {
            throw new InvocationTargetException(new Exception("Collection DAO class "
                    + Introspector.decapitalize(beanClazzName) + "Dao could not be loaded"));
        }
    }

    boolean namespaceStrict = properties.containsKey("namespaceStrict");

    for (int index = 0; index < indexProperty.length; index++) {
        String guid = (String) indexProperty[index];

        boolean newBean = false;
        Object bean = null;
        if (!noload && guid != null && guid.length() > 0) {
            bean = (odao != null) ? odao.load(guid) : cdao.load(Integer.parseInt(guid));
        }
        if (bean == null) {
            bean = Class.forName(beanClazz).newInstance();
            newBean = true;
        }
        if (v != null) {
            v.prePopulate(bean, properties);
        }
        if (log.isDebugEnabled()) {
            log.debug("BeanMonkey.populate(" + bean + ", " + properties + ")");
        }

        Errors e = null;

        if (validate) {
            String beanClassName = null;
            beanClassName = bean.getClass().getName();
            beanClassName = beanClassName.substring(beanClassName.lastIndexOf(".") + 1);
            e = new BindException(bean, beanClassName);
        }

        String namespace = null;
        if (properties.containsKey("namespace") && !"".equals(properties.get("namespace"))) {
            namespace = (String) properties.get("namespace") + ".";
        }

        // Loop through the property name/value pairs to be set
        Iterator names = properties.keySet().iterator();
        while (names.hasNext()) {

            // Identify the property name and value(s) to be assigned
            String name = (String) names.next();
            if (name == null || (indexPropertyName.equals(name) && !noload)
                    || (namespaceStrict && !name.startsWith(namespace))) {
                continue;
            }

            Object value = null;
            if (properties.get(name) == null) {
                log.warn("Property [" + name + "] does not have a value in the map.");
                continue;
            }
            if (properties.get(name).getClass().isArray() && index < ((Object[]) properties.get(name)).length) {
                value = ((Object[]) properties.get(name))[index];
            } else if (properties.get(name).getClass().isArray()) {
                value = ((Object[]) properties.get(name))[0];
            } else {
                value = properties.get(name);
            }
            if (namespace != null) {
                name = name.replace(namespace, "");
            }

            PropertyDescriptor descriptor = null;
            Class type = null; // Java type of target property
            try {
                descriptor = PropertyUtils.getPropertyDescriptor(bean, name);
                if (descriptor == null) {
                    continue; // Skip this property setter
                }
            } catch (NoSuchMethodException nsm) {
                continue; // Skip this property setter
            } catch (IllegalArgumentException iae) {
                continue; // Skip null nested property
            }
            if (descriptor.getWriteMethod() == null) {
                if (log.isDebugEnabled()) {
                    log.debug("Skipping read-only property");
                }
                continue; // Read-only, skip this property setter
            }
            type = descriptor.getPropertyType();
            String className = type.getName();

            try {
                value = evaluatePropertyValue(name, className, namespace, value, properties, bean);
            } catch (NoSuchMethodException nsm) {
                continue;
            }

            try {
                BeanUtils.setProperty(bean, name, value);
            } catch (ConversionException ce) {
                log.error("populate - exception [bean:" + bean.getClass().getName() + " name:" + name
                        + " value:" + value + "] ");
                if (validate) {
                    e.rejectValue(name, name + ".conversionError", ce.getMessage());
                } else {
                    throw new ValidationException(bean, className, name, ce.getMessage());
                }
            } catch (Exception be) {
                log.error("populate - exception [bean:" + bean.getClass().getName() + " name:" + name
                        + " value:" + value + "] ");
                if (validate) {
                    e.rejectValue(name, name + ".error", be.getMessage());
                } else {
                    throw new ValidationException(bean, className, name, be.getMessage());
                }
            }
        }
        /*
        if (newBean && cdao != null) {
          BeanUtils.setProperty(bean, "id", -1);
        }
        */
        if (validate && e.getErrorCount() > 0) {
            throw new ValidationException(e);
        }

        returnObjs.add(bean);
    }

    return returnObjs;
}

From source file:org.toobsframework.data.beanutil.BeanMonkey.java

public static void populate(Object bean, Map properties, boolean validate)
        throws IllegalAccessException, InvocationTargetException, ValidationException, PermissionException {

    // Do nothing unless both arguments have been specified
    if ((bean == null) || (properties == null)) {
        return;//w ww .j av a  2  s  .co m
    }
    if (log.isDebugEnabled()) {
        log.debug("BeanMonkey.populate(" + bean + ", " + properties + ")");
    }

    Errors e = null;
    if (validate) {
        String beanClassName = null;
        beanClassName = bean.getClass().getName();
        beanClassName = beanClassName.substring(beanClassName.lastIndexOf(".") + 1);
        e = new BindException(bean, beanClassName);
    }

    String namespace = null;
    if (properties.containsKey("namespace") && !"".equals(properties.get("namespace"))) {
        namespace = (String) properties.get("namespace") + ".";
    }

    // Loop through the property name/value pairs to be set
    Iterator names = properties.keySet().iterator();
    while (names.hasNext()) {

        // Identify the property name and value(s) to be assigned
        String name = (String) names.next();
        if (name == null) {
            continue;
        }
        Object value = properties.get(name);
        if (namespace != null) {
            name = name.replace(namespace, "");
        }

        PropertyDescriptor descriptor = null;
        Class type = null; // Java type of target property
        try {
            descriptor = PropertyUtils.getPropertyDescriptor(bean, name);
            if (descriptor == null) {
                continue; // Skip this property setter
            }
        } catch (NoSuchMethodException nsm) {
            continue; // Skip this property setter
        } catch (IllegalArgumentException iae) {
            continue; // Skip null nested property
        }

        if (descriptor.getWriteMethod() == null) {
            if (log.isDebugEnabled()) {
                log.debug("Skipping read-only property");
            }
            continue; // Read-only, skip this property setter
        }
        type = descriptor.getPropertyType();
        String className = type.getName();

        try {
            value = evaluatePropertyValue(name, className, namespace, value, properties, bean);
        } catch (NoSuchMethodException nsm) {
            continue;
        }

        try {
            if (value != null) {
                BeanUtils.setProperty(bean, name, value);
            }
        } catch (ConversionException ce) {
            log.error("populate - exception [bean:" + bean.getClass().getName() + " name:" + name + " value:"
                    + value + "] ");
            if (validate) {
                e.rejectValue(name, name + ".conversionError", ce.getMessage());
            } else {
                throw new ValidationException(bean, className, name, ce.getMessage());
            }
        } catch (Exception be) {
            log.error("populate - exception [bean:" + bean.getClass().getName() + " name:" + name + " value:"
                    + value + "] ");
            if (validate) {
                e.rejectValue(name, name + ".error", be.getMessage());
            } else {
                throw new ValidationException(bean, className, name, be.getMessage());
            }
        }
    }
    if (validate && e.getErrorCount() > 0) {
        throw new ValidationException(e);
    }
}