HHH-7800 - Tests cleanup

This commit is contained in:
Lukasz Antoniak 2012-11-22 12:01:47 +01:00
parent ca44ff7949
commit 6498ca969c
3 changed files with 137 additions and 62 deletions

View File

@ -30,14 +30,14 @@ import java.util.Set;
import javax.persistence.EntityManager;
import junit.framework.Assert;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.query.AuditEntity;
import org.hibernate.envers.query.criteria.AuditDisjunction;
import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase;
import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.entities.StrIntTestEntity;
import org.hibernate.testing.TestForIssue;
import org.junit.Assert;
import org.junit.Test;
/**
@ -49,8 +49,8 @@ public class MaximalizePropertyQuery extends BaseEnversJPAFunctionalTestCase {
Integer id2;
Integer id3;
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(StrIntTestEntity.class);
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { StrIntTestEntity.class };
}
@Test
@ -141,33 +141,32 @@ public class MaximalizePropertyQuery extends BaseEnversJPAFunctionalTestCase {
.add(AuditEntity.property("number").eq(10)))
.getResultList();
System.out.println(result);
assert Arrays.asList(2).equals(result);
}
@Test
public void testMaximizeInDisjunction() {
List<Integer> idsToQuery = Arrays.asList(id1, id3);
@Test
@TestForIssue(jiraKey = "HHH-7800")
public void testMaximizeInDisjunction() {
List<Integer> idsToQuery = Arrays.asList( id1, id3 );
AuditDisjunction disjunction = AuditEntity.disjunction();
AuditDisjunction disjunction = AuditEntity.disjunction();
for (Integer id : idsToQuery) {
disjunction.add(AuditEntity.revisionNumber().maximize()
.add(AuditEntity.id().eq(id)));
}
List result = getAuditReader().createQuery()
.forRevisionsOfEntity(StrIntTestEntity.class, true, true)
.add(disjunction)
.getResultList();
for ( Integer id : idsToQuery ) {
disjunction.add( AuditEntity.revisionNumber().maximize().add( AuditEntity.id().eq( id ) ) );
}
List result = getAuditReader().createQuery()
.forRevisionsOfEntity( StrIntTestEntity.class, true, true )
.add( disjunction )
.getResultList();
Set<Integer> idsSeen = new HashSet<Integer>();
for (Object o : result) {
StrIntTestEntity entity = (StrIntTestEntity) o;
Integer id = entity.getId();
Assert.assertTrue("Entity with ID "+id+" returned but not queried for.", idsToQuery.contains(id));
if (!idsSeen.add(id)) {
Assert.fail("Multiple revisions returned with ID "+id+"; expected only one.");
}
}
}
Set<Integer> idsSeen = new HashSet<Integer>();
for ( Object o : result ) {
StrIntTestEntity entity = (StrIntTestEntity) o;
Integer id = entity.getId();
Assert.assertTrue( "Entity with ID " + id + " returned but not queried for.", idsToQuery.contains( id ) );
if ( !idsSeen.add( id ) ) {
Assert.fail( "Multiple revisions returned with ID " + id + "; expected only one." );
}
}
}
}

View File

@ -39,6 +39,7 @@ import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase;
import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.entities.StrIntTestEntity;
import org.hibernate.envers.test.tools.TestTools;
import org.hibernate.testing.TestForIssue;
/**
* @author Adam Warski (adam at warski dot org)
@ -325,4 +326,21 @@ public class SimpleQuery extends BaseEnversJPAFunctionalTestCase {
List result = getAuditReader().createQuery().forEntitiesModifiedAtRevision(StrIntTestEntity.class, 5).getResultList();
Assert.assertTrue(result.isEmpty());
}
@Test
@TestForIssue(jiraKey = "HHH-7800")
public void testBetweenInsideDisjunction() {
List result = getAuditReader().createQuery()
.forRevisionsOfEntity( StrIntTestEntity.class, true, true )
.add( AuditEntity.disjunction()
.add( AuditEntity.property( "number" ).between( 0, 5 ) )
.add( AuditEntity.property( "number" ).between( 20, 100 ) ) )
.getResultList();
for ( Object o : result ) {
StrIntTestEntity entity = (StrIntTestEntity) o;
int number = entity.getNumber();
Assert.assertTrue( ( number >= 0 && number <= 5 ) || ( number >= 20 && number <= 100 ) );
}
}
}

View File

@ -23,29 +23,38 @@
*/
package org.hibernate.envers.test.integration.query;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import junit.framework.Assert;
import org.junit.Test;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.RevisionType;
import org.hibernate.envers.enhanced.SequenceIdRevisionEntity;
import org.hibernate.envers.query.AuditEntity;
import org.hibernate.envers.query.criteria.AuditCriterion;
import org.hibernate.envers.query.criteria.AuditDisjunction;
import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase;
import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.entities.StrIntTestEntity;
import org.hibernate.testing.TestForIssue;
/**
* A test which checks if the data of a deleted entity is stored when the setting is on.
* @author Adam Warski (adam at warski dot org)
*/
@SuppressWarnings({"unchecked"})
@SuppressWarnings({ "unchecked" })
public class StoreDeletedData extends BaseEnversJPAFunctionalTestCase {
private Integer id1;
private Integer id1;
private Integer id2;
private Integer id3;
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(StrIntTestEntity.class);
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { StrIntTestEntity.class };
}
@Override
protected void addConfigOptions(Map options) {
@ -54,37 +63,86 @@ public class StoreDeletedData extends BaseEnversJPAFunctionalTestCase {
}
@Test
@Priority(10)
public void initData() {
// Revision 1
EntityManager em = getEntityManager();
em.getTransaction().begin();
@Priority(10)
public void initData() {
EntityManager em = getEntityManager();
StrIntTestEntity site1 = new StrIntTestEntity("a", 10);
// Revision 1
em.getTransaction().begin();
StrIntTestEntity site1 = new StrIntTestEntity( "a", 10 );
em.persist( site1 );
id1 = site1.getId();
em.getTransaction().commit();
em.persist(site1);
// Revision 2
em.getTransaction().begin();
em.remove( site1 );
em.getTransaction().commit();
id1 = site1.getId();
// Revision 3
em.getTransaction().begin();
StrIntTestEntity site2 = new StrIntTestEntity( "b", 20 );
em.persist( site2 );
id2 = site2.getId();
StrIntTestEntity site3 = new StrIntTestEntity( "c", 30 );
em.persist( site3 );
id3 = site3.getId();
em.getTransaction().commit();
em.getTransaction().commit();
// Revision 4
em.getTransaction().begin();
em.remove( site2 );
em.remove( site3 );
em.getTransaction().commit();
// Revision 2
em.getTransaction().begin();
em.close();
}
em.remove(site1);
@Test
public void testRevisionsPropertyEqQuery() {
List revs_id1 = getAuditReader().createQuery()
.forRevisionsOfEntity( StrIntTestEntity.class, false, true )
.add( AuditEntity.id().eq( id1 ) )
.getResultList();
em.getTransaction().commit();
}
Assert.assertEquals( 2, revs_id1.size() );
Assert.assertEquals( new StrIntTestEntity( "a", 10, id1 ), ( (Object[]) revs_id1.get( 0 ) )[0] );
Assert.assertEquals( new StrIntTestEntity( "a", 10, id1 ), ( (Object[]) revs_id1.get( 1 ) )[0] );
}
@Test
public void testRevisionsPropertyEqQuery() {
List revs_id1 = getAuditReader().createQuery()
.forRevisionsOfEntity(StrIntTestEntity.class, false, true)
.add(AuditEntity.id().eq(id1))
.getResultList();
@Test
@TestForIssue(jiraKey = "HHH-7800")
public void testMaximizeInDisjunction() {
List<Integer> queryIds = Arrays.asList( id2, id3 );
assert revs_id1.size() == 2;
assert ((Object[]) revs_id1.get(0))[0].equals(new StrIntTestEntity("a", 10, id1));
assert ((Object[]) revs_id1.get(1))[0].equals(new StrIntTestEntity("a", 10, id1));
}
AuditDisjunction disjunction = AuditEntity.disjunction();
for ( Integer id : queryIds ) {
AuditCriterion crit = AuditEntity.revisionNumber().maximize()
.add( AuditEntity.id().eq( id ) )
.add( AuditEntity.revisionType().ne( RevisionType.DEL ) );
disjunction.add( crit );
// Workaround: using this line instead works correctly:
// disjunction.add(AuditEntity.conjunction().add(crit));
}
List<?> beforeDeletionRevisions = getAuditReader().createQuery()
.forRevisionsOfEntity( StrIntTestEntity.class, false, false )
.add( disjunction )
.addOrder( AuditEntity.property( "id" ).asc() )
.getResultList();
Assert.assertEquals( 2, beforeDeletionRevisions.size() );
Object[] result1 = (Object[]) beforeDeletionRevisions.get( 0 );
Object[] result2 = (Object[]) beforeDeletionRevisions.get( 1 );
Assert.assertEquals( new StrIntTestEntity( "b", 20, id2 ), result1[0] );
// Making sure that we have received an entity added at revision 3.
Assert.assertEquals( 3, ( (SequenceIdRevisionEntity) result1[1] ).getId() );
Assert.assertEquals( RevisionType.ADD, result1[2] );
Assert.assertEquals( new StrIntTestEntity( "c", 30, id3 ), result2[0] );
// Making sure that we have received an entity added at revision 3.
Assert.assertEquals( 3, ( (SequenceIdRevisionEntity) result2[1] ).getId() );
Assert.assertEquals( RevisionType.ADD, result2[2] );
}
}