1 package ru.indvdum.jpa.tests
4 import static org.junit.Assert.*
6 import java.lang.reflect.Field
7 import java.lang.reflect.Method
8 import java.lang.reflect.Modifier
10 import javax.persistence.EmbeddedId
11 import javax.persistence.Entity
12 import javax.persistence.GeneratedValue
13 import javax.persistence.Id
14 import javax.persistence.IdClass
15 import javax.persistence.Transient
19 import ru.indvdum.jpa.dao.JPADataAccessObject
20 import ru.indvdum.jpa.entities.AbstractEntity;
24 * JUnit test case for testing of a creating, listing, updating and removing
25 * operations with database of the JPA entities.
27 * @author indvdum (gotoindvdum@gmail.com)
28 * @since 23.12.2011 22:54:26
31 abstract class AbstractJPAEntityTest extends AbstractJPATest {
33 protected def uniqueValue = 1
34 protected JPADataAccessObject dao = null
35 protected Set toRemove = new HashSet()
36 protected Map<Class, Integer> existedEntitiesCount = [:]
39 public void testEntity() {
42 testEntity(getEntityClass())
43 } catch (Throwable t) {
52 * @return Your implementation of JPADataAccessObject
54 protected abstract JPADataAccessObject createDAO();
57 * Test creating, listing, updating, and removing of an {@code entityClass} object
61 protected void testEntity(Class entityClass) {
63 assert AbstractEntity.class.isAssignableFrom(entityClass)
65 Map<String, Object> fieldsValues
69 def entity = createEntity(entityClass)
70 testCreatedEntity(entity)
74 assert dao.list(it.class).each { it2 ->
75 assert it.class.isAssignableFrom(it2.class)
76 }.size() == toRemove.findAll { it2 ->
77 it.class.isAssignableFrom(it2.class)
78 }.size() + existedEntitiesCount[it.class]
82 fieldsValues = updateFields(entity)
83 assert dao.persist(entity)
84 assert dao.contains(entity)
85 dbEntity = dao.find(entityClass, (entity as AbstractEntity).getIdentifierValue())
86 assert dbEntity != null
87 assert checkEntityFieldValues(dbEntity, fieldsValues)
88 testUpdatedEntity(entity)
91 assert dao.remove(toRemove)
92 assert !dao.contains(toRemove)
93 testRemovedEntity(entity)
97 * Create and persist to database an {@code entityClass} object
100 * @return created entity object
102 protected Object createEntity(Class entityClass) {
103 assertNotNull entityClass.annotations.find {it instanceof Entity}
104 def entity = entityClass.newInstance()
105 assert entity.class == entityClass
107 if (!existedEntitiesCount.containsKey(entityClass))
108 existedEntitiesCount[entityClass] = dao.list(entityClass).size()
110 Map<String, Object> fieldsValues = updateFields(entity)
111 assert dao.persist(entity)
112 assert dao.contains(entity)
113 def dbEntity = dao.find(entityClass, (entity as AbstractEntity).getIdentifierValue())
114 assert dbEntity != null
115 assert checkEntityFieldValues(dbEntity, fieldsValues)
121 * Update all fields of an {@code entity} object
123 * @return generated field values
125 protected Map<String, Object> updateFields(Object entity) {
126 Map<String, Object> fieldsValues = new HashMap<String, Object>()
127 getFields(entity).grep {
128 it.annotations.find {
129 it instanceof Transient || it instanceof GeneratedValue
132 def newValue = generateFieldValue(entity, it)
133 setFieldValueBySetter(entity, it, newValue)
134 fieldsValues.put(it.name, newValue)
140 * Collections and arrays will not be processed
144 * @return generated field value
146 protected Object generateFieldValue(Object entity, Field field) {
147 def type = field.getType()
149 if(type.toString() == 'boolean' || type == Boolean.class) {
150 newValue = (boolean) (uniqueValue++ % 2i == 0i)
151 } else if(type.toString() == 'byte' || type == Byte) {
152 newValue = (byte) (uniqueValue++ % Byte.MAX_VALUE + 1i)
153 } else if(type.toString() == 'char' || type == Character) {
154 newValue = (char) (uniqueValue++ % (int) Character.MAX_VALUE + 1i)
155 } else if(type.toString() == 'short' || type == Short) {
156 newValue = (short) (uniqueValue++ % Short.MAX_VALUE + 1i)
157 } else if(type.toString() == 'int' || type == Integer) {
158 newValue = (int) (uniqueValue++ % Integer.MAX_VALUE + 1i)
159 } else if(type.toString() == 'long' || type == Long) {
160 newValue = (long) uniqueValue++ % Long.MAX_VALUE + 1L
161 } else if(type.toString() == 'float' || type == Float) {
162 newValue = (float) uniqueValue++ % Float.MAX_VALUE + 1f
163 } else if(type.toString() == 'double' || type == Double) {
164 newValue = (double) uniqueValue++ % Double.MAX_VALUE + 1d
165 } else if(Number.class.isAssignableFrom(type)) {
166 // trying to use constructor with int argument
167 newValue = type.newInstance(uniqueValue++ % Byte.MAX_VALUE + 1i)
168 } else if(type == String.class) {
169 newValue = (String) "test${uniqueValue++}"
170 } else if(type instanceof Class && (type as Class).annotations.find {it instanceof Entity} != null) { // modifying of a primary keys is deprecated
171 // an attempt to use already created entities
172 def currentValue = getFieldValue(entity, field)
173 newValue = toRemove.find {it.class == type && it != currentValue}
175 newValue = createEntity(type as Class)
176 } else if(Enum.class.isAssignableFrom(type)) {
177 def values = type.values();
178 newValue = values[uniqueValue++ % values.size()];
180 type instanceof Class
182 field.clazz.annotations.find {it instanceof IdClass} != null
183 && field.declaredAnnotations.find {it instanceof Id} != null
185 && field.declaredAnnotations.find {it instanceof EmbeddedId} == null
186 && !Collection.class.isAssignableFrom(type)
187 && !(type as Class).isArray()
188 ) { // modifying of a primary keys is deprecated
189 newValue = (type as Class).newInstance()
191 newValue = getFieldValue(entity, field)
199 * @return {@code true}, if all entity fields is equals to {@code rightValues}
201 protected boolean checkEntityFieldValues(Object entity, Map<String, Object> rightValues) {
203 def fields = getFields(entity)
204 rightValues.each { key, value ->
205 fields.find { it.name == key }.each {
206 if(!getFieldValue(entity, it).equals(value)) {
215 * Set a {@code field} to a {@code value} of an entity {@code object}
221 protected void setFieldValue(Object object, Field field, Object value) {
222 boolean isAccessible = field.accessible
223 field.accessible = true
224 field.set(object, value)
225 field.accessible = isAccessible
229 * Trying set a {@code field} to a {@code value} of an entity {@code object}
230 * by using a setter method. If setter not found, field will be set directly
231 * throw {@code setFieldValue} method.
237 protected void setFieldValueBySetter(Object object, Field field, Object value) {
238 boolean isSetted = false
239 getMethods(object).grep {
241 Modifier.isPublic(it.modifiers)
242 && it.parameterTypes.size() == 1
245 || it.parameterTypes[0].isAssignableFrom(value.class)
247 && it.name =~ /^set(?i:${field.name.charAt(0)})${field.name.replaceFirst("^.{1}", "")}/
250 object."${it.name}"(value)
255 setFieldValue(object, field, value)
261 * @return the field value of an object
263 protected Object getFieldValue(Object object, Field field) {
264 boolean isAccessible = field.accessible
265 field.accessible = true
266 def value = field.get(object)
267 field.accessible = isAccessible
273 * @return all object fields, including inherited
275 protected Collection<Field> getFields(obj) {
276 Class clazz = obj.getClass()
277 Collection<Field> fields = clazz.declaredFields
278 while(clazz.superclass != null) {
279 clazz = clazz.superclass
280 fields.addAll(clazz.declaredFields)
284 !Modifier.isStatic(it.modifiers) &&
285 !Modifier.isTransient(it.modifiers)
291 * @return all object methods, including inherited
293 protected Collection<Method> getMethods(obj) {
294 Class clazz = obj.getClass()
295 Collection<Method> methods = clazz.declaredMethods
296 while(clazz.superclass != null) {
297 clazz = clazz.superclass
298 methods.addAll(clazz.declaredMethods)
302 !Modifier.isStatic(it.modifiers) &&
303 !Modifier.isTransient(it.modifiers)
308 * For implement in successors
312 protected void testCreatedEntity(Object entity) {
317 * For implement in successors
321 protected void testUpdatedEntity(Object entity) {
326 * For implement in successors
330 protected void testRemovedEntity(Object entity) {
335 * @return an {@link AbstractEntity} successor
337 abstract protected Class getEntityClass()