HHH-7800 - Tests cleanup

This commit is contained in:
Lukasz Antoniak 2012-11-22 12:01:47 +01:00
parent dadc43af23
commit 468c58358e
3 changed files with 132 additions and 73 deletions

View File

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

View File

@ -34,11 +34,11 @@ import org.junit.Test;
import org.hibernate.envers.enhanced.SequenceIdRevisionEntity; import org.hibernate.envers.enhanced.SequenceIdRevisionEntity;
import org.hibernate.envers.RevisionType; import org.hibernate.envers.RevisionType;
import org.hibernate.envers.query.AuditEntity; import org.hibernate.envers.query.AuditEntity;
import org.hibernate.envers.query.criteria.AuditDisjunction;
import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase; import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase;
import org.hibernate.envers.test.Priority; import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.entities.StrIntTestEntity; import org.hibernate.envers.test.entities.StrIntTestEntity;
import org.hibernate.envers.test.tools.TestTools; import org.hibernate.envers.test.tools.TestTools;
import org.hibernate.testing.TestForIssue;
/** /**
* @author Adam Warski (adam at warski dot org) * @author Adam Warski (adam at warski dot org)
@ -327,19 +327,20 @@ public class SimpleQuery extends BaseEnversJPAFunctionalTestCase {
Assert.assertTrue(result.isEmpty()); Assert.assertTrue(result.isEmpty());
} }
@Test @Test
public void testBetweenInsideDisjunction() { @TestForIssue(jiraKey = "HHH-7800")
List result = getAuditReader().createQuery() public void testBetweenInsideDisjunction() {
.forRevisionsOfEntity(StrIntTestEntity.class, true, true) List result = getAuditReader().createQuery()
.add(AuditEntity.disjunction() .forRevisionsOfEntity( StrIntTestEntity.class, true, true )
.add(AuditEntity.property("number").between(0, 5)) .add( AuditEntity.disjunction()
.add(AuditEntity.property("number").between(20, 100))) .add( AuditEntity.property( "number" ).between( 0, 5 ) )
.getResultList(); .add( AuditEntity.property( "number" ).between( 20, 100 ) ) )
.getResultList();
for (Object o : result) { for ( Object o : result ) {
StrIntTestEntity entity = (StrIntTestEntity)o; StrIntTestEntity entity = (StrIntTestEntity) o;
int number = entity.getNumber(); int number = entity.getNumber();
assert ( number >= 0 && number <= 5) || ( number >= 20 && number <= 100); Assert.assertTrue( ( number >= 0 && number <= 5 ) || ( number >= 20 && number <= 100 ) );
} }
} }
} }

View File

@ -23,29 +23,38 @@
*/ */
package org.hibernate.envers.test.integration.query; package org.hibernate.envers.test.integration.query;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import junit.framework.Assert;
import org.junit.Test; import org.junit.Test;
import org.hibernate.envers.RevisionType;
import org.hibernate.envers.enhanced.SequenceIdRevisionEntity;
import org.hibernate.envers.query.AuditEntity; 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.BaseEnversJPAFunctionalTestCase;
import org.hibernate.envers.test.Priority; import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.entities.StrIntTestEntity; 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. * 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) * @author Adam Warski (adam at warski dot org)
*/ */
@SuppressWarnings({"unchecked"}) @SuppressWarnings({ "unchecked" })
public class StoreDeletedData extends BaseEnversJPAFunctionalTestCase { public class StoreDeletedData extends BaseEnversJPAFunctionalTestCase {
private Integer id1; private Integer id1;
private Integer id2;
private Integer id3;
@Override @Override
protected Class<?>[] getAnnotatedClasses() { protected Class<?>[] getAnnotatedClasses() {
return new Class[] { StrIntTestEntity.class }; return new Class[] { StrIntTestEntity.class };
} }
@Override @Override
protected void addConfigOptions(Map options) { protected void addConfigOptions(Map options) {
@ -54,37 +63,86 @@ public class StoreDeletedData extends BaseEnversJPAFunctionalTestCase {
} }
@Test @Test
@Priority(10) @Priority(10)
public void initData() { public void initData() {
// Revision 1 EntityManager em = getEntityManager();
EntityManager em = getEntityManager();
em.getTransaction().begin();
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.close();
em.getTransaction().begin(); }
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 @Test
public void testRevisionsPropertyEqQuery() { @TestForIssue(jiraKey = "HHH-7800")
List revs_id1 = getAuditReader().createQuery() public void testMaximizeInDisjunction() {
.forRevisionsOfEntity(StrIntTestEntity.class, false, true) List<Integer> queryIds = Arrays.asList( id2, id3 );
.add(AuditEntity.id().eq(id1))
.getResultList();
assert revs_id1.size() == 2; AuditDisjunction disjunction = AuditEntity.disjunction();
assert ((Object[]) revs_id1.get(0))[0].equals(new StrIntTestEntity("a", 10, id1)); for ( Integer id : queryIds ) {
assert ((Object[]) revs_id1.get(1))[0].equals(new StrIntTestEntity("a", 10, id1)); 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] );
}
} }