Skip to content

Commit ac85ee2

Browse files
committed
Make call to beanutils.ConvertUtils optional (fixes JXPATH-190)
1 parent e48043d commit ac85ee2

3 files changed

Lines changed: 87 additions & 9 deletions

File tree

pom.xml

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,33 @@ under the License.
8686
<plugins>
8787
<plugin>
8888
<artifactId>maven-surefire-plugin</artifactId>
89-
<configuration>
90-
<includes>
91-
<include>**/*Test.java</include>
92-
</includes>
93-
</configuration>
89+
<executions>
90+
<execution>
91+
<id>default-test</id>
92+
<configuration>
93+
<includes>
94+
<include>**/*Test.java</include>
95+
</includes>
96+
<excludes>
97+
<exclude>**/*WithoutBeanUtilsTest.java</exclude>
98+
</excludes>
99+
</configuration>
100+
</execution>
101+
<execution>
102+
<id>without-beanutils-test</id>
103+
<goals>
104+
<goal>test</goal>
105+
</goals>
106+
<configuration>
107+
<includes>
108+
<include>**/*WithoutBeanUtilsTest.java</include>
109+
</includes>
110+
<classpathDependencyExcludes>
111+
<classpathDependencyExclude>commons-beanutils:commons-beanutils</classpathDependencyExclude>
112+
</classpathDependencyExcludes>
113+
</configuration>
114+
</execution>
115+
</executions>
94116
</plugin>
95117
<plugin>
96118
<artifactId>maven-assembly-plugin</artifactId>

src/main/java/org/apache/commons/jxpath/util/BasicTypeConverter.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,13 @@ public boolean canConvert(Object object, final Class toType) {
143143
if (object instanceof Pointer) {
144144
return canConvert(((Pointer) object).getValue(), useType);
145145
}
146-
return ConvertUtils.lookup(useType) != null;
146+
147+
try {
148+
ClassLoaderUtil.getClass("org.apache.commons.beanutils.ConvertUtils");
149+
return ConvertUtils.lookup(useType) != null;
150+
} catch (ClassNotFoundException e) {
151+
return false;
152+
}
147153
}
148154

149155
/**
@@ -269,9 +275,15 @@ public Object convert(Object object, final Class toType) {
269275
}
270276
}
271277

272-
Converter converter = ConvertUtils.lookup(useType);
273-
if (converter != null) {
274-
return converter.convert(useType, object);
278+
try {
279+
ClassLoaderUtil.getClass("org.apache.commons.beanutils.ConvertUtils");
280+
281+
Converter converter = ConvertUtils.lookup(useType);
282+
if (converter != null) {
283+
return converter.convert(useType, object);
284+
}
285+
} catch (ClassNotFoundException e) {
286+
// Fall through to conversation error.
275287
}
276288

277289
throw new JXPathTypeConversionException("Cannot convert "
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.jxpath.util;
18+
19+
import java.math.BigDecimal;
20+
21+
/**
22+
* Tests BasicTypeConverter (without common-beanutils) - all other tests from
23+
* BasicTypeConverterTest should still run, but trying to convert anything
24+
* needing BeanUtils should fail.
25+
*
26+
* @author Tobias Gruetzmacher
27+
* @version $Revision$ $Date$
28+
*/
29+
public class BasicTypeConverterWithoutBeanUtilsTest extends BasicTypeConverterTest {
30+
31+
public void testBeanUtilsConverter() {
32+
assertFalse("Cannot convert: String to BigDecimal without BeanUtils",
33+
TypeUtils.canConvert("12", BigDecimal.class));
34+
35+
Exception e = null;
36+
try {
37+
TypeUtils.convert("12", BigDecimal.class);
38+
}
39+
catch (Exception ex) {
40+
e = ex;
41+
}
42+
assertNotNull("Exception thrown when trying to convert", e);
43+
}
44+
}

0 commit comments

Comments
 (0)