src/test/java/ru/indvdum/jpa/tests/AbstractJPAEntityTest.groovy
author indvdum (gotoindvdum[at]gmail[dot]com)
Mon, 12 Nov 2012 05:48:41 +0400
changeset 10 7241826f43f6
parent 2 f692598b3742
child 14 22646fd200c5
permissions -rw-r--r--
Rollback transaction on fail test. Check existed entities count.
indvdum@2
     1
package ru.indvdum.jpa.tests
indvdum@2
     2
;
indvdum@2
     3
indvdum@2
     4
import static org.junit.Assert.*
indvdum@2
     5
indvdum@2
     6
import java.lang.reflect.Field
indvdum@2
     7
import java.lang.reflect.Method
indvdum@2
     8
import java.lang.reflect.Modifier
indvdum@2
     9
indvdum@2
    10
import javax.persistence.EmbeddedId
indvdum@2
    11
import javax.persistence.Entity
indvdum@2
    12
import javax.persistence.GeneratedValue
indvdum@2
    13
import javax.persistence.Id
indvdum@2
    14
import javax.persistence.IdClass
indvdum@2
    15
import javax.persistence.Transient
indvdum@2
    16
indvdum@2
    17
import org.junit.Test
indvdum@2
    18
indvdum@2
    19
import ru.indvdum.jpa.dao.JPADataAccessObject
indvdum@2
    20
import ru.indvdum.jpa.entities.AbstractEntity;
indvdum@2
    21
indvdum@2
    22
indvdum@2
    23
/**
indvdum@2
    24
 * JUnit test case for testing of a creating, listing, updating and removing 
indvdum@2
    25
 * operations with database of the JPA entities.
indvdum@2
    26
 * 
indvdum@2
    27
 * @author 	indvdum (gotoindvdum@gmail.com)
indvdum@2
    28
 * @since 23.12.2011 22:54:26
indvdum@2
    29
 *
indvdum@2
    30
 */
indvdum@2
    31
