HHH-8505: More advanced tests

This commit is contained in:
zuchos 2013-09-30 12:47:01 +02:00 committed by adamw
parent 7c492ddbc3
commit bf469ca0e4
4 changed files with 282 additions and 26 deletions

View File

@ -1,11 +1,16 @@
package org.hibernate.envers.test.integration.components.dynamic; package org.hibernate.envers.test.integration.components.dynamic;
import com.google.common.collect.Lists;
import junit.framework.Assert; import junit.framework.Assert;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.envers.test.BaseEnversFunctionalTestCase; import org.hibernate.envers.test.BaseEnversFunctionalTestCase;
import org.hibernate.envers.test.Priority; import org.hibernate.envers.test.Priority;
import org.junit.Test; import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AuditedDynamicComponentsAdvancedCasesTest extends BaseEnversFunctionalTestCase { public class AuditedDynamicComponentsAdvancedCasesTest extends BaseEnversFunctionalTestCase {
public static final String PROP_BOOLEAN = "propBoolean"; public static final String PROP_BOOLEAN = "propBoolean";
@ -13,12 +18,50 @@ public class AuditedDynamicComponentsAdvancedCasesTest extends BaseEnversFunctio
public static final String PROP_FLOAT = "propFloat"; public static final String PROP_FLOAT = "propFloat";
public static final String PROP_MANY_TO_ONE = "propManyToOne"; public static final String PROP_MANY_TO_ONE = "propManyToOne";
public static final String PROP_ONE_TO_ONE = "propOneToOne"; public static final String PROP_ONE_TO_ONE = "propOneToOne";
public static final String INTERNAL_COMPONENT = "internalComponent";
public static final String INTERNAL_LIST = "internalList";
public static final String INTERNAL_MAP = "internalMap";
public static final String INTERNAL_MAP_WITH_MANY_TO_MANY = "internalMapWithEntities";
@Override @Override
protected String[] getMappings() { protected String[] getMappings() {
return new String[]{"mappings/dynamicComponents/mapAdvanced.hbm.xml"}; return new String[]{"mappings/dynamicComponents/mapAdvanced.hbm.xml"};
} }
private OneToOneEntity getOneToOneEntity() {
return new OneToOneEntity(1L, "OneToOne");
}
private ManyToManyEntity getManyToManyEntity() {
return new ManyToManyEntity(1L, "ManyToMany");
}
private ManyToOneEntity getManyToOneEntity() {
return new ManyToOneEntity(1L, "ManyToOne");
}
private AdvancedEntity getAdvancedEntity(ManyToOneEntity manyToOne, OneToOneEntity oneToOne, ManyToManyEntity manyToManyEntity) {
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);
advancedEntity.getDynamicConfiguration().put(INTERNAL_COMPONENT, new InternalComponent("Internal value"));
advancedEntity.getDynamicConfiguration().put(INTERNAL_LIST, Lists.newArrayList("One", "Two", "Three"));
Map<String, String> map = new HashMap<String, String>();
map.put("one", "1");
map.put("two", "2");
advancedEntity.getDynamicConfiguration().put(INTERNAL_MAP, map);
Map<String, ManyToManyEntity> mapWithManyToMany = new HashMap<String, ManyToManyEntity>();
mapWithManyToMany.put("entity1", manyToManyEntity);
advancedEntity.getDynamicConfiguration().put(INTERNAL_MAP_WITH_MANY_TO_MANY, mapWithManyToMany);
return advancedEntity;
}
@Test @Test
@Priority(10) @Priority(10)
//smoke test to make sure that hibernate & envers are working with the entity&mappings //smoke test to make sure that hibernate & envers are working with the entity&mappings
@ -26,13 +69,52 @@ public class AuditedDynamicComponentsAdvancedCasesTest extends BaseEnversFunctio
//given //given
ManyToOneEntity manyToOne = getManyToOneEntity(); ManyToOneEntity manyToOne = getManyToOneEntity();
OneToOneEntity oneToOne = getOneToOneEntity(); OneToOneEntity oneToOne = getOneToOneEntity();
AdvancedEntity advancedEntity = getAdvancedEntity(manyToOne, oneToOne); ManyToManyEntity manyToManyEntity = getManyToManyEntity();
AdvancedEntity advancedEntity = getAdvancedEntity(manyToOne, oneToOne, manyToManyEntity);
//rev 1
Session session = openSession(); Session session = openSession();
session.getTransaction().begin(); session.getTransaction().begin();
session.save(manyToOne); session.save(manyToOne);
session.save(oneToOne); session.save(oneToOne);
session.save(manyToManyEntity);
session.save(advancedEntity);
session.getTransaction().commit();
//rev 2
session.getTransaction().begin();
InternalComponent internalComponent = (InternalComponent) advancedEntity.getDynamicConfiguration().get(INTERNAL_COMPONENT);
internalComponent.setProperty("new value");
session.save(advancedEntity);
session.getTransaction().commit();
//rev 3
session.getTransaction().begin();
List<String> internalList = (List) advancedEntity.getDynamicConfiguration().get(INTERNAL_LIST);
internalList.add("four");
session.save(advancedEntity);
session.getTransaction().commit();
//rev 4
session.getTransaction().begin();
Map<String, String> map = (Map) advancedEntity.getDynamicConfiguration().get(INTERNAL_MAP);
map.put("three", "3");
session.save(advancedEntity);
session.getTransaction().commit();
//rev 5
session.getTransaction().begin();
Map<String, ManyToManyEntity> mapWithManyToMany = (Map) advancedEntity.getDynamicConfiguration().get(INTERNAL_MAP_WITH_MANY_TO_MANY);
ManyToManyEntity manyToManyEntity2 = new ManyToManyEntity(2L, "new value");
mapWithManyToMany.put("entity2", manyToManyEntity2);
session.save(manyToManyEntity2);
session.save(advancedEntity);
session.getTransaction().commit();
//rev 6
session.getTransaction().begin();
mapWithManyToMany = (Map) advancedEntity.getDynamicConfiguration().get(INTERNAL_MAP_WITH_MANY_TO_MANY);
mapWithManyToMany.clear();
session.save(advancedEntity); session.save(advancedEntity);
session.getTransaction().commit(); session.getTransaction().commit();
@ -41,43 +123,84 @@ public class AuditedDynamicComponentsAdvancedCasesTest extends BaseEnversFunctio
Assert.assertEquals(advancedEntity, advancedEntityActual); 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 @Test
public void shouldMakeFirstRevision() { public void shouldMakeRevisions() {
Session session = openSession(); Session session = openSession();
session.getTransaction().begin();
//given & when shouldSaveEntity //given & when shouldSaveEntity
ManyToOneEntity manyToOne = getManyToOneEntity(); ManyToOneEntity manyToOne = getManyToOneEntity();
OneToOneEntity oneToOne = getOneToOneEntity(); OneToOneEntity oneToOne = getOneToOneEntity();
AdvancedEntity advancedEntity = getAdvancedEntity(manyToOne, oneToOne); ManyToManyEntity manyToManyEntity = getManyToManyEntity();
AdvancedEntity advancedEntity = getAdvancedEntity(manyToOne, oneToOne, manyToManyEntity);
//then //then v1
session.getTransaction().begin();
AdvancedEntity ver1 = getAuditReader().find( AdvancedEntity ver1 = getAuditReader().find(
AdvancedEntity.class, AdvancedEntity.class,
advancedEntity.getId(), advancedEntity.getId(),
1 1
); );
Assert.assertEquals(advancedEntity, ver1); Assert.assertEquals(advancedEntity, ver1);
//then v2
InternalComponent internalComponent = (InternalComponent) advancedEntity.getDynamicConfiguration().get(INTERNAL_COMPONENT);
internalComponent.setProperty("new value");
AdvancedEntity ver2 = getAuditReader().find(
AdvancedEntity.class,
advancedEntity.getId(),
2
);
Assert.assertEquals(advancedEntity, ver2);
//then v3
List internalList = (List) advancedEntity.getDynamicConfiguration().get(INTERNAL_LIST);
internalList.add("four");
AdvancedEntity ver3 = getAuditReader().find(
AdvancedEntity.class,
advancedEntity.getId(),
3
);
Assert.assertEquals(advancedEntity, ver3);
//then v4
Map<String, String> map = (Map) advancedEntity.getDynamicConfiguration().get(INTERNAL_MAP);
map.put("three", "3");
AdvancedEntity ver4 = getAuditReader().find(
AdvancedEntity.class,
advancedEntity.getId(),
4
);
Assert.assertEquals(advancedEntity, ver4);
//then v5
Map<String, ManyToManyEntity> mapWithManyToMany = (Map) advancedEntity.getDynamicConfiguration().get(INTERNAL_MAP_WITH_MANY_TO_MANY);
ManyToManyEntity manyToManyEntity2 = new ManyToManyEntity(2L, "new value");
mapWithManyToMany.put("entity2", manyToManyEntity2);
AdvancedEntity ver5 = getAuditReader().find(
AdvancedEntity.class,
advancedEntity.getId(),
5
);
Assert.assertEquals(advancedEntity, ver5);
//then v6
mapWithManyToMany = (Map) advancedEntity.getDynamicConfiguration().get(INTERNAL_MAP_WITH_MANY_TO_MANY);
mapWithManyToMany.clear();
AdvancedEntity ver6 = getAuditReader().find(
AdvancedEntity.class,
advancedEntity.getId(),
6
);
Assert.assertEquals(advancedEntity, ver6);
session.getTransaction().commit(); session.getTransaction().commit();
} }
private OneToOneEntity getOneToOneEntity() {
return new OneToOneEntity(1L, "OneToOne");
}
private ManyToOneEntity getManyToOneEntity() {
return new ManyToOneEntity(1L, "ManyToOne");
}
} }

