Re-enable additional tests

This commit is contained in:
Andrea Boriero 2021-06-17 14:15:59 +02:00 committed by Andrea Boriero
parent ee07497e85
commit 8e3e119d7d
6 changed files with 223 additions and 199 deletions

View File

@ -4,12 +4,11 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. * 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.ecid; package org.hibernate.orm.test.ecid;
import java.io.Serializable; import java.io.Serializable;
import java.util.Iterator; import java.util.Iterator;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
@ -17,31 +16,45 @@ import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.orm.junit.DomainModel;
import org.junit.Test; import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertTrue;
/** /**
* @author Gail Badner * @author Gail Badner
*/ */
public class CompositeIdAssociationsWithEmbeddedCompositeIdTest extends BaseCoreFunctionalTestCase { @DomainModel(
annotatedClasses = {
CompositeIdAssociationsWithEmbeddedCompositeIdTest.Parent.class,
CompositeIdAssociationsWithEmbeddedCompositeIdTest.Person.class
}
)
@SessionFactory
public class CompositeIdAssociationsWithEmbeddedCompositeIdTest {
@Override @AfterEach
protected Class<?>[] getAnnotatedClasses() { public void tearDown(SessionFactoryScope scope){
return new Class<?>[] { Parent.class, Person.class }; scope.inTransaction(
session -> {
session.createQuery( "delete from Person" ).executeUpdate();
session.createQuery( "delete from Parent" ).executeUpdate();
}
);
} }
@Test @Test
@TestForIssue( jiraKey = "HHH-13114") @TestForIssue(jiraKey = "HHH-13114")
public void testQueries() { public void testQueries(SessionFactoryScope scope) {
Parent parent1 = new Parent( "Jane", 0 ); Parent parent1 = new Parent( "Jane", 0 );
Parent parent2 = new Parent( "Jim", 1 ); Parent parent2 = new Parent( "Jim", 1 );
Person person = doInHibernate( Person person = scope.fromTransaction(
this::sessionFactory, session -> { session -> {
Person p = new Person(); Person p = new Person();
p.setParent1( parent1 ); p.setParent1( parent1 );
p.setParent2( parent2 ); p.setParent2( parent2 );
@ -50,27 +63,27 @@ public class CompositeIdAssociationsWithEmbeddedCompositeIdTest extends BaseCore
session.persist( parent2 ); session.persist( parent2 );
session.persist( p ); session.persist( p );
return p; return p;
}); } );
doInHibernate( scope.inTransaction(
this::sessionFactory, session -> { session ->
checkResult( session.get( Person.class, person ) ); checkResult( session.get( Person.class, person ) )
}); );
doInHibernate( scope.inTransaction(
this::sessionFactory, session -> { session ->
checkResult( session.createQuery( "from Person p", Person.class ).getSingleResult() ); checkResult( session.createQuery( "from Person p", Person.class ).getSingleResult() )
}); );
doInHibernate( scope.inTransaction(
this::sessionFactory, session -> { session -> {
Iterator<Person> iterator = session.createQuery( "from Person p", Person.class ).list().iterator(); Iterator<Person> iterator = session.createQuery( "from Person p", Person.class ).list().iterator();
assertTrue( iterator.hasNext() ); assertTrue( iterator.hasNext() );
Person p = iterator.next(); Person p = iterator.next();
checkResult( p ); checkResult( p );
assertFalse( iterator.hasNext() ); assertFalse( iterator.hasNext() );
}); } );
} }
private void checkResult(Person p) { private void checkResult(Person p) {
@ -83,7 +96,7 @@ public class CompositeIdAssociationsWithEmbeddedCompositeIdTest extends BaseCore
@Entity(name = "Person") @Entity(name = "Person")
public static class Person implements Serializable { public static class Person implements Serializable {
@Id @Id
@JoinColumns( value = { @JoinColumns(value = {
@JoinColumn(name = "p1Name"), @JoinColumn(name = "p1Name"),
@JoinColumn(name = "p1Index") @JoinColumn(name = "p1Index")
}) })
@ -91,7 +104,7 @@ public class CompositeIdAssociationsWithEmbeddedCompositeIdTest extends BaseCore
private Parent parent1; private Parent parent1;
@Id @Id
@JoinColumns( value = { @JoinColumns(value = {
@JoinColumn(name = "p2Name"), @JoinColumn(name = "p2Name"),
@JoinColumn(name = "p2Index") @JoinColumn(name = "p2Index")
}) })
@ -116,6 +129,7 @@ public class CompositeIdAssociationsWithEmbeddedCompositeIdTest extends BaseCore
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
@ -123,6 +137,7 @@ public class CompositeIdAssociationsWithEmbeddedCompositeIdTest extends BaseCore
public Parent getParent1() { public Parent getParent1() {
return parent1; return parent1;
} }
public void setParent1(Parent parent1) { public void setParent1(Parent parent1) {
this.parent1 = parent1; this.parent1 = parent1;
} }
@ -130,6 +145,7 @@ public class CompositeIdAssociationsWithEmbeddedCompositeIdTest extends BaseCore
public Parent getParent2() { public Parent getParent2() {
return parent2; return parent2;
} }
public void setParent2(Parent parent2) { public void setParent2(Parent parent2) {
this.parent2 = parent2; this.parent2 = parent2;
} }
@ -137,6 +153,7 @@ public class CompositeIdAssociationsWithEmbeddedCompositeIdTest extends BaseCore
public int getBirthOrder() { public int getBirthOrder() {
return birthOrder; return birthOrder;
} }
public void setBirthOrder(int birthOrder) { public void setBirthOrder(int birthOrder) {
this.birthOrder = birthOrder; this.birthOrder = birthOrder;
} }
@ -148,7 +165,7 @@ public class CompositeIdAssociationsWithEmbeddedCompositeIdTest extends BaseCore
private String name; private String name;
@Id @Id
@Column(name="ind") @Column(name = "ind")
private int index; private int index;
public Parent() { public Parent() {

View File

@ -10,7 +10,7 @@
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping <hibernate-mapping
package="org.hibernate.test.ecid" package="org.hibernate.orm.test.ecid"
default-access="field"> default-access="field">
<!-- <!--

View File

@ -6,7 +6,7 @@
*/ */
//$Id: Course.java 6913 2005-05-25 17:37:51Z oneovthafew $ //$Id: Course.java 6913 2005-05-25 17:37:51Z oneovthafew $
package org.hibernate.test.ecid; package org.hibernate.orm.test.ecid;
import java.io.Serializable; import java.io.Serializable;
/** /**

View File

@ -0,0 +1,172 @@
/*
* 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.ecid;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.proxy.HibernateProxy;
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.AfterEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Gavin King
*/
@DomainModel(
xmlMappings = "org/hibernate/orm/test/ecid/Course.hbm.xml"
)
@SessionFactory
public class EmbeddedCompositeIdTest {
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session ->
session.createQuery( "delete from Course" ).executeUpdate()
);
}
@Test
public void testMerge(SessionFactoryScope scope) {
Course uc = new UniversityCourse( "mat2000", "Monash", "second year maths", 0 );
Course c = new Course( "eng5000", "BHS", "grade 5 english" );
scope.inTransaction(
session -> {
session.persist( uc );
session.persist( c );
}
);
c.setDescription( "Grade 5 English" );
uc.setDescription( "Second year mathematics" );
scope.inTransaction(
session -> {
session.merge( c );
session.merge( uc );
}
);
scope.inTransaction(
session -> {
session.delete( c );
session.delete( uc );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-799")
public void testMerging(SessionFactoryScope scope) {
Course course = new Course( "EN-101", "BA", "preparatory english" );
scope.inTransaction(
session ->
session.persist( course )
);
String newDesc = "basic preparatory english";
course.setDescription( newDesc );
scope.inTransaction(
session -> {
Course c = (Course) session.merge( course );
assertEquals( newDesc, c.getDescription(), "description not merged" );
}
);
scope.inTransaction(
session -> {
Course cid = new Course( "EN-101", "BA", null );
Course c = session.get( Course.class, cid );
assertEquals( newDesc, c.getDescription(), "description not merged" );
session.delete( c );
}
);
}
@Test
public void testPolymorphism(SessionFactoryScope scope) {
Course uc = new UniversityCourse( "mat2000", "Monash", "second year maths", 0 );
Course c = new Course( "eng5000", "BHS", "grade 5 english" );
scope.inTransaction(
session -> {
session.persist( uc );
session.persist( c );
}
);
scope.inTransaction(
session -> {
Course ucid = new Course( "mat2000", "Monash", null );
Course cid = new Course( "eng5000", "BHS", null );
Course luc = session.load( Course.class, ucid );
Course lc = session.load( Course.class, cid );
assertFalse( Hibernate.isInitialized( luc ) );
assertFalse( Hibernate.isInitialized( lc ) );
assertEquals( UniversityCourse.class, Hibernate.getClass( luc ) );
assertEquals( Course.class, Hibernate.getClass( lc ) );
assertSame( ( (HibernateProxy) lc ).getHibernateLazyInitializer().getImplementation(), cid );
assertEquals( "eng5000", c.getCourseCode() );
assertEquals( "mat2000", uc.getCourseCode() );
}
);
scope.inTransaction(
session -> {
Course ucid = new Course( "mat2000", "Monash", null );
Course cid = new Course( "eng5000", "BHS", null );
Course luc = session.get( Course.class, ucid );
Course lc = session.get( Course.class, cid );
assertTrue( Hibernate.isInitialized( luc ) );
assertTrue( Hibernate.isInitialized( lc ) );
assertEquals( UniversityCourse.class, Hibernate.getClass( luc ) );
assertEquals( Course.class, Hibernate.getClass( lc ) );
assertSame( lc, cid );
assertEquals( "eng5000", c.getCourseCode() );
assertEquals( "mat2000", uc.getCourseCode() );
}
);
List<Course> courses = scope.fromTransaction(
session -> {
List<Course> list = session.createQuery( "from Course order by courseCode" ).list();
assertTrue( list.get( 0 ) instanceof Course );
assertTrue( list.get( 1 ) instanceof UniversityCourse );
assertEquals( "eng5000", list.get( 0 ).getCourseCode() );
assertEquals( "mat2000", list.get( 1 ).getCourseCode() );
return list;
}
);
courses.get( 0 ).setDescription( "Grade 5 English" );
courses.get( 1 ).setDescription( "Second year mathematics" );
scope.inTransaction(
session -> {
session.saveOrUpdate( courses.get( 0 ) );
session.saveOrUpdate( courses.get( 1 ) );
}
);
scope.inTransaction(
session ->
courses.forEach( course -> session.delete( c ) )
);
}
}

View File

@ -6,7 +6,7 @@
*/ */
//$Id: UniversityCourse.java 6913 2005-05-25 17:37:51Z oneovthafew $ //$Id: UniversityCourse.java 6913 2005-05-25 17:37:51Z oneovthafew $
package org.hibernate.test.ecid; package org.hibernate.orm.test.ecid;
/** /**

View File

@ -1,165 +0,0 @@
/*
* 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.test.ecid;
import java.util.List;
import org.junit.Test;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
/**
* @author Gavin King
*/
public class EmbeddedCompositeIdTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {
return new String[] { "ecid/Course.hbm.xml" };
}
@Test
public void testMerge() {
Session s = openSession();
Transaction t = s.beginTransaction();
Course uc = new UniversityCourse("mat2000", "Monash", "second year maths", 0);
Course c = new Course("eng5000", "BHS", "grade 5 english");
s.persist(uc);
s.persist(c);
t.commit();
s.close();
c.setDescription("Grade 5 English");
uc.setDescription("Second year mathematics");
s = openSession();
t = s.beginTransaction();
s.merge(c);
s.merge(uc);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
s.delete(c);
s.delete(uc);
t.commit();
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-799" )
public void testMerging() {
Session s = openSession();
Transaction t = s.beginTransaction();
Course course = new Course( "EN-101", "BA", "preparatory english" );
s.persist( course );
t.commit();
s.close();
String newDesc = "basic preparatory english";
course.setDescription( newDesc );
s = openSession();
t = s.beginTransaction();
Course c = (Course) s.merge( course );
assertEquals( "description not merged", newDesc, c.getDescription() );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
Course cid = new Course( "EN-101", "BA", null );
course = ( Course ) s.get( Course.class, cid );
assertEquals( "description not merged", newDesc, course.getDescription() );
s.delete( course );
t.commit();
s.close();
}
@Test
public void testPolymorphism() {
Session s = openSession();
Transaction t = s.beginTransaction();
Course uc = new UniversityCourse("mat2000", "Monash", "second year maths", 0);
Course c = new Course("eng5000", "BHS", "grade 5 english");
s.persist(uc);
s.persist(c);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
Course ucid = new Course("mat2000", "Monash", null);
Course cid = new Course("eng5000", "BHS", null);
Course luc = (Course) s.load(Course.class, ucid);
Course lc = (Course) s.load(Course.class, cid);
assertFalse( Hibernate.isInitialized(luc) );
assertFalse( Hibernate.isInitialized(lc) );
assertEquals( UniversityCourse.class, Hibernate.getClass(luc) );
assertEquals( Course.class, Hibernate.getClass(lc) );
assertSame( ( (HibernateProxy) lc ).getHibernateLazyInitializer().getImplementation(), cid );
assertEquals( c.getCourseCode(), "eng5000" );
assertEquals( uc.getCourseCode(), "mat2000" );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
ucid = new Course("mat2000", "Monash", null);
cid = new Course("eng5000", "BHS", null);
luc = (Course) s.get(Course.class, ucid);
lc = (Course) s.get(Course.class, cid);
assertTrue( Hibernate.isInitialized(luc) );
assertTrue( Hibernate.isInitialized(lc) );
assertEquals( UniversityCourse.class, Hibernate.getClass(luc) );
assertEquals( Course.class, Hibernate.getClass(lc) );
assertSame( lc, cid );
assertEquals( c.getCourseCode(), "eng5000" );
assertEquals( uc.getCourseCode(), "mat2000" );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
List list = s.createQuery("from Course order by courseCode").list();
assertTrue( list.get(0) instanceof Course );
assertTrue( list.get(1) instanceof UniversityCourse );
c = (Course) list.get(0);
uc = (UniversityCourse) list.get(1);
assertEquals( c.getCourseCode(), "eng5000" );
assertEquals( uc.getCourseCode(), "mat2000" );
t.commit();
s.close();
c.setDescription("Grade 5 English");
uc.setDescription("Second year mathematics");
s = openSession();
t = s.beginTransaction();
s.saveOrUpdate(c);
s.saveOrUpdate(uc);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
s.delete(c);
s.delete(uc);
t.commit();
s.close();
}
}