This is the codeAbility Sharing Platform! Learn more about the codeAbility Sharing Platform.

Skip to content
Snippets Groups Projects
Commit 0f5b141d authored by Michael Breu's avatar Michael Breu :speech_balloon:
Browse files

Noch bessere Tests :-)

parent 355a0387
1 merge request!17Initial Merge to Prepare Release 1.0.0
......@@ -4,6 +4,7 @@ import java.lang.reflect.InvocationTargetException;
import at.ac.uibk.gitsearch.service.MailService;
import at.ac.uibk.gitsearch.testingUtilities.AbstractBeansTestInfrastructure;
import at.ac.uibk.gitsearch.testingUtilities.PropertiesTester;
/**
* Integration tests for {@link MailService}.
......@@ -14,6 +15,10 @@ public class UserProvidedMetadataDTOTest {
public void testUserProvidedMetadata() throws IllegalAccessException, InvocationTargetException {
UserProvidedMetadataDTO cd = new UserProvidedMetadataDTO();
AbstractBeansTestInfrastructure.testGetterAndSetter(cd);
PropertiesTester pt = new PropertiesTester();
pt.testProperties(UserProvidedMetadataDTO.class);
}
}
package at.ac.uibk.gitsearch.testingUtilities;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Assert;
import org.springframework.stereotype.Controller;
/**
* tests all getters and setters of a bean.
* @author arctis Softwaretechnologie Team
*
*/
@Controller
public class PropertiesTester {
private static final Logger LOGGER = LogManager.getLogger(PropertiesTester.class);
/**
*
* @param beanClass the class to test
* @param setterExceptions the names of setters that should not be tested.
*/
public void testProperties(Class<?> beanClass, String... setterExceptions) {
// first try to instantiate bean;
Object bean = null;
try {
bean = beanClass.getConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
LOGGER.info("Cannot instantiate " + beanClass.getCanonicalName(), e);
return;
}
testSetters(beanClass, bean, setterExceptions);
}
/**
* just tests all setters on bean
* @param beanClass
* @param bean
* @param setterExceptions
*/
public void testSetters(Class<?> beanClass, Object bean, String... setterExceptions) {
final Method[] methods = beanClass.getMethods();
for(Method method: methods) {
if(method.getName().startsWith("set")) {
if(!Arrays.stream(setterExceptions).anyMatch(noTest -> method.getName().equals(noTest)))
testSetter(beanClass, bean, method);
}
}
// finally test toString
String stringRepresentation = bean.toString();
Assert.assertNotNull(stringRepresentation);
}
public void testSetter(Class<?> beanClass, Object bean, Method setter) {
if(setter.getParameterCount()!=1) {
return;
}
Parameter p = setter.getParameters()[0];
String propertyName = setter.getName().substring(3);
Class<?> type = p.getType();
Method getter = null;
Object property = null;
if(Boolean.class.equals(type) || boolean.class.equals(type) ) {
try {
getter = beanClass.getMethod("is"+propertyName);
property = Boolean.TRUE;
} catch (NoSuchMethodException | SecurityException e) {
LOGGER.info("Cannot find getter for " + beanClass.getCanonicalName() + "." + setter.getName(), e);
return;
}
} else {
try {
getter = beanClass.getMethod("get" + propertyName);
if(Integer.class.isAssignableFrom(type) || "int".equals(type.getName())) {
property = new Integer(2343234);
}
else if(Long.class.isAssignableFrom(type) || "long".equals(type.getName())) {
property = new Long(12321332343234L);
}
else if(Byte.class.isAssignableFrom(type) || "byte".equals(type.getName())) {
property = new Byte((byte)22);
}
else if(String.class.isAssignableFrom(type)) {
property = "xyzzy";
}
else if(byte[].class.isAssignableFrom(type)) {
property = new byte[]{3,4};
}
else if(type.isArray()) {
property = Array.newInstance(type.getComponentType(), 5);
}
else if(Timestamp.class.isAssignableFrom(type)) {
property = new Timestamp(System.currentTimeMillis());
} else if(Date.class.isAssignableFrom(type)) {
property = new Date();
} else if(List.class.isAssignableFrom(type)) {
property = new ArrayList<>();
} else if(Enum.class.isAssignableFrom(type)) {
Object o;
try {
o = type.getMethod("values").invoke(type);
if( o.getClass().isArray() && Array.getLength(o)>=0) {
property = Array.get(o, 0);
} else {
LOGGER.warn("Cannot get first element of enumeration {}", type.getName());
property = null;
}
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
LOGGER.info("Cannot instantiate Enum for " + type.getCanonicalName() + "." + setter.getName(), e);
property = null;
}
} else if(Set.class.isAssignableFrom(type)) {
property = Collections.EMPTY_SET;
} else if(type.isInterface()) {
LOGGER.info("Cannot instantiate interface {} in {} for class {}", type.getName(), setter.getName(), bean.getClass().getName());
}
else {
try {
property = type.getConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
LOGGER.info("Cannot instantiate Property for " + beanClass.getCanonicalName() + "." + setter.getName(), e);
property = null;
}
}
} catch (NoSuchMethodException | SecurityException e) {
LOGGER.info("Cannot find getter for " + beanClass.getCanonicalName() + "." + setter.getName(), e);
return;
}
if(getter==null) {
// not getter found
return;
}
try {
setter.invoke(bean, property);
Object result = getter.invoke(bean);
Assert.assertEquals("Testing " + setter.getName() + " of " + beanClass.getCanonicalName(), property, result);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
LOGGER.info("Cannot invoke ..." + beanClass.getCanonicalName() + "." + setter.getName(), e);
return;
}
}
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment