Skip to content

Commit

Permalink
HHH-13756 enrich hql testing class
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanQingyangXu authored and sebersole committed Mar 19, 2020
1 parent 3b9c1cd commit 979e146
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 18 deletions.
Expand Up @@ -74,6 +74,7 @@
}
)
@SessionFactory
@TestForIssue( jiraKey = "HHH-13756" )
public class EntityGraphLoadPlanBuilderTest {

@Test
Expand All @@ -96,7 +97,6 @@ void testBasicFetchLoadPlanBuilding(SessionFactoryScope scope) {
}

@Test
@TestForIssue( jiraKey = "HHH-13756" )
void testFetchLoadPlanBuildingWithSubgraph(SessionFactoryScope scope) {
scope.inTransaction(
em -> {
Expand All @@ -115,7 +115,6 @@ void testFetchLoadPlanBuildingWithSubgraph(SessionFactoryScope scope) {
}

@Test
@TestForIssue( jiraKey = "HHH-13756" )
void testFetchLoadPlanBuildingWithDeepSubgraph(SessionFactoryScope scope) {
scope.inTransaction(
em -> {
Expand Down Expand Up @@ -187,7 +186,6 @@ void testBasicLoadLoadPlanBuilding(SessionFactoryScope scope) {
}

@Test
@TestForIssue( jiraKey = "HHH-13756" )
void testLoadLoadPlanBuildingWithSubgraph(SessionFactoryScope scope) {
scope.inTransaction(
em -> {
Expand Down Expand Up @@ -218,7 +216,6 @@ void testLoadLoadPlanBuildingWithSubgraph(SessionFactoryScope scope) {
}

@Test
@TestForIssue( jiraKey = "HHH-13756" )
void testBasicElementCollectionsLoadGraph(SessionFactoryScope scope) {
scope.inTransaction(
em -> {
Expand All @@ -234,7 +231,6 @@ void testBasicElementCollectionsLoadGraph(SessionFactoryScope scope) {
}

@Test
@TestForIssue( jiraKey = "HHH-13756" )
void testBasicElementCollectionsFetchGraph(SessionFactoryScope scope) {
scope.inTransaction(
em -> {
Expand All @@ -250,7 +246,6 @@ void testBasicElementCollectionsFetchGraph(SessionFactoryScope scope) {
}

@Test
@TestForIssue( jiraKey = "HHH-13756" )
void testEmbeddedCollectionLoadSubgraph(SessionFactoryScope scope) {
scope.inTransaction(
em -> {
Expand Down
Expand Up @@ -6,8 +6,11 @@
*/
package org.hibernate.orm.test.query.hql.entitygraph;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import javax.persistence.ElementCollection;
import javax.persistence.Embeddable;
import javax.persistence.Embedded;
Expand All @@ -34,22 +37,28 @@
import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
import org.hibernate.sql.ast.tree.from.CompositeTableGroup;
import org.hibernate.sql.ast.tree.from.FromClause;
import org.hibernate.sql.ast.tree.from.StandardTableGroup;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupJoin;
import org.hibernate.sql.ast.tree.select.SelectStatement;
import org.hibernate.sql.results.graph.DomainResult;
import org.hibernate.sql.results.graph.Fetch;
import org.hibernate.sql.results.graph.collection.internal.DelayedCollectionFetch;
import org.hibernate.sql.results.graph.embeddable.internal.EmbeddableFetchImpl;
import org.hibernate.sql.results.graph.entity.EntityFetch;
import org.hibernate.sql.results.graph.entity.EntityResult;
import org.hibernate.sql.results.graph.entity.internal.EntityFetchDelayedImpl;
import org.hibernate.sql.results.graph.entity.internal.EntityFetchJoinedImpl;

import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hibernate.testing.hamcrest.AssignableMatcher.assignableTo;
import static org.hibernate.testing.hamcrest.CollectionMatchers.hasSize;
import static org.hibernate.testing.hamcrest.CollectionMatchers.isEmpty;
Expand All @@ -68,6 +77,7 @@
}
)
@SessionFactory
@TestForIssue( jiraKey = "HHH-13756" )
public class HqlEntityGraphTest {

@Test
Expand All @@ -82,13 +92,183 @@ void testBasicFetchSemantics(SessionFactoryScope scope) {
assertEmptyJoinedGroup( sqlAst );

// Check the domain-result graph
assertDomainResult( sqlAst, HqlEntityGraphTest.Cat.class, "owner", EntityGraphLoadPlanBuilderTest.Person.class,
assertDomainResult( sqlAst, HqlEntityGraphTest.Cat.class, "owner", Person.class,
entityFetch -> assertThat( entityFetch, instanceOf( EntityFetchDelayedImpl.class ) )
);
}
);
}

@Test
void testFetchSemanticsWithSubgraph(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
final RootGraphImplementor<HqlEntityGraphTest.Cat> eg = session.createEntityGraph( HqlEntityGraphTest.Cat.class );
eg.addSubgraph( "owner", HqlEntityGraphTest.Person.class );

final SelectStatement sqlAst = buildSqlSelectAst( HqlEntityGraphTest.Cat.class, "select c from Cat as c", eg, GraphSemantic.FETCH, session );

// Check the from-clause
assertEntityValuedJoinedGroup( sqlAst, "owner", HqlEntityGraphTest.Person.class, this::assertPersonHomeAddressJoinedGroup );

// Check the domain-result graph
assertDomainResult( sqlAst, HqlEntityGraphTest.Cat.class, "owner", HqlEntityGraphTest.Person.class, entityFetch -> {} );
}
);
}

@Test
void testFetchSemanticsWithDeepSubgraph(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
final RootGraphImplementor<HqlEntityGraphTest.Cat> eg = session.createEntityGraph( HqlEntityGraphTest.Cat.class );
eg.addSubgraph( "owner", HqlEntityGraphTest.Person.class ).addSubgraph( "company", HqlEntityGraphTest.ExpressCompany.class );

final SelectStatement sqlAst = buildSqlSelectAst( HqlEntityGraphTest.Cat.class, "select c from Cat as c", eg, GraphSemantic.FETCH, session );

// Check the from-clause
assertEntityValuedJoinedGroup( sqlAst, "owner", HqlEntityGraphTest.Person.class, tableGroup -> {
Set<TableGroupJoin> tableGroupJoins = tableGroup.getTableGroupJoins();
Map<String, Class<? extends TableGroup>> tableGroupByName = tableGroupJoins.stream()
.map( TableGroupJoin::getJoinedGroup )
.collect( Collectors.toMap(
tg -> tg.getModelPart().getPartName(),
TableGroup::getClass
) );
Map<String, Class<? extends TableGroup> > expectedTableGroupByName = new HashMap<>();
expectedTableGroupByName.put( "homeAddress", CompositeTableGroup.class );
expectedTableGroupByName.put( "company", StandardTableGroup.class );
assertThat( tableGroupByName, is( expectedTableGroupByName ) );
} );

// Check the domain-result graph
assertDomainResult( sqlAst, HqlEntityGraphTest.Cat.class, "owner", HqlEntityGraphTest.Person.class, entityFetch -> {
assertThat( entityFetch, instanceOf( EntityFetchJoinedImpl.class ) );
final EntityResult ownerEntityResult = ( (EntityFetchJoinedImpl) entityFetch ).getEntityResult();
final Map<String, Class<? extends Fetch>> fetchClassByAttributeName = ownerEntityResult.getFetches()
.stream().collect( Collectors.toMap(
fetch -> fetch.getFetchedMapping().getPartName(),
Fetch::getClass
) );
final Map<String, Class<? extends Fetch>> expectedFetchClassByAttributeName = new HashMap<>();
expectedFetchClassByAttributeName.put( "homeAddress", EmbeddableFetchImpl.class );
expectedFetchClassByAttributeName.put( "pets", DelayedCollectionFetch.class );
expectedFetchClassByAttributeName.put( "company", EntityFetchJoinedImpl.class );
assertThat( fetchClassByAttributeName, is( expectedFetchClassByAttributeName ) );

final Fetch companyFetch = ownerEntityResult.findFetch( "company" );
assertThat( companyFetch, notNullValue() );

final EntityResult companyEntityResult = ( (EntityFetchJoinedImpl) companyFetch).getEntityResult();
assertThat( companyEntityResult.getFetches(), hasSize( 1 ) );

final Fetch shipAddressesFetch = companyEntityResult.getFetches().get( 0 );
assertThat( shipAddressesFetch.getFetchedMapping().getPartName(), is( "shipAddresses" ) );
assertThat( shipAddressesFetch, instanceOf( DelayedCollectionFetch.class ) );
} );
}
);
}

@Test
void testBasicLoadSemantics(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
final RootGraphImplementor<HqlEntityGraphTest.Cat> eg = session.createEntityGraph( HqlEntityGraphTest.Cat.class );

final SelectStatement sqlAst = buildSqlSelectAst( HqlEntityGraphTest.Cat.class, "select c from Cat as c", eg, GraphSemantic.LOAD, session );

// Check the from-clause
assertEmptyJoinedGroup( sqlAst );

// Check the domain-result graph
assertDomainResult( sqlAst, HqlEntityGraphTest.Cat.class, "owner", HqlEntityGraphTest.Person.class,
entityFetch -> assertThat( entityFetch, instanceOf( EntityFetchDelayedImpl.class ) ) );
}
);
}

@Test
void testLoadLoadPlanBuildingWithSubgraph(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
final RootGraphImplementor<HqlEntityGraphTest.Cat> eg = session.createEntityGraph( HqlEntityGraphTest.Cat.class );
eg.addSubgraph( "owner", HqlEntityGraphTest.Person.class );

final SelectStatement sqlAst = buildSqlSelectAst( HqlEntityGraphTest.Cat.class, "select c from Cat as c", eg, GraphSemantic.LOAD, session );

// Check the from-clause
assertEntityValuedJoinedGroup( sqlAst, "owner", HqlEntityGraphTest.Person.class, this::assertPersonHomeAddressJoinedGroup );

// Check the domain-result graph
assertDomainResult( sqlAst, HqlEntityGraphTest.Cat.class, "owner", HqlEntityGraphTest.Person.class, entityFetch -> {
assertThat( entityFetch, instanceOf( EntityFetchJoinedImpl.class ) );
final EntityResult entityResult = ( (EntityFetchJoinedImpl) entityFetch ).getEntityResult();
final Map<String, Class<? extends Fetch>> fetchClassByAttributeName = entityResult.getFetches().stream().collect( Collectors.toMap(
fetch -> fetch.getFetchedMapping().getPartName(),
Fetch::getClass
) );
final Map<String, Class<? extends Fetch>> expectedFetchClassByAttributeName = new HashMap<>();
expectedFetchClassByAttributeName.put( "pets", DelayedCollectionFetch.class );
expectedFetchClassByAttributeName.put( "homeAddress", EmbeddableFetchImpl.class );
expectedFetchClassByAttributeName.put( "company", EntityFetchDelayedImpl.class );
assertThat( fetchClassByAttributeName, is( expectedFetchClassByAttributeName ) );
} );
}
);
}

@Test
void testBasicElementCollectionsLoadGraph(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
final RootGraphImplementor<HqlEntityGraphTest.Dog> eg = session.createEntityGraph( HqlEntityGraphTest.Dog.class );
eg.addAttributeNodes( "favorites" );

final SelectStatement sqlAst = buildSqlSelectAst( HqlEntityGraphTest.Dog.class, "select d from Dog as d", eg, GraphSemantic.LOAD, session );

// Check the from-clause
assertPluralAttributeJoinedGroup( sqlAst, "favorites" );
}
);
}

@Test
void testBasicElementCollectionsFetchGraph(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
final RootGraphImplementor<HqlEntityGraphTest.Dog> eg = session.createEntityGraph( HqlEntityGraphTest.Dog.class );
eg.addAttributeNodes( "favorites" );

final SelectStatement sqlAst = buildSqlSelectAst( HqlEntityGraphTest.Dog.class, "select d from Dog as d", eg, GraphSemantic.FETCH, session );

// Check the from-clause
assertPluralAttributeJoinedGroup( sqlAst, "favorites" );
}
);
}

@Test
void testEmbeddedCollectionLoadSubgraph(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
final RootGraphImplementor<HqlEntityGraphTest.ExpressCompany> eg = session.createEntityGraph( HqlEntityGraphTest.ExpressCompany.class );
eg.addAttributeNodes( "shipAddresses" );

final SelectStatement sqlAst = buildSqlSelectAst(
HqlEntityGraphTest.ExpressCompany.class,
"select company from ExpressCompany as company",
eg, GraphSemantic.LOAD,
session
);

// Check the from-clause
assertPluralAttributeJoinedGroup( sqlAst, "shipAddresses" );

}
);
}

// util methods for verifying 'from-clause' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

private void assertEmptyJoinedGroup(SelectStatement sqlAst) {
Expand Down Expand Up @@ -200,7 +380,7 @@ private <T> SelectStatement buildSqlSelectAst(
return sqmInterpretation.getSqlAst();
}

@Entity
@Entity(name = "Dog")
public static class Dog {
@Id
String name;
Expand All @@ -209,46 +389,46 @@ public static class Dog {
Set<String> favorites;
}

@Entity
@Entity(name = "Cat")
public static class Cat {
@Id
String name;

@ManyToOne(fetch = FetchType.LAZY)
EntityGraphLoadPlanBuilderTest.Person owner;
Person owner;
}

@Entity
@Entity(name = "Person")
public static class Person {
@Id
String name;

@OneToMany(mappedBy = "owner")
Set<EntityGraphLoadPlanBuilderTest.Cat> pets;
Set<Cat> pets;

@Embedded
EntityGraphLoadPlanBuilderTest.Address homeAddress;
Address homeAddress;

@ManyToOne(fetch = FetchType.LAZY)
EntityGraphLoadPlanBuilderTest.ExpressCompany company;
ExpressCompany company;
}

@Embeddable
public static class Address {
@ManyToOne
EntityGraphLoadPlanBuilderTest.Country country;
Country country;
}

@Entity
@Entity(name = "ExpressCompany")
public static class ExpressCompany {
@Id
String name;

@ElementCollection
Set<EntityGraphLoadPlanBuilderTest.Address> shipAddresses;
Set<Address> shipAddresses;
}

@Entity
@Entity(name = "Country")
public static class Country {
@Id
String name;
Expand Down

0 comments on commit 979e146

Please sign in to comment.