Skip to content

Commit 6b84fd6

Browse files
committed
Make call to beanutils.ConvertUtils optional (fixes JXPATH-190)
1 parent 3ab6dc9 commit 6b84fd6

3 files changed

Lines changed: 86 additions & 9 deletions

File tree

pom.xml

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,33 @@
117117
</plugin>
118118
<plugin>
119119
<artifactId>maven-surefire-plugin</artifactId>
120-
<configuration>
121-
<includes>
122-
<include>**/*Test.java</include>
123-
</includes>
124-
</configuration>
120+
<executions>
121+
<execution>
122+
<id>default-test</id>
123+
<configuration>
124+
<includes>
125+
<include>**/*Test.java</include>
126+
</includes>
127+
<excludes>
128+
<exclude>**/*WithoutBeanUtilsTest.java</exclude>
129+
</excludes>
130+
</configuration>
131+
</execution>
132+
<execution>
133+
<id>without-beanutils-test</id>
134+
<goals>
135+
<goal>test</goal>
136+
</goals>
137+
<configuration>
138+
<includes>
139+
<include>**/*WithoutBeanUtilsTest.java</include>
140+
</includes>
141+
<classpathDependencyExcludes>
142+
<classpathDependencyExclude>commons-beanutils:commons-beanutils</classpathDependencyExclude>
143+
</classpathDependencyExcludes>
144+
</configuration>
145+
</execution>
146+
</executions>
125147
</plugin>
126148
<plugin>
127149
<artifactId>maven-assembly-plugin</artifactId>

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,14 @@ public boolean canConvert(final Object object, final Class toType) {
141141
if (object instanceof Pointer) {
142142
return canConvert(((Pointer) object).getValue(), useType);
143143
}
144-
return ConvertUtils.lookup(useType) != null;
144+
145+
try {
146+
ClassLoaderUtil.getClass("org.apache.commons.beanutils.ConvertUtils");
147+
return ConvertUtils.lookup(useType) != null;
148+
}
149+
catch (ClassNotFoundException e) {
150+
return false;
151+
}
145152
}
146153

147154
/**
@@ -268,9 +275,16 @@ public Object convert(final Object object, final Class toType) {
268275
}
269276
}
270277

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

276290
throw new JXPathTypeConversionException("Cannot convert "
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
public class BasicTypeConverterWithoutBeanUtilsTest extends BasicTypeConverterTest {
27+
28+
public void testBeanUtilsConverter() {
29+
assertFalse("Cannot convert: String to BigDecimal without BeanUtils",
30+
TypeUtils.canConvert("12", BigDecimal.class));
31+
32+
Exception e = null;
33+
try {
34+
TypeUtils.convert("12", BigDecimal.class);
35+
}
36+
catch (Exception ex) {
37+
e = ex;
38+
}
39+
assertNotNull("Exception thrown when trying to convert", e);
40+
}
41+
}

0 commit comments

Comments
 (0)