com.evasion.framework.test.BestPracticeTesterUtils Maven / Gradle / Ivy
/*
* Document confidentiel - Diffusion interdite
*/
package com.evasion.framework.test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import junit.framework.Assert;
import org.apache.commons.lang.StringUtils;
/**
* Class Utils for BestPractice.
*
*/
public final class BestPracticeTesterUtils {
/**
*
* Constructeur par d�faut d'une classe utile.
*
*/
private BestPracticeTesterUtils() {
}
/**
*
* Test la protection des propri�t�s de type
* java.util.Collection
(voir
* {@link org.apache.commons.collections.collection.UnmodifiableCollection}.
* BEST PRACTICE:
* 1 - un getter de collection ne peut renvoyer null;
* 2 - les m�thodes add et remove de l'instance de collection retourn� par
* le getter doivent renvoyer une erreur de type
* {@link UnsupportedOperationException}
*
* @param bean
* objet � tester;
* @param propertyName
* nom de la propri�t� de type java.util.Collection
* � tester;
*/
@SuppressWarnings("unchecked")
public static void testCollectionProtection(final Object bean,
final String propertyName) {
final String internalPropertyName = propertyName + "Internal";
Method getterInternal = ReflectionUtils.findGetter(bean,
internalPropertyName);
Method setterInternat = ReflectionUtils.findSetter(bean,
internalPropertyName);
if (getterInternal == null
|| Modifier.isPublic(getterInternal.getModifiers())
|| !Collection.class.isAssignableFrom(getterInternal.getReturnType())
|| setterInternat == null
|| Modifier.isPublic(setterInternat.getModifiers())
|| (setterInternat.getParameterTypes().length != 1)
|| !Collection.class.isAssignableFrom(setterInternat.getParameterTypes()[0])) {
Assert.fail("No accessor internal has found or not protected");
}
Method getter = ReflectionUtils.findGetter(bean, propertyName);
if (getter == null || !Modifier.isPublic(getter.getModifiers())
|| !Collection.class.isAssignableFrom(getter.getReturnType())) {
Assert.fail("external getter has not found");
}
Method setter = ReflectionUtils.findSetter(bean, propertyName);
if (setter != null) {
Assert.fail("external Setter has found");
}
try {
final Collection