HHH-10667 - Added test case.

(cherry picked from commit 1ae930e)
This commit is contained in:
yinzara 2016-04-01 22:47:41 -04:00 committed by Chris Cranford
parent 8510b6c0ef
commit fbf93d0962
4 changed files with 366 additions and 0 deletions

View File

@ -0,0 +1,108 @@
/*
* 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.envers.test.integration.ids.idclass;
import junit.framework.Assert;
import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase;
import org.hibernate.envers.test.Priority;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import javax.persistence.EntityManager;
import java.util.Arrays;
/**
* @author Matthew Morrissette (yinzara at gmail dot com)
*/
@TestForIssue(jiraKey = "HHH-10667")
public class IdClassReferenceIdentifierTest extends BaseEnversJPAFunctionalTestCase {
private ReferenceIdentifierClassId entityId = null;
private Integer typeId = null;
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {ReferenceIdentifierEntity.class, ReferenceIdentifierClassId.class, ClassType.class, IntegerGeneratedIdentityEntity.class};
}
@Test
@Priority(10)
public void initData() {
EntityManager em = getEntityManager();
// Revision 1
em.getTransaction().begin();
ClassType type = new ClassType( "type", "initial description" );
em.persist( type );
IntegerGeneratedIdentityEntity type2 = new IntegerGeneratedIdentityEntity();
em.persist(type2);
ReferenceIdentifierEntity entity = new ReferenceIdentifierEntity();
entity.setSampleValue( "initial data" );
entity.setType( type );
entity.setIiie( type2 );
em.persist( entity );
em.getTransaction().commit();
typeId = type2.getId();
entityId = new ReferenceIdentifierClassId( typeId, type.getType() );
// Revision 2
em.getTransaction().begin();
type = em.find( ClassType.class, type.getType() );
type.setDescription( "modified description" );
em.merge( type );
em.getTransaction().commit();
// Revision 3
em.getTransaction().begin();
entity = em.find( ReferenceIdentifierEntity.class, entityId );
entity.setSampleValue( "modified data" );
em.merge( entity );
em.getTransaction().commit();
em.close();
}
@Test
public void testRevisionsCounts() {
Assert.assertEquals( Arrays.asList( 1, 2 ), getAuditReader().getRevisions( ClassType.class, "type" ) );
Assert.assertEquals( Arrays.asList( 1 ), getAuditReader().getRevisions( IntegerGeneratedIdentityEntity.class, typeId ) );
Assert.assertEquals( Arrays.asList( 1, 3 ), getAuditReader().getRevisions( ReferenceIdentifierEntity.class, entityId ) );
}
@Test
public void testHistoryOfEntity() {
// given
ReferenceIdentifierEntity entity = new ReferenceIdentifierEntity( new IntegerGeneratedIdentityEntity(typeId), new ClassType( "type", "initial description" ), "initial data" );
// when
ReferenceIdentifierEntity ver1 = getAuditReader().find( ReferenceIdentifierEntity.class, entityId, 1 );
// then
Assert.assertEquals( entity.getIiie().getId(), ver1.getIiie().getId() );
Assert.assertEquals( entity.getSampleValue(), ver1.getSampleValue() );
Assert.assertEquals( entity.getType().getType(), ver1.getType().getType() );
Assert.assertEquals( entity.getType().getDescription(), ver1.getType().getDescription() );
// given
entity.setSampleValue( "modified data" );
entity.getType().setDescription( "modified description" );
// when
ReferenceIdentifierEntity ver2 = getAuditReader().find( ReferenceIdentifierEntity.class, entityId, 3 );
// then
Assert.assertEquals( entity.getIiie().getId(), ver2.getIiie().getId() );
Assert.assertEquals( entity.getSampleValue(), ver2.getSampleValue() );
Assert.assertEquals( entity.getType().getType(), ver2.getType().getType() );
Assert.assertEquals( entity.getType().getDescription(), ver2.getType().getDescription() );
}
}

View File

