HHH-13724 - Add more tests

This commit is contained in:
Andrea Boriero 2019-11-20 17:00:40 +00:00
parent a53772f6a0
commit e4f70d508f
20 changed files with 206 additions and 214 deletions

View File

@ -1,77 +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.jpa.test.association;
import javax.persistence.EntityManager;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.junit.Test;
/**
* @author Emmanuel Bernard
*/
public class AssociationTest extends BaseEntityManagerFunctionalTestCase {
@Test
public void testBidirOneToOne() throws Exception {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
String id = "10";
Incident i = em.find( Incident.class, id );
if ( i == null ) {
i = new Incident( id );
IncidentStatus ist = new IncidentStatus( id );
i.setIncidentStatus( ist );
ist.setIncident( i );
em.persist( i );
}
em.getTransaction().commit();
em.close();
em = getOrCreateEntityManager();
em.getTransaction().begin();
em.remove( em.find(Incident.class, id) );
em.getTransaction().commit();
em.close();
}
@Test
public void testMergeAndBidirOneToOne() throws Exception {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
Oven oven = new Oven();
Kitchen kitchen = new Kitchen();
em.persist( oven );
em.persist( kitchen );
kitchen.setOven( oven );
oven.setKitchen( kitchen );
em.getTransaction().commit();
em.close();
em = getOrCreateEntityManager();
em.getTransaction().begin();
oven = em.merge( oven );
em.getTransaction().commit();
em.close();
em = getOrCreateEntityManager();
em.getTransaction().begin();
em.remove( em.find( Oven.class, oven.getId() ) );
em.getTransaction().commit();
em.close();
}
@Override
public Class[] getAnnotatedClasses() {
return new Class[]{
Incident.class,
IncidentStatus.class,
Kitchen.class,
Oven.class
};
}
}

View File

@ -0,0 +1,75 @@
/*
* 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.jpa.association;
import org.hibernate.testing.junit5.EntityManagerFactoryBasedFunctionalTest;
import org.junit.jupiter.api.Test;
/**
* @author Emmanuel Bernard
*/
public class AssociationTest extends EntityManagerFactoryBasedFunctionalTest {
@Test
public void testBidirOneToOne() {
final String id = "10";
inTransaction(
entityManager -> {
Incident i = entityManager.find( Incident.class, id );
if ( i == null ) {
i = new Incident( id );
IncidentStatus ist = new IncidentStatus( id );
i.setIncidentStatus( ist );
ist.setIncident( i );
entityManager.persist( i );
}
} );
inTransaction(
entityManager -> {
entityManager.remove( entityManager.find( Incident.class, id ) );
} );
}
@Test
public void testMergeAndBidirOneToOne() {
final Oven persistedOven = inTransaction(
entityManager -> {
Oven oven = new Oven();
Kitchen kitchen = new Kitchen();
entityManager.persist( oven );
entityManager.persist( kitchen );
kitchen.setOven( oven );
oven.setKitchen( kitchen );
return oven;
} );
Oven mergedOven = inTransaction(
entityManager -> {
return entityManager.merge( persistedOven );
}
);
inTransaction(
entityManager -> {
entityManager.remove( entityManager.find( Oven.class, mergedOven.getId() ) );
} );
}
@Override
public Class[] getAnnotatedClasses() {
return new Class[] {
Incident.class,
IncidentStatus.class,
Kitchen.class,
Oven.class
};
}
}

View File

