HHH-8505: Advanced test - OneToOne & ManyToOne

This commit is contained in:
zuchos 2013-09-30 11:49:49 +02:00 committed by adamw
parent 0aefe3a55b
commit 7c492ddbc3
7 changed files with 318 additions and 1 deletions

View File

@ -443,7 +443,7 @@ public class AuditedPropertiesReader {
final boolean isAudited = fillPropertyData(property, componentData, accessType, allClassAudited);
final PersistentPropertiesSource componentPropertiesSource;
if (propertyValue.isDynamic() && isAudited) {
if (propertyValue.isDynamic()) {
componentPropertiesSource = new DynamicComponentSource(reflectionManager, propertyValue, property);
} else {
componentPropertiesSource = new ComponentPropertiesSource(reflectionManager, propertyValue);

View File

@ -0,0 +1,72 @@
package org.hibernate.envers.test.integration.components.dynamic;
import org.hibernate.envers.Audited;
import java.util.HashMap;
import java.util.Map;
@Audited
public class AdvancedEntity {
private Long id;
private String note;
private Map<String,Object> dynamicConfiguration = new HashMap<String, Object>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public Map<String, Object> getDynamicConfiguration() {
return dynamicConfiguration;
}
public void setDynamicConfiguration(Map<String, Object> dynamicConfiguration) {
this.dynamicConfiguration = dynamicConfiguration;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof AdvancedEntity)) return false;
AdvancedEntity that = (AdvancedEntity) o;
if (dynamicConfiguration != null ? !dynamicConfiguration.equals(that.dynamicConfiguration) : that.dynamicConfiguration != null)
return false;
if (id != null ? !id.equals(that.id) : that.id != null) return false;
if (note != null ? !note.equals(that.note) : that.note != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (note != null ? note.hashCode() : 0);
result = 31 * result + (dynamicConfiguration != null ? dynamicConfiguration.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "AdvancedEntity{" +
"id=" + id +
", note='" + note + '\'' +
", dynamicConfiguration=" + dynamicConfiguration +
'}';
}
}

View File

@ -0,0 +1,83 @@
package org.hibernate.envers.test.integration.components.dynamic;
import junit.framework.Assert;
import org.hibernate.Session;
import org.hibernate.envers.test.BaseEnversFunctionalTestCase;
import org.hibernate.envers.test.Priority;
import org.junit.Test;
public class AuditedDynamicComponentsAdvancedCasesTest extends BaseEnversFunctionalTestCase {
public static final String PROP_BOOLEAN = "propBoolean";
public static final String PROP_INT = "propInt";
public static final String PROP_FLOAT = "propFloat";
public static final String PROP_MANY_TO_ONE = "propManyToOne";
public static final String PROP_ONE_TO_ONE = "propOneToOne";
@Override
protected String[] getMappings() {
return new String[]{"mappings/dynamicComponents/mapAdvanced.hbm.xml"};
}
@Test
@Priority(10)
//smoke test to make sure that hibernate & envers are working with the entity&mappings
public void shouldSaveEntity() {
//given
ManyToOneEntity manyToOne = getManyToOneEntity();
OneToOneEntity oneToOne = getOneToOneEntity();
AdvancedEntity advancedEntity = getAdvancedEntity(manyToOne, oneToOne);
Session session = openSession();
session.getTransaction().begin();
session.save(manyToOne);
session.save(oneToOne);
session.save(advancedEntity);
session.getTransaction().commit();
AdvancedEntity advancedEntityActual = (AdvancedEntity) session.load(AdvancedEntity.class, 1L);
Assert.assertEquals(advancedEntity, advancedEntityActual);
}
private AdvancedEntity getAdvancedEntity(ManyToOneEntity manyToOne, OneToOneEntity oneToOne) {
AdvancedEntity advancedEntity = new AdvancedEntity();
advancedEntity.setId(1L);
advancedEntity.setNote("Test note");
advancedEntity.getDynamicConfiguration().put(PROP_BOOLEAN, true);
advancedEntity.getDynamicConfiguration().put(PROP_INT, 19);
advancedEntity.getDynamicConfiguration().put(PROP_FLOAT, 15.9f);
advancedEntity.getDynamicConfiguration().put(PROP_MANY_TO_ONE, manyToOne);
advancedEntity.getDynamicConfiguration().put(PROP_ONE_TO_ONE, oneToOne);
return advancedEntity;
}
@Test
public void shouldMakeFirstRevision() {
Session session = openSession();
//given & when shouldSaveEntity
ManyToOneEntity manyToOne = getManyToOneEntity();
OneToOneEntity oneToOne = getOneToOneEntity();
AdvancedEntity advancedEntity = getAdvancedEntity(manyToOne, oneToOne);
//then
session.getTransaction().begin();
AdvancedEntity ver1 = getAuditReader().find(
AdvancedEntity.class,
advancedEntity.getId(),
1
);
Assert.assertEquals(advancedEntity, ver1);
session.getTransaction().commit();
}
private OneToOneEntity getOneToOneEntity() {
return new OneToOneEntity(1L, "OneToOne");
}
private ManyToOneEntity getManyToOneEntity() {
return new ManyToOneEntity(1L, "ManyToOne");
}
}

View File

@ -0,0 +1,62 @@
package org.hibernate.envers.test.integration.components.dynamic;
import org.hibernate.envers.Audited;
@Audited
public class ManyToOneEntity {
private Long id;
private String note;
public ManyToOneEntity() {
}
public ManyToOneEntity(Long id, String note) {
this.id = id;
this.note = note;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ManyToOneEntity)) return false;
ManyToOneEntity that = (ManyToOneEntity) o;
if (id != null ? !id.equals(that.id) : that.id != null) return false;
if (note != null ? !note.equals(that.note) : that.note != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (note != null ? note.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "ManyToOneEntity{" +
"id=" + id +
", note='" + note + '\'' +
'}';
}
}

View File

@ -0,0 +1,62 @@
package org.hibernate.envers.test.integration.components.dynamic;
import org.hibernate.envers.Audited;
@Audited
public class OneToOneEntity {
private Long id;
private String note;
public OneToOneEntity() {
}
public OneToOneEntity(Long id, String note) {
this.id = id;
this.note = note;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof OneToOneEntity)) return false;
OneToOneEntity that = (OneToOneEntity) o;
if (id != null ? !id.equals(that.id) : that.id != null) return false;
if (note != null ? !note.equals(that.note) : that.note != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (note != null ? note.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "OneToOneEntity{" +
"id=" + id +
", note='" + note + '\'' +
'}';
}
}

View File

@ -8,6 +8,8 @@ public class SimpleEntity {
private Long id;
private String simpleProperty;
private AdvancedEntity parent;
public SimpleEntity() {
}
@ -60,4 +62,12 @@ public class SimpleEntity {
", simpleProperty='" + simpleProperty + '\'' +
'}';
}
public AdvancedEntity getParent() {
return parent;
}
public void setParent(AdvancedEntity parent) {
this.parent = parent;
}
}

View File

@ -0,0 +1,28 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-lazy="false">
<class name="org.hibernate.envers.test.integration.components.dynamic.AdvancedEntity" table="advanced_entity">
<id name="id" type="long" column="id"/>
<property name="note" type="string"/>
<dynamic-component name="dynamicConfiguration">
<property name="propInt" type="integer"/>
<property name="propFloat" type="float"/>
<property name="propBoolean" type="boolean"/>
<many-to-one name="propManyToOne" class="org.hibernate.envers.test.integration.components.dynamic.ManyToOneEntity" column="MANY_TO_ONE_ID"/>
<one-to-one name="propOneToOne" class="org.hibernate.envers.test.integration.components.dynamic.OneToOneEntity"/>
</dynamic-component>
<!--<one-to-one name="oneToOneEntity" class="org.hibernate.envers.test.integration.components.dynamic.OneToOneEntity"/>-->
</class>
<class name="org.hibernate.envers.test.integration.components.dynamic.OneToOneEntity" table="one_to_one_entity">
<id name="id" type="long" column="id"/>
<property name="note" type="string"/>
</class>
<class name="org.hibernate.envers.test.integration.components.dynamic.ManyToOneEntity" table="many_to_one_entity">
<id name="id" type="long" column="id"/>
<property name="note" type="string"/>
</class>
</hibernate-mapping>