abstract class AbstractJPAEntityTest extends AbstractJPATest {
indvdum@2
    32
indvdum@2
    33
	protected def uniqueValue = 1
indvdum@2
    34
	protected JPADataAccessObject dao = null
indvdum@2
    35
	protected Set toRemove = new HashSet()
indvdum@10
    36
	protected Map<Class, Integer> existedEntitiesCount = [:]
indvdum@2
    37
indvdum@2
    38
	@Test
indvdum@2
    39
	public void testEntity() {
indvdum@2
    40
		dao = createDAO()
indvdum@2
    41
		try {
indvdum@2
    42
			testEntity(getEntityClass())
indvdum@10
    43
		} catch (Throwable t) {
indvdum@10
    44
			dao.rollback()
indvdum@10
    45
			throw t
indvdum@2
    46
		} finally {
indvdum@2
    47
			dao.close()		
indvdum@2
    48
		}
indvdum@2
    49
	}
indvdum@2
    50
	
indvdum@2
    51
	/**
indvdum@2
    52
	 * @return Your implementation of JPADataAccessObject
indvdum@2
    53
	 */
indvdum@2
    54
	protected abstract JPADataAccessObject createDAO();
indvdum@2
    55
indvdum@2
    56
	/**
indvdum@2
    57
	 * Test creating, listing, updating, and removing of an {@code entityClass} object
indvdum@2
    58
	 * 
indvdum@2
    59
	 * @param entityClass
indvdum@2
    60
	 */
indvdum@2
    61
	protected void testEntity(Class entityClass) {
indvdum@2
    62
		
indvdum@2
    63
		assert AbstractEntity.class.isAssignableFrom(entityClass)
indvdum@2
    64
indvdum@2
    65
		Map<String, Object> fieldsValues
indvdum@2
    66
		def dbEntity
indvdum@2
    67
indvdum@2
    68
		// creating
indvdum@2
    69
		def entity = createEntity(entityClass)
indvdum@2
    70
		testCreatedEntity(entity)
indvdum@2
    71
indvdum@2
    72
		// listing
indvdum@2
    73
		toRemove.each {
indvdum@2
    74
			assert dao.list(it.class).each { it2 ->
indvdum@2
    75
				assert it.class.isAssignableFrom(it2.class)
indvdum@2
    76
			}.size() == toRemove.findAll { it2 ->
indvdum@2
    77
				it.class.isAssignableFrom(it2.class)
indvdum@10
    78
			}.size() + existedEntitiesCount[it.class]
indvdum@2
    79
		}
indvdum@2
    80
indvdum@2
    81
		// updating
indvdum@2
    82
		fieldsValues = updateFields(entity)
indvdum@2
    83
		assert dao.persist(entity)
indvdum@2
    84
		assert dao.contains(entity)
indvdum@2
    85
		dbEntity = dao.find(entityClass, (entity as AbstractEntity).getIdentifierValue())
indvdum@2
    86
		assert dbEntity != null
indvdum@2
    87
		assert checkEntityFieldValues(dbEntity, fieldsValues)
indvdum@2
    88
		testUpdatedEntity(entity)
indvdum@2
    89
indvdum@2
    90
		// removing
indvdum@2
    91
		assert dao.remove(toRemove)
indvdum@2
    92
		assert !dao.contains(toRemove)
indvdum@2
    93
		testRemovedEntity(entity)
indvdum@2
    94
	}
indvdum@2
    95
	
indvdum@2
    96
	/**
indvdum@2
    97
	 * Create and persist to database an {@code entityClass} object
indvdum@2
    98
	 * 
indvdum@2
    99
	 * @param entityClass
indvdum@2
   100
	 * @return created entity object
indvdum@2
   101
	 */
indvdum@2
   102
	protected Object createEntity(Class entityClass) {
indvdum@2
   103
		assertNotNull entityClass.annotations.find {it instanceof Entity}
indvdum@2
   104
		def entity = entityClass.newInstance()
indvdum@2
   105
		assert entity.class == entityClass
indvdum@2
   106
		toRemove.add(entity)
indvdum@10
   107
		if (!existedEntitiesCount.containsKey(entityClass))
indvdum@10
   108
			existedEntitiesCount[entityClass] = dao.list(entityClass).size()
indvdum@2
   109
		
indvdum@2
   110
		Map<String, Object> fieldsValues = updateFields(entity)
indvdum@2
   111
		assert dao.persist(entity)
indvdum@2
   112
		assert dao.contains(entity)
indvdum@2
   113
		def dbEntity = dao.find(entityClass, (entity as AbstractEntity).getIdentifierValue())
indvdum@2
   114
		assert dbEntity != null
indvdum@2
   115
		assert checkEntityFieldValues(dbEntity, fieldsValues)
indvdum@2
   116
		
indvdum@2
   117
		return entity
indvdum@2
   118
	}
indvdum@2
   119
indvdum@2
   120
	/**
indvdum@2
   121
	 * Update all fields of an {@code entity} object
indvdum@2
   122
	 * @param entity
indvdum@2
   123
	 * @return generated field values
indvdum@2
   124
	 */
indvdum@2
   125
	protected Map<String, Object> updateFields(Object entity) {
indvdum@2
   126
		Map<String, Object> fieldsValues = new HashMap<String, Object>()
indvdum@2
   127
		getFields(entity).grep {
indvdum@2
   128
			it.annotations.find {
indvdum@2
   129
				it instanceof Transient || it instanceof GeneratedValue
indvdum@2
   130
			} == null
indvdum@2
   131
		}.each {
indvdum@2
   132
			def newValue = generateFieldValue(entity, it)
indvdum@2
   133
			setFieldValueBySetter(entity, it, newValue)
indvdum@2
   134
			fieldsValues.put(it.name, newValue)
indvdum@2
   135
		}
indvdum@2
   136
		return fieldsValues
indvdum@2
   137
	}
indvdum@2
   138
	
indvdum@2
   139
	/**
indvdum@2
   140
	 * Collections and arrays will not be processed
indvdum@2
   141
	 * 
indvdum@2
   142
	 * @param entity
indvdum@2
   143
	 * @param field
indvdum@2
   144
	 * @return generated field value
indvdum@2
   145
	 */
indvdum@2
   146
	protected Object generateFieldValue(Object entity, Field field) {
indvdum@2
   147
		def type = field.getType()
indvdum@2
   148
		def newValue
indvdum@2
   149
		if(type.toString() == 'boolean' || type == Boolean.class) {
indvdum@2
   150
			newValue = (boolean) (uniqueValue++ % 2i == 0i)
indvdum@2
   151
		} else if(type.toString() == 'byte' || type == Byte) {
indvdum@2
   152
			newValue = (byte) (uniqueValue++ % Byte.MAX_VALUE + 1i)
indvdum@2
   153
		} else if(type.toString() == 'char' || type == Character) {
indvdum@2
   154
			newValue = (char) (uniqueValue++ % (int) Character.MAX_VALUE + 1i)
indvdum@2
   155
		} else if(type.toString() == 'short' || type == Short) {
indvdum@2
   156
			newValue = (short) (uniqueValue++ % Short.MAX_VALUE + 1i)
indvdum@2
   157
		} else if(type.toString() == 'int' || type == Integer) {
indvdum@2
   158
			newValue = (int) (uniqueValue++ % Integer.MAX_VALUE + 1i)
indvdum@2
   159
		} else if(type.toString() == 'long' || type == Long) {
indvdum@2
   160
			newValue = (long) uniqueValue++ % Long.MAX_VALUE + 1L
indvdum@2
   161
		} else if(type.toString() == 'float' || type == Float) {
indvdum@2
   162
			newValue = (float) uniqueValue++ % Float.MAX_VALUE + 1f
indvdum@2
   163
		} else if(type.toString() == 'double' || type == Double) {
indvdum@2
   164
			newValue = (double) uniqueValue++ % Double.MAX_VALUE + 1d
indvdum@2
   165
		} else if(type == String.class) {
indvdum@2
   166
			newValue = (String) "test${uniqueValue++}"
indvdum@2
   167
		} else if(type instanceof Class && (type as Class).annotations.find {it instanceof Entity} != null) { // modifying of a primary keys is deprecated
indvdum@2
   168
			// an attempt to use already created entities
indvdum@2
   169
			def currentValue = getFieldValue(entity, field)
indvdum@2
   170
			newValue = toRemove.find {it.class == type && it != currentValue}
indvdum@2
   171
			if(newValue == null)
indvdum@2
   172
				newValue = createEntity(type as Class)
indvdum@2
   173
		} else if(
indvdum@2
   174
				type instanceof Class 
indvdum@2
   175
				&& !(
indvdum@2
   176
					field.clazz.annotations.find {it instanceof IdClass} != null 
indvdum@2
   177
					&& field.declaredAnnotations.find {it instanceof Id} != null
indvdum@2
   178
				)
indvdum@2
   179
				&& field.declaredAnnotations.find {it instanceof EmbeddedId} == null
indvdum@2
   180
				&& !Collection.class.isAssignableFrom(type)
indvdum@2
   181
				&& !(type as Class).isArray()
indvdum@2
   182
			) { // modifying of a primary keys is deprecated
indvdum@2
   183
			newValue = (type as Class).newInstance()
indvdum@2
   184
		} else {
indvdum@2
   185
			newValue = getFieldValue(entity, field)
indvdum@2
   186
		}
indvdum@2
   187
		return newValue
indvdum@2
   188
	}
indvdum@2
   189
indvdum@2
   190
	/**
indvdum@2
   191
	 * @param entity
indvdum@2
   192
	 * @param rightValues
indvdum@2
   193
	 * @return {@code true}, if all entity fields is equals to {@code rightValues}
indvdum@2
   194
	 */
indvdum@2
   195
	protected boolean checkEntityFieldValues(Object entity, Map<String, Object> rightValues) {
indvdum@2
   196
		def result = true
indvdum@2
   197
		def fields = getFields(entity)
indvdum@2
   198
		rightValues.each {  key, value ->
indvdum@2
   199
			fields.find { it.name == key }.each {
indvdum@2
   200
				if(!getFieldValue(entity, it).equals(value)) {
indvdum@2
   201
					result = false
indvdum@2
   202
				}
indvdum@2
   203
			}
indvdum@2
   204
		}
indvdum@2
   205
		return result
indvdum@2
   206
	}
indvdum@2
   207
indvdum@2
   208
	/**
indvdum@2
   209
	 * Set a {@code field} to a {@code value} of an entity {@code object}
indvdum@2
   210
	 * 
indvdum@2
   211
	 * @param object
indvdum@2
   212
	 * @param field
indvdum@2
   213
	 * @param value
indvdum@2
   214
	 */
indvdum@2
   215
	protected void setFieldValue(Object object, Field field, Object value) {
indvdum@2
   216
		boolean isAccessible = field.accessible
indvdum@2
   217
		field.accessible = true
indvdum@2
   218
		field.set(object, value)
indvdum@2
   219
		field.accessible = isAccessible
indvdum@2
   220
	}
indvdum@2
   221
	
indvdum@2
   222
	/**
indvdum@2
   223
	 * Trying set a {@code field} to a {@code value} of an entity {@code object}
indvdum@2
   224
	 * by using a setter method. If setter not found, field will be set directly
indvdum@2
   225
	 * throw {@code setFieldValue} method.
indvdum@2
   226
	 * 
indvdum@2
   227
	 * @param object
indvdum@2
   228
	 * @param field
indvdum@2
   229
	 * @param value
indvdum@2
   230
	 */
indvdum@2
   231
	protected void setFieldValueBySetter(Object object, Field field, Object value) {
indvdum@2
   232
		boolean isSetted = false
indvdum@2
   233
		getMethods(object).grep {
indvdum@2
   234
			(
indvdum@2
   235
				Modifier.isPublic(it.modifiers) 
indvdum@2
   236
				&& it.parameterTypes.size() == 1 
indvdum@2
   237
				&& (
indvdum@2
   238
					value == null 
indvdum@2
   239
					|| it.parameterTypes[0].isAssignableFrom(value.class)
indvdum@2
   240
					)
indvdum@2
   241
				&& it.name =~ /^set(?i:${field.name.charAt(0)})${field.name.replaceFirst("^.{1}", "")}/
indvdum@2
   242
			)
indvdum@2
   243
		}.each {
indvdum@2
   244
			object."${it.name}"(value)
indvdum@2
   245
			isSetted = true
indvdum@2
   246
		}
indvdum@2
   247
		if(isSetted)
indvdum@2
   248
			return
indvdum@2
   249
		setFieldValue(object, field, value)
indvdum@2
   250
	}
indvdum@2
   251
indvdum@2
   252
	/**
indvdum@2
   253
	 * @param object
indvdum@2
   254
	 * @param field
indvdum@2
   255
	 * @return the field value of an object
indvdum@2
   256
	 */
indvdum@2
   257
	protected Object getFieldValue(Object object, Field field) {
indvdum@2
   258
		boolean isAccessible = field.accessible
indvdum@2
   259
		field.accessible = true
indvdum@2
   260
		def value = field.get(object)
indvdum@2
   261
		field.accessible = isAccessible
indvdum@2
   262
		return value
indvdum@2
   263
	}
indvdum@2
   264
indvdum@2
   265
	/**
indvdum@2
   266
	 * @param obj
indvdum@2
   267
	 * @return all object fields, including inherited
indvdum@2
   268
	 */
indvdum@2
   269
	protected Collection<Field> getFields(obj) {
indvdum@2
   270
		Class clazz = obj.getClass()
indvdum@2
   271
		Collection<Field> fields = clazz.declaredFields
indvdum@2
   272
		while(clazz.superclass != null) {
indvdum@2
   273
			clazz = clazz.superclass
indvdum@2
   274
			fields.addAll(clazz.declaredFields)
indvdum@2
   275
		}
indvdum@2
   276
		fields.grep {
indvdum@2
   277
			!it.synthetic &&
indvdum@2
   278
					!Modifier.isStatic(it.modifiers) &&
indvdum@2
   279
					!Modifier.isTransient(it.modifiers)
indvdum@2
   280
		}
indvdum@2
   281
	}
indvdum@2
   282
indvdum@2
   283
	/**
indvdum@2
   284
	 * @param obj
indvdum@2
   285
	 * @return all object methods, including inherited
indvdum@2
   286
	 */
indvdum@2
   287
	protected Collection<Method> getMethods(obj) {
indvdum@2
   288
		Class clazz = obj.getClass()
indvdum@2
   289
		Collection<Method> methods = clazz.declaredMethods
indvdum@2
   290
		while(clazz.superclass != null) {
indvdum@2
   291
			clazz = clazz.superclass
indvdum@2
   292
			methods.addAll(clazz.declaredMethods)
indvdum@2
   293
		}
indvdum@2
   294
		methods.grep {
indvdum@2
   295
			!it.synthetic &&
indvdum@2
   296
					!Modifier.isStatic(it.modifiers) &&
indvdum@2
   297
					!Modifier.isTransient(it.modifiers)
indvdum@2
   298
		}
indvdum@2
   299
	}
indvdum@2
   300
	
indvdum@2
   301
	/**
indvdum@2
   302
	 * For implement in successors
indvdum@2
   303
	 *
indvdum@2
   304
	 * @param entity
indvdum@2
   305
	 */
indvdum@2
   306
	protected void testCreatedEntity(Object entity) {
indvdum@2
   307
		
indvdum@2
   308
	}
indvdum@2
   309
	
indvdum@2
   310
	/**
indvdum@2
   311
	 * For implement in successors
indvdum@2
   312
	 *
indvdum@2
   313
	 * @param entity
indvdum@2
   314
	 */
indvdum@2
   315
	protected void testUpdatedEntity(Object entity) {
indvdum@2
   316
		
indvdum@2
   317
	}
indvdum@2
   318
	
indvdum@2
   319
	/**
indvdum@2
   320
	 * For implement in successors
indvdum@2
   321
	 *
indvdum@2
   322
	 * @param entity
indvdum@2
   323
	 */
indvdum@2
   324
	protected void testRemovedEntity(Object entity) {
indvdum@2
   325
		
indvdum@2
   326
	}
indvdum@2
   327
indvdum@2
   328
	/**
indvdum@2
   329
	 * @return an {@link AbstractEntity} successor
indvdum@2
   330
	 */
indvdum@2
   331
	abstract protected Class getEntityClass()
indvdum@2
   332
}