HHH-6374 - Add test case for EnumMap support.

This commit is contained in:
Naros 2016-05-05 20:34:01 -05:00 committed by Chris Cranford
parent 2b66cd72bb
commit b775dd1847
3 changed files with 181 additions and 0 deletions

View File

@ -0,0 +1,53 @@
/*
* 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.entities.collection;
import java.util.EnumMap;
import java.util.Map;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import org.hibernate.envers.Audited;
/**
* @author Chris Cranford
*/
@Entity
@Audited
public class EnumMapEntity {
@Id
@GeneratedValue
private Integer id;
@OneToMany(cascade = CascadeType.ALL)
private Map<EnumType, EnumMapType> types = new EnumMap<>(EnumType.class);
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Map<EnumType, EnumMapType> getTypes() {
return types;
}
public void setTypes(Map<EnumType, EnumMapType> types) {
this.types = types;
}
public enum EnumType {
TYPE_A,
TYPE_B,
TYPE_C
}
}

View File

@ -0,0 +1,49 @@
/*
* 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.entities.collection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.envers.Audited;
/**
* @author Chris Cranford
*/
@Entity
@Audited
public class EnumMapType {
@Id
@GeneratedValue
private Integer id;
private String type;
EnumMapType() {
}
public EnumMapType(String type) {
this.type = type;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}

View File

@ -0,0 +1,79 @@
/*
* 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.collection;
import java.util.Arrays;
import javax.persistence.EntityManager;
import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase;
import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.entities.collection.EnumMapEntity;
import org.hibernate.envers.test.entities.collection.EnumMapType;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author Chris Cranford
*/
@TestForIssue(jiraKey = "HHH-6374")
public class EnumMapTest extends BaseEnversJPAFunctionalTestCase {
private Integer entityId;
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
EnumMapEntity.class,
EnumMapType.class
};
}
@Test
@Priority(10)
public void initData() {
EntityManager em = getOrCreateEntityManager();
try {
// revision 1
EnumMapEntity entity = new EnumMapEntity();
entity.getTypes().put( EnumMapEntity.EnumType.TYPE_A, new EnumMapType( "A" ) );
entity.getTypes().put( EnumMapEntity.EnumType.TYPE_B, new EnumMapType( "B" ) );
em.getTransaction().begin();
em.persist( entity );
em.getTransaction().commit();
// revision 2
em.getTransaction().begin();
entity = em.find( EnumMapEntity.class, entity.getId() );
entity.getTypes().remove( EnumMapEntity.EnumType.TYPE_A );
entity.getTypes().put( EnumMapEntity.EnumType.TYPE_C, new EnumMapType( "C" ) );
em.getTransaction().commit();
entityId = entity.getId();
}
finally {
em.close();
}
}
@Test
public void testRevisionsCount() {
assertEquals( 2, getAuditReader().getRevisions( EnumMapEntity.class, entityId ).size() );
}
@Test
public void testAuditEnumMapCollection() {
EnumMapEntity rev1 = getAuditReader().find( EnumMapEntity.class, entityId, 1 );
assertTrue( rev1.getTypes().keySet().containsAll(
Arrays.asList( EnumMapEntity.EnumType.TYPE_A, EnumMapEntity.EnumType.TYPE_B )
) );
EnumMapEntity rev2 = getAuditReader().find( EnumMapEntity.class, entityId, 2 );
assertTrue( rev2.getTypes().keySet().containsAll(
Arrays.asList( EnumMapEntity.EnumType.TYPE_B, EnumMapEntity.EnumType.TYPE_C )
) );
}
}