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

HV-1604 Fix instantiation of the JPATraversableResolver #951

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -21,11 +21,13 @@
* query the reachability of a property.
* This resolver will be automatically enabled if JPA 2 is on the classpath and the default {@code TraversableResolver} is
* used.
* <p>
* This class needs to be public as it's instantiated via a privileged action that is not in this package.
*
* @author Hardy Ferentschik
* @author Emmanuel Bernard
*/
class JPATraversableResolver implements TraversableResolver {
public class JPATraversableResolver implements TraversableResolver {

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

Expand Down
Expand Up @@ -106,10 +106,7 @@ public static TraversableResolver getDefault() {
return run( NewInstance.action( jpaAwareResolverClass, "" ) );
}
catch (ValidationException e) {
LOG.debugf(
"Unable to load or instantiate JPA aware resolver %s. All properties will per default be traversable.",
JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME
);
LOG.logUnableToLoadOrInstantiateJPAAwareResolver( JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME );
return getTraverseAllTraversableResolver();
}
}
Expand Down
Expand Up @@ -845,4 +845,8 @@ ValidationException getUnableToAccessMethodException(Lookup lookup, @FormatWith(

@Message(id = 241, value = "Encountered unsupported element %1$s while parsing the XML configuration.")
ValidationException logUnknownElementInXmlConfiguration(String tag);

@LogMessage(level = WARN)
@Message(id = 242, value = "Unable to load or instantiate JPA aware resolver %1$s. All properties will per default be traversable.")
void logUnableToLoadOrInstantiateJPAAwareResolver(String traversableResolverClassName);
}
Expand Up @@ -14,7 +14,7 @@
import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import org.hibernate.validator.internal.engine.resolver.TraversableResolvers;
import org.hibernate.validator.internal.engine.resolver.JPATraversableResolver;
import org.hibernate.validator.testutil.TestForIssue;
import org.hibernate.validator.testutils.ValidatorUtil;
import org.testng.annotations.BeforeTest;
Expand All @@ -32,7 +32,7 @@ public class JpaTraversableResolverTest {
@BeforeTest
public void setUp() {
Configuration<?> configuration = ValidatorUtil.getConfiguration();
configuration.traversableResolver( TraversableResolvers.getDefault() );
configuration.traversableResolver( new JPATraversableResolver() );
validator = configuration.buildValidatorFactory().getValidator();
}

Expand All @@ -55,5 +55,3 @@ public void testWithoutBooks() {
assertTrue( results.isEmpty() );
}
}


@@ -0,0 +1,56 @@
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.integration.wildfly.jpa;

import static org.assertj.core.api.Assertions.assertThat;

import javax.validation.TraversableResolver;

import org.hibernate.validator.integration.AbstractArquillianIT;
import org.hibernate.validator.internal.engine.resolver.JPATraversableResolver;
import org.hibernate.validator.internal.engine.resolver.TraversableResolvers;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.descriptor.api.Descriptors;
import org.jboss.shrinkwrap.descriptor.api.persistence20.PersistenceDescriptor;
import org.testng.annotations.Test;

/**
* Tests that the default {@link TraversableResolver} for a JPA environment is {@code JPATraversableResolver}.
*
* @author Guillaume Smet
*/
public class JPATraversableResolverIT extends AbstractArquillianIT {

private static final String WAR_FILE_NAME = JPATraversableResolverIT.class.getSimpleName() + ".war";

@Deployment
public static Archive<?> createTestArchive() {
return buildTestArchive( WAR_FILE_NAME )
.addAsResource( persistenceXml(), "META-INF/persistence.xml" )
.addAsWebInfResource( EmptyAsset.INSTANCE, "beans.xml" );
}

private static Asset persistenceXml() {
String persistenceXml = Descriptors.create( PersistenceDescriptor.class )
.version( "2.0" )
.createPersistenceUnit()
.name( "default" )
.jtaDataSource( "java:jboss/datasources/ExampleDS" )
.up()
.exportAsString();
return new StringAsset( persistenceXml );
}

@Test
public void testDefaultTraversableResolverInJPAEnvironmentIsJPATraversableResolver() throws Exception {
assertThat( TraversableResolvers.getDefault() ).isInstanceOf( JPATraversableResolver.class );
}
}