Migrate test to JUnit 5
This commit is contained in:
parent
8ce2098deb
commit
b2b8197388
|
@ -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(
|
||||||
Client client = session.find(Client.class, c.getId());
|
session ->
|
||||||
assertEquals(1, client.getDebitAccounts().size());
|
session.persist( c )
|
||||||
assertEquals(1, client.getCreditAccounts().size());
|
);
|
||||||
assertNotEquals(client.getDebitAccounts().iterator().next().getId(),
|
|
||||||
client.getCreditAccounts().iterator().next().getId());
|
|
||||||
session.close();
|
|
||||||
|
|
||||||
session = openSession();
|
scope.inSession(
|
||||||
client = session.createQuery("from Client", Client.class).getSingleResult();
|
session -> {
|
||||||
assertEquals(1, client.getDebitAccounts().size());
|
Client client = session.find( Client.class, c.getId() );
|
||||||
assertEquals(1, client.getCreditAccounts().size());
|
assertEquals( 1, client.getDebitAccounts().size() );
|
||||||
assertNotEquals(client.getDebitAccounts().iterator().next().getId(),
|
assertEquals( 1, client.getCreditAccounts().size() );
|
||||||
client.getCreditAccounts().iterator().next().getId());
|
assertNotEquals(
|
||||||
session.close();
|
client.getDebitAccounts().iterator().next().getId(),
|
||||||
|
client.getCreditAccounts().iterator().next().getId()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
session = openSession();
|
scope.inSession(
|
||||||
client = session.createQuery("from Client c left join fetch c.debitAccounts", Client.class).getSingleResult();
|
session -> {
|
||||||
assertEquals(1, client.getDebitAccounts().size());
|
Client client = session.createQuery( "from Client", Client.class ).getSingleResult();
|
||||||
assertEquals(1, client.getCreditAccounts().size());
|
assertEquals( 1, client.getDebitAccounts().size() );
|
||||||
assertNotEquals(client.getDebitAccounts().iterator().next().getId(),
|
assertEquals( 1, client.getCreditAccounts().size() );
|
||||||
client.getCreditAccounts().iterator().next().getId());
|
assertNotEquals(
|
||||||
session.close();
|
client.getDebitAccounts().iterator().next().getId(),
|
||||||
|
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 -> {
|
||||||
assertEquals(1, client.getDebitAccounts().size());
|
Client client = session.createQuery( "from Client c left join fetch c.debitAccounts", Client.class )
|
||||||
assertEquals(1, client.getCreditAccounts().size());
|
.getSingleResult();
|
||||||
assertNotEquals(client.getDebitAccounts().iterator().next().getId(),
|
assertEquals( 1, client.getDebitAccounts().size() );
|
||||||
client.getCreditAccounts().iterator().next().getId());
|
assertEquals( 1, client.getCreditAccounts().size() );
|
||||||
session.close();
|
assertNotEquals(
|
||||||
|
client.getDebitAccounts().iterator().next().getId(),
|
||||||
|
client.getCreditAccounts().iterator().next().getId()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
session = openSession();
|
scope.inSession(
|
||||||
JpaCriteriaQuery<Client> query = sessionFactory().getCriteriaBuilder().createQuery(Client.class);
|
session -> {
|
||||||
query.from(Client.class).fetch(Client_.creditAccounts, JoinType.LEFT);
|
Client client = session.createQuery(
|
||||||
client = session.createQuery(query).getSingleResult();
|
"from Client c left join fetch c.debitAccounts left join fetch c.creditAccounts",
|
||||||
assertEquals(1, client.getDebitAccounts().size());
|
Client.class
|
||||||
assertEquals(1, client.getCreditAccounts().size());
|
).getSingleResult();
|
||||||
assertNotEquals(client.getDebitAccounts().iterator().next().getId(),
|
assertEquals( 1, client.getDebitAccounts().size() );
|
||||||
client.getCreditAccounts().iterator().next().getId());
|
assertEquals( 1, client.getCreditAccounts().size() );
|
||||||
session.close();
|
assertNotEquals(
|
||||||
|
client.getDebitAccounts().iterator().next().getId(),
|
||||||
|
client.getCreditAccounts().iterator().next().getId()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
scope.inSession(
|
||||||
|
session -> {
|
||||||
|
JpaCriteriaQuery<Client> query = scope.getSessionFactory()
|
||||||
|
.getCriteriaBuilder()
|
||||||
|
.createQuery( Client.class );
|
||||||
|
query.from( Client.class ).fetch( Client_.creditAccounts, JoinType.LEFT );
|
||||||
|
Client client = session.createQuery( query ).getSingleResult();
|
||||||
|
assertEquals( 1, client.getDebitAccounts().size() );
|
||||||
|
assertEquals( 1, client.getCreditAccounts().size() );
|
||||||
|
assertNotEquals(
|
||||||
|
client.getDebitAccounts().iterator().next().getId(),
|
||||||
|
client.getCreditAccounts().iterator().next().getId()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue