Skip to content

Commit

Permalink
Make call to beanutils.ConvertUtils optional (fixes JXPATH-190)
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiX committed Oct 16, 2023
1 parent 3ab6dc9 commit 3b0f302
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 9 deletions.
32 changes: 27 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,33 @@
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
<executions>
<execution>
<id>default-test</id>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
<excludes>
<exclude>**/*WithoutBeanUtilsTest.java</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>without-beanutils-test</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>**/*WithoutBeanUtilsTest.java</include>
</includes>
<classpathDependencyExcludes>
<classpathDependencyExclude>commons-beanutils:commons-beanutils</classpathDependencyExclude>
</classpathDependencyExcludes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/**
Expand Down Expand Up @@ -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 "
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 3b0f302

Please sign in to comment.