All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.evasion.framework.test.BestPracticeTesterUtils Maven / Gradle / Ivy

There is a newer version: 2.0.0.2
Show newest version
/*
 * 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 result = (Collection) getter.invoke(bean); if (result == null) { Assert.fail("Collection getter can not return null"); } result.add(new Object()); result.remove(new Object()); Assert.fail("Getter is not protected by UnmodifiableCollection decorator '" + propertyName + "' \n"); } catch (IllegalArgumentException e) { Assert.fail("Wrong argument while invoking getter or setter for property '" + propertyName + "'\n" + e.getMessage()); } catch (IllegalAccessException e) { Assert.fail("Unable to invoke getter or setter, are they accessible ? \n" + e.getMessage()); } catch (InvocationTargetException e) { Assert.fail("Error while invoking getter or setter for property '" + propertyName + "' \n" + e.getMessage()); } catch (UnsupportedOperationException e) { Assert.assertNotNull(e); } } /** * * Test la protection des propri�t�s de type * java.util.Date.
* BEST PRACTICE:
* 1 - l'objet getter ne retourne pas la m�me instance d'objet que celle pass� en param�tre du setter. * * @param bean * objet � tester; * @param propertyName * nom de la propri�t� de type java.util.Date � * tester; */ public static void testDateProtection(final Object bean, final String propertyName) { if (StringUtils.isBlank(propertyName)) { throw new IllegalArgumentException( "Property 'propertyName' can not be null \n"); } final Method getter = ReflectionUtils.findGetter(bean, propertyName); if (getter == null) { Assert.fail("Missing getter for property '" + propertyName + "'"); } final Method setter = ReflectionUtils.findSetter(bean, propertyName); if (setter == null) { Assert.fail("Missing setter for property '" + propertyName + "'"); } final Method getterInternal = ReflectionUtils.findGetter(bean, propertyName + "Internal"); if (getter == null) { Assert.fail("Missing getter for property '" + propertyName + "'"); } final Method setterInternal = ReflectionUtils.findSetter(bean, propertyName + "Internal"); if (setter == null) { Assert.fail("Missing setter for property '" + propertyName + "'"); } try { ReflectionUtils.makeAccessible(getter); ReflectionUtils.makeAccessible(setter); if (getterInternal != null) { ReflectionUtils.makeAccessible(getterInternal); } if (setterInternal != null) { ReflectionUtils.makeAccessible(setterInternal); } Date value1 = new Date(); setter.invoke(bean, value1); final Object result = getter.invoke(bean); Assert.assertNotSame(value1, result); if (getterInternal != null) { final Object val1 = getterInternal.invoke(bean); Date value2 = new Date(); setter.invoke(bean, value2); if (val1 == getterInternal.invoke(bean)) { Assert.fail("Calendar can not be equal(property: " + propertyName + "Internal"); } } } catch (IllegalArgumentException e) { Assert.fail("Wrong argument while invoking getter or setter for property '" + propertyName + "'\n" + e.getMessage()); } catch (IllegalAccessException e) { Assert.fail("Unable to invoke getter or setter, are they accessible ? \n" + e.getMessage()); } catch (InvocationTargetException e) { Assert.fail("Error while invoking getter or setter for property '" + propertyName + "' \n" + e.getMessage()); } } }