@ -0,0 +1,82 @@
/*
* 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.envers.test.integration.ids.idclass;
import org.hibernate.envers.Audited;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.io.Serializable;
/**
* @author Matthew Morrissette (yinzara at gmail dot com)
*/
@Audited
@Entity
public class IntegerGeneratedIdentityEntity implements Serializable {
@Id
@GeneratedValue
private Integer id;
private String description;
public IntegerGeneratedIdentityEntity() {
}
public IntegerGeneratedIdentityEntity(Integer id) {
this.id = id;
}
public IntegerGeneratedIdentityEntity(String description) {
this.description = description;
}
public IntegerGeneratedIdentityEntity(Integer id, String description) {
this.id = id;
this.description = description;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || !(o instanceof IntegerGeneratedIdentityEntity)) return false;
IntegerGeneratedIdentityEntity that = (IntegerGeneratedIdentityEntity) o;
return id != null ? id.equals(that.id) : that.id == null;
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
@Override
public String toString() {
return "IntegerGeneratedIdentityEntity(id = " + id + ", description = " + description + ")";
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

View File

@ -0,0 +1,68 @@
package org.hibernate.envers.test.integration.ids.idclass;
import java.io.Serializable;
/**
* Created by yinzara on 4/1/16.
*/
public class ReferenceIdentifierClassId implements Serializable {
private Integer iiie;
private String type;
public ReferenceIdentifierClassId() {
}
public ReferenceIdentifierClassId(Integer iiie, String type) {
this.iiie = iiie;
this.type = type;
}
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( !(o instanceof ReferenceIdentifierClassId) ) {
return false;
}
ReferenceIdentifierClassId that = (ReferenceIdentifierClassId) o;
if ( iiie != null ? !iiie.equals( that.iiie) : that.iiie != null ) {
return false;
}
if ( type != null ? !type.equals( that.type ) : that.type != null ) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = iiie != null ? iiie.hashCode() : 0;
result = 31 * result + (type != null ? type.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "ReferenceIdentifierClassId(iiie = " + iiie + ", type = " + type + ")";
}
public Integer getIiie() {
return iiie;
}
public void setIiie(Integer iiie) {
this.iiie = iiie;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}

View File

@ -0,0 +1,108 @@
/*
* 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.envers.test.integration.ids.idclass;
import org.hibernate.envers.Audited;
import javax.persistence.*;
import java.io.Serializable;
/**
* @author Matthew Morrissette (yinzara at gmail dot com)
*/
@Audited
@Entity
@IdClass(ReferenceIdentifierClassId.class)
public class ReferenceIdentifierEntity implements Serializable {
@Id
@JoinColumn(name="ClassTypeId", nullable=false)
@GeneratedValue
@ManyToOne(fetch = FetchType.LAZY, optional = false)
private IntegerGeneratedIdentityEntity iiie;
@Id
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "ClassTypeName")
private ClassType type;
private String sampleValue;
public ReferenceIdentifierEntity() {
}
public ReferenceIdentifierEntity(ClassType type) {
this.type = type;
}
public ReferenceIdentifierEntity(IntegerGeneratedIdentityEntity iiie, ClassType type) {
this.iiie = iiie;
this.type = type;
}
public ReferenceIdentifierEntity(IntegerGeneratedIdentityEntity iiie, ClassType type, String sampleValue) {
this.iiie = iiie;
this.type = type;
this.sampleValue = sampleValue;
}
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( !(o instanceof ReferenceIdentifierEntity) ) {
return false;
}
ReferenceIdentifierEntity sampleClass = (ReferenceIdentifierEntity) o;
if ( iiie != null ? !iiie.equals( sampleClass.iiie) : sampleClass.iiie != null ) {
return false;
}
if ( type != null ? !type.equals( sampleClass.type ) : sampleClass.type != null ) {
return false;
}
if ( sampleValue != null ? !sampleValue.equals( sampleClass.sampleValue ) : sampleClass.sampleValue != null ) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = iiie != null ? iiie.hashCode() : 0;
result = 31 * result + (type != null ? type.hashCode() : 0);
result = 31 * result + (sampleValue != null ? sampleValue.hashCode() : 0);
return result;
}
public IntegerGeneratedIdentityEntity getIiie() {
return iiie;
}
public void setIiie(IntegerGeneratedIdentityEntity iiie) {
this.iiie = iiie;
}
public ClassType getType() {
return type;
}
public void setType(ClassType type) {
this.type = type;
}
public String getSampleValue() {
return sampleValue;
}
public void setSampleValue(String sampleValue) {
this.sampleValue = sampleValue;
}
}