Rollback transaction on fail test. Check existed entities count.
authorindvdum (gotoindvdum[at]gmail[dot]com)
Mon, 12 Nov 2012 05:48:41 +0400
changeset 107241826f43f6
parent 9 dc42467eb5dc
child 11 85972c87bea4
Rollback transaction on fail test. Check existed entities count.
src/test/java/ru/indvdum/jpa/tests/AbstractJPAEntityTest.groovy
     1.1 --- a/src/test/java/ru/indvdum/jpa/tests/AbstractJPAEntityTest.groovy	Mon Nov 12 05:45:11 2012 +0400
     1.2 +++ b/src/test/java/ru/indvdum/jpa/tests/AbstractJPAEntityTest.groovy	Mon Nov 12 05:48:41 2012 +0400
     1.3 @@ -33,12 +33,16 @@
     1.4  	protected def uniqueValue = 1
     1.5  	protected JPADataAccessObject dao = null
     1.6  	protected Set toRemove = new HashSet()
     1.7 +	protected Map<Class, Integer> existedEntitiesCount = [:]
     1.8  
     1.9  	@Test
    1.10  	public void testEntity() {
    1.11  		dao = createDAO()
    1.12  		try {
    1.13  			testEntity(getEntityClass())
    1.14 +		} catch (Throwable t) {
    1.15 +			dao.rollback()
    1.16 +			throw t
    1.17  		} finally {
    1.18  			dao.close()		
    1.19  		}
    1.20 @@ -71,7 +75,7 @@
    1.21  				assert it.class.isAssignableFrom(it2.class)
    1.22  			}.size() == toRemove.findAll { it2 ->
    1.23  				it.class.isAssignableFrom(it2.class)
    1.24 -			}.size()
    1.25 +			}.size() + existedEntitiesCount[it.class]
    1.26  		}
    1.27  
    1.28  		// updating
    1.29 @@ -100,6 +104,8 @@
    1.30  		def entity = entityClass.newInstance()
    1.31  		assert entity.class == entityClass
    1.32  		toRemove.add(entity)
    1.33 +		if (!existedEntitiesCount.containsKey(entityClass))
    1.34 +			existedEntitiesCount[entityClass] = dao.list(entityClass).size()
    1.35  		
    1.36  		Map<String, Object> fieldsValues = updateFields(entity)
    1.37  		assert dao.persist(entity)