src/test/java/ru/indvdum/jpa/tests/AbstractJPAEntityTest.groovy
changeset 20 a05948e9458c
parent 15 7d8a7e7635d2
equal deleted inserted replaced
19:39381427da3f 20:a05948e9458c
    15 import javax.persistence.Transient
    15 import javax.persistence.Transient
    16 
    16 
    17 import org.junit.Test
    17 import org.junit.Test
    18 
    18 
    19 import ru.indvdum.jpa.dao.JPADataAccessObject
    19 import ru.indvdum.jpa.dao.JPADataAccessObject
    20 import ru.indvdum.jpa.entities.AbstractEntity;
    20 import ru.indvdum.jpa.entities.AbstractEntity
    21 
    21 
    22 
    22 
    23 /**
    23 /**
    24  * JUnit test case for testing of a creating, listing, updating and removing 
    24  * JUnit test case for testing of a creating, listing, updating and removing 
    25  * operations with database of the JPA entities.
    25  * operations with database of the JPA entities.
    98 	 * 
    98 	 * 
    99 	 * @param entityClass
    99 	 * @param entityClass
   100 	 * @return created entity object
   100 	 * @return created entity object
   101 	 */
   101 	 */
   102 	protected Object createEntity(Class entityClass) {
   102 	protected Object createEntity(Class entityClass) {
       
   103 		if (entityClass.isInterface())
       
   104 			return null;
   103 		assertNotNull entityClass.annotations.find {it instanceof Entity}
   105 		assertNotNull entityClass.annotations.find {it instanceof Entity}
   104 		def entity = entityClass.newInstance()
   106 		def entity = entityClass.newInstance()
   105 		assert entity.class == entityClass
   107 		assert entity.class == entityClass
   106 		toRemove.add(entity)
   108 		toRemove.add(entity)
   107 		if (!existedEntitiesCount.containsKey(entityClass))
   109 		if (!existedEntitiesCount.containsKey(entityClass))
   165 		} else if(Number.class.isAssignableFrom(type)) {
   167 		} else if(Number.class.isAssignableFrom(type)) {
   166 			// trying to use constructor with int argument
   168 			// trying to use constructor with int argument
   167 			newValue = type.newInstance(uniqueValue++ % Byte.MAX_VALUE + 1i)
   169 			newValue = type.newInstance(uniqueValue++ % Byte.MAX_VALUE + 1i)
   168 		} else if(type == String.class) {
   170 		} else if(type == String.class) {
   169 			newValue = (String) "test${uniqueValue++}"
   171 			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
   172 		} else if(type instanceof Class && ((type as Class).isInterface() || (type as Class).annotations.find {it instanceof Entity} != null)) { // modifying of a primary keys is deprecated
   171 			// an attempt to use already created entities
   173 			// an attempt to use already created entities
   172 			def currentValue = getFieldValue(entity, field)
   174 			def currentValue = getFieldValue(entity, field)
   173 			newValue = toRemove.find {it.class == type && it != currentValue}
   175 			newValue = toRemove.find {it.class.isAssignableFrom(type) && it != currentValue}
   174 			if(newValue == null)
   176 			if(newValue == null)
   175 				newValue = createEntity(type as Class)
   177 				newValue = createEntity(type as Class)
   176 		} else if(Enum.class.isAssignableFrom(type)) {
   178 		} else if(Enum.class.isAssignableFrom(type)) {
   177 			def values = type.values();
   179 			def values = type.values();
   178 			newValue = values[uniqueValue++ % values.size()];
   180 			newValue = values[uniqueValue++ % values.size()];