From 23c1a476678803f881f2a73d65af0f33c8795511 Mon Sep 17 00:00:00 2001 From: Emmanuel Bernard Date: Mon, 21 Dec 2009 15:49:55 +0000 Subject: [PATCH] HHH-4665 add tests for getIdentifier() git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18302 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../org/hibernate/ejb/test/util/Book.java | 32 +++++++ .../ejb/test/util/GetIdentifierTest.java | 62 ++++++++++++ .../org/hibernate/ejb/test/util/Sickness.java | 96 +++++++++++++++++++ .../org/hibernate/ejb/test/util/Umbrella.java | 81 ++++++++++++++++ 4 files changed, 271 insertions(+) create mode 100644 entitymanager/src/test/java/org/hibernate/ejb/test/util/Book.java create mode 100644 entitymanager/src/test/java/org/hibernate/ejb/test/util/GetIdentifierTest.java create mode 100644 entitymanager/src/test/java/org/hibernate/ejb/test/util/Sickness.java create mode 100644 entitymanager/src/test/java/org/hibernate/ejb/test/util/Umbrella.java diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/util/Book.java b/entitymanager/src/test/java/org/hibernate/ejb/test/util/Book.java new file mode 100644 index 0000000000..c93810f255 --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/test/util/Book.java @@ -0,0 +1,32 @@ +package org.hibernate.ejb.test.util; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.GeneratedValue; + +/** + * @author Emmanuel Bernard + */ +@Entity +public class Book { + private Long id; + private String name; + + @Id + @GeneratedValue + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/util/GetIdentifierTest.java b/entitymanager/src/test/java/org/hibernate/ejb/test/util/GetIdentifierTest.java new file mode 100644 index 0000000000..ee4def555e --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/test/util/GetIdentifierTest.java @@ -0,0 +1,62 @@ +package org.hibernate.ejb.test.util; + +import javax.persistence.EntityManager; +import javax.persistence.Persistence; + +import org.hibernate.ejb.test.TestCase; + +/** + * @author Emmanuel Bernard + */ +public class GetIdentifierTest extends TestCase { + + public void testSimpleId() { + EntityManager em = factory.createEntityManager(); + em.getTransaction().begin(); + Book book = new Book(); + em.persist( book ); + em.flush(); + assertEquals( book.getId(), em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier( book ) ); + em.getTransaction().rollback(); + em.close(); + } + + public void testEmbeddedId() { + EntityManager em = factory.createEntityManager(); + em.getTransaction().begin(); + Umbrella umbrella = new Umbrella(); + umbrella.setId( new Umbrella.PK() ); + umbrella.getId().setBrand( "Burberry" ); + umbrella.getId().setModel( "Red Hat" ); + em.persist( umbrella ); + em.flush(); + assertEquals( umbrella.getId(), em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier( umbrella ) ); + em.getTransaction().rollback(); + em.close(); + } + + public void testIdClass() { + EntityManager em = factory.createEntityManager(); + em.getTransaction().begin(); + Sickness sick = new Sickness(); + + sick.setClassification( "H1N1" ); + sick.setType("Flu"); + em.persist( sick ); + em.flush(); + Sickness.PK id = new Sickness.PK(); + id.setClassification( sick.getClassification() ); + id.setType( sick.getType() ); + assertEquals( id, em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier( sick ) ); + em.getTransaction().rollback(); + em.close(); + } + + public Class[] getAnnotatedClasses() { + return new Class[] { + Book.class, + Umbrella.class, + Sickness.class + }; + } +} diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/util/Sickness.java b/entitymanager/src/test/java/org/hibernate/ejb/test/util/Sickness.java new file mode 100644 index 0000000000..3b625af03e --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/test/util/Sickness.java @@ -0,0 +1,96 @@ +package org.hibernate.ejb.test.util; + +import java.util.Date; +import java.io.Serializable; +import javax.persistence.Entity; +import javax.persistence.Temporal; +import javax.persistence.Id; +import javax.persistence.IdClass; + +/** + * @author Emmanuel Bernard + */ +@Entity +@IdClass(Sickness.PK.class) +public class Sickness { + private Date beginTime; + private String type; + private String classification; + + @Id + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + @Id + public String getClassification() { + return classification; + } + + public void setClassification(String classification) { + this.classification = classification; + } + + + @Temporal(javax.persistence.TemporalType.DATE) + public Date getBeginTime() { + return beginTime; + } + + public void setBeginTime(Date beginTime) { + this.beginTime = beginTime; + } + + public static class PK implements Serializable { + private String type; + private String classification; + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getClassification() { + return classification; + } + + public void setClassification(String classification) { + this.classification = classification; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + + PK pk = ( PK ) o; + + if ( classification != null ? !classification.equals( pk.classification ) : pk.classification != null ) { + return false; + } + if ( type != null ? !type.equals( pk.type ) : pk.type != null ) { + return false; + } + + return true; + } + + @Override + public int hashCode() { + int result = type != null ? type.hashCode() : 0; + result = 31 * result + ( classification != null ? classification.hashCode() : 0 ); + return result; + } + } +} diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/util/Umbrella.java b/entitymanager/src/test/java/org/hibernate/ejb/test/util/Umbrella.java new file mode 100644 index 0000000000..9216e65bbf --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/test/util/Umbrella.java @@ -0,0 +1,81 @@ +package org.hibernate.ejb.test.util; + +import java.io.Serializable; +import javax.persistence.Entity; +import javax.persistence.EmbeddedId; + +/** + * @author Emmanuel Bernard + */ +@Entity +public class Umbrella { + private PK id; + + private int size; + + @EmbeddedId + public PK getId() { + return id; + } + + public void setId(PK id) { + this.id = id; + } + + public int getSize() { + return size; + } + + public void setSize(int size) { + this.size = size; + } + + public static class PK implements Serializable { + private String model; + private String brand; + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String getBrand() { + return brand; + } + + public void setBrand(String brand) { + this.brand = brand; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + + PK pk = ( PK ) o; + + if ( brand != null ? !brand.equals( pk.brand ) : pk.brand != null ) { + return false; + } + if ( model != null ? !model.equals( pk.model ) : pk.model != null ) { + return false; + } + + return true; + } + + @Override + public int hashCode() { + int result = model != null ? model.hashCode() : 0; + result = 31 * result + ( brand != null ? brand.hashCode() : 0 ); + return result; + } + } +}