View File

@ -0,0 +1,45 @@
package org.hibernate.envers.test.integration.components.dynamic;
public class InternalComponent {
private String property;
public InternalComponent() {
}
public InternalComponent(String property) {
this.property = property;
}
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof InternalComponent)) return false;
InternalComponent that = (InternalComponent) o;
if (property != null ? !property.equals(that.property) : that.property != null) return false;
return true;
}
@Override
public int hashCode() {
return property != null ? property.hashCode() : 0;
}
@Override
public String toString() {
return "InternalComponent{" +
"property='" + property + '\'' +
'}';
}
}

View File

@ -0,0 +1,62 @@
package org.hibernate.envers.test.integration.components.dynamic;
import org.hibernate.envers.Audited;
@Audited
public class ManyToManyEntity {
private Long id;
private String note;
public ManyToManyEntity() {
}
public ManyToManyEntity(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 ManyToManyEntity)) return false;
ManyToManyEntity that = (ManyToManyEntity) 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 "ManyToManyEntity{" +
"id=" + id +
", note='" + note + '\'' +
'}';
}
}

View File

@ -10,8 +10,30 @@
<property name="propInt" type="integer"/> <property name="propInt" type="integer"/>
<property name="propFloat" type="float"/> <property name="propFloat" type="float"/>
<property name="propBoolean" type="boolean"/> <property name="propBoolean" type="boolean"/>
<many-to-one name="propManyToOne" class="org.hibernate.envers.test.integration.components.dynamic.ManyToOneEntity" column="MANY_TO_ONE_ID"/> <many-to-one name="propManyToOne"
<one-to-one name="propOneToOne" class="org.hibernate.envers.test.integration.components.dynamic.OneToOneEntity"/> 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"/>
<component name="internalComponent"
class="org.hibernate.envers.test.integration.components.dynamic.InternalComponent">
<property name="property" column="internal_comp_prop"/>
</component>
<list name="internalList" table="INTERNAL_LIST">
<key column="ADVANCED_ID"/>
<index column="POSITION"/>
<element type="string" column="LIST_ENTRY"/>
</list>
<map name="internalMap" table="INTERNAL_MAP">
<key column="ADVANCED_ID"/>
<map-key type="string" column="MAP_KEY"/>
<element type="string" column="MAP_VALUE"/>
</map>
<map name="internalMapWithEntities" table="INTERNAL_MAP_WITH_ENTITIES">
<key column="ADVANCED_ID"/>
<map-key type="string" column="MAP_KEY"/>
<many-to-many class="org.hibernate.envers.test.integration.components.dynamic.ManyToManyEntity" column="MAP_VAL_MANY_TO_MANY"/>
</map>
</dynamic-component> </dynamic-component>
<!--<one-to-one name="oneToOneEntity" class="org.hibernate.envers.test.integration.components.dynamic.OneToOneEntity"/>--> <!--<one-to-one name="oneToOneEntity" class="org.hibernate.envers.test.integration.components.dynamic.OneToOneEntity"/>-->
</class> </class>
@ -23,6 +45,10 @@
<id name="id" type="long" column="id"/> <id name="id" type="long" column="id"/>
<property name="note" type="string"/> <property name="note" type="string"/>
</class> </class>
<class name="org.hibernate.envers.test.integration.components.dynamic.ManyToManyEntity" table="many_to_many_entity">
<id name="id" type="long" column="id"/>
<property name="note" type="string"/>
</class>
</hibernate-mapping> </hibernate-mapping>