Author: epbernard
Date: 2009-12-21 10:49:55 -0500 (Mon, 21 Dec 2009)
New Revision: 18302
Added:
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/util/
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/util/Book.java
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/util/GetIdentifierTest.java
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/util/Sickness.java
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/util/Umbrella.java
Log:
HHH-4665 add tests for getIdentifier()
Added: core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/util/Book.java
===================================================================
--- core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/util/Book.java
(rev 0)
+++
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/util/Book.java 2009-12-21
15:49:55 UTC (rev 18302)
@@ -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;
+ }
+}
Added:
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/util/GetIdentifierTest.java
===================================================================
---
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/util/GetIdentifierTest.java
(rev 0)
+++
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/util/GetIdentifierTest.java 2009-12-21
15:49:55 UTC (rev 18302)
@@ -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
+ };
+ }
+}
Added: core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/util/Sickness.java
===================================================================
--- core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/util/Sickness.java
(rev 0)
+++
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/util/Sickness.java 2009-12-21
15:49:55 UTC (rev 18302)
@@ -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
+(a)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;
+ }
+ }
+}
Added: core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/util/Umbrella.java
===================================================================
--- core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/util/Umbrella.java
(rev 0)
+++
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/util/Umbrella.java 2009-12-21
15:49:55 UTC (rev 18302)
@@ -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;
+ }
+ }
+}
Show replies by date