HHH-14925 Add additional unit tests.

This commit is contained in:
Karel Maesen 2021-11-11 17:36:11 +01:00
parent 0300e54fef
commit 5a7ad6d11c
3 changed files with 147 additions and 2 deletions

View File

@ -6,8 +6,12 @@
*/
package org.hibernate.orm.test.mapping.type.contribution.array;
import java.util.List;
import org.hibernate.boot.model.FunctionContributor;
import org.hibernate.boot.model.TypeContributor;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.query.criteria.HibernateCriteriaBuilder;
import org.hibernate.testing.orm.junit.BootstrapServiceRegistry;
import org.hibernate.testing.orm.junit.BootstrapServiceRegistry.JavaService;
@ -18,13 +22,21 @@ import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import org.assertj.core.util.Arrays;
/**
* @author Steve Ebersole
*/
@BootstrapServiceRegistry(
javaServices = @JavaService( role = TypeContributor.class, impl = StringArrayTypeContributor.class )
javaServices = {
@JavaService(role = TypeContributor.class, impl = StringArrayTypeContributor.class),
@JavaService(role = FunctionContributor.class, impl = StringArrayFunctionContributor.class)
}
)
@DomainModel(annotatedClasses = Post.class)
@SessionFactory
@ -50,11 +62,38 @@ public class StringArrayContributorTests {
session.createQuery( "select p from Post p where array_contains(:arr, p.title) = true" )
.setParameter( "arr", Arrays.array( "a", "b" ) )
.list();
});
} );
}
@Test
public void testParameterInJpaCriteria(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<Post> cr = cb.createQuery( Post.class );
Root<Post> root = cr.from( Post.class );
cr.select( root ).where(
ArrayPredicates.equalLength( cb, root.get( "tags" ), Arrays.array( "a", "b" ) )
);
List<Post> resultList = session.createQuery( cr ).getResultList();
} );
}
@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> session.createQuery( "delete Post" ).executeUpdate() );
}
}
class ArrayPredicates {
public static Predicate equalLength(
CriteriaBuilder criteriaBuilder, Expression<? extends String[]> arr1,
String[] arr2) {
HibernateCriteriaBuilder cb = (HibernateCriteriaBuilder) criteriaBuilder;
return cb.equal(
criteriaBuilder.function( "array_length", long.class, arr1 ),
criteriaBuilder.function( "array_length", long.class, cb.value( arr2 ) )
);
}
}

View File

@ -0,0 +1,42 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.mapping.type.contribution.array;
import org.hibernate.boot.model.FunctionContributions;
import org.hibernate.boot.model.FunctionContributor;
import org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor;
import org.hibernate.query.sqm.produce.function.StandardArgumentsValidators;
import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.spi.TypeConfiguration;
public class StringArrayFunctionContributor implements FunctionContributor {
@Override
public void contributeFunctions(FunctionContributions functionContributions) {
TypeConfiguration typeConfiguration = functionContributions.getTypeConfiguration();
functionContributions.getFunctionRegistry().register(
"array_length",
new NamedSqmFunctionDescriptor(
"array_length",
true,
StandardArgumentsValidators.exactly( 1 ),
StandardFunctionReturnTypeResolvers.invariant(
typeConfiguration.getBasicTypeRegistry()
.resolve( StandardBasicTypes.INTEGER ) )
)
);
}
}

View File

@ -0,0 +1,64 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.spatial.integration.predicates;
import java.util.List;
import org.hibernate.spatial.predicate.GeolatteSpatialPredicates;
import org.hibernate.spatial.testing.IsSupportedBySpatial;
import org.hibernate.spatial.testing.domain.GeomEntity;
import org.hibernate.spatial.testing.domain.SpatialDomainModel;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.SessionFactoryScopeAware;
import org.junit.jupiter.api.Test;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import org.geolatte.geom.G2D;
import org.geolatte.geom.Polygon;
import static org.geolatte.geom.builder.DSL.g;
import static org.geolatte.geom.builder.DSL.polygon;
import static org.geolatte.geom.builder.DSL.ring;
import static org.geolatte.geom.crs.CoordinateReferenceSystems.WGS84;
@DomainModel(modelDescriptorClasses = SpatialDomainModel.class)
@RequiresDialectFeature(feature = IsSupportedBySpatial.class)
@SessionFactory
public class PredicateSmokeTest implements SessionFactoryScopeAware {
private SessionFactoryScope scope;
Polygon<G2D> poly = polygon(
WGS84,
ring( g( 0, 0 ), g( 0, 10 ), g( 10, 10 ), g( 10, 0 ), g( 0, 0 ) )
);
@Test
public void test() {
scope.inTransaction( (session) -> {
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<GeomEntity> cr = cb.createQuery( GeomEntity.class );
Root<GeomEntity> root = cr.from( GeomEntity.class );
cr.select( root ).where(
GeolatteSpatialPredicates.intersects( cb, root.get("geom"), poly )
);
List<GeomEntity> resultList = session.createQuery( cr ).getResultList();
} );
}
@Override
public void injectSessionFactoryScope(SessionFactoryScope scope) {
this.scope = scope;
}
}