Migrate test to JUnit 5

This commit is contained in:
Andrea Boriero 2021-09-07 19:02:56 +02:00
parent 8ce2098deb
commit b2b8197388
1 changed files with 91 additions and 61 deletions

View File

@ -6,80 +6,110 @@
*/ */
package org.hibernate.orm.test.discriminatedcollections; package org.hibernate.orm.test.discriminatedcollections;
import org.hibernate.Session;
import org.hibernate.query.criteria.JpaCriteriaQuery;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import javax.persistence.criteria.JoinType; import javax.persistence.criteria.JoinType;
import static org.junit.Assert.assertEquals; import org.hibernate.query.criteria.JpaCriteriaQuery;
import static org.junit.Assert.assertNotEquals;
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.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
/** /**
* @author Gavin King * @author Gavin King
*/ */
public class TempTest extends BaseCoreFunctionalTestCase { @DomainModel(
annotatedClasses = {
@Override Client.class, Account.class, DebitAccount.class, CreditAccount.class
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { Client.class, Account.class, DebitAccount.class, CreditAccount.class };
} }
)
@SessionFactory
public class TempTest {
@Test @Test
public void test() { public void test(SessionFactoryScope scope) {
Client c = new Client( "Gavin" ); Client c = new Client( "Gavin" );
DebitAccount da = new DebitAccount( c ); DebitAccount da = new DebitAccount( c );
CreditAccount ca = new CreditAccount( c ); CreditAccount ca = new CreditAccount( c );
c.getDebitAccounts().add( da ); c.getDebitAccounts().add( da );
c.getCreditAccounts().add( ca ); c.getCreditAccounts().add( ca );
Session session = openSession();
session.beginTransaction();
session.persist(c);
session.getTransaction().commit();
session.close();
session = openSession(); scope.inTransaction(
session ->
session.persist( c )
);
scope.inSession(
session -> {
Client client = session.find( Client.class, c.getId() ); Client client = session.find( Client.class, c.getId() );
assertEquals( 1, client.getDebitAccounts().size() ); assertEquals( 1, client.getDebitAccounts().size() );
assertEquals( 1, client.getCreditAccounts().size() ); assertEquals( 1, client.getCreditAccounts().size() );
assertNotEquals(client.getDebitAccounts().iterator().next().getId(), assertNotEquals(
client.getCreditAccounts().iterator().next().getId()); client.getDebitAccounts().iterator().next().getId(),
session.close(); client.getCreditAccounts().iterator().next().getId()
);
}
);
session = openSession(); scope.inSession(
client = session.createQuery("from Client", Client.class).getSingleResult(); session -> {
Client client = session.createQuery( "from Client", Client.class ).getSingleResult();
assertEquals( 1, client.getDebitAccounts().size() ); assertEquals( 1, client.getDebitAccounts().size() );
assertEquals( 1, client.getCreditAccounts().size() ); assertEquals( 1, client.getCreditAccounts().size() );
assertNotEquals(client.getDebitAccounts().iterator().next().getId(), assertNotEquals(
client.getCreditAccounts().iterator().next().getId()); client.getDebitAccounts().iterator().next().getId(),
session.close(); client.getCreditAccounts().iterator().next().getId()
);
}
);
session = openSession(); scope.inSession(
client = session.createQuery("from Client c left join fetch c.debitAccounts", Client.class).getSingleResult(); session -> {
Client client = session.createQuery( "from Client c left join fetch c.debitAccounts", Client.class )
.getSingleResult();
assertEquals( 1, client.getDebitAccounts().size() ); assertEquals( 1, client.getDebitAccounts().size() );
assertEquals( 1, client.getCreditAccounts().size() ); assertEquals( 1, client.getCreditAccounts().size() );
assertNotEquals(client.getDebitAccounts().iterator().next().getId(), assertNotEquals(
client.getCreditAccounts().iterator().next().getId()); client.getDebitAccounts().iterator().next().getId(),
session.close(); client.getCreditAccounts().iterator().next().getId()
);
}
);
session = openSession(); scope.inSession(
client = session.createQuery("from Client c left join fetch c.debitAccounts left join fetch c.creditAccounts", Client.class).getSingleResult(); session -> {
Client client = session.createQuery(
"from Client c left join fetch c.debitAccounts left join fetch c.creditAccounts",
Client.class
).getSingleResult();
assertEquals( 1, client.getDebitAccounts().size() ); assertEquals( 1, client.getDebitAccounts().size() );
assertEquals( 1, client.getCreditAccounts().size() ); assertEquals( 1, client.getCreditAccounts().size() );
assertNotEquals(client.getDebitAccounts().iterator().next().getId(), assertNotEquals(
client.getCreditAccounts().iterator().next().getId()); client.getDebitAccounts().iterator().next().getId(),
session.close(); client.getCreditAccounts().iterator().next().getId()
);
}
);
session = openSession(); scope.inSession(
JpaCriteriaQuery<Client> query = sessionFactory().getCriteriaBuilder().createQuery(Client.class); session -> {
JpaCriteriaQuery<Client> query = scope.getSessionFactory()
.getCriteriaBuilder()
.createQuery( Client.class );
query.from( Client.class ).fetch( Client_.creditAccounts, JoinType.LEFT ); query.from( Client.class ).fetch( Client_.creditAccounts, JoinType.LEFT );
client = session.createQuery(query).getSingleResult(); Client client = session.createQuery( query ).getSingleResult();
assertEquals( 1, client.getDebitAccounts().size() ); assertEquals( 1, client.getDebitAccounts().size() );
assertEquals( 1, client.getCreditAccounts().size() ); assertEquals( 1, client.getCreditAccounts().size() );
assertNotEquals(client.getDebitAccounts().iterator().next().getId(), assertNotEquals(
client.getCreditAccounts().iterator().next().getId()); client.getDebitAccounts().iterator().next().getId(),
session.close(); client.getCreditAccounts().iterator().next().getId()
);
}
);
} }
} }