Skip to content

Commit 961e94d

Browse files
committed
HV-1604 Fix instantiation of the JPATraversableResolver
Also raise the log message to WARN as it's important to be warned if there is an issue instantiating it.
1 parent 37ac728 commit 961e94d

File tree

5 files changed

+66
-9
lines changed

5 files changed

+66
-9
lines changed

engine/src/main/java/org/hibernate/validator/internal/engine/resolver/JPATraversableResolver.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
* query the reachability of a property.
2222
* This resolver will be automatically enabled if JPA 2 is on the classpath and the default {@code TraversableResolver} is
2323
* used.
24+
* <p>
25+
* This class needs to be public as it's instantiated via a privileged action that is not in this package.
2426
*
2527
* @author Hardy Ferentschik
2628
* @author Emmanuel Bernard
2729
*/
28-
class JPATraversableResolver implements TraversableResolver {
30+
public class JPATraversableResolver implements TraversableResolver {
2931

3032
private static final Log LOG = LoggerFactory.make( MethodHandles.lookup() );
3133

engine/src/main/java/org/hibernate/validator/internal/engine/resolver/TraversableResolvers.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,7 @@ public static TraversableResolver getDefault() {
106106
return run( NewInstance.action( jpaAwareResolverClass, "" ) );
107107
}
108108
catch (ValidationException e) {
109-
LOG.debugf(
110-
"Unable to load or instantiate JPA aware resolver %s. All properties will per default be traversable.",
111-
JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME
112-
);
109+
LOG.logUnableToLoadOrInstantiateJPAAwareResolver( JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME );
113110
return getTraverseAllTraversableResolver();
114111
}
115112
}

engine/src/main/java/org/hibernate/validator/internal/util/logging/Log.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,4 +845,8 @@ ValidationException getUnableToAccessMethodException(Lookup lookup, @FormatWith(
845845

846846
@Message(id = 241, value = "Encountered unsupported element %1$s while parsing the XML configuration.")
847847
ValidationException logUnknownElementInXmlConfiguration(String tag);
848+
849+
@LogMessage(level = WARN)
850+
@Message(id = 242, value = "Unable to load or instantiate JPA aware resolver %1$s. All properties will per default be traversable.")
851+
void logUnableToLoadOrInstantiateJPAAwareResolver(String traversableResolverClassName);
848852
}

engine/src/test/java/org/hibernate/validator/test/internal/engine/traversableresolver/JpaTraversableResolverTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import javax.validation.ConstraintViolation;
1515
import javax.validation.Validator;
1616

17-
import org.hibernate.validator.internal.engine.resolver.TraversableResolvers;
17+
import org.hibernate.validator.internal.engine.resolver.JPATraversableResolver;
1818
import org.hibernate.validator.testutil.TestForIssue;
1919
import org.hibernate.validator.testutils.ValidatorUtil;
2020
import org.testng.annotations.BeforeTest;
@@ -32,7 +32,7 @@ public class JpaTraversableResolverTest {
3232
@BeforeTest
3333
public void setUp() {
3434
Configuration<?> configuration = ValidatorUtil.getConfiguration();
35-
configuration.traversableResolver( TraversableResolvers.getDefault() );
35+
configuration.traversableResolver( new JPATraversableResolver() );
3636
validator = configuration.buildValidatorFactory().getValidator();
3737
}
3838

@@ -55,5 +55,3 @@ public void testWithoutBooks() {
5555
assertTrue( results.isEmpty() );
5656
}
5757
}
58-
59-
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.integration.wildfly.jpa;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
import javax.validation.TraversableResolver;
12+
13+
import org.hibernate.validator.integration.AbstractArquillianIT;
14+
import org.hibernate.validator.internal.engine.resolver.JPATraversableResolver;
15+
import org.hibernate.validator.internal.engine.resolver.TraversableResolvers;
16+
import org.jboss.arquillian.container.test.api.Deployment;
17+
import org.jboss.shrinkwrap.api.Archive;
18+
import org.jboss.shrinkwrap.api.asset.Asset;
19+
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
20+
import org.jboss.shrinkwrap.api.asset.StringAsset;
21+
import org.jboss.shrinkwrap.descriptor.api.Descriptors;
22+
import org.jboss.shrinkwrap.descriptor.api.persistence20.PersistenceDescriptor;
23+
import org.testng.annotations.Test;
24+
25+
/**
26+
* Tests that the default {@link TraversableResolver} for a JPA environment is {@code JPATraversableResolver}.
27+
*
28+
* @author Guillaume Smet
29+
*/
30+
public class JPATraversableResolverIT extends AbstractArquillianIT {
31+
32+
private static final String WAR_FILE_NAME = JPATraversableResolverIT.class.getSimpleName() + ".war";
33+
34+
@Deployment
35+
public static Archive<?> createTestArchive() {
36+
return buildTestArchive( WAR_FILE_NAME )
37+
.addAsResource( persistenceXml(), "META-INF/persistence.xml" )
38+
.addAsWebInfResource( EmptyAsset.INSTANCE, "beans.xml" );
39+
}
40+
41+
private static Asset persistenceXml() {
42+
String persistenceXml = Descriptors.create( PersistenceDescriptor.class )
43+
.version( "2.0" )
44+
.createPersistenceUnit()
45+
.name( "default" )
46+
.jtaDataSource( "java:jboss/datasources/ExampleDS" )
47+
.up()
48+
.exportAsString();
49+
return new StringAsset( persistenceXml );
50+
}
51+
52+
@Test
53+
public void testDefaultTraversableResolverInJPAEnvironmentIsJPATraversableResolver() throws Exception {
54+
assertThat( TraversableResolvers.getDefault() ).isInstanceOf( JPATraversableResolver.class );
55+
}
56+
}

0 commit comments

Comments
 (0)