@ -4,9 +4,8 @@
* 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.jpa.association;
//$Id$
package org.hibernate.jpa.test.association;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
@ -18,10 +17,10 @@ import javax.persistence.OneToOne;
@Entity
public class Incident {
@Id
String id;
private String id;
@OneToOne(cascade = CascadeType.ALL)
IncidentStatus incidentStatus;
private IncidentStatus incidentStatus;
public Incident() {
}

View File

@ -4,9 +4,8 @@
* 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.jpa.association;
//$Id$
package org.hibernate.jpa.test.association;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;

View File

@ -4,9 +4,8 @@
* 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.jpa.association;
//$Id$
package org.hibernate.jpa.test.association;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@ -17,7 +16,8 @@ import javax.persistence.OneToOne;
*/
@Entity
public class Kitchen {
@Id @GeneratedValue
@Id
@GeneratedValue
private Long id;
@OneToOne(mappedBy = "kitchen")

View File

@ -4,9 +4,8 @@
* 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.jpa.association;
//$Id$
package org.hibernate.jpa.test.association;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
@ -19,7 +18,8 @@ import javax.persistence.OneToOne;
*/
@Entity
public class Oven {
@Id @GeneratedValue
@Id
@GeneratedValue
private Long id;
@OneToOne(fetch= FetchType.LAZY)

View File

@ -6,7 +6,7 @@
*/
//$Id$
package org.hibernate.test.annotations.onetoone;
package org.hibernate.orm.test.onetoone;
import org.hibernate.AnnotationException;
import org.hibernate.SessionFactory;
@ -15,15 +15,17 @@ import org.hibernate.cfg.Environment;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.testing.ServiceRegistryBuilder;
import org.hibernate.test.annotations.onetoone.Show;
import org.hibernate.test.annotations.onetoone.ShowDescription;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;
/**
* @author Emmanuel Bernard
*/
public class OneToOneErrorTest {
@Test
public void testWrongOneToOne() throws Exception {
@Test
public void testWrongOneToOne() {
Configuration cfg = new Configuration();
cfg.addAnnotatedClass( Show.class )
.addAnnotatedClass( ShowDescription.class );
@ -33,13 +35,13 @@ public class OneToOneErrorTest {
try {
serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( Environment.getProperties() );
sessionFactory = cfg.buildSessionFactory( serviceRegistry );
Assert.fail( "Wrong mappedBy does not fail property" );
Assert.fail( "Wrong mappedBy does not fail property" );
}
catch (AnnotationException e) {
//success
}
finally {
if(sessionFactory!=null){
if ( sessionFactory != null ) {
sessionFactory.close();
}
if ( serviceRegistry != null ) {

View File

@ -4,7 +4,7 @@
* 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.annotations.onetoone;
package org.hibernate.orm.test.onetoone;
import javax.persistence.CascadeType;
import javax.persistence.Column;
@ -20,11 +20,11 @@ import javax.persistence.Table;
import org.hibernate.PropertyValueException;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import org.hibernate.testing.junit5.SessionFactoryBasedFunctionalTest;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@ -32,7 +32,7 @@ import static org.junit.Assert.fail;
* @author Andrea Boriero
*/
@TestForIssue(jiraKey = "HHH-11596")
public class OneToOneJoinTableNonOptionalTest extends BaseCoreFunctionalTestCase {
public class OneToOneJoinTableNonOptionalTest extends SessionFactoryBasedFunctionalTest {
@Override
protected Class[] getAnnotatedClasses() {

View File

@ -4,7 +4,7 @@
* 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.annotations.onetoone;
package org.hibernate.orm.test.onetoone;
import javax.persistence.CascadeType;
import javax.persistence.Column;
@ -17,12 +17,10 @@ import javax.persistence.JoinTable;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import org.hibernate.testing.junit5.SessionFactoryBasedFunctionalTest;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
@ -30,7 +28,7 @@ import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
* @author Andrea Boriero
*/
@TestForIssue(jiraKey = "HHH-11596")
public class OneToOneJoinTableOptionalTest extends BaseCoreFunctionalTestCase {
public class OneToOneJoinTableOptionalTest extends SessionFactoryBasedFunctionalTest {
@Override
protected Class[] getAnnotatedClasses() {

View File

@ -4,7 +4,7 @@
* 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.annotations.onetoone;
package org.hibernate.orm.test.onetoone;
import javax.persistence.Entity;
import javax.persistence.Id;
@ -17,10 +17,12 @@ import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.persister.entity.AbstractEntityPersister;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit5.EntityManagerFactoryBasedFunctionalTest;
import org.hibernate.testing.junit5.SessionFactoryBasedFunctionalTest;
import org.hibernate.testing.logger.LoggerInspectionRule;
import org.hibernate.testing.logger.Triggerable;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.jboss.logging.Logger;
@ -32,7 +34,7 @@ import static org.junit.Assert.assertTrue;
* @author Vlad Mihalcea
*/
@TestForIssue( jiraKey = "HHH-13228" )
public class OneToOneMapsIdChangeParentTest extends BaseEntityManagerFunctionalTestCase {
public class OneToOneMapsIdChangeParentTest extends EntityManagerFactoryBasedFunctionalTest {
@Rule
public LoggerInspectionRule logInspection = new LoggerInspectionRule(

View File

@ -4,7 +4,7 @@
* 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.annotations.onetoone;
package org.hibernate.orm.test.onetoone;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
@ -14,17 +14,17 @@ import javax.persistence.JoinColumn;
import javax.persistence.MapsId;
import javax.persistence.OneToOne;
import org.hibernate.annotations.NaturalId;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.junit.Test;
import org.hibernate.testing.junit5.EntityManagerFactoryBasedFunctionalTest;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
/**
* @author Vlad Mihalcea
*/
public class OneToOneMapsIdJoinColumnTest extends BaseEntityManagerFunctionalTestCase {
public class OneToOneMapsIdJoinColumnTest extends EntityManagerFactoryBasedFunctionalTest {
@Override
protected Class<?>[] getAnnotatedClasses() {

View File

@ -4,7 +4,7 @@
* 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.annotations.onetoone;
package org.hibernate.orm.test.onetoone;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
@ -13,17 +13,17 @@ import javax.persistence.MapsId;
import javax.persistence.OneToOne;
import org.hibernate.annotations.NaturalId;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.junit.Test;
import org.hibernate.testing.junit5.EntityManagerFactoryBasedFunctionalTest;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals;
/**
* @author Vlad Mihalcea
*/
public class OneToOneMapsIdTest extends BaseEntityManagerFunctionalTestCase {
public class OneToOneMapsIdTest extends EntityManagerFactoryBasedFunctionalTest {
@Override
protected Class<?>[] getAnnotatedClasses() {

View File

@ -4,7 +4,7 @@
* 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.annotations.onetoone.hhh9798;
package org.hibernate.orm.test.onetoone.hhh9798;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;

View File

@ -0,0 +1,76 @@
/*
* 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.onetoone.hhh9798;
import javax.persistence.PersistenceException;
import org.hibernate.Transaction;
import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit5.SessionFactoryBasedFunctionalTest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.fail;
@TestForIssue(jiraKey = "HHH-9798")
public class OneToOneJoinTableTest extends SessionFactoryBasedFunctionalTest {
@Test
public void storeNonUniqueRelationship() {
inSession(
session -> {
try {
Transaction tx = session.beginTransaction();
Item someItem = new Item( "Some Item" );
session.save( someItem );
Shipment shipment1 = new Shipment( someItem );
session.save( shipment1 );
Shipment shipment2 = new Shipment( someItem );
session.save( shipment2 );
tx.commit();
fail();
}
catch (PersistenceException e) {
assertTyping( ConstraintViolationException.class, e.getCause() );
// expected
}
finally {
if ( session != null ) {
session.getTransaction().rollback();
session.close();
}
}
}
);
}
@AfterEach
public void cleanUpData() {
inTransaction(
session -> {
session.createQuery( "delete Shipment" ).executeUpdate();
session.createQuery( "delete Item" ).executeUpdate();
}
);
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Shipment.class,
Item.class
};
}
}

View File

@ -4,8 +4,9 @@
* 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.annotations.onetoone.hhh9798;
package org.hibernate.orm.test.onetoone.hhh9798;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
@ -15,7 +16,6 @@ import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToOne;
import javax.validation.constraints.NotNull;
import java.util.Date;
@Entity
public class Shipment {

View File

@ -4,7 +4,7 @@
* 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.annotations.onetoone.hhh9798;
package org.hibernate.orm.test.onetoone.hhh9798;
public enum ShipmentState {

View File

@ -6,7 +6,7 @@
*/
//$Id: A320.java 14736 2008-06-04 14:23:42Z hardy.ferentschik $
package org.hibernate.test.annotations.onetoone.primarykey;
package org.hibernate.orm.test.onetoone.primarykey;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;

View File

@ -6,7 +6,7 @@
*/
//$Id: A320.java 14736 2008-06-04 14:23:42Z hardy.ferentschik $
package org.hibernate.test.annotations.onetoone.primarykey;
package org.hibernate.orm.test.onetoone.primarykey;
import java.util.HashMap;
import java.util.List;
@ -21,11 +21,9 @@ import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.schema.internal.SchemaCreatorImpl;
import org.hibernate.testing.ServiceRegistryBuilder;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.hibernate.testing.junit5.BaseUnitTest;
import org.junit.Assert;
import org.junit.Test;
import org.jboss.logging.Logger;
import org.junit.jupiter.api.Test;
/**
* Test harness for ANN-742.
@ -33,10 +31,9 @@ import org.jboss.logging.Logger;
* @author Hardy Ferentschik
*
*/
public class NullablePrimaryKeyTest extends BaseUnitTestCase {
public class NullablePrimaryKeyTest extends BaseUnitTest {
@Test
@SuppressWarnings("unchecked")
public void testGeneratedSql() {
Map settings = new HashMap();
@ -47,14 +44,11 @@ public class NullablePrimaryKeyTest extends BaseUnitTestCase {
try {
MetadataSources ms = new MetadataSources( serviceRegistry );
ms.addAnnotatedClass(Address.class);
ms.addAnnotatedClass(Person.class);
ms.addAnnotatedClass( Address.class);
ms.addAnnotatedClass( Person.class);
final Metadata metadata = ms.buildMetadata();
final List<String> commands = new SchemaCreatorImpl( serviceRegistry ).generateCreationCommands( metadata, false );
for (String s : commands) {
log.debug( s );
}
String expectedMappingTableSql = "create table personAddress (address_id numeric(19,0), " +
"person_id numeric(19,0) not null, primary key (person_id))";

View File

@ -6,7 +6,7 @@
*/
//$Id: A320.java 14736 2008-06-04 14:23:42Z hardy.ferentschik $
package org.hibernate.test.annotations.onetoone.primarykey;
package org.hibernate.orm.test.onetoone.primarykey;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;

View File

@ -1,76 +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.annotations.onetoone.hhh9798;
import javax.persistence.PersistenceException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.id.IdentifierGenerationException;
import org.junit.Test;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.fail;
@TestForIssue(jiraKey = "HHH-9798")
public class OneToOneJoinTableTest extends BaseCoreFunctionalTestCase {
@Test
public void storeNonUniqueRelationship() throws Throwable {
Session session = null;
try {
session = openSession();
Transaction tx = session.beginTransaction();
Item someItem = new Item( "Some Item" );
session.save( someItem );
Shipment shipment1 = new Shipment( someItem );
session.save( shipment1 );
Shipment shipment2 = new Shipment( someItem );
session.save( shipment2 );
tx.commit();
fail();
}catch (PersistenceException e){
assertTyping( ConstraintViolationException.class, e.getCause());
// expected
}
finally {
if ( session != null ) {
session.getTransaction().rollback();
session.close();
}
cleanUpData();
}
}
private void cleanUpData() {
Session session = openSession();
session.beginTransaction();
session.createQuery( "delete Shipment" ).executeUpdate();
session.createQuery( "delete Item" ).executeUpdate();
session.getTransaction().commit();
session.close();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Shipment.class,
Item.class
};
}
}