HHH-8505: Query test cases & small fixes
This commit is contained in:
parent
4e810effe5
commit
6bc1d2ef01
|
@ -1,19 +1,21 @@
|
|||
package org.hibernate.envers.test.integration.components.dynamic;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.QueryException;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.envers.query.AuditEntity;
|
||||
import org.hibernate.envers.test.BaseEnversFunctionalTestCase;
|
||||
import org.hibernate.envers.test.Priority;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Lukasz Zuchowski (author at zuchos dot com)
|
||||
|
@ -22,246 +24,285 @@ import java.util.Set;
|
|||
@TestForIssue(jiraKey = "HHH-8049")
|
||||
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";
|
||||
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";
|
||||
public static final String INTERNAL_SET = "internalSet";
|
||||
public static final String INTERNAL_SET_OF_COMPONENTS = "internalSetOfComponents";
|
||||
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";
|
||||
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";
|
||||
public static final String INTERNAL_SET = "internalSet";
|
||||
public static final String INTERNAL_SET_OF_COMPONENTS = "internalSetOfComponents";
|
||||
|
||||
@Override
|
||||
protected String[] getMappings() {
|
||||
return new String[]{"mappings/dynamicComponents/mapAdvanced.hbm.xml"};
|
||||
}
|
||||
@Override
|
||||
protected String[] getMappings() {
|
||||
return new String[] { "mappings/dynamicComponents/mapAdvanced.hbm.xml" };
|
||||
}
|
||||
|
||||
private OneToOneEntity getOneToOneEntity() {
|
||||
return new OneToOneEntity(1L, "OneToOne");
|
||||
}
|
||||
private OneToOneEntity getOneToOneEntity() {
|
||||
return new OneToOneEntity( 1L, "OneToOne" );
|
||||
}
|
||||
|
||||
private ManyToManyEntity getManyToManyEntity() {
|
||||
return new ManyToManyEntity(1L, "ManyToMany");
|
||||
}
|
||||
private ManyToManyEntity getManyToManyEntity() {
|
||||
return new ManyToManyEntity( 1L, "ManyToMany" );
|
||||
}
|
||||
|
||||
private ManyToOneEntity getManyToOneEntity() {
|
||||
return new ManyToOneEntity(1L, "ManyToOne");
|
||||
}
|
||||
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);
|
||||
advancedEntity.getDynamicConfiguration().put(INTERNAL_SET, Sets.newHashSet("Uno", "Due"));
|
||||
advancedEntity.getDynamicConfiguration().put(INTERNAL_SET_OF_COMPONENTS, Sets.newHashSet(new InternalComponent("Ein"), new InternalComponent("Zwei")));
|
||||
return advancedEntity;
|
||||
}
|
||||
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" ) );
|
||||
List<String> list = new ArrayList<String>();
|
||||
list.add( "One" );
|
||||
list.add( "Two" );
|
||||
list.add( "Three" );
|
||||
advancedEntity.getDynamicConfiguration().put( INTERNAL_LIST, list );
|
||||
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 );
|
||||
Set<String> set = new HashSet<String>();
|
||||
set.add( "Une" );
|
||||
set.add( "Due" );
|
||||
advancedEntity.getDynamicConfiguration().put( INTERNAL_SET, set );
|
||||
Set<InternalComponent> componentSet = new HashSet<InternalComponent>();
|
||||
componentSet.add( new InternalComponent( "Ein" ) );
|
||||
componentSet.add( new InternalComponent( "Zwei" ) );
|
||||
advancedEntity.getDynamicConfiguration().put( INTERNAL_SET_OF_COMPONENTS, componentSet );
|
||||
return advancedEntity;
|
||||
}
|
||||
|
||||
@Test
|
||||
@Priority(10)
|
||||
//smoke test to make sure that hibernate & envers are working with the entity&mappings
|
||||
public void shouldInitData() {
|
||||
//given
|
||||
ManyToOneEntity manyToOne = getManyToOneEntity();
|
||||
OneToOneEntity oneToOne = getOneToOneEntity();
|
||||
ManyToManyEntity manyToManyEntity = getManyToManyEntity();
|
||||
AdvancedEntity advancedEntity = getAdvancedEntity(manyToOne, oneToOne, manyToManyEntity);
|
||||
@Test
|
||||
@Priority(10)
|
||||
//smoke test to make sure that hibernate & envers are working with the entity&mappings
|
||||
public void shouldInitData() {
|
||||
//given
|
||||
ManyToOneEntity manyToOne = getManyToOneEntity();
|
||||
OneToOneEntity oneToOne = getOneToOneEntity();
|
||||
ManyToManyEntity manyToManyEntity = getManyToManyEntity();
|
||||
AdvancedEntity advancedEntity = getAdvancedEntity( manyToOne, oneToOne, manyToManyEntity );
|
||||
|
||||
//rev 1
|
||||
Session session = openSession();
|
||||
session.getTransaction().begin();
|
||||
session.save(manyToOne);
|
||||
session.save(oneToOne);
|
||||
session.save(manyToManyEntity);
|
||||
session.save(advancedEntity);
|
||||
session.getTransaction().commit();
|
||||
//rev 1
|
||||
Session session = openSession();
|
||||
session.getTransaction().begin();
|
||||
session.save( manyToOne );
|
||||
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 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 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 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 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.getTransaction().commit();
|
||||
//rev 6
|
||||
session.getTransaction().begin();
|
||||
mapWithManyToMany = (Map) advancedEntity.getDynamicConfiguration().get( INTERNAL_MAP_WITH_MANY_TO_MANY );
|
||||
mapWithManyToMany.clear();
|
||||
session.save( advancedEntity );
|
||||
session.getTransaction().commit();
|
||||
|
||||
//rev 7
|
||||
session.getTransaction().begin();
|
||||
Set<InternalComponent> internalComponentSet = (Set) advancedEntity.getDynamicConfiguration().get(INTERNAL_SET_OF_COMPONENTS);
|
||||
internalComponentSet.add(new InternalComponent("drei"));
|
||||
session.save(advancedEntity);
|
||||
session.getTransaction().commit();
|
||||
//rev 7
|
||||
session.getTransaction().begin();
|
||||
Set<InternalComponent> internalComponentSet = (Set) advancedEntity.getDynamicConfiguration()
|
||||
.get( INTERNAL_SET_OF_COMPONENTS );
|
||||
internalComponentSet.add( new InternalComponent( "drei" ) );
|
||||
session.save( advancedEntity );
|
||||
session.getTransaction().commit();
|
||||
|
||||
AdvancedEntity advancedEntityActual = (AdvancedEntity) session.load(AdvancedEntity.class, 1L);
|
||||
AdvancedEntity advancedEntityActual = (AdvancedEntity) session.load( AdvancedEntity.class, 1L );
|
||||
|
||||
Assert.assertEquals(advancedEntity, advancedEntityActual);
|
||||
}
|
||||
Assert.assertEquals( advancedEntity, advancedEntityActual );
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldMakeRevisions() {
|
||||
Session session = openSession();
|
||||
session.getTransaction().begin();
|
||||
//given & when shouldInitData
|
||||
ManyToOneEntity manyToOne = getManyToOneEntity();
|
||||
OneToOneEntity oneToOne = getOneToOneEntity();
|
||||
ManyToManyEntity manyToManyEntity = getManyToManyEntity();
|
||||
AdvancedEntity advancedEntity = getAdvancedEntity(manyToOne, oneToOne, manyToManyEntity);
|
||||
@Test
|
||||
public void shouldMakeRevisions() {
|
||||
Session session = openSession();
|
||||
session.getTransaction().begin();
|
||||
//given & when shouldInitData
|
||||
ManyToOneEntity manyToOne = getManyToOneEntity();
|
||||
OneToOneEntity oneToOne = getOneToOneEntity();
|
||||
ManyToManyEntity manyToManyEntity = getManyToManyEntity();
|
||||
AdvancedEntity advancedEntity = getAdvancedEntity( manyToOne, oneToOne, manyToManyEntity );
|
||||
|
||||
//then v1
|
||||
AdvancedEntity ver1 = getAuditReader().find(
|
||||
AdvancedEntity.class,
|
||||
advancedEntity.getId(),
|
||||
1
|
||||
);
|
||||
Assert.assertEquals(advancedEntity, ver1);
|
||||
//then v1
|
||||
AdvancedEntity ver1 = getAuditReader().find(
|
||||
AdvancedEntity.class,
|
||||
advancedEntity.getId(),
|
||||
1
|
||||
);
|
||||
Assert.assertEquals( advancedEntity, ver1 );
|
||||
|
||||
//then v2
|
||||
InternalComponent internalComponent = (InternalComponent) advancedEntity.getDynamicConfiguration().get(INTERNAL_COMPONENT);
|
||||
internalComponent.setProperty("new value");
|
||||
//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);
|
||||
AdvancedEntity ver2 = getAuditReader().find(
|
||||
AdvancedEntity.class,
|
||||
advancedEntity.getId(),
|
||||
2
|
||||
);
|
||||
Assert.assertEquals( advancedEntity, ver2 );
|
||||
|
||||
//then v3
|
||||
//then v3
|
||||
|
||||
List internalList = (List) advancedEntity.getDynamicConfiguration().get(INTERNAL_LIST);
|
||||
internalList.add("four");
|
||||
List internalList = (List) advancedEntity.getDynamicConfiguration().get( INTERNAL_LIST );
|
||||
internalList.add( "four" );
|
||||
|
||||
AdvancedEntity ver3 = getAuditReader().find(
|
||||
AdvancedEntity.class,
|
||||
advancedEntity.getId(),
|
||||
3
|
||||
);
|
||||
Assert.assertEquals(advancedEntity, ver3);
|
||||
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");
|
||||
//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);
|
||||
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);
|
||||
//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);
|
||||
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();
|
||||
//then v6
|
||||
mapWithManyToMany = (Map) advancedEntity.getDynamicConfiguration().get( INTERNAL_MAP_WITH_MANY_TO_MANY );
|
||||
mapWithManyToMany.clear();
|
||||
|
||||
AdvancedEntity ver6 = getAuditReader().find(
|
||||
AdvancedEntity.class,
|
||||
advancedEntity.getId(),
|
||||
6
|
||||
);
|
||||
AdvancedEntity ver6 = getAuditReader().find(
|
||||
AdvancedEntity.class,
|
||||
advancedEntity.getId(),
|
||||
6
|
||||
);
|
||||
|
||||
//then v7
|
||||
Set<InternalComponent> internalComponentSet = (Set) advancedEntity.getDynamicConfiguration().get(INTERNAL_SET_OF_COMPONENTS);
|
||||
internalComponentSet.add(new InternalComponent("drei"));
|
||||
//then v7
|
||||
Set<InternalComponent> internalComponentSet = (Set) advancedEntity.getDynamicConfiguration()
|
||||
.get( INTERNAL_SET_OF_COMPONENTS );
|
||||
internalComponentSet.add( new InternalComponent( "drei" ) );
|
||||
|
||||
AdvancedEntity ver7 = getAuditReader().find(
|
||||
AdvancedEntity.class,
|
||||
advancedEntity.getId(),
|
||||
7
|
||||
);
|
||||
Assert.assertEquals(advancedEntity, ver7);
|
||||
AdvancedEntity ver7 = getAuditReader().find(
|
||||
AdvancedEntity.class,
|
||||
advancedEntity.getId(),
|
||||
7
|
||||
);
|
||||
Assert.assertEquals( advancedEntity, ver7 );
|
||||
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOfQueryOnDynamicComponent() {
|
||||
//given (and result of shouldInitData()
|
||||
AdvancedEntity entity = getAdvancedEntity(getManyToOneEntity(), getOneToOneEntity(), getManyToManyEntity());
|
||||
@Test
|
||||
public void testOfQueryOnDynamicComponent() {
|
||||
//given (and result of shouldInitData()
|
||||
AdvancedEntity entity = getAdvancedEntity( getManyToOneEntity(), getOneToOneEntity(), getManyToManyEntity() );
|
||||
|
||||
//when
|
||||
OneToOneEntity oneToOneEntity = (OneToOneEntity) entity.getDynamicConfiguration().get(PROP_ONE_TO_ONE);
|
||||
List resultList = getAuditReader().createQuery()
|
||||
.forEntitiesAtRevision(AuditedDynamicComponentEntity.class, 1)
|
||||
.add(AuditEntity.relatedId("dynamicConfiguration_" + PROP_ONE_TO_ONE).eq(oneToOneEntity.getId()))
|
||||
.getResultList();
|
||||
//when
|
||||
ManyToOneEntity manyToOneEntity = (ManyToOneEntity) entity.getDynamicConfiguration().get( PROP_MANY_TO_ONE );
|
||||
List resultList = getAuditReader().createQuery()
|
||||
.forEntitiesAtRevision( AdvancedEntity.class, 1 )
|
||||
.add(
|
||||
AuditEntity.relatedId( "dynamicConfiguration_" + PROP_MANY_TO_ONE )
|
||||
.eq( manyToOneEntity.getId() )
|
||||
)
|
||||
.getResultList();
|
||||
|
||||
//then
|
||||
Assert.assertEquals(entity, resultList.get(0)); //when
|
||||
//then
|
||||
Assert.assertEquals( entity, resultList.get( 0 ) );
|
||||
|
||||
ManyToOneEntity manyToOneEntity = (ManyToOneEntity) entity.getDynamicConfiguration().get(PROP_MANY_TO_ONE);
|
||||
resultList = getAuditReader().createQuery()
|
||||
.forEntitiesAtRevision(AuditedDynamicComponentEntity.class, 1)
|
||||
.add(AuditEntity.relatedId("manyToOneEntity_" + PROP_MANY_TO_ONE).eq(1L))
|
||||
.getResultList();
|
||||
//when
|
||||
try {
|
||||
OneToOneEntity oneToOneEntity = (OneToOneEntity) entity.getDynamicConfiguration().get( PROP_ONE_TO_ONE );
|
||||
getAuditReader().createQuery()
|
||||
.forEntitiesAtRevision( AdvancedEntity.class, 1 )
|
||||
.add( AuditEntity.property( "dynamicConfiguration_" + PROP_ONE_TO_ONE ).eq( oneToOneEntity ) )
|
||||
.getResultList();
|
||||
|
||||
//then
|
||||
Assert.assertEquals(entity, resultList.get(0));
|
||||
//then
|
||||
Assert.fail();
|
||||
}
|
||||
catch ( QueryException e ) {
|
||||
|
||||
resultList = getAuditReader().createQuery()
|
||||
.forEntitiesAtRevision(AuditedDynamicComponentEntity.class, 1)
|
||||
.add(AuditEntity.property("dynamicConfiguration_" + INTERNAL_LIST).eq(entity.getDynamicConfiguration().get(INTERNAL_LIST)))
|
||||
.getResultList();
|
||||
}
|
||||
catch ( Exception e ) {
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
//then
|
||||
Assert.assertEquals(entity, resultList.get(0));
|
||||
}
|
||||
try {
|
||||
getAuditReader().createQuery()
|
||||
.forEntitiesAtRevision( AdvancedEntity.class, 1 )
|
||||
.add(
|
||||
AuditEntity.property( "dynamicConfiguration_" + INTERNAL_LIST )
|
||||
.eq( entity.getDynamicConfiguration().get( INTERNAL_LIST ) )
|
||||
)
|
||||
.getResultList();
|
||||
Assert.fail();
|
||||
}
|
||||
catch ( QueryException e ) {
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
catch ( Exception e ) {
|
||||
Assert.fail();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,80 @@
|
|||
package org.hibernate.envers.test.integration.components.dynamic;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PlainComponent {
|
||||
|
||||
private String componentNote;
|
||||
private List<ManyToManyEntity> manyToManyList = new ArrayList<ManyToManyEntity>();
|
||||
private OneToOneEntity oneToOneEntity;
|
||||
private ManyToOneEntity manyToOneEntity;
|
||||
|
||||
public String getComponentNote() {
|
||||
return componentNote;
|
||||
}
|
||||
|
||||
public void setComponentNote(String componentNote) {
|
||||
this.componentNote = componentNote;
|
||||
}
|
||||
|
||||
public List<ManyToManyEntity> getManyToManyList() {
|
||||
return manyToManyList;
|
||||
}
|
||||
|
||||
public void setManyToManyList(List<ManyToManyEntity> manyToManyList) {
|
||||
this.manyToManyList = manyToManyList;
|
||||
}
|
||||
|
||||
public OneToOneEntity getOneToOneEntity() {
|
||||
return oneToOneEntity;
|
||||
}
|
||||
|
||||
public void setOneToOneEntity(OneToOneEntity oneToOneEntity) {
|
||||
this.oneToOneEntity = oneToOneEntity;
|
||||
}
|
||||
|
||||
public ManyToOneEntity getManyToOneEntity() {
|
||||
return manyToOneEntity;
|
||||
}
|
||||
|
||||
public void setManyToOneEntity(ManyToOneEntity manyToOneEntity) {
|
||||
this.manyToOneEntity = manyToOneEntity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( !( o instanceof PlainComponent ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PlainComponent that = (PlainComponent) o;
|
||||
|
||||
if ( componentNote != null ? !componentNote.equals( that.componentNote ) : that.componentNote != null ) {
|
||||
return false;
|
||||
}
|
||||
if ( manyToManyList != null ? !manyToManyList.equals( that.manyToManyList ) : that.manyToManyList != null ) {
|
||||
return false;
|
||||
}
|
||||
if ( manyToOneEntity != null ? !manyToOneEntity.equals( that.manyToOneEntity ) : that.manyToOneEntity != null ) {
|
||||
return false;
|
||||
}
|
||||
if ( oneToOneEntity != null ? !oneToOneEntity.equals( that.oneToOneEntity ) : that.oneToOneEntity != null ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = componentNote != null ? componentNote.hashCode() : 0;
|
||||
result = 31 * result + ( manyToManyList != null ? manyToManyList.hashCode() : 0 );
|
||||
result = 31 * result + ( oneToOneEntity != null ? oneToOneEntity.hashCode() : 0 );
|
||||
result = 31 * result + ( manyToOneEntity != null ? manyToOneEntity.hashCode() : 0 );
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
package org.hibernate.envers.test.integration.components.dynamic;
|
||||
|
||||
import org.hibernate.envers.Audited;
|
||||
|
||||
@Audited
|
||||
public class PlainEntity {
|
||||
|
||||
private Long id;
|
||||
private String note;
|
||||
private PlainComponent component;
|
||||
|
||||
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 PlainComponent getComponent() {
|
||||
return component;
|
||||
}
|
||||
|
||||
public void setComponent(PlainComponent component) {
|
||||
this.component = component;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( !( o instanceof PlainEntity ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PlainEntity that = (PlainEntity) o;
|
||||
|
||||
if ( component != null ? !component.equals( that.component ) : that.component != 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 + ( component != null ? component.hashCode() : 0 );
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PlainEntity{" +
|
||||
"id=" + id +
|
||||
", note='" + note + '\'' +
|
||||
", component=" + component +
|
||||
'}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,152 @@
|
|||
package org.hibernate.envers.test.integration.components.dynamic;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.QueryException;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.envers.exception.AuditException;
|
||||
import org.hibernate.envers.query.AuditEntity;
|
||||
import org.hibernate.envers.test.BaseEnversFunctionalTestCase;
|
||||
import org.hibernate.envers.test.Priority;
|
||||
|
||||
public class SanityCheckTest extends BaseEnversFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected String[] getMappings() {
|
||||
return new String[] { "mappings/dynamicComponents/mapSanityCheck.hbm.xml" };
|
||||
}
|
||||
|
||||
@Test
|
||||
@Priority(10)
|
||||
public void shouldInit() {
|
||||
Session session = getSession();
|
||||
session.getTransaction().begin();
|
||||
|
||||
ManyToOneEntity manyToOne = getManyToOneEntity();
|
||||
ManyToManyEntity manyToMany = getManyToManyEntity();
|
||||
OneToOneEntity oneToOne = getOneToOneEntity();
|
||||
|
||||
PlainEntity plainEntity = getPlainEntity( manyToOne, manyToMany, oneToOne );
|
||||
|
||||
session.save( manyToMany );
|
||||
session.save( manyToOne );
|
||||
session.save( oneToOne );
|
||||
session.save( plainEntity );
|
||||
|
||||
session.getTransaction().commit();
|
||||
session.getTransaction().begin();
|
||||
PlainEntity load = (PlainEntity) session.load( PlainEntity.class, 1L );
|
||||
|
||||
Assert.assertEquals( plainEntity, load );
|
||||
session.getTransaction().commit();
|
||||
|
||||
}
|
||||
|
||||
private PlainEntity getPlainEntity(ManyToOneEntity manyToOne, ManyToManyEntity manyToMany, OneToOneEntity oneToOne) {
|
||||
PlainComponent plainComponent = new PlainComponent();
|
||||
List<ManyToManyEntity> manyToManyEntityList = new ArrayList<ManyToManyEntity>();
|
||||
manyToManyEntityList.add( manyToMany );
|
||||
plainComponent.setManyToManyList( manyToManyEntityList );
|
||||
plainComponent.setComponentNote( "Note" );
|
||||
plainComponent.setOneToOneEntity( oneToOne );
|
||||
plainComponent.setManyToOneEntity( manyToOne );
|
||||
|
||||
PlainEntity plainEntity = new PlainEntity();
|
||||
plainEntity.setId( 1L );
|
||||
plainEntity.setNote( "Plain note" );
|
||||
plainEntity.setComponent( plainComponent );
|
||||
return plainEntity;
|
||||
}
|
||||
|
||||
private ManyToOneEntity getManyToOneEntity() {
|
||||
return new ManyToOneEntity( 1L, "ManyToOne" );
|
||||
}
|
||||
|
||||
private OneToOneEntity getOneToOneEntity() {
|
||||
return new OneToOneEntity( 1L, "OneToOne" );
|
||||
}
|
||||
|
||||
private ManyToManyEntity getManyToManyEntity() {
|
||||
return new ManyToManyEntity( 1L, "ManyToMany" );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindRevisionBySimpleProperty() {
|
||||
ManyToOneEntity manyToOne = getManyToOneEntity();
|
||||
ManyToManyEntity manyToMany = getManyToManyEntity();
|
||||
OneToOneEntity oneToOne = getOneToOneEntity();
|
||||
|
||||
PlainEntity entity = getPlainEntity( manyToOne, manyToMany, oneToOne );
|
||||
|
||||
|
||||
//given (and result of shouldInitData()
|
||||
|
||||
//when
|
||||
List resultList = getAuditReader().createQuery()
|
||||
.forEntitiesAtRevision( PlainEntity.class, 1 )
|
||||
.add( AuditEntity.property( "component_componentNote" ).eq( "Note" ) )
|
||||
.getResultList();
|
||||
|
||||
Assert.assertEquals( entity, resultList.get( 0 ) );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFailWhenQueryOnManyToMany() {
|
||||
ManyToManyEntity manyToMany = getManyToManyEntity();
|
||||
|
||||
//when
|
||||
|
||||
List<ManyToManyEntity> manyToManyEntities = new ArrayList<ManyToManyEntity>();
|
||||
manyToManyEntities.add( manyToMany );
|
||||
try {
|
||||
getAuditReader().createQuery()
|
||||
.forEntitiesAtRevision( PlainEntity.class, 1 )
|
||||
.add( AuditEntity.property( "component_manyToManyList" ).eq( manyToManyEntities ) )
|
||||
.getResultList();
|
||||
//then
|
||||
Assert.fail();
|
||||
}
|
||||
catch ( AuditException e ) {
|
||||
}
|
||||
catch ( Exception e ) {
|
||||
Assert.fail();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFailWhenQueryOnManyToOne() {
|
||||
//when
|
||||
PlainEntity plainEntity = (PlainEntity) getAuditReader().createQuery()
|
||||
.forEntitiesAtRevision( PlainEntity.class, 1 )
|
||||
.add( AuditEntity.relatedId( "component_manyToOneEntity" ).eq( getManyToOneEntity().getId() ) )
|
||||
.getResultList().get( 0 );
|
||||
|
||||
//then
|
||||
Assert.assertEquals( getManyToOneEntity(), plainEntity.getComponent().getManyToOneEntity() );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFailWhenQueryOnOneToOne() {
|
||||
//when
|
||||
try {
|
||||
getAuditReader().createQuery()
|
||||
.forEntitiesAtRevision( PlainEntity.class, 1 )
|
||||
.add( AuditEntity.relatedId( "component_oneToOneEntity" ).eq( getOneToOneEntity().getId() ) )
|
||||
.getResultList();
|
||||
|
||||
//then
|
||||
Assert.fail();
|
||||
}
|
||||
catch ( QueryException e ) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch ( Exception e ) {
|
||||
e.printStackTrace();
|
||||
Assert.fail();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
<?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">
|
||||
<!-- shortened table name for Oracle -->
|
||||
<class name="org.hibernate.envers.test.integration.components.dynamic.PlainEntity" table="Plain_entity">
|
||||
<id name="id" type="long" column="id"/>
|
||||
<property name="note" type="string"/>
|
||||
<component name="component" class="org.hibernate.envers.test.integration.components.dynamic.PlainComponent">
|
||||
<property name="componentNote" type="string"/>
|
||||
<one-to-one name="oneToOneEntity"
|
||||
class="org.hibernate.envers.test.integration.components.dynamic.OneToOneEntity"/>
|
||||
<many-to-one name="manyToOneEntity"
|
||||
class="org.hibernate.envers.test.integration.components.dynamic.ManyToOneEntity"
|
||||
column="MANY_TO_ONE_ID"/>
|
||||
<list name="manyToManyList" table="MANY_TO_MANY_LIST">
|
||||
<key column="PLAIN_ENTITY_ID"/>
|
||||
<index column="POSITION"/>
|
||||
<many-to-many class="org.hibernate.envers.test.integration.components.dynamic.ManyToManyEntity"
|
||||
column="MANY_TO_MANY_ID"/>
|
||||
</list>
|
||||
</component>
|
||||
</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>
|
||||
<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>
|
Loading…
Reference in New Issue