Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make call to beanutils.ConvertUtils optional (fixes JXPATH-190) #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,41 @@
/*
* 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.
*/
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);
}
}