From 3b0f302cb7a92d03974fc8d89b79d54f3e1a3afe Mon Sep 17 00:00:00 2001 From: Tobias Gruetzmacher Date: Wed, 15 Mar 2017 14:27:45 +0100 Subject: [PATCH] Make call to beanutils.ConvertUtils optional (fixes JXPATH-190) --- pom.xml | 32 +++++++++++--- .../jxpath/util/BasicTypeConverter.java | 22 ++++++++-- ...asicTypeConverterWithoutBeanUtilsTest.java | 44 +++++++++++++++++++ 3 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 src/test/java/org/apache/commons/jxpath/util/BasicTypeConverterWithoutBeanUtilsTest.java diff --git a/pom.xml b/pom.xml index 9646a494e..f763f6f5a 100644 --- a/pom.xml +++ b/pom.xml @@ -117,11 +117,33 @@ maven-surefire-plugin - - - **/*Test.java - - + + + default-test + + + **/*Test.java + + + **/*WithoutBeanUtilsTest.java + + + + + without-beanutils-test + + test + + + + **/*WithoutBeanUtilsTest.java + + + commons-beanutils:commons-beanutils + + + + maven-assembly-plugin diff --git a/src/main/java/org/apache/commons/jxpath/util/BasicTypeConverter.java b/src/main/java/org/apache/commons/jxpath/util/BasicTypeConverter.java index 482de3d88..604531390 100644 --- a/src/main/java/org/apache/commons/jxpath/util/BasicTypeConverter.java +++ b/src/main/java/org/apache/commons/jxpath/util/BasicTypeConverter.java @@ -141,7 +141,14 @@ public boolean canConvert(final Object object, final Class toType) { if (object instanceof Pointer) { return canConvert(((Pointer) object).getValue(), useType); } - return ConvertUtils.lookup(useType) != null; + + try { + ClassLoaderUtil.getClass("org.apache.commons.beanutils.ConvertUtils"); + return ConvertUtils.lookup(useType) != null; + } + catch (ClassNotFoundException e) { + return false; + } } /** @@ -268,9 +275,16 @@ public Object convert(final Object object, final Class toType) { } } - final Converter converter = ConvertUtils.lookup(useType); - if (converter != null) { - return converter.convert(useType, object); + try { + ClassLoaderUtil.getClass("org.apache.commons.beanutils.ConvertUtils"); + + final Converter converter = ConvertUtils.lookup(useType); + if (converter != null) { + return converter.convert(useType, object); + } + } + catch (ClassNotFoundException e) { // NOPMD + // Fall through to conversation error. } throw new JXPathTypeConversionException("Cannot convert " diff --git a/src/test/java/org/apache/commons/jxpath/util/BasicTypeConverterWithoutBeanUtilsTest.java b/src/test/java/org/apache/commons/jxpath/util/BasicTypeConverterWithoutBeanUtilsTest.java new file mode 100644 index 000000000..f2da81063 --- /dev/null +++ b/src/test/java/org/apache/commons/jxpath/util/BasicTypeConverterWithoutBeanUtilsTest.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.jxpath.util; + +import java.math.BigDecimal; + +/** + * Tests BasicTypeConverter (without common-beanutils) - all other tests from + * BasicTypeConverterTest should still run, but trying to convert anything + * needing BeanUtils should fail. + * + * @author Tobias Gruetzmacher + * @version $Revision$ $Date$ + */ +public class BasicTypeConverterWithoutBeanUtilsTest extends BasicTypeConverterTest { + + public void testBeanUtilsConverter() { + assertFalse("Cannot convert: String to BigDecimal without BeanUtils", + TypeUtils.canConvert("12", BigDecimal.class)); + + Exception e = null; + try { + TypeUtils.convert("12", BigDecimal.class); + } + catch (Exception ex) { + e = ex; + } + assertNotNull("Exception thrown when trying to convert", e); + } +} \ No newline at end of file