src/test/java/ru/indvdum/jpa/tests/AbstractJPAEntityTest.groovy
changeset 2 f692598b3742
child 10 7241826f43f6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/test/java/ru/indvdum/jpa/tests/AbstractJPAEntityTest.groovy	Fri Nov 09 03:00:49 2012 +0400
     1.3 @@ -0,0 +1,326 @@
     1.4 +package ru.indvdum.jpa.tests
     1.5 +;
     1.6 +
     1.7 +import static org.junit.Assert.*
     1.8 +
     1.9 +import java.lang.reflect.Field
    1.10 +import java.lang.reflect.Method
    1.11 +import java.lang.reflect.Modifier
    1.12 +
    1.13 +import javax.persistence.EmbeddedId
    1.14 +import javax.persistence.Entity
    1.15 +import javax.persistence.GeneratedValue
    1.16 +import javax.persistence.Id
    1.17 +import javax.persistence.IdClass
    1.18 +import javax.persistence.Transient
    1.19 +
    1.20 +import org.junit.Test
    1.21 +
    1.22 +import ru.indvdum.jpa.dao.JPADataAccessObject
    1.23 +import ru.indvdum.jpa.entities.AbstractEntity;
    1.24 +
    1.25 +
    1.26 +/**
    1.27 + * JUnit test case for testing of a creating, listing, updating and removing 
    1.28 + * operations with database of the JPA entities.
    1.29 + * 
    1.30 + * @author 	indvdum (gotoindvdum@gmail.com)
    1.31 + * @since 23.12.2011 22:54:26
    1.32 + *
    1.33 + */
    1.34 +abstract class AbstractJPAEntityTest extends AbstractJPATest {
    1.35 +
    1.36 +	protected def uniqueValue = 1
    1.37 +	protected JPADataAccessObject dao = null
    1.38 +	protected Set toRemove = new HashSet()
    1.39 +
    1.40 +	@Test
    1.41 +	public void testEntity() {
    1.42 +		dao = createDAO()
    1.43 +		try {
    1.44 +			testEntity(getEntityClass())
    1.45 +		} finally {
    1.46 +			dao.close()		
    1.47 +		}
    1.48 +	}
    1.49 +	
    1.50 +	/**
    1.51 +	 * @return Your implementation of JPADataAccessObject
    1.52 +	 */
    1.53 +	protected abstract JPADataAccessObject createDAO();
    1.54 +
    1.55 +	/**
    1.56 +	 * Test creating, listing, updating, and removing of an {@code entityClass} object
    1.57 +	 * 
    1.58 +	 * @param entityClass
    1.59 +	 */
    1.60 +	protected void testEntity(Class entityClass) {
    1.61 +		
    1.62 +		assert AbstractEntity.class.isAssignableFrom(entityClass)
    1.63 +
    1.64 +		Map<String, Object> fieldsValues
    1.65 +		def dbEntity
    1.66 +
    1.67 +		// creating
    1.68 +		def entity = createEntity(entityClass)
    1.69 +		testCreatedEntity(entity)
    1.70 +
    1.71 +		// listing
    1.72 +		toRemove.each {
    1.73 +			assert dao.list(it.class).each { it2 ->
    1.74 +				assert it.class.isAssignableFrom(it2.class)
    1.75 +			}.size() == toRemove.findAll { it2 ->
    1.76 +				it.class.isAssignableFrom(it2.class)
    1.77 +			}.size()
    1.78 +		}
    1.79 +
    1.80 +		// updating
    1.81 +		fieldsValues = updateFields(entity)
    1.82 +		assert dao.persist(entity)
    1.83 +		assert dao.contains(entity)
    1.84 +		dbEntity = dao.find(entityClass, (entity as AbstractEntity).getIdentifierValue())
    1.85 +		assert dbEntity != null
    1.86 +		assert checkEntityFieldValues(dbEntity, fieldsValues)
    1.87 +		testUpdatedEntity(entity)
    1.88 +
    1.89 +		// removing
    1.90 +		assert dao.remove(toRemove)
    1.91 +		assert !dao.contains(toRemove)
    1.92 +		testRemovedEntity(entity)
    1.93 +	}
    1.94 +	
    1.95 +	/**
    1.96 +	 * Create and persist to database an {@code entityClass} object
    1.97 +	 * 
    1.98 +	 * @param entityClass
    1.99 +	 * @return created entity object
   1.100 +	 */
   1.101 +	protected Object createEntity(Class entityClass) {
   1.102 +		assertNotNull entityClass.annotations.find {it instanceof Entity}
   1.103 +		def entity = entityClass.newInstance()
   1.104 +		assert entity.class == entityClass
   1.105 +		toRemove.add(entity)
   1.106 +		
   1.107 +		Map<String, Object> fieldsValues = updateFields(entity)
   1.108 +		assert dao.persist(entity)
   1.109 +		assert dao.contains(entity)
   1.110 +		def dbEntity = dao.find(entityClass, (entity as AbstractEntity).getIdentifierValue())
   1.111 +		assert dbEntity != null
   1.112 +		assert checkEntityFieldValues(dbEntity, fieldsValues)
   1.113 +		
   1.114 +		return entity
   1.115 +	}
   1.116 +
   1.117 +	/**
   1.118 +	 * Update all fields of an {@code entity} object
   1.119 +	 * @param entity
   1.120 +	 * @return generated field values
   1.121 +	 */
   1.122 +	protected Map<String, Object> updateFields(Object entity) {
   1.123 +		Map<String, Object> fieldsValues = new HashMap<String, Object>()
   1.124 +		getFields(entity).grep {
   1.125 +			it.annotations.find {
   1.126 +				it instanceof Transient || it instanceof GeneratedValue
   1.127 +			} == null
   1.128 +		}.each {
   1.129 +			def newValue = generateFieldValue(entity, it)
   1.130 +			setFieldValueBySetter(entity, it, newValue)
   1.131 +			fieldsValues.put(it.name, newValue)
   1.132 +		}
   1.133 +		return fieldsValues
   1.134 +	}
   1.135 +	
   1.136 +	/**
   1.137 +	 * Collections and arrays will not be processed
   1.138 +	 * 
   1.139 +	 * @param entity
   1.140 +	 * @param field
   1.141 +	 * @return generated field value
   1.142 +	 */
   1.143 +	protected Object generateFieldValue(Object entity, Field field) {
   1.144 +		def type = field.getType()
   1.145 +		def newValue
   1.146 +		if(type.toString() == 'boolean' || type == Boolean.class) {
   1.147 +			newValue = (boolean) (uniqueValue++ % 2i == 0i)
   1.148 +		} else if(type.toString() == 'byte' || type == Byte) {
   1.149 +			newValue = (byte) (uniqueValue++ % Byte.MAX_VALUE + 1i)
   1.150 +		} else if(type.toString() == 'char' || type == Character) {
   1.151 +			newValue = (char) (uniqueValue++ % (int) Character.MAX_VALUE + 1i)
   1.152 +		} else if(type.toString() == 'short' || type == Short) {
   1.153 +			newValue = (short) (uniqueValue++ % Short.MAX_VALUE + 1i)
   1.154 +		} else if(type.toString() == 'int' || type == Integer) {
   1.155 +			newValue = (int) (uniqueValue++ % Integer.MAX_VALUE + 1i)
   1.156 +		} else if(type.toString() == 'long' || type == Long) {
   1.157 +			newValue = (long) uniqueValue++ % Long.MAX_VALUE + 1L
   1.158 +		} else if(type.toString() == 'float' || type == Float) {
   1.159 +			newValue = (float) uniqueValue++ % Float.MAX_VALUE + 1f
   1.160 +		} else if(type.toString() == 'double' || type == Double) {
   1.161 +			newValue = (double) uniqueValue++ % Double.MAX_VALUE + 1d
   1.162 +		} else if(type == String.class) {
   1.163 +			newValue = (String) "test${uniqueValue++}"
   1.164 +		} else if(type instanceof Class && (type as Class).annotations.find {it instanceof Entity} != null) { // modifying of a primary keys is deprecated
   1.165 +			// an attempt to use already created entities
   1.166 +			def currentValue = getFieldValue(entity, field)
   1.167 +			newValue = toRemove.find {it.class == type && it != currentValue}
   1.168 +			if(newValue == null)
   1.169 +				newValue = createEntity(type as Class)
   1.170 +		} else if(
   1.171 +				type instanceof Class 
   1.172 +				&& !(
   1.173 +					field.clazz.annotations.find {it instanceof IdClass} != null 
   1.174 +					&& field.declaredAnnotations.find {it instanceof Id} != null
   1.175 +				)
   1.176 +				&& field.declaredAnnotations.find {it instanceof EmbeddedId} == null
   1.177 +				&& !Collection.class.isAssignableFrom(type)
   1.178 +				&& !(type as Class).isArray()
   1.179 +			) { // modifying of a primary keys is deprecated
   1.180 +			newValue = (type as Class).newInstance()
   1.181 +		} else {
   1.182 +			newValue = getFieldValue(entity, field)
   1.183 +		}
   1.184 +		return newValue
   1.185 +	}
   1.186 +
   1.187 +	/**
   1.188 +	 * @param entity
   1.189 +	 * @param rightValues
   1.190 +	 * @return {@code true}, if all entity fields is equals to {@code rightValues}
   1.191 +	 */
   1.192 +	protected boolean checkEntityFieldValues(Object entity, Map<String, Object> rightValues) {
   1.193 +		def result = true
   1.194 +		def fields = getFields(entity)
   1.195 +		rightValues.each {  key, value ->
   1.196 +			fields.find { it.name == key }.each {
   1.197 +				if(!getFieldValue(entity, it).equals(value)) {
   1.198 +					result = false
   1.199 +				}
   1.200 +			}
   1.201 +		}
   1.202 +		return result
   1.203 +	}
   1.204 +
   1.205 +	/**
   1.206 +	 * Set a {@code field} to a {@code value} of an entity {@code object}
   1.207 +	 * 
   1.208 +	 * @param object
   1.209 +	 * @param field
   1.210 +	 * @param value
   1.211 +	 */
   1.212 +	protected void setFieldValue(Object object, Field field, Object value) {
   1.213 +		boolean isAccessible = field.accessible
   1.214 +		field.accessible = true
   1.215 +		field.set(object, value)
   1.216 +		field.accessible = isAccessible
   1.217 +	}
   1.218 +	
   1.219 +	/**
   1.220 +	 * Trying set a {@code field} to a {@code value} of an entity {@code object}
   1.221 +	 * by using a setter method. If setter not found, field will be set directly
   1.222 +	 * throw {@code setFieldValue} method.
   1.223 +	 * 
   1.224 +	 * @param object
   1.225 +	 * @param field
   1.226 +	 * @param value
   1.227 +	 */
   1.228 +	protected void setFieldValueBySetter(Object object, Field field, Object value) {
   1.229 +		boolean isSetted = false
   1.230 +		getMethods(object).grep {
   1.231 +			(
   1.232 +				Modifier.isPublic(it.modifiers) 
   1.233 +				&& it.parameterTypes.size() == 1 
   1.234 +				&& (
   1.235 +					value == null 
   1.236 +					|| it.parameterTypes[0].isAssignableFrom(value.class)
   1.237 +					)
   1.238 +				&& it.name =~ /^set(?i:${field.name.charAt(0)})${field.name.replaceFirst("^.{1}", "")}/
   1.239 +			)
   1.240 +		}.each {
   1.241 +			object."${it.name}"(value)
   1.242 +			isSetted = true
   1.243 +		}
   1.244 +		if(isSetted)
   1.245 +			return
   1.246 +		setFieldValue(object, field, value)
   1.247 +	}
   1.248 +
   1.249 +	/**
   1.250 +	 * @param object
   1.251 +	 * @param field
   1.252 +	 * @return the field value of an object
   1.253 +	 */
   1.254 +	protected Object getFieldValue(Object object, Field field) {
   1.255 +		boolean isAccessible = field.accessible
   1.256 +		field.accessible = true
   1.257 +		def value = field.get(object)
   1.258 +		field.accessible = isAccessible
   1.259 +		return value
   1.260 +	}
   1.261 +
   1.262 +	/**
   1.263 +	 * @param obj
   1.264 +	 * @return all object fields, including inherited
   1.265 +	 */
   1.266 +	protected Collection<Field> getFields(obj) {
   1.267 +		Class clazz = obj.getClass()
   1.268 +		Collection<Field> fields = clazz.declaredFields
   1.269 +		while(clazz.superclass != null) {
   1.270 +			clazz = clazz.superclass
   1.271 +			fields.addAll(clazz.declaredFields)
   1.272 +		}
   1.273 +		fields.grep {
   1.274 +			!it.synthetic &&
   1.275 +					!Modifier.isStatic(it.modifiers) &&
   1.276 +					!Modifier.isTransient(it.modifiers)
   1.277 +		}
   1.278 +	}
   1.279 +
   1.280 +	/**
   1.281 +	 * @param obj
   1.282 +	 * @return all object methods, including inherited
   1.283 +	 */
   1.284 +	protected Collection<Method> getMethods(obj) {
   1.285 +		Class clazz = obj.getClass()
   1.286 +		Collection<Method> methods = clazz.declaredMethods
   1.287 +		while(clazz.superclass != null) {
   1.288 +			clazz = clazz.superclass
   1.289 +			methods.addAll(clazz.declaredMethods)
   1.290 +		}
   1.291 +		methods.grep {
   1.292 +			!it.synthetic &&
   1.293 +					!Modifier.isStatic(it.modifiers) &&
   1.294 +					!Modifier.isTransient(it.modifiers)
   1.295 +		}
   1.296 +	}
   1.297 +	
   1.298 +	/**
   1.299 +	 * For implement in successors
   1.300 +	 *
   1.301 +	 * @param entity
   1.302 +	 */
   1.303 +	protected void testCreatedEntity(Object entity) {
   1.304 +		
   1.305 +	}
   1.306 +	
   1.307 +	/**
   1.308 +	 * For implement in successors
   1.309 +	 *
   1.310 +	 * @param entity
   1.311 +	 */
   1.312 +	protected void testUpdatedEntity(Object entity) {
   1.313 +		
   1.314 +	}
   1.315 +	
   1.316 +	/**
   1.317 +	 * For implement in successors
   1.318 +	 *
   1.319 +	 * @param entity
   1.320 +	 */
   1.321 +	protected void testRemovedEntity(Object entity) {
   1.322 +		
   1.323 +	}
   1.324 +
   1.325 +	/**
   1.326 +	 * @return an {@link AbstractEntity} successor
   1.327 +	 */
   1.328 +	abstract protected Class getEntityClass()
   1.329 +}