From 6f8968e58a00d9552d6e771909824f984b9589c8 Mon Sep 17 00:00:00 2001 From: Chris Cranford Date: Tue, 12 Sep 2017 23:38:12 -0400 Subject: [PATCH] HHH-11841 - Added test case. --- .../EntityMapCompositeElementTest.java | 239 ++++++++++++++++++ .../mappings/collections/Category.hbm.xml | 35 +++ .../mappings/collections/Item.hbm.xml | 18 ++ 3 files changed, 292 insertions(+) create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/collection/EntityMapCompositeElementTest.java create mode 100644 hibernate-envers/src/test/resources/mappings/collections/Category.hbm.xml create mode 100644 hibernate-envers/src/test/resources/mappings/collections/Item.hbm.xml diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/collection/EntityMapCompositeElementTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/collection/EntityMapCompositeElementTest.java new file mode 100644 index 000000000000..63df29f95f15 --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/collection/EntityMapCompositeElementTest.java @@ -0,0 +1,239 @@ +/* + * 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 . + */ +package org.hibernate.envers.test.integration.collection; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.hibernate.envers.AuditReader; +import org.hibernate.envers.Audited; +import org.hibernate.envers.query.AuditEntity; +import org.hibernate.envers.query.AuditQuery; +import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase; +import org.hibernate.envers.test.Priority; +import org.junit.Test; + +import org.hibernate.testing.transaction.TransactionUtil; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * @author Chris Cranford + */ +public class EntityMapCompositeElementTest extends BaseEnversJPAFunctionalTestCase { + + private Category category; + private Item item; + + @Test + @Priority(10) + public void initData() { + TransactionUtil.doInJPA( this::entityManagerFactory, entityManager-> { + final Item item = new Item( "The Item" ); + entityManager.persist( item ); + + final Category category = new Category( "The Category" ); + category.setDescription( "The description" ); + category.setValue( item, new Value( "The Value", 4711L ) ); + category.setText( item, "The text" ); + entityManager.persist( category ); + + this.category = category; + this.item = item; + } ); + } + + @Test + public void testRevisionHistory() { + final AuditReader reader = getAuditReader(); + + AuditQuery categoryQuery = reader.createQuery().forRevisionsOfEntity( Category.class, false, true ) + .addOrder( AuditEntity.revisionProperty( "timestamp" ).asc() ) + .add( AuditEntity.id().eq( category.getId() ) ); + + @SuppressWarnings( "unchecked" ) + List history = (List) categoryQuery.getResultList(); + assertNotNull( history ); + assertEquals( 1, history.size() ); + + final Category category = (Category) reader.createQuery().forEntitiesAtRevision( Category.class, 1 ) + .add( AuditEntity.property( "id" ).eq( this.category.getId() ) ) + .setMaxResults( 1 ) + .getSingleResult(); + + assertEquals( this.category.getName(), category.getName() ); + assertEquals( this.category.getDescription(), category.getDescription() ); + assertEquals( "The text", category.getText( this.item ) ); + + final Value value = category.getValue( this.item ); + assertEquals( "The Value", value.getText() ); + assertEquals( Long.valueOf( 4711L ), value.getNumber() ); + } + + @Override + protected String[] getMappings() { + return new String[] { + "mappings/collections/Category.hbm.xml", + "mappings/collections/Item.hbm.xml" + }; + } + + @Audited + public static class Category { + private Long id; + private String name; + private String description; + private Map textItem = new HashMap<>(); + private Map categoryItem = new HashMap<>(); + + Category() { + + } + + Category(String name) { + this.name = name; + } + + 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; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Map getTextItem() { + return textItem; + } + + public void setTextItem(Map textItem) { + this.textItem = textItem; + } + + public Map getCategoryItem() { + return categoryItem; + } + + public void setCategoryItem(Map categoryItem) { + this.categoryItem = categoryItem; + } + + public void setValue(Item key, Value value) { + this.categoryItem.put( key, value ); + } + + public Value getValue(Item key) { + return this.categoryItem.get( key ); + } + + public void setText(Item key, String value) { + this.textItem.put( key, value ); + } + + public String getText(Item key) { + return this.textItem.get( key ); + } + } + + @Audited + public static class Item { + private Long id; + private String name; + + Item() { + + } + + Item(String name) { + this.name = name; + } + + 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; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + + Item item = (Item) o; + + return id != null ? id.equals( item.id ) : item.id == null; + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } + } + + public static class Value implements Serializable { + private String text; + private Long number; + + Value() { + + } + + Value(String text, Long number) { + this.text = text; + this.number = number; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public Long getNumber() { + return number; + } + + public void setNumber(Long number) { + this.number = number; + } + } +} diff --git a/hibernate-envers/src/test/resources/mappings/collections/Category.hbm.xml b/hibernate-envers/src/test/resources/mappings/collections/Category.hbm.xml new file mode 100644 index 000000000000..607f703cb312 --- /dev/null +++ b/hibernate-envers/src/test/resources/mappings/collections/Category.hbm.xml @@ -0,0 +1,35 @@ + + + + + + + + category_id_seq + + + + + + + + + + + + + + + + + + + + + + diff --git a/hibernate-envers/src/test/resources/mappings/collections/Item.hbm.xml b/hibernate-envers/src/test/resources/mappings/collections/Item.hbm.xml new file mode 100644 index 000000000000..53975b4008c0 --- /dev/null +++ b/hibernate-envers/src/test/resources/mappings/collections/Item.hbm.xml @@ -0,0 +1,18 @@ + + + + + + + + item_id_seq + + + + + \ No newline at end of file